Skip to content

Commit 17972e0

Browse files
committed
update comments
1 parent 326772e commit 17972e0

File tree

7 files changed

+92
-1
lines changed

7 files changed

+92
-1
lines changed

storage/operation/badgerimpl/iterator.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ func newBadgerIterator(db *badger.DB, startPrefix, endPrefix []byte, ops storage
3434
}
3535
}
3636

37+
// First seeks to the smallest key greater than or equal to the given key.
3738
func (i *badgerIterator) First() {
3839
i.iter.Seek(i.lowerBound)
3940
}
4041

42+
// Valid returns whether the iterator is positioned at a valid key-value pair.
4143
func (i *badgerIterator) Valid() bool {
4244
// if it's beyond the upper bound, it's invalid
4345
if !i.iter.Valid() {
@@ -49,16 +51,20 @@ func (i *badgerIterator) Valid() bool {
4951
return valid
5052
}
5153

54+
// Next advances the iterator to the next key-value pair.
5255
func (i *badgerIterator) Next() {
5356
i.iter.Next()
5457
}
5558

59+
// IterItem returns the current key-value pair, or nil if done.
5660
func (i *badgerIterator) IterItem() storage.IterItem {
5761
return i.iter.Item()
5862
}
5963

6064
var _ storage.IterItem = (*badger.Item)(nil)
6165

66+
// Close closes the iterator. Iterator must be closed, otherwise it causes memory leak.
67+
// No errors expected during normal operation
6268
func (i *badgerIterator) Close() error {
6369
i.iter.Close()
6470
return nil

storage/operation/badgerimpl/reader.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ var _ io.Closer = (*noopCloser)(nil)
2020

2121
func (noopCloser) Close() error { return nil }
2222

23+
// Get gets the value for the given key. It returns ErrNotFound if the DB
24+
// does not contain the key.
25+
// other errors are exceptions
26+
//
27+
// The caller should not modify the contents of the returned slice, but it is
28+
// safe to modify the contents of the argument after Get returns. The
29+
// returned slice will remain valid until the returned Closer is closed. On
30+
// success, the caller MUST call closer.Close() or a memory leak will occur.
2331
func (b dbReader) Get(key []byte) ([]byte, io.Closer, error) {
2432
tx := b.db.NewTransaction(false)
2533
defer tx.Discard()
@@ -40,6 +48,11 @@ func (b dbReader) Get(key []byte) ([]byte, io.Closer, error) {
4048
return value, noopCloser{}, nil
4149
}
4250

51+
// NewIter returns a new Iterator for the given key prefix range [startPrefix, endPrefix], both inclusive.
52+
// Specifically, all keys that meet ANY of the following conditions are included in the iteration:
53+
// - have a prefix equal to startPrefix OR
54+
// - have a prefix equal to the endPrefix OR
55+
// - have a prefix that is lexicographically between startPrefix and endPrefix
4356
func (b dbReader) NewIter(startPrefix, endPrefix []byte, ops storage.IteratorOption) (storage.Iterator, error) {
4457
return newBadgerIterator(b.db, startPrefix, endPrefix, ops), nil
4558
}

storage/operation/badgerimpl/writer.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,36 @@ type ReaderBatchWriter struct {
1919

2020
var _ storage.ReaderBatchWriter = (*ReaderBatchWriter)(nil)
2121

22+
// GlobalReader returns a database-backed reader which reads the latest committed global database state ("read-committed isolation").
23+
// This reader will not read writes written to ReaderBatchWriter.Writer until the write batch is committed.
24+
// This reader may observe different values for the same key on subsequent reads.
2225
func (b *ReaderBatchWriter) GlobalReader() storage.Reader {
2326
return b.globalReader
2427
}
2528

29+
// Writer returns a writer associated with a batch of writes. The batch is pending until it is committed.
30+
// When we `Write` into the batch, that write operation is added to the pending batch, but not committed.
31+
// The commit operation is atomic w.r.t. the batch; either all writes are applied to the database, or no writes are.
32+
// Note:
33+
// - The writer cannot be used concurrently for writing.
2634
func (b *ReaderBatchWriter) Writer() storage.Writer {
2735
return b
2836
}
2937

38+
// BadgerWriteBatch returns the badger write batch
3039
func (b *ReaderBatchWriter) BadgerWriteBatch() *badger.WriteBatch {
3140
return b.batch
3241
}
3342

43+
// AddCallback adds a callback to execute after the batch has been flush
44+
// regardless the batch update is succeeded or failed.
45+
// The error parameter is the error returned by the batch update.
3446
func (b *ReaderBatchWriter) AddCallback(callback func(error)) {
3547
b.callbacks.AddCallback(callback)
3648
}
3749

50+
// Commit flushes the batch to the database.
51+
// No errors expected during normal operation
3852
func (b *ReaderBatchWriter) Commit() error {
3953
err := b.batch.Flush()
4054

@@ -69,14 +83,27 @@ func NewReaderBatchWriter(db *badger.DB) *ReaderBatchWriter {
6983

7084
var _ storage.Writer = (*ReaderBatchWriter)(nil)
7185

86+
// Set sets the value for the given key. It overwrites any previous value
87+
// for that key; a DB is not a multi-map.
88+
//
89+
// It is safe to modify the contents of the arguments after Set returns.
90+
// No errors expected during normal operation
7291
func (b *ReaderBatchWriter) Set(key, value []byte) error {
7392
return b.batch.Set(key, value)
7493
}
7594

95+
// Delete deletes the value for the given key. Deletes are blind all will
96+
// succeed even if the given key does not exist.
97+
//
98+
// It is safe to modify the contents of the arguments after Delete returns.
99+
// No errors expected during normal operation
76100
func (b *ReaderBatchWriter) Delete(key []byte) error {
77101
return b.batch.Delete(key)
78102
}
79103

104+
// DeleteByRange removes all keys with a prefix that falls within the
105+
// range [start, end], both inclusive.
106+
// No errors expected during normal operation
80107
func (b *ReaderBatchWriter) DeleteByRange(globalReader storage.Reader, startPrefix, endPrefix []byte) error {
81108
err := operation.IterateKeysInPrefixRange(startPrefix, endPrefix, func(key []byte) error {
82109
err := b.batch.Delete(key)

storage/operation/pebbleimpl/iterator.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,22 @@ func newPebbleIterator(reader pebble.Reader, startPrefix, endPrefix []byte, ops
3232
}, nil
3333
}
3434

35+
// First seeks to the smallest key greater than or equal to the given key.
3536
func (i *pebbleIterator) First() {
3637
i.iter.First()
3738
}
3839

40+
// Valid returns whether the iterator is positioned at a valid key-value pair.
3941
func (i *pebbleIterator) Valid() bool {
4042
return i.iter.Valid()
4143
}
4244

45+
// Next advances the iterator to the next key-value pair.
4346
func (i *pebbleIterator) Next() {
4447
i.iter.Next()
4548
}
4649

50+
// IterItem returns the current key-value pair, or nil if done.
4751
func (i *pebbleIterator) IterItem() storage.IterItem {
4852
return pebbleIterItem{iter: i.iter}
4953
}
@@ -67,6 +71,8 @@ func (i pebbleIterItem) Value(fn func([]byte) error) error {
6771
return fn(val)
6872
}
6973

74+
// Close closes the iterator. Iterator must be closed, otherwise it causes memory leak.
75+
// No errors expected during normal operation
7076
func (i *pebbleIterator) Close() error {
7177
return i.iter.Close()
7278
}

storage/operation/pebbleimpl/reader.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ var _ io.Closer = (*noopCloser)(nil)
2222

2323
func (noopCloser) Close() error { return nil }
2424

25+
// Get gets the value for the given key. It returns ErrNotFound if the DB
26+
// does not contain the key.
27+
// other errors are exceptions
28+
//
29+
// The caller should not modify the contents of the returned slice, but it is
30+
// safe to modify the contents of the argument after Get returns. The
31+
// returned slice will remain valid until the returned Closer is closed. On
32+
// success, the caller MUST call closer.Close() or a memory leak will occur.
2533
func (b dbReader) Get(key []byte) ([]byte, io.Closer, error) {
2634
value, closer, err := b.db.Get(key)
2735

@@ -37,6 +45,11 @@ func (b dbReader) Get(key []byte) ([]byte, io.Closer, error) {
3745
return value, closer, nil
3846
}
3947

48+
// NewIter returns a new Iterator for the given key prefix range [startPrefix, endPrefix], both inclusive.
49+
// Specifically, all keys that meet ANY of the following conditions are included in the iteration:
50+
// - have a prefix equal to startPrefix OR
51+
// - have a prefix equal to the endPrefix OR
52+
// - have a prefix that is lexicographically between startPrefix and endPrefix
4053
func (b dbReader) NewIter(startPrefix, endPrefix []byte, ops storage.IteratorOption) (storage.Iterator, error) {
4154
return newPebbleIterator(b.db, startPrefix, endPrefix, ops)
4255
}

storage/operation/pebbleimpl/writer.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,18 @@ type ReaderBatchWriter struct {
1616

1717
var _ storage.ReaderBatchWriter = (*ReaderBatchWriter)(nil)
1818

19+
// GlobalReader returns a database-backed reader which reads the latest committed global database state ("read-committed isolation").
20+
// This reader will not read writes written to ReaderBatchWriter.Writer until the write batch is committed.
21+
// This reader may observe different values for the same key on subsequent reads.
1922
func (b *ReaderBatchWriter) GlobalReader() storage.Reader {
2023
return b.globalReader
2124
}
2225

26+
// Writer returns a writer associated with a batch of writes. The batch is pending until it is committed.
27+
// When we `Write` into the batch, that write operation is added to the pending batch, but not committed.
28+
// The commit operation is atomic w.r.t. the batch; either all writes are applied to the database, or no writes are.
29+
// Note:
30+
// - The writer cannot be used concurrently for writing.
2331
func (b *ReaderBatchWriter) Writer() storage.Writer {
2432
return b
2533
}
@@ -28,10 +36,15 @@ func (b *ReaderBatchWriter) PebbleWriterBatch() *pebble.Batch {
2836
return b.batch
2937
}
3038

39+
// AddCallback adds a callback to execute after the batch has been flush
40+
// regardless the batch update is succeeded or failed.
41+
// The error parameter is the error returned by the batch update.
3142
func (b *ReaderBatchWriter) AddCallback(callback func(error)) {
3243
b.callbacks.AddCallback(callback)
3344
}
3445

46+
// Commit flushes the batch to the database.
47+
// No errors expected during normal operation
3548
func (b *ReaderBatchWriter) Commit() error {
3649
err := b.batch.Commit(pebble.Sync)
3750

@@ -66,15 +79,27 @@ func NewReaderBatchWriter(db *pebble.DB) *ReaderBatchWriter {
6679

6780
var _ storage.Writer = (*ReaderBatchWriter)(nil)
6881

82+
// Set sets the value for the given key. It overwrites any previous value
83+
// for that key; a DB is not a multi-map.
84+
//
85+
// It is safe to modify the contents of the arguments after Set returns.
86+
// No errors expected during normal operation
6987
func (b *ReaderBatchWriter) Set(key, value []byte) error {
7088
return b.batch.Set(key, value, pebble.Sync)
7189
}
7290

91+
// Delete deletes the value for the given key. Deletes are blind all will
92+
// succeed even if the given key does not exist.
93+
//
94+
// It is safe to modify the contents of the arguments after Delete returns.
95+
// No errors expected during normal operation
7396
func (b *ReaderBatchWriter) Delete(key []byte) error {
7497
return b.batch.Delete(key, pebble.Sync)
7598
}
7699

77-
// DeleteByRange deletes all keys with a prefix in the range [startPrefix, endPrefix] (both inclusive).
100+
// DeleteByRange removes all keys with a prefix that falls within the
101+
// range [start, end], both inclusive.
102+
// No errors expected during normal operation
78103
func (b *ReaderBatchWriter) DeleteByRange(_ storage.Reader, startPrefix, endPrefix []byte) error {
79104
// DeleteRange takes the prefix range with start (inclusive) and end (exclusive, note: not inclusive).
80105
// therefore, we need to increment the endPrefix to make it inclusive.

storage/operations.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ type Reader interface {
6363
}
6464

6565
// Writer is an interface for batch writing to a storage backend.
66+
// It cannot be used concurrently for writing.
6667
type Writer interface {
6768
// Set sets the value for the given key. It overwrites any previous value
6869
// for that key; a DB is not a multi-map.

0 commit comments

Comments
 (0)