blob: 6d842414cfb32c4edd9dd89ef35607fa7e3047d3 [file] [log] [blame]
giolekva892a4e22020-04-27 16:46:22 +04001package chunk
2
3import (
4 "context"
5 "io"
6
7 "github.com/giolekva/pcloud/pfs/api"
8)
9
10type RemoteChunk struct {
11 chunkId string
12 client api.ChunkStorageClient
13}
14
15func (r *RemoteChunk) Stats() (info ChunkInfo, err error) {
16 resp, err := r.client.GetChunkStatus(
17 context.Background(),
18 &api.GetChunkStatusRequest{ChunkId: r.chunkId})
19 if err != nil {
20 return
21 }
22 info = ChunkInfo{
23 resp.Status,
24 int(resp.TotalBytes),
25 int(resp.CommittedBytes)}
26 return
27}
28
29func (r *RemoteChunk) ReaderAt() io.ReaderAt {
30 return &remoteChunkReaderAt{
31 chunkId: r.chunkId,
32 client: r.client}
33}
34
35func (r *RemoteChunk) WriterAt() io.WriterAt {
36 return &remoteChunkWriterAt{
37 chunkId: r.chunkId,
38 client: r.client}
39}
40
41type remoteChunkReaderAt struct {
42 chunkId string
43 client api.ChunkStorageClient
44}
45
46func (c *remoteChunkReaderAt) ReadAt(p []byte, offset int64) (n int, err error) {
47 req := api.ReadChunkRequest{
48 ChunkId: c.chunkId,
49 Offset: int32(offset),
50 NumBytes: int32(len(p))}
51 resp, err := c.client.ReadChunk(context.Background(), &req)
52 if err != nil {
53 return
54 }
55 n = copy(p, resp.Data)
56 return
57}
58
59type remoteChunkWriterAt struct {
60 chunkId string
61 client api.ChunkStorageClient
62}
63
64func (c *remoteChunkWriterAt) WriteAt(p []byte, offset int64) (n int, err error) {
65 req := api.WriteChunkRequest{
66 ChunkId: c.chunkId,
67 Offset: int32(offset),
68 Data: p}
69 resp, err := c.client.WriteChunk(context.Background(), &req)
70 if resp != nil {
71 n = int(resp.BytesWritten)
72 }
73 return
74}