/
editing.go
291 lines (232 loc) · 6.34 KB
/
editing.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package db
import (
"errors"
"github.com/ShoshinNikita/bolt"
)
var (
ErrNeedWriteMode = errors.New("Need WriteMode. Mode of the db is ReadOnly")
)
// Structures for saving data of a bucket in memory
type record struct {
k []byte
v []byte
}
type bucket struct {
k []byte
nextLevel *data
}
type data struct {
records []record
buckets []bucket
}
func (d *data) addRecord(k, v []byte) {
r := record{k, v}
d.records = append(d.records, r)
}
func (d *data) addBucket(k []byte, pointer *data) {
b := bucket{k, pointer}
d.buckets = append(d.buckets, b)
}
// End of structures declaration
// AddBucket adds a new bucket.
// Function returns an error if:
// * the bucket already exists - "bucket already exists"
// * there's a record with same key - "it's a record"
func (db *BoltAPI) AddBucket(bucketName string) (err error) {
if db.ReadOnly {
return ErrNeedWriteMode
}
err = db.db.Update(func(tx *bolt.Tx) error {
b := db.getCurrentBucket(tx)
// Bucket already exists
if b.Bucket([]byte(bucketName)) != nil {
return errors.New("bucket already exists")
}
// There's a record with same key
if b.Get([]byte(bucketName)) != nil {
return errors.New("it's a record")
}
_, err := b.CreateBucket([]byte(bucketName))
return err
})
if err == nil {
db.recordsAmount++
}
return err
}
// DeleteBucket deletes a bucket.
// Function returns an error if:
// * it's a record, not bucket - "it's a record"
// * there's no such bucket - "there's no such bucket"
func (db *BoltAPI) DeleteBucket(key string) (err error) {
if db.ReadOnly {
return ErrNeedWriteMode
}
err = db.db.Update(func(tx *bolt.Tx) error {
b := db.getCurrentBucket(tx)
// It's a record, not bucket
if b.Get([]byte(key)) != nil {
return errors.New("it's a record")
}
// There's no such bucket
if b.Bucket([]byte(key)) == nil {
return errors.New("there's no such bucket")
}
return b.DeleteBucket([]byte(key))
})
if err == nil {
db.recordsAmount--
}
return err
}
// EditBucketName renames buckets
// Process:
// * firstly, data of a bucket is copied to memory (it is a tree);
// * secondary, bucket with old name is deleted, bucket with new name is created;
// * thirdly, the data from memory is copied to the new bucket.
// Copying works recursively.
func (db *BoltAPI) EditBucketName(oldKey, newKey string) (err error) {
if db.ReadOnly {
return ErrNeedWriteMode
}
currentData := new(data)
return db.db.Update(func(tx *bolt.Tx) error {
b := db.getCurrentBucket(tx)
// Check is there a bucket with newKey
if temp := b.Bucket([]byte(newKey)); temp != nil {
return errors.New("Bucket " + newKey + " already exists")
}
oldBucket := b.Bucket([]byte(oldKey))
// Copy data from a db to memory
copyDataToMemory(currentData, oldBucket)
newBucket, err := b.CreateBucket([]byte(newKey))
if err != nil {
return err
}
// Delete old bucket and create new with refreshed name
b.DeleteBucket([]byte(oldKey))
// Copy data to the db
copyDataToDB(currentData, newBucket)
return nil
})
}
func copyDataToMemory(d *data, bucket *bolt.Bucket) {
bucket.ForEach(func(k, v []byte) error {
if v != nil {
// record
d.addRecord(k, v)
} else {
// bucket
// Create a new element
ptr := new(data)
d.addBucket(k, ptr)
// Go to the nested bucket
nestedBucket := bucket.Bucket(k)
copyDataToMemory(ptr, nestedBucket)
}
return nil
})
}
func copyDataToDB(d *data, bucket *bolt.Bucket) {
// Checking just in case
if d == nil {
return
}
// Add records
for _, r := range d.records {
bucket.Put(r.k, r.v)
}
// Add buckets
for _, b := range d.buckets {
newBucket, _ := bucket.CreateBucket(b.k)
copyDataToDB(b.nextLevel, newBucket)
}
}
// AddRecord adds a new record.
// Function returns an error if:
// * there's a bucket with same key - "it's a bucket"
// * the record already exists - "record already exists"
func (db *BoltAPI) AddRecord(key, value string) (err error) {
if db.ReadOnly {
return ErrNeedWriteMode
}
err = db.db.Update(func(tx *bolt.Tx) error {
b := db.getCurrentBucket(tx)
// If it is a bucket
if b.Bucket([]byte(key)) != nil {
return errors.New("it's a bucket")
}
// If record exist
if b.Get([]byte(key)) != nil {
return errors.New("record already exists")
}
return b.Put([]byte(key), []byte(value))
})
if err == nil {
db.recordsAmount++
}
return err
}
// DeleteRecord deletes a record
// Function returns an error if:
// * it's a bucket, nor record - "it's a bucket"
// * there's no such record - "there's no such record"
func (db *BoltAPI) DeleteRecord(key string) (err error) {
if db.ReadOnly {
return ErrNeedWriteMode
}
err = db.db.Update(func(tx *bolt.Tx) error {
b := db.getCurrentBucket(tx)
// If it is bucket
if b.Bucket([]byte(key)) != nil {
return errors.New("it's a bucket")
}
// If there's no such record
if b.Get([]byte(key)) == nil {
return errors.New("there's no such record")
}
// If record exists
return b.Delete([]byte(key))
})
if err == nil {
db.recordsAmount--
}
return err
}
// EditRecord edits a record
// Function returns an error if:
// * there's a bucket with key == oldKey - "it's a bucket"
// * there's no record with key == oldKey - "there's no such record"
// * there's a bucket with key == newKey - "there's a bucket with key == newKey"
// * there's a record with key == newKey - "there's a record with key == newKey"
func (db *BoltAPI) EditRecord(oldKey, newKey, newValue string) error {
if db.ReadOnly {
return ErrNeedWriteMode
}
return db.db.Update(func(tx *bolt.Tx) error {
b := db.getCurrentBucket(tx)
// If there's a bucket with key == oldKey
if b.Bucket([]byte(oldKey)) != nil {
return errors.New("it's a bucket")
}
// If there's no record with key == oldKey
if b.Get([]byte(oldKey)) == nil {
return errors.New("there's no such record")
}
// If there's a bucket with key == newKey
if b.Bucket([]byte(newKey)) != nil {
return errors.New("there's a bucket with key == newKey")
}
// If there's a record with key == newKey
if oldKey != newKey && b.Get([]byte(newKey)) != nil {
return errors.New("there's a record with key == newKey")
}
// We can overwrite the existing record
if oldKey == newKey {
return b.Put([]byte(oldKey), []byte(newValue))
}
// We have to delete old record and create new one
b.Delete([]byte(oldKey))
return b.Put([]byte(newKey), []byte(newValue))
})
}