diff --git a/go.mod b/go.mod index 77d388d..b8ee122 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,6 @@ require ( github.com/cockroachdb/pebble v0.0.0-20231222152533-1cce3d01f6d9 github.com/dgraph-io/badger/v4 v4.2.1-0.20231218065111-7b5baa11879c github.com/nutsdb/nutsdb v1.0.3-0.20231224042427-ed51a64e0297 - github.com/rosedblabs/rosedb/v2 v2.2.2 github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d github.com/ucwong/go-ttlmap v1.0.2-0.20221020173635-331e7ddde2bb go.etcd.io/bbolt v1.3.7-0.20231211160616-a7a791ca815e @@ -33,7 +32,6 @@ require ( github.com/golang/protobuf v1.5.2 // indirect github.com/golang/snappy v0.0.4 // indirect github.com/google/flatbuffers v1.12.1 // indirect - github.com/hashicorp/golang-lru/v2 v2.0.4 // indirect github.com/klauspost/compress v1.15.15 // indirect github.com/kr/pretty v0.3.1 // indirect github.com/kr/text v0.2.0 // indirect @@ -44,8 +42,6 @@ require ( github.com/prometheus/common v0.32.1 // indirect github.com/prometheus/procfs v0.7.3 // indirect github.com/rogpeppe/go-internal v1.9.0 // indirect - github.com/rosedblabs/go-immutable-radix/v2 v2.0.1-0.20230614125820-f2a7bc058c90 // indirect - github.com/rosedblabs/wal v1.3.1 // indirect github.com/tidwall/btree v1.6.0 // indirect github.com/xujiajun/mmap-go v1.0.1 // indirect github.com/xujiajun/utils v0.0.0-20220904132955-5f7c5b914235 // indirect diff --git a/go.sum b/go.sum index fa486db..1d265ee 100644 --- a/go.sum +++ b/go.sum @@ -175,12 +175,8 @@ github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLe github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= -github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8= -github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= -github.com/hashicorp/golang-lru/v2 v2.0.4 h1:7GHuZcgid37q8o5i3QI9KMT4nCWQQ3Kx3Ov6bb9MfK0= -github.com/hashicorp/golang-lru/v2 v2.0.4/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= @@ -268,12 +264,6 @@ github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1 github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= -github.com/rosedblabs/go-immutable-radix/v2 v2.0.1-0.20230614125820-f2a7bc058c90 h1:AeuxZLgpVnxCjR6mvRJiDQATgOeSme59HX/rWCBjjvw= -github.com/rosedblabs/go-immutable-radix/v2 v2.0.1-0.20230614125820-f2a7bc058c90/go.mod h1:Hk7adp95/ngEfetvapVWdgneuZb15mi9nH/keSH/KqI= -github.com/rosedblabs/rosedb/v2 v2.2.2 h1:LtzL5fya57RZ5aRH+V+opUcB6G+yxs5Wk/t5Yt5+kas= -github.com/rosedblabs/rosedb/v2 v2.2.2/go.mod h1:O62E80aqxZC2L6P4yMiyYM3dzbCaFNT/LnOWKzmmVHk= -github.com/rosedblabs/wal v1.3.1 h1:QFAmnEPGJgUYfbn9WRO/43VkDdXVo9AacRcB0FfCNTI= -github.com/rosedblabs/wal v1.3.1/go.mod h1:tYh0WapCkDQrID7PNsNHpsZDlkTczJVAFaTySmwaD7U= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= diff --git a/kv.go b/kv.go index 76a65da..2bba041 100644 --- a/kv.go +++ b/kv.go @@ -21,7 +21,6 @@ import ( "github.com/ucwong/golang-kv/leveldb" "github.com/ucwong/golang-kv/nutsdb" "github.com/ucwong/golang-kv/pebble" - "github.com/ucwong/golang-kv/rosedb" ) func Badger(path string, opt ...badger.BadgerOption) Bucket { @@ -43,7 +42,3 @@ func Pebble(path string, opt ...pebble.PebbleOption) Bucket { func NutsDB(path string, opt ...nutsdb.NutsDBOption) Bucket { return nutsdb.Open(path, opt...) } - -func RoseDB(path string, opt ...rosedb.RoseDBOption) Bucket { - return rosedb.Open(path, opt...) -} diff --git a/rosedb/rosedb.go b/rosedb/rosedb.go deleted file mode 100644 index 2af206c..0000000 --- a/rosedb/rosedb.go +++ /dev/null @@ -1,178 +0,0 @@ -// Copyright (C) 2023 ucwong -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see - -package rosedb - -import ( - "bytes" - "path/filepath" - "sync" - "time" - - "github.com/rosedblabs/rosedb/v2" - "github.com/ucwong/go-ttlmap" - "github.com/ucwong/golang-kv/common" -) - -type RoseDB struct { - engine *rosedb.DB - ttl_map *ttlmap.Map - once sync.Once -} - -type RoseDBOption func(rosedb.Options) rosedb.Options - -func Open(path string, opts ...RoseDBOption) *RoseDB { - path = filepath.Join(path, common.GLOBAL_SPACE, ".rose") - db := &RoseDB{} - option := rosedb.DefaultOptions //opt.Options{OpenFilesCacheCapacity: 32} - for _, op := range opts { - option = op(option) - } - option.DirPath = path - rdb, err := rosedb.Open(option) - if err != nil { - return nil - } - db.engine = rdb - - options := &ttlmap.Options{ - InitialCapacity: 1024 * 1024, - OnWillExpire: func(key string, item ttlmap.Item) { - //fmt.Printf("expired: [%s=%v]\n", key, item.Value()) - //b.Del([]byte(key)) - }, - OnWillEvict: func(key string, item ttlmap.Item) { - //fmt.Printf("evicted: [%s=%v]\n", key, item.Value()) - //db.Del([]byte(key)) - db.engine.Delete([]byte(key)) - }, - } - db.ttl_map = ttlmap.New(options) - return db -} - -func (rdb *RoseDB) Get(k []byte) (v []byte) { - item, err := rdb.ttl_map.Get(string(k)) - if err == nil { - return []byte(item.Value().(string)) - } - - v, _ = rdb.engine.Get(k) - return -} - -func (rdb *RoseDB) Set(k, v []byte) (err error) { - //if _, err = rdb.ttl_map.Delete(string(k)); err != nil { - // return - //} - - err = rdb.engine.Put(k, v) - return -} - -func (rdb *RoseDB) Del(k []byte) (err error) { - if _, err = rdb.ttl_map.Delete(string(k)); err != nil { - return - } - - err = rdb.engine.Delete(k) - return -} - -func (rdb *RoseDB) Prefix(k []byte) (res [][]byte) { - iterOptions := rosedb.DefaultIteratorOptions - iterOptions.Prefix = k - iter := rdb.engine.NewIterator(iterOptions) - defer iter.Close() - for ; iter.Valid(); iter.Next() { - val, _ := iter.Value() - res = append(res, common.SafeCopy(nil, val)) - } - return -} - -func (rdb *RoseDB) Suffix(k []byte) (res [][]byte) { - iterOptions := rosedb.DefaultIteratorOptions - iter := rdb.engine.NewIterator(iterOptions) - defer iter.Close() - for ; iter.Valid(); iter.Next() { - if bytes.HasSuffix(iter.Key(), k) { - val, _ := iter.Value() - res = append(res, common.SafeCopy(nil, val)) - } - } - return -} - -func (rdb *RoseDB) Range(start, limit []byte) (res [][]byte) { - iterOptions := rosedb.DefaultIteratorOptions - iter := rdb.engine.NewIterator(iterOptions) - defer iter.Close() - for iter.Seek(start); iter.Valid(); iter.Next() { - if bytes.Compare(limit, iter.Key()) > 0 && bytes.Compare(start, iter.Key()) <= 0 { - val, _ := iter.Value() - res = append(res, common.SafeCopy(nil, val)) - } else { - break - } - } - return -} - -func (rdb *RoseDB) Scan() (res [][]byte) { - iterOptions := rosedb.DefaultIteratorOptions - iter := rdb.engine.NewIterator(iterOptions) - defer iter.Close() - for ; iter.Valid(); iter.Next() { - val, _ := iter.Value() - res = append(res, common.SafeCopy(nil, val)) - } - return -} - -func (rdb *RoseDB) SetTTL(k, v []byte, expire time.Duration) (err error) { - if err = rdb.ttl_map.Set(string(k), ttlmap.NewItem(string(v), ttlmap.WithTTL(expire)), nil); err != nil { - return - } - - err = rdb.engine.Put(k, v) - - if err != nil { - // TODO - rdb.ttl_map.Delete(string(k)) - } - - return -} - -func (rdb *RoseDB) Close() error { - rdb.once.Do(func() { - rdb.ttl_map.Drain() - }) - return rdb.engine.Close() -} - -func (rdb *RoseDB) BatchSet(kvs map[string][]byte) error { - wb := rdb.engine.NewBatch(rosedb.DefaultBatchOptions) - for k, v := range kvs { - wb.Put([]byte(k), v) - } - return wb.Commit() -} - -func (rdb *RoseDB) Name() string { - return "rosedb" -} diff --git a/rosedb/rosedb_test.go b/rosedb/rosedb_test.go deleted file mode 100644 index 8deb008..0000000 --- a/rosedb/rosedb_test.go +++ /dev/null @@ -1,120 +0,0 @@ -// Copyright (C) 2023 ucwong -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see - -package rosedb - -import ( - "fmt" - //"github.com/ucwong/golang-kv" - "strconv" - "testing" - "time" -) - -func TestLocal(t *testing.T) { - rosedb1() -} - -var batch int = 10 - -func rosedb1() { - - db := Open("") - - fmt.Println("start..") - - db.Set([]byte("yx"), []byte("yx")) - fmt.Println("Set") - db.Set([]byte("yy"), []byte("yy")) - db.Set([]byte("aabb"), []byte("aabb")) - db.Set([]byte("bb"), []byte("bb")) - db.Set([]byte("x"), []byte("x")) - db.Set([]byte("y"), []byte("y")) - db.Set([]byte("xxy"), []byte("xxy")) - db.Set([]byte("xxxyx"), []byte("xxxyx")) - db.Set([]byte("xxx"), []byte("xxx")) - db.Set([]byte("xyy"), []byte("xyy")) - - db.SetTTL([]byte("ttlxxxyx"), []byte("ttlxxxyx"), 1000*time.Millisecond) - db.SetTTL([]byte("ttlxxxyx1"), []byte("ttlxxxyx1"), 2000*time.Millisecond) - db.SetTTL([]byte("ttlxxxyx2"), []byte("ttlxxxyx2"), 5000*time.Millisecond) - db.SetTTL([]byte("ttlxxxyx3"), []byte("ttlxxxyx3"), 5000*time.Millisecond) - for i := 0; i < batch; i++ { - db.SetTTL([]byte("ttlxxxyx3"+strconv.Itoa(i)), []byte("ttlxxxyx3"+strconv.Itoa(i)), 2000*time.Millisecond) - } - for i := 0; i < batch; i++ { - db.SetTTL([]byte("ttlxxxyx4"+strconv.Itoa(i)), []byte("ttlxxxyx4"+strconv.Itoa(i)), 5000*time.Millisecond) - } - res := db.Scan() - for _, i := range res { - fmt.Printf("scan...%v...%s\n", len(res), string(i)) - } - res = db.Range([]byte("xxx"), []byte("xxz")) - for _, i := range res { - fmt.Printf("range...%v...%s\n", len(res), string(i)) - } - res = db.Prefix([]byte("xx")) - for _, i := range res { - fmt.Printf("prefix(xx)...%v...%s\n", len(res), string(i)) - } - res = db.Suffix([]byte("x")) - for _, i := range res { - fmt.Printf("suffix(x)...%v...%s\n", len(res), string(i)) - } - kvs := make(map[string][]byte) - kvs["batch1"] = []byte("batchv1") - kvs["batch2"] = []byte("batchv2") - kvs["batch3"] = []byte("batchv3") - kvs["batch4"] = []byte("batchv4") - db.BatchSet(kvs) - res = db.Prefix([]byte("batch")) - for _, i := range res { - fmt.Printf("prefix(batch)...%v...%s\n", len(res), string(i)) - } - res = db.Scan() - for _, i := range res { - fmt.Printf("scan...%v...%s\n", len(res), string(i)) - } - db.Del([]byte("xx")) - time.Sleep(500 * time.Millisecond) - f := db.Get([]byte("ttlxxxyx")) - fmt.Printf("...........%s\n", string(f)) - - f1 := db.Get([]byte("xxy")) - fmt.Printf("...........%s\n", string(f1)) - - for i := 0; i < batch/2; i++ { - db.Set([]byte("ttlxxxyx4"+strconv.Itoa(i)), []byte("reset -> ttlxxxyx4"+strconv.Itoa(i))) - } - - for i := 0; i < batch; i++ { - fmt.Printf("..........%s .%s\n", "ttlxxxyx4"+strconv.Itoa(i), string(db.Get([]byte("ttlxxxyx4"+strconv.Itoa(i))))) - } - - db.Del([]byte("ttlxxxyx1")) - - time.Sleep(3000 * time.Millisecond) - m := db.Get([]byte("ttlxxxyx")) - fmt.Printf("...........%s\n", string(m)) - - db.Del([]byte("ttlxxxyx1")) - - m2 := db.Get([]byte("ttlxxxyx1")) - fmt.Printf("...........%s\n", string(m2)) - - f2 := db.Get([]byte("xxy")) - fmt.Printf("...........%s\n", string(f2)) - db.Close() -}