2014-07-06 05:46:48 -07:00
|
|
|
// Copyright (c) 2012, Suryandaru Triandana <syndtr@gmail.com>
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
package leveldb
|
|
|
|
|
|
|
|
import (
|
2014-11-18 05:24:42 -07:00
|
|
|
"fmt"
|
2014-07-06 05:46:48 -07:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
|
2014-11-18 05:24:42 -07:00
|
|
|
"github.com/syndtr/goleveldb/leveldb/errors"
|
2014-07-06 05:46:48 -07:00
|
|
|
"github.com/syndtr/goleveldb/leveldb/journal"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/opt"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/storage"
|
|
|
|
)
|
|
|
|
|
2016-03-05 13:01:58 -07:00
|
|
|
// ErrManifestCorrupted records manifest corruption.
|
2014-11-18 05:24:42 -07:00
|
|
|
type ErrManifestCorrupted struct {
|
|
|
|
Field string
|
|
|
|
Reason string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ErrManifestCorrupted) Error() string {
|
|
|
|
return fmt.Sprintf("leveldb: manifest corrupted (field '%s'): %s", e.Field, e.Reason)
|
|
|
|
}
|
|
|
|
|
2016-03-05 13:01:58 -07:00
|
|
|
func newErrManifestCorrupted(fd storage.FileDesc, field, reason string) error {
|
|
|
|
return errors.NewErrCorrupted(fd, &ErrManifestCorrupted{field, reason})
|
2014-11-18 05:24:42 -07:00
|
|
|
}
|
|
|
|
|
2014-07-06 05:46:48 -07:00
|
|
|
// session represent a persistent database session.
|
|
|
|
type session struct {
|
|
|
|
// Need 64-bit alignment.
|
2016-03-05 13:01:58 -07:00
|
|
|
stNextFileNum int64 // current unused file number
|
|
|
|
stJournalNum int64 // current journal file number; need external synchronization
|
|
|
|
stPrevJournalNum int64 // prev journal file number; no longer used; for compatibility with older version of leveldb
|
|
|
|
stTempFileNum int64
|
2014-11-18 05:24:42 -07:00
|
|
|
stSeqNum uint64 // last mem compacted seq; need external synchronization
|
2014-07-06 05:46:48 -07:00
|
|
|
|
|
|
|
stor storage.Storage
|
2016-03-05 13:01:58 -07:00
|
|
|
storLock storage.Lock
|
2014-11-18 05:24:42 -07:00
|
|
|
o *cachedOptions
|
2014-07-06 14:13:10 -07:00
|
|
|
icmp *iComparer
|
2014-07-06 05:46:48 -07:00
|
|
|
tops *tOps
|
|
|
|
|
|
|
|
manifest *journal.Writer
|
|
|
|
manifestWriter storage.Writer
|
2016-03-05 13:01:58 -07:00
|
|
|
manifestFd storage.FileDesc
|
2014-07-06 05:46:48 -07:00
|
|
|
|
2016-03-05 13:01:58 -07:00
|
|
|
stCompPtrs []internalKey // compaction pointers; need external synchronization
|
|
|
|
stVersion *version // current version
|
2014-11-18 05:24:42 -07:00
|
|
|
vmu sync.Mutex
|
2014-07-06 05:46:48 -07:00
|
|
|
}
|
|
|
|
|
2014-07-22 23:31:36 -07:00
|
|
|
// Creates new initialized session instance.
|
2014-07-06 05:46:48 -07:00
|
|
|
func newSession(stor storage.Storage, o *opt.Options) (s *session, err error) {
|
|
|
|
if stor == nil {
|
|
|
|
return nil, os.ErrInvalid
|
|
|
|
}
|
|
|
|
storLock, err := stor.Lock()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s = &session{
|
2016-03-05 13:01:58 -07:00
|
|
|
stor: stor,
|
|
|
|
storLock: storLock,
|
2014-07-06 05:46:48 -07:00
|
|
|
}
|
|
|
|
s.setOptions(o)
|
2014-12-29 04:22:58 -07:00
|
|
|
s.tops = newTableOps(s)
|
2014-11-18 05:24:42 -07:00
|
|
|
s.setVersion(newVersion(s))
|
|
|
|
s.log("log@legend F·NumFile S·FileSize N·Entry C·BadEntry B·BadBlock Ke·KeyError D·DroppedEntry L·Level Q·SeqNum T·TimeElapsed")
|
2014-07-06 05:46:48 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close session.
|
|
|
|
func (s *session) close() {
|
|
|
|
s.tops.close()
|
|
|
|
if s.manifest != nil {
|
|
|
|
s.manifest.Close()
|
|
|
|
}
|
|
|
|
if s.manifestWriter != nil {
|
|
|
|
s.manifestWriter.Close()
|
|
|
|
}
|
|
|
|
s.manifest = nil
|
|
|
|
s.manifestWriter = nil
|
|
|
|
s.stVersion = nil
|
|
|
|
}
|
|
|
|
|
2014-07-22 23:31:36 -07:00
|
|
|
// Release session lock.
|
2014-07-06 05:46:48 -07:00
|
|
|
func (s *session) release() {
|
|
|
|
s.storLock.Release()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new database session; need external synchronization.
|
|
|
|
func (s *session) create() error {
|
|
|
|
// create manifest
|
|
|
|
return s.newManifest(nil, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recover a database session; need external synchronization.
|
|
|
|
func (s *session) recover() (err error) {
|
|
|
|
defer func() {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// Don't return os.ErrNotExist if the underlying storage contains
|
|
|
|
// other files that belong to LevelDB. So the DB won't get trashed.
|
2016-03-05 13:01:58 -07:00
|
|
|
if fds, _ := s.stor.List(storage.TypeAll); len(fds) > 0 {
|
|
|
|
err = &errors.ErrCorrupted{Fd: storage.FileDesc{Type: storage.TypeManifest}, Err: &errors.ErrMissingFiles{}}
|
2014-07-06 05:46:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-03-05 13:01:58 -07:00
|
|
|
fd, err := s.stor.GetMeta()
|
2014-07-06 05:46:48 -07:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-05 13:01:58 -07:00
|
|
|
reader, err := s.stor.Open(fd)
|
2014-07-06 05:46:48 -07:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer reader.Close()
|
|
|
|
|
2015-06-15 12:10:18 -07:00
|
|
|
var (
|
|
|
|
// Options.
|
2016-03-05 13:01:58 -07:00
|
|
|
strict = s.o.GetStrict(opt.StrictManifest)
|
2015-06-15 12:10:18 -07:00
|
|
|
|
2016-03-05 13:01:58 -07:00
|
|
|
jr = journal.NewReader(reader, dropper{s, fd}, strict, true)
|
2015-06-15 12:10:18 -07:00
|
|
|
rec = &sessionRecord{}
|
|
|
|
staging = s.stVersion.newStaging()
|
|
|
|
)
|
2014-07-06 05:46:48 -07:00
|
|
|
for {
|
|
|
|
var r io.Reader
|
|
|
|
r, err = jr.Next()
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
err = nil
|
|
|
|
break
|
|
|
|
}
|
2016-03-05 13:01:58 -07:00
|
|
|
return errors.SetFd(err, fd)
|
2014-07-06 05:46:48 -07:00
|
|
|
}
|
|
|
|
|
2016-03-05 13:01:58 -07:00
|
|
|
err = rec.decode(r)
|
2014-07-06 05:46:48 -07:00
|
|
|
if err == nil {
|
|
|
|
// save compact pointers
|
2014-11-18 05:24:42 -07:00
|
|
|
for _, r := range rec.compPtrs {
|
2016-03-05 13:01:58 -07:00
|
|
|
s.setCompPtr(r.level, internalKey(r.ikey))
|
2014-07-06 05:46:48 -07:00
|
|
|
}
|
|
|
|
// commit record to version staging
|
|
|
|
staging.commit(rec)
|
|
|
|
} else {
|
2016-03-05 13:01:58 -07:00
|
|
|
err = errors.SetFd(err, fd)
|
2014-11-18 05:24:42 -07:00
|
|
|
if strict || !errors.IsCorrupted(err) {
|
|
|
|
return
|
|
|
|
}
|
2016-03-05 13:01:58 -07:00
|
|
|
s.logf("manifest error: %v (skipped)", errors.SetFd(err, fd))
|
2014-07-06 05:46:48 -07:00
|
|
|
}
|
2014-11-18 05:24:42 -07:00
|
|
|
rec.resetCompPtrs()
|
2014-07-06 05:46:48 -07:00
|
|
|
rec.resetAddedTables()
|
|
|
|
rec.resetDeletedTables()
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case !rec.has(recComparer):
|
2016-03-05 13:01:58 -07:00
|
|
|
return newErrManifestCorrupted(fd, "comparer", "missing")
|
2014-07-06 14:13:10 -07:00
|
|
|
case rec.comparer != s.icmp.uName():
|
2016-03-05 13:01:58 -07:00
|
|
|
return newErrManifestCorrupted(fd, "comparer", fmt.Sprintf("mismatch: want '%s', got '%s'", s.icmp.uName(), rec.comparer))
|
2014-11-18 05:24:42 -07:00
|
|
|
case !rec.has(recNextFileNum):
|
2016-03-05 13:01:58 -07:00
|
|
|
return newErrManifestCorrupted(fd, "next-file-num", "missing")
|
2014-07-06 05:46:48 -07:00
|
|
|
case !rec.has(recJournalNum):
|
2016-03-05 13:01:58 -07:00
|
|
|
return newErrManifestCorrupted(fd, "journal-file-num", "missing")
|
2014-11-18 05:24:42 -07:00
|
|
|
case !rec.has(recSeqNum):
|
2016-03-05 13:01:58 -07:00
|
|
|
return newErrManifestCorrupted(fd, "seq-num", "missing")
|
2014-07-06 05:46:48 -07:00
|
|
|
}
|
|
|
|
|
2016-03-05 13:01:58 -07:00
|
|
|
s.manifestFd = fd
|
2014-07-06 05:46:48 -07:00
|
|
|
s.setVersion(staging.finish())
|
2014-11-18 05:24:42 -07:00
|
|
|
s.setNextFileNum(rec.nextFileNum)
|
2014-07-06 05:46:48 -07:00
|
|
|
s.recordCommited(rec)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit session; need external synchronization.
|
|
|
|
func (s *session) commit(r *sessionRecord) (err error) {
|
2014-11-18 05:24:42 -07:00
|
|
|
v := s.version()
|
|
|
|
defer v.release()
|
|
|
|
|
2014-07-06 05:46:48 -07:00
|
|
|
// spawn new version based on current version
|
2014-11-18 05:24:42 -07:00
|
|
|
nv := v.spawn(r)
|
2014-07-06 05:46:48 -07:00
|
|
|
|
|
|
|
if s.manifest == nil {
|
|
|
|
// manifest journal writer not yet created, create one
|
|
|
|
err = s.newManifest(r, nv)
|
|
|
|
} else {
|
|
|
|
err = s.flushManifest(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// finally, apply new version if no error rise
|
|
|
|
if err == nil {
|
|
|
|
s.setVersion(nv)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|