• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

linxGnu / grocksdb / 21472311072

29 Jan 2026 09:13AM UTC coverage: 70.971% (-0.001%) from 70.972%
21472311072

Pull #192

github

web-flow
Merge 4c749d895 into 79bf5809d
Pull Request #192: Adapt RocksDB 10.9.1

40 of 54 new or added lines in 4 files covered. (74.07%)

212 existing lines in 1 file now uncovered.

3985 of 5615 relevant lines covered (70.97%)

0.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.4
/iterator.go
1
package grocksdb
2

3
// #include <stdlib.h>
4
// #include "rocksdb/c.h"
5
import "C"
6

7
import (
8
        "bytes"
9
)
10

11
// Iterator provides a way to seek to specific keys and iterate through
12
// the keyspace from that point, as well as access the values of those keys.
13
//
14
// For example:
15
//
16
//             it := db.NewIterator(readOpts)
17
//             defer it.Close()
18
//
19
//             it.Seek([]byte("foo"))
20
//                        for ; it.Valid(); it.Next() {
21
//                 fmt.Printf("Key: %v Value: %v\n", it.Key().Data(), it.Value().Data())
22
//                        }
23
//
24
//             if err := it.Err(); err != nil {
25
//                 return err
26
//             }
27
type Iterator struct {
28
        c    *C.rocksdb_iterator_t
29
        opts *ReadOptions
30
}
31

32
// NewNativeIterator creates a Iterator object.
33
func newNativeIterator(c *C.rocksdb_iterator_t) *Iterator {
1✔
34
        return &Iterator{c: c}
1✔
35
}
1✔
36

37
// newNativeIteratorWithReadOptions creates a Iterator object with ReadOptions.
38
func newNativeIteratorWithReadOptions(c *C.rocksdb_iterator_t, opts *ReadOptions) *Iterator {
1✔
39
        return &Iterator{
1✔
40
                c:    c,
1✔
41
                opts: opts,
1✔
42
        }
1✔
43
}
1✔
44

45
// Valid returns false only when an Iterator has iterated past either the
46
// first or the last key in the database.
47
func (iter *Iterator) Valid() bool {
1✔
48
        return C.rocksdb_iter_valid(iter.c) != 0
1✔
49
}
1✔
50

51
// ValidForPrefix returns false only when an Iterator has iterated past the
52
// first or the last key in the database or the specified prefix.
53
func (iter *Iterator) ValidForPrefix(prefix []byte) bool {
1✔
54
        if C.rocksdb_iter_valid(iter.c) == 0 {
2✔
55
                return false
1✔
56
        }
1✔
57

58
        key := iter.Key()
1✔
59
        result := bytes.HasPrefix(key.Data(), prefix)
1✔
60
        key.Free()
1✔
61
        return result
1✔
62
}
63

64
// Key returns the key the iterator currently holds.
65
func (iter *Iterator) Key() *Slice {
1✔
66
        var cLen C.size_t
1✔
67
        cKey := C.rocksdb_iter_key(iter.c, &cLen)
1✔
68
        if cKey == nil {
1✔
69
                return nil
×
70
        }
×
71
        return &Slice{data: cKey, size: cLen, freed: true}
1✔
72
}
73

74
func (iter *Iterator) KeySlice() OptimizedSlice {
1✔
75
        return newNativeOptimizeSlice(C.rocksdb_iter_key_slice(iter.c))
1✔
76
}
1✔
77

78
// Timestamp returns the timestamp in the database the iterator currently holds.
79
func (iter *Iterator) Timestamp() *Slice {
1✔
80
        var cLen C.size_t
1✔
81
        cTs := C.rocksdb_iter_timestamp(iter.c, &cLen)
1✔
82
        if cTs == nil {
1✔
83
                return nil
×
84
        }
×
85
        return &Slice{data: cTs, size: cLen, freed: true}
1✔
86
}
87

NEW
88
func (iter *Iterator) TimestampSlice() OptimizedSlice {
×
NEW
89
        return newNativeOptimizeSlice(C.rocksdb_iter_timestamp_slice(iter.c))
×
NEW
90
}
×
91

92
// Value returns the value in the database the iterator currently holds.
93
func (iter *Iterator) Value() *Slice {
1✔
94
        var cLen C.size_t
1✔
95
        cVal := C.rocksdb_iter_value(iter.c, &cLen)
1✔
96
        if cVal == nil {
1✔
97
                return nil
×
98
        }
×
99
        return &Slice{data: cVal, size: cLen, freed: true}
1✔
100
}
101

NEW
102
func (iter *Iterator) ValueSlice() OptimizedSlice {
×
NEW
103
        return newNativeOptimizeSlice(C.rocksdb_iter_value_slice(iter.c))
×
NEW
104
}
×
105

106
// Next moves the iterator to the next sequential key in the database.
107
func (iter *Iterator) Next() {
1✔
108
        C.rocksdb_iter_next(iter.c)
1✔
109
}
1✔
110

111
// Prev moves the iterator to the previous sequential key in the database.
112
func (iter *Iterator) Prev() {
1✔
113
        C.rocksdb_iter_prev(iter.c)
1✔
114
}
1✔
115

116
// SeekToFirst moves the iterator to the first key in the database.
117
func (iter *Iterator) SeekToFirst() {
1✔
118
        C.rocksdb_iter_seek_to_first(iter.c)
1✔
119
}
1✔
120

121
// SeekToLast moves the iterator to the last key in the database.
122
func (iter *Iterator) SeekToLast() {
1✔
123
        C.rocksdb_iter_seek_to_last(iter.c)
1✔
124
}
1✔
125

126
// Seek moves the iterator to the position greater than or equal to the key.
127
func (iter *Iterator) Seek(key []byte) {
1✔
128
        cKey := refGoBytes(key)
1✔
129
        C.rocksdb_iter_seek(iter.c, cKey, C.size_t(len(key)))
1✔
130
}
1✔
131

132
// SeekForPrev moves the iterator to the last key that less than or equal
133
// to the target key, in contrast with Seek.
134
func (iter *Iterator) SeekForPrev(key []byte) {
×
135
        cKey := refGoBytes(key)
×
136
        C.rocksdb_iter_seek_for_prev(iter.c, cKey, C.size_t(len(key)))
×
137
}
×
138

139
// Err returns nil if no errors happened during iteration, or the actual
140
// error otherwise.
141
func (iter *Iterator) Err() (err error) {
1✔
142
        var cErr *C.char
1✔
143
        C.rocksdb_iter_get_error(iter.c, &cErr)
1✔
144
        err = fromCError(cErr)
1✔
145
        return err
1✔
146
}
1✔
147

148
// Refresh if supported, the DB state that the iterator reads from is updated to
149
// the latest state. The iterator will be invalidated after the call.
150
// Regardless of whether the iterator was created/refreshed previously
151
// with or without a snapshot, the iterator will be reading the
152
// latest DB state after this call.
153
// Note that you will need to call a Seek*() function to get the iterator
154
// back into a valid state before calling a function that assumes the
155
// state is already valid, like Next().
156
func (iter *Iterator) Refresh() (err error) {
1✔
157
        var cErr *C.char
1✔
158
        C.rocksdb_iter_refresh(iter.c, &cErr)
1✔
159
        err = fromCError(cErr)
1✔
160
        return err
1✔
161
}
1✔
162

163
// Close closes the iterator.
164
func (iter *Iterator) Close() {
1✔
165
        C.rocksdb_iter_destroy(iter.c)
1✔
166
        iter.c = nil
1✔
167
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc