forked from deepfabric/elasticell
/
nemo_engine_data.go
112 lines (94 loc) · 2.92 KB
/
nemo_engine_data.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
107
108
109
110
111
112
// Copyright 2016 DeepFabric, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
// +build freebsd openbsd netbsd dragonfly linux
package nemo
import (
"github.com/201341/elasticell/pkg/util"
gonemo "github.com/deepfabric/go-nemo"
"github.com/pkg/errors"
"golang.org/x/net/context"
)
type nemoDataEngine struct {
db *gonemo.NEMO
limiter *util.Limiter
limiterTargetScan *util.Limiter
}
func newNemoDataEngine(db *gonemo.NEMO, cfg *NemoCfg) DataEngine {
return &nemoDataEngine{
limiter: util.NewLimiter(cfg.LimitConcurrencyWrite),
limiterTargetScan: util.NewLimiter(1),
db: db,
}
}
func (e *nemoDataEngine) RangeDelete(start, end []byte) error {
e.limiter.Wait(context.TODO())
err := e.db.RangeDel(start, end)
e.limiter.Release()
return err
}
func (e *nemoDataEngine) GetTargetSizeKey(startKey []byte, endKey []byte, size uint64) (uint64, []byte, error) {
var currentSize uint64
var targetKey []byte
e.limiterTargetScan.Wait(context.TODO())
it := e.db.NewVolumeIterator(startKey, endKey)
if it.TargetScan(int64(size)) {
targetKey = it.TargetKey()
} else {
currentSize = uint64(it.TotalVolume())
}
it.Free()
e.limiterTargetScan.Release()
return currentSize, targetKey, nil
}
// CreateSnapshot create a snapshot file under the giving path
func (e *nemoDataEngine) CreateSnapshot(path string, start, end []byte) error {
e.limiter.Wait(context.TODO())
err := e.db.RawScanSaveRange(path, start, end, true)
e.limiter.Release()
return err
}
// ApplySnapshot apply a snapshort file from giving path
func (e *nemoDataEngine) ApplySnapshot(path string) error {
e.limiter.Wait(context.TODO())
err := e.db.IngestFile(path)
e.limiter.Release()
return err
}
func (e *nemoDataEngine) ScanIndexInfo(startKey []byte, endKey []byte, skipEmpty bool, handler func(key, idxInfo []byte) error) (cnt int, firstErr error) {
var err error
it := e.db.HmeataScan(startKey, endKey, false, skipEmpty)
for ; it.Valid(); it.Next() {
if err = handler(it.Key(), it.IndexInfo()); err != nil {
if cnt == 0 {
firstErr = err
}
cnt++
}
}
it.Free()
return
}
func (e *nemoDataEngine) SetIndexInfo(key, idxInfo []byte) error {
e.limiter.Wait(context.TODO())
err := e.db.HSetIndexInfo(key, idxInfo)
e.limiter.Release()
return err
}
func (e *nemoDataEngine) GetIndexInfo(key []byte) (idxInfo []byte, err error) {
var ret int
idxInfo, ret, err = e.db.HGetIndexInfo(key)
if err == nil && ret < 0 {
err = errors.Errorf("key %+v doesn't exist", key)
}
return
}