-
Notifications
You must be signed in to change notification settings - Fork 0
/
hybrid.go
84 lines (67 loc) · 2.64 KB
/
hybrid.go
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package objectstore
import (
"encoding"
"fmt"
"log"
)
// hybridObjectStore is a type implementing the objectstore.ObjectStore interface with a hybrid storage backend.
// It combines an in-memory backend and a persistent backend.
type hybridObjectStore struct {
badgerObjectStore *badgerObjectStore
memObjectStore *memObjectStore
}
// NewHybridObjectStore creates a new ObjectStore instance.
func NewHybridObjectStore(conf Config) (*hybridObjectStore, error) {
badgerObjectStore, err := NewBadgerObjectStore(conf)
if err != nil {
return nil, fmt.Errorf("error while creating BadgerDB ObjectStore in hybrid ObjectStore :%w", err)
}
memObjectStore := NewMemObjectStore()
objstore := &hybridObjectStore{
badgerObjectStore: badgerObjectStore,
memObjectStore: memObjectStore,
}
return objstore, nil
}
func (objstore *hybridObjectStore) Store(objectID string, object encoding.BinaryMarshaler) error {
// no error handling necessary for Store() in-memory ObjectStore
objstore.memObjectStore.Store(objectID, object)
if err := objstore.badgerObjectStore.Store(objectID, object); err != nil {
return fmt.Errorf("error while storing in Hybrid ObjectStore :%w", err)
}
return nil
}
func (objstore *hybridObjectStore) Load(objectID string, object encoding.BinaryUnmarshaler) error {
// attempt to load the object from the in-memory ObjectStore
if err := objstore.memObjectStore.Load(objectID, object); err == nil {
return nil
}
// in-memory ObjectStore failed, attempt to load the object from the persistent ObjectStore
log.Printf("warning: could not load object %s from in-memory ObjectStore\n", objectID)
if err := objstore.badgerObjectStore.Load(objectID, object); err != nil {
log.Printf("error: could not load object %s from persistent ObjectStore: %s\n", objectID, err)
return err
}
// propagate the object to the in-memory ObjectStore
objectToStore, ok := object.(encoding.BinaryMarshaler)
if ok {
objstore.memObjectStore.Store(objectID, objectToStore)
}
log.Printf("warning: could not propagate object %s to in-memory ObjectStore\n", objectID)
return nil
}
func (objstore *hybridObjectStore) IsPresent(objectID string) (bool, error) {
// no error handling necessary for IsPresent() in-memory ObjectStore
if present, _ := objstore.memObjectStore.IsPresent(objectID); present {
return true, nil
}
//log.Printf("warning: object %s was not present in in-memory ObjectStore\n", objectID)
present, err := objstore.badgerObjectStore.IsPresent(objectID)
return present, err
}
func (objstore *hybridObjectStore) Close() error {
if err := objstore.badgerObjectStore.Close(); err != nil {
return err
}
return objstore.memObjectStore.Close()
}