From cff780bc74dac81fd665bc549d1892a1dd7184d6 Mon Sep 17 00:00:00 2001 From: Jeffrey Picard Date: Mon, 28 Feb 2022 16:09:54 -0500 Subject: [PATCH] fix some issues with test suite --- db/db.go | 33 +++++++++++++++++++-------------- db/db_resolve.go | 6 +++--- db/db_stack/db_stack.go | 2 +- db/db_test.go | 38 ++++++++++++++++++++++++++++++++++---- 4 files changed, 57 insertions(+), 22 deletions(-) diff --git a/db/db.go b/db/db.go index 5836f9f..ac5132c 100644 --- a/db/db.go +++ b/db/db.go @@ -249,10 +249,17 @@ func (ps *PathSegment) String() string { return ps.name } +// // BisectRight returns the index of the first element in the list that is greater than or equal to the value. +// // https://stackoverflow.com/questions/29959506/is-there-a-go-analog-of-pythons-bisect-module +// func BisectRight(arr []uint32, val uint32) uint32 { +// i := sort.Search(len(arr), func(i int) bool { return arr[i] >= val }) +// return uint32(i) +// } + // BisectRight returns the index of the first element in the list that is greater than or equal to the value. // https://stackoverflow.com/questions/29959506/is-there-a-go-analog-of-pythons-bisect-module -func BisectRight(arr []uint32, val uint32) uint32 { - i := sort.Search(len(arr), func(i int) bool { return arr[i] >= val }) +func BisectRight(arr []interface{}, val uint32) uint32 { + i := sort.Search(len(arr), func(i int) bool { return arr[i].(uint32) >= val }) return uint32(i) } @@ -509,19 +516,18 @@ func GetDBColumnFamlies(name string, cfNames []string) (*ReadOnlyDBColumnFamily, } func Advance(db *ReadOnlyDBColumnFamily, height uint32) { - /* - def advance(self, height: int): - tx_count = self.db.prefix_db.tx_count.get(height).tx_count - assert len(self.db.tx_counts) == height, f"{len(self.db.tx_counts)} != {height}" - self.db.tx_counts.append(tx_count) - self.db.headers.append(self.db.prefix_db.header.get(height, deserialize_value=False)) - */ // TODO: assert tx_count not in self.db.tx_counts, f'boom {tx_count} in {len(self.db.tx_counts)} tx counts' - if db.TxCounts.Len() >= 0 && db.TxCounts.Len() != height { + if db.TxCounts.Len() != height { log.Println("Error: tx count len:", db.TxCounts.Len(), "height:", height) return } + headerObj, err := GetHeader(db, height) + if err != nil { + log.Println("Error getting header:", err) + return + } + txCountObj, err := GetTxCount(db, height) if err != nil { log.Println("Error getting tx count:", err) @@ -529,7 +535,7 @@ func Advance(db *ReadOnlyDBColumnFamily, height uint32) { } txCount := txCountObj.TxCount db.TxCounts.Push(txCount) - + db.Headers.Push(headerObj) } func Unwind(db *ReadOnlyDBColumnFamily) { @@ -611,12 +617,13 @@ func DetectChanges(db *ReadOnlyDBColumnFamily) error { //TODO: ClearCache db.LastState = state //TODO: block count metric - // self.last_state = state ???:w //TODO: update blocked streams //TODO: update filtered streams } } + + return nil /* if self.last_state: while True: @@ -645,8 +652,6 @@ func DetectChanges(db *ReadOnlyDBColumnFamily) error { self.db.filtering_channel_hashes ) */ - - return nil } /* diff --git a/db/db_resolve.go b/db/db_resolve.go index 633e01f..ad19bbf 100644 --- a/db/db_resolve.go +++ b/db/db_resolve.go @@ -37,9 +37,9 @@ func PrepareResolveResult( return nil, err } - var txCounts []uint32 - txCounts = db.TxCounts.GetSlice().([]uint32) - txCounts = txCounts[db.TxCounts.Len()-1:] + var txCounts []interface{} + txCounts = db.TxCounts.GetSlice() + txCounts = txCounts[:db.TxCounts.Len()] height := BisectRight(txCounts, txNum) createdHeight := BisectRight(txCounts, rootTxNum) lastTakeoverHeight := controllingClaim.Height diff --git a/db/db_stack/db_stack.go b/db/db_stack/db_stack.go index 950e2f7..29322e9 100644 --- a/db/db_stack/db_stack.go +++ b/db/db_stack/db_stack.go @@ -51,6 +51,6 @@ func (s *SliceBackedStack) Size() int { return len(s.slice) } -func (s *SliceBackedStack) GetSlice() interface{} { +func (s *SliceBackedStack) GetSlice() []interface{} { return s.slice } diff --git a/db/db_test.go b/db/db_test.go index 4917bc5..0e072f7 100644 --- a/db/db_test.go +++ b/db/db_test.go @@ -6,6 +6,7 @@ import ( "encoding/hex" "log" "os" + "strings" "testing" dbpkg "github.com/lbryio/hub/db" @@ -41,7 +42,13 @@ func OpenAndFillTmpDBColumnFamlies(filePath string) (*dbpkg.ReadOnlyDBColumnFami } var handleMap map[string]*grocksdb.ColumnFamilyHandle = make(map[string]*grocksdb.ColumnFamilyHandle) - for _, cfNameRune := range records[0][0] { + // Make sure we always create the TxCounts column family + var cfNameRunes string = records[0][0] + txCountPrefix := string(prefixes.TxCount) + if !strings.Contains(cfNameRunes, txCountPrefix) { + cfNameRunes = cfNameRunes + txCountPrefix + } + for _, cfNameRune := range cfNameRunes { cfName := string(cfNameRune) log.Println(cfName) handle, err := db.CreateColumnFamily(opts, cfName) @@ -75,11 +82,34 @@ func OpenAndFillTmpDBColumnFamlies(filePath string) (*dbpkg.ReadOnlyDBColumnFami } myDB := &dbpkg.ReadOnlyDBColumnFamily{ - DB: db, - Handles: handleMap, - Opts: grocksdb.NewDefaultReadOptions(), + DB: db, + Handles: handleMap, + Opts: grocksdb.NewDefaultReadOptions(), + BlockedStreams: make(map[string][]byte), + BlockedChannels: make(map[string][]byte), + FilteredStreams: make(map[string][]byte), + FilteredChannels: make(map[string][]byte), + TxCounts: nil, + LastState: nil, + Height: 0, + Headers: nil, } + // err = dbpkg.ReadDBState(myDB) //TODO: Figure out right place for this + // if err != nil { + // return nil, nil, nil, err + // } + + err = dbpkg.InitTxCounts(myDB) + if err != nil { + return nil, nil, nil, err + } + + // err = dbpkg.InitHeaders(myDB) + // if err != nil { + // return nil, nil, nil, err + // } + return myDB, records, toDefer, nil }