本文整理汇总了Golang中github.com/jmhodges/levigo.NewLRUCache函数的典型用法代码示例。如果您正苦于以下问题:Golang NewLRUCache函数的具体用法?Golang NewLRUCache怎么用?Golang NewLRUCache使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewLRUCache函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: main
func main() {
flag.Parse()
if *showVersion {
fmt.Printf("gocountme: v%s\n", VERSION)
return
}
if *defaultSize <= 0 {
fmt.Printf("--default-size must be greater than 0\n")
return
}
if _, err := os.Stat(*dblocation); err != nil {
if os.IsNotExist(err) {
fmt.Println("Database location does not exist:", *dblocation)
return
}
}
log.Println("Opening levelDB")
Default_KMinValues_Size = *defaultSize
opts := levigo.NewOptions()
opts.SetCache(levigo.NewLRUCache(*leveldbLRUCache))
opts.SetCreateIfMissing(true)
db, err := levigo.Open(*dblocation, opts)
defer db.Close()
if err != nil {
log.Panicln(err)
}
RequestChan = make(chan RequestCommand, *nWorkers)
workerWaitGroup := sync.WaitGroup{}
log.Printf("Starting %d workers", *nWorkers)
for i := 0; i < *nWorkers; i++ {
go func(id int) {
workerWaitGroup.Add(1)
levelDBWorker(db, RequestChan)
workerWaitGroup.Done()
}(i)
}
http.HandleFunc("/get", GetHandler)
http.HandleFunc("/delete", DeleteHandler)
http.HandleFunc("/cardinality", CardinalityHandler)
http.HandleFunc("/jaccard", JaccardHandler)
http.HandleFunc("/correlation", CorrelationMatrixHandler)
http.HandleFunc("/add", AddHandler)
http.HandleFunc("/addhash", AddHashHandler)
http.HandleFunc("/query", QueryHandler)
http.HandleFunc("/exit", ExitHandler)
log.Printf("Starting gocountme HTTP server on %s", *httpAddress)
go func() {
log.Fatal(http.ListenAndServe(*httpAddress, nil))
}()
workerWaitGroup.Wait()
}
示例2: 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)
}
示例3: InitDB
func InitDB() (*levigo.DB, error) {
opts := levigo.NewOptions()
opts.SetCache(levigo.NewLRUCache(1024 * 1024))
opts.SetCreateIfMissing(true)
homedir := os.Getenv("HOME")
dbdir := homedir + "/.overseer/db"
os.MkdirAll(dbdir, 0700)
db, err := levigo.Open(dbdir, opts)
if err != nil {
fmt.Println("Failed to open database", err)
return nil, err
}
procs, err := ListProcs(db)
for p, status := range procs {
log.Printf("Proc ID: %d, status: %d", p, status)
if isProcAlive(p) == false {
setProcStatus(db, p, PROC_STOPPED)
} else {
setProcStatus(db, p, PROC_ALIVE)
}
}
return db, nil
}
示例4: 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()
}
}
示例5: NewDatabase
// Will panic if there is a problem with the database.
// Should only be called on server initialization.
func NewDatabase(databaseLocation string) *Database {
buckets := new(Database)
buckets.DBMap = make(map[string]*levigo.DB)
buckets.BaseLocation = databaseLocation
os.MkdirAll(databaseLocation, 0755)
files, err := ioutil.ReadDir(databaseLocation)
if err != nil {
panic(err)
}
for _, file := range files {
if !file.IsDir() || strings.HasPrefix(file.Name(), "_") {
continue
}
opts := levigo.NewOptions()
opts.SetCreateIfMissing(true)
opts.SetCache(levigo.NewLRUCache(4194304))
buckets.DBMap[file.Name()], err = levigo.Open(path.Join(databaseLocation, file.Name()), opts)
if err != nil {
panic(err)
}
}
return buckets
}
示例6: 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
}
示例7: NewTree
func NewTree(dbname string) *Tree {
debug("New tree")
n := new(Tree)
n.closed = false
opts := levigo.NewOptions()
opts.SetCache(levigo.NewLRUCache(3 << 30))
opts.SetCreateIfMissing(true)
d, err := levigo.Open(dbname, opts)
if err != nil {
Error(err)
}
n.DB = d
if n.Root, err = n.Get(nil, 0); err != nil {
debug("could not get", err)
n.Root = &Node{Parent: 0}
if err := n.Put(nil, n.Root); err != nil {
Error("could not set", err)
}
}
n.Newid = make(chan int)
go func() {
id := n.Root.Parent + 1
for ; ; id++ {
n.Newid <- id
if n.closed {
break
}
}
log.Println("exiting Newid goroutine")
}()
return n
}
示例8: 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
}
示例9: 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
}
}
示例10: open
func (c *cache) open(path string) error {
opts := levigo.NewOptions()
opts.SetCreateIfMissing(true)
if c.options.CacheSizeM > 0 {
c.cache = levigo.NewLRUCache(c.options.CacheSizeM * 1024 * 1024)
opts.SetCache(c.cache)
}
if c.options.MaxOpenFiles > 0 {
opts.SetMaxOpenFiles(c.options.MaxOpenFiles)
}
if c.options.BlockRestartInterval > 0 {
opts.SetBlockRestartInterval(c.options.BlockRestartInterval)
}
if c.options.WriteBufferSizeM > 0 {
opts.SetWriteBufferSize(c.options.WriteBufferSizeM * 1024 * 1024)
}
if c.options.BlockSizeK > 0 {
opts.SetBlockSize(c.options.BlockSizeK * 1024)
}
db, err := levigo.Open(path, opts)
if err != nil {
return err
}
c.db = db
c.wo = levigo.NewWriteOptions()
c.ro = levigo.NewReadOptions()
return nil
}
示例11: initOptions
func (db *DB) initOptions(cfg *Config) *levigo.Options {
opts := levigo.NewOptions()
opts.SetCreateIfMissing(true)
if cfg.CacheSize > 0 {
db.cache = levigo.NewLRUCache(cfg.CacheSize)
opts.SetCache(db.cache)
}
//we must use bloomfilter
db.filter = levigo.NewBloomFilter(defaultFilterBits)
opts.SetFilterPolicy(db.filter)
if !cfg.Compression {
opts.SetCompression(levigo.NoCompression)
}
if cfg.BlockSize > 0 {
opts.SetBlockSize(cfg.BlockSize)
}
if cfg.WriteBufferSize > 0 {
opts.SetWriteBufferSize(cfg.WriteBufferSize)
}
return opts
}
示例12: 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
}
示例13: initDB
func initDB() (*levigo.DB, error) {
opts := levigo.NewOptions()
opts.SetCache(levigo.NewLRUCache(3 << 30))
opts.SetCreateIfMissing(true)
return levigo.Open(settings.dir+"/db", opts)
}
示例14: NewLevelDbShardDatastore
func NewLevelDbShardDatastore(config *configuration.Configuration) (*LevelDbShardDatastore, error) {
baseDbDir := filepath.Join(config.DataDir, SHARD_DATABASE_DIR)
err := os.MkdirAll(baseDbDir, 0744)
if err != nil {
return nil, err
}
opts := levigo.NewOptions()
opts.SetCache(levigo.NewLRUCache(config.LevelDbLruCacheSize))
opts.SetCreateIfMissing(true)
opts.SetBlockSize(64 * ONE_KILOBYTE)
filter := levigo.NewBloomFilter(SHARD_BLOOM_FILTER_BITS_PER_KEY)
opts.SetFilterPolicy(filter)
opts.SetMaxOpenFiles(config.LevelDbMaxOpenFiles)
return &LevelDbShardDatastore{
baseDbDir: baseDbDir,
config: config,
shards: make(map[uint32]*LevelDbShard),
levelDbOptions: opts,
maxOpenShards: config.LevelDbMaxOpenShards,
lastAccess: make(map[uint32]int64),
shardRefCounts: make(map[uint32]int),
shardsToClose: make(map[uint32]bool),
pointBatchSize: config.LevelDbPointBatchSize,
}, nil
}
示例15: NewLevelDbDatastore
func NewLevelDbDatastore(dbDir string) (Datastore, error) {
opts := levigo.NewOptions()
opts.SetCache(levigo.NewLRUCache(ONE_GIGABYTE))
opts.SetCreateIfMissing(true)
opts.SetBlockSize(TWO_FIFTY_SIX_KILOBYTES)
filter := levigo.NewBloomFilter(BLOOM_FILTER_BITS_PER_KEY)
opts.SetFilterPolicy(filter)
db, err := levigo.Open(dbDir, opts)
if err != nil {
return nil, err
}
ro := levigo.NewReadOptions()
lastIdBytes, err2 := db.Get(ro, NEXT_ID_KEY)
if err2 != nil {
return nil, err2
}
lastId := uint64(0)
if lastIdBytes != nil {
lastId, err2 = binary.ReadUvarint(bytes.NewBuffer(lastIdBytes))
if err2 != nil {
return nil, err2
}
}
wo := levigo.NewWriteOptions()
return &LevelDbDatastore{db: db, lastIdUsed: lastId, readOptions: ro, writeOptions: wo}, nil
}