data_workshop/base.go

150 lines
3.3 KiB
Go
Raw Permalink Normal View History

2020-05-13 06:57:57 +00:00
package main
import (
"bytes"
"compress/flate"
"encoding/gob"
"log"
"math/rand"
"os"
2020-05-19 10:12:18 +00:00
"reflect"
2020-05-13 06:57:57 +00:00
)
2020-05-19 10:12:18 +00:00
type KeyKind struct {
Key string
FieldName string
}
func NewKeyKind(key string, fieldname string) KeyKind {
return KeyKind{Key: key, FieldName: fieldname}
}
type KeyList struct {
Keys []interface{}
}
func (kl *KeyList) GetKeys() []interface{} {
return kl.Keys
}
func (kl *KeyList) AppendKey(key interface{}) {
kl.Keys = append(kl.Keys, key)
}
func (kl *KeyList) GetLength() int {
return len(kl.Keys)
}
2020-05-13 06:57:57 +00:00
// CheckErrorPanic 查错误 存在就panic操作
func CheckErrorPanic(err error) {
if err != nil {
log.Panic(err)
}
}
// OpenDB 打开rocksdb
2020-06-03 06:27:09 +00:00
// func OpenDB() *gorocksdb.DB {
// bbto := gorocksdb.NewDefaultBlockBasedTableOptions()
2020-05-13 06:57:57 +00:00
2020-06-03 06:27:09 +00:00
// bbto.SetBlockCache(gorocksdb.NewLRUCache(2 << 30))
// bbto.SetCacheIndexAndFilterBlocksWithHighPriority(true)
2020-05-13 06:57:57 +00:00
2020-06-03 06:27:09 +00:00
// opts := gorocksdb.NewDefaultOptions()
2020-05-13 06:57:57 +00:00
2020-06-03 06:27:09 +00:00
// bbto.SetFilterPolicy(gorocksdb.NewBloomFilter(16))
2020-05-13 06:57:57 +00:00
2020-06-03 06:27:09 +00:00
// opts.SetBlockBasedTableFactory(bbto)
// opts.SetCreateIfMissing(true)
// opts.SetCreateIfMissingColumnFamilies(true)
// opts.SetCompression(gorocksdb.LZ4Compression)
2020-05-13 06:57:57 +00:00
2020-06-03 06:27:09 +00:00
// db, err := gorocksdb.OpenDb(opts, ".rocksdb")
// CheckErrorPanic(err)
// return db
// }
2020-05-13 06:57:57 +00:00
2020-06-03 06:27:09 +00:00
// var db = OpenDB()
// var wopts = gorocksdb.NewDefaultWriteOptions()
// var ropts = gorocksdb.NewDefaultReadOptions()
2020-05-13 06:57:57 +00:00
// IKeyList key list interface
type IKeyList interface {
2020-05-19 10:12:18 +00:00
AppendKey(key interface{})
GetKeys() []interface{}
2020-05-13 06:57:57 +00:00
GetLength() int
}
// SaveData gob序列化存数据
2020-05-19 10:12:18 +00:00
func SaveData(fname string, v interface{}) {
2020-05-13 06:57:57 +00:00
var keybuf = &bytes.Buffer{}
enc := gob.NewEncoder(keybuf)
enc.Encode(v)
2020-05-19 10:12:18 +00:00
f, err := os.OpenFile(fname, os.O_CREATE|os.O_RDWR|os.O_TRUNC, os.ModePerm)
2020-05-13 06:57:57 +00:00
CheckErrorPanic(err)
2020-05-19 10:12:18 +00:00
gw, err := flate.NewWriter(f, 7)
2020-05-13 06:57:57 +00:00
CheckErrorPanic(err)
defer gw.Close()
gw.Write(keybuf.Bytes())
}
// SaveGob 用于解析rocksdb源数据, 爬取数据存储在rocksdb后取出 序列化存储
2020-06-03 06:27:09 +00:00
// func SaveGob(fname string, datatype reflect.Type, kfs ...KeyKind) {
// // cl := &CountryList{}
// kl := &KeyList{}
// i := 0
// iter := db.NewIterator(ropts)
// bkey := []byte(kfs[0].Key)
// iter.Seek(bkey)
// for ; iter.ValidForPrefix(bkey); iter.Next() {
// i++
// first := iter.Value().Data()
// data := reflect.New(datatype).Elem()
// data.FieldByName(kfs[0].FieldName).Set(reflect.ValueOf(first))
// // country := &Country{}
// // country.Name = name
// for n := 1; n < len(kfs); n++ {
// v, err := db.Get(ropts, []byte(fmt.Sprintf(kfs[n].Key, first)))
// if err != nil {
// panic(err)
// }
// fv := data.FieldByName(kfs[n].FieldName)
// fv.Set(reflect.ValueOf(v.Data()))
// }
// kl.AppendKey(data.Interface())
// }
// SaveData(fname, kl)
// }
2020-05-19 10:12:18 +00:00
2020-05-13 06:57:57 +00:00
// LoadGob load gob from file
func LoadGob(fname string, v IKeyList) {
2020-05-13 06:57:57 +00:00
f, err := os.Open(fname)
log.Println("load data: ", fname)
2020-05-13 06:57:57 +00:00
CheckErrorPanic(err)
reader := flate.NewReader(f)
dec := gob.NewDecoder(reader)
CheckErrorPanic(dec.Decode(v))
2020-05-13 06:57:57 +00:00
}
// GetRandomKey get ikeylist key by random
2020-05-19 10:12:18 +00:00
func GetRandomKey(v IKeyList) interface{} {
ret := v.GetKeys()[rand.Intn(v.GetLength())]
return ret
2020-05-13 06:57:57 +00:00
}
2020-05-20 08:51:40 +00:00
// GetRandomKeyBySlice get ikeylist key by random
func GetRandomKeyBySlice(v interface{}) interface{} {
sli := reflect.ValueOf(v)
return sli.Index(rand.Intn(sli.Len())).Interface()
}