Skip to content

Commit

Permalink
stash
Browse files Browse the repository at this point in the history
  • Loading branch information
xgzlucario committed Nov 4, 2023
1 parent 4787df5 commit 4eff604
Show file tree
Hide file tree
Showing 11 changed files with 360 additions and 188 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ English | [中文](README_ZN.md) | [doc](https://www.yuque.com/1ucario/devdoc/nt

## 📃Introduction

This is Rotom, a high-performance Key-Value memory database written in Go. It has built-in multiple commonly used data types, supports persistent storage, and can be used in Golang as an imported package or as a server (the client part is under development and does not support all commands yet).
This is Rotom, a high-performance Key-Value memory database written in Go. It has built-in multiple commonly used data types, supports persistent storage, and can be used in Golang as an imported package or as a server.

Features:

1. Built-in data types like String, Map, Set, List, ZSet, BitMap, etc., supporting more than 20 commands.
1. Built-in data types like String, Map, Set, List, ZSet, BitMap, etc., supporting more than 30 commands.
2. Microsecond-level expiration time (ttl).
3. Based on [GigaCache](https://github.com/xgzlucario/GigaCache), it can avoid GC overhead and have stronger multithreaded performance.
4. RDB + AOF hybrid persistence strategy.
Expand Down
4 changes: 2 additions & 2 deletions README_ZN.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

## 📃介绍

这里是 Rotom,一个 Go 编写高性能 Key-Value 内存数据库,内置多种常用数据类型,支持持久化存储,可以在 Golang 中以包引入的方式使用,也可以作为服务器使用(客户端部分正在开发中,暂不支持所有命令)
这里是 Rotom,一个 Go 编写高性能 Key-Value 内存数据库,内置多种常用数据类型,支持持久化存储,可以在 Golang 中以包引入的方式使用,也可以作为网络服务器使用

目前支持的功能:

1. 内置数据类型 String,Map,Set,List,ZSet,BitMap 等,支持 20 多种命令
1. 内置数据类型 String,Map,Set,List,ZSet,BitMap 等,支持 30 多种命令
2. 微秒级别的过期时间(ttl)
3. 底层基于 [GigaCache](https://github.com/xgzlucario/GigaCache),能规避GC开销,多线程性能更强
4. 基于 RDB + AOF 混合的持久化策略
Expand Down
24 changes: 24 additions & 0 deletions base/strconv.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package base

import (
"encoding/binary"

"github.com/bytedance/sonic"
)

Expand All @@ -9,6 +11,7 @@ const (
RADIX = VALID - 1
)

// FormatInt
func FormatInt[T Integer](n T) []byte {
if n < 0 {
panic("negative number")
Expand All @@ -26,6 +29,7 @@ func FormatInt[T Integer](n T) []byte {
return sb
}

// ParseInt
func ParseInt[T Integer](b []byte) T {
var n T
for i := len(b) - 1; i >= 0; i-- {
Expand All @@ -34,13 +38,33 @@ func ParseInt[T Integer](b []byte) T {
return n
}

// FormatStrSlice
func FormatStrSlice(ss []string) []byte {
src, _ := sonic.Marshal(ss)
return src
}

// ParseStrSlice
func ParseStrSlice(b []byte) []string {
var ss []string
sonic.Unmarshal(b, &ss)
return ss
}

// FormatU32Slice
func FormatU32Slice(ss []uint32) []byte {
bytes := make([]byte, 0, len(ss)*4)
for _, s := range ss {
bytes = binary.NativeEndian.AppendUint32(bytes, s)
}
return bytes
}

// ParseU32Slice
func ParseU32Slice(b []byte) []uint32 {
ss := make([]uint32, 0, len(b)/4)
for i := 0; i < len(b); i += 4 {
ss = append(ss, binary.NativeEndian.Uint32(b[i:]))
}
return ss
}
26 changes: 19 additions & 7 deletions base/types.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package base

import (
"bytes"

"golang.org/x/exp/constraints"
)

type Ordered constraints.Ordered

type Number interface {
Integer | float64 | float32
}

type Integer interface {
~int | ~int32 | ~int64 | ~uint | ~uint32 | ~uint64
}
Expand All @@ -31,15 +29,29 @@ type Gober interface {

// Writer
type Writer interface {
Write([]byte) (int, error)
WriteByte(byte) error
Write([]byte) error
}

// CWriter
type CWriter struct {
*bytes.Buffer
}

func (w *CWriter) Write(b []byte) error {
_, err := w.Buffer.Write(b)
return err
}

func (w *CWriter) WriteByte(b byte) error {
return w.Buffer.WriteByte(b)
}

// NullWriter
type NullWriter struct{}

func (NullWriter) Write([]byte) (int, error) {
return 0, nil
func (NullWriter) Write([]byte) error {
return nil
}

func (NullWriter) WriteByte(byte) error {
Expand Down
65 changes: 61 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rotom
import (
"errors"
"net"
"strconv"
"time"

"github.com/bytedance/sonic"
Expand All @@ -24,6 +25,11 @@ func NewClient(addr string) (c *Client, err error) {
return
}

// Ping
func (c *Client) Ping() ([]byte, error) {
return c.do(NewCodec(OpPing))
}

// Set
func (c *Client) Set(key string, val []byte) error {
return c.SetTx(key, val, noTTL)
Expand All @@ -39,13 +45,22 @@ func (c *Client) SetTx(key string, val []byte, ts int64) error {
return c.doNoRes(NewCodec(OpSetTx).Type(TypeString).Str(key).Int(ts / timeCarry).Bytes(val))
}

// Incr
func (c *Client) Incr(key string, val float64) (float64, error) {
args, err := c.do(NewCodec(OpIncr).Str(key).Float(val))
if err != nil {
return 0, err
}
return strconv.ParseFloat(*b2s(args), 64)
}

// Remove
func (c *Client) Remove(key string) (bool, error) {
args, err := c.do(NewCodec(OpRemove).Str(key))
func (c *Client) Remove(keys ...string) (int, error) {
args, err := c.do(NewCodec(OpRemove).StrSlice(keys))
if err != nil {
return false, err
return 0, err
}
return args[0] == _true, nil
return base.ParseInt[int](args), nil
}

// Rename
Expand Down Expand Up @@ -124,6 +139,15 @@ func (c *Client) SRemove(key, item string) error {
return c.doNoRes(NewCodec(OpSRemove).Str(key).Str(item))
}

// SPop
func (c *Client) SPop(key string) (string, error) {
args, err := c.do(NewCodec(OpSPop).Str(key))
if err != nil {
return "", err
}
return string(args), nil
}

// SHas
func (c *Client) SHas(key, item string) (bool, error) {
args, err := c.do(NewCodec(OpSHas).Str(key).Str(item))
Expand Down Expand Up @@ -185,6 +209,39 @@ func (c *Client) BitFlip(key string, offset uint32) error {
return c.doNoRes(NewCodec(OpBitFlip).Str(key).Uint(offset))
}

// BitOr
func (c *Client) BitOr(dstKey string, srcKeys ...string) error {
return c.doNoRes(NewCodec(OpBitOr).Str(dstKey).StrSlice(srcKeys))
}

// BitAnd
func (c *Client) BitAnd(dstKey string, srcKeys ...string) error {
return c.doNoRes(NewCodec(OpBitAnd).Str(dstKey).StrSlice(srcKeys))
}

// BitXor
func (c *Client) BitXor(dstKey string, srcKeys ...string) error {
return c.doNoRes(NewCodec(OpBitXor).Str(dstKey).StrSlice(srcKeys))
}

// BitCount
func (c *Client) BitCount(key string) (uint64, error) {
args, err := c.do(NewCodec(OpBitCount).Str(key))
if err != nil {
return 0, err
}
return base.ParseInt[uint64](args), nil
}

// BitArray
func (c *Client) BitArray(key string) ([]uint32, error) {
res, err := c.do(NewCodec(OpBitArray).Str(key))
if err != nil {
return nil, err
}
return base.ParseU32Slice(res), nil
}

// Close
func (c *Client) Close() error {
return c.c.Close()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
)

require (
github.com/bits-and-blooms/bitset v1.10.0 // indirect
github.com/bits-and-blooms/bitset v1.11.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
github.com/RoaringBitmap/roaring v1.6.0 h1:dc7kRiroETgJcHhWX6BerXkZz2b3JgLGg9nTURJL/og=
github.com/RoaringBitmap/roaring v1.6.0/go.mod h1:plvDsJQpxOC5bw8LRteu/MLWHsHez/3y6cubLI4/1yE=
github.com/bits-and-blooms/bitset v1.2.0/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edYb8uY+O0FJTyyDA=
github.com/bits-and-blooms/bitset v1.10.0 h1:ePXTeiPEazB5+opbv5fr8umg2R/1NlzgDsyepwsSr88=
github.com/bits-and-blooms/bitset v1.10.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8=
github.com/bits-and-blooms/bitset v1.11.0 h1:RMyy2mBBShArUAhfVRZJ2xyBO58KCBCtZFShw3umo6k=
github.com/bits-and-blooms/bitset v1.11.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8=
github.com/brianvoe/gofakeit/v6 v6.23.2 h1:lVde18uhad5wII/f5RMVFLtdQNE0HaGFuBUXmYKk8i8=
github.com/brianvoe/gofakeit/v6 v6.23.2/go.mod h1:Ow6qC71xtwm79anlwKRlWZW6zVq9D2XHE4QSSMP/rU8=
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
Expand Down
Loading

0 comments on commit 4eff604

Please sign in to comment.