-
Notifications
You must be signed in to change notification settings - Fork 0
/
in_memory_database_table.go
102 lines (83 loc) · 2.51 KB
/
in_memory_database_table.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package database
import (
"fmt"
. "github.com/go-yaaf/yaaf-common/entity"
)
// ITable is a database table interface
type ITable interface {
// Get single entity by ID
Get(entityID string) (entity Entity, err error)
// Exists checks if entity exists by ID
Exists(entityID string) (result bool, err error)
// Insert entity
Insert(entity Entity) (added Entity, err error)
// Update entity
Update(entity Entity) (added Entity, err error)
// Upsert update entity or insert if not found
Upsert(entity Entity) (added Entity, err error)
// Delete entity
Delete(entityID string) (err error)
// Table get access to the underlying data structure
Table() (result map[string]Entity)
}
// In memory table implementation --------------------------------------------------------------------------------------
// InMemoryTable represents a table in the DB
type InMemoryTable struct {
table map[string]Entity
}
// NewInMemTable factory method
func NewInMemTable() ITable {
return &InMemoryTable{table: make(map[string]Entity)}
}
// Get single entity by ID
func (tbl *InMemoryTable) Get(entityID string) (entity Entity, err error) {
if ent, ok := tbl.table[entityID]; ok {
return ent, nil
} else {
return nil, fmt.Errorf("item not found")
}
}
// Exists checks if entity exists by ID
func (tbl *InMemoryTable) Exists(entityID string) (result bool, err error) {
_, ok := tbl.table[entityID]
return ok, nil
}
// Insert entity
func (tbl *InMemoryTable) Insert(entity Entity) (added Entity, err error) {
entityID := fmt.Sprintf("%v", entity.ID())
if _, ok := tbl.table[entityID]; ok {
return nil, fmt.Errorf("item exists")
} else {
tbl.table[entityID] = entity
return entity, nil
}
}
// Update entity
func (tbl *InMemoryTable) Update(entity Entity) (added Entity, err error) {
entityID := fmt.Sprintf("%v", entity.ID())
if _, ok := tbl.table[entityID]; ok {
tbl.table[entityID] = entity
return entity, nil
} else {
return nil, fmt.Errorf("item not exists")
}
}
// Upsert update entity or insert if not found
func (tbl *InMemoryTable) Upsert(entity Entity) (added Entity, err error) {
entityID := fmt.Sprintf("%v", entity.ID())
tbl.table[entityID] = entity
return entity, nil
}
// Delete entity
func (tbl *InMemoryTable) Delete(entityID string) (err error) {
if _, ok := tbl.table[entityID]; ok {
delete(tbl.table, entityID)
return nil
} else {
return fmt.Errorf("item not found")
}
}
// Table get access to the underlying data structure
func (tbl *InMemoryTable) Table() (result map[string]Entity) {
return tbl.table
}