package
0.2.0
Repository: https://github.com/unit-io/unitdb.git
Documentation: pkg.go.dev

# README

memdb GoDoc Go Report Card

The memdb is blazing fast specialized in memory key-value store for time-series database. The in-memory key-value data store persist entries into a WAL for immediate durability. The Write Ahead Log (WAL) retains memdb data when the db restarts. The WAL ensures data is durable in case of an unexpected failure.

About memdb

Key characteristics

  • 100% Go
  • Optimized for fast lookups and writes
  • All DB methods are safe for concurrent use by multiple goroutines.

Quick Start

To build memdb from source code use go get command.

go get -u github.com/unit-io/unitdb/memdb

Usage

Detailed API documentation is available using the go.dev service.

Make use of the client by importing it in your Go client source code. For example,

import "github.com/unit-io/unitdb/memdb"

The memdb supports Get, Set, Delete operations. It also supports batch operations.

Samples are available in the examples directory for reference.

Opening a database

To open or create a new database, use the memdb.Open() function:

	package main

	import (
		"log"

		"github.com/unit-io/unitdb/memdb"
	)

	func main() {
		// Opening a database.
		// Open DB with reset flag to to skip recovery from log
		db, err := memdb.Open(memdb.WithLogFilePath("unitdb"), memdb.WithLogReset())
		if err != nil {
			log.Fatal(err)
			return
		}	
		defer db.Close()
	}

Writing to a database

Store a message

Use DB.Put() function to insert a new key-value pair. Note, if key exists then it overrides key-value when the writes happen within same timeID (see timeRecordInterval option) or it insert a new key-value pair in different timeID.

	if timeID, err := db.Put(1, []byte("msg 1")); err != nil {
		log.Fatal(err)
		return
    }

Read message

Use DB.Get() function to read inserted value. It gets entry from most recent timeID for the provided key.

	if val, err := db.Get(1); err == nil {
        log.Printf("%s ", val)
    }

Lookup message

Use DB.Lookup() function to lookup entry for faster read.

	timeID, err := db.Put(1, []byte("msg 1"))
	if err != nil {
		log.Fatal(err)
		return
    }

	if val, err := db.Lookup(timeID, 1); err == nil {
        log.Printf("%s ", val)
    }

Deleting a message

use DB.Delete() function to delete a key-value pair.

    if err := db.Delete(1); err != nil {
        fmt.Println("error: ", err)
    }

Batch operation

Use batch operation to bulk insert records into memdb or bulk delete records from memdb. See examples under examples/memdb folder.

Writing to a batch

Use Batch.Put() to insert a new key-value or Batch.Delete() to delete a key-value from DB.

	db.Batch(func(b *memdb.Batch, completed <-chan struct{}) error {
		for i := 1; i <= 10; i++ {
            val := []byte(fmt.Sprintf("msg.%2d", i))
            b.Put(uint64(i), val)
        }
		return nil
    })

Contributing

If you'd like to contribute, please fork the repository and use a feature branch. Pull requests are welcome.

Licensing

This project is licensed under Apache-2.0 License.

# Functions

NewMeter provide meter to capture statistics.
Open initializes database.
ResponseHandler handles responses for monitoring routes.
WithBufferSize sets max size of buffer to use for buffer pooling.
WithDefaultOptions will open DB with some default values.
WithLogFilePath sets database directory for storing logs.
WithLogInterval sets interval for a time block.
WithLogReset flag to skip recovery on DB open and reset WAL.
WithMemdbSize sets max size of DB.
WithTimeBlockInterval sets interval for a time block.

# Structs

Batch is a write batch.
DB represents an SSD-optimized mem store.
Meter meter provides various db statistics.
Varz outputs memdb stats on the monitoring port at /varz.

# Interfaces

Options it contains configurable options and flags for DB.