From f39179553f2fedd8b5cc5f0f83b3bb72b9e45db8 Mon Sep 17 00:00:00 2001 From: Ye Yin Date: Mon, 20 Jul 2015 20:37:32 +0800 Subject: [PATCH] Add rados_write_full function Signed-off-by: Ye Yin --- rados/ioctx.go | 18 ++++++++++++++++++ rados/rados_test.go | 10 ++++++++++ 2 files changed, 28 insertions(+) diff --git a/rados/ioctx.go b/rados/ioctx.go index e6ee0c8..1cdfbbb 100644 --- a/rados/ioctx.go +++ b/rados/ioctx.go @@ -68,6 +68,24 @@ func (ioctx *IOContext) Write(oid string, data []byte, offset uint64) error { } } +// WriteFull writes len(data) bytes to the object with key oid. +// The object is filled with the provided data. If the object exists, +// it is atomically truncated and then written. It returns an error, if any. +func (ioctx *IOContext) WriteFull(oid string, data []byte) error { + c_oid := C.CString(oid) + defer C.free(unsafe.Pointer(c_oid)) + + ret := C.rados_write_full(ioctx.ioctx, c_oid, + (*C.char)(unsafe.Pointer(&data[0])), + (C.size_t)(len(data))) + + if ret == 0 { + return nil + } else { + return RadosError(int(ret)) + } +} + // Read reads up to len(data) bytes from the object with key oid starting at byte // offset offset. It returns the number of bytes read and an error, if any. func (ioctx *IOContext) Read(oid string, data []byte, offset uint64) (int, error) { diff --git a/rados/rados_test.go b/rados/rados_test.go index f8a144e..655578c 100644 --- a/rados/rados_test.go +++ b/rados/rados_test.go @@ -307,6 +307,16 @@ func TestReadWrite(t *testing.T) { assert.Equal(t, n_out, len(bytes_in)) assert.Equal(t, bytes_in, bytes_out) + bytes_in = []byte("input another data") + err = pool.WriteFull("obj", bytes_in) + assert.NoError(t, err) + + bytes_out = make([]byte, len(bytes_in)) + n_out, err = pool.Read("obj", bytes_out, 0) + + assert.Equal(t, n_out, len(bytes_in)) + assert.Equal(t, bytes_in, bytes_out) + pool.Destroy() }