forked from sitanshunanda/shield
-
Notifications
You must be signed in to change notification settings - Fork 1
/
leveldb.go
180 lines (167 loc) · 4.19 KB
/
leveldb.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package shield
import (
"strconv"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/util"
)
// LevelDBStore store
type LevelDBStore struct {
filePath string
leveldb *leveldb.DB
}
// NewLevelDBStore - new redis store
func NewLevelDBStore(pathToDB string) Store {
return &LevelDBStore{
filePath: pathToDB,
}
}
func (ls *LevelDBStore) conn() (*leveldb.DB, error) {
if ls.leveldb == nil {
db, err := leveldb.OpenFile(ls.filePath, nil)
if err != nil {
return nil, err
}
ls.leveldb = db
}
return ls.leveldb, nil
}
// Classes - list classes
func (ls *LevelDBStore) Classes() (a []string, err error) {
db, err := ls.conn()
if err != nil {
return
}
iter := db.NewIterator(util.BytesPrefix([]byte("cs-")), nil)
for iter.Next() {
name := string(iter.Key())
if len(name) > 3 {
a = append(a, name[3:])
}
}
return
}
// AddClass - add class
func (ls *LevelDBStore) AddClass(class string) (err error) {
db, err := ls.conn()
if err != nil {
return
}
if class == "" {
panic("invalid class: " + class)
}
err = db.Put([]byte("cs-"+class), []byte("1"), nil)
return
}
// ClassWordCounts - count words by class
func (ls *LevelDBStore) ClassWordCounts(class string, words []string) (mc map[string]int64, err error) {
db, err := ls.conn()
if err != nil {
return
}
mc = make(map[string]int64)
for _, w := range words {
key := "cl-"+class+"-"+w
val, _ := db.Get([]byte(key), nil)
if val != nil {
count, e := strconv.ParseInt(string(val),10,64)
if e == nil {
mc[w] = count
} else {
mc[w] = 0
}
} else {
mc[w] = 0
}
}
return
}
// IncrementClassWordCounts - increment vals
func (ls *LevelDBStore) IncrementClassWordCounts(m map[string]map[string]int64) (err error) {
db, err := ls.conn()
if err != nil {
return
}
type tuple struct {
word string
d int64
}
for class, words := range m {
for word, d := range words {
key := "cl-"+class+"-"+word
d = appendIntValue(db, key, d, true)
appendIntValue(db, "sum-"+class, d, true)
}
}
return
}
// TotalClassWordCounts - count words in classes
func (ls *LevelDBStore) TotalClassWordCounts() (m map[string]int64, err error) {
db, err := ls.conn()
if err != nil {
return
}
m = make(map[string]int64)
iter := db.NewIterator(util.BytesPrefix([]byte("sum-")), nil)
for iter.Next() {
name := string(iter.Key())
if len(name) > 4 {
count, e := strconv.ParseInt(string(iter.Value()),10,64)
if e == nil {
m[name[4:]] = count
} else {
m[name[4:]] = 0
}
}
}
return
}
// Reset - clean store
func (ls *LevelDBStore) Reset() (err error) {
db, err := ls.conn()
if err != nil {
return
}
batch := new(leveldb.Batch)
iter := db.NewIterator(nil, nil)
for iter.Next() {
batch.Delete(iter.Key())
}
db.Write(batch, nil)
return
}
// Close - close connection
func (ls *LevelDBStore) Close() error {
if ls.leveldb != nil {
err := ls.leveldb.Close()
if err != nil {
return err
}
ls.leveldb = nil
}
return nil
}
func appendIntValue(db *leveldb.DB, key string, d int64, minusTest bool) int64 {
if db != nil {
val, e := db.Get([]byte(key), nil)
if e != nil {
if d > 0 {
db.Put([]byte(key), []byte(strconv.FormatInt(d,10)), nil)
}
} else {
v, e := strconv.ParseInt(string(val),10,64)
if e != nil {
if d > 0 {
db.Put([]byte(key), []byte(strconv.FormatInt(d,10)), nil)
}
} else {
if minusTest {
if (v+d) < 0 {
d = v * -1
}
}
db.Put([]byte(key), []byte(strconv.FormatInt(v+d,10)), nil)
}
}
}
return d
}