| giolekva | 7be17df | 2020-03-21 13:57:02 +0400 | [diff] [blame] | 1 | package chunk |
| 2 | |
| giolekva | c5126d9 | 2020-03-21 16:39:56 +0400 | [diff] [blame^] | 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "io" |
| giolekva | 7be17df | 2020-03-21 13:57:02 +0400 | [diff] [blame] | 7 | |
| giolekva | c5126d9 | 2020-03-21 16:39:56 +0400 | [diff] [blame^] | 8 | "pcloud/api" |
| 9 | ) |
| giolekva | 7be17df | 2020-03-21 13:57:02 +0400 | [diff] [blame] | 10 | |
| 11 | type RemoteChunk struct { |
| 12 | chunkId string |
| 13 | client api.ChunkStorageClient |
| 14 | } |
| 15 | |
| 16 | func (r *RemoteChunk) SizeBytes() int { |
| 17 | return 0 |
| 18 | } |
| 19 | |
| 20 | func (r *RemoteChunk) ReadSeeker() io.ReadSeeker { |
| 21 | return &remoteChunkReadSeeker{ |
| 22 | chunkId: r.chunkId, |
| 23 | client: r.client} |
| 24 | } |
| 25 | |
| 26 | func (r *RemoteChunk) Writer() io.Writer { |
| 27 | return nil |
| 28 | } |
| 29 | |
| 30 | type remoteChunkReadSeeker struct { |
| 31 | chunkId string |
| 32 | client api.ChunkStorageClient |
| 33 | offset int64 |
| 34 | } |
| 35 | |
| 36 | func (c *remoteChunkReadSeeker) Seek(offset int64, whence int) (int64, error) { |
| 37 | if whence != io.SeekStart { |
| 38 | return 0, errors.New("Seek: RemoteChunk only supports SeekStart whence") |
| 39 | } |
| 40 | c.offset = offset |
| 41 | return offset, nil |
| 42 | } |
| 43 | |
| 44 | func (c *remoteChunkReadSeeker) Read(p []byte) (n int, err error) { |
| 45 | req := api.ReadChunkRequest{ |
| 46 | ChunkId: c.chunkId, |
| 47 | Offset: int32(c.offset), // TODO(lekva): must be int64 |
| 48 | NumBytes: int32(len(p))} |
| 49 | resp, err := c.client.ReadChunk(context.Background(), &req) |
| 50 | if err != nil { |
| 51 | return |
| 52 | } |
| 53 | n = copy(p, resp.Data) |
| 54 | c.offset += int64(n) |
| 55 | return |
| 56 | } |
| 57 | |
| 58 | type PrimaryReplicaChunk struct { |
| 59 | chunkId string |
| 60 | } |