rados: use rados_write_op_omap_set2()

WriteOp.SetOmap now uses rados_write_op_omap_set2().

The entirety of setOmapStep struct was removed as it's not necessary for it to
own the various C-allocated values passed to rados_write_op_omap_set2()
as arguments. rados_write_op_omap_set2() makes copies of all the string-based
args [1], so it's sufficient for them to be short lived.

[1] 2bd3dd512a/src/librados/librados_c.cc (L3852-L3871)
This commit is contained in:
Robert Vasek 2022-02-10 20:50:44 +01:00 committed by mergify[bot]
parent 3f9bcf03cc
commit 044c3733e9
2 changed files with 21 additions and 68 deletions

View File

@ -10,69 +10,8 @@ import "C"
import (
"runtime"
"unsafe"
"github.com/ceph/go-ceph/internal/cutil"
)
// setOmapStep is a write op step. It holds C memory used in the operation.
type setOmapStep struct {
withRefs
withoutUpdate
// C arguments
cKeys cutil.CPtrCSlice
cValues cutil.CPtrCSlice
cLengths cutil.SizeTCSlice
cNum C.size_t
}
func newSetOmapStep(pairs map[string][]byte) *setOmapStep {
maplen := len(pairs)
cKeys := cutil.NewCPtrCSlice(maplen)
cValues := cutil.NewCPtrCSlice(maplen)
cLengths := cutil.NewSizeTCSlice(maplen)
sos := &setOmapStep{
cKeys: cKeys,
cValues: cValues,
cLengths: cLengths,
cNum: C.size_t(maplen),
}
var i uintptr
for key, value := range pairs {
// key
ck := C.CString(key)
sos.add(unsafe.Pointer(ck))
cKeys[i] = cutil.CPtr(ck)
// value and its length
vlen := cutil.SizeT(len(value))
if vlen > 0 {
cv := C.CBytes(value)
sos.add(cv)
cValues[i] = cutil.CPtr(cv)
} else {
cValues[i] = nil
}
cLengths[i] = vlen
i++
}
runtime.SetFinalizer(sos, opStepFinalizer)
return sos
}
func (sos *setOmapStep) free() {
sos.cKeys.Free()
sos.cValues.Free()
sos.cLengths.Free()
sos.withRefs.free()
}
// OmapKeyValue items are returned by the GetOmapStep's Next call.
type OmapKeyValue struct {
Key string

View File

@ -10,6 +10,7 @@ import "C"
import (
"unsafe"
"github.com/ceph/go-ceph/internal/cutil"
ts "github.com/ceph/go-ceph/internal/timespec"
)
@ -92,14 +93,27 @@ func (w *WriteOp) Create(exclusive CreateOption) {
// SetOmap appends the map `pairs` to the omap `oid`.
func (w *WriteOp) SetOmap(pairs map[string][]byte) {
sos := newSetOmapStep(pairs)
w.steps = append(w.steps, sos)
C.rados_write_op_omap_set(
keys := make([]string, len(pairs))
values := make([][]byte, len(pairs))
idx := 0
for k, v := range pairs {
keys[idx] = k
values[idx] = v
idx++
}
cKeys := cutil.NewBufferGroupStrings(keys)
cValues := cutil.NewBufferGroupBytes(values)
defer cKeys.Free()
defer cValues.Free()
C.rados_write_op_omap_set2(
w.op,
(**C.char)(sos.cKeys.Ptr()),
(**C.char)(sos.cValues.Ptr()),
(*C.size_t)(sos.cLengths.Ptr()),
sos.cNum)
(**C.char)(cKeys.BuffersPtr()),
(**C.char)(cValues.BuffersPtr()),
(*C.size_t)(cKeys.LengthsPtr()),
(*C.size_t)(cValues.LengthsPtr()),
(C.size_t)(len(pairs)))
}
// RmOmapKeys removes the specified `keys` from the omap `oid`.