Exemple #1
0
func (kc *KCDB) Cas(key, oval, nval []byte) (err error) {
	ckey := (*C.char)(unsafe.Pointer(&key[0]))
	var coval, cnval *C.char
	loval, lnval := len(oval), len(nval)
	if loval > 0 {
		coval = (*C.char)(unsafe.Pointer(&oval[0]))
	}
	if lnval > 0 {
		cnval = (*C.char)(unsafe.Pointer(&nval[0]))
	}
	if C.kcdbcas(kc.db, ckey, C.size_t(len(key)), coval, C.size_t(loval), cnval, C.size_t(lnval)) == 0 {
		err = kc.error()
	}
	return
}
Exemple #2
0
// CompareAndSwap performs a compare-and-swap operation, receiving three
// parameters: key, old and new.
//
// If the value corresponding to key is equal to old, then it is set to new. If
// the operation fails, this method returns a non-nil error.
func (d *DB) CompareAndSwap(key, old, new string) error {
	cKey := C.CString(key)
	defer C.free(unsafe.Pointer(cKey))
	lKey := C.size_t(len(key))
	cOldValue := C.CString(old)
	defer C.free(unsafe.Pointer(cOldValue))
	lOldValue := C.size_t(len(old))
	cNewValue := C.CString(new)
	defer C.free(unsafe.Pointer(cNewValue))
	lNewValue := C.size_t(len(new))
	if C.kcdbcas(d.db, cKey, lKey, cOldValue, lOldValue, cNewValue, lNewValue) == 0 {
		return d.LastError()
	}
	return nil
}