|
| 1 | +// Copyright (C) 2023 ucwong |
| 2 | +// |
| 3 | +// This program is free software: you can redistribute it and/or modify |
| 4 | +// it under the terms of the GNU General Public License as published by |
| 5 | +// the Free Software Foundation, either version 3 of the License, or |
| 6 | +// (at your option) any later version. |
| 7 | +// |
| 8 | +// This program is distributed in the hope that it will be useful, |
| 9 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +// GNU General Public License for more details. |
| 12 | +// |
| 13 | +// You should have received a copy of the GNU General Public License |
| 14 | +// along with this program. If not, see <https://www.gnu.org/licenses/> |
| 15 | + |
| 16 | +package pebble |
| 17 | + |
| 18 | +import ( |
| 19 | + "bytes" |
| 20 | + //"fmt" |
| 21 | + "path/filepath" |
| 22 | + "sync" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/cockroachdb/pebble" |
| 26 | + "github.com/ucwong/go-ttlmap" |
| 27 | + "github.com/ucwong/golang-kv/common" |
| 28 | +) |
| 29 | + |
| 30 | +type Pebble struct { |
| 31 | + engine *pebble.DB |
| 32 | + ttl_map *ttlmap.Map |
| 33 | + wb *pebble.Batch |
| 34 | + once sync.Once |
| 35 | +} |
| 36 | + |
| 37 | +type PebbleOption func(pebble.Options) pebble.Options |
| 38 | + |
| 39 | +func Open(path string, opts ...PebbleOption) *Pebble { |
| 40 | + path = filepath.Join(path, common.GLOBAL_SPACE, ".pebble") |
| 41 | + db := &Pebble{} |
| 42 | + option := pebble.Options{} |
| 43 | + for _, op := range opts { |
| 44 | + option = op(option) |
| 45 | + } |
| 46 | + peb, err := pebble.Open(path, &option) |
| 47 | + if err != nil { |
| 48 | + return nil |
| 49 | + } |
| 50 | + db.engine = peb |
| 51 | + db.wb = new(pebble.Batch) |
| 52 | + |
| 53 | + options := &ttlmap.Options{ |
| 54 | + InitialCapacity: 1024 * 1024, |
| 55 | + OnWillExpire: func(key string, item ttlmap.Item) { |
| 56 | + //fmt.Printf("expired: [%s=%v]\n", key, item.Value()) |
| 57 | + //b.Del([]byte(key)) |
| 58 | + }, |
| 59 | + OnWillEvict: func(key string, item ttlmap.Item) { |
| 60 | + //fmt.Printf("evicted: [%s=%v]\n", key, item.Value()) |
| 61 | + //db.Del([]byte(key)) |
| 62 | + db.engine.Delete([]byte(key), nil) |
| 63 | + }, |
| 64 | + } |
| 65 | + db.ttl_map = ttlmap.New(options) |
| 66 | + return db |
| 67 | +} |
| 68 | + |
| 69 | +func (peb *Pebble) Get(k []byte) (v []byte) { |
| 70 | + item, err := peb.ttl_map.Get(string(k)) |
| 71 | + if err == nil { |
| 72 | + return []byte(item.Value().(string)) |
| 73 | + } |
| 74 | + |
| 75 | + v, closer, err := peb.engine.Get(k) |
| 76 | + if err == nil { |
| 77 | + defer closer.Close() |
| 78 | + } |
| 79 | + return |
| 80 | +} |
| 81 | + |
| 82 | +func (peb *Pebble) Set(k, v []byte) (err error) { |
| 83 | + if _, err = peb.ttl_map.Delete(string(k)); err != nil { |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + err = peb.engine.Set(k, v, pebble.Sync) |
| 88 | + return |
| 89 | +} |
| 90 | + |
| 91 | +func (peb *Pebble) Del(k []byte) (err error) { |
| 92 | + if _, err = peb.ttl_map.Delete(string(k)); err != nil { |
| 93 | + return |
| 94 | + } |
| 95 | + |
| 96 | + err = peb.engine.Delete(k, nil) |
| 97 | + return |
| 98 | +} |
| 99 | + |
| 100 | +func (peb *Pebble) Prefix(k []byte) (res [][]byte) { |
| 101 | + keyUpperBound := func(b []byte) []byte { |
| 102 | + end := make([]byte, len(b)) |
| 103 | + copy(end, b) |
| 104 | + for i := len(end) - 1; i >= 0; i-- { |
| 105 | + end[i] = end[i] + 1 |
| 106 | + if end[i] != 0 { |
| 107 | + return end[:i+1] |
| 108 | + } |
| 109 | + } |
| 110 | + return nil // no upper-bound |
| 111 | + } |
| 112 | + prefixIterOptions := func(prefix []byte) *pebble.IterOptions { |
| 113 | + return &pebble.IterOptions{ |
| 114 | + LowerBound: prefix, |
| 115 | + UpperBound: keyUpperBound(prefix), |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + iter := peb.engine.NewIter(prefixIterOptions(k)) |
| 120 | + defer iter.Close() |
| 121 | + for iter.First(); iter.Valid(); iter.Next() { |
| 122 | + res = append(res, common.SafeCopy(nil, iter.Value())) |
| 123 | + } |
| 124 | + return |
| 125 | +} |
| 126 | + |
| 127 | +func (peb *Pebble) Suffix(k []byte) (res [][]byte) { |
| 128 | + iter := peb.engine.NewIter(nil) |
| 129 | + defer iter.Close() |
| 130 | + for iter.First(); iter.Valid(); iter.Next() { |
| 131 | + if bytes.HasSuffix(iter.Key(), k) { |
| 132 | + res = append(res, common.SafeCopy(nil, iter.Value())) |
| 133 | + } |
| 134 | + } |
| 135 | + return |
| 136 | +} |
| 137 | + |
| 138 | +func (peb *Pebble) Range(start, limit []byte) (res [][]byte) { |
| 139 | + return |
| 140 | +} |
| 141 | + |
| 142 | +func (peb *Pebble) Scan() (res [][]byte) { |
| 143 | + iter := peb.engine.NewIter(nil) |
| 144 | + defer iter.Close() |
| 145 | + for iter.First(); iter.Valid(); iter.Next() { |
| 146 | + res = append(res, common.SafeCopy(nil, iter.Value())) |
| 147 | + } |
| 148 | + return |
| 149 | +} |
| 150 | + |
| 151 | +func (peb *Pebble) SetTTL(k, v []byte, expire time.Duration) (err error) { |
| 152 | + if err = peb.ttl_map.Set(string(k), ttlmap.NewItem(string(v), ttlmap.WithTTL(expire)), nil); err != nil { |
| 153 | + return |
| 154 | + } |
| 155 | + |
| 156 | + err = peb.engine.Set(k, v, pebble.Sync) |
| 157 | + |
| 158 | + if err != nil { |
| 159 | + // TODO |
| 160 | + peb.ttl_map.Delete(string(k)) |
| 161 | + } |
| 162 | + |
| 163 | + return |
| 164 | +} |
| 165 | + |
| 166 | +func (peb *Pebble) Close() error { |
| 167 | + peb.once.Do(func() { |
| 168 | + peb.ttl_map.Drain() |
| 169 | + }) |
| 170 | + return peb.engine.Close() |
| 171 | +} |
| 172 | + |
| 173 | +func (peb *Pebble) BatchSet(kvs map[string][]byte) error { |
| 174 | + for k, v := range kvs { |
| 175 | + peb.wb.Set([]byte(k), v, nil) |
| 176 | + } |
| 177 | + peb.engine.Apply(peb.wb, nil) |
| 178 | + return peb.wb.SyncWait() |
| 179 | +} |
0 commit comments