diff --git a/lib/go/camli/blobref/fetcher.go b/lib/go/camli/blobref/fetcher.go index ca6a0dc68..f5dfcfe34 100644 --- a/lib/go/camli/blobref/fetcher.go +++ b/lib/go/camli/blobref/fetcher.go @@ -17,12 +17,16 @@ limitations under the License. package blobref import ( + "crypto" "fmt" "io" + "log" "os" "path/filepath" ) +var _ = log.Printf + type Fetcher interface { // Fetch returns a blob. If the blob is not found then // os.ENOENT should be returned for the error (not a wrapped @@ -105,3 +109,22 @@ func (df *DirFetcher) Fetch(b *BlobRef) (file ReadSeekCloser, size int64, err os size = stat.Size return } + +// MemoryStore stores blobs in memory and is a Fetcher and +// StreamingFetcher. Its zero value is usable. +type MemoryStore struct { + m map[string][]byte +} + +func (s *MemoryStore) AddBlob(hashtype crypto.Hash, data []byte) os.Error { + if hashtype != crypto.SHA1 { + return os.NewError("blobref: unsupported hash type") + } + hash := hashtype.New() + hash.Write(data) + bstr := fmt.Sprintf("sha1-%x", hash.Sum()) + s.m[bstr] = data + log.Printf("added %s", bstr) + return nil +} +