diff --git a/backend_test.go b/backend_test.go index ad42766e0..9bba5f290 100644 --- a/backend_test.go +++ b/backend_test.go @@ -335,11 +335,9 @@ func testDBIterator(t *testing.T, backend BackendType) { func verifyIterator(t *testing.T, itr Iterator, expected []int64, msg string) { var list []int64 for itr.Valid() { - key, err := itr.Key() - assert.NoError(t, err) + key := itr.Key() list = append(list, bytes2Int64(key)) - err = itr.Next() - assert.NoError(t, err) + itr.Next() } assert.Equal(t, expected, list, msg) } diff --git a/boltdb.go b/boltdb.go index eecdc0bc7..5b9d22027 100644 --- a/boltdb.go +++ b/boltdb.go @@ -135,6 +135,7 @@ func (bdb *BoltDB) Print() error { if err != nil { return err } + return nil } func (bdb *BoltDB) Stats() map[string]string { @@ -310,34 +311,31 @@ func (itr *boltDBIterator) Valid() bool { return true } -func (itr *boltDBIterator) Next() error { - if err := itr.assertIsValid(); err != nil { - return err - } +func (itr *boltDBIterator) Next() { + itr.assertIsValid() if itr.isReverse { itr.currentKey, itr.currentValue = itr.itr.Prev() } else { itr.currentKey, itr.currentValue = itr.itr.Next() } - return nil } -func (itr *boltDBIterator) Key() ([]byte, error) { - if err := itr.assertIsValid(); err != nil { - return nil, err - } - return append([]byte{}, itr.currentKey...), nil +func (itr *boltDBIterator) Key() []byte { + itr.assertIsValid() + return append([]byte{}, itr.currentKey...) } -func (itr *boltDBIterator) Value() ([]byte, error) { - if err := itr.assertIsValid(); err != nil { - return nil, err - } +func (itr *boltDBIterator) Value() []byte { + itr.assertIsValid() var value []byte if itr.currentValue != nil { value = append([]byte{}, itr.currentValue...) } - return value, nil + return value +} + +func (itr *boltDBIterator) Error() error { + return nil } func (itr *boltDBIterator) Close() { @@ -347,11 +345,10 @@ func (itr *boltDBIterator) Close() { } } -func (itr *boltDBIterator) assertIsValid() error { +func (itr *boltDBIterator) assertIsValid() { if !itr.Valid() { - return errors.New("boltdb-iterator is invalid") + panic("boltdb-iterator is invalid") } - return nil } // nonEmptyKey returns a []byte("nil") if key is empty. diff --git a/boltdb_test.go b/boltdb_test.go index 3fd472fb6..0fd12bbe2 100644 --- a/boltdb_test.go +++ b/boltdb_test.go @@ -20,8 +20,6 @@ func TestBoltDBNewBoltDB(t *testing.T) { db.Close() } -//TODO: ADD TESTS!!! - func BenchmarkBoltDBRandomReadsWrites(b *testing.B) { name := fmt.Sprintf("test_%x", randStr(12)) db, err := NewBoltDB(name, "") diff --git a/c_level_db.go b/c_level_db.go index 2fdf9ca6a..8bfaed6ee 100644 --- a/c_level_db.go +++ b/c_level_db.go @@ -122,21 +122,14 @@ func (db *CLevelDB) Close() error { // Implements DB. func (db *CLevelDB) Print() error { - itr := db.Iterator(nil, nil) + itr, err := db.Iterator(nil, nil) + if err != nil { + return err + } defer itr.Close() - var err error - for ; itr.Valid(); err = itr.Next() { - if err != nil { - return errors.Wrap(err, "next") - } - key, err := itr.Key() - if err != nil { - return errors.Wrap(err, "key") - } - value, err := itr.Value() - if err != nil { - return errors.Wrap(err, "value") - } + for ; itr.Valid(); itr.Next() { + key := itr.Key() + value := itr.Value() fmt.Printf("[%X]:\t[%X]\n", key, value) } return nil @@ -303,55 +296,45 @@ func (itr cLevelDBIterator) Valid() bool { return true } -func (itr cLevelDBIterator) Key() ([]byte, error) { - if err := itr.assertNoError(); err != nil { - return nil, err - } - if err = itr.assertIsValid(); err != nil { - return nil, err - } - return itr.source.Key(), nil +func (itr cLevelDBIterator) Key() []byte { + itr.assertNoError() + itr.assertIsValid() + return itr.source.Key() } -func (itr cLevelDBIterator) Value() ([]byte, error) { - if err := itr.assertNoError(); err != nil { - return nil, err - } - if err = itr.assertIsValid(); err != nil { - return nil, err - } - return itr.source.Value(), nil +func (itr cLevelDBIterator) Value() []byte { + itr.assertNoError() + itr.assertIsValid() + return itr.source.Value() } -func (itr cLevelDBIterator) Next() error { - if err := itr.assertNoError(); err != nil { - return err - } - if err = itr.assertIsValid(); err != nil { - return err - } +func (itr cLevelDBIterator) Next() { + itr.assertNoError() + itr.assertIsValid() if itr.isReverse { itr.source.Prev() } else { itr.source.Next() } - return nil +} + +func (itr cLevelDBIterator) Error() error { + return itr.source.GetError() } func (itr cLevelDBIterator) Close() { itr.source.Close() } -func (itr cLevelDBIterator) assertNoError() error { - if err := itr.source.GetError(); err != nil { - return err +func (itr cLevelDBIterator) assertNoError() { + err := itr.source.GetError() + if err != nil { + panic(err) } - return nil } -func (itr cLevelDBIterator) assertIsValid() error { +func (itr cLevelDBIterator) assertIsValid() { if !itr.Valid() { - return errors.New("cLevelDBIterator is invalid") + panic("cLevelDBIterator is invalid") } - return nil } diff --git a/common_test.go b/common_test.go index d594727d0..d17c49e00 100644 --- a/common_test.go +++ b/common_test.go @@ -34,8 +34,8 @@ func checkNext(t *testing.T, itr Iterator, expected bool) { require.Equal(t, expected, valid) } -func checkNextErrors(t *testing.T, itr Iterator) { - assert.Error(t, itr.Next(), "checkNextErrors expected an error but didn't") +func checkNextPanics(t *testing.T, itr Iterator) { + assert.Panics(t, func() { itr.Next() }, "checkNextPanics expected an error but didn't") } func checkDomain(t *testing.T, itr Iterator, start, end []byte) { @@ -45,11 +45,9 @@ func checkDomain(t *testing.T, itr Iterator, start, end []byte) { } func checkItem(t *testing.T, itr Iterator, key []byte, value []byte) { - v, err := itr.Value() - assert.NoError(t, err) + v := itr.Value() - k, err := itr.Key() - assert.NoError(t, err) + k := itr.Key() assert.Exactly(t, key, k) assert.Exactly(t, value, v) @@ -57,21 +55,17 @@ func checkItem(t *testing.T, itr Iterator, key []byte, value []byte) { func checkInvalid(t *testing.T, itr Iterator) { checkValid(t, itr, false) - checkKeyErrors(t, itr) - checkValueErrors(t, itr) - checkNextErrors(t, itr) + checkKeyPanics(t, itr) + checkValuePanics(t, itr) + checkNextPanics(t, itr) } -func checkKeyErrors(t *testing.T, itr Iterator) { - key, err := itr.Key() - assert.Empty(t, key) - assert.Error(t, err, "checkKeyErrors expected an error but didn't") +func checkKeyPanics(t *testing.T, itr Iterator) { + assert.Panics(t, func() { itr.Key() }, "checkKeyPanics expected panic but didn't") } -func checkValueErrors(t *testing.T, itr Iterator) { - value, err := itr.Value() - assert.Empty(t, value) - assert.Error(t, err, "checkValueErrors expected an error but didn't") +func checkValuePanics(t *testing.T, itr Iterator) { + assert.Panics(t, func() { itr.Value() }) } func newTempDB(t *testing.T, backend BackendType) (db DB, dbDir string) { @@ -190,16 +184,18 @@ func (mockIterator) Valid() bool { return false } -func (mockIterator) Next() error { +func (mockIterator) Next() {} + +func (mockIterator) Key() []byte { return nil } -func (mockIterator) Key() ([]byte, error) { - return nil, nil +func (mockIterator) Value() []byte { + return nil } -func (mockIterator) Value() ([]byte, error) { - return nil, nil +func (mockIterator) Error() error { + return nil } func (mockIterator) Close() { diff --git a/db_test.go b/db_test.go index 03728fd5f..1e531509d 100644 --- a/db_test.go +++ b/db_test.go @@ -22,7 +22,7 @@ func TestDBIteratorSingleKey(t *testing.T) { checkValid(t, itr, true) checkNext(t, itr, false) checkValid(t, itr, false) - checkNextErrors(t, itr) + checkNextPanics(t, itr) // Once invalid... checkInvalid(t, itr) @@ -53,7 +53,7 @@ func TestDBIteratorTwoKeys(t *testing.T) { checkNext(t, itr, false) checkValid(t, itr, false) - checkNextErrors(t, itr) + checkNextPanics(t, itr) // Once invalid... checkInvalid(t, itr) @@ -83,12 +83,9 @@ func TestDBIteratorMany(t *testing.T) { assert.NoError(t, err) defer itr.Close() - for ; itr.Valid(); err = itr.Next() { - assert.NoError(t, err) - key, err := itr.Key() - assert.NoError(t, err) - value, err = itr.Value() - assert.NoError(t, err) + for ; itr.Valid(); itr.Next() { + key := itr.Key() + value = itr.Value() value1, err := db.Get(key) assert.NoError(t, err) assert.Equal(t, value1, value) diff --git a/go_level_db.go b/go_level_db.go index 9f264a1db..0a162c084 100644 --- a/go_level_db.go +++ b/go_level_db.go @@ -271,9 +271,7 @@ func (itr *goLevelDBIterator) Valid() bool { } // Panic on DB error. No way to recover. - if err := itr.assertNoError(); err != nil { - panic(err) - } + itr.assertNoError() // If source is invalid, invalid. if !itr.source.Valid() { @@ -303,45 +301,36 @@ func (itr *goLevelDBIterator) Valid() bool { } // Implements Iterator. -func (itr *goLevelDBIterator) Key() ([]byte, error) { +func (itr *goLevelDBIterator) Key() []byte { // Key returns a copy of the current key. // See https://github.com/syndtr/goleveldb/blob/52c212e6c196a1404ea59592d3f1c227c9f034b2/leveldb/iterator/iter.go#L88 - if err := itr.assertNoError(); err != nil { - return nil, err - } - if err := itr.assertIsValid(); err != nil { - return nil, err - } - return cp(itr.source.Key()), nil + itr.assertNoError() + itr.assertIsValid() + return cp(itr.source.Key()) } // Implements Iterator. -func (itr *goLevelDBIterator) Value() ([]byte, error) { +func (itr *goLevelDBIterator) Value() []byte { // Value returns a copy of the current value. // See https://github.com/syndtr/goleveldb/blob/52c212e6c196a1404ea59592d3f1c227c9f034b2/leveldb/iterator/iter.go#L88 - if err := itr.assertNoError(); err != nil { - return nil, err - } - if err := itr.assertIsValid(); err != nil { - return nil, err - } - return cp(itr.source.Value()), nil + itr.assertNoError() + itr.assertIsValid() + return cp(itr.source.Value()) } // Implements Iterator. -func (itr *goLevelDBIterator) Next() error { - if err := itr.assertNoError(); err != nil { - return err - } - if err := itr.assertIsValid(); err != nil { - return err - } +func (itr *goLevelDBIterator) Next() { + itr.assertNoError() + itr.assertIsValid() if itr.isReverse { itr.source.Prev() } else { itr.source.Next() } - return nil +} + +func (itr *goLevelDBIterator) Error() error { + return itr.source.Error() } // Implements Iterator. @@ -349,16 +338,15 @@ func (itr *goLevelDBIterator) Close() { itr.source.Release() } -func (itr *goLevelDBIterator) assertNoError() error { - if err := itr.source.Error(); err != nil { - return err +func (itr *goLevelDBIterator) assertNoError() { + err := itr.source.Error() + if err != nil { + panic(err) } - return nil } -func (itr goLevelDBIterator) assertIsValid() error { +func (itr goLevelDBIterator) assertIsValid() { if !itr.Valid() { - return errors.New("goLevelDBIterator is invalid") + panic("goLevelDBIterator is invalid") } - return nil } diff --git a/mem_db.go b/mem_db.go index 4215e32a3..a88e764ef 100644 --- a/mem_db.go +++ b/mem_db.go @@ -4,8 +4,6 @@ import ( "fmt" "sort" "sync" - - "github.com/pkg/errors" ) func init() { @@ -209,34 +207,30 @@ func (itr *memDBIterator) Valid() bool { } // Implements Iterator. -func (itr *memDBIterator) Next() error { - err := itr.assertIsValid() - if err != nil { - return err - } +func (itr *memDBIterator) Next() { + itr.assertIsValid() itr.cur++ - return nil } // Implements Iterator. -func (itr *memDBIterator) Key() ([]byte, error) { - if err := itr.assertIsValid(); err != nil { - return nil, err - } - return []byte(itr.keys[itr.cur]), nil +func (itr *memDBIterator) Key() []byte { + itr.assertIsValid() + return []byte(itr.keys[itr.cur]) } // Implements Iterator. -func (itr *memDBIterator) Value() ([]byte, error) { - if err := itr.assertIsValid(); err != nil { - return nil, err - } +func (itr *memDBIterator) Value() []byte { + itr.assertIsValid() key := []byte(itr.keys[itr.cur]) bytes, err := itr.db.Get(key) if err != nil { - return nil, err + return nil } - return bytes, nil + return bytes +} + +func (itr *memDBIterator) Error() error { + return nil } // Implements Iterator. @@ -245,11 +239,10 @@ func (itr *memDBIterator) Close() { itr.db = nil } -func (itr *memDBIterator) assertIsValid() error { +func (itr *memDBIterator) assertIsValid() { if !itr.Valid() { - return errors.New("memDBIterator is invalid") + panic("memDBIterator is invalid") } - return nil } //---------------------------------------- diff --git a/mem_db_test.go b/mem_db_test.go index 55998cf2b..7f6468ee6 100644 --- a/mem_db_test.go +++ b/mem_db_test.go @@ -16,18 +16,18 @@ func TestMemDB_Iterator(t *testing.T) { defer itr.Close() assert.False(t, itr.Valid()) - db.Set([]byte("foo"), []byte("bar")) + err = db.Set([]byte("foo"), []byte("bar")) + assert.NoError(t, err) // single iteration itr, err = db.Iterator(nil, nil) assert.NoError(t, err) defer itr.Close() - key, err := itr.Key() - assert.NoError(t, err) + key := itr.Key() assert.True(t, itr.Valid()) assert.Equal(t, []byte("foo"), key) - value, err := itr.Value() - assert.NoError(t, err) + + value := itr.Value() assert.Equal(t, []byte("bar"), value) itr.Next() assert.False(t, itr.Valid()) diff --git a/prefix_db.go b/prefix_db.go index 1ec2ac824..b5fe41ac3 100644 --- a/prefix_db.go +++ b/prefix_db.go @@ -4,8 +4,6 @@ import ( "bytes" "fmt" "sync" - - "github.com/pkg/errors" ) // IteratePrefix is a convenience function for iterating over a key domain @@ -135,12 +133,8 @@ func (pdb *PrefixDB) Iterator(start, end []byte) (Iterator, error) { if err != nil { return nil, err } - return newPrefixIterator( - pdb.prefix, - start, - end, - itr, - ), nil + + return newPrefixIterator(pdb.prefix, start, end, itr) } // Implements DB. @@ -159,12 +153,8 @@ func (pdb *PrefixDB) ReverseIterator(start, end []byte) (Iterator, error) { if err != nil { return nil, err } - return newPrefixIterator( - pdb.prefix, - start, - end, - ritr, - ), nil + + return newPrefixIterator(pdb.prefix, start, end, ritr) } // Implements DB. @@ -216,18 +206,9 @@ func (pdb *PrefixDB) Print() error { return err } defer itr.Close() - for ; itr.Valid(); err = itr.Next() { - if err != nil { - return errors.Wrap(err, "next") - } - key, err := itr.Key() - if err != nil { - return errors.Wrap(err, "key") - } - value, err := itr.Value() - if err != nil { - return errors.Wrap(err, "value") - } + for ; itr.Valid(); itr.Next() { + key := itr.Key() + value := itr.Value() fmt.Printf("[%X]:\t[%X]\n", key, value) } return nil @@ -300,19 +281,23 @@ type prefixIterator struct { valid bool } -func newPrefixIterator(prefix, start, end []byte, source Iterator) *prefixIterator { - // Ignoring the error here as the iterator is invalid - // but this is being conveyed in the below if statement - key, _ := source.Key() //nolint:errcheck +func newPrefixIterator(prefix, start, end []byte, source Iterator) (*prefixIterator, error) { - if !source.Valid() || !bytes.HasPrefix(key, prefix) { - return &prefixIterator{ - prefix: prefix, - start: start, - end: end, - source: source, - valid: false, - } + pitrInvalid := &prefixIterator{ + prefix: prefix, + start: start, + end: end, + source: source, + valid: false, + } + + if !source.Valid() { + return pitrInvalid, nil + } + key := source.Key() + + if !bytes.HasPrefix(key, prefix) { + return pitrInvalid, nil } return &prefixIterator{ prefix: prefix, @@ -320,7 +305,7 @@ func newPrefixIterator(prefix, start, end []byte, source Iterator) *prefixIterat end: end, source: source, valid: true, - } + }, nil } func (itr *prefixIterator) Domain() (start []byte, end []byte) { @@ -331,45 +316,35 @@ func (itr *prefixIterator) Valid() bool { return itr.valid && itr.source.Valid() } -func (itr *prefixIterator) Next() error { +func (itr *prefixIterator) Next() { if !itr.valid { - return errors.New("prefixIterator invalid; cannot call Next()") - } - err := itr.source.Next() - if err != nil { - return err + panic("prefixIterator invalid; cannot call Next()") } - key, err := itr.source.Key() - if err != nil { - return err - } - if !itr.source.Valid() || !bytes.HasPrefix(key, itr.prefix) { + itr.source.Next() + + if !itr.source.Valid() || !bytes.HasPrefix(itr.source.Key(), itr.prefix) { itr.valid = false - return nil } - return nil } -func (itr *prefixIterator) Key() (key []byte, err error) { +func (itr *prefixIterator) Key() (key []byte) { if !itr.valid { - return nil, errors.New("prefixIterator invalid; cannot call Key()") - } - key, err = itr.source.Key() - if err != nil { - return nil, err + panic("prefixIterator invalid; cannot call Key()") } - return stripPrefix(key, itr.prefix), nil + key = itr.source.Key() + return stripPrefix(key, itr.prefix) } -func (itr *prefixIterator) Value() (value []byte, err error) { +func (itr *prefixIterator) Value() (value []byte) { if !itr.valid { - return nil, errors.New("prefixIterator invalid; cannot call Value()") + panic("prefixIterator invalid; cannot call Value()") } - value, err = itr.source.Value() - if err != nil { - return nil, err - } - return value, nil + value = itr.source.Value() + return value +} + +func (itr *prefixIterator) Error() error { + return itr.source.Error() } func (itr *prefixIterator) Close() { @@ -383,7 +358,7 @@ func stripPrefix(key []byte, prefix []byte) (stripped []byte) { panic("should not happen") } if !bytes.Equal(key[:len(prefix)], prefix) { - panic("should not happn") + panic("should not happen") } return key[len(prefix):] } diff --git a/remotedb/grpcdb/server.go b/remotedb/grpcdb/server.go index 47e83316b..7c3431d83 100644 --- a/remotedb/grpcdb/server.go +++ b/remotedb/grpcdb/server.go @@ -166,14 +166,9 @@ func (s *server) Iterator(query *protodb.Entity, dis protodb.DB_IteratorServer) func (s *server) handleIterator(it db.Iterator, sendFunc func(*protodb.Iterator) error) error { for it.Valid() { start, end := it.Domain() - key, err := it.Key() - if err != nil { - return err - } - value, err := it.Value() - if err != nil { - return err - } + key := it.Key() + value := it.Value() + out := &protodb.Iterator{ Domain: &protodb.Domain{Start: start, End: end}, Valid: it.Valid(), @@ -184,10 +179,8 @@ func (s *server) handleIterator(it db.Iterator, sendFunc func(*protodb.Iterator) return err } - // Finally move the iterator forward - if err = it.Next(); err != nil { - return err - } + // Finally move the iterator forward, + it.Next() } return nil diff --git a/remotedb/remotedb.go b/remotedb/remotedb.go index 8e4ee7f4a..bef708673 100644 --- a/remotedb/remotedb.go +++ b/remotedb/remotedb.go @@ -156,32 +156,34 @@ func (rItr *reverseIterator) Domain() (start, end []byte) { } // Next advances the current reverseIterator -func (rItr *reverseIterator) Next() error { +func (rItr *reverseIterator) Next() { var err error rItr.cur, err = rItr.dric.Recv() if err != nil { - return errors.Errorf("RemoteDB.ReverseIterator.Next error: %v", err) + panic(fmt.Sprintf("RemoteDB.ReverseIterator.Next error: %v", err)) } - return nil } -func (rItr *reverseIterator) Key() ([]byte, error) { +func (rItr *reverseIterator) Key() []byte { if rItr.cur == nil { - return nil, errors.New("key does not exist") + panic("key does not exist") } - return rItr.cur.Key, nil + return rItr.cur.Key } -func (rItr *reverseIterator) Value() ([]byte, error) { +func (rItr *reverseIterator) Value() []byte { if rItr.cur == nil { - return nil, errors.New("key does not exist") + panic("key does not exist") } - return rItr.cur.Value, nil + return rItr.cur.Value } -func (rItr *reverseIterator) Close() { +func (rItr *reverseIterator) Error() error { + return nil } +func (rItr *reverseIterator) Close() {} + // iterator implements the db.Iterator by retrieving // streamed iterators from the remote backend as // needed. It is NOT safe for concurrent usage, @@ -205,27 +207,30 @@ func (itr *iterator) Domain() (start, end []byte) { } // Next advances the current iterator -func (itr *iterator) Next() error { +func (itr *iterator) Next() { var err error itr.cur, err = itr.dic.Recv() if err != nil { - return errors.Errorf("remoteDB.Iterator.Next error: %v", err) + panic(fmt.Sprintf("remoteDB.Iterator.Next error: %v", err)) } - return nil } -func (itr *iterator) Key() ([]byte, error) { +func (itr *iterator) Key() []byte { if itr.cur == nil { - return nil, errors.New("key does not exist") + return nil } - return itr.cur.Key, nil + return itr.cur.Key } -func (itr *iterator) Value() ([]byte, error) { +func (itr *iterator) Value() []byte { if itr.cur == nil { - return nil, errors.New("current poisition is not valid") + panic("current poisition is not valid") } - return itr.cur.Value, nil + return itr.cur.Value +} + +func (itr *iterator) Error() error { + return nil } func (itr *iterator) Close() { diff --git a/remotedb/remotedb_test.go b/remotedb/remotedb_test.go index 2b508bb39..c8b866e0c 100644 --- a/remotedb/remotedb_test.go +++ b/remotedb/remotedb_test.go @@ -52,18 +52,13 @@ func TestRemoteDB(t *testing.T) { itr, err := client.Iterator(nil, nil) require.NoError(t, err) - err = itr.Next() - require.NoError(t, err) + itr.Next() - key1, err := itr.Key() - require.NoError(t, err) - - value, err := itr.Value() - require.NoError(t, err) + key1 := itr.Key() + value := itr.Value() require.Equal(t, key1, []byte("key-1")) require.Equal(t, value, []byte("value-1")) - require.Error(t, itr.Next()) itr.Close() // Set some more keys @@ -82,28 +77,21 @@ func TestRemoteDB(t *testing.T) { itr, err = client.Iterator(nil, nil) require.NoError(t, err) - err = itr.Next() - require.NoError(t, err) + itr.Next() - key1, err = itr.Key() - require.NoError(t, err) + key1 = itr.Key() - value, err = itr.Value() - require.NoError(t, err) + value = itr.Value() require.Equal(t, key1, []byte("key-1")) require.Equal(t, value, []byte("value-1")) - err = itr.Next() - require.NoError(t, err) + itr.Next() - key1, err = itr.Key() - require.NoError(t, err) + key1 = itr.Key() - value, err = itr.Value() - require.NoError(t, err) + value = itr.Value() require.Equal(t, key1, []byte("key-2")) require.Equal(t, value, []byte("value-2")) - require.Error(t, itr.Next()) itr.Close() // Deletion diff --git a/rocks_db.go b/rocks_db.go index e8ee29b87..409ff5af3 100644 --- a/rocks_db.go +++ b/rocks_db.go @@ -9,7 +9,6 @@ import ( "runtime" "github.com/tecbot/gorocksdb" - "github.com/pkg/errors" ) func init() { @@ -76,12 +75,11 @@ func (db *RocksDB) Get(key []byte) ([]byte, error) { // Implements DB. func (db *RocksDB) Has(key []byte) (bool, error) { - bytes, err := db.Get(key) - if err != nil { - return false, err - } - return bytes != nil, nil + bytes, err := db.Get(key) + if err != nil { + return false, err } + return bytes != nil, nil } // Implements DB. @@ -96,7 +94,7 @@ func (db *RocksDB) Set(key []byte, value []byte) error { } // Implements DB. -func (db *RocksDB) SetSync(key []byte, value []byte) error{ +func (db *RocksDB) SetSync(key []byte, value []byte) error { key = nonNilBytes(key) value = nonNilBytes(value) err := db.db.Put(db.woSync, key, value) @@ -123,6 +121,7 @@ func (db *RocksDB) DeleteSync(key []byte) error { if err != nil { return nil } + return nil } func (db *RocksDB) DB() *gorocksdb.DB { @@ -130,16 +129,20 @@ func (db *RocksDB) DB() *gorocksdb.DB { } // Implements DB. -func (db *RocksDB) Close() { +func (db *RocksDB) Close() error { db.ro.Destroy() db.wo.Destroy() db.woSync.Destroy() db.db.Close() + return nil } // Implements DB. -func (db *RocksDB) Print() error{ - itr := db.Iterator(nil, nil) +func (db *RocksDB) Print() error { + itr, err := db.Iterator(nil, nil) + if err != nil { + return err + } defer itr.Close() for ; itr.Valid(); itr.Next() { key := itr.Key() @@ -265,7 +268,7 @@ func (itr rocksDBIterator) Domain() ([]byte, []byte) { return itr.start, itr.end } -func (itr rocksDBIterator) Valid() (bool, error) { +func (itr rocksDBIterator) Valid() bool { // Once invalid, forever invalid. if itr.isInvalid { @@ -273,14 +276,12 @@ func (itr rocksDBIterator) Valid() (bool, error) { } // Panic on DB error. No way to recover. - if err := itr.assertNoError(); err != nil { - return nil, err - } + itr.assertNoError() // If source is invalid, invalid. if !itr.source.Valid() { itr.isInvalid = true - return false, nil + return false } // If key is end or past it, invalid. @@ -303,57 +304,46 @@ func (itr rocksDBIterator) Valid() (bool, error) { return true } -func (itr rocksDBIterator) Key() ([]byte, error) { - if err := itr.assertNoError(); err != nil { - return nil, err - } - if err := itr.assertIsValid(); err != nil { - return nil, err - } - return moveSliceToBytes(itr.source.Key()), nil +func (itr rocksDBIterator) Key() []byte { + itr.assertNoError() + itr.assertIsValid() + return moveSliceToBytes(itr.source.Key()) } func (itr rocksDBIterator) Value() []byte { - if err := itr.assertNoError(); err != nil { - return nil, err - } - if err := itr.assertIsValid(); err != nil { - return nil, err - } - return moveSliceToBytes(itr.source.Value()), nil + itr.assertNoError() + itr.assertIsValid() + return moveSliceToBytes(itr.source.Value()) } -func (itr rocksDBIterator) Next() error { - if err := itr.assertNoError(); err != nil { - return nil, err - } - if err := itr.assertIsValid(); err != nil { - return nil, err - } +func (itr rocksDBIterator) Next() { + itr.assertNoError() + itr.assertIsValid() if itr.isReverse { itr.source.Prev() } else { itr.source.Next() } - return nil +} + +func (itr rocksDBIterator) Error() error { + return itr.source.Err() } func (itr rocksDBIterator) Close() { itr.source.Close() } -func (itr rocksDBIterator) assertNoError() error { +func (itr rocksDBIterator) assertNoError() { if err := itr.source.Err(); err != nil { - return err + panic(err) } - return nil } -func (itr rocksDBIterator) assertIsValid() error { +func (itr rocksDBIterator) assertIsValid() { if !itr.Valid() { - return errors.New("rocksDBIterator is invalid") + panic("rocksDBIterator is invalid") } - return nil } // moveSliceToBytes will free the slice and copy out a go []byte diff --git a/types.go b/types.go index 3cf8d5466..1de83e96f 100644 --- a/types.go +++ b/types.go @@ -103,15 +103,20 @@ type Iterator interface { // Next moves the iterator to the next sequential key in the database, as // defined by order of iteration. - Next() error + // If Valid returns false, this method will panic. + Next() // Key returns the key of the cursor. + // If Valid returns false, this method will panic. // CONTRACT: key readonly []byte - Key() (key []byte, err error) + Key() (key []byte) // Value returns the value of the cursor. + // If Valid returns false, this method will panic. // CONTRACT: value readonly []byte - Value() (value []byte, err error) + Value() (value []byte) + + Error() error // Close releases the Iterator. Close()