示例#1
0
func (t *treeCache) getTree(db *DB, prefix int, name string, canCreate bool, cacheSize int) (r *lldb.BTree, err error) {
	m := t.get()
	r, ok := m[name]
	if ok {
		return
	}

	root, err := db.root()
	if err != nil {
		return
	}

	val, err := root.get(prefix, name)
	if err != nil {
		return
	}

	switch x := val.(type) {
	case nil:
		if !canCreate {
			return
		}

		var h int64
		r, h, err = lldb.CreateBTree(db.alloc, collate)
		if err != nil {
			return nil, err
		}

		if err = root.set(h, prefix, name); err != nil {
			return nil, err
		}
	case int64:
		if r, err = lldb.OpenBTree(db.alloc, collate, x); err != nil {
			return nil, err
		}
	default:
		return nil, &lldb.ErrINVAL{Src: "corrupted root directory value for", Val: fmt.Sprintf("%q, %q", prefix, name)}
	}

	if len(m) > cacheSize {
		i, j, n := 0, cacheSize/2, mathutil.Min(cacheSize/20, 10)
	loop:
		for k := range m {
			if i++; i >= j {
				delete(m, k)
				if n == 0 {
					break loop
				}

				n--
			}
		}
	}

	m[name] = r
	return
}
示例#2
0
func verifyAllocator(a *lldb.Allocator) error {
	bits, err := ioutil.TempFile("", "kv-verify-")
	if err != nil {
		return err
	}

	defer func() {
		nm := bits.Name()
		bits.Close()
		os.Remove(nm)
	}()

	var lerr error
	if err = a.Verify(
		lldb.NewSimpleFileFiler(bits),
		func(err error) bool {
			lerr = err
			return false
		},
		nil,
	); err != nil {
		return err
	}

	if lerr != nil {
		return lerr
	}

	t, err := lldb.OpenBTree(a, nil, 1)
	if err != nil {
		return err
	}

	e, err := t.SeekFirst()
	if err != nil {
		if err == io.EOF {
			err = nil
		}
		return err
	}

	for {
		_, _, err := e.Next()
		if err != nil {
			if err == io.EOF {
				err = nil
			}
			return err
		}
	}
}
示例#3
0
func (db *DB) root() (r *Array, err error) {
	if r = db._root; r != nil {
		return
	}

	sz, err := db.filer.Size()
	if err != nil {
		return
	}

	switch {
	case sz < db.emptySize:
		panic(fmt.Errorf("internal error: %d", sz))
	case sz == db.emptySize:
		tree, h, err := lldb.CreateBTree(db.alloc, collate)
		if err != nil {
			return nil, err
		}

		if h != 1 {
			panic("internal error")
		}

		r = &Array{db, tree, nil, "", 0}
		db._root = r
		return r, nil
	default:
		tree, err := lldb.OpenBTree(db.alloc, collate, 1)
		if err != nil {
			return nil, err
		}

		r = &Array{db, tree, nil, "", 0}
		db._root = r
		return r, nil
	}
}
示例#4
0
文件: kv.go 项目: rn2dy/camlistore
// Open opens the named DB file for reading/writing. If successful, methods on
// the returned DB can be used for I/O; the associated file descriptor has mode
// os.O_RDWR. If there is an error, it will be of type *os.PathError.
//
// Note: While a DB is opened, it is locked and cannot be simultaneously opened
// again.
//
// For the meaning of opts please see documentation of Options.
func Open(name string, opts *Options) (db *DB, err error) {
	opts = opts.clone()
	opts._ACID = _ACIDFull
	defer func() {
		lock := opts.lock
		if err != nil && lock != nil {
			lock.Close()
			db = nil
		}
		if err != nil {
			if db != nil {
				db.Close()
				db = nil
			}
		}
	}()

	if err = opts.check(name, false, true); err != nil {
		return
	}

	f, err := os.OpenFile(name, os.O_RDWR, 0666)
	if err != nil {
		return
	}

	filer := lldb.Filer(lldb.NewSimpleFileFiler(f))
	sz, err := filer.Size()
	if err != nil {
		return
	}

	if sz%16 != 0 {
		return nil, &os.PathError{Op: "kv.Open:", Path: name, Err: fmt.Errorf("file size %d(%#x) is not 0 (mod 16)", sz, sz)}
	}

	var b [16]byte
	if n, err := filer.ReadAt(b[:], 0); n != 16 || err != nil {
		return nil, &os.PathError{Op: "kv.Open.ReadAt", Path: name, Err: err}
	}

	var h header
	if err = h.rd(b[:]); err != nil {
		return nil, &os.PathError{Op: "kv.Open:validate header", Path: name, Err: err}
	}

	db = &DB{f: f, lock: opts.lock}
	if filer, err = opts.acidFiler(db, filer); err != nil {
		return nil, err
	}

	db.filer = filer
	switch h.ver {
	default:
		return nil, &os.PathError{Op: "kv.Open", Path: name, Err: fmt.Errorf("unknown/unsupported kv file format version %#x", h.ver)}
	case 0x00:
		if _, err = open00(name, db); err != nil {
			return nil, err
		}
	}

	db.root, err = lldb.OpenBTree(db.alloc, opts.Compare, 1)
	db.wal = opts.wal
	return
}
示例#5
0
func (db *DB) victor(removes Array, h int64) {
	atomic.AddInt32(&activeVictors, 1)
	var err error
	var finished bool
	defer func() {
		if finished {
			func() {
				db.enter()

				defer func() {
					if e := recover(); e != nil {
						err = fmt.Errorf("%v", e)
					}
					db.leave(&err)
				}()

				lldb.RemoveBTree(db.alloc, h)
				removes.delete(h)
				db.setRemoving(h, false)
			}()
		}
		db.wg.Done()
		atomic.AddInt32(&activeVictors, -1)
	}()

	db.enter()

	doLeave := true
	defer func() {
		if e := recover(); e != nil {
			err = fmt.Errorf("%v", e)
		}
		if doLeave {
			db.leave(&err)
		}
	}()

	t, err := lldb.OpenBTree(db.alloc, collate, h)
	if err != nil {
		finished = true
		return
	}

	doLeave = false
	if db.leave(&err) != nil {
		return
	}

	for {
		runtime.Gosched()
		select {
		case _, ok := <-db.stop:
			if !ok {
				return
			}
		default:
		}

		db.enter()
		doLeave = true
		if finished, err = t.DeleteAny(); finished || err != nil {
			return
		}

		doLeave = false
		if db.leave(&err) != nil {
			return
		}
	}
}