rocksdb-api/main.go

92 lines
1.4 KiB
Go
Raw Normal View History

2020-03-03 10:31:48 +00:00
package main
import (
"encoding/binary"
2020-03-03 10:31:48 +00:00
"log"
"sync"
"github.com/tecbot/gorocksdb"
)
func CreateData() {
db, cfs := OpenDataBase()
GCOUNT := 10
2020-03-03 10:31:48 +00:00
wg := &sync.WaitGroup{}
wg.Add(GCOUNT)
for i := 0; i < GCOUNT; i++ {
go putData(wg, db, cfs)
}
wg.Wait()
}
func queryData(wg *sync.WaitGroup, db *gorocksdb.DB, cfs []*gorocksdb.ColumnFamilyHandle) {
defer wg.Done()
// now := time.Now().Unix()
ro := gorocksdb.NewDefaultReadOptions()
COUNT := 100
2020-03-03 10:31:48 +00:00
cf := cfs[len(cfs)-1]
iter := db.NewIteratorCF(ro, cf)
2020-03-03 10:31:48 +00:00
temp := make([]byte, 8)
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)
2020-03-03 10:31:48 +00:00
value := string(iter.Value().Data())
log.Println(binary.BigEndian.Uint64(bkey[0:8]), "\nkey:", key, "\n", "value:", value)
2020-03-03 10:31:48 +00:00
iter.Next()
}
// log.Println(int64(COUNT) / (time.Now().Unix() - now))
}
func QueryData() {
db, cfs := OpenDataBase()
GCOUNT := 1
wg := &sync.WaitGroup{}
wg.Add(GCOUNT)
for i := 0; i < GCOUNT; i++ {
go queryData(wg, db, cfs)
}
wg.Wait()
}
func main() {
QueryData()
// CreateData()
2020-03-03 10:31:48 +00:00
// db.Put(wo, []byte("foo"), []byte("bar"))
// ro := gorocksdb.NewDefaultReadOptions()
// db, err := gorocksdb.OpenDb(opts, "./.rocksdb")
// if err != nil {
// panic(err)
// }
// s, err := db.Get(ro, []byte("foo"))
// if err != nil {
// panic(err)
// }
// log.Println(string(s.Data()))
}