Skip to content

Commit

Permalink
db add SetNx
Browse files Browse the repository at this point in the history
  • Loading branch information
xgzlucario committed May 5, 2024
1 parent 34def57 commit e7915cd
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 49 deletions.
6 changes: 3 additions & 3 deletions batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func checkts(batches []*Batch) {
// BatchSet
func (db *DB) BatchSet(batches ...*Batch) {
checkts(batches)

codec := codeman.NewCodec()

for _, b := range batches {
codec = codec.Byte(byte(OpSetTx)).Str(b.Key).Int(b.Timestamp).Bytes(b.Val)
db.m.SetTx(b.Key, b.Val, b.Timestamp)
Expand All @@ -49,8 +49,8 @@ func (db *DB) BatchHSet(key string, batches ...*Batch) error {
if err != nil {
return err
}

codec := newCodec(OpHSetTx).Str(key).Int(int64(len(batches)))

for _, b := range batches {
codec = codec.Str(b.Key).Bytes(b.Val).Int(b.Timestamp)
m.Set(b.Key, b.Val, b.Timestamp)
Expand All @@ -66,8 +66,8 @@ func (db *DB) BatchZSet(key string, batches ...*ZSBatch) error {
if err != nil {
return err
}

codec := newCodec(OpZSet).Str(key).Int(int64(len(batches)))

for _, b := range batches {
codec = codec.Str(b.Key).Int(b.Score)
m.Set(b.Key, b.Score)
Expand Down
12 changes: 10 additions & 2 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,16 @@ func (db *DB) SetTx(key string, val []byte, ts int64) {
db.BatchSet(&Batch{key, val, ts})
}

// SetNx stoe key-value if not exist.
func (db *DB) SetNx(key string, val []byte, ts int64) bool {
_, _, ok := db.m.Get(key)
if ok {
return false
}
db.SetTx(key, val, ts)
return true
}

// SetTTL set expired time of key-value.
func (db *DB) SetTTL(key string, ts int64) bool {
if ts < 0 {
Expand Down Expand Up @@ -406,9 +416,7 @@ func (db *DB) Len() int {

// GC triggers the garbage collection to evict expired kv datas.
func (db *DB) GC() {
db.mu.Lock()
db.m.Migrate()
db.mu.Unlock()
}

// Scan
Expand Down
97 changes: 62 additions & 35 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func createDB() (*DB, error) {
}

func TestDB(t *testing.T) {
t.Parallel()
assert := assert.New(t)
const N = 5000
db, err := createDB()
Expand Down Expand Up @@ -150,6 +151,7 @@ func TestDB(t *testing.T) {
}

func TestHmap(t *testing.T) {
t.Parallel()
assert := assert.New(t)
db, err := createDB()
assert.Nil(err)
Expand Down Expand Up @@ -247,6 +249,7 @@ func randString() string {
}

func TestList(t *testing.T) {
t.Parallel()
assert := assert.New(t)
db, err := createDB()
assert.Nil(err)
Expand Down Expand Up @@ -399,6 +402,7 @@ func TestList(t *testing.T) {
}

func TestSet(t *testing.T) {
t.Parallel()
assert := assert.New(t)
db, err := createDB()
assert.Nil(err)
Expand Down Expand Up @@ -516,6 +520,7 @@ func TestSet(t *testing.T) {
}

func TestBitmap(t *testing.T) {
t.Parallel()
assert := assert.New(t)
db, err := createDB()
assert.Nil(err)
Expand Down Expand Up @@ -637,6 +642,7 @@ func TestBitmap(t *testing.T) {
}

func TestZSet(t *testing.T) {
t.Parallel()
assert := assert.New(t)
db, err := createDB()
assert.Nil(err)
Expand Down Expand Up @@ -757,6 +763,7 @@ func TestZSet(t *testing.T) {
}

func TestInvalidCodec(t *testing.T) {
t.Parallel()
assert := assert.New(t)

// read args.
Expand All @@ -774,12 +781,16 @@ func TestInvalidCodec(t *testing.T) {
assert.Panics(func() {
reader.Byte()
})
assert.Panics(func() {
reader.Int64()
})
assert.Panics(func() {
reader.RawBytes()
})
}

func TestRace(t *testing.T) {
func TestCheckOption(t *testing.T) {
t.Parallel()
assert := assert.New(t)

t.Run("checkOptions", func(t *testing.T) {
Expand All @@ -788,21 +799,19 @@ func TestRace(t *testing.T) {
_, err := Open(options)
assert.NotNil(err)

options.DirPath = "test1"
options.ShardCount = 0
options.DirPath = "README.md"
_, err = Open(options)
assert.NotNil(err)
})

t.Run("open-wal", func(t *testing.T) {
options := DefaultOptions
options.DirPath = "README.md"
_, err := Open(options)
options.DirPath = "test1"
options.ShardCount = 0
_, err = Open(options)
assert.NotNil(err)
})
}

func TestUnmarshalError(t *testing.T) {
t.Parallel()
assert := assert.New(t)

for _, types := range []int64{TypeMap, TypeList, TypeSet, TypeZSet, TypeBitmap} {
Expand All @@ -817,50 +826,68 @@ func TestUnmarshalError(t *testing.T) {
}
}

func TestShrink(t *testing.T) {
func TestIncr(t *testing.T) {
t.Parallel()
assert := assert.New(t)
db, _ := createDB()

for i := 0; i < 10000; i++ {
db.Set(fmt.Sprintf("%06d", i), []byte(fmt.Sprintf("v-%06d", i)))
for i := 0; i < 1000; i++ {
n, err := db.Incr(fmt.Sprintf("key-%d", i), 1)
assert.Equal(n, int64(1))
assert.Nil(err)
}

// shrink
err := db.Shrink()
assert.Nil(err)
for i := 0; i < 1000; i++ {
n, err := db.Incr(fmt.Sprintf("key-%d", i), 1)
assert.Equal(n, int64(2))
assert.Nil(err)
}

// Error
db.Set("ss", []byte("abcde"))
n, err := db.Incr("ss", 1)
assert.Equal(n, int64(0))
assert.NotNil(err)

// Reopen
db.Close()
db, _ = Open(db.GetOptions())

// reopen
db2, _ := Open(db.GetOptions())
for i := 0; i < 10000; i++ {
key := fmt.Sprintf("%06d", i)
val, ts, err := db2.Get(key)
assert.Equal(val, []byte(fmt.Sprintf("v-%06d", i)))
// Get
for i := 0; i < 1000; i++ {
val, ts, err := db.Get(fmt.Sprintf("key-%d", i))
assert.Equal(val, []byte("2"))
assert.Equal(ts, int64(0))
assert.Nil(err)
}

// Shrink
err = db.Shrink()
assert.Nil(err)
}

func TestIncr(t *testing.T) {
func TestSetNx(t *testing.T) {
t.Parallel()
assert := assert.New(t)
db, _ := createDB()

n, err := db.Incr("key", 1)
assert.Equal(n, int64(1))
assert.Nil(err)
ok := db.SetNx("key", []byte("val"), time.Now().Add(time.Second).UnixNano())
assert.True(ok)

n, err = db.Incr("key", 2)
assert.Equal(n, int64(3))
assert.Nil(err)
for i := 0; i < 100; i++ {
ok := db.SetNx("key", []byte("val"), 0)
assert.False(ok)
}

// Error
db.Set("ss", []byte("abcde"))
n, err = db.Incr("ss", 1)
assert.Equal(n, int64(0))
assert.NotNil(err)
// Reopen
db.Close()
db, _ = Open(db.GetOptions())

// Shrink
err = db.Shrink()
assert.Nil(err)
ok = db.SetNx("key", []byte("val"), 0)
assert.False(ok)

time.Sleep(time.Second * 2)

ok = db.SetNx("key", []byte("val"), 0)
assert.True(ok)
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/rosedblabs/wal v1.3.6
github.com/sakeven/RbTree v0.0.0-20240321014605-9899538dc980
github.com/stretchr/testify v1.9.0
github.com/xgzlucario/GigaCache v0.0.0-20240426182053-056e47d54001
github.com/xgzlucario/GigaCache v0.0.0-20240504114334-a0b0c3267b83
github.com/xgzlucario/quicklist v0.0.0-20240428064242-d453ca4cbed3
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f
)
Expand All @@ -28,7 +28,7 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
golang.org/x/arch v0.7.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/sys v0.20.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,18 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/xgzlucario/GigaCache v0.0.0-20240426182053-056e47d54001 h1:WfekAg4NyVcQfiNdBHslRbI+taIllBvTLL6CL5yD75Q=
github.com/xgzlucario/GigaCache v0.0.0-20240426182053-056e47d54001/go.mod h1:sPwGPAuvd9WdiONTmusXGNocqcY5L/J7+st1upAMlX8=
github.com/xgzlucario/GigaCache v0.0.0-20240504114334-a0b0c3267b83 h1:fb65K3a6m6ytmKE5MOwvZ5I3xzwotwfkp+iKNniodg8=
github.com/xgzlucario/GigaCache v0.0.0-20240504114334-a0b0c3267b83/go.mod h1:sPwGPAuvd9WdiONTmusXGNocqcY5L/J7+st1upAMlX8=
github.com/xgzlucario/quicklist v0.0.0-20240428064242-d453ca4cbed3 h1:82nERuq61AXhOexK6sfhE1jPHrP1Zr6NpXNXdYBhoZQ=
github.com/xgzlucario/quicklist v0.0.0-20240428064242-d453ca4cbed3/go.mod h1:1ZgyZNk91XIllYdOPpwP+9L2RCw6QGSy6alTYF+Z0iU=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.7.0 h1:pskyeJh/3AmoQ8CPE95vxHLqp1G1GfGNXTmcl9NEKTc=
golang.org/x/arch v0.7.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f h1:99ci1mjWVBWwJiEKYY6jWa4d2nTQVIEhZIptnrVb1XY=
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f/go.mod h1:/lliqkxwWAhPjf5oSOIJup2XcqJaw8RGS6k3TGEc7GI=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down

0 comments on commit e7915cd

Please sign in to comment.