🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

github.com/Livbz/mossdb

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/Livbz/mossdb

v0.0.2
Source
Go
Version published
Created
Source

MossDB

MossDB is a in-memory, persistent and embedded key-value store.

Features:

  • Key-value memory store, just use a simple map to store key/value
  • Transactions supported
  • AOL persistent, use the wal to store every commit, just like redis append-only file
  • TTL supported, user can set the key with expire time that based on time heap
  • Watch interface, caller will receive event when the value of key changes
  • Embedded with a simple API
  • Multi storage engine, now it support Map and RadixTree as backend storage

The architecture of MossDB as follows:

MossDB

Getting Started

Getting MossDB

To start using MossDB, just use go get:

go get github.com/qingwave/mossdb

Using MossDB

There is a simple example shows how to use MossDB.

package main

import (
	"context"
	"errors"
	"log"
	"time"

	"github.com/qingwave/mossdb"
)

func () {
    // create db instance
	db, err := mossdb.New(&mossdb.Config{})
	if err != nil {
		log.Fatal(err)
	}

    // set, get, delete data
	db.Set("key1", []byte("val1"))
    log.Printf("get key1: %s", db.Get("key1"))
    db.Delete("key1", []byte("val1"))
}

Transactions

The database allows for rich transactions, in which multiple objects are inserted, updated or deleted. Note that one transaction allowed at a time.

    db.Tx(func(tx *mossdb.Tx) error {
        val1 := tx.Get("key1")

        tx.Set("key2", val1)

        return nil
    })

Watch

Watch interface just like etcd Watch(watch a key or prefix key), caller will receive event when the watched keys modified.

func Watch(db *mossdb.DB) {
	key := "watch-key"

	go func() {
		db.Set(key, mossdb.Val("val1"))
		db.Set(key, mossdb.Val("val2"))
		db.Delete(key)

		time.Sleep(100 * time.Second)
		db.Set(key, mossdb.Val("val3"))
	}()

	ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
	defer cancel()

	ch := db.Watch(ctx, key)

	for {
		select {
		case <-ctx.Done():
			log.Printf("context done")
			return
		case resp, ok := <-ch:
			if !ok {
				log.Printf("watch done")
				return
			}
			log.Printf("receive event: %s, key: %s, new val: %s", resp.Event.Op, resp.Event.Key, resp.Event.Val)
		}
	}
}

TTL

Key with expired time in useful sometimes, MossDB use a time heap to expire key near real-time.

db.Set("key-ttl", []byte("val-ttl"), mossdb.WithTTL(100*time.Millisecond))

FAQs

Package last updated on 07 Apr 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts