Skip to content

Commit 04c4788

Browse files
committed
Add Size method
1 parent d8485ea commit 04c4788

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

cdb.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ type Reader interface {
4444
Iterator() (Iterator, error)
4545
// IteratorAt returns new Iterator object that points on first record associated with given key.
4646
IteratorAt(key []byte) (Iterator, error)
47+
// Size returns the size of the dataset
48+
Size() int
4749
}
4850

4951
// Iterator provides API for walking through database's records. Do not share object between multiple goroutines.

cdb_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ func TestShouldReturnAllValues(t *testing.T) {
5050
t.Errorf("Expected value %s, got %s", c.value, value)
5151
}
5252
}
53+
54+
if reader.Size() != 7 {
55+
t.Errorf("Expected size %d, got %d", 7, reader.Size())
56+
}
5357
}
5458

5559
func TestShouldReturnNilOnNonExistingKeys(t *testing.T) {

reader.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type readerImpl struct {
2020
reader io.ReaderAt
2121
hasher Hasher
2222
endPos uint32
23+
size int
2324
}
2425

2526
// newReader return new readerImpl object on success, otherwise return nil and error
@@ -48,6 +49,7 @@ func (r *readerImpl) initialize() error {
4849
for i := range &r.refs {
4950
j := i * 8
5051
r.refs[i].position, r.refs[i].length = binary.LittleEndian.Uint32(buf[j:j+4]), binary.LittleEndian.Uint32(buf[j+4:j+8])
52+
r.size += int(r.refs[i].length >> 1)
5153
}
5254

5355
for _, ref := range &r.refs {
@@ -165,6 +167,11 @@ func (r *readerImpl) IteratorAt(key []byte) (Iterator, error) {
165167
), nil
166168
}
167169

170+
// Size returns the size of the dataset
171+
func (r *readerImpl) Size() int {
172+
return r.size
173+
}
174+
168175
// calcHash returns hash value of given key
169176
func (r *readerImpl) calcHash(key []byte) uint32 {
170177
hashFunc := r.hasher()
@@ -176,7 +183,7 @@ func (r *readerImpl) calcHash(key []byte) uint32 {
176183
func (r *readerImpl) checkEntry(entry slot, key []byte) (*sectionReaderFactory, uint32, error) {
177184
var (
178185
keySize, valSize uint32
179-
givenKeySize uint32 = uint32(len(key))
186+
givenKeySize = uint32(len(key))
180187
)
181188

182189
err := r.readPair(entry.position, &keySize, &valSize)

writer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func (w *writerImpl) Close() error {
109109
for _, slot := range table {
110110
k := (slot.hash >> 8) % n
111111

112-
// linear ...
112+
// Linear probing
113113
for slots[k].position != 0 {
114114
k = (k + 1) % n
115115
}

0 commit comments

Comments
 (0)