diff options
| -rw-r--r-- | objectstore/memory_backend.go | 48 | ||||
| -rw-r--r-- | objectstore/memory_backend_test.go | 8 |
2 files changed, 28 insertions, 28 deletions
diff --git a/objectstore/memory_backend.go b/objectstore/memory_backend.go index 36ee7b6..3c20e20 100644 --- a/objectstore/memory_backend.go +++ b/objectstore/memory_backend.go @@ -7,7 +7,7 @@ package objectstore import ( "crypto/sha256" "encoding/hex" - "fmt" + "os" "sync" ) @@ -29,27 +29,27 @@ type MemObjectStore struct { } func NewMemBackend() *MemObjectStore { - os := &MemObjectStore{} - os.buckets = make(map[string]memBucket) - return os + mos := &MemObjectStore{} + mos.buckets = make(map[string]memBucket) + return mos } -func (os *MemObjectStore) CreateObject(bucketId string, objectId string, payload string) error { - os.Lock() - defer os.Unlock() +func (mos *MemObjectStore) CreateObject(bucketId string, objectId string, payload []byte) error { + mos.Lock() + defer mos.Unlock() // Create bucket if it doesn't exist - bucket, ok := os.buckets[bucketId] + bucket, ok := mos.buckets[bucketId] if !ok { bucket = memBucket{} bucket.objects = make(map[string]memObject) bucket.hashes = make(map[string]string) - os.buckets[bucketId] = bucket + mos.buckets[bucketId] = bucket } // Hash the object content hasher := sha256.New() - hasher.Write([]byte(payload)) + hasher.Write(payload) hash := hex.EncodeToString(hasher.Sum(nil)) // Check for duplicates @@ -59,7 +59,7 @@ func (os *MemObjectStore) CreateObject(bucketId string, objectId string, payload // Store the object object := memObject{ - payload: payload, + payload: string(payload), hash: hash, } bucket.objects[objectId] = object @@ -67,35 +67,35 @@ func (os *MemObjectStore) CreateObject(bucketId string, objectId string, payload return nil } -func (os *MemObjectStore) GetObject(bucketId string, objectId string) (string, error) { - os.Lock() - defer os.Unlock() +func (mos *MemObjectStore) GetObject(bucketId string, objectId string) (string, error) { + mos.Lock() + defer mos.Unlock() - bucket, ok := os.buckets[bucketId] + bucket, ok := mos.buckets[bucketId] if !ok { - return "", fmt.Errorf("Bucket not found") + return "", os.ErrNotExist } object, ok := bucket.objects[objectId] if !ok { - return "", fmt.Errorf("Object not found") + return "", os.ErrNotExist } return object.payload, nil } -func (os *MemObjectStore) DeleteObject(bucketId string, objectId string) error { - os.Lock() - defer os.Unlock() +func (mos *MemObjectStore) DeleteObject(bucketId string, objectId string) error { + mos.Lock() + defer mos.Unlock() - bucket, ok := os.buckets[bucketId] + bucket, ok := mos.buckets[bucketId] if !ok { - return fmt.Errorf("Bucket not found") + return os.ErrNotExist } object, ok := bucket.objects[objectId] if !ok { - return fmt.Errorf("Object not found") + return os.ErrNotExist } // Delete both, object and hash @@ -104,7 +104,7 @@ func (os *MemObjectStore) DeleteObject(bucketId string, objectId string) error { // Delete the bucket if it's empty if len(bucket.objects) == 0 { - delete(os.buckets, bucketId) + delete(mos.buckets, bucketId) } return nil diff --git a/objectstore/memory_backend_test.go b/objectstore/memory_backend_test.go index 3fbe7a5..c50d97f 100644 --- a/objectstore/memory_backend_test.go +++ b/objectstore/memory_backend_test.go @@ -15,7 +15,7 @@ func TestCreateAndGet(t *testing.T) { // Create the object bucketId := "foo" objectId := "bar" - objectContent := "Lorem ipsum" + objectContent := []byte("Lorem ipsum") objects.CreateObject(bucketId, objectId, objectContent) // Retrieve the object @@ -25,7 +25,7 @@ func TestCreateAndGet(t *testing.T) { } // Check the object content - if object != objectContent { + if object != string(objectContent) { t.Errorf("Check test object. Got (%v), want (%v)", object, objectContent) } @@ -42,7 +42,7 @@ func TestCreateAndDelete(t *testing.T) { // Create the object bucketId := "foo" objectId := "bar" - objectContent := "Lorem ipsum" + objectContent := []byte("Lorem ipsum") objects.CreateObject(bucketId, objectId, objectContent) // Try to delete a non-existent object @@ -77,7 +77,7 @@ func TestDuplicatesDetection(t *testing.T) { // Create an object bucketId := "foo" objectId := "bar" - objectContent := "Lorem ipsum" + objectContent := []byte("Lorem ipsum") objects.CreateObject(bucketId, objectId, objectContent) // Error type reference |
