"bazil.org/bazil/cas/chunks/chunkutil" ) type mapkey struct { Key cas.Key Type string Level uint8 } // InMemory is a chunks.Store that all chunks in an in-memory map. // It is intended for unit test use only. type InMemory struct { m map[mapkey][]byte } var _ = chunks.Store(&InMemory{}) func (c *InMemory) get(key cas.Key, typ string, level uint8) ([]byte, error) { data := c.m[mapkey{key, typ, level}] return data, nil } // Get fetches a Chunk. See chunks.Store.Get. func (c *InMemory) Get(key cas.Key, typ string, level uint8) (*chunks.Chunk, error) { return chunkutil.HandleGet(c.get, key, typ, level) } // 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 {
package mock import ( "bazil.org/bazil/cas" "bazil.org/bazil/cas/chunks" ) // NeverUsed is a chunks.Store meant for unit tests that don't touch // the CAS, but where the API requires one. type NeverUsed struct{} var _ = chunks.Store(NeverUsed{}) // Get fetches a Chunk. See chunks.Store.Get. func (NeverUsed) Get(key cas.Key, typ string, level uint8) (*chunks.Chunk, error) { panic("NeverUsed.Get was called") } // Add adds a Chunk to the Store. See chunks.Store.Add. func (NeverUsed) Add(chunk *chunks.Chunk) (key cas.Key, err error) { panic("NeverUsed.Add was called") }
// TODO maybe move this into chunks -- but then chunkutil needs to // merge there, too, to avoid an import cycle import ( "bazil.org/bazil/cas" "bazil.org/bazil/cas/chunks" "bazil.org/bazil/cas/chunks/chunkutil" "bazil.org/bazil/kv" ) type storeInKV struct { kv kv.KV } var _ = chunks.Store(&storeInKV{}) func makeKey(key cas.Key, typ string, level uint8) []byte { k := make([]byte, 0, cas.KeySize+len(typ)+1) k = append(k, key.Bytes()...) k = append(k, typ...) k = append(k, level) return k } func (s *storeInKV) get(key cas.Key, type_ string, level uint8) ([]byte, error) { k := makeKey(key, type_, level) data, err := s.kv.Get(k) if err != nil { return nil, err }