Skip to content

Commit

Permalink
optimize some code
Browse files Browse the repository at this point in the history
  • Loading branch information
damotiansheng committed Sep 30, 2023
1 parent bf6d203 commit 52e6506
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 33 deletions.
5 changes: 0 additions & 5 deletions errors.go
Expand Up @@ -2,7 +2,6 @@ package nutsdb

import (
"errors"
"fmt"
)

// IsDBClosed is true if the error indicates the db was closed.
Expand Down Expand Up @@ -39,7 +38,3 @@ func IsPrefixScan(err error) bool {
func IsPrefixSearchScan(err error) bool {
return errors.Is(err, ErrPrefixSearchScan)
}

func GetMergeReadEntryError(err error) error {
return fmt.Errorf("when merge operation build hintIndex readAt err: %s", err)
}
24 changes: 0 additions & 24 deletions errors_test.go
Expand Up @@ -220,27 +220,3 @@ func TestIsPrefixSearchScan(t *testing.T) {
})
})
}

func TestGetMergeReadEntryError(t *testing.T) {
type args struct {
err error
}
tests := []struct {
name string
args args
wantErr error
}{
{
name: "test error",
args: args{
err: errors.New("test error"),
},
wantErr: fmt.Errorf("when merge operation build hintIndex readAt err: %s", errors.New("test error")),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.wantErr, GetMergeReadEntryError(tt.args.err))
})
}
}
9 changes: 5 additions & 4 deletions merge.go
Expand Up @@ -127,10 +127,11 @@ func (db *DB) merge() error {
err := db.Update(func(tx *Tx) error {
// check if we have a new entry with same key and bucket
if ok := db.isPendingMergeEntry(entry); ok {
switch entry.Meta.Flag {
case DataLPushFlag:
if entry.Meta.Flag == DataLPushFlag {
return tx.LPushRaw(string(entry.Bucket), entry.Key, entry.Value)
case DataRPushFlag:
}

if entry.Meta.Flag == DataRPushFlag {
return tx.RPushRaw(string(entry.Bucket), entry.Key, entry.Value)
}

Check warning on line 136 in merge.go

View check run for this annotation

Codecov / codecov/patch

merge.go#L135-L136

Added lines #L135 - L136 were not covered by tests

Expand Down Expand Up @@ -167,7 +168,7 @@ func (db *DB) merge() error {
if err == io.ErrUnexpectedEOF {
break
}
return GetMergeReadEntryError(err)
return fmt.Errorf("when merge operation build hintIndex readAt err: %s", err)
}
}

Expand Down
4 changes: 4 additions & 0 deletions tx_list_test.go
Expand Up @@ -65,6 +65,8 @@ func TestTx_LPush(t *testing.T) {
pushDataByStartEnd(t, db, bucket, 1, 10, 19, true)
pushDataByStartEnd(t, db, bucket, 2, 20, 29, true)

txPush(t, db, bucket, []byte("012|sas"), GetTestBytes(0), true, ErrSeparatorForListKey, nil)

for i := 0; i < 10; i++ {
txPop(t, db, bucket, GetTestBytes(0), GetTestBytes(9-i), nil, true)
}
Expand Down Expand Up @@ -104,6 +106,8 @@ func TestTx_RPushRaw(t *testing.T) {
txPushRaw(t, db, bucket, key, GetTestBytes(i), false, nil, nil)
}

txPush(t, db, bucket, []byte("012|sas"), GetTestBytes(0), false, ErrSeparatorForListKey, nil)

for i := 0; i <= 100; i++ {
v := GetTestBytes(100 - i)
txPop(t, db, bucket, []byte("0"), v, nil, false)
Expand Down

0 comments on commit 52e6506

Please sign in to comment.