-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
95 lines (74 loc) · 1.6 KB
/
db.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
package pangolin
import (
"errors"
"reflect"
"github.com/google/uuid"
)
type Option struct {
UUID string
Engine string
EngineOption interface{}
}
func DefaultOption(uuid string) *Option {
return &Option{
UUID: uuid,
Engine: "lsm",
}
}
type DB struct {
Option *Option
UUID uuid.UUID
engine Engine
}
func OpenDB(option *Option) (*DB, error) {
if option == nil {
return nil, errors.New("please provide options")
}
UUID, err := uuid.Parse(option.UUID)
if err != nil {
return nil, errors.New("please provide the correct uuid")
}
engine, err := engines[option.Engine](UUID, option.EngineOption)
if err != nil {
return nil, err
}
return &DB{
option,
UUID,
engine,
}, nil
}
func (db *DB) Insert(time int64, value interface{}) error {
var t ValueType
switch reflect.TypeOf(value).Kind() {
case reflect.Int, reflect.Int64:
t = IntType
case reflect.Float32, reflect.Float64:
t = FloatType
default:
t = StringType
}
return db.engine.Insert(&Entry{KV: KV{Key: time, Value: value}, Type: t})
}
func (db *DB) InsertEntry(e *Entry) error {
if e.Value == nil {
return errors.New("value can't be nil")
}
return db.engine.Insert(e)
}
func (db *DB) GetRange(startTime, endTime int64, filter *QueryFilter) ([]KV, error) {
return db.engine.GetRange(startTime, endTime, filter)
}
func (db *DB) Get(key int64, filter *QueryFilter) (interface{}, error) {
result, err := db.engine.GetRange(key, key+1, filter)
if err != nil {
return nil, err
}
return result[0], nil
}
func (db *DB) Engine() Engine {
return db.engine
}
func (db *DB) Close() error {
return db.engine.Close()
}