rados: add Write, WriteFull, and WriteSame WriteOp functions

Add Write implementing rados_write_op_write.
Add WriteFull implementing rados_write_op_write_full.
Add WriteSame implementing rados_write_op_writesame.

These are some of the basic functions needed to write data to
a rados object using a WriteOp.

Signed-off-by: John Mulligan <jmulligan@redhat.com>
This commit is contained in:
John Mulligan 2020-06-25 15:35:13 -04:00 committed by mergify[bot]
parent e4e1257768
commit c8d8d8c364
2 changed files with 86 additions and 0 deletions

View File

@ -124,3 +124,56 @@ func (w *WriteOp) CleanOmap() {
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)
}

33
rados/write_step.go Normal file
View File

@ -0,0 +1,33 @@
package rados
// #include <stdint.h>
import "C"
import (
"unsafe"
)
type writeStep struct {
withoutUpdate
withoutFree
// the c pointer utilizes the Go byteslice data and no free is needed
// inputs:
b []byte
// arguments:
cBuffer *C.char
cDataLen C.size_t
cWriteLen C.size_t
cOffset C.uint64_t
}
func newWriteStep(b []byte, writeLen, offset uint64) *writeStep {
return &writeStep{
b: b,
cBuffer: (*C.char)(unsafe.Pointer(&b[0])),
cDataLen: C.size_t(len(b)),
cWriteLen: C.size_t(writeLen),
cOffset: C.uint64_t(offset),
}
}