// Add adds a Chunk to the Store. See chunks.Store.Add. func (c *InMemory) Add(chunk *chunks.Chunk) (key cas.Key, err error) { key = chunkutil.Hash(chunk) if c.m == nil { c.m = make(map[mapkey][]byte) } c.m[mapkey{key, chunk.Type, chunk.Level}] = chunk.Buf return key, nil }
func TestHashSomeZeroes(t *testing.T) { chunk := &chunks.Chunk{ Type: "testchunk", Level: 42, Buf: []byte{0x00, 0x00, 0x00}, } k := chunkutil.Hash(chunk) if g, e := k.String(), "85edc8cb5137ca38de1351f85216b2c59e4f19bcd334bb9fca26a68dcf606a1a7c9a9c5c445522e068336f98f1729d32a057b6f96dbdc18158558d5b4d518860"; g != e { t.Errorf("wrong key for some zero bytes: %v != %v", g, e) } }
func TestHashEmpty(t *testing.T) { chunk := &chunks.Chunk{ Type: "testchunk", Level: 42, Buf: []byte{}, } k := chunkutil.Hash(chunk) if g, e := k, cas.Empty; g != e { t.Errorf("wrong key for zero chunk: %v != %v", g, e) } }
func (s *storeInKV) Add(chunk *chunks.Chunk) (key cas.Key, err error) { key = chunkutil.Hash(chunk) if key.IsSpecial() { return key, nil } k := makeKey(key, chunk.Type, chunk.Level) err = s.kv.Put(k, chunk.Buf) if err != nil { return cas.Invalid, err } return key, nil }
func (c *hashCommand) Run() error { var buf bytes.Buffer const kB = 1024 const MB = 1024 const Max = 256 * MB n, err := io.CopyN(&buf, os.Stdin, Max) if err != nil && err != io.EOF { return fmt.Errorf("reading standard input: %v", err) } if n == Max { return errors.New("aborting because chunk is unreasonably big") } chunk := &chunks.Chunk{ Type: c.Arguments.Type, Level: c.Arguments.Level, Buf: buf.Bytes(), } key := chunkutil.Hash(chunk) fmt.Printf("%s\n", key) return nil }