Skip to content

Commit

Permalink
finish auto merge (#471)
Browse files Browse the repository at this point in the history
* add list merge

* add auto merge unit test for list

* add string unit test, set unit test for auto merge

* finish auto merge unit test and remote LSet of list for auto merge

* gofumpt code

* add unit test for some func

* optimize some code

---------

Co-authored-by: huyp3 <huyp3@chinatelecom.cn>
  • Loading branch information
damotiansheng and huyp3 committed Oct 1, 2023
1 parent f8fd552 commit 551d8b9
Show file tree
Hide file tree
Showing 14 changed files with 496 additions and 402 deletions.
21 changes: 0 additions & 21 deletions README-CN.md
Expand Up @@ -72,7 +72,6 @@ https://www.bilibili.com/video/BV1T34y1x7AS/
- [LRange](#lrange)
- [LRem](#lrem)
- [LRemByIndex](#lrembyindex)
- [LSet](#lset)
- [LTrim](#LTrim)
- [LSize](#lsize)
- [LKeys](#lkeys)
Expand Down Expand Up @@ -916,26 +915,6 @@ if err := db.Update(
}
```

##### LSet

设置指定bucket的指定list的index位置的的值为value。

```golang
if err := db.Update(
func(tx *nutsdb.Tx) error {
bucket := "bucketForList"
key := []byte("myList")
if err := tx.LSet(bucket, key, 0, []byte("val11")); err != nil {
return err
} else {
fmt.Println("LSet ok, index 0 item value => val11")
}
return nil
}); err != nil {
log.Fatal(err)
}
```

##### LTrim

修剪一个已存在的 list,这样 list 就会只包含指定范围的指定元素。start 和 stop 都是由0开始计数的, 这里的 0 是列表里的第一个元素(表头),1 是第二个元素,以此类推。
Expand Down
8 changes: 0 additions & 8 deletions db.go
Expand Up @@ -57,9 +57,6 @@ const (
// DataRPopFlag represents the data RPop flag
DataRPopFlag

// DataLSetFlag represents the data LSet flag
DataLSetFlag

// DataLTrimFlag represents the data LTrim flag
DataLTrimFlag

Expand Down Expand Up @@ -846,11 +843,6 @@ func (db *DB) buildListIdx(r *Record) error {
_, err = l.LPop(string(key))
case DataRPopFlag:
_, err = l.RPop(string(key))
case DataLSetFlag:
keyAndIndex := strings.Split(string(key), SeparatorForListKey)
newKey := keyAndIndex[0]
index, _ := strconv2.StrToInt(keyAndIndex[1])
err = l.LSet(newKey, index, r)
case DataLTrimFlag:
keyAndStartIndex := strings.Split(string(key), SeparatorForListKey)
newKey := keyAndStartIndex[0]
Expand Down
29 changes: 17 additions & 12 deletions db_test.go
Expand Up @@ -221,15 +221,6 @@ func txLRemByIndex(t *testing.T, db *DB, bucket string, key []byte, expectErr er
require.NoError(t, err)
}

func txLSet(t *testing.T, db *DB, bucket string, key []byte, index int, value []byte, expectErr error) {
err := db.Update(func(tx *Tx) error {
err := tx.LSet(bucket, key, index, value)
assertErr(t, err, expectErr)
return nil
})
require.NoError(t, err)
}

func txSAdd(t *testing.T, db *DB, bucket string, key, value []byte, expectErr error, finalExpectErr error) {
err := db.Update(func(tx *Tx) error {
err := tx.SAdd(bucket, key, value)
Expand Down Expand Up @@ -541,6 +532,23 @@ func txPush(t *testing.T, db *DB, bucket string, key, val []byte, isLeft bool, e
assertErr(t, err, finalExpectErr)
}

func txPushRaw(t *testing.T, db *DB, bucket string, key, val []byte, isLeft bool, expectErr error, finalExpectErr error) {
err := db.Update(func(tx *Tx) error {
var err error

if isLeft {
err = tx.LPushRaw(bucket, key, val)
} else {
err = tx.RPushRaw(bucket, key, val)
}

assertErr(t, err, expectErr)

return nil
})
assertErr(t, err, finalExpectErr)
}

func txExpireList(t *testing.T, db *DB, bucket string, key []byte, ttl uint32, expectErr error) {
err := db.Update(func(tx *Tx) error {
err := tx.ExpireList(bucket, key, ttl)
Expand Down Expand Up @@ -670,7 +678,6 @@ func TestDB_Close(t *testing.T) {

func TestDB_ErrThenReadWrite(t *testing.T) {
runNutsDBTest(t, nil, func(t *testing.T, db *DB) {

bucket := "testForDeadLock"
err = db.View(
func(tx *Tx) error {
Expand Down Expand Up @@ -794,7 +801,6 @@ func withDBOption(t *testing.T, opt Options, fn func(t *testing.T, db *DB)) {
}

func withDefaultDB(t *testing.T, fn func(t *testing.T, db *DB)) {

tmpdir, _ := os.MkdirTemp("", "nutsdb")
opt := DefaultOptions
opt.Dir = tmpdir
Expand All @@ -813,7 +819,6 @@ func withRAMIdxDB(t *testing.T, fn func(t *testing.T, db *DB)) {
}

func TestDB_HintKeyValAndRAMIdxMode_RestartDB(t *testing.T) {

opts := DefaultOptions
runNutsDBTest(t, &opts, func(t *testing.T, db *DB) {
bucket := "bucket"
Expand Down
21 changes: 0 additions & 21 deletions docs/user_guides/data-structure.md
Expand Up @@ -189,27 +189,6 @@ if err := db.Update(
```
</details>
<details>
<summary><b>LSet</b></summary>
Sets the list element at index to value.
```go
if err := db.Update(
func(tx *nutsdb.Tx) error {
bucket := "bucketForList"
key := []byte("myList")
if err := tx.LSet(bucket, key, 0, []byte("val11")); err != nil {
return err
} else {
fmt.Println("LSet ok, index 0 item value => val11")
}
return nil
}); err != nil {
log.Fatal(err)
}
```
</details>
<details>
<summary><b>LTrim</b></summary>
Trims an existing list so that it will contain only the specified range of elements specified. The offsets start and stop are zero-based indexes 0 being the first element of the list (the head of the list), 1 being the next element and so on.Start and end can also be negative numbers indicating offsets from the end of the list, where -1 is the last element of the list, -2 the penultimate element and so on.
Expand Down
4 changes: 3 additions & 1 deletion errors.go
@@ -1,6 +1,8 @@
package nutsdb

import "errors"
import (
"errors"
)

// IsDBClosed is true if the error indicates the db was closed.
func IsDBClosed(err error) bool {
Expand Down
4 changes: 2 additions & 2 deletions errors_test.go
Expand Up @@ -2,15 +2,15 @@ package nutsdb

import (
"fmt"
"github.com/stretchr/testify/require"
"testing"

"github.com/stretchr/testify/require"

"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)

func TestIsKeyNotFound(t *testing.T) {

ts := []struct {
err error

Expand Down
19 changes: 1 addition & 18 deletions examples/list/main.go
Expand Up @@ -54,8 +54,6 @@ func main() {

testLRange()

testLSet()

testLPeek()

testRPeek()
Expand Down Expand Up @@ -151,6 +149,7 @@ func testLPop() {
log.Fatal(err)
}
}

func testRPop() {
if err := db.Update(
func(tx *nutsdb.Tx) error {
Expand Down Expand Up @@ -221,21 +220,6 @@ func testLRem() {
fmt.Println("LRem count : ", count, string(value))
}

func testLSet() {
if err := db.Update(
func(tx *nutsdb.Tx) error {
key := []byte("myList")
err := tx.LSet(bucket, key, 0, []byte("val11"))
if err != nil {
return err
}
fmt.Println("LSet ok, index 0 item value => val11")
return nil
}); err != nil {
log.Fatal(err)
}
}

func testLPeek() {
if err := db.View(
func(tx *nutsdb.Tx) error {
Expand All @@ -250,7 +234,6 @@ func testLPeek() {
}); err != nil {
log.Fatal(err)
}

}

func testRPeek() {
Expand Down
32 changes: 0 additions & 32 deletions list.go
Expand Up @@ -24,9 +24,6 @@ var (
// ErrListNotFound is returned when the list not found.
ErrListNotFound = errors.New("the list not found")

// ErrIndexOutOfRange is returned when use LSet function set index out of range.
ErrIndexOutOfRange = errors.New("index out of range")

// ErrCount is returned when count is error.
ErrCount = errors.New("err count")

Expand Down Expand Up @@ -265,35 +262,6 @@ func (l *List) LRem(key string, count int, cmp func(r *Record) (bool, error)) er
return nil
}

// LSet sets the list element at index to value.
func (l *List) LSet(key string, index int, r *Record) error {
if l.IsExpire(key) {
return ErrListNotFound
}
if _, ok := l.Items[key]; !ok {
return ErrListNotFound
}

size, _ := l.Size(key)
if index >= size || index < 0 {
return ErrIndexOutOfRange
}

list := l.Items[key]
allItems := list.AllItems()
for i, item := range allItems {
if i != index {
continue
}

if !list.InsertRecord(item.key, r) {
return ErrBuildListIndex
}
}

return nil
}

// LTrim trim an existing list so that it will contain only the specified range of elements specified.
func (l *List) LTrim(key string, start, end int) error {
if l.IsExpire(key) {
Expand Down
21 changes: 0 additions & 21 deletions list_test.go
Expand Up @@ -188,27 +188,6 @@ func TestList_LRem(t *testing.T) {
ListCmp(t, list, key, expectRecords, false)
}

func TestList_LSet(t *testing.T) {
list := NewList()
expectRecords := generateRecords(5)
key := string(GetTestBytes(0))
seqInfo := HeadTailSeq{Head: initialListSeq, Tail: initialListSeq + 1}

for i := 0; i < len(expectRecords); i++ {
seq := generateSeq(&seqInfo, false)
newKey := encodeListKey([]byte(key), seq)
expectRecords[i].H.Key = newKey
expectRecords[i].H.Meta.KeySize = uint32(len(newKey))
ListPush(t, list, string(newKey), expectRecords[i], false, nil)
}

err := list.LSet(key, 3, expectRecords[4])
require.NoError(t, err)

expectRecords[3] = expectRecords[4]
ListCmp(t, list, key, expectRecords, false)
}

func TestList_LTrim(t *testing.T) {
list := NewList()
expectRecords := generateRecords(5)
Expand Down

0 comments on commit 551d8b9

Please sign in to comment.