本文整理汇总了Golang中github.com/jmhodges/levigo.NewReadOptions函数的典型用法代码示例。如果您正苦于以下问题:Golang NewReadOptions函数的具体用法?Golang NewReadOptions怎么用?Golang NewReadOptions使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewReadOptions函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Open
// Open returns a keyvalue DB backed by a LevelDB database at the given
// filepath. If opts==nil, the DefaultOptions are used.
func Open(path string, opts *Options) (keyvalue.DB, error) {
if opts == nil {
opts = DefaultOptions
}
options := levigo.NewOptions()
defer options.Close()
cache := levigo.NewLRUCache(opts.CacheCapacity)
options.SetCache(cache)
options.SetCreateIfMissing(!opts.MustExist)
if opts.WriteBufferSize > 0 {
options.SetWriteBufferSize(opts.WriteBufferSize)
}
db, err := levigo.Open(path, options)
if err != nil {
return nil, fmt.Errorf("could not open LevelDB at %q: %v", path, err)
}
largeReadOpts := levigo.NewReadOptions()
largeReadOpts.SetFillCache(opts.CacheLargeReads)
return &levelDB{
db: db,
cache: cache,
readOpts: levigo.NewReadOptions(),
largeReadOpts: largeReadOpts,
writeOpts: levigo.NewWriteOptions(),
}, nil
}
示例2: NewLeveldbCache
func NewLeveldbCache(dbname string, cacheM int) (*LeveldbCache, error) {
opts := levigo.NewOptions()
filter := levigo.NewBloomFilter(10)
cache := levigo.NewLRUCache(1024 * 1024 * cacheM)
opts.SetFilterPolicy(filter)
opts.SetCache(cache)
opts.SetCreateIfMissing(true)
opts.SetWriteBufferSize(8 * 1024 * 104) // 8M
opts.SetCompression(levigo.SnappyCompression)
if ldb, err := levigo.Open(dbname, opts); err == nil {
so := levigo.NewReadOptions()
so.SetFillCache(false)
return &LeveldbCache{
db: ldb,
fp: filter,
cache: cache,
Ro: levigo.NewReadOptions(),
Wo: levigo.NewWriteOptions(),
So: so,
}, nil
} else {
return nil, err
}
}
示例3: newSnapshot
func newSnapshot(db *DB) *Snapshot {
s := new(Snapshot)
s.db = db
s.s = db.db.NewSnapshot()
s.readOpts = levigo.NewReadOptions()
s.readOpts.SetSnapshot(s.s)
s.iteratorOpts = levigo.NewReadOptions()
s.iteratorOpts.SetSnapshot(s.s)
s.iteratorOpts.SetFillCache(false)
return s
}
示例4: OpenWithConfig
func OpenWithConfig(cfg *Config) (*DB, error) {
db := new(DB)
db.cfg = cfg
db.opts = db.initOptions(cfg)
db.readOpts = levigo.NewReadOptions()
db.writeOpts = levigo.NewWriteOptions()
db.iteratorOpts = levigo.NewReadOptions()
db.iteratorOpts.SetFillCache(false)
var err error
db.db, err = levigo.Open(cfg.Path, db.opts)
return db, err
}
示例5: AddProc
func AddProc(procID int, db *levigo.DB) error {
ro := levigo.NewReadOptions()
wo := levigo.NewWriteOptions()
data, err := db.Get(ro, []byte("procs"))
spdata := bytes.Split(data, []byte(":"))
for i, e := range spdata {
if string(e) != "" {
fmt.Println("ProcID: #", i, string(e))
pid, err := strconv.Atoi(string(e))
if err != nil {
return err
}
if pid == procID {
return errors.New("Process already exists")
}
if isProcAlive(pid) == false {
removeProc(pid, db)
}
}
if err != nil {
return err
}
}
strdata := string(data)
strdata = strdata + ":" + strconv.Itoa(procID)
err = db.Put(wo, []byte("procs"), []byte(strdata))
return err
}
示例6: GetAllKeysByAnchor
func (l *Level) GetAllKeysByAnchor(anchor []byte) []byte {
l.mu.Lock()
defer l.mu.Unlock()
var buffer bytes.Buffer
ro := levigo.NewReadOptions()
defer ro.Close()
ro.SetFillCache(false)
it := l.db.NewIterator(ro)
defer it.Close()
buffer.WriteByte('[')
it.Seek(anchor)
if !it.Valid() {
return nil
}
it.Next()
for it.Valid() && bytes.HasPrefix(it.Key(), anchor) {
buffer.Write(it.Value())
buffer.WriteByte(',')
it.Next()
}
bs := buffer.Bytes()
bs = bytes.TrimRight(bs, ",")
bs = append(bs, ']')
if err := it.GetError(); err != nil {
fmt.Println(err)
}
return bs
}
示例7: main
func main() {
dbname := "leveldb"
opts := levigo.NewOptions()
opts.SetCache(levigo.NewLRUCache(3 << 20))
opts.SetCreateIfMissing(true)
_ = levigo.DestroyDatabase(dbname, opts)
db, _ := levigo.Open(dbname, opts)
wo := levigo.NewWriteOptions()
ro := levigo.NewReadOptions()
start := time.Now()
for i := 0; i < 10e4; i++ {
db.Put(wo, []byte(fmt.Sprintf("a%v", i)), []byte(strconv.Itoa(i)))
}
for i := 0; i < 10e4; i++ {
db.Get(ro, []byte(fmt.Sprintf("a%v", i)))
}
for i := 0; i < 10e4; i++ {
db.Delete(wo, []byte(fmt.Sprintf("a%v", i)))
}
duration := time.Since(start)
log.Printf("Elapsed: %v.", duration)
}
示例8: TestLevigo
func TestLevigo(t *testing.T) {
path := "/tmp/levigo_test_10101"
os.RemoveAll(path)
opts := levigo.NewOptions()
filter := levigo.NewBloomFilter(10)
opts.SetFilterPolicy(filter)
opts.SetCache(levigo.NewLRUCache(1024 << 20)) // 1G
opts.SetCreateIfMissing(true)
if ldb, err := levigo.Open(path, opts); err == nil {
key := []byte("test-test hwl0dsfds")
val := []byte("value")
if err = ldb.Put(levigo.NewWriteOptions(), key, val); err != nil {
t.Fail()
} else {
ro := levigo.NewReadOptions()
if data, err := ldb.Get(ro, key); err == nil && reflect.DeepEqual(data, val) {
ro.SetFillCache(false)
it := ldb.NewIterator(ro)
it.Seek([]byte{0})
for ; it.Valid(); it.Next() {
log.Printf("%s => %s", it.Key(), it.Value())
}
} else {
t.Fail()
}
}
} else {
t.Fail()
}
}
示例9: DeleteAllRecords
func (store *LevelDbStore) DeleteAllRecords() error {
store.dbOpenLock.Lock()
defer store.dbOpenLock.Unlock()
if store.readOptions == nil && store.writeOptions == nil {
panic("You may only call DeleteAllRecords after starting reading or writing")
}
writeOptions := store.writeOptions
if writeOptions == nil {
writeOptions = levigo.NewWriteOptions()
defer writeOptions.Close()
}
readOptions := store.readOptions
if readOptions == nil {
readOptions = levigo.NewReadOptions()
defer readOptions.Close()
}
it := store.db.NewIterator(readOptions)
defer it.Close()
it.SeekToFirst()
for ; it.Valid(); it.Next() {
if err := store.db.Delete(writeOptions, it.Key()); err != nil {
return fmt.Errorf("Error clearing keys from database: %v", err)
}
}
if err := it.GetError(); err != nil {
return fmt.Errorf("Error iterating through database: %v", err)
}
return nil
}
示例10: NewLevelDB
func NewLevelDB(path string, config interface{}) (Engine, error) {
c, ok := config.(*LevelDbConfiguration)
if !ok {
return nil, fmt.Errorf("Config is of type %T instead of %T", config, LevelDbConfiguration{})
}
// if it wasn't set, set it to 100
if c.MaxOpenFiles == 0 {
c.MaxOpenFiles = 100
}
// if it wasn't set, set it to 200 MB
if c.LruCacheSize == 0 {
c.LruCacheSize = 200 * 1024 * 1024
}
// initialize the global cache
if cache == nil {
cacheLock.Lock()
if cache == nil {
cache = levigo.NewLRUCache(int(c.LruCacheSize))
}
cacheLock.Unlock()
}
opts := levigo.NewOptions()
opts.SetCache(cache)
opts.SetCreateIfMissing(true)
opts.SetMaxOpenFiles(c.MaxOpenFiles)
db, err := levigo.Open(path, opts)
wopts := levigo.NewWriteOptions()
ropts := levigo.NewReadOptions()
return LevelDB{db, opts, wopts, ropts, path}, err
}
示例11: replayFromLog
func (self *LevelDbDatastore) replayFromLog(seekKey []byte, requestLog *requestLogDb, yield func(*[]byte) error) error {
ro := levigo.NewReadOptions()
defer ro.Close()
ro.SetFillCache(false)
it := requestLog.db.NewIterator(ro)
defer it.Close()
startingKey := seekKey[:len(seekKey)-8]
sliceTo := len(startingKey)
it.Seek(seekKey)
if it.Valid() {
if bytes.Equal(it.Key(), seekKey) {
it.Next()
}
}
for it = it; it.Valid(); it.Next() {
k := it.Key()
if !bytes.Equal(k[:sliceTo], startingKey) {
return nil
}
b := it.Value()
err := yield(&b)
if err != nil {
return err
}
}
return nil
}
示例12: ReadDBCount
// Get the data count from leveldb
//
// @param string dbName The levelDb database name.
// @param func filter The condition filter function.
//
func ReadDBCount(dbName, condition string) (int, error) {
dbfile, err := getDbName(dbName)
if err != nil {
return 0, err
}
dbfile = DBRoot + "/" + dbfile
db, err := openDB(dbfile)
if err != nil {
return 0, err
}
ro := levigo.NewReadOptions()
ro.SetFillCache(false)
it := db.NewIterator(ro)
defer it.Close()
foundCnt := 0
for it.Seek([]byte(condition)); it.Valid(); it.Next() {
foundCnt++
}
if err := it.GetError(); err != nil {
return 0, err
}
return foundCnt, nil
}
示例13: NewLevelDBPersistence
func NewLevelDBPersistence(storageRoot string, cacheCapacity, bitsPerBloomFilterEncoded int) (p *LevelDBPersistence, err error) {
options := levigo.NewOptions()
options.SetCreateIfMissing(true)
options.SetParanoidChecks(true)
cache := levigo.NewLRUCache(cacheCapacity)
options.SetCache(cache)
filterPolicy := levigo.NewBloomFilter(bitsPerBloomFilterEncoded)
options.SetFilterPolicy(filterPolicy)
storage, err := levigo.Open(storageRoot, options)
if err != nil {
return
}
readOptions := levigo.NewReadOptions()
writeOptions := levigo.NewWriteOptions()
writeOptions.SetSync(true)
p = &LevelDBPersistence{
cache: cache,
filterPolicy: filterPolicy,
options: options,
readOptions: readOptions,
storage: storage,
writeOptions: writeOptions,
}
return
}
示例14: getPlaylistFromDB
func getPlaylistFromDB(db *levigo.DB) []playListItem {
log.Println("Loading Playlist from file...")
ro := levigo.NewReadOptions()
data, err := db.Get(ro, []byte("playlist"))
if err != nil {
log.Fatal(err)
}
p := bytes.NewBuffer(data)
dec := gob.NewDecoder(p)
var playlist []playListItem
//we must decode into a pointer, so we'll take the address of e
err = dec.Decode(&playlist)
if err != nil {
log.Print(err)
playlist = []playListItem{}
}
log.Println("Loaded ", len(playlist), " items into playlist")
log.Println(playlist)
return playlist
}
示例15: Test_DelKey
func Test_DelKey(t *testing.T) {
_key, _val, expectedVal := "name", "levigoNS", ""
dbpath := "/tmp/delete-this-leveldb"
db := CreateDB(dbpath)
writer := levigo.NewWriteOptions()
defer writer.Close()
keyname := []byte(_key)
value := []byte(_val)
err := db.Put(writer, keyname, value)
if err != nil {
t.Error("Fail: (DelKey) Pushing key " + _key + " for value " + _val + " failed")
}
statusDelete := DelKey(_key, db)
reader := levigo.NewReadOptions()
defer reader.Close()
resultVal, err := db.Get(reader, []byte(_key))
if err != nil {
t.Error("Fail: (DelKey) Reading key " + _key + " failed")
}
if string(resultVal) != expectedVal {
t.Error("Fail: DelKey sets " + string(resultVal))
}
if !statusDelete {
t.Error("Fail: DelKey returns False status")
}
CloseAndDeleteDB(dbpath, db)
}