summaryrefslogtreecommitdiff
path: root/objectstore/memory_backend.go
diff options
context:
space:
mode:
authorPablo M. Bermudo Garay <pablombg@gmail.com>2022-02-09 23:04:16 +0100
committerPablo M. Bermudo Garay <pablombg@gmail.com>2022-02-10 13:14:19 +0100
commita92f55e2ce14ddf3b8d115f3740b0f88aee8b149 (patch)
tree879c7ec454be477761530cc60c8dfe00d177cb45 /objectstore/memory_backend.go
parent3aa4d62526537ec0fe624899a2f05566ea80a198 (diff)
Adapt memory backend to the new interface
The introduction of the file backend required some breaking changes to the interface between the http server and the backends. This commit adapts the memory backend to the new interface.
Diffstat (limited to 'objectstore/memory_backend.go')
-rw-r--r--objectstore/memory_backend.go48
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