treedb

package module
v0.0.0-...-b0c7cc0 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2019 License: MIT Imports: 13 Imported by: 0

README

TreeDB

TreeDB is a cache path sensitive hierarchical data store, used as NoSQL database.

TreeDB is in early stage of development, none parts should be considered as stable. Use it with your own caution!

Why another database ?

Database is a facility that provides both memory cache and disk storage, I concludes to this after years development in game server scenarios. What hit me often is memory cache.

The primary reason to use memory cache is performance. Today, performance promotion benefits from scenario insensitive cache used in disk oriented databases is far behind requirement. Memory cache systems are adopted to alleviate database load. Hence boilerplates exists in many projects to handle the similar situations: cache missings, cache loadings, cache consistence, and data writebacks. Full memory databases, such as Redis, suffers from other problems: costs and capacities.

Things complicates things. The origin problem is that cache policy used by disk oriented databases results in little performance promotion. Why not give some hits to cache facility? This leads me to create TreeDB.

First look

Data stored in TreeDB are structed as hierachical tree structure. Data are accessed through path, similar to the filesystem one. Here is the demonstration using treedb Go package.

// Open database, if missing, clone it from database "template0".
db, err := treedb.Open("tcp://:3456", "test", &treedb.Options{TemplateDB: "template0", CreateIfMissing: true})

// Set cache pattern for all keys under tree "/game/users/".
// You can set cache patterns in template database.
db.Cache("/game/users/*", time.Minute * 30)

// Match previous cache pattern, the path "/game/users/10023" will be cached in memory
// as a whole for about 30 minutes.
userData, err := db.Get("/game/users/10023", treedb.FieldTree)

// No cache pattern matched, the path "/game/modules/arena" may not cached in memory based on other configs.
db.Get("/game/modules/arena", treedb.FieldBinary)

// Modify data.
db.Set("/game/users/10023/items/20003", binaryData)
db.Delete("/game/users/100034/items/20345")

// Update path's last access timestamp if in memory.
db.Touch("/game/users/10023")

// Delete the whole path, whether it is binary or tree.
db.Delete("/game/users/10023")

// Create whole tree at path.
var userData map[string]interface{}
db.Set("/game/users/10234", userData)

Repository layout

The layout of this repository is divided to four parts:

  • Server daemon treedbd locates in cmd/treedbd directory.
  • Command line tool treedb locates in cmd/treedb directory.
  • Client-Server protocol locates in protocol directory, demonstrated as Go package.
  • Go package as client interface to server is located in the top directory.

TODO

  • Customized leveldb to collect orphan tree in merging time
  • Replication
    • Customized leveldb to store WAL for synchronization

License

The MIT License (MIT). See LICENSE for the full license text.

Contribution

Issues and pull requests are welcome.

Documentation

Index

Constants

View Source
const (
	FieldAny = iota
	FieldTree
	FieldBinary
)
View Source
const (
	CachePathNever   = -2
	CachePathDelete  = -1
	CachePathDefault = 0
)
View Source
const (
	DefaultChannelCap = 128
)

Variables

View Source
var (
	ErrClientClosed        = errors.New("treedb: client closed")
	ErrServerClosed        = errors.New("treedb: server closed")
	ErrIncompatibleVersion = errors.New("treedb: incompatible version")
)
View Source
var (
	ErrDBClosed = errors.New("treedb: db closed")
)

Functions

This section is empty.

Types

type Client

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

func Dial

func Dial(addr string, opts *DialOptions) (*Client, error)

func (*Client) Close

func (c *Client) Close() error

func (*Client) OpenDB

func (c *Client) OpenDB(name string, options *OpenOptions) (*DB, error)

func (*Client) Release

func (c *Client) Release()

type DB

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

func Open

func Open(addr, name string, opts *Options) (*DB, error)

func (*DB) Cache

func (db *DB) Cache(path string, timeout int64) error

func (*DB) Close

func (db *DB) Close() error

func (*DB) Delete

func (db *DB) Delete(path string) error

func (*DB) Get

func (db *DB) Get(path string, kind FieldType) (interface{}, error)

func (*DB) Set

func (db *DB) Set(path string, value interface{}) error

func (*DB) Touch

func (db *DB) Touch(path string) error

func (*DB) Tree

func (db *DB) Tree(path string) *Tree

type DialOptions

type DialOptions struct {
	ChannelCap int
}

type FieldType

type FieldType uint

type OpenOptions

type OpenOptions struct {
	TemplateDB      string
	CreateIfMissing bool
	ErrorIfExists   bool
}

type Options

type Options struct {
	ChannelCap int

	TemplateDB      string
	CreateIfMissing bool
	ErrorIfExists   bool
}

type ResponseTypeError

type ResponseTypeError struct {
	Type reflect.Type
}

func (*ResponseTypeError) Error

func (e *ResponseTypeError) Error() string

type ServerAddressError

type ServerAddressError struct {
	Addr string
}

func (*ServerAddressError) Error

func (e *ServerAddressError) Error() string

type Tree

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

func (*Tree) Cache

func (t *Tree) Cache(path string, timeout int64) error

func (*Tree) Delete

func (t *Tree) Delete(path string) error

func (*Tree) Get

func (t *Tree) Get(path string, kind FieldType) (interface{}, error)

func (*Tree) Set

func (t *Tree) Set(path string, value interface{}) error

func (*Tree) Touch

func (t *Tree) Touch(path string) error

func (*Tree) Tree

func (t *Tree) Tree(path string) *Tree

Directories

Path Synopsis
cmd
Package protocol is a generated protocol buffer package.
Package protocol is a generated protocol buffer package.

Jump to

Keyboard shortcuts

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