Exemple #1
0
// Internal helper method to hide the two constants GDBM_INSERT and
// GDBM_REPLACE from the user.
func (db *Database) update(key string, value string, flag C.int) (err error) {
	// Convert key and value into libgdbm's `datum` data structure. See the
	// C definition at the top for the implementation of C.mk_datum(string).
	kcs := C.CString(key)
	vcs := C.CString(value)
	k := C.mk_datum(kcs)
	v := C.mk_datum(vcs)
	defer C.free(unsafe.Pointer(kcs))
	defer C.free(unsafe.Pointer(vcs))

	retv := C.gdbm_store(db.dbf, k, v, flag)
	if retv != 0 {
		err = lastError()
	}
	return err
}
Exemple #2
0
// Removes a key-value pair from the database. If the database is opened in "r"
// mode, an error is returned
func (db *Database) Delete(key string) (err error) {
	kcs := C.CString(key)
	k := C.mk_datum(kcs)
	defer C.free(unsafe.Pointer(kcs))

	retv := C.gdbm_delete(db.dbf, k)
	if retv == -1 && db.mode == C.GDBM_READER {
		err = lastError()
	}
	return err
}
Exemple #3
0
// Returns true or false, depending on whether the specified key exists in the
// database.
func (db *Database) Exists(key string) bool {
	kcs := C.CString(key)
	k := C.mk_datum(kcs)
	defer C.free(unsafe.Pointer(kcs))

	e := C.gdbm_exists(db.dbf, k)
	if e == 1 {
		return true
	}
	return false
}
Exemple #4
0
// Fetches the value of the given key. If the key is not in the database, an
// error will be returned in err. Otherwise, value will be the value string
// that is keyed by `key`.
func (db *Database) Fetch(key string) (value string, err error) {
	kcs := C.CString(key)
	k := C.mk_datum(kcs)
	defer C.free(unsafe.Pointer(kcs))

	vdatum := C.gdbm_fetch(db.dbf, k)
	if vdatum.dptr == nil {
		return "", lastError()
	}

	value = C.GoStringN(vdatum.dptr, vdatum.dsize)
	defer C.free(unsafe.Pointer(vdatum.dptr))
	return value, nil
}