Total Lines | 64 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | package memory |
||
2 | |||
3 | import ( |
||
4 | "context" |
||
5 | "sync" |
||
6 | |||
7 | "github.com/hashicorp/go-memdb" |
||
8 | ) |
||
9 | |||
10 | // Memory - Structure for in memory db |
||
11 | type Memory struct { |
||
12 | sync.RWMutex |
||
13 | rid uint64 |
||
14 | aid uint64 |
||
15 | |||
16 | DB *memdb.MemDB |
||
17 | } |
||
18 | |||
19 | // New - Creates new database schema in memory |
||
20 | func New(schema *memdb.DBSchema) (*Memory, error) { |
||
21 | db, err := memdb.NewMemDB(schema) |
||
22 | return &Memory{ |
||
23 | DB: db, |
||
24 | }, err |
||
25 | } |
||
26 | |||
27 | func (m *Memory) RelationTupleID() (id uint64) { |
||
28 | m.Lock() |
||
29 | defer m.Unlock() |
||
30 | if m.rid == 0 { |
||
31 | m.rid++ |
||
32 | } |
||
33 | id = m.rid |
||
34 | m.rid++ |
||
35 | return |
||
36 | } |
||
37 | |||
38 | func (m *Memory) AttributeID() (id uint64) { |
||
39 | m.Lock() |
||
40 | defer m.Unlock() |
||
41 | if m.aid == 0 { |
||
42 | m.aid++ |
||
43 | } |
||
44 | id = m.aid |
||
45 | m.aid++ |
||
46 | return |
||
47 | } |
||
48 | |||
49 | // GetEngineType - Gets engine type, returns as string |
||
50 | func (m *Memory) GetEngineType() string { |
||
51 | return "memory" |
||
52 | } |
||
53 | |||
54 | // Close - Closing the in memory instance |
||
55 | func (m *Memory) Close() error { |
||
56 | m.Lock() |
||
57 | defer m.Unlock() |
||
58 | m.DB = nil |
||
59 | return nil |
||
60 | } |
||
61 | |||
62 | // IsReady - Check if database is ready |
||
63 | func (m *Memory) IsReady(_ context.Context) (bool, error) { |
||
64 | return true, nil |
||
65 | } |
||
66 |