taodb

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2019 License: Apache-2.0 Imports: 18 Imported by: 0

README

TaoDB

This is server and client of goleveldb

Dependencies

Install TaoDB

go get github.com/markusleevip/taodb

Getting Startted

Starting the server

cd taodb
./build.sh
./taodbd -dbPath=/data/storage/taodb -addr=:7398
Starting the client(test)

cd taodb
./taodb -addr=:7398
Usage the client(example)
package main

import (
	"flag"
	"fmt"
	"github.com/markusleevip/taodb/client"
	"github.com/markusleevip/taodb/log"
	"github.com/markusleevip/taodb/resp"
	"net"
)

var flags struct {
	addr string
	logto    string
	loglevel string
}

func init() {
	flag.StringVar(&flags.addr,"addr", ":7398", "server addr")
	flag.StringVar(&flags.logto,"log", "stdout", "Write log messages to this file. 'stdout' and 'none' have special meanings")
	flag.StringVar(&flags.loglevel,"log-level", "DEBUG", "The level of messages to log. One of: DEBUG, INFO, WARNING, ERROR")
}

func main() {
	flag.Parse()

	// init logging
	log.LogTo(flags.logto, flags.loglevel)

	pool,_ := client.New(func() (net.Conn, error) {
		return net.Dial("tcp", flags.addr)
	})
	defer pool.Close()
	cn, _  :=pool.Get()
	defer pool.Put(cn)

	cn.WriteCmdString("PING")
	cn.WriteCmdString("ECHO", "HEllO")
	cn.WriteCmd("SET", []byte("hello"), []byte("Hello 世界 哈哈"))
	cn.WriteCmdString("GET", "key")
	if err := cn.Flush(); err != nil {
		cn.MarkFailed()
		panic(err)
	}

	// Consume responses
	for i := 0; i < 4; i++ {
		t, err := cn.PeekType()
		if err != nil {
			return
		}

		switch t {
		case resp.TypeInline:
			s, _ := cn.ReadInlineString()
			fmt.Println(s)
		case resp.TypeBulk:
			s, _ := cn.ReadBulk(nil)
			fmt.Println(string(s[:]))
		case resp.TypeInt:
			n, _ := cn.ReadInt()
			fmt.Println(n)
		case resp.TypeNil:
			_ = cn.ReadNil()
			fmt.Println(nil)
		case resp.TypeError:
			err,_:=cn.ReadError()
			fmt.Println(err)
		default:
			panic("unexpected response type")
		}
	}

	/*
	for i := 0; i < 100; i++ {
		client.Set(fmt.Sprintf("hello%d", i), []byte(fmt.Sprintf("Hello World!%d", i)))
	}

	for i := 0; i < 100; i++ {
		value, _ := client.Get(fmt.Sprintf("hello%d", i))
		log.Info("get key:hello%d,value=%s\n", i, string(value[:]))
	}

	ctx, _ := client.Prefix("hello")
	if len(ctx) == 0 {
		log.Info("ctx is null")
	} else {
		fmt.Println("cit is not null")
		data := make(map[string]string)
		err := json.Unmarshal(ctx, &data)
		if err != nil {
			log.Error("json error:", err)
		}
		if len(data) > 0 {
			for key, value := range data {
				log.Info("pre.key=%s,%s\n", key, value)
			}
		}
	}
	ctx, _ = client.PrefixOnlyKey("hello")
	if len(ctx) == 0 {
		log.Info("ctx is null")
	} else {
		data := make([]string, 0)
		err := json.Unmarshal(ctx, &data)
		if err != nil {
			log.Error("json error:", err)
			return
		}
		if len(data) > 0 {
			log.Info("data.len=%d", len(data))
			for i, key := range data {
				log.Info("pre.i=%d,key=%s", i, key)
				value, _ := client.Get(key)
				log.Info("getValue=%s", value[:])
			}
		}

	}
	*/

}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrNotFound added in v1.0.0

func ErrNotFound(key string) error

func ErrUnknownCommand added in v1.0.0

func ErrUnknownCommand(cmd string) error

ErrUnknownCommand returns an unknown command error

func ErrWrongNumberOfArgs added in v1.0.0

func ErrWrongNumberOfArgs(cmd string) error

ErrWrongNumberOfArgs returns an unknown command error

func NotFound added in v1.0.0

func NotFound(key string) string

func UnknownCommand added in v1.0.0

func UnknownCommand(cmd string) string

UnknownCommand returns an unknown command error string

func WrongData added in v1.0.0

func WrongData(cmd string) string

func WrongNumberOfArgs added in v1.0.0

func WrongNumberOfArgs(cmd string) string

WrongNumberOfArgs returns an unknown command error string

Types

type Client added in v1.0.0

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

Client contains information about a client connection

func GetClient added in v1.0.0

func GetClient(ctx context.Context) *Client

GetClient retrieves the client from a the context. This function may return nil if a client is not set.

func (*Client) Close added in v1.0.0

func (c *Client) Close()

Close will disconnect as soon as all pending replies have been written to the client

func (*Client) Context added in v1.0.0

func (c *Client) Context() context.Context

Context return the client context

func (*Client) ID added in v1.0.0

func (c *Client) ID() uint64

ID return the unique client id

func (*Client) RemoteAddr added in v1.0.0

func (c *Client) RemoteAddr() net.Addr

RemoteAddr return the remote client address

func (*Client) SetContext added in v1.0.0

func (c *Client) SetContext(ctx context.Context)

SetContext sets the client's context

type ClientInfo added in v1.0.0

type ClientInfo struct {
	// ID is the internal client ID
	ID uint64

	// RemoteAddr is the remote address string
	RemoteAddr string

	// LastCmd is the last command called by this client
	LastCmd string

	// CreateTime returns the time at which the client has
	// connected to the server
	CreateTime time.Time

	// AccessTime returns the time of the last access
	AccessTime time.Time
}

ClientInfo contains client stats

func (*ClientInfo) String added in v1.0.0

func (i *ClientInfo) String() string

String generates an info string

type CommandDescription added in v1.0.0

type CommandDescription struct {
	// Name is the command name, returned as a lowercase string.
	Name string

	// Arity is the command arity specification.
	// https://redis.io/commands/command#command-arity.
	// It follows a simple pattern:
	//   positive if command has fixed number of required arguments.
	//   negative if command has minimum number of required arguments, but may have more.
	Arity int64

	// Flags is an enumeration of command flags.
	// https://redis.io/commands/command#flags.
	Flags []string

	// FirstKey is the position of first key in argument list.
	// https://redis.io/commands/command#first-key-in-argument-list
	FirstKey int64

	// LastKey is the position of last key in argument list.
	// https://redis.io/commands/command#last-key-in-argument-list
	LastKey int64

	// KeyStepCount is the step count for locating repeating keys.
	// https://redis.io/commands/command#step-count
	KeyStepCount int64
}

CommandDescription describes supported commands

type CommandDescriptions added in v1.0.0

type CommandDescriptions []CommandDescription

CommandDescriptions returns a command handler. https://redis.io/commands/command

func (CommandDescriptions) ServeRedeo added in v1.0.0

func (s CommandDescriptions) ServeRedeo(w resp.ResponseWriter, c *resp.Command)

type Config added in v1.0.0

type Config struct {
	Timeout     time.Duration
	IdleTimeout time.Duration

	TCPKeepAlive time.Duration
}

type DB

type DB interface {
	Set(string, []byte) error
	Get(string) ([]byte, error)
	Del(string) error
	State(string) (string, error)
	Iterator(prefix string) (map[string]string, error)
	IteratorOnlyKey(prefix string) ([]string, error)
}

type Handler added in v1.0.0

type Handler interface {
	// ServeRedeo serves a request.
	ServeRedeo(w resp.ResponseWriter, c *resp.Command)
}

Handler is an abstract handler interface for responding to commands

func Del added in v1.0.0

func Del(s *Server) Handler

func Echo added in v1.0.0

func Echo() Handler

Echo returns an echo handler. https://redis.io/commands/echo

func Get added in v1.0.0

func Get(s *Server) Handler

func Info added in v1.0.0

func Info(s *Server) Handler

Info returns an info handler. https://redis.io/commands/info

func Iterator added in v1.0.0

func Iterator(s *Server) Handler

func Ping added in v1.0.0

func Ping() Handler

Ping returns a ping handler. https://redis.io/commands/ping

func Set added in v1.0.0

func Set(s *Server) Handler

type HandlerFunc added in v1.0.0

type HandlerFunc func(w resp.ResponseWriter, c *resp.Command)

HandlerFunc is a callback function, implementing Handler.

func (HandlerFunc) ServeRedeo added in v1.0.0

func (f HandlerFunc) ServeRedeo(w resp.ResponseWriter, c *resp.Command)

ServeRedeo calls f(w, c).

type Server added in v1.0.0

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

func NewServer added in v1.0.0

func NewServer(config *Config, dbPath string) *Server

NewServer creates a new server instance

func (*Server) Handle added in v1.0.0

func (srv *Server) Handle(name string, h Handler)

Handle registers a handler for a command.

func (*Server) HandleFunc added in v1.0.0

func (srv *Server) HandleFunc(name string, fn HandlerFunc)

HandleFunc registers a handler func for a command.

func (*Server) HandleStream added in v1.0.0

func (srv *Server) HandleStream(name string, h StreamHandler)

HandleStream registers a handler for a streaming command.

func (*Server) HandleStreamFunc added in v1.0.0

func (srv *Server) HandleStreamFunc(name string, fn StreamHandlerFunc)

HandleStreamFunc registers a handler func for a command

func (*Server) Info added in v1.0.0

func (srv *Server) Info() *ServerInfo

Info returns the server info registry

func (*Server) Serve added in v1.0.0

func (srv *Server) Serve(lis net.Listener) error

Serve accepts incoming connections on a listener, creating a new service goroutine for each.

type ServerInfo added in v1.0.0

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

ServerInfo contains server stats

func (*ServerInfo) ClientInfo added in v1.0.0

func (i *ServerInfo) ClientInfo() []ClientInfo

ClientInfo returns details about connected clients

func (*ServerInfo) Fetch added in v1.0.0

func (i *ServerInfo) Fetch(name string) *info.Section

Fetch finds or creates an info section. This method is not thread-safe.

func (*ServerInfo) Find added in v1.0.0

func (i *ServerInfo) Find(name string) *info.Section

Find finds an info section by name.

func (*ServerInfo) NumClients added in v1.0.0

func (i *ServerInfo) NumClients() int

NumClients returns the number of connected clients

func (*ServerInfo) Register added in v1.0.0

func (i *ServerInfo) Register(c *Client)

func (*ServerInfo) String added in v1.0.0

func (i *ServerInfo) String() string

String generates an info string

func (*ServerInfo) TotalCommands added in v1.0.0

func (i *ServerInfo) TotalCommands() int64

TotalCommands returns the total number of commands executed since the start of the server.

func (*ServerInfo) TotalConnections added in v1.0.0

func (i *ServerInfo) TotalConnections() int64

TotalConnections returns the total number of connections made since the start of the server.

type StreamHandler added in v1.0.0

type StreamHandler interface {
	// ServeRedeoStream serves a streaming request.
	ServeRedeoStream(w resp.ResponseWriter, c *resp.CommandStream)
}

StreamHandler is an interface for responding to streaming commands

type StreamHandlerFunc added in v1.0.0

type StreamHandlerFunc func(w resp.ResponseWriter, c *resp.CommandStream)

StreamHandlerFunc is a callback function, implementing Handler.

func (StreamHandlerFunc) ServeRedeoStream added in v1.0.0

func (f StreamHandlerFunc) ServeRedeoStream(w resp.ResponseWriter, c *resp.CommandStream)

ServeRedeoStream calls f(w, c).

type SubCommands added in v1.0.0

type SubCommands map[string]Handler

SubCommands returns a handler that is parsing sub-commands

func (SubCommands) ServeRedeo added in v1.0.0

func (s SubCommands) ServeRedeo(w resp.ResponseWriter, c *resp.Command)

type WrapperFunc added in v1.0.0

type WrapperFunc func(c *resp.Command) interface{}

WrapperFunc implements Handler, accepts a command and must return one of the following types:

nil
error
string
[]byte
bool
float32, float64
int, int8, int16, int32, int64
uint, uint8, uint16, uint32, uint64
resp.CustomResponse instances
slices of any of the above typs
maps containing keys and values of any of the above types

func (WrapperFunc) ServeRedeo added in v1.0.0

func (f WrapperFunc) ServeRedeo(w resp.ResponseWriter, c *resp.Command)

ServeRedeo implements Handler

Directories

Path Synopsis
Package client implements a minimalist client for working with redis servers.
Package client implements a minimalist client for working with redis servers.
main
Package pool is a generic, high-performance pool for net.Conn objects.
Package pool is a generic, high-performance pool for net.Conn objects.
Package resp implements low-level primitives for dealing with RESP (REdis Serialization Protocol).
Package resp implements low-level primitives for dealing with RESP (REdis Serialization Protocol).

Jump to

Keyboard shortcuts

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