diff --git a/grumble.go b/grumble.go index 3fafa6b..45a4814 100644 --- a/grumble.go +++ b/grumble.go @@ -5,7 +5,7 @@ package main import ( - "blobstore" + "grumble/blobstore" "flag" "fmt" "os" diff --git a/pkg/blobstore/Makefile b/pkg/blobstore/Makefile index 62ad8a0..0487605 100644 --- a/pkg/blobstore/Makefile +++ b/pkg/blobstore/Makefile @@ -1,6 +1,6 @@ include $(GOROOT)/src/Make.inc -TARG = blobstore +TARG = grumble/blobstore GOFILES = \ blobstore.go \ blobreader.go \ diff --git a/pkg/blobstore/blobstore.go b/pkg/blobstore/blobstore.go index 558db3b..0b914cd 100644 --- a/pkg/blobstore/blobstore.go +++ b/pkg/blobstore/blobstore.go @@ -13,6 +13,7 @@ import ( "path/filepath" "os" "strconv" + "sync" "syscall" ) @@ -30,6 +31,55 @@ var ( ErrInvalidKey = os.NewError("invalid key") ) +var ( + defaultStore *BlobStore + defaultMutex sync.Mutex +) + +// Open an existing, or create a new BlobStore at path. +// Path must point to a directory, and must already exist. +// See NewBlobStore for more information. +func Open(path string, makeall bool) (err os.Error) { + defaultMutex.Lock() + defer defaultMutex.Unlock() + + if defaultStore != nil { + panic("Default BlobStore already open") + } + + defaultStore, err = NewBlobStore(path, makeall) + return +} + +// Close the open default BlobStore. This removes the lockfile allowing +// other processes to open the BlobStore. +func Close() (err os.Error) { + if defaultStore == nil { + panic("DefaultStore not open") + } + + err = defaultStore.Close() + if err != nil { + return + } + + defaultStore = nil + return +} + +// Lookup a blob by its key and return a buffer containing the contents +// of the blob. +func Get(key string) (buf []byte, err os.Error) { + return defaultStore.Get(key) +} + +// Store a blob. If the blob was successfully stored, the returned key +// can be used to retrieve the buf from the BlobStore. +func Put(buf []byte) (key string, err os.Error) { + return defaultStore.Put(buf) +} + + // Open an existing, or create a new BlobStore residing at path. // Path must point to a directory, and must already exist. // diff --git a/pkg/blobstore/blobstore_test.go b/pkg/blobstore/blobstore_test.go index 384ca70..c0fedf3 100644 --- a/pkg/blobstore/blobstore_test.go +++ b/pkg/blobstore/blobstore_test.go @@ -205,3 +205,33 @@ func TestReadInvalidKeyNonHex(t *testing.T) { return } } + +func TestDefaultBlobStore(t *testing.T) { + dir, err := ioutil.TempDir("", "blobstore") + if err != nil { + t.Errorf(err.String()) + return + } + defer os.RemoveAll(dir) + + err = Open(dir, false) + if err != nil { + t.Errorf(err.String()) + } + + data := []byte{0xf, 0x0, 0x0, 0xb, 0xa, 0xf} + + key, err := Put(data) + if err != nil { + t.Errorf(err.String()) + } + + fetchedData, err := Get(key) + if err != nil { + t.Errorf(err.String()) + } + + if !bytes.Equal(fetchedData, data) { + t.Errorf("stored data and retrieved data does not match: %v vs. %v", fetchedData, data) + } +}