btree

package module
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 20, 2024 License: AGPL-3.0 Imports: 9 Imported by: 0

README

GO BTree

A fast, simple persistent BTree implementation in Go.

Features

  • Easy to use API with Put, Get, Delete, Remove, Iterator, Range methods
  • Disk based storage
  • Supports keys with multiple values
  • Supports large keys and values

[!WARNING] Not thread safe. You must handle concurrency control yourself.

Usage

Importing
import "github.com/guycipher/btree"
Creating a new BTree

You can use the Open method to open an existing btree or create a new one. You can specify the file, permission and T(degree)

// name of the file, flags, file mode, T(degree)
btree, err := Open("btree.db", os.O_CREATE|os.O_RDWR, 0644, 3)
if err != nil {
..
}
Inserting a key-value pair

You can insert a value into a key using the Put method. Keys can store many values.

err := bt.Put([]byte("key"), []byte("value"))
Getting a value

To get a value you can you the Get method. The get method will return all the keys values.

values, err := bt.Get([]byte("key"))
Deleting a key

To delete a key and all of it's values you can use the Delete method.

err := btree.Delete([]byte("key"))
Removing a value within key

To remove a value from a key you can use the Remove method.

err := btree.Remove([]byte("key"), []byte("value"))
Iterator

The iterator is used to iterate over values of a key

iterator := key.Iterator()

for {
    value, ok := iterator()
    if !ok {
        break
    }

    fmt.Println(string(value))
}

Result

value1
value2
value3
Range query
keys, err := bt.Range([]byte("key1"), []byte("key3"))
Closing the BTree

You can close the BTree by calling the Close function.

err := btree.Close()

Documentation

Overview

Package btree A BTree implementation optimized for disk storage. # Copyright (C) Alex Gaetano Padula

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

Package btree File pager implementation Copyright (C) Alex Gaetano Padula

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

Index

Constants

View Source
const HEADER_SIZE = 256 // next (overflowed), deleted
View Source
const PAGE_SIZE = 1024 // Page size

Variables

This section is empty.

Functions

This section is empty.

Types

type BTree

type BTree struct {
	Pager *Pager // The pager for the btree
	T     int    // The order of the tree
}

BTree is the main BTree struct ** not thread safe

func Open

func Open(name string, flag, perm int, t int) (*BTree, error)

Open opens a new or existing BTree

func (*BTree) Close

func (b *BTree) Close() error

Close closes the BTree

func (*BTree) Delete

func (b *BTree) Delete(k []byte) error

Delete deletes a key from the BTree

func (*BTree) Get

func (b *BTree) Get(k []byte) (*Key, error)

Get returns the values associated with a key

func (*BTree) PrintTree

func (b *BTree) PrintTree() error

PrintTree prints the tree (for debugging purposes ****)

func (*BTree) Put

func (b *BTree) Put(key, value []byte) error

Put inserts a key into the BTree A key can have multiple values Put inserts a key value pair into the BTree

func (*BTree) Range

func (b *BTree) Range(start, end []byte) ([]interface{}, error)

Range returns all keys in the BTree that are within the range [start, end]

func (*BTree) Remove

func (b *BTree) Remove(key, value []byte) error

Remove removes a value from key

type Key

type Key struct {
	K []byte   // The key
	V [][]byte // The values
}

Key is the key struct for the BTree

func (*Key) Iterator

func (k *Key) Iterator() func() ([]byte, bool)

type Node

type Node struct {
	Page     int64   // The page number of the node
	Keys     []*Key  // The keys in node
	Children []int64 // The children of the node
	Leaf     bool    // If the node is a leaf node
}

Node is the node struct for the BTree

type Pager

type Pager struct {
	// contains filtered or unexported fields
}

Pager manages pages in a file

func OpenPager

func OpenPager(filename string, flag int, perm os.FileMode) (*Pager, error)

OpenPager opens a file for page management

func (*Pager) Close

func (p *Pager) Close() error

Close closes the file

func (*Pager) DeletePage

func (p *Pager) DeletePage(pageID int64) error

DeletePage deletes a page

func (*Pager) GetPage

func (p *Pager) GetPage(pageID int64) ([]byte, error)

GetPage gets a page and returns the data Will gather all the pages that are linked together

func (*Pager) Write

func (p *Pager) Write(data []byte) (int64, error)

Write writes data to the next available page

func (*Pager) WriteTo

func (p *Pager) WriteTo(pageID int64, data []byte) error

WriteTo writes data to a specific page

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL