From b79a234f895fe1b85e5c6b060cd4fc0eaeb03ef4 Mon Sep 17 00:00:00 2001 From: ucwong Date: Fri, 9 Sep 2022 04:14:48 +0800 Subject: [PATCH] ha module removed --- README.md | 3 - ha/ha.go | 269 -------------------------------------------------- ha/ha_test.go | 105 -------------------- kv.go | 5 - kv_test.go | 78 --------------- 5 files changed, 460 deletions(-) delete mode 100644 ha/ha.go delete mode 100644 ha/ha_test.go diff --git a/README.md b/README.md index 2136bb9..144db97 100644 --- a/README.md +++ b/README.md @@ -25,9 +25,6 @@ bolt.setTTL([]byte("k"), []byte("v"), time.Second) ldb := kv.LevelDB("") defer ldb.Close() -ha := kv.HA("") -defer ha.Close() - ... ``` ## Test diff --git a/ha/ha.go b/ha/ha.go deleted file mode 100644 index 08bb80f..0000000 --- a/ha/ha.go +++ /dev/null @@ -1,269 +0,0 @@ -// Copyright (C) 2022 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 ha - -import ( - "fmt" - "github.com/ucwong/golang-kv/badger" - "github.com/ucwong/golang-kv/bolt" - "github.com/ucwong/golang-kv/leveldb" - "sync" - "time" -) - -type Ha struct { - ldb *leveldb.LevelDB - bgr *badger.Badger - bot *bolt.Bolt - wg sync.WaitGroup -} - -func Open(path string, level int) *Ha { - if len(path) == 0 { - //path = ".ha" - } - - ha := &Ha{} - switch level { - case 0: - ha.bgr = badger.Open(path) - case 1: - ha.bot = bolt.Open(path) - ha.bgr = badger.Open(path) - case 2: - ha.bot = bolt.Open(path) - ha.bgr = badger.Open(path) - ha.ldb = leveldb.Open(path) - default: - ha.bgr = badger.Open(path) - } - - if ha.bot == nil && ha.bgr == nil && ha.ldb == nil { - // suc when one engine is available - return nil - } - fmt.Printf("bolt:%v badger:%v leveldb:%v\n", ha.bot, ha.bgr, ha.ldb) - - return ha -} - -func (b *Ha) Get(k []byte) (v []byte) { - if b.bot != nil { - v = b.bot.Get(k) - } - if v == nil { - if b.bgr != nil { - v = b.bgr.Get(k) - } - } - - if v == nil { - if b.ldb != nil { - v = b.ldb.Get(k) - } - } - return -} - -func (b *Ha) Set(k, v []byte) (err error) { - if b.bot != nil { - b.wg.Add(1) - go func() { - defer b.wg.Done() - b.bot.Set(k, v) - }() - } - if b.bgr != nil { - b.wg.Add(1) - go func() { - defer b.wg.Done() - b.bgr.Set(k, v) - }() - } - - if b.ldb != nil { - b.wg.Add(1) - go func() { - defer b.wg.Done() - b.ldb.Set(k, v) - }() - } - b.wg.Wait() - return -} - -func (b *Ha) Del(k []byte) (err error) { - if b.bot != nil { - b.wg.Add(1) - go func() { - defer b.wg.Done() - b.bot.Del(k) - }() - } - - if b.bgr != nil { - b.wg.Add(1) - go func() { - defer b.wg.Done() - b.bgr.Del(k) - }() - } - - if b.ldb != nil { - b.wg.Add(1) - go func() { - defer b.wg.Done() - b.ldb.Del(k) - }() - } - - b.wg.Wait() - - return -} - -func (b *Ha) Prefix(prefix []byte) (res [][]byte) { - if b.bot != nil { - res = b.bot.Prefix(prefix) - } - if res == nil { - if b.bgr != nil { - res = b.bgr.Prefix(prefix) - } - } - - if res == nil { - if b.ldb != nil { - res = b.ldb.Prefix(prefix) - } - } - return -} - -func (b *Ha) Suffix(suffix []byte) (res [][]byte) { - if b.bot != nil { - res = b.bot.Suffix(suffix) - } - if res == nil { - if b.bgr != nil { - res = b.bgr.Suffix(suffix) - } - } - if res == nil { - if b.ldb != nil { - res = b.ldb.Suffix(suffix) - } - } - return -} - -func (b *Ha) Scan() (res [][]byte) { - if b.bot != nil { - res = b.bot.Scan() - } - if res == nil { - if b.bgr != nil { - res = b.bgr.Scan() - } - } - if res == nil { - if b.ldb != nil { - res = b.ldb.Scan() - } - } - return -} - -func (b *Ha) SetTTL(k, v []byte, expire time.Duration) (err error) { - if b.bot != nil { - b.wg.Add(1) - go func() { - defer b.wg.Done() - b.bot.SetTTL(k, v, expire) - }() - } - if b.bgr != nil { - b.wg.Add(1) - go func() { - defer b.wg.Done() - b.bgr.SetTTL(k, v, expire) - }() - } - if b.ldb != nil { - b.wg.Add(1) - go func() { - defer b.wg.Done() - b.ldb.SetTTL(k, v, expire) - }() - } - - b.wg.Wait() - - return -} - -func (b *Ha) Range(start, limit []byte) (res [][]byte) { - if b.bot != nil { - res = b.bot.Range(start, limit) - } - if res == nil { - if b.bgr != nil { - res = b.bgr.Range(start, limit) - } - } - - if res == nil { - if b.ldb != nil { - res = b.ldb.Range(start, limit) - } - } - - return -} - -func (b *Ha) Close() (err error) { - if b.bot != nil { - b.wg.Add(1) - go func() { - defer b.wg.Done() - b.bot.Close() - }() - } - - if b.ldb != nil { - b.wg.Add(1) - go func() { - defer b.wg.Done() - b.ldb.Close() - }() - } - - if b.bgr != nil { - b.wg.Add(1) - go func() { - defer b.wg.Done() - b.bgr.Close() - }() - } - - b.wg.Wait() - - return -} - -func (b *Ha) Batch(kvs map[string][]byte) error { - panic("Not support") -} diff --git a/ha/ha_test.go b/ha/ha_test.go deleted file mode 100644 index 1bf6d39..0000000 --- a/ha/ha_test.go +++ /dev/null @@ -1,105 +0,0 @@ -// Copyright (C) 2022 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 ha - -import ( - "fmt" - //"github.com/ucwong/golang-kv" - "strconv" - "testing" - "time" -) - -func TestLocal(t *testing.T) { - ha1() -} - -var batch int = 10 - -func ha1() { - db := Open("", 2) - - db.Set([]byte("yx"), []byte("yx")) - 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)) - } - 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() -} diff --git a/kv.go b/kv.go index b994101..05bcd71 100644 --- a/kv.go +++ b/kv.go @@ -18,7 +18,6 @@ package kv import ( "github.com/ucwong/golang-kv/badger" "github.com/ucwong/golang-kv/bolt" - "github.com/ucwong/golang-kv/ha" "github.com/ucwong/golang-kv/leveldb" ) @@ -33,7 +32,3 @@ func Bolt(path string) Bucket { func LevelDB(path string) Bucket { return leveldb.Open(path) } - -func HA(path string, level int) Bucket { - return ha.Open(path, level) -} diff --git a/kv_test.go b/kv_test.go index 4e5203f..e132581 100644 --- a/kv_test.go +++ b/kv_test.go @@ -27,7 +27,6 @@ func TestLocal(t *testing.T) { bolt1() badger1() leveldb1() - ha1() } var batch int = 10 @@ -110,83 +109,6 @@ func leveldb1() { db.Close() } -func ha1() { - var db Bucket - - db = HA("", 2) - - db.Set([]byte("yx"), []byte("yx")) - 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)) - } - 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() -} - func bolt1() { var db Bucket