Exemplo 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
}
Exemplo n.º 2
0
// Merge queues a merge of "value" with the existing value of "key".
func (w *WriteBatch) Merge(key, value []byte) {
	cKey := byteToChar(key)
	cValue := byteToChar(value)
	C.rocksdb_writebatch_merge(w.c, cKey, C.size_t(len(key)), cValue, C.size_t(len(value)))
}