-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdb.go
120 lines (94 loc) · 2.51 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package bolt
import (
"crypto/aes"
"crypto/cipher"
"errors"
"fmt"
"os"
bolt "go.etcd.io/bbolt"
)
type DB struct {
*bolt.DB
aesgcm cipher.AEAD
}
func Open(key []byte, path string, mode os.FileMode, options *Options) (*DB, error) {
if len(key) != 32 {
return nil, fmt.Errorf("Encryption key must be 32 bytes long")
}
db, err := bolt.Open(path, mode, options)
if err != nil {
return nil, err
}
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
return &DB{DB: db, aesgcm: aesgcm}, nil
}
func (db *DB) View(fn func(*Tx) error) error {
if db.DB == nil {
return ErrDatabaseNotOpen
}
return db.DB.View(func(tx *bolt.Tx) error {
return fn(&Tx{Tx: tx, aesgcm: db.aesgcm})
})
}
func (db *DB) Update(fn func(*Tx) error) error {
if db.DB == nil {
return ErrDatabaseNotOpen
}
return db.DB.Update(func(tx *bolt.Tx) error {
return fn(&Tx{Tx: tx, aesgcm: db.aesgcm})
})
}
func (db *DB) Begin(writable bool) (*Tx, error) {
if db.DB == nil {
return nil, ErrDatabaseNotOpen
}
tx, err := db.DB.Begin(writable)
if err != nil {
return nil, err
}
return &Tx{Tx: tx, aesgcm: db.aesgcm}, nil
}
func (db *DB) Batch(fn func(*Tx) error) error {
return db.DB.Batch(func(tx *bolt.Tx) error {
return fn(&Tx{Tx: tx, aesgcm: db.aesgcm})
})
}
type Info = bolt.Info
type Options = bolt.Options
type PageInfo = bolt.PageInfo
type Stats = bolt.Stats
var (
ErrDatabaseNotOpen = bolt.ErrDatabaseNotOpen
ErrDatabaseOpen = bolt.ErrDatabaseOpen
ErrInvalid = bolt.ErrInvalid
ErrVersionMismatch = bolt.ErrVersionMismatch
ErrChecksum = bolt.ErrChecksum
ErrTimeout = bolt.ErrTimeout
ErrTxNotWritable = bolt.ErrTxNotWritable
ErrTxClosed = bolt.ErrTxClosed
ErrDatabaseReadOnly = bolt.ErrDatabaseReadOnly
ErrBucketNotFound = bolt.ErrBucketNotFound
ErrBucketExists = bolt.ErrBucketExists
ErrBucketNameRequired = bolt.ErrBucketNameRequired
ErrKeyRequired = bolt.ErrKeyRequired
ErrKeyTooLarge = bolt.ErrKeyTooLarge
ErrValueTooLarge = bolt.ErrValueTooLarge
ErrIncompatibleValue = bolt.ErrIncompatibleValue
ErrDecrypt = errors.New("could not decrypt data")
)
const (
MaxKeySize = bolt.MaxKeySize
MaxValueSize = bolt.MaxValueSize
DefaultMaxBatchSize = bolt.DefaultMaxBatchSize
DefaultMaxBatchDelay = bolt.DefaultMaxBatchDelay
DefaultAllocSize = bolt.DefaultAllocSize
DefaultFillPercent = bolt.DefaultFillPercent
IgnoreNoSync = bolt.IgnoreNoSync
)