edb/rocksdb.go
2020-03-06 12:02:22 +08:00

141 lines
2.6 KiB
Go

package main
import (
"fmt"
"log"
"os"
"sync/atomic"
"time"
"github.com/tecbot/gorocksdb"
)
// EDB 全局操作的edb对象
var EDB *EasyDataBase
// OpenLog 初始化日志设置
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()
EDB = NewEasyDataBase()
}
func NewEasyDataBase() *EasyDataBase {
edb := &EasyDataBase{}
db, cfs := OpenDataBase()
edb.DB = db
edb.CFS = cfs
return edb
}
type EasyDataBase struct {
DB *gorocksdb.DB
CFS gorocksdb.ColumnFamilyHandles
Tables map[uint32]*Table
TableDict map[string]uint32
Metadata
}
type Metadata struct {
tidCount uint32 // int16
}
type Field struct {
Key string
ValueType int
ID int
IsIndex bool
IsUnique bool
}
type Table struct {
Name string
Type int
ID uint32 // uint16
IsAllKey bool
Fields []*Field
fidCount uint16 // uint16
idxCount uint64
rawCount uint64
delCount uint64
}
func CreateTable(name string, field []*Field) {
if _, ok := EDB.TableDict[name]; !ok {
ntid := atomic.AddUint32(&EDB.Metadata.tidCount, 1)
table := &Table{Name: name, ID: ntid, Type: 1}
} else {
log.Println("table name is exists")
}
}
// gson
// OpenDataBase (cf-key.{tableid(2)}{fieldid(2)}) {value}{indexid(6)} = {rowid(6)} (cf-row.{tableid(2)} {row-sharding-id}){rowid(6)} = {values}
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
}