rocksdb-api/base.go

67 lines
1.4 KiB
Go
Raw Permalink Normal View History

package main
import (
"fmt"
"log"
"os"
"time"
"github.com/tecbot/gorocksdb"
)
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)
f, err := os.OpenFile("./log", os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0660)
if err != nil {
panic(err)
}
log.SetOutput(f)
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(names)
db, cfs, err := gorocksdb.OpenDbColumnFamilies(opts, ".rocksdb", names, opslist)
if err != nil {
panic(err)
}
return db, cfs
}