// Basic in-memory object store // // Licensed under CC0 1.0 Universal: // https://creativecommons.org/publicdomain/zero/1.0/legalcode package objectstore import ( "fmt" "sync" ) type memBucket struct { objects map[string]string } type MemObjectStore struct { sync.Mutex buckets map[string]memBucket } func NewMemBackend() *MemObjectStore { os := &MemObjectStore{} os.buckets = make(map[string]memBucket) return os } func (os *MemObjectStore) CreateObject(bucketId string, objectId string, object string) { os.Lock() defer os.Unlock() bucket, ok := os.buckets[bucketId] if !ok { bucket = memBucket{} bucket.objects = make(map[string]string) os.buckets[bucketId] = bucket } bucket.objects[objectId] = object } func (os *MemObjectStore) GetObject(bucketId string, objectId string) (string, error) { os.Lock() defer os.Unlock() bucket, ok := os.buckets[bucketId] if !ok { return "", fmt.Errorf("Bucket not found") } object, ok := bucket.objects[objectId] if !ok { return "", fmt.Errorf("Object not found") } return object, nil } func (os *MemObjectStore) DeleteObject(bucketId string, objectId string) error { os.Lock() defer os.Unlock() bucket, ok := os.buckets[bucketId] if !ok { return fmt.Errorf("Bucket not found") } _, ok = bucket.objects[objectId] if !ok { return fmt.Errorf("Object not found") } delete(bucket.objects, objectId) return nil }