func (kc *KCDB) IncrInt(key []byte, amount int64) (result int64, err error) { ckey := (*C.char)(unsafe.Pointer(&key[0])) cresult := C.kcdbincrint(kc.db, ckey, C.size_t(len(key)), C.int64_t(amount), 0.0) if cresult == C.INT64_MIN { err = kc.error() } else { result = int64(cresult) } return }
// Increments the value of a numeric record by a given number, // and return the incremented value // // In case of errors, returns 0 and a KCError instance with detailed // error message func (d *DB) Increment(key string, number int) (int, error) { cKey := C.CString(key) defer C.free(unsafe.Pointer(cKey)) lKey := C.size_t(len(key)) cValue := C.int64_t(number) v := C.kcdbincrint(d.db, cKey, lKey, cValue, 0) if v == C.INT64_MIN { return 0, KCError("It's not possible to increment a non-numeric record") } return int(v), nil }
// GetInt gets a numeric record from the database. // // In case of errors (e.g.: when the given key refers to a non-numeric record), // returns 0 and a KCError instance. func (d *DB) GetInt(key string) (int, error) { v, err := d.Get(key) if err != nil { return 0, err } else if strings.IndexRune(v, '\x00') != 0 { return 0, KCError("Error: GetInt can't be used to get a non-numeric record") } cKey := C.CString(key) defer C.free(unsafe.Pointer(cKey)) lKey := C.size_t(len(key)) number := C.kcdbincrint(d.db, cKey, lKey, 0, 0) return int(number), nil }
// Gets a numeric record from the database // // In case of errors (e.g.: when the given key refers to a non-numeric record), // returns 0 and a KCError instance. func (d *DB) GetInt(key string) (int, error) { v, err := d.Get(key) if err != nil { return 0, err } else if v != "" { err := KCError("Error: don't use GetInt to get a non-numeric record") return 0, err } cKey := C.CString(key) defer C.free(unsafe.Pointer(cKey)) lKey := C.size_t(len(key)) number := C.kcdbincrint(d.db, cKey, lKey, 0, 0) return int(number), nil }
// Sets the value of an integer record, creating it when there is no // record correspondent to the given key // // Returns an KCError in case of errors setting the value func (d *DB) SetInt(key string, number int) error { if d.mode < WRITE { return KCError("SetInt doesn't work in read-only mode") } d.Remove(key) cKey := C.CString(key) defer C.free(unsafe.Pointer(cKey)) lKey := C.size_t(len(key)) cValue := C.int64_t(number) if C.kcdbincrint(d.db, cKey, lKey, cValue, 0) == C.INT64_MIN { errMsg := d.LastError() return KCError(fmt.Sprintf("Error setting integer value: %s", errMsg)) } return nil }