spruce

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

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

Go to latest
Published: May 22, 2020 License: Apache-2.0 Imports: 26 Imported by: 1

README

Spruce

High Performance Distributed Key-value Database

We use many kinds of high performance algorithms

  • High performance hash storage algorithm
    • Blizzard Hash
    • Fast Search Algorithms
  • Consistent hash slot
  • message queue (ring architecture)

Usage
create single server
  1. create app.go
  2. write it like this
    conf := make([]spruce.DNode, 1)
    conf[0] = spruce.DNode{
            Name:     "master",
            Ip:       "127.0.0.1:6999",
            Weigh:    2,
            Password: "",
    }
    spruce.StartSpruceDistributed(spruce.Config{
            ConfigType:    spruce.MEMORY,
            DCSConfigFile: "",
            DNode:         conf,
            Addr:          ":6998", 
                    MaxSlot:        4096
            NowIP:         "127.0.0.1:6999",
            KeepAlive:     false,
            IsBackup:      false,
    })
    
  3. connect it (two)
    1. gRpc

      service Operation {
        rpc Get(OperationArgs) returns (Result){}
        rpc Set(OperationArgs) returns (SetResult){}
        rpc Delete(OperationArgs) returns (DeleteResult){}
      }
      message OperationArgs{
       bytes Key =1;
       bytes Value =2;
       int64 Expiration=3;
      }
      message Result{
       bytes Value=1;
      }
      
    2. achieve protocol

Documentation

Index

Constants

View Source
const (
	FILE = iota
	MEMORY
)

Variables

View Source
var CRY []uint
View Source
var File_remote_operation_proto protoreflect.FileDescriptor

Functions

func AddSlot

func AddSlot()

增加新的插槽

func ComplementHex

func ComplementHex(s string, x int) string

func CreateLocalPWD

func CreateLocalPWD() []byte

func CreateNewId

func CreateNewId(length int) []byte

func CreateUUID

func CreateUUID(length int, xtr []byte, self []byte) []byte

func Decrypt

func Decrypt(s []byte) []byte

func DeleteRpc

func DeleteRpc(args *OperationArgs, address string) []byte

func DropSlot

func DropSlot()

删除新的插槽

func EchoNoKeepAlive

func EchoNoKeepAlive(c net.Conn, slot *Slot)

func Encrypt

func Encrypt(string2 []byte) []byte

func EntryDelete

func EntryDelete(key []byte) []byte

func EntryGet

func EntryGet(key []byte) []byte

func EntryHashDelete

func EntryHashDelete(key []byte) []byte

func EntryHashGet

func EntryHashGet(key []byte) []byte

func EntryHashSet

func EntryHashSet(key, value []byte, ti int) []byte

func EntrySet

func EntrySet(key, value []byte, ti int) []byte

func Equal

func Equal(one []byte, two []byte) bool

func FindAll

func FindAll(n []*node) []byte

func FindString

func FindString(v []byte, p []byte) interface{}

func GetData

func GetData(a net.Conn) []byte

func GetRpc

func GetRpc(args *OperationArgs, address string) []byte

func MD5

func MD5(b []byte) []byte

func NewClockTask

func NewClockTask(e int64) *clockTask

func NewDial

func NewDial() *ap.Pool

func NoRpcServer

func NoRpcServer(config *Config)

func ParsingExpirationDate

func ParsingExpirationDate(tm interface{}) interface{}

func RegisterOperationServer

func RegisterOperationServer(s *grpc.Server, srv OperationServer)

func RegisterWatcherServer

func RegisterWatcherServer(s *grpc.Server, srv WatcherServer)

func ReplaceTabCharacter

func ReplaceTabCharacter(in []byte) []byte

replace tab character function \n -> 0 \r -> 1 \t -> 2

func ReplaceTabCharacterToNormal

func ReplaceTabCharacterToNormal(in []byte) []byte

func RpcStart

func RpcStart(config Config)

func SendDeleteMessage

func SendDeleteMessage()

func SendGetMessage

func SendGetMessage(key []byte) []byte

func SendSetMessage

func SendSetMessage(lang []byte, ti []byte) []byte

func SetRpc

func SetRpc(args *OperationArgs, address string) int

func SplitKeyValue

func SplitKeyValue(b []byte) ([]byte, []byte)

传输协议的制定 0-> 表示操作码 1 状态码 2-3 过期时间 4-10 暂时没想到 11-N 语句 操作码 0 delete | 1 set | 2 get | 3 status 状态码 0 操作失败 1操作成功 2 操作中 set key 和 value 以0作为分割 新加 2-3 存放过期时间 最大 0xFFFF or 65535

func SplitString

func SplitString(str []byte, p []byte) [][]byte
func CheckConfig(nw interface{}, deft interface{}) {
	switch reflect.TypeOf(nw).Kind() {
	case reflect.Struct:
		t := reflect.TypeOf(nw).Elem()
		v := reflect.ValueOf(nw).Elem()
		for i := 0; i < t.NumField(); i++ {
			n := v.Field(i)
			switch n.Kind() {
			case reflect.String:
				if n.IsZero() {
					n.SetString(reflect.ValueOf(deft).Field(i).String())
				}
			case reflect.Int:
				if n.IsZero() {
					n.SetInt(reflect.ValueOf(deft).Field(i).Int())
				}
			case reflect.Int64:
				if n.IsZero() {
					n.SetInt(reflect.ValueOf(deft).Field(i).Int())
				}
			case reflect.Bool:
				if n.IsZero() {
					n.SetBool(reflect.ValueOf(deft).Field(i).Bool())
				}
			case reflect.Float64:
				if n.IsZero() {
					n.SetFloat(reflect.ValueOf(deft).Field(i).Float())
				}
			}
		}
	case reflect.Ptr:
		n := reflect.ValueOf(nw).Elem()
		switch n.Kind() {
		case reflect.Int:
			if n.IsZero() && n.CanSet() {
				n.SetInt(reflect.ValueOf(deft).Int())
			}
		case reflect.String:
			if n.IsZero() && n.CanSet() {
				n.SetString(reflect.ValueOf(deft).String())
			}
		case reflect.Struct:
			t := reflect.TypeOf(nw).Elem()
			v := reflect.ValueOf(nw).Elem()
			for i := 0; i < t.NumField(); i++ {
				n := v.Field(i)
				switch n.Kind() {
				case reflect.String:
					if n.IsZero() {
						n.SetString(reflect.ValueOf(deft).Field(i).String())
					}
				case reflect.Int:
					if n.IsZero() {
						n.SetInt(reflect.ValueOf(deft).Field(i).Int())
					}
				case reflect.Int64:
					if n.IsZero() {
						n.SetInt(reflect.ValueOf(deft).Field(i).Int())
					}
				case reflect.Bool:
					if n.IsZero() {
						n.SetBool(reflect.ValueOf(deft).Field(i).Bool())
					}
				case reflect.Float64:
					if n.IsZero() {
						n.SetFloat(reflect.ValueOf(deft).Field(i).Float())
					}
				}
			}
		}

	}
}

func ToBytes

func ToBytes(x interface{}) ([]byte, error)

set all thing to bytes

Types

type Config

type Config struct {
	ConfigType      int     `配置方式`
	DCSConfigFile   string  `分布式的配置文件路径`
	DNode           []DNode `采用内存的方式部署`
	Addr            string  `跑在哪个端口上`
	NowIP           string  `当前服务器运行的IP地址 暂时必须`
	KeepAlive       bool
	IsBackup        bool `自动备份`
	ConnChanBufSize int  `连接信道缓冲区大小`
	ConnChanMaxSize int  `最大连接数`
	MaxSlot         int  `最大hash槽数量`
}

type DNode

type DNode struct {
	Name     string `yaml:"name"`
	Ip       string `yaml:"ip"`
	Weigh    int    `yaml:"weigh"`
	Password string `yaml:"password"`
}
type DCSConfig struct {
	Name     string `json:"name"`
	Ip       string `json:"ip"`
	Weigh    int    `json:"weigh"`
	Password string `json:"password"`
}

func ParseConfigFile

func ParseConfigFile(path string) []DNode

type DeleteResult

type DeleteResult struct {
	Value []byte `protobuf:"bytes,1,opt,name=Value,proto3" json:"Value,omitempty"`
	// contains filtered or unexported fields
}

func (*DeleteResult) Descriptor deprecated

func (*DeleteResult) Descriptor() ([]byte, []int)

Deprecated: Use DeleteResult.ProtoReflect.Descriptor instead.

func (*DeleteResult) GetValue

func (x *DeleteResult) GetValue() []byte

func (*DeleteResult) ProtoMessage

func (*DeleteResult) ProtoMessage()

func (*DeleteResult) ProtoReflect

func (x *DeleteResult) ProtoReflect() protoreflect.Message

func (*DeleteResult) Reset

func (x *DeleteResult) Reset()

func (*DeleteResult) String

func (x *DeleteResult) String() string

type FileConfig

type FileConfig struct {
	Config []DNode `yaml:"config"`
}

type Hash

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

func CreateHash

func CreateHash(n int) *Hash

func (*Hash) Clone

func (h *Hash) Clone() int

func (*Hash) Delete

func (h *Hash) Delete(key []byte) []byte

func (*Hash) Get

func (h *Hash) Get(key []byte) interface{}

func (*Hash) GetAll

func (h *Hash) GetAll() []interface{}

func (*Hash) GetHashPos

func (h *Hash) GetHashPos(str []byte) uint

func (*Hash) Load

func (h *Hash) Load()

这个直接读取是程序启动时会默认执行的

func (*Hash) Reload

func (h *Hash) Reload()

重新从文件中读取到内存中来

func (*Hash) Set

func (h *Hash) Set(key []byte, value interface{}, expTime int64) int

func (*Hash) Storage

func (h *Hash) Storage()

type Operation

type Operation struct {
	UnimplementedOperationServer
}

func (*Operation) Delete

func (o *Operation) Delete(ctx context.Context, in *OperationArgs) (*DeleteResult, error)

func (*Operation) Get

func (o *Operation) Get(ctx context.Context, in *OperationArgs) (*Result, error)

func (*Operation) Set

func (o *Operation) Set(ctx context.Context, in *OperationArgs) (*SetResult, error)

type OperationArgs

type OperationArgs struct {
	Key        []byte `protobuf:"bytes,1,opt,name=Key,proto3" json:"Key,omitempty"`
	Value      []byte `protobuf:"bytes,2,opt,name=Value,proto3" json:"Value,omitempty"`
	Expiration int64  `protobuf:"varint,3,opt,name=Expiration,proto3" json:"Expiration,omitempty"`
	// contains filtered or unexported fields
}

func (*OperationArgs) Descriptor deprecated

func (*OperationArgs) Descriptor() ([]byte, []int)

Deprecated: Use OperationArgs.ProtoReflect.Descriptor instead.

func (*OperationArgs) GetExpiration

func (x *OperationArgs) GetExpiration() int64

func (*OperationArgs) GetKey

func (x *OperationArgs) GetKey() []byte

func (*OperationArgs) GetValue

func (x *OperationArgs) GetValue() []byte

func (*OperationArgs) ProtoMessage

func (*OperationArgs) ProtoMessage()

func (*OperationArgs) ProtoReflect

func (x *OperationArgs) ProtoReflect() protoreflect.Message

func (*OperationArgs) Reset

func (x *OperationArgs) Reset()

func (*OperationArgs) String

func (x *OperationArgs) String() string

type OperationClient

type OperationClient interface {
	Get(ctx context.Context, in *OperationArgs, opts ...grpc.CallOption) (*Result, error)
	Set(ctx context.Context, in *OperationArgs, opts ...grpc.CallOption) (*SetResult, error)
	Delete(ctx context.Context, in *OperationArgs, opts ...grpc.CallOption) (*DeleteResult, error)
}

OperationClient is the client API for Operation service.

For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.

func NewOperationClient

func NewOperationClient(cc grpc.ClientConnInterface) OperationClient

type OperationServer

type OperationServer interface {
	Get(context.Context, *OperationArgs) (*Result, error)
	Set(context.Context, *OperationArgs) (*SetResult, error)
	Delete(context.Context, *OperationArgs) (*DeleteResult, error)
}

OperationServer is the server API for Operation service.

type Result

type Result struct {
	Value []byte `protobuf:"bytes,1,opt,name=Value,proto3" json:"Value,omitempty"`
	// contains filtered or unexported fields
}

func (*Result) Descriptor deprecated

func (*Result) Descriptor() ([]byte, []int)

Deprecated: Use Result.ProtoReflect.Descriptor instead.

func (*Result) GetValue

func (x *Result) GetValue() []byte

func (*Result) ProtoMessage

func (*Result) ProtoMessage()

func (*Result) ProtoReflect

func (x *Result) ProtoReflect() protoreflect.Message

func (*Result) Reset

func (x *Result) Reset()

func (*Result) String

func (x *Result) String() string

type SetResult

type SetResult struct {
	Position int64 `protobuf:"varint,1,opt,name=Position,proto3" json:"Position,omitempty"`
	// contains filtered or unexported fields
}

func (*SetResult) Descriptor deprecated

func (*SetResult) Descriptor() ([]byte, []int)

Deprecated: Use SetResult.ProtoReflect.Descriptor instead.

func (*SetResult) GetPosition

func (x *SetResult) GetPosition() int64

func (*SetResult) ProtoMessage

func (*SetResult) ProtoMessage()

func (*SetResult) ProtoReflect

func (x *SetResult) ProtoReflect() protoreflect.Message

func (*SetResult) Reset

func (x *SetResult) Reset()

func (*SetResult) String

func (x *SetResult) String() string

type Slot

type Slot struct {
	Count int
	Face  DNode
	Other []DNode

	All []DNode
	Mux sync.Mutex
	// contains filtered or unexported fields
}
var (
	AllSlot   []DNode
	EntrySlot *Slot
)

func StartSpruceDistributed

func StartSpruceDistributed(config Config) *Slot

TODO Start

func (*Slot) Delete

func (s *Slot) Delete(lang []byte) []byte

func (*Slot) Get

func (s *Slot) Get(lang []byte) interface{}

func (*Slot) Position

func (s *Slot) Position(key []byte) int

func (*Slot) ResetSlot

func (s *Slot) ResetSlot(n DNode)

func (*Slot) Set

func (s *Slot) Set(lang []byte) []byte

func (*Slot) Storage

func (s *Slot) Storage()

type UnimplementedOperationServer

type UnimplementedOperationServer struct {
}

UnimplementedOperationServer can be embedded to have forward compatible implementations.

func (*UnimplementedOperationServer) Delete

func (*UnimplementedOperationServer) Get

func (*UnimplementedOperationServer) Set

type UnimplementedWatcherServer

type UnimplementedWatcherServer struct {
}

UnimplementedWatcherServer can be embedded to have forward compatible implementations.

func (*UnimplementedWatcherServer) Pong

type Watcher

type Watcher struct {
	UnimplementedWatcherServer
}

func (*Watcher) Dead

func (w *Watcher) Dead(args *WatcherData, result *int8) error

func (*Watcher) Do

func (w *Watcher) Do(args *WatcherData, result *int8) error

func (*Watcher) Pong

func (w *Watcher) Pong(ctx context.Context, in *WatcherData) (*WatcherResult, error)

func (w *Watcher) Ping(ip string) int8 {

}

type WatcherClient

type WatcherClient interface {
	Pong(ctx context.Context, in *WatcherData, opts ...grpc.CallOption) (*WatcherResult, error)
}

WatcherClient is the client API for Watcher service.

For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.

func NewWatcherClient

func NewWatcherClient(cc grpc.ClientConnInterface) WatcherClient

type WatcherData

type WatcherData struct {
	Time int64 `protobuf:"varint,1,opt,name=Time,proto3" json:"Time,omitempty"`
	// contains filtered or unexported fields
}

func (*WatcherData) Descriptor deprecated

func (*WatcherData) Descriptor() ([]byte, []int)

Deprecated: Use WatcherData.ProtoReflect.Descriptor instead.

func (*WatcherData) GetTime

func (x *WatcherData) GetTime() int64

func (*WatcherData) ProtoMessage

func (*WatcherData) ProtoMessage()

func (*WatcherData) ProtoReflect

func (x *WatcherData) ProtoReflect() protoreflect.Message

func (*WatcherData) Reset

func (x *WatcherData) Reset()

func (*WatcherData) String

func (x *WatcherData) String() string

type WatcherResult

type WatcherResult struct {
	Res int64 `protobuf:"varint,1,opt,name=Res,proto3" json:"Res,omitempty"`
	// contains filtered or unexported fields
}

func (*WatcherResult) Descriptor deprecated

func (*WatcherResult) Descriptor() ([]byte, []int)

Deprecated: Use WatcherResult.ProtoReflect.Descriptor instead.

func (*WatcherResult) GetRes

func (x *WatcherResult) GetRes() int64

func (*WatcherResult) ProtoMessage

func (*WatcherResult) ProtoMessage()

func (*WatcherResult) ProtoReflect

func (x *WatcherResult) ProtoReflect() protoreflect.Message

func (*WatcherResult) Reset

func (x *WatcherResult) Reset()

func (*WatcherResult) String

func (x *WatcherResult) String() string

type WatcherServer

type WatcherServer interface {
	Pong(context.Context, *WatcherData) (*WatcherResult, error)
}

WatcherServer is the server API for Watcher service.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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