Giter Site home page Giter Site logo

rogulraj / libradb Goto Github PK

View Code? Open in Web Editor NEW

This project forked from amit-davidson/libradb

0.0 0.0 0.0 93 KB

LibraDB is a simple, persistent key/value store written in pure Go in less than 1000 lines for learning purposes.

License: GNU General Public License v3.0

Go 100.00%

libradb's Introduction

LibraDB

made-with-Go made-with-Go MIT license PRs Welcome amit-davidson

LibraDB is a simple, persistent key/value store written in pure Go. The project aims to provide a working yet simple example of a working database. If you're interested in databases, I encourage you to start here.

This database accompanies my blog post on how to write a database from scratch.

Installing

To start using LibraDB, install Go and run go get:

go get -u github.com/amit-davidson/LibraDB

Basic usage

package main

import "github.com/amit-davidson/LibraDB"

func main() {
	path := "libra.db"
	db, _ := LibraDB.Open(path, LibraDB.DefaultOptions)

	tx := db.WriteTx()
	name := []byte("test")
	collection, _ := tx.CreateCollection(name)

	key, value := []byte("key1"), []byte("value1")
	_ = collection.Put(key, value)

	_ = tx.Commit()
}

Transactions

Read-only and read-write transactions are supported. LibraDB allows multiple read transactions or one read-write transaction at the same time. Transactions are goroutine-safe.

LibraDB has an isolation level: Serializable. In simpler words, transactions are executed one after another and not at the same time.This is the highest isolation level.

Read-write transactions

tx := db.WriteTx()
...
if err := tx.Commit(); err != nil {
    return err
}

Read-only transactions

tx := db.ReadTx()
...
if err := tx.Commit(); err != nil {
    return err
}

Collections

Collections are a grouping of key-value pairs. Collections are used to organize and quickly access data as each collection is B-Tree by itself. All keys in a collection must be unique.

tx := db.WriteTx()
collection, err := tx.CreateCollection([]byte("test"))
if err != nil {
	return err
}
_ = tx.Commit()

Auto generating ID

The Collection.ID() function returns an integer to be used as a unique identifier for key/value pairs.

tx := db.WriteTx()
collection, err := tx.GetCollection([]byte("test"))
if err != nil {
    return err
}
id := collection.ID()
_ = tx.Commit()

Key-Value Pairs

Key/value pairs reside inside collections. CRUD operations are possible using the methods Collection.Put Collection.Find Collection.Remove as shown below.

tx := db.WriteTx()
collection, err := tx.GetCollection([]byte("test"))
if  err != nil {
    return err
}

key, value := []byte("key1"), []byte("value1")
if err := collection.Put(key, value); err != nil {
    return err
}
if item, err := collection.Find(key); err != nil {
    return err
}

if err := collection.Remove(key); err != nil {
    return err
}
_ = tx.Commit()

libradb's People

Contributors

amit-davidson avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.