diff options
Diffstat (limited to 'objectstore/memory_backend.go')
| -rw-r--r-- | objectstore/memory_backend.go | 48 |
1 files changed, 24 insertions, 24 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 |
