From dc9b3abe9e595594b2749c1ab5e7ebb3b3ae49de Mon Sep 17 00:00:00 2001 From: huangsimin Date: Thu, 5 Mar 2020 18:10:40 +0800 Subject: [PATCH] todo: create table --- api.go | 6 ++-- proto/api.proto | 19 ++++++++++--- proto/test.proto | 7 ++--- rocksdb.go | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 11 deletions(-) create mode 100644 rocksdb.go diff --git a/api.go b/api.go index f088605..bd1670f 100644 --- a/api.go +++ b/api.go @@ -14,12 +14,12 @@ type server struct{} // SayHello implements func (s *server) SayHello(ctx context.Context, in *goproto.Request) (*goproto.Reply, error) { - return &goproto.Reply{Msg: "Hello " + in.GetWhat()}, nil + return &goproto.Reply{Msg: "Hello " + in.What, Status: 1, Content: []string{"dasasdasdas"}}, nil } // SayHello implements -func (s *server) CreateTable(ctx context.Context, in *goproto.TableRequest) (*goproto.TableReply, error) { - return &goproto.TableReply{Msg: "Hello " + in.Name}, nil +func (s *server) CreateTable(ctx context.Context, in *goproto.TableRequest) (*goproto.Reply, error) { + return &goproto.Reply{Msg: "Create Table " + in.Name, Status: 1, Content: []string{"dasasdasdas"}}, nil } func createTestServer() { diff --git a/proto/api.proto b/proto/api.proto index c4d3a10..b27f4a1 100644 --- a/proto/api.proto +++ b/proto/api.proto @@ -4,13 +4,24 @@ package goproto; // 服务端定义 service EasyData { // 服务端返馈信息方法 - rpc CreateTable(TableRequest) returns (TableReply) {} + rpc CreateTable(TableRequest) returns (Reply) {} } // 包含用户名的请求信息 + +message Field { + string name = 1; + string type = 2; + int64 size = 3; +} + message TableRequest { - string name = 1; + string name = 1; + repeated Field field = 2; + } // 服务端响应信息 -message TableReply { - string msg = 1; +message Reply { + int64 status = 1; + string msg = 2; + repeated string content = 3; } diff --git a/proto/test.proto b/proto/test.proto index 1893acb..8f978da 100644 --- a/proto/test.proto +++ b/proto/test.proto @@ -1,6 +1,9 @@ syntax = "proto3"; package goproto; + +import "api.proto"; + // 服务端定义 service Test { // 服务端返馈信息方法 @@ -10,7 +13,3 @@ service Test { message Request { string what = 1; } -// 服务端响应信息 -message Reply { - string msg = 1; -} diff --git a/rocksdb.go b/rocksdb.go new file mode 100644 index 0000000..8824225 --- /dev/null +++ b/rocksdb.go @@ -0,0 +1,73 @@ +package main + +import ( + "fmt" + "log" + "os" + "time" + + "github.com/tecbot/gorocksdb" +) + +func OpenLog() { + f, err := os.OpenFile("./rocksdb.log", os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0660) + if err != nil { + panic(err) + } + log.SetOutput(f) +} + +func init() { + OpenLog() +} + +func OpenDataBase() (*gorocksdb.DB, []*gorocksdb.ColumnFamilyHandle) { + + bbto := gorocksdb.NewDefaultBlockBasedTableOptions() + + bbto.SetBlockCache(gorocksdb.NewLRUCache(3 << 30)) + bbto.SetCacheIndexAndFilterBlocksWithHighPriority(true) + + opts := gorocksdb.NewDefaultOptions() + + bbto.SetFilterPolicy(gorocksdb.NewBloomFilter(16)) + + opts.SetBlockBasedTableFactory(bbto) + opts.SetCreateIfMissing(true) + opts.SetCreateIfMissingColumnFamilies(true) + opts.SetCompression(gorocksdb.LZ4Compression) + + year, month, day := time.Now().Date() + + timeCFStr := fmt.Sprintf("%d-%d-%d", year, int64(month), day) + log.Println(timeCFStr) + names, err := gorocksdb.ListColumnFamilies(opts, ".rocksdb") + if err != nil { + log.Println(".rocksdb 文件不存在") + names = append(names, "default") + } + + isadd := true + + var opslist []*gorocksdb.Options + for _, name := range names { + opslist = append(opslist, opts) + if timeCFStr == name { + isadd = false + } + } + + if isadd { + names = append(names, timeCFStr) + opslist = append(opslist, opts) + } + + log.Println("ListColumnFamilies:", names) + + db, cfs, err := gorocksdb.OpenDbColumnFamilies(opts, ".rocksdb", names, opslist) + if err != nil { + panic(err) + } + + return db, cfs +}