Skip to content

Commit

Permalink
added WriteOpCmpExtStep with malloc'd rval
Browse files Browse the repository at this point in the history
  • Loading branch information
gman0 committed Oct 7, 2021
1 parent 4b4b9e0 commit 496dc49
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
6 changes: 6 additions & 0 deletions internal/cutil/aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@ const (

// SizeTSize is the size of C.size_t
SizeTSize = C.sizeof_size_t

// IntSize is the size of C.int
IntSize = C.sizeof_int
)

// Compile-time assertion ensuring that Go's `int` is at least as large as C's.
const _ = unsafe.Sizeof(int(0)) - unsafe.Sizeof(C.int(0))

// SizeT wraps size_t from C.
type SizeT C.size_t

Expand Down
37 changes: 34 additions & 3 deletions rados/write_op_preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,41 @@ package rados

// #cgo LDFLAGS: -lrados
// #include <rados/librados.h>
// #include <stdlib.h>
//
import "C"

import (
"unsafe"

"github.com/ceph/go-ceph/internal/cutil"
)

// WriteOpCmpExtStep holds result of the CmpExt write operation.
// Result is valid only after Operate() was called.
type WriteOpCmpExtStep struct {
prval *C.int

// Result of the CmpExt write operation.
Result int
}

func (s *WriteOpCmpExtStep) update() error {
s.Result = (int)(*s.prval)
return nil
}

func (s *WriteOpCmpExtStep) free() {
C.free(unsafe.Pointer(s.prval))
s.prval = nil
}

func newWriteOpCmpExtStep() *WriteOpCmpExtStep {
return &WriteOpCmpExtStep{
prval: (*C.int)(C.malloc(cutil.IntSize)),
}
}

// CmpExt ensures that given object range (extent) satisfies comparison (PREVIEW).
//
// Implements:
Expand All @@ -19,13 +47,16 @@ import (
// size_t cmp_len,
// uint64_t off,
// int * prval);
func (w *WriteOp) CmpExt(b []byte, offset uint64, prval *int32) {
func (w *WriteOp) CmpExt(b []byte, offset uint64) *WriteOpCmpExtStep {
oe := newWriteStep(b, 0, offset)
w.steps = append(w.steps, oe)
cmpExtStep := newWriteOpCmpExtStep()
w.steps = append(w.steps, oe, cmpExtStep)
C.rados_write_op_cmpext(
w.op,
oe.cBuffer,
oe.cDataLen,
oe.cOffset,
(*C.int)(unsafe.Pointer(prval)))
cmpExtStep.prval)

return cmpExtStep
}
9 changes: 4 additions & 5 deletions rados/write_op_preview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ func (suite *RadosTestSuite) TestWriteOpCmpExt() {

oid := "TestWriteOpCmpExt"
data := []byte("compare this")
var prval int32

// Create an object and populate it with data.
op1 := CreateWriteOp()
Expand All @@ -25,16 +24,16 @@ func (suite *RadosTestSuite) TestWriteOpCmpExt() {
// Compare contents of the object. Should succeed.
op2 := CreateWriteOp()
defer op2.Release()
op2.CmpExt(data, 0, &prval)
cmpExtRes1 := op2.CmpExt(data, 0)
err = op2.Operate(suite.ioctx, oid, OperationNoFlag)
ta.NoError(err)
ta.Equal(prval, int32(0))
ta.Equal(cmpExtRes1.Result, int(0))

// Compare contents of the object. Should fail.
op3 := CreateWriteOp()
defer op3.Release()
op3.CmpExt([]byte("xxx"), 0, &prval)
cmpExtRes2 := op3.CmpExt([]byte("xxx"), 0)
err = op3.Operate(suite.ioctx, oid, OperationNoFlag)
ta.Error(err)
ta.NotEqual(prval, int32(0))
ta.NotEqual(cmpExtRes2.Result, int(0))
}

0 comments on commit 496dc49

Please sign in to comment.