summaryrefslogtreecommitdiff
path: root/objectstore/memory_backend.go
blob: a21abca4a47e0e8b195a443464dbfd0935d66bbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Basic in-memory object store
//
// Licensed under CC0 1.0 Universal:
// https://creativecommons.org/publicdomain/zero/1.0/legalcode
package objectstore

import "fmt"

type memBucket struct {
	objects map[string]string
}

type MemObjectStore struct {
	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) {
	if bucket, ok := os.buckets[bucketId]; ok {
		bucket.objects[objectId] = object
	} else {
		bucket := memBucket{}
		bucket.objects = make(map[string]string)

		bucket.objects[objectId] = object
		os.buckets[bucketId] = bucket
	}
}

func (os *MemObjectStore) GetObject(bucketId string, objectId string) (string, error) {
	if bucket, ok := os.buckets[bucketId]; ok {
		if object, ok := bucket.objects[objectId]; ok {
			return object, nil
		} else {
			return "", fmt.Errorf("Object not found")
		}
	} else {
		return "", fmt.Errorf("Bucket not found")
	}
}

func (os *MemObjectStore) DeleteObject(bucketId string, objectId string) error {
	if bucket, ok := os.buckets[bucketId]; ok {
		if _, ok := bucket.objects[objectId]; ok {
			delete(bucket.objects, objectId)
			return nil
		} else {
			return fmt.Errorf("Object not found")
		}
	} else {
		return fmt.Errorf("Bucket not found")
	}
}