Skip to content

roy2220/plainkv

Folders and files

NameName
Last commit message
Last commit date

Latest commit

4d95416 · Jan 4, 2022

History

47 Commits
Jun 15, 2020
Mar 4, 2020
Mar 28, 2020
Jun 15, 2020
Jun 1, 2020
Jun 15, 2020
Apr 22, 2020
Jun 15, 2020
Jun 1, 2020
Mar 4, 2020
Jun 15, 2020
Jan 4, 2022
Mar 26, 2020
Jun 15, 2020
Mar 28, 2020
Jun 15, 2020
Mar 26, 2020
Jun 15, 2020
Mar 26, 2020

Repository files navigation

plainkv

GoDoc Build Status Coverage Status

A simple key-value storage library for Go which provides:

OrderedDict

An on-disk B+ tree

Structure

Structure

Non-Leaf Structure

Leaf Structure

Leaf KV Structure

Example

package main

import (
        "fmt"

        "github.com/roy2220/plainkv"
)

func main() {
        func() {
                od, err := plainkv.OpenOrderedDict("./testdata/ordereddict.tmp", true)
                if err != nil {
                        panic(err)
                }
                defer od.Close()

                od.Set([]byte("foo"), []byte("bar"), false /* don't return the replaced value */)

                _, ok := od.SetIfNotExists([]byte("hello"), []byte("w0rd"), false /* don't return the present value */)
                fmt.Printf("%v\n", ok)

                v, ok := od.SetIfExists([]byte("hello"), []byte("world"), true /* return the replaced value */)
                fmt.Printf("%v %q\n", ok, v)
        }()

        func() {
                od, err := plainkv.OpenOrderedDict("./testdata/ordereddict.tmp", false)
                if err != nil {
                        panic(err)
                }
                defer od.Close()

                for it := od.RangeAsc(plainkv.MinKey, plainkv.MaxKey); !it.IsAtEnd(); it.Advance() {
                        k, v, _ := it.ReadRecordAll()
                        fmt.Printf("%q %q\n", k, v)
                }

                for it := od.RangeDesc([]byte("foo"), plainkv.MaxKey); !it.IsAtEnd(); it.Advance() {
                        k, v, _ := it.ReadRecordAll()
                        fmt.Printf("%q %q\n", k, v)
                }

                minKey, _ := od.RangeAsc(plainkv.MinKey, plainkv.MinKey).ReadKeyAll()
                maxKey, _ := od.RangeDesc(plainkv.MaxKey, plainkv.MaxKey).ReadKeyAll()
                fmt.Printf("%q...%q\n", minKey, maxKey)

                v, ok := od.Test([]byte("foo"), true /* return the present value */)
                fmt.Printf("%v %q\n", ok, v)

                v, ok = od.Clear([]byte("hello"), true /* return the removed value */)
                fmt.Printf("%v %q\n", ok, v)
        }()
        // Output:
        // true
        // true "w0rd"
        // "foo" "bar"
        // "hello" "world"
        // "hello" "world"
        // "foo" "bar"
        // "foo"..."hello"
        // true "bar"
        // true "world"
}

Dict

An on-disk hash map

Structure

Structure

Example

package main

import (
        "fmt"

        "github.com/roy2220/plainkv"
)

func main() {
        func() {
                d, err := plainkv.OpenDict("./testdata/dict.tmp", true)
                if err != nil {
                        panic(err)
                }
                defer d.Close()

                d.Set([]byte("foo"), []byte("bar"), false /* don't return the replaced value */)

                _, ok := d.SetIfNotExists([]byte("hello"), []byte("w0rd"), false /* don't return the present value */)
                fmt.Printf("%v\n", ok)

                v, ok := d.SetIfExists([]byte("hello"), []byte("world"), true /* return the replaced value */)
                fmt.Printf("%v %q\n", ok, v)
        }()

        func() {
                d, err := plainkv.OpenDict("./testdata/dict.tmp", false)
                if err != nil {
                        panic(err)
                }
                defer d.Close()

                dc := plainkv.DictCursor{}
                for {
                        k, v, ok := d.Scan(&dc)
                        if !ok {
                                break
                        }
                        fmt.Printf("%q %q\n", k, v)
                }

                v, ok := d.Test([]byte("foo"), true /* return the present value */)
                fmt.Printf("%v %q\n", ok, v)

                v, ok = d.Clear([]byte("hello"), true /* return the removed value */)
                fmt.Printf("%v %q\n", ok, v)
        }()
        // Output:
        // true
        // true "w0rd"
        // "foo" "bar"
        // "hello" "world"
        // true "bar"
        // true "world"
}

About

A simple key-value storage library for Go

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages