Skip to content

Commit a3dd01a

Browse files
committed
doc: minor documentation update
1 parent de7eded commit a3dd01a

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

roaring.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,26 +277,34 @@ func (rb *Bitmap) Checksum() uint64 {
277277
return hash
278278
}
279279

280-
// FromUnsafeBytes reads a serialized version of this bitmap from the byte buffer without copy.
280+
// FromUnsafeBytes reads a serialized version of this bitmap from the byte buffer without copy
281+
// (for advanced users only, you must be an expert Go programmer!).
282+
// E.g., you can use this method to read a serialized bitmap from a memory-mapped file written out
283+
// with the WriteTo method.
284+
// The format specification is
285+
// https://github.com/RoaringBitmap/RoaringFormatSpec
281286
// It is the caller's responsibility to ensure that the input data is not modified and remains valid for the entire lifetime of this bitmap.
282287
// This method avoids small allocations but holds references to the input data buffer. It is GC-friendly, but it may consume more memory eventually.
283288
// The containers in the resulting bitmap are immutable containers tied to the provided byte array and they rely on
284289
// copy-on-write which means that modifying them creates copies. Thus FromUnsafeBytes is more likely to be appropriate for read-only use cases,
285290
// when the resulting bitmap can be considered immutable.
286291
//
287-
// See also the FromBuffer function.
292+
// See also the FromBuffer function: most users should prefer FromBuffer unless the need the performance,
293+
// and are aware of the implications of holding references to the input data buffer.
288294
// See https://github.com/RoaringBitmap/roaring/pull/395 for more details.
289295
func (rb *Bitmap) FromUnsafeBytes(data []byte, cookieHeader ...byte) (p int64, err error) {
290296
stream := internal.NewByteBuffer(data)
291297
return rb.ReadFrom(stream)
292298
}
293299

294300
// ReadFrom reads a serialized version of this bitmap from stream.
301+
// E.g., you can use this method to read a serialized bitmap from a file written
302+
// with the WriteTo method.
295303
// The format is compatible with other RoaringBitmap
296304
// implementations (Java, C) and is documented here:
297305
// https://github.com/RoaringBitmap/RoaringFormatSpec
298-
// Since io.Reader is regarded as a stream and cannot be read twice.
299-
// So add cookieHeader to accept the 4-byte data that has been read in roaring64.ReadFrom.
306+
// Since io.Reader is regarded as a stream and cannot be read twice,
307+
// we add cookieHeader to accept the 4-byte data that has been read in roaring64.ReadFrom.
300308
// It is not necessary to pass cookieHeader when call roaring.ReadFrom to read the roaring32 data directly.
301309
func (rb *Bitmap) ReadFrom(reader io.Reader, cookieHeader ...byte) (p int64, err error) {
302310
stream, ok := reader.(internal.ByteInput)
@@ -325,7 +333,7 @@ func (rb *Bitmap) MustReadFrom(reader io.Reader, cookieHeader ...byte) (p int64,
325333
return
326334
}
327335

328-
// FromBuffer creates a bitmap from its serialized version stored in buffer
336+
// FromBuffer creates a bitmap from its serialized version stored in buffer (E.g., as written by WriteTo).
329337
//
330338
// The format specification is available here:
331339
// https://github.com/RoaringBitmap/RoaringFormatSpec

serialization_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,19 @@ func TestSerializationBasic2_041(t *testing.T) {
240240
assert.True(t, rb.Equals(newrb))
241241
}
242242

243+
func TestFromUnsafeBytes(t *testing.T) {
244+
rb := BitmapOf(1, 2, 3, 4, 5, 100, 1000, 10000, 100000, 1000000)
245+
buf := &bytes.Buffer{}
246+
_, err := rb.WriteTo(buf)
247+
require.NoError(t, err)
248+
b := buf.Bytes()
249+
rb2 := NewBitmap()
250+
_, err2 := rb2.FromUnsafeBytes(b)
251+
require.NoError(t, err2)
252+
assert.True(t, rb.Equals(rb2))
253+
runtime.KeepAlive(&b)
254+
}
255+
243256
// roaringarray.writeTo and .readFrom should serialize and unserialize when containing all 3 container types
244257
func TestSerializationBasic3_042(t *testing.T) {
245258
rb := BitmapOf(1, 2, 3, 4, 5, 100, 1000, 10000, 100000, 1000000)

0 commit comments

Comments
 (0)