2020-02-05 19:23:00 +00:00
|
|
|
package rados
|
|
|
|
|
2020-12-01 21:07:18 +00:00
|
|
|
/*
|
|
|
|
#cgo LDFLAGS: -lrados
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <rados/librados.h>
|
|
|
|
|
|
|
|
typedef void* voidptr;
|
|
|
|
|
|
|
|
*/
|
2020-02-05 19:23:00 +00:00
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
2020-12-01 21:07:18 +00:00
|
|
|
"runtime"
|
2020-02-05 19:23:00 +00:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2020-12-01 21:07:18 +00:00
|
|
|
const (
|
|
|
|
ptrSize = C.sizeof_voidptr
|
|
|
|
sizeTSize = C.sizeof_size_t
|
|
|
|
)
|
|
|
|
|
|
|
|
// setOmapStep is a write op step. It holds C memory used in the operation.
|
|
|
|
type setOmapStep struct {
|
|
|
|
withRefs
|
|
|
|
withoutUpdate
|
|
|
|
|
|
|
|
// C arguments
|
|
|
|
cKeys **C.char
|
|
|
|
cValues **C.char
|
|
|
|
cLengths *C.size_t
|
|
|
|
cNum C.size_t
|
|
|
|
}
|
2020-02-05 19:23:00 +00:00
|
|
|
|
2020-12-01 21:07:18 +00:00
|
|
|
func newSetOmapStep(pairs map[string][]byte) *setOmapStep {
|
2020-02-05 19:23:00 +00:00
|
|
|
|
2020-12-01 21:07:18 +00:00
|
|
|
maplen := C.size_t(len(pairs))
|
|
|
|
cKeys := C.malloc(maplen * ptrSize)
|
|
|
|
cValues := C.malloc(maplen * ptrSize)
|
|
|
|
cLengths := C.malloc(maplen * sizeTSize)
|
2020-02-05 19:23:00 +00:00
|
|
|
|
2020-12-01 21:07:18 +00:00
|
|
|
sos := &setOmapStep{
|
|
|
|
cKeys: (**C.char)(cKeys),
|
|
|
|
cValues: (**C.char)(cValues),
|
|
|
|
cLengths: (*C.size_t)(cLengths),
|
|
|
|
cNum: C.size_t(len(pairs)),
|
|
|
|
}
|
|
|
|
sos.add(cKeys)
|
|
|
|
sos.add(cValues)
|
|
|
|
sos.add(cLengths)
|
2020-02-05 19:23:00 +00:00
|
|
|
|
2020-12-01 21:07:18 +00:00
|
|
|
var i uintptr
|
2020-02-05 19:23:00 +00:00
|
|
|
for key, value := range pairs {
|
|
|
|
// key
|
2020-12-01 21:07:18 +00:00
|
|
|
ck := C.CString(key)
|
|
|
|
sos.add(unsafe.Pointer(ck))
|
|
|
|
ckp := (**C.char)(unsafe.Pointer(uintptr(cKeys) + i*ptrSize))
|
|
|
|
*ckp = ck
|
2020-02-05 19:23:00 +00:00
|
|
|
|
|
|
|
// value and its length
|
2020-12-01 21:07:18 +00:00
|
|
|
cvp := (**C.char)(unsafe.Pointer(uintptr(cValues) + i*ptrSize))
|
|
|
|
vlen := C.size_t(len(value))
|
|
|
|
if vlen > 0 {
|
|
|
|
cv := C.CBytes(value)
|
|
|
|
sos.add(cv)
|
|
|
|
*cvp = (*C.char)(cv)
|
2020-02-05 19:23:00 +00:00
|
|
|
} else {
|
2020-12-01 21:07:18 +00:00
|
|
|
*cvp = nil
|
2020-02-05 19:23:00 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 21:07:18 +00:00
|
|
|
clp := (*C.size_t)(unsafe.Pointer(uintptr(cLengths) + i*ptrSize))
|
|
|
|
*clp = vlen
|
2020-02-05 19:23:00 +00:00
|
|
|
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
2020-12-01 21:07:18 +00:00
|
|
|
runtime.SetFinalizer(sos, opStepFinalizer)
|
|
|
|
return sos
|
|
|
|
}
|
2020-02-05 19:23:00 +00:00
|
|
|
|
2020-12-01 21:07:18 +00:00
|
|
|
func (sos *setOmapStep) free() {
|
|
|
|
sos.cKeys = nil
|
|
|
|
sos.cValues = nil
|
|
|
|
sos.cLengths = nil
|
|
|
|
sos.withRefs.free()
|
|
|
|
}
|
2020-02-05 19:23:00 +00:00
|
|
|
|
2020-12-01 21:07:18 +00:00
|
|
|
// SetOmap appends the map `pairs` to the omap `oid`
|
|
|
|
func (ioctx *IOContext) SetOmap(oid string, pairs map[string][]byte) error {
|
|
|
|
op := CreateWriteOp()
|
|
|
|
defer op.Release()
|
|
|
|
op.SetOmap(pairs)
|
|
|
|
return op.operateCompat(ioctx, oid)
|
2020-02-05 19:23:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OmapListFunc is the type of the function called for each omap key
|
|
|
|
// visited by ListOmapValues
|
|
|
|
type OmapListFunc func(key string, value []byte)
|
|
|
|
|
2020-02-05 19:32:21 +00:00
|
|
|
// ListOmapValues iterates over the keys and values in an omap by way of
|
|
|
|
// a callback function.
|
|
|
|
//
|
2020-02-05 19:23:00 +00:00
|
|
|
// `startAfter`: iterate only on the keys after this specified one
|
|
|
|
// `filterPrefix`: iterate only on the keys beginning with this prefix
|
|
|
|
// `maxReturn`: iterate no more than `maxReturn` key/value pairs
|
|
|
|
// `listFn`: the function called at each iteration
|
|
|
|
func (ioctx *IOContext) ListOmapValues(oid string, startAfter string, filterPrefix string, maxReturn int64, listFn OmapListFunc) error {
|
|
|
|
c_oid := C.CString(oid)
|
|
|
|
c_start_after := C.CString(startAfter)
|
|
|
|
c_filter_prefix := C.CString(filterPrefix)
|
|
|
|
c_max_return := C.uint64_t(maxReturn)
|
|
|
|
|
|
|
|
defer C.free(unsafe.Pointer(c_oid))
|
|
|
|
defer C.free(unsafe.Pointer(c_start_after))
|
|
|
|
defer C.free(unsafe.Pointer(c_filter_prefix))
|
|
|
|
|
|
|
|
op := C.rados_create_read_op()
|
|
|
|
|
|
|
|
var c_iter C.rados_omap_iter_t
|
|
|
|
var c_prval C.int
|
|
|
|
C.rados_read_op_omap_get_vals2(
|
|
|
|
op,
|
|
|
|
c_start_after,
|
|
|
|
c_filter_prefix,
|
|
|
|
c_max_return,
|
|
|
|
&c_iter,
|
|
|
|
nil,
|
|
|
|
&c_prval,
|
|
|
|
)
|
|
|
|
|
|
|
|
ret := C.rados_read_op_operate(op, ioctx.ioctx, c_oid, 0)
|
|
|
|
|
|
|
|
if int(ret) != 0 {
|
2020-03-30 20:57:54 +00:00
|
|
|
return getError(ret)
|
2020-02-05 19:23:00 +00:00
|
|
|
} else if int(c_prval) != 0 {
|
2020-03-30 21:03:52 +00:00
|
|
|
return getError(c_prval)
|
2020-02-05 19:23:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
var c_key *C.char
|
|
|
|
var c_val *C.char
|
|
|
|
var c_len C.size_t
|
|
|
|
|
|
|
|
ret = C.rados_omap_get_next(c_iter, &c_key, &c_val, &c_len)
|
|
|
|
|
|
|
|
if int(ret) != 0 {
|
2020-03-30 20:57:54 +00:00
|
|
|
return getError(ret)
|
2020-02-05 19:23:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if c_key == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
listFn(C.GoString(c_key), C.GoBytes(unsafe.Pointer(c_val), C.int(c_len)))
|
|
|
|
}
|
|
|
|
|
|
|
|
C.rados_omap_get_end(c_iter)
|
|
|
|
C.rados_release_read_op(op)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-05 19:32:21 +00:00
|
|
|
// GetOmapValues fetches a set of keys and their values from an omap and returns then as a map
|
2020-02-05 19:23:00 +00:00
|
|
|
// `startAfter`: retrieve only the keys after this specified one
|
|
|
|
// `filterPrefix`: retrieve only the keys beginning with this prefix
|
|
|
|
// `maxReturn`: retrieve no more than `maxReturn` key/value pairs
|
|
|
|
func (ioctx *IOContext) GetOmapValues(oid string, startAfter string, filterPrefix string, maxReturn int64) (map[string][]byte, error) {
|
|
|
|
omap := map[string][]byte{}
|
|
|
|
|
|
|
|
err := ioctx.ListOmapValues(
|
|
|
|
oid, startAfter, filterPrefix, maxReturn,
|
|
|
|
func(key string, value []byte) {
|
|
|
|
omap[key] = value
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
return omap, err
|
|
|
|
}
|
|
|
|
|
2020-02-05 19:32:21 +00:00
|
|
|
// GetAllOmapValues fetches all the keys and their values from an omap and returns then as a map
|
2020-02-05 19:23:00 +00:00
|
|
|
// `startAfter`: retrieve only the keys after this specified one
|
|
|
|
// `filterPrefix`: retrieve only the keys beginning with this prefix
|
|
|
|
// `iteratorSize`: internal number of keys to fetch during a read operation
|
|
|
|
func (ioctx *IOContext) GetAllOmapValues(oid string, startAfter string, filterPrefix string, iteratorSize int64) (map[string][]byte, error) {
|
|
|
|
omap := map[string][]byte{}
|
|
|
|
omapSize := 0
|
|
|
|
|
|
|
|
for {
|
|
|
|
err := ioctx.ListOmapValues(
|
|
|
|
oid, startAfter, filterPrefix, iteratorSize,
|
|
|
|
func(key string, value []byte) {
|
|
|
|
omap[key] = value
|
|
|
|
startAfter = key
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return omap, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// End of omap
|
|
|
|
if len(omap) == omapSize {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
omapSize = len(omap)
|
|
|
|
}
|
|
|
|
|
|
|
|
return omap, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RmOmapKeys removes the specified `keys` from the omap `oid`
|
|
|
|
func (ioctx *IOContext) RmOmapKeys(oid string, keys []string) error {
|
|
|
|
c_oid := C.CString(oid)
|
|
|
|
defer C.free(unsafe.Pointer(c_oid))
|
|
|
|
|
|
|
|
var c *C.char
|
|
|
|
ptrSize := unsafe.Sizeof(c)
|
|
|
|
|
|
|
|
c_keys := C.malloc(C.size_t(len(keys)) * C.size_t(ptrSize))
|
|
|
|
defer C.free(unsafe.Pointer(c_keys))
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for _, key := range keys {
|
|
|
|
c_key_ptr := (**C.char)(unsafe.Pointer(uintptr(c_keys) + uintptr(i)*ptrSize))
|
|
|
|
*c_key_ptr = C.CString(key)
|
|
|
|
defer C.free(unsafe.Pointer(*c_key_ptr))
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
op := C.rados_create_write_op()
|
|
|
|
C.rados_write_op_omap_rm_keys(
|
|
|
|
op,
|
|
|
|
(**C.char)(c_keys),
|
|
|
|
C.size_t(len(keys)))
|
|
|
|
|
|
|
|
ret := C.rados_write_op_operate(op, ioctx.ioctx, c_oid, nil, 0)
|
|
|
|
C.rados_release_write_op(op)
|
|
|
|
|
2020-03-30 20:57:54 +00:00
|
|
|
return getError(ret)
|
2020-02-05 19:23:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CleanOmap clears the omap `oid`
|
|
|
|
func (ioctx *IOContext) CleanOmap(oid string) error {
|
|
|
|
c_oid := C.CString(oid)
|
|
|
|
defer C.free(unsafe.Pointer(c_oid))
|
|
|
|
|
|
|
|
op := C.rados_create_write_op()
|
|
|
|
C.rados_write_op_omap_clear(op)
|
|
|
|
|
|
|
|
ret := C.rados_write_op_operate(op, ioctx.ioctx, c_oid, nil, 0)
|
|
|
|
C.rados_release_write_op(op)
|
|
|
|
|
2020-03-30 20:57:54 +00:00
|
|
|
return getError(ret)
|
2020-02-05 19:23:00 +00:00
|
|
|
}
|