forked from lotusdblabs/lotusdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
106 lines (92 loc) · 2.64 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
96
97
98
99
100
101
102
103
104
105
106
package lotusdb
import (
"errors"
"os"
"sync"
"github.com/flower-corp/lotusdb/util"
)
var (
// ErrDefaultCfNil default column family is nil.
ErrDefaultCfNil = errors.New("default column family is nil")
)
// LotusDB provide basic operations for a persistent kv store.
// It`s methods(Put Get Delete) are self explanatory, and executed in default ColumnFamily.
// You can create a custom ColumnFamily by calling method OpenColumnFamily.
type LotusDB struct {
// all column families.
cfs map[string]*ColumnFamily
opts Options
mu sync.RWMutex
}
// Open a new LotusDB instance, actually will just open the default column family.
func Open(opt Options) (*LotusDB, error) {
if !util.PathExist(opt.DBPath) {
if err := os.MkdirAll(opt.DBPath, os.ModePerm); err != nil {
return nil, err
}
}
db := &LotusDB{opts: opt, cfs: make(map[string]*ColumnFamily)}
// load default column family.
if opt.CfOpts.CfName == "" {
opt.CfOpts.CfName = DefaultColumnFamilyName
}
if _, err := db.OpenColumnFamily(opt.CfOpts); err != nil {
return nil, err
}
return db, nil
}
// Close close database.
func (db *LotusDB) Close() error {
for _, cf := range db.cfs {
if err := cf.Close(); err != nil {
return err
}
}
return nil
}
// Sync syncs the content of all column families to disk.
func (db *LotusDB) Sync() error {
for _, cf := range db.cfs {
if err := cf.Sync(); err != nil {
return err
}
}
return nil
}
// Put put to default column family.
func (db *LotusDB) Put(key, value []byte) error {
return db.PutWithOptions(key, value, nil)
}
// PutWithOptions put to default column family with options.
func (db *LotusDB) PutWithOptions(key, value []byte, opt *WriteOptions) error {
columnFamily := db.getColumnFamily(DefaultColumnFamilyName)
if columnFamily == nil {
return ErrDefaultCfNil
}
return columnFamily.PutWithOptions(key, value, opt)
}
// Get get from default column family.
func (db *LotusDB) Get(key []byte) ([]byte, error) {
columnFamily := db.getColumnFamily(DefaultColumnFamilyName)
if columnFamily == nil {
return nil, ErrDefaultCfNil
}
return columnFamily.Get(key)
}
// Delete delete from default column family.
func (db *LotusDB) Delete(key []byte) error {
return db.DeleteWithOptions(key, nil)
}
// DeleteWithOptions delete from default column family with options.
func (db *LotusDB) DeleteWithOptions(key []byte, opt *WriteOptions) error {
columnFamily := db.getColumnFamily(DefaultColumnFamilyName)
if columnFamily == nil {
return ErrDefaultCfNil
}
return columnFamily.DeleteWithOptions(key, opt)
}
func (db *LotusDB) getColumnFamily(cfName string) *ColumnFamily {
db.mu.RLock()
defer db.mu.RUnlock()
return db.cfs[cfName]
}