kantadb

package module
v0.0.0-...-2976f93 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2022 License: GPL-3.0 Imports: 15 Imported by: 0

README

kanta

Go Report Card

A embeddable database for Go written in pure Go. It is based on LSM-trees meaning that it is more fit for high writes and low reads. The database contains fault-tolerance in the form of logs for in-memory tables and some other things.

Simple usage

You firstly need to install it:

go get -u github.com/nireo/kantadb

Then the basic usage is:

package main

import  (
	"log"

	"github.com/nireo/kantadb"
)


func main() {
	db := kantadb.New(kantadb.DefaultConfiguration())
	defer db.Stop()

	db.Run()

	// put a value
	db.Put("hello", "world")

	// get the value
	value, ok := db.Get("hello")
	if !ok {
		log.Println("value has not been found")
	}

	log.Printf("got value %s", value)

	// delete the value
	db.Delete("hello")
}

How it works

Firstly there is a simple in-memory table, which has a certain max capacity. Once that capacity gets filled, the in-memory table is placed in to a queue, in which it will be written to disk. The queue is checked every 25-100 milliseconds and then if that queue is not empty, tables from the queue are written to disk as sstables.

The combination of in-memory table log files and the sstables makes persistence possible. All the log files and sstables are checked when the database is started up again. Log files are used to make sure in-memory data doesn't get lost if the database suddenly shuts down.

Todo

- Add transactions
- Stop using mutexes and start using channels for concurrency
- Add levels
- Improve the compaction process

Documentation

Index

Constants

View Source
const MaxMemSize int64 = 1024 * 256

256 kb

View Source
const MaxSSTableSize int64 = 1024 * 1014 * 24

24 mb

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	StorageDir string
	MaxMemSize int
	Debug      bool
}

Config represents different parameters to change te default behaviour of the database

func DefaultConfiguration

func DefaultConfiguration() *Config

DefaultConfiguration returns a database config that has some default values

type DB

type DB struct {
	Alive    bool
	MEM      *mem.MEM
	SSTables []*sstable.SSTable

	MEMQueue []*mem.MEM
	// contains filtered or unexported fields
}

DB represents the database as a whole.

func New

func New(config *Config) *DB

New returns a instance of a database given a storage directory for sstables.

func (*DB) CompactNTables

func (db *DB) CompactNTables(n int) error

compactNTables combines the last n sstables.

func (*DB) Delete

func (db *DB) Delete(key string) error

Delete has almost exactly the same functionality as read, but instead we set the value of the key to a TombstoneValue which just corresponds to a string where the first character is a null-byte. We cannot remove the key from the in-memory table since it might reside in the queue or sstable. The key will be ultimately deleted when sstable compaction happens.

func (*DB) Get

func (db *DB) Get(key string) (string, bool)

Get tries to find the wanted key from the in-memory table, and if not found checks it then checks the queue for the value. If the value is not found in the queue, check the sstables. If the value corresponds to a TombstoneValue return a invalid key since the key was "deleted".

func (*DB) GetCompactableFiles

func (db *DB) GetCompactableFiles() []string

GetCompactableFiles returns all of the files in the SSTable directory that are small enough.

func (*DB) GetDirectory

func (db *DB) GetDirectory() string

GetDirectory returns the directory in which all of the files are stored.

func (*DB) GetSSTableIndex

func (db *DB) GetSSTableIndex(filename string) int

GetSSTableIndex returns the index of the sstable with a given filename

func (*DB) GetTableSize

func (db *DB) GetTableSize() int

GetTableSize returns the amount of sstables indexed

func (*DB) MergeFiles

func (db *DB) MergeFiles(f1, f2 string) error

func (*DB) Put

func (db *DB) Put(key, val string) error

Put writes a value into the in-memory table and also checks if the amount of items in the in-memory table exceeds the amount specified in the database configuation. If the number is exceeded, add the in-memory table to the start of the queue.

func (*DB) Run

func (db *DB) Run() error

Run starts the db service and starts checking for queue and other things

func (*DB) Stop

func (db *DB) Stop() error

Stop clears the data gracefully from the memtables are sstable write queue

type WRequest

type WRequest struct {
	Key   string
	Value string
	// contains filtered or unexported fields
}

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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