Пример #1
0
// This methods appends a string to the end of the value of a string
// record. It can't append any value to a numeric record.
//
// Returns a KCError instance when trying to append a string to a numeric
// record and when trying to append a string in read-only mode.
//
// If the append is successful, the method returns nil
func (d *DB) Append(key, value string) error {
	if d.mode < WRITE {
		return KCError("The database was opened in read-only mode, you can't append strings to records")
	}

	if _, err := d.GetInt(key); err == nil {
		return KCError("The database doesn't support append a string to a numeric record")
	}

	cKey := C.CString(key)
	defer C.free(unsafe.Pointer(cKey))

	cValue := C.CString(value)
	defer C.free(unsafe.Pointer(cValue))

	lKey := C.size_t(len(key))
	lValue := C.size_t(len(value))

	if C.kcdbappend(d.db, cKey, lKey, cValue, lValue) == 0 {
		errMsg := d.LastError()
		return KCError(fmt.Sprintf("Failed to append a string to a record: %s", errMsg))
	}

	return nil
}
Пример #2
0
func (kc *KCDB) Append(key, value []byte) (err error) {
	ckey := (*C.char)(unsafe.Pointer(&key[0]))
	cvalue := (*C.char)(unsafe.Pointer(&value[0]))
	if C.kcdbappend(kc.db, ckey, C.size_t(len(key)), cvalue, C.size_t(len(value))) == 0 {
		err = kc.error()
	}
	return
}