blob: c35d761d29e98d9943c2bddc5b21f9bf78a3e8fc [file] [log] [blame]
Giorgi Lekveishvilib8f089f2020-03-18 23:28:12 +04001package chunk
2
3import "context"
4import "sync"
5
6import "pcloud/api"
7
8type ChunkServer struct {
9 chunks sync.Map
10}
11
12func NewChunkServer() *ChunkServer {
13 return &ChunkServer{}
14}
15
16func (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
27func (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
36func (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}