Skip to content

Commit

Permalink
fixed tikv seek to iter
Browse files Browse the repository at this point in the history
  • Loading branch information
zhs committed Jan 10, 2019
1 parent 1aa1d59 commit aab980d
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 244 deletions.
235 changes: 1 addition & 234 deletions command/hashes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,89 +105,6 @@ func TestHDel(t *testing.T) {
assert.Equal(t, ":2", lines[0])
}

func TestHSetNX(t *testing.T) {
// init
key := "hash-key"
initHashes(t, key, 3)

// case 1
ctx := ContextTest("hsetnx", key, "1", "haha")
Call(ctx)
lines := ctxLines(ctx.Out)
assert.Equal(t, ":0", lines[0])
ctx = ContextTest("hget", key, "1")
Call(ctx)
lines = ctxLines(ctx.Out)
assert.Equal(t, "$3", lines[0])
assert.Equal(t, "bar", lines[1])

// case 2
ctx = ContextTest("hsetnx", key, "4", "haha")
Call(ctx)
lines = ctxLines(ctx.Out)
assert.Equal(t, ":1", lines[0])
ctx = ContextTest("hget", key, "4")
Call(ctx)
lines = ctxLines(ctx.Out)
assert.Equal(t, "$4", lines[0])
assert.Equal(t, "haha", lines[1])

// case 3
ctx = ContextTest("hsetnx", key, "4", "bar")
Call(ctx)
lines = ctxLines(ctx.Out)
assert.Equal(t, ":0", lines[0])
ctx = ContextTest("hget", key, "4")
Call(ctx)
lines = ctxLines(ctx.Out)
assert.Equal(t, "$4", lines[0])
assert.Equal(t, "haha", lines[1])

// end
clearHashes(t, key)
}
func TestHSet(t *testing.T) {
// init
key := "hash-key"
initHashes(t, key, 3)

// case 1
ctx := ContextTest("hset", key, "1", "haha")
Call(ctx)
lines := ctxLines(ctx.Out)
assert.Equal(t, ":0", lines[0])
ctx = ContextTest("hget", key, "1")
Call(ctx)
lines = ctxLines(ctx.Out)
assert.Equal(t, "$4", lines[0])
assert.Equal(t, "haha", lines[1])

// case 2
ctx = ContextTest("hset", key, "4", "haha")
Call(ctx)
lines = ctxLines(ctx.Out)
assert.Equal(t, ":1", lines[0])
ctx = ContextTest("hget", key, "4")
Call(ctx)
lines = ctxLines(ctx.Out)
assert.Equal(t, "$4", lines[0])
assert.Equal(t, "haha", lines[1])

// case 3
ctx = ContextTest("hset", key, "4", "bar")
Call(ctx)
lines = ctxLines(ctx.Out)
assert.Equal(t, ":0", lines[0])
ctx = ContextTest("hget", key, "4")
Call(ctx)
lines = ctxLines(ctx.Out)
assert.Equal(t, "$3", lines[0])
assert.Equal(t, "bar", lines[1])

// end
clearHashes(t, key)
}

func TestHGet(t *testing.T) {
// init
key := "hash-key"
Expand All @@ -209,61 +126,6 @@ func TestHGet(t *testing.T) {
// end
clearHashes(t, key)
}

func TestHGetAll(t *testing.T) {
// init
key := "hash-key"
initHashes(t, key, 3)

// case 1
ctx := ContextTest("hgetall", key)
Call(ctx)
lines := ctxLines(ctx.Out)
assert.Equal(t, "*6", lines[0])
assert.Equal(t, "$1", lines[1])
assert.Equal(t, "1", lines[2])
assert.Equal(t, "$3", lines[3])
assert.Equal(t, "bar", lines[4])
assert.Equal(t, "$1", lines[5])
assert.Equal(t, "2", lines[6])
assert.Equal(t, "$3", lines[7])
assert.Equal(t, "bar", lines[8])
assert.Equal(t, "$1", lines[9])
assert.Equal(t, "3", lines[10])
assert.Equal(t, "$3", lines[11])
assert.Equal(t, "bar", lines[12])

// case 2
ctx = ContextTest("hset", key, "foo", "haha")
Call(ctx)
lines = ctxLines(ctx.Out)
assert.Equal(t, ":1", lines[0])

ctx = ContextTest("hgetall", key)
Call(ctx)
lines = ctxLines(ctx.Out)
assert.Equal(t, "*8", lines[0])
assert.Equal(t, "$1", lines[1])
assert.Equal(t, "1", lines[2])
assert.Equal(t, "$3", lines[3])
assert.Equal(t, "bar", lines[4])
assert.Equal(t, "$1", lines[5])
assert.Equal(t, "2", lines[6])
assert.Equal(t, "$3", lines[7])
assert.Equal(t, "bar", lines[8])
assert.Equal(t, "$1", lines[9])
assert.Equal(t, "3", lines[10])
assert.Equal(t, "$3", lines[11])
assert.Equal(t, "bar", lines[12])
assert.Equal(t, "$3", lines[13])
assert.Equal(t, "foo", lines[14])
assert.Equal(t, "$4", lines[15])
assert.Equal(t, "haha", lines[16])

// end
clearHashes(t, key)
}

func TestHMGet(t *testing.T) {
// init
key := "hash-key"
Expand Down Expand Up @@ -387,6 +249,7 @@ func TestHSetNX(t *testing.T) {
// end
clearHashes(t, key)
}

func TestHSet(t *testing.T) {
// init
key := "hash-key"
Expand Down Expand Up @@ -429,28 +292,6 @@ func TestHSet(t *testing.T) {
clearHashes(t, key)
}

func TestHGet(t *testing.T) {
// init
key := "hash-key"
initHashes(t, key, 3)

// case 1
ctx := ContextTest("hget", key, "1")
Call(ctx)
lines := ctxLines(ctx.Out)
assert.Equal(t, "$3", lines[0])
assert.Equal(t, "bar", lines[1])

// case 2
ctx = ContextTest("hget", key, "5")
Call(ctx)
lines = ctxLines(ctx.Out)
assert.Equal(t, "$-1", lines[0])

// end
clearHashes(t, key)
}

func TestHGetAll(t *testing.T) {
// init
key := "hash-key"
Expand Down Expand Up @@ -505,80 +346,6 @@ func TestHGetAll(t *testing.T) {
clearHashes(t, key)
}

func TestHMGet(t *testing.T) {
// init
key := "hash-key"
initHashes(t, key, 3)

// case 1
ctx := ContextTest("hmget", key, "1", "2", "3")
Call(ctx)
lines := ctxLines(ctx.Out)
assert.Equal(t, "*3", lines[0])
assert.Equal(t, "$3", lines[1])
assert.Equal(t, "bar", lines[2])
assert.Equal(t, "$3", lines[3])
assert.Equal(t, "bar", lines[4])
assert.Equal(t, "$3", lines[5])
assert.Equal(t, "bar", lines[6])

// case 2
ctx = ContextTest("hset", key, "foo", "haha")
Call(ctx)
lines = ctxLines(ctx.Out)
assert.Equal(t, ":1", lines[0])

ctx = ContextTest("hmget", key, "1", "foo")
Call(ctx)
lines = ctxLines(ctx.Out)
assert.Equal(t, "*2", lines[0])
assert.Equal(t, "$3", lines[1])
assert.Equal(t, "bar", lines[2])
assert.Equal(t, "$4", lines[3])
assert.Equal(t, "haha", lines[4])

// end
clearHashes(t, key)
}
func TestHMSet(t *testing.T) {
// init
key := "hash-key"
initHashes(t, key, 3)

// case 1
ctx := ContextTest("hmset", key, "1", "ha", "2", "haha", "3", "hahaha")
Call(ctx)
lines := ctxLines(ctx.Out)
assert.Equal(t, "+OK", lines[0])
ctx = ContextTest("hgetall", key)
Call(ctx)
lines = ctxLines(ctx.Out)
assert.Equal(t, "*6", lines[0])
assert.Equal(t, "1", lines[2])
assert.Equal(t, "ha", lines[4])
assert.Equal(t, "2", lines[6])
assert.Equal(t, "haha", lines[8])
assert.Equal(t, "3", lines[10])
assert.Equal(t, "hahaha", lines[12])

// case 2
ctx = ContextTest("hmset", key, "foo", "bar")
Call(ctx)
lines = ctxLines(ctx.Out)
assert.Equal(t, "+OK", lines[0])
ctx = ContextTest("hmget", key, "1", "foo")
Call(ctx)
lines = ctxLines(ctx.Out)
assert.Equal(t, "*2", lines[0])
assert.Equal(t, "$2", lines[1])
assert.Equal(t, "ha", lines[2])
assert.Equal(t, "$3", lines[3])
assert.Equal(t, "bar", lines[4])

// end
clearHashes(t, key)
}

func TestHExists(t *testing.T) {
// init
key := "hash-key"
Expand Down
12 changes: 2 additions & 10 deletions db/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,6 @@ func hashItemKey(key []byte, field []byte) []byte {
return append(dkey, field...)
}

func slotGC(txn *Transaction, objID []byte) error {
key := MetaSlotKey(txn.db, objID, nil)
if err := gc(txn.t, key); err != nil {
return err
}
return nil
}

func (hash *Hash) calculateSlotID(limit int64) int64 {
if !hash.isMetaSlot() || limit <= 1 {
return 0
Expand Down Expand Up @@ -588,7 +580,7 @@ func (hash *Hash) HScan(cursor []byte, f func(key, val []byte) bool) error {
dkey := DataKey(hash.txn.db, hash.meta.ID)
prefix := hashItemKey(dkey, nil)
ikey := hashItemKey(dkey, cursor)
iter, err := hash.txn.t.Seek(ikey)
iter, err := hash.txn.t.Iter(ikey, nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -744,7 +736,7 @@ func (hash *Hash) getSliceSlot(index int64) (*Slot, error) {
var rawSlots [][]byte
prefixKey := MetaSlotKey(hash.txn.db, hash.meta.ID, nil)
startKey := MetaSlotKey(hash.txn.db, hash.meta.ID, EncodeInt64(index))
iter, err := hash.txn.t.Seek(startKey)
iter, err := hash.txn.t.Iter(startKey, nil)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit aab980d

Please sign in to comment.