github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/database/leveldb.chai2010/doc.go (about)

     1  // Copyright 2013 <chaishushan{AT}gmail.com>. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  /*
     6  Package leveldb provides Go bindings for LevelDB.
     7  
     8  leveldb.Open opens and creates databases.
     9  
    10  	opt := &leveldb.Options{}
    11  	opt.SetBlockCacheCapacity(64 << 20)
    12  	opt.SetFlag(leveldb.OFCreateIfMissing)
    13  	db, err := leveldb.Open("/path/to/db", opt)
    14  	if err != nil {
    15  		log.Fatal(err) // *leveldb.Error
    16  	}
    17  	defer db.Close()
    18  
    19  The DB struct returned by Open provides DB.Get, DB.Put and DB.Delete to modify
    20  and query the database.
    21  
    22  	data, err := db.Get([]byte("key"), nil)
    23  	...
    24  	err = db.Put([]byte("anotherkey"), data, nil)
    25  	...
    26  	err = db.Delete([]byte("key"), nil)
    27  
    28  For bulk reads, use an Iterator. If you want to avoid disturbing your live
    29  traffic while doing the bulk read, be sure to call SetFillCache(false) on the
    30  ReadOptions you use when creating the Iterator.
    31  
    32  	ro := &leveldb.ReadOptions{}
    33  	ro.SetFlag(leveldb.RFDontFillCache)
    34  	it := db.NewIterator(ro)
    35  	defer it.Release()
    36  
    37  	for it.Seek(mykey); it.Valid(); it.Next() {
    38  		doSomething(it.Key(), it.Value())
    39  	}
    40  	if err := it.GetError(); err != nil {
    41  		// ...
    42  	}
    43  
    44  Batched, atomic writes can be performed with a WriteBatch and
    45  DB.Write.
    46  
    47  	b := leveldb.NewWriteBatch()
    48  	defer b.Release()
    49  
    50  	b.Delete([]byte("removed"))
    51  	b.Put([]byte("added"), []byte("data"))
    52  	b.Put([]byte("anotheradded"), []byte("more"))
    53  	err := db.Write(b, nil)
    54  
    55  If your working dataset does not fit in memory, you'll want to add a bloom
    56  filter to your database. NewBloomFilter and Options.SetFilterPolicy is what
    57  you want. NewBloomFilter is amount of bits in the filter to use per key in
    58  your database.
    59  
    60  	filter := leveldb.NewBloomFilter(10)
    61  	opt := &leveldb.Options{}
    62  	opt.SetFilter(leveldb.NewBloomFilter(10))
    63  	db, err := leveldb.Open("/path/to/db", opt)
    64  
    65  If you're using a custom comparator in your code, be aware you may have to
    66  make your own filter policy object.
    67  
    68  This documentation is not a complete discussion of LevelDB. Please read the
    69  LevelDB documentation <http://code.google.com/p/leveldb> for information on
    70  its operation. You'll find lots of goodies there.
    71  */
    72  package leveldb