当前位置: 首页>>代码示例>>Golang>>正文


Golang memdb.New函数代码示例

本文整理汇总了Golang中github.com/syndtr/goleveldb/leveldb/memdb.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了New函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: newMem

// Create new memdb and froze the old one; need external synchronization.
func (d *DB) newMem() (m *memdb.DB, err error) {
	s := d.s

	num := s.allocFileNum()
	w, err := newJournalWriter(s.getJournalFile(num))
	if err != nil {
		s.reuseFileNum(num)
		return
	}

	old := d.journal
	d.journal = w
	if old != nil {
		old.close()
		d.fjournal = old
	}

	d.fseq = d.seq

	m = memdb.New(s.cmp)
	mem := &memSet{cur: m}
	if old := d.getMem_NB(); old != nil {
		mem.froze = old.cur
	}
	atomic.StorePointer(&d.mem, unsafe.Pointer(mem))

	return
}
开发者ID:29n,项目名称:goleveldb,代码行数:29,代码来源:db_state.go

示例2: newMem

// Create new memdb and froze the old one; need external synchronization.
// newMem only called synchronously by the writer.
func (d *DB) newMem() (mem *memdb.DB, err error) {
	s := d.s

	num := s.allocFileNum()
	file := s.getJournalFile(num)
	w, err := file.Create()
	if err != nil {
		s.reuseFileNum(num)
		return
	}
	d.memMu.Lock()
	if d.journal == nil {
		d.journal = journal.NewWriter(w)
	} else {
		d.journal.Reset(w)
		d.journalWriter.Close()
		d.frozenJournalFile = d.journalFile
	}
	d.journalWriter = w
	d.journalFile = file
	d.frozenMem = d.mem
	d.mem = memdb.New(s.cmp, toPercent(d.s.o.GetWriteBuffer(), kWriteBufferPercent))
	mem = d.mem
	// The seq only incremented by the writer.
	d.frozenSeq = d.seq
	d.memMu.Unlock()
	return
}
开发者ID:paultyng,项目名称:padlock-cloud,代码行数:30,代码来源:db_state.go

示例3: newMem

// Create new memdb and froze the old one; need external synchronization.
// newMem only called synchronously by the writer.
func (db *DB) newMem(n int) (mem *memdb.DB, err error) {
	num := db.s.allocFileNum()
	file := db.s.getJournalFile(num)
	w, err := file.Create()
	if err != nil {
		db.s.reuseFileNum(num)
		return
	}

	db.memMu.Lock()
	defer db.memMu.Unlock()

	if db.journal == nil {
		db.journal = journal.NewWriter(w)
	} else {
		db.journal.Reset(w)
		db.journalWriter.Close()
		db.frozenJournalFile = db.journalFile
	}
	db.journalWriter = w
	db.journalFile = file
	db.frozenMem = db.mem
	db.mem = memdb.New(db.s.icmp, maxInt(db.s.o.GetWriteBuffer(), n))
	mem = db.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
	return
}
开发者ID:retgoat,项目名称:syncthing,代码行数:31,代码来源:db_state.go

示例4: init

func (mc *stConstructor_MergedMemDB) init(t *testing.T, ho *stHarnessOpt) error {
	ho.Randomize = true
	mc.t = t
	for i := range mc.db {
		mc.db[i] = memdb.New(comparer.DefaultComparer, 0)
	}
	return nil
}
开发者ID:pombredanne,项目名称:goleveldb,代码行数:8,代码来源:sorted_test.go

示例5: init

func (p *stConstructor_MergedMemDB) init(t *testing.T, ho *stHarnessOpt) error {
	ho.Randomize = true
	p.t = t
	for i := range p.mem {
		p.mem[i] = memdb.New(comparer.BytesComparer{})
	}
	return nil
}
开发者ID:29n,项目名称:goleveldb,代码行数:8,代码来源:sorted_test.go

示例6: mpoolGet

func (db *DB) mpoolGet(n int) *memDB {
	var mdb *memdb.DB
	select {
	case mdb = <-db.memPool:
	default:
	}
	if mdb == nil || mdb.Capacity() < n {
		mdb = memdb.New(db.s.icmp, maxInt(db.s.o.GetWriteBuffer(), n))
	}
	return &memDB{
		db: db,
		DB: mdb,
	}
}
开发者ID:PrFalken,项目名称:prometheus,代码行数:14,代码来源:db_state.go

示例7: TestBatch_Size

func TestBatch_Size(t *testing.T) {
	b := new(Batch)
	for i := 0; i < 2; i++ {
		b.Put([]byte("key1"), []byte("value1"))
		b.Put([]byte("key2"), []byte("value2"))
		b.Delete([]byte("key1"))
		b.Put([]byte("foo"), []byte("foovalue"))
		b.Put([]byte("bar"), []byte("barvalue"))
		mem := memdb.New(&iComparer{comparer.DefaultComparer}, 0)
		b.memReplay(mem)
		if b.size() != mem.Size() {
			t.Errorf("invalid batch size calculation, want=%d got=%d", mem.Size(), b.size())
		}
		b.Reset()
	}
}
开发者ID:WeavingCode,项目名称:syncthing,代码行数:16,代码来源:batch_test.go

示例8: newMem

// Create new memdb and froze the old one; need external synchronization.
// newMem only called synchronously by the writer.
func (db *DB) newMem(n int) (mem *memDB, err error) {
	num := db.s.allocFileNum()
	file := db.s.getJournalFile(num)
	w, err := file.Create()
	if err != nil {
		db.s.reuseFileNum(num)
		return
	}

	db.memMu.Lock()
	defer db.memMu.Unlock()

	if db.frozenMem != nil {
		panic("still has frozen mem")
	}

	if db.journal == nil {
		db.journal = journal.NewWriter(w)
	} else {
		db.journal.Reset(w)
		db.journalWriter.Close()
		db.frozenJournalFile = db.journalFile
	}
	db.journalWriter = w
	db.journalFile = file
	db.frozenMem = db.mem
	mem, ok := db.memPool.Get().(*memDB)
	if ok && mem.db.Capacity() >= n {
		mem.db.Reset()
		mem.incref()
	} else {
		mem = &memDB{
			pool: db.memPool,
			db:   memdb.New(db.s.icmp, maxInt(db.s.o.GetWriteBuffer(), n)),
			ref:  1,
		}
	}
	mem.incref()
	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
	return
}
开发者ID:Blueprint-Marketing,项目名称:syncthing,代码行数:46,代码来源:db_state.go

示例9: newMem

// Create new memdb and froze the old one; need external synchronization.
// newMem only called synchronously by the writer.
func (db *DB) newMem(n int) (mem *memDB, err error) {
	fd := storage.FileDesc{Type: storage.TypeJournal, Num: db.s.allocFileNum()}
	w, err := db.s.stor.Create(fd)
	if err != nil {
		db.s.reuseFileNum(fd.Num)
		return
	}

	db.memMu.Lock()
	defer db.memMu.Unlock()

	if db.frozenMem != nil {
		panic("still has frozen mem")
	}

	if db.journal == nil {
		db.journal = journal.NewWriter(w)
	} else {
		db.journal.Reset(w)
		db.journalWriter.Close()
		db.frozenJournalFd = db.journalFd
	}
	db.journalWriter = w
	db.journalFd = fd
	db.frozenMem = db.mem
	mdb := db.mpoolGet()
	if mdb == nil || mdb.Capacity() < n {
		mdb = memdb.New(db.s.icmp, maxInt(db.s.o.GetWriteBuffer(), n))
	}
	mem = &memDB{
		db:  db,
		DB:  mdb,
		ref: 2,
	}
	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
	return
}
开发者ID:tendermint,项目名称:functional-tester,代码行数:42,代码来源:db_state.go

示例10: recoverLog

func (d *DB) recoverLog() (err error) {
	s := d.s
	icmp := s.cmp

	s.printf("LogRecovery: started, min=%d", s.stLogNum)

	var mem *memdb.DB
	batch := new(Batch)
	cm := newCMem(s)

	logs, skip := files(s.getFiles(desc.TypeLog)), 0
	logs.sort()
	for _, log := range logs {
		if log.Num() < s.stLogNum {
			skip++
			continue
		}
		s.markFileNum(log.Num())
	}

	var r, fr *logReader
	for _, log := range logs[skip:] {
		s.printf("LogRecovery: recovering, num=%d", log.Num())

		r, err = newLogReader(log, true, s.logDropFunc("log", log.Num()))
		if err != nil {
			return
		}

		if mem != nil {
			if mem.Len() > 0 {
				err = cm.flush(mem, 0)
				if err != nil {
					return
				}
			}

			err = cm.commit(r.file.Num(), d.seq)
			if err != nil {
				return
			}

			cm.reset()

			fr.remove()
			fr = nil
		}

		mem = memdb.New(icmp)

		for r.log.Next() {
			err = batch.decode(r.log.Record())
			if err != nil {
				return
			}

			err = batch.memReplay(mem)
			if err != nil {
				return
			}

			d.seq = batch.seq + uint64(batch.len())

			if mem.Size() > s.o.GetWriteBuffer() {
				// flush to table
				err = cm.flush(mem, 0)
				if err != nil {
					return
				}

				// create new memdb
				mem = memdb.New(icmp)
			}
		}

		err = r.log.Error()
		if err != nil {
			return
		}

		r.close()
		fr = r
	}

	// create new log
	_, err = d.newMem()
	if err != nil {
		return
	}

	if mem != nil && mem.Len() > 0 {
		err = cm.flush(mem, 0)
		if err != nil {
			return
		}
	}

	err = cm.commit(d.log.file.Num(), d.seq)
	if err != nil {
		return
//.........这里部分代码省略.........
开发者ID:yufeng108,项目名称:goleveldb,代码行数:101,代码来源:db.go

示例11: NewMemDbBuffer

// NewMemDbBuffer creates a new memDbBuffer.
func NewMemDbBuffer() MemBuffer {
	return &memDbBuffer{db: memdb.New(comparer.DefaultComparer, 4*1024)}
}
开发者ID:lovedboy,项目名称:tidb,代码行数:4,代码来源:memdb_buffer.go

示例12: IsErrNotFound

// conditionType is the type for condition consts.
type conditionType int

const (
	// conditionIfNotExist means the condition doesn't exist.
	conditionIfNotExist conditionType = iota + 1
	// conditionIfEqual means the condition is equals.
	conditionIfEqual
	// conditionForceSet means the condition is force set.
	conditionForceSet
)

var (
	p = pool.NewCache("memdb pool", 100, func() interface{} {
		return memdb.New(comparer.DefaultComparer, 1*1024*1024)
	})
)

// conditionValue is a data structure used to store current stored data and data verification condition.
type conditionValue struct {
	originValue []byte
	condition   conditionType
}

// IsErrNotFound checks if err is a kind of NotFound error.
func IsErrNotFound(err error) bool {
	if errors2.ErrorEqual(err, leveldb.ErrNotFound) || errors2.ErrorEqual(err, ErrNotExist) {
		return true
	}
开发者ID:Brian110,项目名称:tidb,代码行数:29,代码来源:union_store.go

示例13: recoverJournalRO

func (db *DB) recoverJournalRO() error {
	// Get all journals and sort it by file number.
	rawFds, err := db.s.stor.List(storage.TypeJournal)
	if err != nil {
		return err
	}
	sortFds(rawFds)

	// Journals that will be recovered.
	var fds []storage.FileDesc
	for _, fd := range rawFds {
		if fd.Num >= db.s.stJournalNum || fd.Num == db.s.stPrevJournalNum {
			fds = append(fds, fd)
		}
	}

	var (
		// Options.
		strict      = db.s.o.GetStrict(opt.StrictJournal)
		checksum    = db.s.o.GetStrict(opt.StrictJournalChecksum)
		writeBuffer = db.s.o.GetWriteBuffer()

		mdb = memdb.New(db.s.icmp, writeBuffer)
	)

	// Recover journals.
	if len(fds) > 0 {
		db.logf("[email protected] RO·Mode F·%d", len(fds))

		var (
			jr    *journal.Reader
			buf   = &util.Buffer{}
			batch = &Batch{}
		)

		for _, fd := range fds {
			db.logf("[email protected] recovering @%d", fd.Num)

			fr, err := db.s.stor.Open(fd)
			if err != nil {
				return err
			}

			// Create or reset journal reader instance.
			if jr == nil {
				jr = journal.NewReader(fr, dropper{db.s, fd}, strict, checksum)
			} else {
				jr.Reset(fr, dropper{db.s, fd}, strict, checksum)
			}

			// Replay journal to memdb.
			for {
				r, err := jr.Next()
				if err != nil {
					if err == io.EOF {
						break
					}

					fr.Close()
					return errors.SetFd(err, fd)
				}

				buf.Reset()
				if _, err := buf.ReadFrom(r); err != nil {
					if err == io.ErrUnexpectedEOF {
						// This is error returned due to corruption, with strict == false.
						continue
					}

					fr.Close()
					return errors.SetFd(err, fd)
				}
				if err := batch.memDecodeAndReplay(db.seq, buf.Bytes(), mdb); err != nil {
					if !strict && errors.IsCorrupted(err) {
						db.s.logf("journal error: %v (skipped)", err)
						// We won't apply sequence number as it might be corrupted.
						continue
					}

					fr.Close()
					return errors.SetFd(err, fd)
				}

				// Save sequence number.
				db.seq = batch.seq + uint64(batch.Len())
			}

			fr.Close()
		}
	}

	// Set memDB.
	db.mem = &memDB{db: db, DB: mdb, ref: 1}

	return nil
}
开发者ID:KosyanMedia,项目名称:burlesque,代码行数:96,代码来源:db.go

示例14: recoverJournal

func (d *DB) recoverJournal() (err error) {
	s := d.s
	icmp := s.cmp

	s.printf("JournalRecovery: started, min=%d", s.stJournalNum)

	var mem *memdb.DB
	batch := new(Batch)
	cm := newCMem(s)

	journals := files(s.getFiles(storage.TypeJournal))
	journals.sort()
	rJournals := make([]storage.File, 0, len(journals))
	for _, journal := range journals {
		if journal.Num() >= s.stJournalNum || journal.Num() == s.stPrevJournalNum {
			s.markFileNum(journal.Num())
			rJournals = append(rJournals, journal)
		}
	}

	var r, fr *journalReader
	for _, journal := range rJournals {
		s.printf("JournalRecovery: recovering, num=%d", journal.Num())

		r, err = newJournalReader(journal, true, s.journalDropFunc("journal", journal.Num()))
		if err != nil {
			return
		}

		if mem != nil {
			if mem.Len() > 0 {
				err = cm.flush(mem, 0)
				if err != nil {
					return
				}
			}

			err = cm.commit(r.file.Num(), d.seq)
			if err != nil {
				return
			}

			cm.reset()

			fr.remove()
			fr = nil
		}

		mem = memdb.New(icmp)

		for r.journal.Next() {
			err = batch.decode(r.journal.Record())
			if err != nil {
				return
			}

			err = batch.memReplay(mem)
			if err != nil {
				return
			}

			d.seq = batch.seq + uint64(batch.len())

			if mem.Size() > s.o.GetWriteBuffer() {
				// flush to table
				err = cm.flush(mem, 0)
				if err != nil {
					return
				}

				// create new memdb
				mem = memdb.New(icmp)
			}
		}

		err = r.journal.Error()
		if err != nil {
			return
		}

		r.close()
		fr = r
	}

	// create new journal
	_, err = d.newMem()
	if err != nil {
		return
	}

	if mem != nil && mem.Len() > 0 {
		err = cm.flush(mem, 0)
		if err != nil {
			return
		}
	}

	err = cm.commit(d.journal.file.Num(), d.seq)
	if err != nil {
		return
//.........这里部分代码省略.........
开发者ID:JensRantil,项目名称:goleveldb,代码行数:101,代码来源:db.go

示例15: NewMemoryKeyValue

// NewMemoryKeyValue returns a KeyValue implementation that's backed only
// by memory. It's mostly useful for tests and development.
func NewMemoryKeyValue() KeyValue {
	db := memdb.New(comparer.DefaultComparer, 128)
	return &memKeys{db: db}
}
开发者ID:pombredanne,项目名称:camlistore,代码行数:6,代码来源:mem.go


注:本文中的github.com/syndtr/goleveldb/leveldb/memdb.New函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。