diff options
author | n-peugnet <n.peugnet@free.fr> | 2021-08-26 19:21:46 +0200 |
---|---|---|
committer | n-peugnet <n.peugnet@free.fr> | 2021-08-26 19:21:46 +0200 |
commit | 78251f11c91b2504edfc02b760ef53bd352b856c (patch) | |
tree | 1101f5da1f37597eec68c378ed086022dee5dc59 /chunk.go | |
parent | d25d38b8989787551c8b29500f580ea331afc4e8 (diff) | |
download | dna-backup-78251f11c91b2504edfc02b760ef53bd352b856c.tar.gz dna-backup-78251f11c91b2504edfc02b760ef53bd352b856c.zip |
add SketchChunk function
Diffstat (limited to 'chunk.go')
-rw-r--r-- | chunk.go | 23 |
1 files changed, 19 insertions, 4 deletions
@@ -1,6 +1,7 @@ package main import ( + "bufio" "bytes" "fmt" "io" @@ -14,26 +15,40 @@ type ChunkId struct { Idx uint64 } -func (i *ChunkId) Reader(repo string) io.Reader { +func (i *ChunkId) Reader(repo string) ChunkReader { p := path.Join(repo, fmt.Sprintf(versionFmt, i.Ver), chunksName, fmt.Sprintf(chunkIdFmt, i.Idx)) f, err := os.Open(p) if err != nil { log.Printf("Cannot open chunk %s\n", p) } - return f + return bufio.NewReaderSize(f, chunkSize) +} + +type ChunkReader interface { + io.Reader + io.ByteReader } type Chunk struct { + Repo *Repo Id *ChunkId Value []byte } -func (c *Chunk) Reader(repo string) (io.Reader, error) { +func (c *Chunk) Read(buff []byte) (int, error) { + r, err := c.Reader() + if err != nil { + return 0, err + } + return r.Read(buff) +} + +func (c *Chunk) Reader() (ChunkReader, error) { if c.Value != nil { return bytes.NewReader(c.Value), nil } if c.Id != nil { - return c.Id.Reader(repo), nil + return c.Id.Reader(c.Repo.path), nil } return nil, &ChunkError{"Uninitialized chunk"} } |