Example #1
0
func (kc *KCDB) Set(key, value []byte) (err error) {
	ckey := (*C.char)(unsafe.Pointer(&key[0]))
	cvalue := (*C.char)(unsafe.Pointer(&value[0]))
	if C.kcdbset(kc.db, ckey, C.size_t(len(key)), cvalue, C.size_t(len(value))) == 0 {
		err = kc.error()
	}
	return
}
Example #2
0
// Set adds a record to the database. Currently, it's able to store only string
// values.
//
// Returns a KCError instance in case of errors, otherwise, returns nil.
func (d *DB) Set(key, value string) error {
	if d.mode < WRITE {
		return KCError("The database was opened in read-only mode, you can't add records to it")
	}
	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.kcdbset(d.db, cKey, lKey, cValue, lValue) == 0 {
		errMsg := d.LastError()
		return KCError(fmt.Sprintf("Failed to add a record with the value %s and the key %s: %s", value, key, errMsg))
	}
	return nil
}
Example #3
0
func (self *KcDb) Set(key string, obj interface{}) error {
	var err error
	buf := new(bytes.Buffer)
	err = encode(buf, obj)
	if err != nil {
		return err
	}
	bytes := buf.Bytes()
	cKey := C.CString(key)
	defer C.free(unsafe.Pointer(cKey))
	header := (*reflect.SliceHeader)(unsafe.Pointer(&bytes))
	if C.kcdbset(self.cdb, cKey, C.size_t(len(key)), (*C.char)(unsafe.Pointer(header.Data)), C.size_t(header.Len)) == kcFalse {
		return errors.New(fmt.Sprintf("set: %s", key))
	}
	return nil
}
Example #4
0
func (self *KcDb) SetDefault(key string, obj interface{}) (bool, error) {
	cKey := C.CString(key)
	defer C.free(unsafe.Pointer(cKey))
	if C.kcdbcheck(self.cdb, cKey, C.size_t(len(key))) != C.int32_t(-1) {
		return false, nil
	}
	buf := new(bytes.Buffer)
	err := encode(buf, obj)
	if err != nil {
		return false, err
	}
	bytes := buf.Bytes()
	header := (*reflect.SliceHeader)(unsafe.Pointer(&bytes))
	if C.kcdbset(self.cdb, cKey, C.size_t(len(key)), (*C.char)(unsafe.Pointer(header.Data)), C.size_t(header.Len)) == kcFalse {
		return false, errors.New(fmt.Sprintf("set: %s", key))
	}
	return true, nil
}