Skip to content

Commit

Permalink
db add setTTL api
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshi-099 committed Apr 3, 2024
1 parent a68b531 commit 4fff748
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 57 deletions.
4 changes: 2 additions & 2 deletions benchmark/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func benchHSet() {
}

func benchLRPush() {
fmt.Println("========== LRPush ==========")
fmt.Println("========== RPush ==========")
fmt.Println("size: 100*10000 enties")
fmt.Println("desc: value 10 bytes")

Expand All @@ -158,7 +158,7 @@ func benchLRPush() {
for i := 0; i < N; i++ {
t1 := time.Now()
k := fmt.Sprintf("%010d", i)
db.LRPush("mylist", k)
db.RPush("mylist", k)
quant.Add(float64(time.Since(t1)))
}

Expand Down
68 changes: 31 additions & 37 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ const (
mergeTypeOr
mergeTypeXOr

listDirectionLeft = 'L'
listDirectionRight = 'R'

fileLockName = "FLOCK"
)

Expand All @@ -46,8 +43,10 @@ const (
OpSRemove
OpSMerge // union, inter, diff
// list
OpLPush // lpush, rpush
OpLPop // lpop, rpop
OpLPush
OpRPush
OpLPop
OpRPop
// bitmap
OpBitSet
OpBitFlip
Expand Down Expand Up @@ -160,27 +159,22 @@ var cmdTable = []Cmd{
}
}},
{OpLPush, func(db *DB, reader *codeman.Reader) error {
// direct, key, items
direct := reader.Byte()
key := reader.Str()
items := reader.StrSlice()

if direct == listDirectionLeft {
return db.LLPush(key, items...)
}
return db.LRPush(key, items...)
// key, items
return db.LPush(reader.Str(), reader.StrSlice()...)
}},
{OpLPop, func(db *DB, reader *codeman.Reader) (err error) {
// direct, key
direct := reader.Byte()
key := reader.Str()

if direct == listDirectionLeft {
_, err = db.LLPop(key)
} else {
_, err = db.LRPop(key)
}
return
{OpRPush, func(db *DB, reader *codeman.Reader) error {
// key, items
return db.RPush(reader.Str(), reader.StrSlice()...)
}},
{OpLPop, func(db *DB, reader *codeman.Reader) error {
// key
_, err := db.LPop(reader.Str())
return err
}},
{OpRPop, func(db *DB, reader *codeman.Reader) error {
// key
_, err := db.RPop(reader.Str())
return err
}},
{OpBitSet, func(db *DB, reader *codeman.Reader) error {
// key, val, offsets
Expand Down Expand Up @@ -610,25 +604,25 @@ func (db *DB) SDiff(dst string, src ...string) error {
return nil
}

// LLPush
func (db *DB) LLPush(key string, items ...string) error {
// LPush
func (db *DB) LPush(key string, items ...string) error {
ls, err := db.fetchList(key, true)
if err != nil {
return err
}
db.encode(newCodec(OpLPush).Byte(listDirectionLeft).Str(key).StrSlice(items))
db.encode(newCodec(OpLPush).Str(key).StrSlice(items))
ls.LPush(items...)

return nil
}

// LRPush
func (db *DB) LRPush(key string, items ...string) error {
// RPush
func (db *DB) RPush(key string, items ...string) error {
ls, err := db.fetchList(key, true)
if err != nil {
return err
}
db.encode(newCodec(OpLPush).Byte(listDirectionRight).Str(key).StrSlice(items))
db.encode(newCodec(OpRPush).Str(key).StrSlice(items))
ls.RPush(items...)

return nil
Expand All @@ -647,8 +641,8 @@ func (db *DB) LIndex(key string, i int) (string, error) {
return res, nil
}

// LLPop
func (db *DB) LLPop(key string) (string, error) {
// LPop
func (db *DB) LPop(key string) (string, error) {
ls, err := db.fetchList(key)
if err != nil {
return "", err
Expand All @@ -657,13 +651,13 @@ func (db *DB) LLPop(key string) (string, error) {
if !ok {
return "", ErrEmptyList
}
db.encode(newCodec(OpLPop).Byte(listDirectionLeft).Str(key))
db.encode(newCodec(OpLPop).Str(key))

return res, nil
}

// LRPop
func (db *DB) LRPop(key string) (string, error) {
// RPop
func (db *DB) RPop(key string) (string, error) {
ls, err := db.fetchList(key)
if err != nil {
return "", err
Expand All @@ -672,7 +666,7 @@ func (db *DB) LRPop(key string) (string, error) {
if !ok {
return "", ErrEmptyList
}
db.encode(newCodec(OpLPop).Byte(listDirectionRight).Str(key))
db.encode(newCodec(OpRPop).Str(key))

return res, nil
}
Expand Down
35 changes: 17 additions & 18 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,9 @@ func TestList(t *testing.T) {
val := randString()

if i%2 == 0 {
assert.Nil(db.LRPush(key, val))

assert.Nil(db.RPush(key, val))
} else {
assert.Nil(db.LLPush(key, val))
assert.Nil(db.LPush(key, val))
// check
res, err := db.LIndex(key, 0)
assert.Nil(err)
Expand All @@ -269,11 +268,11 @@ func TestList(t *testing.T) {

if i > 4000 {
if i%2 == 0 {
res, err := db.LRPop(key)
res, err := db.RPop(key)
assert.Nil(err)
assert.Equal(res, val)
} else {
res, err := db.LLPop(key)
res, err := db.LPop(key)
assert.Nil(err)
assert.Equal(res, val)
}
Expand All @@ -289,20 +288,20 @@ func TestList(t *testing.T) {
// Error
db.HSet("map", "key", []byte("value"))

err = db.LLPush("map", "1")
err = db.LPush("map", "1")
assert.ErrorContains(err, ErrWrongType.Error())

err = db.LRPush("map", "1")
err = db.RPush("map", "1")
assert.ErrorContains(err, ErrWrongType.Error())

_, err = db.LKeys("map")
assert.ErrorContains(err, ErrWrongType.Error())

res, err := db.LLPop("map")
res, err := db.LPop("map")
assert.Equal(res, "")
assert.ErrorContains(err, ErrWrongType.Error())

res, err = db.LRPop("map")
res, err = db.RPop("map")
assert.Equal(res, "")
assert.ErrorContains(err, ErrWrongType.Error())

Expand All @@ -316,14 +315,14 @@ func TestList(t *testing.T) {

// empty list
{
db.LRPush("list", "1")
db.LRPop("list")
db.RPush("list", "1")
db.RPop("list")

res, err = db.LLPop("list")
res, err = db.LPop("list")
assert.Equal(res, "")
assert.Equal(err, ErrEmptyList)

res, err = db.LRPop("list")
res, err = db.RPop("list")
assert.Equal(res, "")
assert.Equal(err, ErrEmptyList)

Expand All @@ -332,7 +331,7 @@ func TestList(t *testing.T) {
assert.Equal(err, ErrIndexOutOfRange)

for i := 0; i < 100; i++ {
db.LRPush("list", randString())
db.RPush("list", randString())
}
}

Expand All @@ -352,10 +351,10 @@ func TestList(t *testing.T) {
assert.Nil(err)

for i := 0; i < 1000; i++ {
db.LRPush("ls", strconv.Itoa(i))
db.RPush("ls", strconv.Itoa(i))
}
for i := 0; i < 1000; i++ {
v, err := db.LLPop("ls")
v, err := db.LPop("ls")
assert.Equal(v, strconv.Itoa(i))
assert.Nil(err)
}
Expand All @@ -366,10 +365,10 @@ func TestList(t *testing.T) {
assert.Nil(err)

for i := 0; i < 1000; i++ {
db.LLPush("ls", strconv.Itoa(i))
db.LPush("ls", strconv.Itoa(i))
}
for i := 0; i < 1000; i++ {
v, err := db.LRPop("ls")
v, err := db.RPop("ls")
assert.Equal(v, strconv.Itoa(i))
assert.Nil(err)
}
Expand Down

0 comments on commit 4fff748

Please sign in to comment.