From 800322969612ee461c3328a768881bdeb7bed90f Mon Sep 17 00:00:00 2001 From: Leither Date: Tue, 1 Dec 2015 08:50:42 +0800 Subject: [PATCH 1/3] LPut LPut add --- ledis/t_list.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/ledis/t_list.go b/ledis/t_list.go index 079498e..33c0c04 100644 --- a/ledis/t_list.go +++ b/ledis/t_list.go @@ -321,6 +321,37 @@ func (db *DB) LPop(key []byte) ([]byte, error) { func (db *DB) LPush(key []byte, args ...[]byte) (int64, error) { return db.lpush(key, listHeadSeq, args...) } +func (db *DB) LSet(key []byte, index int32, value []byte) error { + if err := checkKeySize(key); err != nil { + return err + } + + var seq int32 + var headSeq int32 + var tailSeq int32 + //var size int32 + var err error + t := db.listBatch + t.Lock() + defer t.Unlock() + metaKey := db.lEncodeMetaKey(key) + + headSeq, tailSeq, _, err = db.lGetMeta(nil, metaKey) + if err != nil { + return err + } + + if index >= 0 { + seq = headSeq + index + } else { + seq = tailSeq + index + 1 + } + + sk := db.lEncodeListKey(key, seq) + t.Put(sk, value) + err = t.Commit() + return err +} func (db *DB) LRange(key []byte, start int32, stop int32) ([][]byte, error) { if err := checkKeySize(key); err != nil { From df27eeedeca42c377fbadbe77361054f241259ea Mon Sep 17 00:00:00 2001 From: Leither Date: Thu, 10 Dec 2015 12:08:14 +0800 Subject: [PATCH 2/3] Update t_list.go --- ledis/t_list.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ledis/t_list.go b/ledis/t_list.go index 33c0c04..96eefb3 100644 --- a/ledis/t_list.go +++ b/ledis/t_list.go @@ -346,7 +346,9 @@ func (db *DB) LSet(key []byte, index int32, value []byte) error { } else { seq = tailSeq + index + 1 } - + if seq < headSeq || seq > tailSeq { + return errListIndex + } sk := db.lEncodeListKey(key, seq) t.Put(sk, value) err = t.Commit() From 7c8bcb26d8e84e93e05598efb1e91aa4daf3145e Mon Sep 17 00:00:00 2001 From: Leither Date: Thu, 10 Dec 2015 12:09:10 +0800 Subject: [PATCH 3/3] Update const.go --- ledis/const.go | 1 + 1 file changed, 1 insertion(+) diff --git a/ledis/const.go b/ledis/const.go index 9b2daaf..7a0450f 100644 --- a/ledis/const.go +++ b/ledis/const.go @@ -50,6 +50,7 @@ var ( errHashFieldSize = errors.New("invalid hash field size") errZSetMemberSize = errors.New("invalid zset member size") errExpireValue = errors.New("invalid expire value") + errListIndex = errors.New("invalid list index") ) const (