prometheus/vendor/github.com/syndtr/goleveldb/leveldb/db_util.go

103 lines
2.4 KiB
Go
Raw Normal View History

2015-09-10 07:17:51 -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 (
"github.com/syndtr/goleveldb/leveldb/errors"
"github.com/syndtr/goleveldb/leveldb/iterator"
"github.com/syndtr/goleveldb/leveldb/opt"
"github.com/syndtr/goleveldb/leveldb/storage"
"github.com/syndtr/goleveldb/leveldb/util"
)
// Reader is the interface that wraps basic Get and NewIterator methods.
// This interface implemented by both DB and Snapshot.
type Reader interface {
Get(key []byte, ro *opt.ReadOptions) (value []byte, err error)
NewIterator(slice *util.Range, ro *opt.ReadOptions) iterator.Iterator
}
2016-07-04 01:08:49 -07:00
// Sizes is list of size.
type Sizes []int64
2015-09-10 07:17:51 -07:00
// Sum returns sum of the sizes.
2016-07-04 01:08:49 -07:00
func (sizes Sizes) Sum() int64 {
var sum int64
for _, size := range sizes {
sum += size
2015-09-10 07:17:51 -07:00
}
2016-07-04 01:08:49 -07:00
return sum
2015-09-10 07:17:51 -07:00
}
// Logging.
func (db *DB) log(v ...interface{}) { db.s.log(v...) }
func (db *DB) logf(format string, v ...interface{}) { db.s.logf(format, v...) }
// Check and clean files.
func (db *DB) checkAndCleanFiles() error {
v := db.s.version()
defer v.release()
2016-07-04 01:08:49 -07:00
tmap := make(map[int64]bool)
for _, tables := range v.levels {
2015-09-10 07:17:51 -07:00
for _, t := range tables {
2016-07-04 01:08:49 -07:00
tmap[t.fd.Num] = false
2015-09-10 07:17:51 -07:00
}
}
2016-07-04 01:08:49 -07:00
fds, err := db.s.stor.List(storage.TypeAll)
2015-09-10 07:17:51 -07:00
if err != nil {
return err
}
2016-07-04 01:08:49 -07:00
var nt int
var rem []storage.FileDesc
for _, fd := range fds {
2015-09-10 07:17:51 -07:00
keep := true
2016-07-04 01:08:49 -07:00
switch fd.Type {
2015-09-10 07:17:51 -07:00
case storage.TypeManifest:
2016-07-04 01:08:49 -07:00
keep = fd.Num >= db.s.manifestFd.Num
2015-09-10 07:17:51 -07:00
case storage.TypeJournal:
2016-07-04 01:08:49 -07:00
if !db.frozenJournalFd.Nil() {
keep = fd.Num >= db.frozenJournalFd.Num
2015-09-10 07:17:51 -07:00
} else {
2016-07-04 01:08:49 -07:00
keep = fd.Num >= db.journalFd.Num
2015-09-10 07:17:51 -07:00
}
case storage.TypeTable:
2016-07-04 01:08:49 -07:00
_, keep = tmap[fd.Num]
2015-09-10 07:17:51 -07:00
if keep {
2016-07-04 01:08:49 -07:00
tmap[fd.Num] = true
nt++
2015-09-10 07:17:51 -07:00
}
}
if !keep {
2016-07-04 01:08:49 -07:00
rem = append(rem, fd)
2015-09-10 07:17:51 -07:00
}
}
2016-07-04 01:08:49 -07:00
if nt != len(tmap) {
var mfds []storage.FileDesc
for num, present := range tmap {
2015-09-10 07:17:51 -07:00
if !present {
2016-07-04 01:08:49 -07:00
mfds = append(mfds, storage.FileDesc{storage.TypeTable, num})
2015-09-10 07:17:51 -07:00
db.logf("db@janitor table missing @%d", num)
}
}
2016-07-04 01:08:49 -07:00
return errors.NewErrCorrupted(storage.FileDesc{}, &errors.ErrMissingFiles{Fds: mfds})
2015-09-10 07:17:51 -07:00
}
2016-07-04 01:08:49 -07:00
db.logf("db@janitor F·%d G·%d", len(fds), len(rem))
for _, fd := range rem {
db.logf("db@janitor removing %s-%d", fd.Type, fd.Num)
if err := db.s.stor.Remove(fd); err != nil {
2015-09-10 07:17:51 -07:00
return err
}
}
return nil
}