Esempio n. 1
0
File: mdbm.go Progetto: tann/mdbm
// Lock locks DB exclusively
func (db *MDBM) Lock() error {
	if !db.hasLock {
		_, e := C.mdbm_lock(db.dbh)
		if e != nil {
			return e
		}
		db.hasLock = true
	}
	return nil
}
Esempio n. 2
0
File: mdbm.go Progetto: 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
}
Esempio n. 3
0
File: mdbm.go Progetto: 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
}