예제 #1
0
func BenchmarkDBReadReverse(b *testing.B) {
	p := openDBBench(b)
	p.populate(b.N)
	p.fill()
	p.drop()

	iter := p.newIter()
	b.ResetTimer()
	iter.Last()
	for iter.Prev() {
	}
	b.StopTimer()
	b.SetBytes(116)
	p.close()
}
예제 #2
0
파일: db.go 프로젝트: yufeng108/goleveldb
// Recover recover database with missing or corrupted manifest file. It will
// ignore any manifest files, valid or not.
func Recover(d desc.Desc, o *opt.Options) (db *DB, err error) {
	s := newSession(d, o)

	// get all files
	ff := files(s.getFiles(desc.TypeAll))
	ff.sort()

	s.printf("Recover: started, files=%d", len(ff))

	rec := new(sessionRecord)

	// recover tables
	ro := &opt.ReadOptions{}
	var nt *tFile
	for _, f := range ff {
		if f.Type() != desc.TypeTable {
			continue
		}

		var size uint64
		size, err = f.Size()
		if err != nil {
			return
		}

		t := newTFile(f, size, nil, nil)
		iter := s.tops.newIterator(t, ro)
		// min ikey
		if iter.First() {
			t.min = iter.Key()
		} else if iter.Error() != nil {
			err = iter.Error()
			return
		} else {
			continue
		}
		// max ikey
		if iter.Last() {
			t.max = iter.Key()
		} else if iter.Error() != nil {
			err = iter.Error()
			return
		} else {
			continue
		}

		// add table to level 0
		rec.addTableFile(0, t)

		nt = t
	}

	// extract largest seq number from newest table
	if nt != nil {
		var lseq uint64
		iter := s.tops.newIterator(nt, ro)
		for iter.Next() {
			seq, _, ok := iKey(iter.Key()).parseNum()
			if !ok {
				continue
			}
			if seq > lseq {
				lseq = seq
			}
		}
		rec.setSeq(lseq)
	}

	// set file num based on largest one
	s.stFileNum = ff[len(ff)-1].Num() + 1

	// create brand new manifest
	err = s.create()
	if err != nil {
		return
	}
	// commit record
	err = s.commit(rec)
	if err != nil {
		return
	}

	return open(s)
}