summaryrefslogtreecommitdiff
path: root/objectstore
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
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')
-rw-r--r--objectstore/memory_backend.go48
-rw-r--r--objectstore/memory_backend_test.go8
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