修改为正常的逻辑存储 二进制数字相关

This commit is contained in:
eson 2020-03-04 03:32:07 +08:00
parent d08133a3a3
commit a511f1b1e6
4 changed files with 139 additions and 95 deletions

66
base.go Normal file
View File

@ -0,0 +1,66 @@
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
}

2
go.mod
View File

@ -1,4 +1,4 @@
module queue module rocksdb-api
go 1.14 go 1.14

112
main.go
View File

@ -1,98 +1,13 @@
package main package main
import ( import (
"fmt" "encoding/binary"
"log" "log"
"os"
"strconv"
"sync" "sync"
"time"
"github.com/Pallinder/go-randomdata"
"github.com/tecbot/gorocksdb" "github.com/tecbot/gorocksdb"
) )
func putData(wg *sync.WaitGroup, db *gorocksdb.DB, cfs []*gorocksdb.ColumnFamilyHandle) {
defer wg.Done()
now := time.Now().Unix()
wo := gorocksdb.NewDefaultWriteOptions()
COUNT := 1000000
for i := 0; i < COUNT; i++ {
name := strconv.Itoa(i) + "-" + randomdata.FirstName(-1) + "-" + randomdata.FullName(-1) + strconv.Itoa(randomdata.Number(0, 10000000))
age := randomdata.Number(0, 105)
level := randomdata.Number(0, 10)
task := randomdata.Email() + randomdata.Adjective() + randomdata.City()
// tx.Exec("insert into user(name, age,level, task, attr, taskid) values(?, ?, ?, ?, ?, ?)", name, age, level, task, attr, taskid)
putbuf := []byte(name)
keybuf := append(putbuf, '\x01', byte(age), '\x01', byte(level), '\x01')
putbuf = append(keybuf, []byte(task)...)
db.PutCF(wo, cfs[1], keybuf, putbuf)
}
log.Println(int64(COUNT) / (time.Now().Unix() - now))
}
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
}
func CreateData() { func CreateData() {
db, cfs := OpenDataBase() db, cfs := OpenDataBase()
@ -116,15 +31,24 @@ func queryData(wg *sync.WaitGroup, db *gorocksdb.DB, cfs []*gorocksdb.ColumnFami
ro := gorocksdb.NewDefaultReadOptions() ro := gorocksdb.NewDefaultReadOptions()
COUNT := 10 COUNT := 100
iter := db.NewIteratorCF(ro, cfs[2]) cf := cfs[len(cfs)-1]
iter.SeekForPrev([]byte("100")) iter := db.NewIteratorCF(ro, cf)
for i := 0; i < COUNT; i++ { temp := make([]byte, 8)
key := string(iter.Key().Data()) binary.BigEndian.PutUint64(temp, 10)
iter.Seek(temp)
log.Println(iter.Valid())
for i := 0; i < COUNT && iter.Valid(); i++ {
bkey := iter.Key().Data()
key := string(bkey)
value := string(iter.Value().Data()) value := string(iter.Value().Data())
log.Println("\nkey:", key, "\n", "value:", value)
log.Println(binary.BigEndian.Uint64(bkey[0:8]), "\nkey:", key, "\n", "value:", value)
iter.Next() iter.Next()
} }
@ -148,8 +72,8 @@ func QueryData() {
func main() { func main() {
// QueryData() QueryData()
CreateData() // CreateData()
// db.Put(wo, []byte("foo"), []byte("bar")) // db.Put(wo, []byte("foo"), []byte("bar"))

54
put_data.go Normal file
View File

@ -0,0 +1,54 @@
package main
import (
"encoding/binary"
"log"
"sync"
"time"
"github.com/Pallinder/go-randomdata"
"github.com/tecbot/gorocksdb"
)
func putData(wg *sync.WaitGroup, db *gorocksdb.DB, cfs []*gorocksdb.ColumnFamilyHandle) {
defer wg.Done()
now := time.Now().Unix()
wo := gorocksdb.NewDefaultWriteOptions()
COUNT := 100000
for i := 0; i < COUNT; i++ {
temp := make([]byte, 8)
name := make([]byte, 8)
binary.BigEndian.PutUint64(name, uint64(i))
name = append(name, '-')
name = append(name, []byte(randomdata.FirstName(-1))...)
name = append(name, '-')
name = append(name, []byte(randomdata.FullName(-1))...)
name = append(name, '-')
binary.BigEndian.PutUint64(temp, uint64(randomdata.Number(0, 10000000)))
name = append(name, temp...)
age := randomdata.Number(0, 105)
level := randomdata.Number(0, 10)
task := randomdata.Email() + randomdata.Adjective() + randomdata.City()
// tx.Exec("insert into user(name, age,level, task, attr, taskid) values(?, ?, ?, ?, ?, ?)", name, age, level, task, attr, taskid)
putbuf := name
keybuf := append(putbuf, '\x01')
binary.BigEndian.PutUint16(temp, uint16(age))
keybuf = append(name, temp[0:2]...)
keybuf = append(keybuf, '\x01')
binary.BigEndian.PutUint16(temp, uint16(level))
keybuf = append(name, temp[0:2]...)
putbuf = append(putbuf, '\x01')
putbuf = append(keybuf, []byte(task)...)
db.PutCF(wo, cfs[len(cfs)-1], keybuf, putbuf)
}
log.Println(int64(COUNT) / (time.Now().Unix() - now))
}