From 2926a90df3ccc3ca8fca76d7ddb241fc1562f9f2 Mon Sep 17 00:00:00 2001 From: Robert Vasek Date: Mon, 27 Sep 2021 14:08:50 +0200 Subject: [PATCH] rados: implement binding for rados_write_op_cmpext This commit implements binding for rados_write_op_cmpext RADOS Write operation. Includes a unit test. Signed-off-by: Robert Vasek --- rados/write_op_preview.go | 31 ++++++++++++++++++++++++++ rados/write_op_preview_test.go | 40 ++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 rados/write_op_preview.go create mode 100644 rados/write_op_preview_test.go diff --git a/rados/write_op_preview.go b/rados/write_op_preview.go new file mode 100644 index 000000000..28e539ff8 --- /dev/null +++ b/rados/write_op_preview.go @@ -0,0 +1,31 @@ +// +build ceph_preview + +package rados + +// #cgo LDFLAGS: -lrados +// #include +// +import "C" + +import ( + "unsafe" +) + +// CmpExt ensures that given object range (extent) satisfies comparison (PREVIEW). +// +// Implements: +// void rados_write_op_cmpext(rados_write_op_t write_op, +// const char * cmp_buf, +// size_t cmp_len, +// uint64_t off, +// int * prval); +func (w *WriteOp) CmpExt(b []byte, offset uint64, prval *int32) { + oe := newWriteStep(b, 0, offset) + w.steps = append(w.steps, oe) + C.rados_write_op_cmpext( + w.op, + oe.cBuffer, + oe.cDataLen, + oe.cOffset, + (*C.int)(unsafe.Pointer(prval))) +} diff --git a/rados/write_op_preview_test.go b/rados/write_op_preview_test.go new file mode 100644 index 000000000..47738d472 --- /dev/null +++ b/rados/write_op_preview_test.go @@ -0,0 +1,40 @@ +// +build ceph_preview + +package rados + +import ( + "github.com/stretchr/testify/assert" +) + +func (suite *RadosTestSuite) TestWriteOpCmpExt() { + suite.SetupConnection() + ta := assert.New(suite.T()) + + oid := "TestWriteOpCmpExt" + data := []byte("compare this") + var prval int32 + + // Create an object and populate it with data. + op1 := CreateWriteOp() + defer op1.Release() + op1.Create(CreateIdempotent) + op1.WriteFull([]byte(data)) + err := op1.Operate(suite.ioctx, oid, OperationNoFlag) + ta.NoError(err) + + // Compare contents of the object. Should succeed. + op2 := CreateWriteOp() + defer op2.Release() + op2.CmpExt(data, 0, &prval) + err = op2.Operate(suite.ioctx, oid, OperationNoFlag) + ta.NoError(err) + ta.Equal(prval, int32(0)) + + // Compare contents of the object. Should fail. + op3 := CreateWriteOp() + defer op3.Release() + op3.CmpExt([]byte("xxx"), 0, &prval) + err = op3.Operate(suite.ioctx, oid, OperationNoFlag) + ta.Error(err) + ta.NotEqual(prval, int32(0)) +}