// Get gets a value for a given key func (db *MDBM) Get(key []byte) (val []byte, err 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)) C.mdbm_lock_smart(db.dbh, &k, 0) defer C.mdbm_unlock_smart(db.dbh, &k, 0) v, e := C.mdbm_fetch(db.dbh, k) if e != nil { return nil, errors.New("Cannot retrieve entry: " + e.Error()) } return C.GoBytes(unsafe.Pointer(v.dptr), v.dsize), nil }
// 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 }
// 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 }