本文整理匯總了Golang中code/google/com/p/leveldb-go/leveldb/db.Options類的典型用法代碼示例。如果您正苦於以下問題:Golang Options類的具體用法?Golang Options怎麽用?Golang Options使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Options類的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: New
// New returns a new MemDB.
func New(o *db.Options) *MemDB {
return &MemDB{
height: 1,
cmp: o.GetComparer(),
kvData: make([]byte, 0, 4096),
// The first maxHeight values of nodeData are the next nodes after the
// head node at each possible height. Their initial value is zeroNode.
nodeData: make([]int, maxHeight, 256),
}
}
示例2: createDB
func createDB(dirname string, opts *db.Options) (retErr error) {
const manifestFileNum = 1
ve := versionEdit{
comparatorName: opts.GetComparer().Name(),
nextFileNumber: manifestFileNum + 1,
}
manifestFilename := dbFilename(dirname, fileTypeManifest, manifestFileNum)
f, err := opts.GetFileSystem().Create(manifestFilename)
if err != nil {
return fmt.Errorf("leveldb: could not create %q: %v", manifestFilename, err)
}
defer func() {
if retErr != nil {
opts.GetFileSystem().Remove(manifestFilename)
}
}()
defer f.Close()
recWriter := record.NewWriter(f)
w, err := recWriter.Next()
if err != nil {
return err
}
err = ve.encode(w)
if err != nil {
return err
}
err = recWriter.Close()
if err != nil {
return err
}
return setCurrentFile(dirname, opts.GetFileSystem(), manifestFileNum)
}
示例3: NewWriter
// NewWriter returns a new table writer for the file. Closing the writer will
// close the file.
func NewWriter(f db.File, o *db.Options) *Writer {
w := &Writer{
closer: f,
blockRestartInterval: o.GetBlockRestartInterval(),
blockSize: o.GetBlockSize(),
cmp: o.GetComparer(),
compression: o.GetCompression(),
prevKey: make([]byte, 0, 256),
restarts: make([]uint32, 0, 256),
}
if f == nil {
w.err = errors.New("leveldb/table: nil file")
return w
}
// If f does not have a Flush method, do our own buffering.
type flusher interface {
Flush() error
}
if _, ok := f.(flusher); ok {
w.writer = f
} else {
w.bufWriter = bufio.NewWriter(f)
w.writer = w.bufWriter
}
return w
}
示例4: NewReader
// NewReader returns a new table reader for the file. Closing the reader will
// close the file.
func NewReader(f db.File, o *db.Options) *Reader {
r := &Reader{
file: f,
comparer: o.GetComparer(),
verifyChecksums: o.GetVerifyChecksums(),
}
if f == nil {
r.err = errors.New("leveldb/table: nil file")
return r
}
stat, err := f.Stat()
if err != nil {
r.err = fmt.Errorf("leveldb/table: invalid table (could not stat file): %v", err)
return r
}
var footer [footerLen]byte
if stat.Size() < int64(len(footer)) {
r.err = errors.New("leveldb/table: invalid table (file size is too small)")
return r
}
_, err = f.ReadAt(footer[:], stat.Size()-int64(len(footer)))
if err != nil && err != io.EOF {
r.err = fmt.Errorf("leveldb/table: invalid table (could not read footer): %v", err)
return r
}
if string(footer[footerLen-len(magic):footerLen]) != magic {
r.err = errors.New("leveldb/table: invalid table (bad magic number)")
return r
}
// Ignore the metaindex.
_, n := decodeBlockHandle(footer[:])
if n == 0 {
r.err = errors.New("leveldb/table: invalid table (bad metaindex block handle)")
return r
}
// Read the index into memory.
indexBH, n := decodeBlockHandle(footer[n:])
if n == 0 {
r.err = errors.New("leveldb/table: invalid table (bad index block handle)")
return r
}
r.index, r.err = r.readBlock(indexBH)
return r
}
示例5: createDB
// 創建leveldb存儲目錄時,需要傳入db.Options, 那麽db.Options有哪些屬性呢?
func createDB(dirname string, opts *db.Options) (retErr error) {
const manifestFileNum = 1
// versionEdit保存版本之間的差異,oldVersion _+ versionEdit = newVersion
ve := versionEdit{
// 原始key比較器的名稱
comparatorName: opts.GetComparer().Name(),
// ?
nextFileNumber: manifestFileNum + 1,
}
// 先創建manifest文件,往裏麵寫一些元信息,如上麵versionEdit的comparatorName、nextFileNumber字段
manifestFilename := dbFilename(dirname, fileTypeManifest, manifestFileNum)
f, err := opts.GetFileSystem().Create(manifestFilename)
if err != nil {
return fmt.Errorf("leveldb: could not create %q: %v", manifestFilename, err)
}
defer func() {
if retErr != nil {
opts.GetFileSystem().Remove(manifestFilename)
}
}()
defer f.Close()
// 生成一個record.Writer對象
recWriter := record.NewWriter(f)
// 返回一個record.SingleWriter對象w
w, err := recWriter.Next()
if err != nil {
return err
}
// 將ve中的數據寫入manifest文件中
err = ve.encode(w)
if err != nil {
return err
}
// 加入重啟點, 壓縮,crc校驗?
err = recWriter.Close()
if err != nil {
return err
}
// 創建current文件,其中寫入了當前的manifest文件名
return setCurrentFile(dirname, opts.GetFileSystem(), manifestFileNum)
}
示例6: Open
// Open opens a LevelDB whose files live in the given directory.
func Open(dirname string, opts *db.Options) (*DB, error) {
d := &DB{
dirname: dirname,
opts: opts,
icmp: internalKeyComparer{opts.GetComparer()},
pendingOutputs: make(map[uint64]struct{}),
}
if opts != nil {
d.icmpOpts = *opts
}
d.icmpOpts.Comparer = d.icmp
tableCacheSize := opts.GetMaxOpenFiles() - numNonTableCacheFiles
if tableCacheSize < minTableCacheSize {
tableCacheSize = minTableCacheSize
}
// tableCache初始化
d.tableCache.init(dirname, opts.GetFileSystem(), &d.icmpOpts, tableCacheSize)
// 初始化一個MemDB
d.mem = memdb.New(&d.icmpOpts)
// sync.Cond在Locker的基礎上增加的一個消息通知的功能。
// Cond有三個方法:Wait,Signal,Broadcast。
// Wait添加一個計數,也就是添加一個阻塞的goroutine。
// Signal解除一個goroutine的阻塞,計數減一。
// Broadcast接觸所有wait goroutine的阻塞。
d.compactionCond = sync.Cond{L: &d.mu}
fs := opts.GetFileSystem()
d.mu.Lock()
defer d.mu.Unlock()
// Lock the database directory.
// If the directory already exists, MkdirAll does nothing and returns nil.
// 如果目錄已經存在,則MkdirAll啥都不幹
err := fs.MkdirAll(dirname, 0755)
if err != nil {
return nil, err
}
// 創建LOCK文件,並加文件鎖
fileLock, err := fs.Lock(dbFilename(dirname, fileTypeLock, 0))
if err != nil {
return nil, err
}
defer func() {
if fileLock != nil {
fileLock.Close()
}
}()
// 若CURRENT文件不存在,則調用createDB
if _, err := fs.Stat(dbFilename(dirname, fileTypeCurrent, 0)); os.IsNotExist(err) {
// Create the DB if it did not already exist.
if err := createDB(dirname, opts); err != nil {
return nil, err
}
} else if err != nil {
return nil, fmt.Errorf("leveldb: database %q: %v", dirname, err)
} else if opts.GetErrorIfDBExists() {
return nil, fmt.Errorf("leveldb: database %q already exists", dirname)
}
// Load the version set.
// 先讀取CURRENT文件內容,獲取manifest文件名,然後逐條記錄讀取manifest文件的內容,根據內容生成一個新version,放入d.versions中
err = d.versions.load(dirname, opts)
if err != nil {
return nil, err
}
// Replay any newer log files than the ones named in the manifest.
var ve versionEdit
ls, err := fs.List(dirname)
if err != nil {
return nil, err
}
var logFiles fileNumAndNameSlice
for _, filename := range ls {
ft, fn, ok := parseDBFilename(filename)
if ok && ft == fileTypeLog && (fn >= d.versions.logNumber || fn == d.versions.prevLogNumber) {
logFiles = append(logFiles, fileNumAndName{fn, filename})
}
}
sort.Sort(logFiles)
for _, lf := range logFiles {
// 根據日誌文件重做日誌中記錄的操作,先將這些操作記錄存入一個臨時的memtable中,然後轉存入磁盤上level0存儲文件中
maxSeqNum, err := d.replayLogFile(&ve, fs, filepath.Join(dirname, lf.name))
if err != nil {
return nil, err
}
d.versions.markFileNumUsed(lf.num)
// 設置最新的操作序列號
if d.versions.lastSequence < maxSeqNum {
d.versions.lastSequence = maxSeqNum
}
}
// Create an empty .log file.
// 創建一個新的空log文件
ve.logNumber = d.versions.nextFileNum()
d.logNumber = ve.logNumber
//.........這裏部分代碼省略.........
示例7: load
// load loads the version set from the manifest file.
func (vs *versionSet) load(dirname string, opts *db.Options) error {
vs.dirname = dirname
vs.opts = opts
vs.fs = opts.GetFileSystem()
vs.ucmp = opts.GetComparer()
vs.icmp = internalKeyComparer{vs.ucmp}
vs.dummyVersion.prev = &vs.dummyVersion
vs.dummyVersion.next = &vs.dummyVersion
// For historical reasons, the next file number is initialized to 2.
vs.nextFileNumber = 2
// Read the CURRENT file to find the current manifest file.
current, err := vs.fs.Open(dbFilename(dirname, fileTypeCurrent, 0))
if err != nil {
return fmt.Errorf("leveldb: could not open CURRENT file for DB %q: %v", dirname, err)
}
defer current.Close()
stat, err := current.Stat()
if err != nil {
return err
}
n := stat.Size()
if n == 0 {
return fmt.Errorf("leveldb: CURRENT file for DB %q is empty", dirname)
}
if n > 4096 {
return fmt.Errorf("leveldb: CURRENT file for DB %q is too large", dirname)
}
b := make([]byte, n)
_, err = current.ReadAt(b, 0)
if err != nil {
return err
}
if b[n-1] != '\n' {
return fmt.Errorf("leveldb: CURRENT file for DB %q is malformed", dirname)
}
b = b[:n-1]
// Read the versionEdits in the manifest file.
var bve bulkVersionEdit
manifest, err := vs.fs.Open(dirname + string(os.PathSeparator) + string(b))
if err != nil {
return fmt.Errorf("leveldb: could not open manifest file %q for DB %q: %v", b, dirname, err)
}
defer manifest.Close()
rr := record.NewReader(manifest)
for {
r, err := rr.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
var ve versionEdit
err = ve.decode(r)
if err != nil {
return err
}
if ve.comparatorName != "" {
if ve.comparatorName != vs.ucmp.Name() {
return fmt.Errorf("leveldb: manifest file %q for DB %q: "+
"comparer name from file %q != comparer name from db.Options %q",
b, dirname, ve.comparatorName, vs.ucmp.Name())
}
}
bve.accumulate(&ve)
if ve.logNumber != 0 {
vs.logNumber = ve.logNumber
}
if ve.prevLogNumber != 0 {
vs.prevLogNumber = ve.prevLogNumber
}
if ve.nextFileNumber != 0 {
vs.nextFileNumber = ve.nextFileNumber
}
if ve.lastSequence != 0 {
vs.lastSequence = ve.lastSequence
}
}
if vs.logNumber == 0 || vs.nextFileNumber == 0 {
if vs.nextFileNumber == 2 {
// We have a freshly created DB.
} else {
return fmt.Errorf("leveldb: incomplete manifest file %q for DB %q", b, dirname)
}
}
vs.markFileNumUsed(vs.logNumber)
vs.markFileNumUsed(vs.prevLogNumber)
vs.manifestFileNumber = vs.nextFileNum()
newVersion, err := bve.apply(nil, vs.icmp)
if err != nil {
return err
}
vs.append(newVersion)
return nil
}
示例8: Open
// Open opens a LevelDB whose files live in the given directory.
func Open(dirname string, opts *db.Options) (*DB, error) {
d := &DB{
dirname: dirname,
opts: opts,
icmp: internalKeyComparer{opts.GetComparer()},
}
fs := opts.GetFileSystem()
// Lock the database directory.
err := fs.MkdirAll(dirname, 0755)
if err != nil {
return nil, err
}
fileLock, err := fs.Lock(dbFilename(dirname, fileTypeLock, 0))
if err != nil {
return nil, err
}
defer func() {
if fileLock != nil {
fileLock.Close()
}
}()
// TODO: add options for CreateIfMissing and ErrorIfExists, and check them here.
// Load the version set.
err = d.versions.load(dirname, opts)
if err != nil {
return nil, err
}
// Replay any newer log files than the ones named in the manifest.
var ve versionEdit
ls, err := fs.List(dirname)
if err != nil {
return nil, err
}
var logFiles fileNumAndNameSlice
for _, filename := range ls {
n := logFileNum(filename)
if n != 0 && (n >= d.versions.logNumber || n == d.versions.prevLogNumber) {
logFiles = append(logFiles, fileNumAndName{n, filename})
}
}
sort.Sort(logFiles)
for _, lf := range logFiles {
maxSeqNum, err := d.replayLogFile(&ve, fs, filepath.Join(dirname, lf.name))
if err != nil {
return nil, err
}
d.versions.markFileNumUsed(lf.num)
if d.versions.lastSequence < maxSeqNum {
d.versions.lastSequence = maxSeqNum
}
}
// Create an empty .log file.
ve.logNumber = d.versions.nextFileNum()
logFile, err := fs.Create(dbFilename(dirname, fileTypeLog, ve.logNumber))
if err != nil {
return nil, err
}
defer func() {
if logFile != nil {
logFile.Close()
}
}()
d.log = record.NewWriter(logFile)
// Write a new manifest to disk.
if err := d.versions.logAndApply(dirname, &ve); err != nil {
return nil, err
}
// TODO: delete obsolete files.
// TODO: maybe schedule compaction?
d.logFile, logFile = logFile, nil
d.fileLock, fileLock = fileLock, nil
return d, nil
}
示例9: load
// load loads the version set from the manifest file.
// 先讀取CURRENT文件內容,獲取manifest文件名,
func (vs *versionSet) load(dirname string, opts *db.Options) error {
vs.dirname = dirname
vs.opts = opts
vs.fs = opts.GetFileSystem()
vs.ucmp = opts.GetComparer()
vs.icmp = internalKeyComparer{vs.ucmp}
vs.dummyVersion.prev = &vs.dummyVersion
vs.dummyVersion.next = &vs.dummyVersion
// For historical reasons, the next file number is initialized to 2.
vs.nextFileNumber = 2
// Read the CURRENT file to find the current manifest file.
current, err := vs.fs.Open(dbFilename(dirname, fileTypeCurrent, 0))
if err != nil {
return fmt.Errorf("leveldb: could not open CURRENT file for DB %q: %v", dirname, err)
}
defer current.Close()
stat, err := current.Stat()
if err != nil {
return err
}
// 文件大小
n := stat.Size()
if n == 0 {
return fmt.Errorf("leveldb: CURRENT file for DB %q is empty", dirname)
}
if n > 4096 {
return fmt.Errorf("leveldb: CURRENT file for DB %q is too large", dirname)
}
b := make([]byte, n)
// 將文件數據讀取到b中
_, err = current.ReadAt(b, 0)
if err != nil {
return err
}
if b[n-1] != '\n' {
return fmt.Errorf("leveldb: CURRENT file for DB %q is malformed", dirname)
}
// 去除掉最後的換行符
// b即為CURRENT所指向的manifest文件的文件名
b = b[:n-1]
// Read the versionEdits in the manifest file.
var bve bulkVersionEdit
manifest, err := vs.fs.Open(dirname + string(os.PathSeparator) + string(b))
if err != nil {
return fmt.Errorf("leveldb: could not open manifest file %q for DB %q: %v", b, dirname, err)
}
defer manifest.Close()
// 生成一個Reader對象
rr := record.NewReader(manifest)
for {
// Next returns a reader for the next record.
r, err := rr.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
var ve versionEdit
// 將記錄讀入ve
err = ve.decode(r)
if err != nil {
return err
}
if ve.comparatorName != "" {
// 讀和寫的key比較方法必須一致
if ve.comparatorName != vs.ucmp.Name() {
return fmt.Errorf("leveldb: manifest file %q for DB %q: "+
"comparer name from file %q != comparer name from db.Options %q",
b, dirname, ve.comparatorName, vs.ucmp.Name())
}
}
// bve.
bve.accumulate(&ve)
if ve.logNumber != 0 {
vs.logNumber = ve.logNumber
}
if ve.prevLogNumber != 0 {
vs.prevLogNumber = ve.prevLogNumber
}
if ve.nextFileNumber != 0 {
vs.nextFileNumber = ve.nextFileNumber
}
if ve.lastSequence != 0 {
vs.lastSequence = ve.lastSequence
}
}
if vs.logNumber == 0 || vs.nextFileNumber == 0 {
if vs.nextFileNumber == 2 {
// We have a freshly created DB.
} else {
return fmt.Errorf("leveldb: incomplete manifest file %q for DB %q", b, dirname)
}
}
// 生成新版本version之前的準備工作
vs.markFileNumUsed(vs.logNumber)
//.........這裏部分代碼省略.........