Merge pull request #27 from hustcat/rados_write_full

Add rados_write_full function

Signed-off-by: Noah Watkins <noahwatkins@gmail.com>
This commit is contained in:
Noah Watkins 2015-07-20 07:23:42 -06:00
commit cd051751e4
2 changed files with 28 additions and 0 deletions

View File

@ -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) {

View File

@ -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()
}