|
| 1 | +//go:build boltdb |
| 2 | +// +build boltdb |
| 3 | + |
| 4 | +package db |
| 5 | + |
| 6 | +import ( |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + |
| 12 | + "go.etcd.io/bbolt" |
| 13 | +) |
| 14 | + |
| 15 | +var bucket = []byte("tm") |
| 16 | + |
| 17 | +func init() { |
| 18 | + registerDBCreator(BoltDBBackend, func(name, dir string) (DB, error) { |
| 19 | + return NewBoltDB(name, dir) |
| 20 | + }) |
| 21 | +} |
| 22 | + |
| 23 | +// BoltDB is a wrapper around etcd's fork of bolt (https://github.com/etcd-io/bbolt). |
| 24 | +// |
| 25 | +// NOTE: All operations (including Set, Delete) are synchronous by default. One |
| 26 | +// can globally turn it off by using NoSync config option (not recommended). |
| 27 | +// |
| 28 | +// A single bucket ([]byte("tm")) is used per a database instance. This could |
| 29 | +// lead to performance issues when/if there will be lots of keys. |
| 30 | +type BoltDB struct { |
| 31 | + db *bbolt.DB |
| 32 | +} |
| 33 | + |
| 34 | +var _ DB = (*BoltDB)(nil) |
| 35 | + |
| 36 | +// NewBoltDB returns a BoltDB with default options. |
| 37 | +// |
| 38 | +// Deprecated: boltdb is deprecated and will be removed in the future. |
| 39 | +func NewBoltDB(name, dir string) (DB, error) { |
| 40 | + return NewBoltDBWithOpts(name, dir, bbolt.DefaultOptions) |
| 41 | +} |
| 42 | + |
| 43 | +// NewBoltDBWithOpts allows you to supply *bbolt.Options. ReadOnly: true is not |
| 44 | +// supported because NewBoltDBWithOpts creates a global bucket. |
| 45 | +func NewBoltDBWithOpts(name string, dir string, opts *bbolt.Options) (DB, error) { |
| 46 | + if opts.ReadOnly { |
| 47 | + return nil, errors.New("ReadOnly: true is not supported") |
| 48 | + } |
| 49 | + |
| 50 | + dbPath := filepath.Join(dir, name+".db") |
| 51 | + db, err := bbolt.Open(dbPath, os.ModePerm, opts) |
| 52 | + if err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + |
| 56 | + // create a global bucket |
| 57 | + err = db.Update(func(tx *bbolt.Tx) error { |
| 58 | + _, err := tx.CreateBucketIfNotExists(bucket) |
| 59 | + return err |
| 60 | + }) |
| 61 | + if err != nil { |
| 62 | + return nil, err |
| 63 | + } |
| 64 | + |
| 65 | + return &BoltDB{db: db}, nil |
| 66 | +} |
| 67 | + |
| 68 | +// Get implements DB. |
| 69 | +func (bdb *BoltDB) Get(key []byte) (value []byte, err error) { |
| 70 | + if len(key) == 0 { |
| 71 | + return nil, errKeyEmpty |
| 72 | + } |
| 73 | + err = bdb.db.View(func(tx *bbolt.Tx) error { |
| 74 | + b := tx.Bucket(bucket) |
| 75 | + if v := b.Get(key); v != nil { |
| 76 | + value = append([]byte{}, v...) |
| 77 | + } |
| 78 | + return nil |
| 79 | + }) |
| 80 | + if err != nil { |
| 81 | + return nil, err |
| 82 | + } |
| 83 | + return |
| 84 | +} |
| 85 | + |
| 86 | +// Has implements DB. |
| 87 | +func (bdb *BoltDB) Has(key []byte) (bool, error) { |
| 88 | + bytes, err := bdb.Get(key) |
| 89 | + if err != nil { |
| 90 | + return false, err |
| 91 | + } |
| 92 | + return bytes != nil, nil |
| 93 | +} |
| 94 | + |
| 95 | +// Set implements DB. |
| 96 | +func (bdb *BoltDB) Set(key, value []byte) error { |
| 97 | + if len(key) == 0 { |
| 98 | + return errKeyEmpty |
| 99 | + } |
| 100 | + if value == nil { |
| 101 | + return errValueNil |
| 102 | + } |
| 103 | + err := bdb.db.Update(func(tx *bbolt.Tx) error { |
| 104 | + b := tx.Bucket(bucket) |
| 105 | + return b.Put(key, value) |
| 106 | + }) |
| 107 | + if err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + return nil |
| 111 | +} |
| 112 | + |
| 113 | +// SetSync implements DB. |
| 114 | +func (bdb *BoltDB) SetSync(key, value []byte) error { |
| 115 | + return bdb.Set(key, value) |
| 116 | +} |
| 117 | + |
| 118 | +// Delete implements DB. |
| 119 | +func (bdb *BoltDB) Delete(key []byte) error { |
| 120 | + if len(key) == 0 { |
| 121 | + return errKeyEmpty |
| 122 | + } |
| 123 | + err := bdb.db.Update(func(tx *bbolt.Tx) error { |
| 124 | + return tx.Bucket(bucket).Delete(key) |
| 125 | + }) |
| 126 | + if err != nil { |
| 127 | + return err |
| 128 | + } |
| 129 | + return nil |
| 130 | +} |
| 131 | + |
| 132 | +// DeleteSync implements DB. |
| 133 | +func (bdb *BoltDB) DeleteSync(key []byte) error { |
| 134 | + return bdb.Delete(key) |
| 135 | +} |
| 136 | + |
| 137 | +// Close implements DB. |
| 138 | +func (bdb *BoltDB) Close() error { |
| 139 | + return bdb.db.Close() |
| 140 | +} |
| 141 | + |
| 142 | +// Print implements DB. |
| 143 | +func (bdb *BoltDB) Print() error { |
| 144 | + stats := bdb.db.Stats() |
| 145 | + fmt.Printf("%v\n", stats) |
| 146 | + |
| 147 | + err := bdb.db.View(func(tx *bbolt.Tx) error { |
| 148 | + tx.Bucket(bucket).ForEach(func(k, v []byte) error { |
| 149 | + fmt.Printf("[%X]:\t[%X]\n", k, v) |
| 150 | + return nil |
| 151 | + }) |
| 152 | + return nil |
| 153 | + }) |
| 154 | + if err != nil { |
| 155 | + return err |
| 156 | + } |
| 157 | + return nil |
| 158 | +} |
| 159 | + |
| 160 | +// Stats implements DB. |
| 161 | +func (bdb *BoltDB) Stats() map[string]string { |
| 162 | + stats := bdb.db.Stats() |
| 163 | + m := make(map[string]string) |
| 164 | + |
| 165 | + // Freelist stats |
| 166 | + m["FreePageN"] = fmt.Sprintf("%v", stats.FreePageN) |
| 167 | + m["PendingPageN"] = fmt.Sprintf("%v", stats.PendingPageN) |
| 168 | + m["FreeAlloc"] = fmt.Sprintf("%v", stats.FreeAlloc) |
| 169 | + m["FreelistInuse"] = fmt.Sprintf("%v", stats.FreelistInuse) |
| 170 | + |
| 171 | + // Transaction stats |
| 172 | + m["TxN"] = fmt.Sprintf("%v", stats.TxN) |
| 173 | + m["OpenTxN"] = fmt.Sprintf("%v", stats.OpenTxN) |
| 174 | + |
| 175 | + return m |
| 176 | +} |
| 177 | + |
| 178 | +// NewBatch implements DB. |
| 179 | +func (bdb *BoltDB) NewBatch() Batch { |
| 180 | + return newBoltDBBatch(bdb) |
| 181 | +} |
| 182 | + |
| 183 | +// WARNING: Any concurrent writes or reads will block until the iterator is |
| 184 | +// closed. |
| 185 | +func (bdb *BoltDB) Iterator(start, end []byte) (Iterator, error) { |
| 186 | + if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) { |
| 187 | + return nil, errKeyEmpty |
| 188 | + } |
| 189 | + tx, err := bdb.db.Begin(false) |
| 190 | + if err != nil { |
| 191 | + return nil, err |
| 192 | + } |
| 193 | + return newBoltDBIterator(tx, start, end, false), nil |
| 194 | +} |
| 195 | + |
| 196 | +// WARNING: Any concurrent writes or reads will block until the iterator is |
| 197 | +// closed. |
| 198 | +func (bdb *BoltDB) ReverseIterator(start, end []byte) (Iterator, error) { |
| 199 | + if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) { |
| 200 | + return nil, errKeyEmpty |
| 201 | + } |
| 202 | + tx, err := bdb.db.Begin(false) |
| 203 | + if err != nil { |
| 204 | + return nil, err |
| 205 | + } |
| 206 | + return newBoltDBIterator(tx, start, end, true), nil |
| 207 | +} |
| 208 | + |
| 209 | +func (bdb *BoltDB) Compact(start, end []byte) error { |
| 210 | + // There is no explicit CompactRange support in BoltDB, only a function that copies the |
| 211 | + // entire DB from one place to another while doing deletions. Hence we do not support it. |
| 212 | + return nil |
| 213 | +} |
0 commit comments