From 0757a84f69945f177273af184a7ff5a9ec31cfaf Mon Sep 17 00:00:00 2001 From: Travis Turner Date: Wed, 17 Jan 2018 17:05:13 -0600 Subject: [PATCH] add PutContainerValues to Containers interface to prevent re-allocation of containers --- roaring/containers_btree.go | 19 +++++++++++++++++++ roaring/containers_skiplist.go | 3 +++ roaring/roaring.go | 14 +++++++++----- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/roaring/containers_btree.go b/roaring/containers_btree.go index f2878fdf9..39e479513 100644 --- a/roaring/containers_btree.go +++ b/roaring/containers_btree.go @@ -47,6 +47,25 @@ func (btc *BTreeContainers) Put(key uint64, c *container) { btc.tree.Set(key, c) } +func (btc *BTreeContainers) PutContainerValues(key uint64, containerType byte, n int, mapped bool) { + f := func(oldV *container, exists bool) (*container, bool) { + // update the existing container + if exists { + oldV.containerType = containerType + oldV.n = n + oldV.mapped = mapped + return oldV, true + } + return &container{ + containerType: containerType, + n: n, + mapped: mapped, + }, true + } + + btc.tree.Put(key, f) +} + func (btc *BTreeContainers) Remove(key uint64) { btc.tree.Delete(key) } diff --git a/roaring/containers_skiplist.go b/roaring/containers_skiplist.go index c80dbeb06..07d6dc870 100644 --- a/roaring/containers_skiplist.go +++ b/roaring/containers_skiplist.go @@ -25,6 +25,9 @@ func (slc *SkipListContainers) Put(key uint64, c *container) { slc.list.Set(key, c) } +func (slc *SkipListContainers) PutContainerValues(key uint64, containerType byte, n int, mapped bool) { +} + func (slc *SkipListContainers) Remove(key uint64) { slc.list.Remove(key) } diff --git a/roaring/roaring.go b/roaring/roaring.go index fb1994bfd..51f1efe9a 100644 --- a/roaring/roaring.go +++ b/roaring/roaring.go @@ -70,6 +70,10 @@ type Containers interface { // Put adds the container at key. Put(key uint64, c *container) + // PutContainerValues updates an existing container at key. + // If a container does not exist for key, a new one is allocated. + PutContainerValues(key uint64, containerType byte, n int, mapped bool) + // Remove takes the container at key out. Remove(key uint64) @@ -631,11 +635,11 @@ func (b *Bitmap) UnmarshalBinary(data []byte) error { // Descriptive header section: Read container keys and cardinalities. for i, buf := 0, data[headerSize:]; i < int(keyN); i, buf = i+1, buf[12:] { - b.conts.Put(binary.LittleEndian.Uint64(buf[0:8]), &container{ - containerType: byte(binary.LittleEndian.Uint16(buf[8:10])), - n: int(binary.LittleEndian.Uint16(buf[10:12])) + 1, - mapped: true, - }) + b.conts.PutContainerValues( + binary.LittleEndian.Uint64(buf[0:8]), + byte(binary.LittleEndian.Uint16(buf[8:10])), + int(binary.LittleEndian.Uint16(buf[10:12]))+1, + true) } opsOffset := headerSize + int(keyN)*12