Exemplo n.º 1
0
// Put writes data associated with a key to the database.
//
// If a nil []byte is passed in as value, it will be returned by Get as an
// zero-length slice.
//
// The key and value byte slices may be reused safely. Put takes a copy of
// them before returning.
func (db *DB) Put(wo *WriteOptions, key, value []byte) error {
	var errStr *C.char
	// leveldb_put, _get, and _delete call memcpy() (by way of Memtable::Add)
	// when called, so we do not need to worry about these []byte being
	// reclaimed by GC.
	var k, v *C.char
	if len(key) != 0 {
		k = (*C.char)(unsafe.Pointer(&key[0]))
	}
	if len(value) != 0 {
		v = (*C.char)(unsafe.Pointer(&value[0]))
	}

	lenk := len(key)
	lenv := len(value)
	C.leveldb_put(
		db.Ldb, wo.Opt, k, C.size_t(lenk), v, C.size_t(lenv), &errStr)

	if errStr != nil {
		gs := C.GoString(errStr)
		C.free(unsafe.Pointer(errStr))
		return DatabaseError(gs)
	}
	return nil
}
Exemplo n.º 2
0
func (db *LevelDB) Put(key, value []byte) error {
	var err *C.char
	C.leveldb_put(db.db, db.writeOpts.Opts, C.CString(string(key)), C.size_t(len(key)),
		C.CString(string(value)), C.size_t(len(value)), &err)
	if err != nil {
		e := &DBError{}
		e.Err = C.GoString(err)
		return e
	}
	return nil
}
Exemplo n.º 3
0
func (self *Leveldb) Set(key string, obj interface{}) error {
	buf := new(bytes.Buffer)
	err := encode(buf, obj)
	if err != nil {
		return err
	}
	value := buf.Bytes()
	cKey := C.CString(key)
	defer C.free(unsafe.Pointer(cKey))
	header := (*reflect.SliceHeader)(unsafe.Pointer(&value))
	var cerr *C.char
	C.leveldb_put(self.cdb, self.write_options, cKey, C.size_t(len(key)), (*C.char)(unsafe.Pointer(header.Data)), C.size_t(header.Len), &cerr)
	if cerr != nil {
		return errors.New(fmt.Sprintf("%s", cerr))
	}
	return nil
}
Exemplo n.º 4
0
func (db *DB) put(wo *WriteOptions, key, value []byte) error {
	var errStr *C.char
	var k, v *C.char
	if len(key) != 0 {
		k = (*C.char)(unsafe.Pointer(&key[0]))
	}
	if len(value) != 0 {
		v = (*C.char)(unsafe.Pointer(&value[0]))
	}

	lenk := len(key)
	lenv := len(value)
	C.leveldb_put(
		db.db, wo.Opt, k, C.size_t(lenk), v, C.size_t(lenv), &errStr)

	if errStr != nil {
		return saveError(errStr)
	}
	return nil
}
// Put key, value to database
func (db *LevelDB) Put(key, value string) (err error) {

	opt := C.leveldb_writeoptions_create() // write option
	defer C.leveldb_free(unsafe.Pointer(opt))

	k := C.CString(key) // copy
	defer C.leveldb_free(unsafe.Pointer(k))

	v := C.CString(value)
	defer C.leveldb_free(unsafe.Pointer(v))

	var cerr *C.char
	C.leveldb_put(db.CLevelDB, opt, k, C.size_t(len(key)), v, C.size_t(len(value)), &cerr)

	if cerr != nil {
		defer C.leveldb_free(unsafe.Pointer(cerr))
		return errors.New(C.GoString(cerr))
	}

	return
}
Exemplo n.º 6
0
func (ldb *Db) Put(key, value []byte, w_options *Writeoptions) error {
	var db_err *C.char

	key_len := len(key)
	value_len := len(value)
	if key_len == 0 {
		return ldb_error("key must be not empty")
	}

	if value_len == 0 {
		return ldb_error("value must be not empty")
	}

	key_c := (*C.char)(unsafe.Pointer(&key[0]))
	value_c := (*C.char)(unsafe.Pointer(&value[0]))
	C.leveldb_put(ldb.ldb, w_options.options,
		key_c, C.size_t(key_len),
		value_c, C.size_t(value_len), &db_err)
	if db_err != nil {
		return ldb_error(C.GoString(db_err))
	}
	return nil
}