示例#1
0
文件: mdbm.go 项目: tann/mdbm
// Unlock releases current lock set by Lock
func (db *MDBM) Unlock() error {
	if db.hasLock {
		_, e := C.mdbm_unlock(db.dbh)
		if e != nil {
			return e
		}
		db.hasLock = false
	}
	return nil
}
示例#2
0
文件: mdbm.go 项目: tann/mdbm
// Delete deletes an entry given a key
func (db *MDBM) Delete(key []byte) error {
	db.mutex.Lock()
	defer db.mutex.Unlock()

	var k C.datum
	k.dptr = (*C.char)(unsafe.Pointer(&key[0]))
	k.dsize = C.int(len(key))

	// Same as storing. Lock is already done
	// implicitly internally
	C.mdbm_lock(db.dbh)
	defer C.mdbm_unlock(db.dbh)
	_, e := C.mdbm_delete(db.dbh, k)
	if e != nil {
		return errors.New("Cannot delete entry: " + e.Error())
	}
	return nil
}
示例#3
0
文件: mdbm.go 项目: tann/mdbm
// Put saves a key-value entry
func (db *MDBM) Put(key []byte, val []byte) error {
	db.mutex.Lock()
	defer db.mutex.Unlock()

	var k, v C.datum
	k.dptr = (*C.char)(unsafe.Pointer(&key[0]))
	k.dsize = C.int(len(key))
	v.dptr = (*C.char)(unsafe.Pointer(&val[0]))
	v.dsize = C.int(len(val))

	// Is locking here needed? storing/deleting implements
	// implicit locking
	C.mdbm_lock(db.dbh)
	defer C.mdbm_unlock(db.dbh)
	_, e := C.mdbm_store(db.dbh, k, v, C.MDBM_REPLACE)
	if e != nil {
		return errors.New("Cannot store entry: " + e.Error())
	}
	return nil
}