github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/database/leveldb/filterpolicy.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 package leveldb 6 7 // #include <stdlib.h> 8 // #include <leveldb/c.h> 9 import "C" 10 11 // FilterPolicy is a factory type that allows the LevelDB database to create a 12 // filter, such as a bloom filter, that is stored in the sstables and used by 13 // DB.Get to reduce reads. 14 // 15 // An instance of this struct may be supplied to Options when opening a 16 // DB. Typical usage is to call NewBloomFilter to get an instance. 17 // 18 // To prevent memory leaks, a FilterPolicy must have Close called on it when 19 // it is no longer needed by the program. 20 type FilterPolicy struct { 21 policy *C.leveldb_filterpolicy_t 22 } 23 24 // NewBloomFilter creates a filter policy that will create a bloom filter when 25 // necessary with the given number of bits per key. 26 // 27 // See the FilterPolicy documentation for more. 28 func NewBloomFilter(bitsPerKey int) *FilterPolicy { 29 policy := C.leveldb_filterpolicy_create_bloom(C.int(bitsPerKey)) 30 return &FilterPolicy{policy} 31 } 32 33 func (fp *FilterPolicy) Close() { 34 C.leveldb_filterpolicy_destroy(fp.policy) 35 }