summaryrefslogtreecommitdiff
path: root/objectstore/memory_backend.go
blob: 735a64990c0b4630dc933a55c7080b32ceb08936 (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
59
60
61
// 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) {
	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) {
	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 {
	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
}