| Giorgi Lekveishvili | b8f089f | 2020-03-18 23:28:12 +0400 | [diff] [blame^] | 1 | package chunk |
| 2 | |
| 3 | import "context" |
| 4 | import "sync" |
| 5 | |
| 6 | import "pcloud/api" |
| 7 | |
| 8 | type ChunkServer struct { |
| 9 | chunks sync.Map |
| 10 | } |
| 11 | |
| 12 | func NewChunkServer() *ChunkServer { |
| 13 | return &ChunkServer{} |
| 14 | } |
| 15 | |
| 16 | func (s *ChunkServer) ListChunks( |
| 17 | ctx context.Context, |
| 18 | request *api.ListChunksRequest) (*api.ListChunksResponse, error) { |
| 19 | resp := api.ListChunksResponse{} |
| 20 | s.chunks.Range(func(k, v interface{}) bool { |
| 21 | resp.ChunkId = append(resp.ChunkId, k.(string)) |
| 22 | return true |
| 23 | }) |
| 24 | return &resp, nil |
| 25 | } |
| 26 | |
| 27 | func (s *ChunkServer) ReadChunk( |
| 28 | ctx context.Context, |
| 29 | request *api.ReadChunkRequest) (*api.ReadChunkResponse, error) { |
| 30 | if data, ok := s.chunks.Load(request.ChunkId); ok { |
| 31 | return &api.ReadChunkResponse{Data: data.([]byte)}, nil |
| 32 | } |
| 33 | return nil, nil |
| 34 | } |
| 35 | |
| 36 | func (s *ChunkServer) StoreChunk( |
| 37 | ctx context.Context, |
| 38 | request *api.StoreChunkRequest) (*api.StoreChunkResponse, error) { |
| 39 | s.chunks.Store(request.ChunkId, request.Data) |
| 40 | return &api.StoreChunkResponse{}, nil |
| 41 | } |