Skip to content

Commit

Permalink
Merge pull request RoaringBitmap#220 from jacksonrnewhouse/add_flush_…
Browse files Browse the repository at this point in the history
…copy_on_write

add flushCopyOnWrite() call so that source bitmaps can be unmapped.
  • Loading branch information
lemire authored Jun 10, 2019
2 parents d7fae94 + 543b871 commit 02d637a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
7 changes: 7 additions & 0 deletions roaring.go
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,13 @@ func (rb *Bitmap) GetCopyOnWrite() (val bool) {
return rb.highlowcontainer.copyOnWrite
}

// clone all containers which have needCopyOnWrite set to true
// This can be used to make sure it is safe to munmap a []byte
// that the roaring array may still have a reference to.
func (rb *Bitmap) CloneCopyOnWriteContainers() {
rb.highlowcontainer.cloneCopyOnWriteContainers()
}

// FlipInt calls Flip after casting the parameters (convenience method)
func FlipInt(bm *Bitmap, rangeStart, rangeEnd int) *Bitmap {
return Flip(bm, uint64(rangeStart), uint64(rangeEnd))
Expand Down
13 changes: 13 additions & 0 deletions roaringarray.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,19 @@ func (ra *roaringArray) clone() *roaringArray {
return &sa
}


// clone all containers which have needCopyOnWrite set to true
// This can be used to make sure it is safe to munmap a []byte
// that the roaring array may still have a reference to.
func (ra *roaringArray) cloneCopyOnWriteContainers() {
for i, needCopyOnWrite := range ra.needCopyOnWrite {
if needCopyOnWrite {
ra.containers[i] = ra.containers[i].clone()
ra.needCopyOnWrite[i] = false
}
}
}

// unused function:
//func (ra *roaringArray) containsKey(x uint16) bool {
// return (ra.binarySearch(0, int64(len(ra.keys)), x) >= 0)
Expand Down
24 changes: 24 additions & 0 deletions roaringcow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package roaring
import (
. "github.com/smartystreets/goconvey/convey"
"github.com/willf/bitset"
"bytes"
"log"
"math/rand"
"strconv"
Expand Down Expand Up @@ -1886,3 +1887,26 @@ func TestFlipVerySmallCOW(t *testing.T) {
So(rbcard, ShouldEqual, 9)
})
}

func TestCloneCOWContainers(t *testing.T) {
Convey("test CloneCopyOnWriteContainers", t, func() {
rb := NewBitmap()
rb.AddRange(0,3000)
buf := &bytes.Buffer{}
rb.WriteTo(buf)

newRb1 := NewBitmap()
newRb1.FromBuffer(buf.Bytes())
newRb1.CloneCopyOnWriteContainers()

rb2 := NewBitmap()
rb2.AddRange(3000,6000)
buf.Reset()
rb2.WriteTo(buf)

So(newRb1.ToArray(), ShouldResemble, rb.ToArray())

})


}
7 changes: 5 additions & 2 deletions serialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -945,8 +945,11 @@ func TestBitmapFromBufferCOW(t *testing.T) {
newRb2 := NewBitmap()
newRb2.FromBuffer(buf2.Bytes())
rbor1 := Or(newRb1, newRb2)
rbor2 := rbor1.Clone()
rbor3 := Or(newRb1.Clone(), newRb2.Clone())
rbor2 := rbor1
rbor3 := Or(newRb1, newRb2)
rbor1.CloneCopyOnWriteContainers()
rbor2.CloneCopyOnWriteContainers()
rbor3.CloneCopyOnWriteContainers()
buf1.Reset()
buf2.Reset()
rbbogus.WriteTo(buf1)
Expand Down

0 comments on commit 02d637a

Please sign in to comment.