Documentation ¶
Overview ¶
Copyright 2017 Pilosa Corp.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Copyright 2017 Pilosa Corp.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Copyright 2017 Pilosa Corp.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
start with https://github.com/benbjohnson/immutable and specialize Map<uint32,int64>
Copyright 2019 Ben Johnson ¶
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Package rbf implements the roaring b-tree file format.
Copyright 2017 Pilosa Corp.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Copyright 2017 Pilosa Corp.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Index ¶
- Constants
- Variables
- func AlwaysPrintf(format string, a ...interface{})
- func Caller(upStack int) string
- func ConvertToLeafArgs(key uint64, c *roaring.Container) (result leafCell)
- func CreateDirIfNotExist(path string)
- func DirExists(name string) bool
- func EnableRowCache() bool
- func FileExists(name string) bool
- func FileLine(depth int) string
- func FileSize(name string) (int64, error)
- func IsBitmapHeader(page []byte) bool
- func IsMetaPage(page []byte) bool
- func Magic32() uint32
- func PP(format string, a ...interface{})
- func Pagedump(b []byte, indent string, writer io.Writer)
- func PanicOn(err error)
- func Printf(format string, a ...interface{}) (n int, err error)
- func RowValues(b []uint64) []uint64
- func SetRowcacheOn(on bool)
- func TSPrintf(format string, a ...interface{})
- func VV(format string, a ...interface{})
- func Walk(tx *Tx, pgno uint32, v func(uint32, []*RootRecord))
- func WalkPage(tx *Tx, pgno uint32, walker Walker)
- func WalkRootRecordPages(page []byte) uint32
- func WriteRootRecord(data []byte, rec *RootRecord) (remaining []byte, err error)
- type BitmapPage
- type BitmapPageInfo
- type BranchCell
- type BranchPage
- type BranchPageInfo
- type ContainerType
- type Cursor
- func (c *Cursor) Add(v uint64) (changed bool, err error)
- func (c *Cursor) AddRoaring(bm *roaring.Bitmap) (changed bool, err error)
- func (c *Cursor) Close()
- func (c *Cursor) Contains(v uint64) (exists bool, err error)
- func (c *Cursor) CurrentPageType() ContainerType
- func (c *Cursor) Dump(name string)
- func (c *Cursor) DumpKeys()
- func (c *Cursor) DumpStack()
- func (c *Cursor) First() error
- func (c *Cursor) Key() uint64
- func (c *Cursor) Last() error
- func (c *Cursor) Next() error
- func (c *Cursor) Prev() error
- func (c *Cursor) Remove(v uint64) (changed bool, err error)
- func (c *Cursor) RemoveRoaring(bm *roaring.Bitmap) (changed bool, err error)
- func (c *Cursor) Row(shard, rowID uint64) (*roaring.Bitmap, error)
- func (c *Cursor) Rows() ([]uint64, error)
- func (c *Cursor) Seek(key uint64) (exact bool, err error)
- func (c *Cursor) Values() []uint16
- type DB
- func (db *DB) Begin(writable bool) (_ *Tx, err error)
- func (db *DB) Check() error
- func (db *DB) Close() (err error)
- func (db *DB) DataPath() string
- func (db *DB) HasData(requireOneHotBit bool) (hasAnyRecords bool, err error)
- func (db *DB) Open() (err error)
- func (db *DB) Size() (int64, error)
- func (db *DB) TxN() int
- func (db *DB) WALPath() string
- func (db *DB) WALSize() int64
- type FreePage
- type FreePageInfo
- type LeafCell
- type LeafPage
- type LeafPageInfo
- type MetaPage
- type MetaPageInfo
- type Metric
- type Nodetype
- type Page
- type PageInfo
- type PageMap
- type PageMapBuilder
- type PageMapIterator
- type RootRecord
- type RootRecordPage
- type RootRecordPageInfo
- type Tx
- func (tx *Tx) Add(name string, a ...uint64) (changeCount int, err error)
- func (tx *Tx) AddRoaring(name string, bm *roaring.Bitmap) (changed bool, err error)
- func (tx *Tx) ApplyFilter(name string, key uint64, filter roaring.BitmapFilter) (err error)
- func (tx *Tx) BitmapNames() ([]string, error)
- func (tx *Tx) Check() error
- func (tx *Tx) Commit() error
- func (tx *Tx) Container(name string, key uint64) (*roaring.Container, error)
- func (tx *Tx) ContainerIterator(name string, key uint64) (citer roaring.ContainerIterator, found bool, err error)
- func (tx *Tx) Contains(name string, v uint64) (bool, error)
- func (tx *Tx) Count(name string) (uint64, error)
- func (tx *Tx) CountRange(name string, start, end uint64) (uint64, error)
- func (tx *Tx) CreateBitmap(name string) error
- func (tx *Tx) CreateBitmapIfNotExists(name string) error
- func (tx *Tx) Cursor(name string) (*Cursor, error)
- func (tx *Tx) DBPath() string
- func (tx *Tx) DeleteBitmap(name string) error
- func (tx *Tx) DeleteBitmapsWithPrefix(prefix string) error
- func (tx *Tx) Dump(short bool, shard uint64)
- func (tx *Tx) DumpString(short bool, shard uint64) (r string)
- func (tx *Tx) FieldViews() []string
- func (tx *Tx) ForEach(name string, fn func(i uint64) error) error
- func (tx *Tx) ForEachRange(name string, start, end uint64, fn func(uint64) error) error
- func (tx *Tx) GetSizeBytesWithPrefix(prefix string) (n uint64, err error)
- func (tx *Tx) GetSortedFieldViewList() (fvs []txkey.FieldView, _ error)
- func (tx *Tx) ImportRoaringBits(name string, itr roaring.RoaringIterator, clear bool, log bool, rowSize uint64, ...) (changed int, rowSet map[uint64]int, err error)
- func (tx *Tx) Max(name string) (uint64, error)
- func (tx *Tx) Min(name string) (uint64, bool, error)
- func (tx *Tx) OffsetRange(name string, offset, start, endx uint64) (*roaring.Bitmap, error)
- func (tx *Tx) PageData(pgno uint32) ([]byte, error)
- func (tx *Tx) PageInfos() ([]PageInfo, error)
- func (tx *Tx) PageN() int
- func (tx *Tx) Pages(pgnos []uint32) ([]Page, error)
- func (tx *Tx) PutContainer(name string, key uint64, ct *roaring.Container) error
- func (tx *Tx) Remove(name string, a ...uint64) (changeCount int, err error)
- func (tx *Tx) RemoveContainer(name string, key uint64) error
- func (tx *Tx) RenameBitmap(oldname, newname string) error
- func (tx *Tx) RoaringBitmap(name string) (*roaring.Bitmap, error)
- func (tx *Tx) Rollback()
- func (tx *Tx) Root(name string) (uint32, error)
- func (tx *Tx) RootRecords() (records *immutable.SortedMap, err error)
- func (tx *Tx) UnionInPlace(name string, others ...*roaring.Bitmap) error
- func (tx *Tx) Writable() bool
- type Walker
Constants ¶
const ( // Magic is the first 4 bytes of the RBF file. Magic = "\xFFRBF" // PageSize is the fixed size for every database page. PageSize = 8192 // ShardWidth represents the number of bits per shard. ShardWidth = 1 << shardwidth.Exponent // RowValueMask masks the low bits for a row. RowValueMask = ShardWidth - 1 // ArrayMaxSize represents the maximum size of array containers. // This is sligtly less than roaring to accommodate the page header. ArrayMaxSize = 4079 // RLEMaxSize represents the maximum size of run length encoded containers. RLEMaxSize = 2039 )
const ( PageTypeRootRecord = 1 PageTypeLeaf = 2 PageTypeBranch = 4 PageTypeBitmapHeader = 8 // Only used by the WAL for marking next page PageTypeBitmap = 16 // Only used internally when walking the b-tree )
Page types.
const ( MetaPageFlagCommit = 1 MetaPageFlagRollback = 2 )
Meta commit/rollback flags.
const (
BitmapN = (1 << 16) / 64
)
const RFC3339MsecTz0 = "2006-01-02T15:04:05.000Z07:00"
const RFC3339UsecTz0 = "2006-01-02T15:04:05.000000Z07:00"
Variables ¶
var ( ErrTxClosed = errors.New("transaction closed") ErrTxNotWritable = errors.New("transaction not writable") ErrBitmapNameRequired = errors.New("bitmap name required") ErrBitmapNotFound = errors.New("bitmap not found") ErrBitmapExists = errors.New("bitmap already exists") ErrTxTooLarge = errors.New("rbf tx too large") )
var Debug bool
Debug is just a temporary flag used for debugging.
var (
ErrClosed = errors.New("rbf: database closed")
)
var OurStdout io.Writer = os.Stdout
so we can multi write easily, use our own printf
var VerboseVerbose bool = false
for tons of debug output
Functions ¶
func AlwaysPrintf ¶
func AlwaysPrintf(format string, a ...interface{})
func ConvertToLeafArgs ¶
func CreateDirIfNotExist ¶
func CreateDirIfNotExist(path string)
func EnableRowCache ¶
func EnableRowCache() bool
func FileExists ¶
func IsBitmapHeader ¶
func Magic32 ¶
func Magic32() uint32
Magic32 returns the magic bytes as a big endian encoded uint32.
func Printf ¶
Printf formats according to a format specifier and writes to standard output. It returns the number of bytes written and any write error encountered.
func SetRowcacheOn ¶ added in v2.3.0
func SetRowcacheOn(on bool)
SetEnableRowCache should only be called in NewHolder before all other reads.
func WalkRootRecordPages ¶
func WriteRootRecord ¶
func WriteRootRecord(data []byte, rec *RootRecord) (remaining []byte, err error)
WriteRootRecord writes a root record with the pgno & name. Returns io.ErrShortBuffer if there is not enough space.
Types ¶
type BitmapPage ¶ added in v2.3.0
type BitmapPage struct { *BitmapPageInfo Values []uint16 }
type BitmapPageInfo ¶ added in v2.3.0
type BranchCell ¶ added in v2.3.0
BranchCell represents a branch cell in the public API.
type BranchPage ¶ added in v2.3.0
type BranchPage struct { *BranchPageInfo Cells []*BranchCell }
type BranchPageInfo ¶ added in v2.3.0
type ContainerType ¶ added in v2.4.0
type ContainerType int
const ( ContainerTypeNone ContainerType = iota ContainerTypeArray ContainerTypeRLE ContainerTypeBitmap ContainerTypeBitmapPtr )
Container types.
func (ContainerType) String ¶ added in v2.4.0
func (typ ContainerType) String() string
ContainerTypeString returns a string representation of the container type.
type Cursor ¶
type Cursor struct {
// contains filtered or unexported fields
}
func (*Cursor) AddRoaring ¶
func (*Cursor) CurrentPageType ¶
func (c *Cursor) CurrentPageType() ContainerType
CurrentPageType returns the type of the container currently pointed to by cursor used in testing sometimes the cursor needs to be positions prior to this call with First/Last etc.
func (*Cursor) Next ¶
Next moves to the next element of the btree. Returns EOF if no more elements exist.
func (*Cursor) RemoveRoaring ¶
type DB ¶
type DB struct { // Path represents the path to the database file. Path string // contains filtered or unexported fields }
DB options like MaxSize, FsyncEnabled, DoAllocZero can be set before calling DB.Open().
func NewDB ¶
NewDB returns a new instance of DB. If cfg is nil we will use the rbfcfg.DefaultConfig().
func (*DB) HasData ¶
HasData with requireOneHotBit=false returns hasAnyRecords true if any record has been stored, even if the value for that bitmap record turned out to have no bits hot (be all zeroes).
In this case, we are taking the attempted storage of any named bitmap into the database as evidence that the db is in use, and we return hasAnyRecords true.
Conversely, if requireOneHotBit is true, then a database consisting of only a named bitmap with an all zeroes (no bits hot) will return hasAnyRecords false. We must find at least a single hot bit inside the db in order to return hasAnyRecords true.
HasData is used by backend migration and blue/green checks.
If there is a disk error we return (false, error), so always check the error before deciding if hasAnyRecords is valid.
We will internally create and rollback a read-only transaction to answer this query.
func (*DB) Open ¶
Open opens a database with the file specified in Path. Creates a new file if one does not already exist.
type FreePage ¶ added in v2.3.0
type FreePage struct {
*FreePageInfo
}
type FreePageInfo ¶ added in v2.3.0
type FreePageInfo struct {
Pgno uint32
}
type LeafCell ¶ added in v2.3.0
type LeafCell struct { Key uint64 Type ContainerType Pgno uint32 // bitmap pointer only Values []uint16 // array & rle containers only }
LeafCell represents a leaf cell in the public API.
type LeafPage ¶ added in v2.3.0
type LeafPage struct { *LeafPageInfo Cells []*LeafCell }
type LeafPageInfo ¶ added in v2.3.0
type MetaPage ¶ added in v2.3.0
type MetaPage struct {
*MetaPageInfo
}
type MetaPageInfo ¶ added in v2.3.0
type Metric ¶ added in v2.4.0
type Metric struct {
// contains filtered or unexported fields
}
Metric is a simple, internal metric for check duration of operations.
type PageInfo ¶ added in v2.3.0
type PageInfo interface {
// contains filtered or unexported methods
}
type PageMap ¶ added in v2.4.0
type PageMap struct {
// contains filtered or unexported fields
}
Map represents an immutable hash map implementation. The map uses a Hasher to generate hashes and check for equality of key values.
It is implemented as an Hash Array Mapped Trie.
func NewPageMap ¶ added in v2.4.0
func NewPageMap() *PageMap
NewMap returns a new instance of Map. If hasher is nil, a default hasher implementation will automatically be chosen based on the first key added. Default hasher implementations only exist for int, string, and byte slice types.
func (*PageMap) Delete ¶ added in v2.4.0
Delete returns a map with the given key removed. Removing a non-existent key will cause this method to return the same map.
func (*PageMap) Get ¶ added in v2.4.0
Get returns the value for a given key and a flag indicating whether the key exists. This flag distinguishes a zero value set on a key versus a non-existent key in the map.
func (*PageMap) Iterator ¶ added in v2.4.0
func (m *PageMap) Iterator() *PageMapIterator
Iterator returns a new iterator for the map.
type PageMapBuilder ¶ added in v2.4.0
type PageMapBuilder struct {
// contains filtered or unexported fields
}
PageMapBuilder represents an efficient builder for creating Maps.
func NewPageMapBuilder ¶ added in v2.4.0
func NewPageMapBuilder() *PageMapBuilder
NewPageMapBuilder returns a new instance of PageMapBuilder.
func (*PageMapBuilder) Delete ¶ added in v2.4.0
func (b *PageMapBuilder) Delete(key uint32)
Delete removes the given key. See Map.Delete() for additional details.
func (*PageMapBuilder) Get ¶ added in v2.4.0
func (b *PageMapBuilder) Get(key uint32) (value int64, ok bool)
Get returns the value for the given key.
func (*PageMapBuilder) Iterator ¶ added in v2.4.0
func (b *PageMapBuilder) Iterator() *PageMapIterator
Iterator returns a new iterator for the underlying map.
func (*PageMapBuilder) Len ¶ added in v2.4.0
func (b *PageMapBuilder) Len() int
Len returns the number of elements in the underlying map.
func (*PageMapBuilder) Map ¶ added in v2.4.0
func (b *PageMapBuilder) Map() *PageMap
Map returns the underlying map. Only call once. Builder is invalid after call. Will panic on second invocation.
func (*PageMapBuilder) Set ¶ added in v2.4.0
func (b *PageMapBuilder) Set(key uint32, value int64)
Set sets the value of the given key. See Map.Set() for additional details.
type PageMapIterator ¶ added in v2.4.0
type PageMapIterator struct {
// contains filtered or unexported fields
}
MapIterator represents an iterator over a map's key/value pairs. Although map keys are not sorted, the iterator's order is deterministic.
func (*PageMapIterator) Done ¶ added in v2.4.0
func (itr *PageMapIterator) Done() bool
Done returns true if no more elements remain in the iterator.
func (*PageMapIterator) First ¶ added in v2.4.0
func (itr *PageMapIterator) First()
First resets the iterator to the first key/value pair.
type RootRecord ¶
func ReadRootRecord ¶
func ReadRootRecord(data []byte) (rec *RootRecord, remaining []byte, err error)
ReadRootRecord reads the page number & name for a root record. If there is not enough space or the pgno is zero then a nil record is returned. Returns the remaining buffer.
type RootRecordPage ¶ added in v2.3.0
type RootRecordPage struct { *RootRecordPageInfo Records []*RootRecord }
type RootRecordPageInfo ¶ added in v2.3.0
type Tx ¶
type Tx struct { // DeleteEmptyContainer lets us by default match the roaring // behavior where an existing container has all its bits cleared // but still sticks around in the database. DeleteEmptyContainer bool // contains filtered or unexported fields }
Tx represents a transaction.
func (*Tx) AddRoaring ¶
func (*Tx) ApplyFilter ¶ added in v2.7.0
func (*Tx) BitmapNames ¶
BitmapNames returns a list of all bitmap names.
func (*Tx) ContainerIterator ¶
func (*Tx) CountRange ¶
roaring.countRange counts the number of bits set between [start, end).
func (*Tx) CreateBitmap ¶
CreateBitmap creates a new empty bitmap with the given name. Returns an error if the bitmap already exists.
func (*Tx) CreateBitmapIfNotExists ¶
CreateBitmapIfNotExists creates a new empty bitmap with the given name. This is a no-op if the bitmap already exists.
func (*Tx) DeleteBitmap ¶
DeleteBitmap removes a bitmap with the given name. Returns an error if the bitmap does not exist.
func (*Tx) DeleteBitmapsWithPrefix ¶
DeleteBitmapsWithPrefix removes all bitmaps with a given prefix.
func (*Tx) FieldViews ¶
func (*Tx) ForEachRange ¶
func (*Tx) GetSizeBytesWithPrefix ¶ added in v2.8.0
GetSizeBytesWithPrefix returns the size of bitmaps with a given key prefix.
func (*Tx) GetSortedFieldViewList ¶ added in v2.7.0
func (*Tx) ImportRoaringBits ¶
func (*Tx) OffsetRange ¶
func (*Tx) PageN ¶ added in v2.3.0
PageN returns the number of pages in the database as seen by this transaction.
func (*Tx) PutContainer ¶
PutContainer inserts a container into a bitmap. Overwrites if key already exists.
func (*Tx) RemoveContainer ¶
RemoveContainer removes a container from the bitmap by key.
func (*Tx) RenameBitmap ¶
RenameBitmap updates the name of an existing bitmap. Returns an error if the bitmap does not exist.
func (*Tx) RoaringBitmap ¶
RoaringBitmap returns a bitmap as a Roaring bitmap.
func (*Tx) Root ¶
Root returns the root page number for a bitmap. Returns 0 if the bitmap does not exist.
func (*Tx) RootRecords ¶
RootRecords returns a list of root records.