kv

package module
v0.0.0-...-2dd8e4d Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2017 License: MIT Imports: 2 Imported by: 0

README

kv

Build Status

A Go key/value store service based on BoltDB, access BoltDB from multiple processes.

Integrate with Go

Please see here.

CLI example

go get github.com/helinwang/kv/cmd/kvctl
go get github.com/helinwang/kv/cmd/kv

# Start kv service:
kv -path db.bin

# Test (open another terminal)
kvctl put :8080 hello hi
kvctl get :8080 hello

# Output: hi

Graceful Shutdown

Supported in the CLI. Please see here.

Documentation

Overview

Example
package main

import (
	"fmt"
	"log"
	"net"
	"net/http"
	"net/rpc"
	"time"

	"github.com/boltdb/bolt"
	"github.com/helinwang/kv"
)

func main() {
	go func() {
		// Start the service.
		db, err := bolt.Open("/tmp/db_test.bin", 0666, nil)
		if err != nil {
			panic(err)
		}

		s := &kv.Service{DB: db}
		rpc.Register(s)
		rpc.HandleHTTP()

		l, e := net.Listen("tcp", ":8081")
		if e != nil {
			log.Fatal("listen error:", e)
		}

		err = http.Serve(l, nil)
		if err != nil {
			panic(err)
		}
	}()

	// Wait for the service to start.
	time.Sleep(50 * time.Millisecond)

	c, err := kv.New(":8081")
	if err != nil {
		panic(err)
	}

	err = c.Put([]byte("hello"), []byte("hi"))
	if err != nil {
		panic(err)
	}

	v, err := c.Get([]byte("hello"))
	if err != nil {
		panic(err)
	}

	fmt.Println(string(v))

}
Output:

hi

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is the client to the kv service.

func New

func New(addr string) (*Client, error)

New creates a new kv client.

func (*Client) Get

func (c *Client) Get(key []byte) ([]byte, error)

Get gets value from kv service.

func (*Client) Put

func (c *Client) Put(key []byte, value []byte) error

Put puts value into kv service.

type KV

type KV struct {
	Key   []byte
	Value []byte
}

KV is the argument for put.

type Service

type Service struct {
	DB *bolt.DB
}

Service is the kv service.

func (*Service) Get

func (s *Service) Get(key []byte, value *[]byte) error

Get gets value from the service.

func (*Service) Put

func (s *Service) Put(kv KV, _ *int) error

Put puts value into the service.

Directories

Path Synopsis
cmd
kv

Jump to

Keyboard shortcuts

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