todo: create table

This commit is contained in:
huangsimin 2020-03-05 18:10:40 +08:00
parent 5bcadb8bb8
commit dc9b3abe9e
4 changed files with 94 additions and 11 deletions

6
api.go
View File

@ -14,12 +14,12 @@ type server struct{}
// SayHello implements // SayHello implements
func (s *server) SayHello(ctx context.Context, in *goproto.Request) (*goproto.Reply, error) { 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 // SayHello implements
func (s *server) CreateTable(ctx context.Context, in *goproto.TableRequest) (*goproto.TableReply, error) { func (s *server) CreateTable(ctx context.Context, in *goproto.TableRequest) (*goproto.Reply, error) {
return &goproto.TableReply{Msg: "Hello " + in.Name}, nil return &goproto.Reply{Msg: "Create Table " + in.Name, Status: 1, Content: []string{"dasasdasdas"}}, nil
} }
func createTestServer() { func createTestServer() {

View File

@ -4,13 +4,24 @@ package goproto;
// //
service EasyData { 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 { message TableRequest {
string name = 1; string name = 1;
repeated Field field = 2;
} }
// //
message TableReply { message Reply {
string msg = 1; int64 status = 1;
string msg = 2;
repeated string content = 3;
} }

View File

@ -1,6 +1,9 @@
syntax = "proto3"; syntax = "proto3";
package goproto; package goproto;
import "api.proto";
// //
service Test { service Test {
// //
@ -10,7 +13,3 @@ service Test {
message Request { message Request {
string what = 1; string what = 1;
} }
//
message Reply {
string msg = 1;
}

73
rocksdb.go Normal file
View File

@ -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
}