• R/O
  • HTTP
  • SSH
  • HTTPS

vapor: Commit

Golang implemented sidechain for Bytom


Commit MetaInfo

Revision4cc569a9c42bcd78f9796c0dd9b5159b4827bf08 (tree)
Time2019-07-02 16:17:31
Authormars <mars@byto...>
Commitermars

Log Message

fix paging

Change Summary

Incremental Difference

--- a/database/leveldb/db.go
+++ b/database/leveldb/db.go
@@ -12,7 +12,7 @@ type DB interface {
1212 NewBatch() Batch
1313 Iterator() Iterator
1414 IteratorPrefix([]byte) Iterator
15- IteratorPrefixWithStart(Prefix, start []byte) Iterator
15+ IteratorPrefixWithStart(Prefix, start []byte, isReverse bool) Iterator
1616
1717 // For debugging
1818 Print()
--- a/database/leveldb/db_test.go
+++ b/database/leveldb/db_test.go
@@ -22,7 +22,7 @@ func TestDBIteratorSingleKey(t *testing.T) {
2222 defer os.RemoveAll(dir)
2323
2424 db.Set([]byte("1"), []byte("value_1"))
25- itr := db.IteratorPrefixWithStart(nil, nil)
25+ itr := db.IteratorPrefixWithStart(nil, nil, false)
2626 require.Equal(t, []byte(""), itr.Key())
2727 require.Equal(t, true, itr.Next())
2828 require.Equal(t, []byte("1"), itr.Key())
@@ -39,12 +39,12 @@ func TestDBIteratorTwoKeys(t *testing.T) {
3939 db.SetSync([]byte("1"), []byte("value_1"))
4040 db.SetSync([]byte("2"), []byte("value_1"))
4141
42- itr := db.IteratorPrefixWithStart(nil, []byte("1"))
42+ itr := db.IteratorPrefixWithStart(nil, []byte("1"), false)
4343
4444 require.Equal(t, []byte("1"), itr.Key())
4545
4646 require.Equal(t, true, itr.Next())
47- itr = db.IteratorPrefixWithStart(nil, []byte("2"))
47+ itr = db.IteratorPrefixWithStart(nil, []byte("2"), false)
4848
4949 require.Equal(t, false, itr.Next())
5050 })
@@ -69,7 +69,7 @@ func TestDBIterator(t *testing.T) {
6969 db.SetSync([]byte("aaa22"), []byte("value_2"))
7070 db.SetSync([]byte("bbb22"), []byte("value_3"))
7171
72- itr := db.IteratorPrefixWithStart([]byte("aaa"), []byte("aaa1"))
72+ itr := db.IteratorPrefixWithStart([]byte("aaa"), []byte("aaa1"), false)
7373 defer itr.Release()
7474
7575 require.Equal(t, true, itr.Next())
@@ -77,7 +77,7 @@ func TestDBIterator(t *testing.T) {
7777
7878 require.Equal(t, false, itr.Next())
7979
80- itr = db.IteratorPrefixWithStart([]byte("aaa"), nil)
80+ itr = db.IteratorPrefixWithStart([]byte("aaa"), nil, false)
8181
8282 require.Equal(t, true, itr.Next())
8383 require.Equal(t, []byte("aaa1"), itr.Key())
@@ -87,6 +87,47 @@ func TestDBIterator(t *testing.T) {
8787
8888 require.Equal(t, false, itr.Next())
8989
90- itr = db.IteratorPrefixWithStart([]byte("bbb"), []byte("aaa1"))
90+ itr = db.IteratorPrefixWithStart([]byte("bbb"), []byte("aaa1"), false)
91+ require.Equal(t, false, itr.Next())
92+}
93+
94+func TestDBIteratorReverse(t *testing.T) {
95+ dirname, err := ioutil.TempDir("", "db_common_test")
96+ require.Nil(t, err)
97+
98+ db, err := NewGoLevelDB("testdb", dirname)
99+ if err != nil {
100+ t.Fatal(err)
101+ }
102+
103+ defer func() {
104+ db.Close()
105+ os.RemoveAll(dirname)
106+ }()
107+
108+ db.SetSync([]byte("aaa1"), []byte("value_1"))
109+ db.SetSync([]byte("aaa22"), []byte("value_2"))
110+ db.SetSync([]byte("bbb22"), []byte("value_3"))
111+
112+ itr := db.IteratorPrefixWithStart([]byte("aaa"), []byte("aaa22"), true)
113+ defer itr.Release()
114+
115+ require.Equal(t, true, itr.Next())
116+ require.Equal(t, []byte("aaa1"), itr.Key())
117+
118+ require.Equal(t, false, itr.Next())
119+
120+ itr = db.IteratorPrefixWithStart([]byte("aaa"), nil, true)
121+
122+ require.Equal(t, []byte("aaa22"), itr.Key())
123+
124+ require.Equal(t, true, itr.Next())
125+ require.Equal(t, []byte("aaa1"), itr.Key())
126+
127+ require.Equal(t, false, itr.Next())
128+
129+ require.Equal(t, false, itr.Next())
130+
131+ itr = db.IteratorPrefixWithStart([]byte("bbb"), []byte("aaa1"), true)
91132 require.Equal(t, false, itr.Next())
92133 }
--- a/database/leveldb/go_level_db.go
+++ b/database/leveldb/go_level_db.go
@@ -118,18 +118,31 @@ func (db *GoLevelDB) Stats() map[string]string {
118118 }
119119
120120 type goLevelDBIterator struct {
121- source iterator.Iterator
122- start []byte
121+ source iterator.Iterator
122+ start []byte
123+ isReverse bool
123124 }
124125
125-func newGoLevelDBIterator(source iterator.Iterator, start []byte) *goLevelDBIterator {
126- if start != nil {
127- source.Seek(start)
126+func newGoLevelDBIterator(source iterator.Iterator, start []byte, isReverse bool) *goLevelDBIterator {
127+ if isReverse {
128+ if start == nil {
129+ source.Last()
130+ } else {
131+ valid := source.Seek(start)
132+ if !valid {
133+ source.Last()
134+ }
135+ }
136+ } else {
137+ if start != nil {
138+ source.Seek(start)
139+ }
128140 }
129141
130142 return &goLevelDBIterator{
131- source: source,
132- start: start,
143+ source: source,
144+ start: start,
145+ isReverse: isReverse,
133146 }
134147 }
135148
@@ -161,6 +174,9 @@ func (it *goLevelDBIterator) Error() error {
161174
162175 func (it *goLevelDBIterator) Next() bool {
163176 it.assertNoError()
177+ if it.isReverse {
178+ return it.source.Prev()
179+ }
164180 return it.source.Next()
165181 }
166182
@@ -182,9 +198,9 @@ func (db *GoLevelDB) IteratorPrefix(prefix []byte) Iterator {
182198 return &goLevelDBIterator{source: db.db.NewIterator(util.BytesPrefix(prefix), nil)}
183199 }
184200
185-func (db *GoLevelDB) IteratorPrefixWithStart(Prefix, start []byte) Iterator {
201+func (db *GoLevelDB) IteratorPrefixWithStart(Prefix, start []byte, isReverse bool) Iterator {
186202 itr := db.db.NewIterator(util.BytesPrefix(Prefix), nil)
187- return newGoLevelDBIterator(itr, start)
203+ return newGoLevelDBIterator(itr, start, isReverse)
188204 }
189205
190206 func (db *GoLevelDB) NewBatch() Batch {
--- a/database/leveldb/mem_db.go
+++ b/database/leveldb/mem_db.go
@@ -9,9 +9,11 @@ import (
99 )
1010
1111 func init() {
12- registerDBCreator(MemDBBackendStr, func(name string, dir string) (DB, error) {
13- return NewMemDB(), nil
14- }, false)
12+ /*
13+ registerDBCreator(MemDBBackendStr, func(name string, dir string) (DB, error) {
14+ return NewMemDB(), nil
15+ }, false)
16+ */
1517 }
1618
1719 type MemDB struct {
@@ -163,7 +165,7 @@ func (db *MemDB) IteratorPrefix(prefix []byte) Iterator {
163165 return it
164166 }
165167
166-func (db *MemDB) IteratorPrefixWithStart(Prefix, start []byte) Iterator {
168+func (db *MemDB) IteratorPrefixWithStart(Prefix, start []byte, isReverse bool) Iterator {
167169 db.mtx.Lock()
168170 defer db.mtx.Unlock()
169171
--- a/wallet/indexer.go
+++ b/wallet/indexer.go
@@ -310,7 +310,7 @@ func (w *Wallet) GetTransactions(accountID string, StartTxID string, count uint,
310310 preFix = UnconfirmedTxPrefix
311311 }
312312
313- itr := w.DB.IteratorPrefixWithStart([]byte(preFix), startKey)
313+ itr := w.DB.IteratorPrefixWithStart([]byte(preFix), startKey, true)
314314 defer itr.Release()
315315
316316 for txNum := count; itr.Next() && txNum > 0; txNum-- {
@@ -327,6 +327,8 @@ func (w *Wallet) GetTransactions(accountID string, StartTxID string, count uint,
327327
328328 if unconfirmed {
329329 sort.Sort(SortByTimestamp(annotatedTxs))
330+ } else {
331+ sort.Sort(SortByHeight(annotatedTxs))
330332 }
331333
332334 return annotatedTxs, nil
--- a/wallet/unconfirmed.go
+++ b/wallet/unconfirmed.go
@@ -33,6 +33,13 @@ func (a SortByTimestamp) Len() int { return len(a) }
3333 func (a SortByTimestamp) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
3434 func (a SortByTimestamp) Less(i, j int) bool { return a[i].Timestamp > a[j].Timestamp }
3535
36+// SortByHeight implements sort.Interface for AnnotatedTx slices
37+type SortByHeight []*query.AnnotatedTx
38+
39+func (a SortByHeight) Len() int { return len(a) }
40+func (a SortByHeight) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
41+func (a SortByHeight) Less(i, j int) bool { return a[i].BlockHeight > a[j].BlockHeight }
42+
3643 // AddUnconfirmedTx handle wallet status update when tx add into txpool
3744 func (w *Wallet) AddUnconfirmedTx(txD *protocol.TxDesc) {
3845 if err := w.saveUnconfirmedTx(txD.Tx); err != nil {
Show on old repository browser