2021-02-10 03:38:18 +00:00
|
|
|
package rados
|
|
|
|
|
|
|
|
// #cgo LDFLAGS: -lrados
|
|
|
|
// #include <errno.h>
|
|
|
|
// #include <stdlib.h>
|
|
|
|
// #include <rados/librados.h>
|
|
|
|
//
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"unsafe"
|
|
|
|
|
2022-03-30 08:46:01 +00:00
|
|
|
"github.com/ceph/go-ceph/internal/cutil"
|
2021-02-10 03:38:18 +00:00
|
|
|
ts "github.com/ceph/go-ceph/internal/timespec"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Timespec is a public type for the internal C 'struct timespec'
|
|
|
|
type Timespec ts.Timespec
|
|
|
|
|
|
|
|
// WriteOp manages a set of discrete actions that will be performed together
|
|
|
|
// atomically.
|
|
|
|
type WriteOp struct {
|
|
|
|
operation
|
|
|
|
op C.rados_write_op_t
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateWriteOp returns a newly constructed write operation.
|
|
|
|
func CreateWriteOp() *WriteOp {
|
|
|
|
return &WriteOp{
|
|
|
|
op: C.rados_create_write_op(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release the resources associated with this write operation.
|
|
|
|
func (w *WriteOp) Release() {
|
|
|
|
C.rados_release_write_op(w.op)
|
|
|
|
w.op = nil
|
|
|
|
w.free()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w WriteOp) operate2(
|
|
|
|
ioctx *IOContext, oid string, mtime *Timespec, flags OperationFlags) error {
|
|
|
|
|
|
|
|
if err := ioctx.validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cOid := C.CString(oid)
|
|
|
|
defer C.free(unsafe.Pointer(cOid))
|
|
|
|
var cMtime *C.struct_timespec
|
|
|
|
if mtime != nil {
|
|
|
|
cMtime = &C.struct_timespec{}
|
|
|
|
ts.CopyToCStruct(
|
|
|
|
ts.Timespec(*mtime),
|
|
|
|
ts.CTimespecPtr(cMtime))
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := C.rados_write_op_operate2(
|
|
|
|
w.op, ioctx.ioctx, cOid, cMtime, C.int(flags))
|
|
|
|
return w.update(writeOp, ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Operate will perform the operation(s).
|
|
|
|
func (w *WriteOp) Operate(ioctx *IOContext, oid string, flags OperationFlags) error {
|
|
|
|
return w.operate2(ioctx, oid, nil, flags)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OperateWithMtime will perform the operation while setting the modification
|
|
|
|
// time stamp to the supplied value.
|
|
|
|
func (w *WriteOp) OperateWithMtime(
|
|
|
|
ioctx *IOContext, oid string, mtime Timespec, flags OperationFlags) error {
|
|
|
|
|
|
|
|
return w.operate2(ioctx, oid, &mtime, flags)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WriteOp) operateCompat(ioctx *IOContext, oid string) error {
|
|
|
|
switch err := w.Operate(ioctx, oid, OperationNoFlag).(type) {
|
|
|
|
case nil:
|
|
|
|
return nil
|
|
|
|
case OperationError:
|
|
|
|
return err.OpError
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a rados object.
|
|
|
|
func (w *WriteOp) Create(exclusive CreateOption) {
|
|
|
|
// category, the 3rd param, is deprecated and has no effect so we do not
|
|
|
|
// implement it in go-ceph
|
|
|
|
C.rados_write_op_create(w.op, C.int(exclusive), nil)
|
|
|
|
}
|
|
|
|
|
2021-06-09 04:54:52 +00:00
|
|
|
// SetOmap appends the map `pairs` to the omap `oid`.
|
2021-02-10 03:38:18 +00:00
|
|
|
func (w *WriteOp) SetOmap(pairs map[string][]byte) {
|
2022-03-30 08:46:01 +00:00
|
|
|
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(
|
2021-02-10 03:38:18 +00:00
|
|
|
w.op,
|
2022-03-30 08:46:01 +00:00
|
|
|
(**C.char)(cKeys.BuffersPtr()),
|
|
|
|
(**C.char)(cValues.BuffersPtr()),
|
|
|
|
(*C.size_t)(cKeys.LengthsPtr()),
|
|
|
|
(*C.size_t)(cValues.LengthsPtr()),
|
|
|
|
(C.size_t)(len(pairs)))
|
2021-02-10 03:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RmOmapKeys removes the specified `keys` from the omap `oid`.
|
|
|
|
func (w *WriteOp) RmOmapKeys(keys []string) {
|
2022-03-30 08:46:01 +00:00
|
|
|
cKeys := cutil.NewBufferGroupStrings(keys)
|
|
|
|
defer cKeys.Free()
|
|
|
|
|
|
|
|
C.rados_write_op_omap_rm_keys2(
|
2021-02-10 03:38:18 +00:00
|
|
|
w.op,
|
2022-03-30 08:46:01 +00:00
|
|
|
(**C.char)(cKeys.BuffersPtr()),
|
|
|
|
(*C.size_t)(cKeys.LengthsPtr()),
|
|
|
|
(C.size_t)(len(keys)))
|
2021-02-10 03:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CleanOmap clears the omap `oid`.
|
|
|
|
func (w *WriteOp) CleanOmap() {
|
|
|
|
C.rados_write_op_omap_clear(w.op)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AssertExists assures the object targeted by the write op exists.
|
|
|
|
//
|
|
|
|
// Implements:
|
|
|
|
// void rados_write_op_assert_exists(rados_write_op_t write_op);
|
|
|
|
func (w *WriteOp) AssertExists() {
|
|
|
|
C.rados_write_op_assert_exists(w.op)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write a given byte slice at the supplied offset.
|
|
|
|
//
|
|
|
|
// Implements:
|
|
|
|
// void rados_write_op_write(rados_write_op_t write_op,
|
|
|
|
// const char *buffer,
|
|
|
|
// size_t len,
|
|
|
|
// uint64_t offset);
|
|
|
|
func (w *WriteOp) Write(b []byte, offset uint64) {
|
|
|
|
oe := newWriteStep(b, 0, offset)
|
|
|
|
w.steps = append(w.steps, oe)
|
|
|
|
C.rados_write_op_write(
|
|
|
|
w.op,
|
|
|
|
oe.cBuffer,
|
|
|
|
oe.cDataLen,
|
|
|
|
oe.cOffset)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteFull writes a given byte slice as the whole object,
|
|
|
|
// atomically replacing it.
|
|
|
|
//
|
|
|
|
// Implements:
|
|
|
|
// void rados_write_op_write_full(rados_write_op_t write_op,
|
|
|
|
// const char *buffer,
|
|
|
|
// size_t len);
|
|
|
|
func (w *WriteOp) WriteFull(b []byte) {
|
|
|
|
oe := newWriteStep(b, 0, 0)
|
|
|
|
w.steps = append(w.steps, oe)
|
|
|
|
C.rados_write_op_write_full(
|
|
|
|
w.op,
|
|
|
|
oe.cBuffer,
|
|
|
|
oe.cDataLen)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteSame write a given byte slice to the object multiple times, until
|
|
|
|
// writeLen is satisfied.
|
|
|
|
//
|
|
|
|
// Implements:
|
|
|
|
// void rados_write_op_writesame(rados_write_op_t write_op,
|
|
|
|
// const char *buffer,
|
|
|
|
// size_t data_len,
|
|
|
|
// size_t write_len,
|
|
|
|
// uint64_t offset);
|
|
|
|
func (w *WriteOp) WriteSame(b []byte, writeLen, offset uint64) {
|
|
|
|
oe := newWriteStep(b, writeLen, offset)
|
|
|
|
w.steps = append(w.steps, oe)
|
|
|
|
C.rados_write_op_writesame(
|
|
|
|
w.op,
|
|
|
|
oe.cBuffer,
|
|
|
|
oe.cDataLen,
|
|
|
|
oe.cWriteLen,
|
|
|
|
oe.cOffset)
|
|
|
|
}
|