github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/database/leveldb/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 the ability to create and access LevelDB databases.
     7  
     8  leveldb.Open opens and creates databases.
     9  
    10  	opts := leveldb.NewOptions()
    11  	opts.SetCache(leveldb.NewLRUCache(3<<30))
    12  	opts.SetCreateIfMissing(true)
    13  	db, err := leveldb.Open("/path/to/db", opts)
    14  
    15  The DB struct returned by Open provides DB.Get, DB.Put and DB.Delete to modify
    16  and query the database.
    17  
    18  	ro := leveldb.NewReadOptions()
    19  	wo := leveldb.NewWriteOptions()
    20  	// if ro and wo are not used again, be sure to Close them.
    21  	data, err := db.Get(ro, []byte("key"))
    22  	...
    23  	err = db.Put(wo, []byte("anotherkey"), data)
    24  	...
    25  	err = db.Delete(wo, []byte("key"))
    26  
    27  For bulk reads, use an Iterator. If you want to avoid disturbing your live
    28  traffic while doing the bulk read, be sure to call SetFillCache(false) on the
    29  ReadOptions you use when creating the Iterator.
    30  
    31  	ro := leveldb.NewReadOptions()
    32  	ro.SetFillCache(false)
    33  	it := db.NewIterator(ro)
    34  	defer it.Close()
    35  	it.Seek(mykey)
    36  	for it = it; it.Valid(); it.Next() {
    37  		munge(it.Key(), it.Value())
    38  	}
    39  	if err := it.GetError(); err != nil {
    40  		...
    41  	}
    42  
    43  Batched, atomic writes can be performed with a WriteBatch and
    44  DB.Write.
    45  
    46  	wb := leveldb.NewWriteBatch()
    47  	// defer wb.Close or use wb.Clear and reuse.
    48  	wb.Delete([]byte("removed"))
    49  	wb.Put([]byte("added"), []byte("data"))
    50  	wb.Put([]byte("anotheradded"), []byte("more"))
    51  	err := db.Write(wo, wb)
    52  
    53  If your working dataset does not fit in memory, you'll want to add a bloom
    54  filter to your database. NewBloomFilter and Options.SetFilterPolicy is what
    55  you want. NewBloomFilter is amount of bits in the filter to use per key in
    56  your database.
    57  
    58  	filter := leveldb.NewBloomFilter(10)
    59  	opts.SetFilterPolicy(filter)
    60  	db, err := leveldb.Open("/path/to/db", opts)
    61  
    62  If you're using a custom comparator in your code, be aware you may have to
    63  make your own filter policy object.
    64  
    65  This documentation is not a complete discussion of LevelDB. Please read the
    66  LevelDB documentation <http://code.google.com/p/leveldb> for information on
    67  its operation. You'll find lots of goodies there.
    68  */
    69  package leveldb