Passed
Pull Request — master (#1470)
by Tolga
02:39
created

memory.*Memory.Close   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 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