From 71621e60baab8ab5fe4033535d33df76c955b65e Mon Sep 17 00:00:00 2001 From: Richard Artoul Date: Fri, 30 Nov 2018 19:23:59 -0500 Subject: [PATCH] Refactor roaring repair operations --- roaring/containers.go | 6 ++---- roaring/roaring.go | 16 ++++++++++++++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/roaring/containers.go b/roaring/containers.go index 0e8fdf988..b9928823a 100644 --- a/roaring/containers.go +++ b/roaring/containers.go @@ -156,11 +156,9 @@ func (sc *sliceContainers) Iterator(key uint64) (citer ContainerIterator, found return &sliceIterator{e: sc, i: i}, found } -func (sc *sliceContainers) repairBitmaps() { +func (sc *sliceContainers) Repair() { for _, c := range sc.containers { - if c.isBitmap() { - c.bitmapRepair() - } + c.Repair() } } diff --git a/roaring/roaring.go b/roaring/roaring.go index 2d88f2868..a1a535ed3 100644 --- a/roaring/roaring.go +++ b/roaring/roaring.go @@ -98,8 +98,12 @@ type Containers interface { Count() uint64 - //Reset will clear the containers collection to allow for recycling during snapshot + // Reset will clear the containers collection to allow for recycling during snapshot Reset() + + // Repair will repair the cardinality of any containers whose cardinality were corrupted + // due to optimized operations. + Repair() } type ContainerIterator interface { @@ -687,7 +691,7 @@ func (b *Bitmap) unionIntoTarget(target *Bitmap, others ...*Bitmap) { // n (container cardinality) to fall out of sync, and then at the very end we perform // a "Repair" to recalculate all the container values. That way we never popcount() // an entire bitmap container more than once per bulk union operation. - target.Containers.(*sliceContainers).repairBitmaps() + target.Containers.Repair() } // Difference returns the difference of b and other. @@ -2091,6 +2095,14 @@ func (c *Container) check() error { return a } +// Repair repairs the cardinality of c if it has been corrupted by +// optimized operations. +func (c *Container) Repair() { + if c.isBitmap() { + c.bitmapRepair() + } +} + func (c *Container) bitmapRepair() { n := int32(0) // Manually unroll loop to make it a little faster.