|
| 1 | +// Copyright (C) 2022 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 nutsdb |
| 17 | + |
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + //"github.com/ucwong/golang-kv" |
| 21 | + "strconv" |
| 22 | + "testing" |
| 23 | + "time" |
| 24 | +) |
| 25 | + |
| 26 | +func TestLocal(t *testing.T) { |
| 27 | + nutsdb1() |
| 28 | +} |
| 29 | + |
| 30 | +var batch int = 10 |
| 31 | + |
| 32 | +func nutsdb1() { |
| 33 | + |
| 34 | + db := Open("") |
| 35 | + if db == nil { |
| 36 | + panic("nutsdb create err") |
| 37 | + } |
| 38 | + db.Set([]byte("yx"), []byte("yx")) |
| 39 | + db.Set([]byte("yy"), []byte("yy")) |
| 40 | + db.Set([]byte("a"), []byte("a")) |
| 41 | + db.Set([]byte("b"), []byte("b")) |
| 42 | + db.Set([]byte("x"), []byte("x")) |
| 43 | + db.Set([]byte("y"), []byte("y")) |
| 44 | + db.Set([]byte("xxy"), []byte("xxy")) |
| 45 | + db.Set([]byte("xxx"), []byte("xxx")) |
| 46 | + db.Set([]byte("xxxyx"), []byte("xxxyx")) |
| 47 | + db.Set([]byte("xyy"), []byte("xyy")) |
| 48 | + db.SetTTL([]byte("ttlxxxyx"), []byte("ttlxxxyx"), 1000*time.Millisecond) |
| 49 | + db.Set([]byte("ttlxxxyx"), []byte("ttlxxxyx")) |
| 50 | + db.SetTTL([]byte("ttlxxxyx1"), []byte("ttlxxxyx1"), 200000*time.Millisecond) |
| 51 | + db.SetTTL([]byte("ttlxxxyx2"), []byte("ttlxxxyx2"), 5000*time.Millisecond) |
| 52 | + db.SetTTL([]byte("ttlxxxyx3"), []byte("ttlxxxyx3"), 5000*time.Millisecond) |
| 53 | + for i := 0; i < batch; i++ { |
| 54 | + db.SetTTL([]byte("ttlxxxyx3"+strconv.Itoa(i)), []byte("ttlxxxyx3"+strconv.Itoa(i)), 2000*time.Millisecond) |
| 55 | + } |
| 56 | + for i := 0; i < batch; i++ { |
| 57 | + db.SetTTL([]byte("ttlxxxyx4"+strconv.Itoa(i)), []byte("ttlxxxyx4"+strconv.Itoa(i)), 5000*time.Millisecond) |
| 58 | + } |
| 59 | + res := db.Scan() |
| 60 | + for _, i := range res { |
| 61 | + fmt.Printf("scan...%v...%s\n", len(res), string(i)) |
| 62 | + } |
| 63 | + res = db.Range([]byte("xxx"), []byte("xxz")) |
| 64 | + for _, i := range res { |
| 65 | + fmt.Printf("range...%v...%s\n", len(res), string(i)) |
| 66 | + } |
| 67 | + res = db.Prefix([]byte("xx")) |
| 68 | + for _, i := range res { |
| 69 | + fmt.Printf("prefix...%v...%s\n", len(res), string(i)) |
| 70 | + } |
| 71 | + res = db.Suffix([]byte("x")) |
| 72 | + for _, i := range res { |
| 73 | + fmt.Printf("suffix...%v...%s\n", len(res), string(i)) |
| 74 | + } |
| 75 | + kvs := make(map[string][]byte) |
| 76 | + kvs["batch1"] = []byte("batchv1") |
| 77 | + kvs["batch2"] = []byte("batchv2") |
| 78 | + kvs["batch3"] = []byte("batchv3") |
| 79 | + kvs["batch4"] = []byte("batchv4") |
| 80 | + db.BatchSet(kvs) |
| 81 | + res = db.Prefix([]byte("batch")) |
| 82 | + for _, i := range res { |
| 83 | + fmt.Printf("prefix(batch)...%v...%s\n", len(res), string(i)) |
| 84 | + } |
| 85 | + res = db.Scan() |
| 86 | + for _, i := range res { |
| 87 | + fmt.Printf("scan...%v...%v\n", len(res), len(i)) |
| 88 | + } |
| 89 | + //db.Del([]byte("xxy")) |
| 90 | + //res = db.Scan() |
| 91 | + //for _, i := range res { |
| 92 | + // fmt.Printf("...%v...%s\n", len(res), string(i)) |
| 93 | + //} |
| 94 | + db.Del([]byte("xx")) |
| 95 | + time.Sleep(500 * time.Millisecond) |
| 96 | + f := db.Get([]byte("ttlxxxyx")) |
| 97 | + fmt.Printf("...........%s\n", string(f)) |
| 98 | + |
| 99 | + f1 := db.Get([]byte("xxy")) |
| 100 | + fmt.Printf("...........%s\n", string(f1)) |
| 101 | + |
| 102 | + for i := 0; i < batch/2; i++ { |
| 103 | + db.Set([]byte("ttlxxxyx4"+strconv.Itoa(i)), []byte("reset -> ttlxxxyx4"+strconv.Itoa(i))) |
| 104 | + } |
| 105 | + |
| 106 | + for i := 0; i < batch; i++ { |
| 107 | + fmt.Printf("...........%s\n", string(db.Get([]byte("ttlxxxyx4"+strconv.Itoa(i))))) |
| 108 | + } |
| 109 | + |
| 110 | + db.Del([]byte("ttlxxxyx1")) |
| 111 | + |
| 112 | + time.Sleep(3000 * time.Millisecond) |
| 113 | + m := db.Get([]byte("ttlxxxyx")) |
| 114 | + fmt.Printf("...........%s\n", string(m)) |
| 115 | + |
| 116 | + db.Del([]byte("ttlxxxyx1")) |
| 117 | + |
| 118 | + m2 := db.Get([]byte("ttlxxxyx1")) |
| 119 | + fmt.Printf("...........%s\n", string(m2)) |
| 120 | + |
| 121 | + f2 := db.Get([]byte("xxy")) |
| 122 | + fmt.Printf("...........%s\n", string(f2)) |
| 123 | + fmt.Printf("Close func call\n") |
| 124 | + db.Close() |
| 125 | +} |
0 commit comments