forked from External/grumble
Add a default blobstore to package grumble/blobstore.
This commit is contained in:
parent
d535cbc6fa
commit
4ac0c4c244
4 changed files with 82 additions and 2 deletions
|
|
@ -5,7 +5,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"blobstore"
|
"grumble/blobstore"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
include $(GOROOT)/src/Make.inc
|
include $(GOROOT)/src/Make.inc
|
||||||
|
|
||||||
TARG = blobstore
|
TARG = grumble/blobstore
|
||||||
GOFILES = \
|
GOFILES = \
|
||||||
blobstore.go \
|
blobstore.go \
|
||||||
blobreader.go \
|
blobreader.go \
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -30,6 +31,55 @@ var (
|
||||||
ErrInvalidKey = os.NewError("invalid key")
|
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.
|
// Open an existing, or create a new BlobStore residing at path.
|
||||||
// Path must point to a directory, and must already exist.
|
// Path must point to a directory, and must already exist.
|
||||||
//
|
//
|
||||||
|
|
|
||||||
|
|
@ -205,3 +205,33 @@ func TestReadInvalidKeyNonHex(t *testing.T) {
|
||||||
return
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue