aboutsummaryrefslogtreecommitdiff
path: root/chunk.go
diff options
context:
space:
mode:
authorn-peugnet <n.peugnet@free.fr>2021-08-26 19:21:46 +0200
committern-peugnet <n.peugnet@free.fr>2021-08-26 19:21:46 +0200
commit78251f11c91b2504edfc02b760ef53bd352b856c (patch)
tree1101f5da1f37597eec68c378ed086022dee5dc59 /chunk.go
parentd25d38b8989787551c8b29500f580ea331afc4e8 (diff)
downloaddna-backup-78251f11c91b2504edfc02b760ef53bd352b856c.tar.gz
dna-backup-78251f11c91b2504edfc02b760ef53bd352b856c.zip
add SketchChunk function
Diffstat (limited to 'chunk.go')
-rw-r--r--chunk.go23
1 files changed, 19 insertions, 4 deletions
diff --git a/chunk.go b/chunk.go
index 321f046..0746fde 100644
--- a/chunk.go
+++ b/chunk.go
@@ -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"}
}