Skip to content

Commit

Permalink
Merge 370b58f into 3bf2911
Browse files Browse the repository at this point in the history
  • Loading branch information
bysomeone authored Jan 13, 2022
2 parents 3bf2911 + 370b58f commit 96d7aed
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 25 deletions.
37 changes: 16 additions & 21 deletions common/db/table/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,31 +69,27 @@ func (query *Query) ListIndex(indexName string, prefix []byte, primaryKey []byte
if isPrimaryIndex(indexName) || indexName == query.table.getOpt().Primary {
return query.listPrimary(prefix, primaryKey, count, direction)
}
p := query.table.indexPrefix(indexName)
var k []byte
keyPrefix := append(query.table.indexPrefix(indexName), prefix...)
var key []byte
if len(primaryKey) > 0 {
row, err := query.table.GetData(primaryKey)
if err != nil {
return nil, err
}
key, err := query.table.index(row, indexName)
indexKey, err := query.table.index(row, indexName)
if err != nil {
return nil, err
}
//如果存在prefix
////assert prefix
if prefix != nil {
p2 := commonPrefix(prefix, key)
if len(p2) != len(prefix) {
cp := commonPrefix(prefix, indexKey)
if len(cp) != len(prefix) {
return nil, types.ErrNotFound
}
p = append(p, p2...)
}
k = query.table.getIndexKey(indexName, key, row.Primary)
} else {
//这个情况下 k == nil
p = append(p, prefix...)
key = query.table.getIndexKey(indexName, indexKey, row.Primary)
}
values, err := query.kvdb.List(p, k, count, direction)
values, err := query.kvdb.List(keyPrefix, key, count, direction)
if err != nil {
return nil, err
}
Expand All @@ -112,21 +108,20 @@ func (query *Query) ListIndex(indexName string, prefix []byte, primaryKey []byte

//ListPrimary list primary data
func (query *Query) listPrimary(prefix []byte, primaryKey []byte, count, direction int32) (rows []*Row, err error) {
p := query.table.primaryPrefix()
var k []byte
metaPrefix := query.table.primaryPrefix()
var key []byte
if primaryKey != nil {
//asset primary key prefix
if prefix != nil {
p2 := commonPrefix(prefix, primaryKey)
if len(p2) != len(prefix) {
cp := commonPrefix(prefix, primaryKey)
if len(cp) != len(prefix) {
return nil, types.ErrNotFound
}
p = append(p, p2...)
}
k = append(p, primaryKey...)
} else {
p = append(p, prefix...)
key = append(metaPrefix, primaryKey...)
}
values, err := query.kvdb.List(p, k, count, direction)
keyPrefix := append(metaPrefix, prefix...)
values, err := query.kvdb.List(keyPrefix, key, count, direction)
if err != nil {
return nil, err
}
Expand Down
72 changes: 68 additions & 4 deletions common/db/table/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,26 @@ import (
"github.com/stretchr/testify/assert"
)

func TestTransactinList(t *testing.T) {
func testTxTable(t *testing.T) (string, db.DB, db.KVDB, *Table) {
dir, ldb, kvdb := util.CreateTestDB()
defer util.CloseTestDB(dir, ldb)
opt := &Option{
Prefix: "prefix",
Name: "name",
Primary: "Hash",
Index: []string{"From", "To"},
}
cfg := types.NewChain33Config(types.GetDefaultCfgstring())
table, err := NewTable(NewTransactionRow(), kvdb, opt)
assert.Nil(t, err)
return dir, ldb, kvdb, table
}

func TestTransactinList(t *testing.T) {
dir, ldb, kvdb, table := testTxTable(t)
defer util.CloseTestDB(dir, ldb)
cfg := types.NewChain33Config(types.GetDefaultCfgstring())
addr1, priv := util.Genaddress()
tx1 := util.CreateNoneTx(cfg, priv)
err = table.Add(tx1)
err := table.Add(tx1)
assert.Nil(t, err)
tx2 := util.CreateNoneTx(cfg, priv)
err = table.Add(tx2)
Expand Down Expand Up @@ -448,3 +453,62 @@ func (tx *TransactionRow) Get(key string) ([]byte, error) {
}
return nil, types.ErrNotFound
}

func TestTableListPrimary(t *testing.T) {

dir, ldb, _, table := testTxTable(t)
defer util.CloseTestDB(dir, ldb)

cfg := types.NewChain33Config(types.GetDefaultCfgstring())
addr1, priv1 := util.Genaddress()
_, priv2 := util.Genaddress()
tx1 := util.CreateNoneTx(cfg, priv1)
assert.Nil(t, table.Add(tx1))
tx2 := util.CreateNoneTx(cfg, priv2)
assert.Nil(t, table.Add(tx2))
kvs, err := table.Save()
assert.Nil(t, err)
//save to database
util.SaveKVList(ldb, kvs)
// get smaller key
hash := tx1.Hash()
if bytes.Compare(hash, tx2.Hash()) > 0 {
hash = tx2.Hash()
}

// List
rows, err := table.ListIndex("Hash", nil, nil, 10, db.ListASC)
assert.Nil(t, err)
assert.Equal(t, 2, len(rows))
assert.Equal(t, hash, rows[0].Primary)

// List with prefix
rows, _ = table.ListIndex("Hash", hash[:20], nil, 10, db.ListASC)
assert.Equal(t, 1, len(rows))

// List with primary
rows, _ = table.ListIndex("Hash", nil, hash, 10, db.ListASC)
assert.Equal(t, 1, len(rows))
_, err = table.ListIndex("Hash", nil, hash, 10, db.ListDESC)
assert.Equal(t, types.ErrNotFound, err)

// List with primary and prefix
_, err = table.ListIndex("Hash", hash[:20], hash, 10, db.ListASC)
assert.Equal(t, types.ErrNotFound, err)
_, err = table.ListIndex("Hash", hash[:20], hash, 10, db.ListDESC)
assert.Equal(t, types.ErrNotFound, err)

// List index
rows, err = table.ListIndex("From", nil, nil, 10, db.ListDESC)
assert.Nil(t, err)
assert.Equal(t, 2, len(rows))

// List with prefix
rows, _ = table.ListIndex("From", []byte(addr1[:20]), nil, 10, db.ListDESC)
assert.Equal(t, 1, len(rows))
assert.Equal(t, tx1.Hash(), rows[0].Primary)

// List with primary and prefix
_, err = table.ListIndex("From", []byte(addr1)[:20], tx1.Hash(), 10, db.ListASC)
assert.Equal(t, types.ErrNotFound, err)
}

0 comments on commit 96d7aed

Please sign in to comment.