rosedb

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2022 License: Apache-2.0 Imports: 24 Imported by: 1

README

rosedb_ico.png

Go Report Card GitHub top language GitHub stars codecov CodeFactor Go Reference Mentioned in Awesome Go LICENSE

English| 简体中文

rosedb is a fast, stable, and embedded NoSQL database based on bitcask, supports a variety of data structures such as string, list, hash, set, and sorted set.

It is similar to Redis but store values on disk.

Key features:

  • Compatible with Redis protocol (not fully)
  • Many data structures: string, list, hash, set, and sorted set
  • Easy to embed into your own Go application
  • High performance, suitable for both read and write intensive workload
  • Values are not limited by RAM

Design Overview

Quick Start

1. embedded usage: see examples

2. command line usage:

start rosedb server

cd rosedb
make
./rosedb-server [-option value]

access data via cli(a copy of redis-cli)

Only mac now, download redis-cli according to your os.

cd rosedb/tools
./cli-mac -p 5200

127.0.0.1:5200> 
127.0.0.1:5200> set my_key RoseDB
OK
127.0.0.1:5200> get my_key
"RoseDB"
127.0.0.1:5200> 

Documentation

See wiki

Community

Welcome to join the Slack channel and Discussions to connect with RoseDB team members and other users.

If you are a Chinese user, you are also welcome to join our WeChat group, scan the QR code and you will be invited:

Contributing

If you are interested in contributing to rosedb, see CONTRIBUTING and how to contribute?

License

rosedb is licensed under the term of the Apache 2.0 License

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrKeyNotFound key not found
	ErrKeyNotFound = errors.New("key not found")

	// ErrLogFileNotFound log file not found
	ErrLogFileNotFound = errors.New("log file not found")

	// ErrWrongNumberOfArgs doesn't match key-value pair numbers
	ErrWrongNumberOfArgs = errors.New("wrong number of arguments")

	// ErrIntegerOverflow overflows int64 limitations
	ErrIntegerOverflow = errors.New("increment or decrement overflow")

	// ErrWrongValueType value is not a number
	ErrWrongValueType = errors.New("value is not an integer")

	// ErrWrongIndex index is out of range
	ErrWrongIndex = errors.New("index is out of range")

	// ErrGCRunning log file gc is running
	ErrGCRunning = errors.New("log file gc is running, retry later")
)
View Source
var ErrDiscardNoSpace = errors.New("not enough space can be allocated for the discard file")

ErrDiscardNoSpace no enough space for discard file.

Functions

This section is empty.

Types

type DataIndexMode

type DataIndexMode int

DataIndexMode the data index mode.

const (
	// KeyValueMemMode key and value are both in memory, read operation will be very fast in this mode.
	// Because there is no disk seek, just get value from the corresponding data structures in memory.
	// This mode is suitable for scenarios where the value are relatively small.
	KeyValueMemMode DataIndexMode = iota

	// KeyOnlyMemMode only key in memory, there is a disk seek while getting a value.
	// Because values are in log file on disk.
	KeyOnlyMemMode
)

type DataType

type DataType = int8

DataType Define the data structure type.

const (
	String DataType = iota
	List
	Hash
	Set
	ZSet
)

Five different data types, support String, List, Hash, Set, Sorted Set right now.

type IOType

type IOType int8

IOType represents different types of file io: FileIO(standard file io) and MMap(Memory Map).

const (
	// FileIO standard file io.
	FileIO IOType = iota
	// MMap Memory Map.
	MMap
)

type Options

type Options struct {
	// DBPath db path, will be created automatically if not exist.
	DBPath string

	// IndexMode mode of index, support KeyValueMemMode and KeyOnlyMemMode now.
	// Note that this mode is only for kv pairs, not List, Hash, Set, and ZSet.
	// Default value is KeyOnlyMemMode.
	IndexMode DataIndexMode

	// IoType file r/w io type, support FileIO and MMap now.
	// Default value is FileIO.
	IoType IOType

	// Sync is whether to sync writes from the OS buffer cache through to actual disk.
	// If false, and the machine crashes, then some recent writes may be lost.
	// Note that if it is just the process that crashes (and the machine does not) then no writes will be lost.
	// Default value is false.
	Sync bool

	// LogFileGCInterval a background goroutine will execute log file garbage collection periodically according to the interval.
	// It will pick the log file that meet the conditions for GC, then rewrite the valid data one by one.
	// Default value is 8 hours.
	LogFileGCInterval time.Duration

	// LogFileGCRatio if discarded data in log file exceeds this ratio, it can be picked up for compaction(garbage collection)
	// And if there are many files reached the ratio, we will pick the highest one by one.
	// The recommended ratio is 0.5, half of the file can be compacted.
	// Default value is 0.5.
	LogFileGCRatio float64

	// LogFileSizeThreshold threshold size of each log file, active log file will be closed if reach the threshold.
	// Important!!! This option must be set to the same value as the first startup.
	// Default value is 512MB.
	LogFileSizeThreshold int64

	// DiscardBufferSize a channel will be created to send the older entry size when a key updated or deleted.
	// Entry size will be saved in the discard file, recording the invalid size in a log file, and be used when log file gc is running.
	// This option represents the size of that channel.
	// If you got errors like `send discard chan fail`, you can increase this option to avoid it.
	DiscardBufferSize int
}

Options for opening a db.

func DefaultOptions

func DefaultOptions(path string) Options

DefaultOptions default options for opening a RoseDB.

type RoseDB

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

RoseDB a db instance.

func Open

func Open(opts Options) (*RoseDB, error)

Open a rosedb instance. You must call Close after using it.

func (*RoseDB) Append

func (db *RoseDB) Append(key, value []byte) error

Append appends the value at the end of the old value if key already exists. It will be similar to Set if key does not exist.

func (*RoseDB) Backup added in v1.1.1

func (db *RoseDB) Backup(path string) error

Backup copies the db directory to the given path for backup. It will create the path if it does not exist.

func (*RoseDB) Close

func (db *RoseDB) Close() error

Close db and save relative configs.

func (*RoseDB) Count added in v1.1.0

func (db *RoseDB) Count() int

Count returns the total number of keys of String.

func (*RoseDB) Decr

func (db *RoseDB) Decr(key []byte) (int64, error)

Decr decrements the number stored at key by one. If the key does not exist, it is set to 0 before performing the operation. It returns ErrWrongKeyType error if the value is not integer type. Also, it returns ErrIntegerOverflow error if the value exceeds after decrementing the value.

func (*RoseDB) DecrBy

func (db *RoseDB) DecrBy(key []byte, decr int64) (int64, error)

DecrBy decrements the number stored at key by decr. If the key doesn't exist, it is set to 0 before performing the operation. It returns ErrWrongKeyType error if the value is not integer type. Also, it returns ErrIntegerOverflow error if the value exceeds after decrementing the value.

func (*RoseDB) Delete

func (db *RoseDB) Delete(key []byte) error

Delete value at the given key.

func (*RoseDB) Get

func (db *RoseDB) Get(key []byte) ([]byte, error)

Get get the value of key. If the key does not exist the error ErrKeyNotFound is returned.

func (*RoseDB) GetDel

func (db *RoseDB) GetDel(key []byte) ([]byte, error)

GetDel gets the value of the key and deletes the key. This method is similar to Get method. It also deletes the key if it exists.

func (*RoseDB) HDel

func (db *RoseDB) HDel(key []byte, fields ...[]byte) (int, error)

HDel removes the specified fields from the hash stored at key. Specified fields that do not exist within this hash are ignored. If key does not exist, it is treated as an empty hash and this command returns false.

func (*RoseDB) HExists

func (db *RoseDB) HExists(key, field []byte) (bool, error)

HExists returns whether the field exists in the hash stored at key. If the hash contains field, it returns true. If the hash does not contain field, or key does not exist, it returns false.

func (*RoseDB) HGet

func (db *RoseDB) HGet(key, field []byte) ([]byte, error)

HGet returns the value associated with field in the hash stored at key.

func (*RoseDB) HGetAll

func (db *RoseDB) HGetAll(key []byte) ([][]byte, error)

HGetAll return all fields and values of the hash stored at key.

func (*RoseDB) HIncrBy added in v1.1.0

func (db *RoseDB) HIncrBy(key, field []byte, incr int64) (int64, error)

HIncrBy increments the number stored at field in the hash stored at key by increment. If key does not exist, a new key holding a hash is created. If field does not exist the value is set to 0 before the operation is performed. The range of values supported by HINCRBY is limited to 64bit signed integers.

func (*RoseDB) HKeys

func (db *RoseDB) HKeys(key []byte) ([][]byte, error)

HKeys returns all field names in the hash stored at key.

func (*RoseDB) HLen

func (db *RoseDB) HLen(key []byte) int

HLen returns the number of fields contained in the hash stored at key.

func (*RoseDB) HMGet added in v1.1.0

func (db *RoseDB) HMGet(key []byte, fields ...[]byte) (vals [][]byte, err error)

HMGet returns the values associated with the specified fields in the hash stored at the key. For every field that does not exist in the hash, a nil value is returned. Because non-existing keys are treated as empty hashes, running HMGET against a non-existing key will return a list of nil values.

func (*RoseDB) HScan added in v1.1.0

func (db *RoseDB) HScan(key []byte, prefix []byte, pattern string, count int) ([][]byte, error)

HScan iterates over a specified key of type Hash and finds its fields and values. Parameter prefix will match field`s prefix, and pattern is a regular expression that also matchs the field. Parameter count limits the number of keys, a nil slice will be returned if count is not a positive number. The returned values will be a mixed data of fields and values, like [field1, value1, field2, value2, etc...].

func (*RoseDB) HSet

func (db *RoseDB) HSet(key []byte, args ...[]byte) error

HSet sets field in the hash stored at key to value. If key does not exist, a new key holding a hash is created. If field already exists in the hash, it is overwritten. Return num of elements in hash of the specified key. Multiple field-value pair is accepted. Parameter order should be like "key", "field", "value", "field", "value"...

func (*RoseDB) HSetNX added in v1.1.0

func (db *RoseDB) HSetNX(key, field, value []byte) (bool, error)

HSetNX sets the given value only if the field doesn't exist. If the key doesn't exist, new hash is created. If field already exist, HSetNX doesn't have side effect.

func (*RoseDB) HStrLen

func (db *RoseDB) HStrLen(key, field []byte) int

HStrLen returns the string length of the value associated with field in the hash stored at key. If the key or the field do not exist, 0 is returned.

func (*RoseDB) HVals

func (db *RoseDB) HVals(key []byte) ([][]byte, error)

HVals return all values in the hash stored at key.

func (*RoseDB) Incr

func (db *RoseDB) Incr(key []byte) (int64, error)

Incr increments the number stored at key by one. If the key does not exist, it is set to 0 before performing the operation. It returns ErrWrongKeyType error if the value is not integer type. Also, it returns ErrIntegerOverflow error if the value exceeds after incrementing the value.

func (*RoseDB) IncrBy

func (db *RoseDB) IncrBy(key []byte, incr int64) (int64, error)

IncrBy increments the number stored at key by incr. If the key doesn't exist, it is set to 0 before performing the operation. It returns ErrWrongKeyType error if the value is not integer type. Also, it returns ErrIntegerOverflow error if the value exceeds after incrementing the value.

func (*RoseDB) LIndex added in v1.1.0

func (db *RoseDB) LIndex(key []byte, index int) ([]byte, error)

LIndex returns the element at index in the list stored at key. If index is out of range, it returns nil.

func (*RoseDB) LLen

func (db *RoseDB) LLen(key []byte) int

LLen returns the length of the list stored at key. If key does not exist, it is interpreted as an empty list and 0 is returned.

func (*RoseDB) LMove added in v1.1.0

func (db *RoseDB) LMove(srcKey, dstKey []byte, srcIsLeft, dstIsLeft bool) ([]byte, error)

LMove atomically returns and removes the first/last element of the list stored at source, and pushes the element at the first/last element of the list stored at destination.

func (*RoseDB) LPop

func (db *RoseDB) LPop(key []byte) ([]byte, error)

LPop removes and returns the first elements of the list stored at key.

func (*RoseDB) LPush

func (db *RoseDB) LPush(key []byte, values ...[]byte) error

LPush insert all the specified values at the head of the list stored at key. If key does not exist, it is created as empty list before performing the push operations.

func (*RoseDB) LPushX added in v1.1.0

func (db *RoseDB) LPushX(key []byte, values ...[]byte) error

LPushX insert specified values at the head of the list stored at key, only if key already exists and holds a list. In contrary to LPUSH, no operation will be performed when key does not yet exist.

func (*RoseDB) LRange added in v1.1.0

func (db *RoseDB) LRange(key []byte, start, end int) (values [][]byte, err error)

LRange returns the specified elements of the list stored at key. The offsets start and stop are zero-based indexes, with 0 being the first element of the list (the head of the list), 1 being the next element and so on. These offsets can also be negative numbers indicating offsets starting at the end of the list. For example, -1 is the last element of the list, -2 the penultimate, and so on. If start is larger than the end of the list, an empty list is returned. If stop is larger than the actual end of the list, Redis will treat it like the last element of the list.

func (*RoseDB) LSet added in v1.1.0

func (db *RoseDB) LSet(key []byte, index int, value []byte) error

LSet Sets the list element at index to element.

func (*RoseDB) MGet

func (db *RoseDB) MGet(keys [][]byte) ([][]byte, error)

MGet get the values of all specified keys. If the key that does not hold a string value or does not exist, nil is returned.

func (*RoseDB) MSet

func (db *RoseDB) MSet(args ...[]byte) error

MSet is multiple set command. Parameter order should be like "key", "value", "key", "value", ...

func (*RoseDB) MSetNX

func (db *RoseDB) MSetNX(args ...[]byte) error

MSetNX sets given keys to their respective values. MSetNX will not perform any operation at all even if just a single key already exists.

func (*RoseDB) RPop

func (db *RoseDB) RPop(key []byte) ([]byte, error)

RPop Removes and returns the last elements of the list stored at key.

func (*RoseDB) RPush

func (db *RoseDB) RPush(key []byte, values ...[]byte) error

RPush insert all the specified values at the tail of the list stored at key. If key does not exist, it is created as empty list before performing the push operation.

func (*RoseDB) RPushX added in v1.1.0

func (db *RoseDB) RPushX(key []byte, values ...[]byte) error

RPushX insert specified values at the tail of the list stored at key, only if key already exists and holds a list. In contrary to RPUSH, no operation will be performed when key does not yet exist.

func (*RoseDB) RunLogFileGC

func (db *RoseDB) RunLogFileGC(dataType DataType, fid int, gcRatio float64) error

RunLogFileGC run log file garbage collection manually.

func (*RoseDB) SAdd

func (db *RoseDB) SAdd(key []byte, members ...[]byte) error

SAdd add the specified members to the set stored at key. Specified members that are already a member of this set are ignored. If key does not exist, a new set is created before adding the specified members.

func (*RoseDB) SCard

func (db *RoseDB) SCard(key []byte) int

SCard returns the set cardinality (number of elements) of the set stored at key.

func (*RoseDB) SDiff

func (db *RoseDB) SDiff(keys ...[]byte) ([][]byte, error)

SDiff returns the members of the set difference between the first set and all the successive sets. Returns error if no key is passed as a parameter.

func (*RoseDB) SIsMember

func (db *RoseDB) SIsMember(key, member []byte) bool

SIsMember returns if member is a member of the set stored at key.

func (*RoseDB) SMembers

func (db *RoseDB) SMembers(key []byte) ([][]byte, error)

SMembers returns all the members of the set value stored at key.

func (*RoseDB) SPop

func (db *RoseDB) SPop(key []byte, count uint) ([][]byte, error)

SPop removes and returns one or more random members from the set value store at key.

func (*RoseDB) SRem

func (db *RoseDB) SRem(key []byte, members ...[]byte) error

SRem remove the specified members from the set stored at key. Specified members that are not a member of this set are ignored. If key does not exist, it is treated as an empty set and this command returns 0.

func (*RoseDB) SUnion

func (db *RoseDB) SUnion(keys ...[]byte) ([][]byte, error)

SUnion returns the members of the set resulting from the union of all the given sets.

func (*RoseDB) Scan added in v1.1.0

func (db *RoseDB) Scan(prefix []byte, pattern string, count int) ([][]byte, error)

Scan iterates over all keys of type String and finds its value. Parameter prefix will match key`s prefix, and pattern is a regular expression that also matchs the key. Parameter count limits the number of keys, a nil slice will be returned if count is not a positive number. The returned values will be a mixed data of keys and values, like [key1, value1, key2, value2, etc...].

func (*RoseDB) Set

func (db *RoseDB) Set(key, value []byte) error

Set set key to hold the string value. If key already holds a value, it is overwritten. Any previous time to live associated with the key is discarded on successful Set operation.

func (*RoseDB) SetEX

func (db *RoseDB) SetEX(key, value []byte, duration time.Duration) error

SetEX set key to hold the string value and set key to timeout after the given duration.

func (*RoseDB) SetNX

func (db *RoseDB) SetNX(key, value []byte) error

SetNX sets the key-value pair if it is not exist. It returns nil if the key already exists.

func (*RoseDB) StrLen

func (db *RoseDB) StrLen(key []byte) int

StrLen returns the length of the string value stored at key. If the key doesn't exist, it returns 0.

func (*RoseDB) Sync

func (db *RoseDB) Sync() error

Sync persist the db files to stable storage.

func (*RoseDB) ZAdd

func (db *RoseDB) ZAdd(key []byte, score float64, member []byte) error

ZAdd adds the specified member with the specified score to the sorted set stored at key.

func (*RoseDB) ZCard

func (db *RoseDB) ZCard(key []byte) int

ZCard returns the sorted set cardinality (number of elements) of the sorted set stored at key.

func (*RoseDB) ZRange

func (db *RoseDB) ZRange(key []byte, start, stop int) ([][]byte, error)

ZRange returns the specified range of elements in the sorted set stored at key.

func (*RoseDB) ZRank added in v1.1.1

func (db *RoseDB) ZRank(key []byte, member []byte) (ok bool, rank int)

ZRank returns the rank of member in the sorted set stored at key, with the scores ordered from low to high. The rank (or index) is 0-based, which means that the member with the lowest score has rank 0.

func (*RoseDB) ZRem

func (db *RoseDB) ZRem(key, member []byte) error

ZRem removes the specified members from the sorted set stored at key. Non existing members are ignored. An error is returned when key exists and does not hold a sorted set.

func (*RoseDB) ZRevRange added in v1.1.1

func (db *RoseDB) ZRevRange(key []byte, start, stop int) ([][]byte, error)

ZRevRange returns the specified range of elements in the sorted set stored at key. The elements are considered to be ordered from the highest to the lowest score.

func (*RoseDB) ZRevRank added in v1.1.1

func (db *RoseDB) ZRevRank(key []byte, member []byte) (ok bool, rank int)

ZRevRank returns the rank of member in the sorted set stored at key, with the scores ordered from high to low. The rank (or index) is 0-based, which means that the member with the highest score has rank 0.

func (*RoseDB) ZScore

func (db *RoseDB) ZScore(key, member []byte) (ok bool, score float64)

ZScore returns the score of member in the sorted set at key.

Directories

Path Synopsis
ds
art
examples

Jump to

Keyboard shortcuts

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