From 54efd345d811437b9d9021970e707fc0c8c8d058 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Tue, 3 May 2011 17:55:57 -0700 Subject: [PATCH] start of blobref.MemoryStore --- lib/go/camli/blobref/fetcher.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) 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 +} +