2015-02-16 15:28:33 +02:00
|
|
|
// Copyright (c) 2013, 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 (
|
2017-09-05 16:04:32 +03:00
|
|
|
"errors"
|
2015-02-16 15:28:33 +02:00
|
|
|
"sync/atomic"
|
2015-04-28 12:18:01 +03:00
|
|
|
"time"
|
2015-02-16 15:28:33 +02:00
|
|
|
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/journal"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/memdb"
|
2016-02-11 16:16:52 +02:00
|
|
|
"github.com/syndtr/goleveldb/leveldb/storage"
|
2015-02-16 15:28:33 +02:00
|
|
|
)
|
|
|
|
|
2017-09-05 16:04:32 +03:00
|
|
|
var (
|
|
|
|
errHasFrozenMem = errors.New("has frozen mem")
|
|
|
|
)
|
|
|
|
|
2015-04-28 12:18:01 +03:00
|
|
|
type memDB struct {
|
2016-02-11 16:16:52 +02:00
|
|
|
db *DB
|
|
|
|
*memdb.DB
|
2015-04-28 12:18:01 +03:00
|
|
|
ref int32
|
|
|
|
}
|
|
|
|
|
2016-02-11 16:16:52 +02:00
|
|
|
func (m *memDB) getref() int32 {
|
|
|
|
return atomic.LoadInt32(&m.ref)
|
|
|
|
}
|
|
|
|
|
2015-04-28 12:18:01 +03:00
|
|
|
func (m *memDB) incref() {
|
|
|
|
atomic.AddInt32(&m.ref, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memDB) decref() {
|
|
|
|
if ref := atomic.AddInt32(&m.ref, -1); ref == 0 {
|
|
|
|
// Only put back memdb with std capacity.
|
2016-02-11 16:16:52 +02:00
|
|
|
if m.Capacity() == m.db.s.o.GetWriteBuffer() {
|
|
|
|
m.Reset()
|
|
|
|
m.db.mpoolPut(m.DB)
|
2015-04-28 12:18:01 +03:00
|
|
|
}
|
|
|
|
m.db = nil
|
2016-02-11 16:16:52 +02:00
|
|
|
m.DB = nil
|
2015-04-28 12:18:01 +03:00
|
|
|
} else if ref < 0 {
|
|
|
|
panic("negative memdb ref")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-16 15:28:33 +02:00
|
|
|
// Get latest sequence number.
|
2015-04-28 12:18:01 +03:00
|
|
|
func (db *DB) getSeq() uint64 {
|
|
|
|
return atomic.LoadUint64(&db.seq)
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Atomically adds delta to seq.
|
2015-04-28 12:18:01 +03:00
|
|
|
func (db *DB) addSeq(delta uint64) {
|
|
|
|
atomic.AddUint64(&db.seq, delta)
|
|
|
|
}
|
|
|
|
|
2016-02-11 16:16:52 +02:00
|
|
|
func (db *DB) setSeq(seq uint64) {
|
|
|
|
atomic.StoreUint64(&db.seq, seq)
|
|
|
|
}
|
|
|
|
|
2016-03-10 11:39:20 +02:00
|
|
|
func (db *DB) sampleSeek(ikey internalKey) {
|
2015-04-28 12:18:01 +03:00
|
|
|
v := db.s.version()
|
|
|
|
if v.sampleSeek(ikey) {
|
|
|
|
// Trigger table compaction.
|
2016-02-11 16:16:52 +02:00
|
|
|
db.compTrigger(db.tcompCmdC)
|
2015-04-28 12:18:01 +03:00
|
|
|
}
|
|
|
|
v.release()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) mpoolPut(mem *memdb.DB) {
|
2016-10-20 15:56:34 +03:00
|
|
|
if !db.isClosed() {
|
|
|
|
select {
|
|
|
|
case db.memPool <- mem:
|
|
|
|
default:
|
|
|
|
}
|
2015-04-28 12:18:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-11 16:16:52 +02:00
|
|
|
func (db *DB) mpoolGet(n int) *memDB {
|
|
|
|
var mdb *memdb.DB
|
2015-04-28 12:18:01 +03:00
|
|
|
select {
|
2016-02-11 16:16:52 +02:00
|
|
|
case mdb = <-db.memPool:
|
2015-04-28 12:18:01 +03:00
|
|
|
default:
|
2016-02-11 16:16:52 +02:00
|
|
|
}
|
|
|
|
if mdb == nil || mdb.Capacity() < n {
|
|
|
|
mdb = memdb.New(db.s.icmp, maxInt(db.s.o.GetWriteBuffer(), n))
|
|
|
|
}
|
|
|
|
return &memDB{
|
|
|
|
db: db,
|
|
|
|
DB: mdb,
|
2015-04-28 12:18:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) mpoolDrain() {
|
|
|
|
ticker := time.NewTicker(30 * time.Second)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
select {
|
|
|
|
case <-db.memPool:
|
|
|
|
default:
|
|
|
|
}
|
2016-10-20 15:56:34 +03:00
|
|
|
case <-db.closeC:
|
|
|
|
ticker.Stop()
|
|
|
|
// Make sure the pool is drained.
|
|
|
|
select {
|
|
|
|
case <-db.memPool:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
}
|
2015-04-28 12:18:01 +03:00
|
|
|
close(db.memPool)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create new memdb and froze the old one; need external synchronization.
|
|
|
|
// newMem only called synchronously by the writer.
|
2015-04-28 12:18:01 +03:00
|
|
|
func (db *DB) newMem(n int) (mem *memDB, err error) {
|
2016-02-11 16:16:52 +02:00
|
|
|
fd := storage.FileDesc{Type: storage.TypeJournal, Num: db.s.allocFileNum()}
|
|
|
|
w, err := db.s.stor.Create(fd)
|
2015-02-16 15:28:33 +02:00
|
|
|
if err != nil {
|
2016-02-11 16:16:52 +02:00
|
|
|
db.s.reuseFileNum(fd.Num)
|
2015-02-16 15:28:33 +02:00
|
|
|
return
|
|
|
|
}
|
2015-04-28 12:18:01 +03:00
|
|
|
|
|
|
|
db.memMu.Lock()
|
|
|
|
defer db.memMu.Unlock()
|
|
|
|
|
|
|
|
if db.frozenMem != nil {
|
2017-09-05 16:04:32 +03:00
|
|
|
return nil, errHasFrozenMem
|
2015-04-28 12:18:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if db.journal == nil {
|
|
|
|
db.journal = journal.NewWriter(w)
|
2015-02-16 15:28:33 +02:00
|
|
|
} else {
|
2015-04-28 12:18:01 +03:00
|
|
|
db.journal.Reset(w)
|
|
|
|
db.journalWriter.Close()
|
2016-02-11 16:16:52 +02:00
|
|
|
db.frozenJournalFd = db.journalFd
|
2015-04-28 12:18:01 +03:00
|
|
|
}
|
|
|
|
db.journalWriter = w
|
2016-02-11 16:16:52 +02:00
|
|
|
db.journalFd = fd
|
2015-04-28 12:18:01 +03:00
|
|
|
db.frozenMem = db.mem
|
2016-02-11 16:16:52 +02:00
|
|
|
mem = db.mpoolGet(n)
|
|
|
|
mem.incref() // for self
|
|
|
|
mem.incref() // for caller
|
2015-04-28 12:18:01 +03:00
|
|
|
db.mem = mem
|
|
|
|
// The seq only incremented by the writer. And whoever called newMem
|
|
|
|
// should hold write lock, so no need additional synchronization here.
|
|
|
|
db.frozenSeq = db.seq
|
2015-02-16 15:28:33 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all memdbs.
|
2015-04-28 12:18:01 +03:00
|
|
|
func (db *DB) getMems() (e, f *memDB) {
|
|
|
|
db.memMu.RLock()
|
|
|
|
defer db.memMu.RUnlock()
|
2016-10-20 15:56:34 +03:00
|
|
|
if db.mem != nil {
|
|
|
|
db.mem.incref()
|
|
|
|
} else if !db.isClosed() {
|
2015-04-28 12:18:01 +03:00
|
|
|
panic("nil effective mem")
|
|
|
|
}
|
|
|
|
if db.frozenMem != nil {
|
|
|
|
db.frozenMem.incref()
|
|
|
|
}
|
|
|
|
return db.mem, db.frozenMem
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
|
2016-10-20 15:56:34 +03:00
|
|
|
// Get effective memdb.
|
2015-04-28 12:18:01 +03:00
|
|
|
func (db *DB) getEffectiveMem() *memDB {
|
|
|
|
db.memMu.RLock()
|
|
|
|
defer db.memMu.RUnlock()
|
2016-10-20 15:56:34 +03:00
|
|
|
if db.mem != nil {
|
|
|
|
db.mem.incref()
|
|
|
|
} else if !db.isClosed() {
|
2015-04-28 12:18:01 +03:00
|
|
|
panic("nil effective mem")
|
|
|
|
}
|
|
|
|
return db.mem
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether we has frozen memdb.
|
2015-04-28 12:18:01 +03:00
|
|
|
func (db *DB) hasFrozenMem() bool {
|
|
|
|
db.memMu.RLock()
|
|
|
|
defer db.memMu.RUnlock()
|
|
|
|
return db.frozenMem != nil
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get frozen memdb.
|
2015-04-28 12:18:01 +03:00
|
|
|
func (db *DB) getFrozenMem() *memDB {
|
|
|
|
db.memMu.RLock()
|
|
|
|
defer db.memMu.RUnlock()
|
|
|
|
if db.frozenMem != nil {
|
|
|
|
db.frozenMem.incref()
|
|
|
|
}
|
|
|
|
return db.frozenMem
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Drop frozen memdb; assume that frozen memdb isn't nil.
|
2015-04-28 12:18:01 +03:00
|
|
|
func (db *DB) dropFrozenMem() {
|
|
|
|
db.memMu.Lock()
|
2016-02-11 16:16:52 +02:00
|
|
|
if err := db.s.stor.Remove(db.frozenJournalFd); err != nil {
|
|
|
|
db.logf("journal@remove removing @%d %q", db.frozenJournalFd.Num, err)
|
2015-02-16 15:28:33 +02:00
|
|
|
} else {
|
2016-02-11 16:16:52 +02:00
|
|
|
db.logf("journal@remove removed @%d", db.frozenJournalFd.Num)
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
2016-02-11 16:16:52 +02:00
|
|
|
db.frozenJournalFd = storage.FileDesc{}
|
2015-04-28 12:18:01 +03:00
|
|
|
db.frozenMem.decref()
|
|
|
|
db.frozenMem = nil
|
|
|
|
db.memMu.Unlock()
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
|
2016-10-20 15:56:34 +03:00
|
|
|
// Clear mems ptr; used by DB.Close().
|
|
|
|
func (db *DB) clearMems() {
|
|
|
|
db.memMu.Lock()
|
|
|
|
db.mem = nil
|
|
|
|
db.frozenMem = nil
|
|
|
|
db.memMu.Unlock()
|
|
|
|
}
|
|
|
|
|
2015-02-16 15:28:33 +02:00
|
|
|
// Set closed flag; return true if not already closed.
|
2015-04-28 12:18:01 +03:00
|
|
|
func (db *DB) setClosed() bool {
|
|
|
|
return atomic.CompareAndSwapUint32(&db.closed, 0, 1)
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether DB was closed.
|
2015-04-28 12:18:01 +03:00
|
|
|
func (db *DB) isClosed() bool {
|
|
|
|
return atomic.LoadUint32(&db.closed) != 0
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check read ok status.
|
2015-04-28 12:18:01 +03:00
|
|
|
func (db *DB) ok() error {
|
|
|
|
if db.isClosed() {
|
2015-02-16 15:28:33 +02:00
|
|
|
return ErrClosed
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|