func (ndbm *NDBM) store(key, value []byte, mode C.int) (C.int, error) { C.dbm_clearerr(ndbm.cDbm) status, err := C.dbm_store(ndbm.cDbm, bytesToDatum(key), bytesToDatum(value), mode) if status == checkError { return status, Error{ dbErrNum: int(C.dbm_error(ndbm.cDbm)), errnoErr: err, } } return status, nil }
// Delete deletes an entry from the database. // Returns KeyNotFound if the key can't be found. func (ndbm *NDBM) Delete(key []byte) error { C.dbm_clearerr(ndbm.cDbm) status, err := C.dbm_delete(ndbm.cDbm, bytesToDatum(key)) if status == checkError { dbErrNum := C.dbm_error(ndbm.cDbm) if dbErrNum == C.DBM_ITEM_NOT_FOUND { return KeyNotFound{key} } return Error{ dbErrNum: int(dbErrNum), errnoErr: err, } } if status == notFound { return KeyNotFound{key} } return nil }
// Fetch retrieves an entry value by key. // Returns KeyNotFound if the key can't be found. func (ndbm *NDBM) Fetch(key []byte) ([]byte, error) { C.dbm_clearerr(ndbm.cDbm) datum, err := C.dbm_fetch(ndbm.cDbm, bytesToDatum(key)) value := datumToBytes(datum) if value == nil { dbErrNum := C.dbm_error(ndbm.cDbm) if dbErrNum == noError { return nil, KeyNotFound{key} } if dbErrNum == C.DBM_ITEM_NOT_FOUND { return nil, KeyNotFound{key} } return nil, Error{ dbErrNum: int(dbErrNum), errnoErr: err, } } return value, nil }