Esempio n. 1
0
// writeBatch applies the puts, merges and deletes atomically via
// the RocksDB write batch facility. The list must only contain
// elements of type Batch{Put,Merge,Delete}.
func (r *RocksDB) writeBatch(cmds []interface{}) error {
	if len(cmds) == 0 {
		return nil
	}
	batch := C.rocksdb_writebatch_create()
	defer C.rocksdb_writebatch_destroy(batch)
	for i, e := range cmds {
		switch v := e.(type) {
		case BatchDelete:
			if len(v) == 0 {
				return emptyKeyError()
			}
			C.rocksdb_writebatch_delete(
				batch,
				(*C.char)(unsafe.Pointer(&v[0])),
				C.size_t(len(v)))
		case BatchPut:
			key, value := v.Key, v.Value
			valuePointer := (*C.char)(nil)
			if len(value.Bytes) > 0 {
				valuePointer = (*C.char)(unsafe.Pointer(&value.Bytes[0]))
			}

			// We write the batch before returning from this method, so we
			// don't need to worry about the GC reclaiming the data stored.
			C.rocksdb_writebatch_put(
				batch,
				(*C.char)(unsafe.Pointer(&key[0])),
				C.size_t(len(key)),
				valuePointer,
				C.size_t(len(value.Bytes)))
		case BatchMerge:
			key, value := v.Key, v.Value
			valuePointer := (*C.char)(nil)
			if len(value.Bytes) > 0 {
				valuePointer = (*C.char)(unsafe.Pointer(&value.Bytes[0]))
			}
			C.rocksdb_writebatch_merge(
				batch,
				(*C.char)(unsafe.Pointer(&key[0])),
				C.size_t(len(key)),
				valuePointer,
				C.size_t(len(value.Bytes)))
		default:
			panic(fmt.Sprintf("illegal operation #%d passed to writeBatch: %v", i, reflect.TypeOf(v)))
		}
	}

	var cErr *C.char
	C.rocksdb_write(r.rdb, r.wOpts, batch, &cErr)
	if cErr != nil {
		return charToErr(cErr)
	}
	return nil
}
Esempio n. 2
0
func (w *WriteBatch) Put(key, value []byte) {
	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.rocksdb_writebatch_put(w.wbatch, k, C.size_t(lenk), v, C.size_t(lenv))
}
Esempio n. 3
0
// Put places a key-value pair into the WriteBatch for writing later.
//
// Both the key and value byte slices may be reused as WriteBatch takes a copy
// of them before returning.
//
func (w *WriteBatch) Put(key, value []byte) {
	// rocksdb_writebatch_put, 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.rocksdb_writebatch_put(w.wbatch, k, C.size_t(lenk), v, C.size_t(lenv))
}
Esempio n. 4
0
// writeBatch applies all puts and deletes atomically via RocksDB write
// batch facility.
func (r *RocksDB) writeBatch(puts []KeyValue, dels []Key) error {
	batch := C.rocksdb_writebatch_create()
	defer C.rocksdb_writebatch_destroy(batch)

	for _, put := range puts {
		key, value := put.Key, put.Value
		if len(key) == 0 {
			return emptyKeyError()
		}

		// Empty values correspond to a null pointer.
		valuePointer := (*C.char)(nil)
		if len(value.Bytes) > 0 {
			valuePointer = (*C.char)(unsafe.Pointer(&value.Bytes[0]))
		}

		// We write the batch before returning from this method, so we
		// don't need to worry about the GC reclaiming the data stored in
		// the "puts" and "dels" parameters.
		C.rocksdb_writebatch_put(
			batch,
			(*C.char)(unsafe.Pointer(&key[0])),
			C.size_t(len(key)),
			valuePointer,
			C.size_t(len(value.Bytes)))
	}

	for _, key := range dels {
		if len(key) == 0 {
			return emptyKeyError()
		}
		C.rocksdb_writebatch_delete(
			batch,
			(*C.char)(unsafe.Pointer(&key[0])),
			C.size_t(len(key)))
	}

	var cErr *C.char
	C.rocksdb_write(r.rdb, r.wOpts, batch, &cErr)
	if cErr != nil {
		return charToErr(cErr)
	}

	return nil
}
Esempio n. 5
0
File: db.go Progetto: tradia/gotable
func (db *DB) Put(rawKey, value []byte, wb *WriteBatch) error {
	var ck, cv *C.char
	if len(rawKey) > 0 {
		ck = (*C.char)(unsafe.Pointer(&rawKey[0]))
	}
	if len(value) > 0 {
		cv = (*C.char)(unsafe.Pointer(&value[0]))
	}

	if wb == nil {
		var errStr *C.char
		C.rocksdb_put(db.db, db.wOpt, ck, C.size_t(len(rawKey)), cv,
			C.size_t(len(value)), &errStr)
		if errStr != nil {
			defer C.free(unsafe.Pointer(errStr))
			return errors.New(C.GoString(errStr))
		}
	} else {
		C.rocksdb_writebatch_put(wb.batch, ck, C.size_t(len(rawKey)), cv,
			C.size_t(len(value)))
	}

	return nil
}
Esempio n. 6
0
// Put queues a key-value pair.
func (w *WriteBatch) Put(key, value []byte) {
	cKey := byteToChar(key)
	cValue := byteToChar(value)
	C.rocksdb_writebatch_put(w.c, cKey, C.size_t(len(key)), cValue, C.size_t(len(value)))
}