예제 #1
0
파일: object.go 프로젝트: glycerine/radosgo
// ReadAt reads len(data) bytes from the given RADOS object at the byte
// offset off. It returns the number of bytes read and the error, if any.
// ReadAt always returns a non-nil error when n < len(data).
// At the end of file, that error is io.EOF.
//
// This function adopted from the Go os.ReadAt() function.
func (c *Context) ReadAt(key string, data []byte, off uint64) (n int, err error) {
	cname := C.CString(key)
	defer C.free(unsafe.Pointer(cname))

	for len(data) > 0 {
		cdata, cdatalen := byteSliceToBuffer(data)
		coff := C.uint64_t(off)

		cerr := C.rados_read(c.ctx, cname, cdata, cdatalen, coff)

		if cerr == 0 {
			return n, io.EOF
		}

		if cerr < 0 {
			err = radosReturnCodeError(cerr)
			break
		}

		n += int(cerr)
		data = data[cerr:]
	}

	return
}
예제 #2
0
// ReadAt reads len(data) bytes from the given RADOS object at the byte
// offset off. It returns the number of bytes read and the error, if any.
// ReadAt always returns a non-nil error when n < len(data).
// At the end of file, that error is io.EOF.
//
// This function adopted from the Go os.ReadAt() function.
func (o *Object) ReadAt(data []byte, off int64) (n int, err error) {
	cname := C.CString(o.name)
	defer C.free(unsafe.Pointer(cname))

	for len(data) > 0 {
		cdata, cdatalen := byteSliceToBuffer(data)
		coff := C.uint64_t(off)

		cerr := C.rados_read(o.c.ctx, cname, cdata, cdatalen, coff)

		if cerr == 0 {
			return n, io.EOF
		}

		if cerr < 0 {
			err = fmt.Errorf("RADOS read %s: %s", o.name, strerror(cerr))
			break
		}

		n += int(cerr)
		data = data[cerr:]
		off += int64(cerr)
	}

	return
}
예제 #3
0
파일: rados_io.go 프로젝트: vuleetu/gorados
func (r *RadosIoCtx) ReadRaw(oid string, length, offset uint64, buf unsafe.Pointer) int {
	coid := C.CString(oid)
	defer func() {
		C.free(unsafe.Pointer(coid))
	}()
	cerr := C.rados_read(*r.ctx, coid, (*C.char)(buf), C.size_t(length), C.uint64_t(offset))
	return int(cerr)
}
예제 #4
0
// Read reads a specified length of data from the object starting at the given offset.
func (o *Object) Read(length, offset uint64) (io.Reader, error) {
	oid := C.CString(o.name)
	defer freeString(oid)
	bufAddr := bufferAddress(int(length))
	ret := C.rados_read(o.ioContext, oid, bufAddr, C.size_t(length), C.uint64_t(offset))
	if err := toRadosError(ret); err != nil {
		err.Message = fmt.Sprintf("Unable to read object %s.", o.name)
		return nil, err
	}
	return bufToReader(bufAddr, ret), nil
}
예제 #5
0
파일: rados_io.go 프로젝트: vuleetu/gorados
func (r *RadosIoCtx) Read(oid string, length, offset uint64) ([]byte, error) {
	var buf = make([]byte, length)
	coid := C.CString(oid)
	defer func() {
		C.free(unsafe.Pointer(coid))
	}()
	cerr := C.rados_read(*r.ctx, coid, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(length), C.uint64_t(offset))
	if cerr < 0 {
		return nil, errors.New("read data failed")
	}
	return buf, nil
}
예제 #6
0
파일: ioctx.go 프로젝트: mittalms/go-ceph
// 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) {
	if len(data) == 0 {
		return 0, nil
	}

	c_oid := C.CString(oid)
	defer C.free(unsafe.Pointer(c_oid))

	ret := C.rados_read(
		ioctx.ioctx,
		c_oid,
		(*C.char)(unsafe.Pointer(&data[0])),
		(C.size_t)(len(data)),
		(C.uint64_t)(offset))

	if ret >= 0 {
		return int(ret), nil
	} else {
		return 0, GetRadosError(ret)
	}
}
예제 #7
0
// Get reads all the data in the named object in the pool referenced by
// the given context. The data is returned as a byte slice.
//
// If the object does not exist, an error is returned.
// If the object contains no data, an empty slice is returned.
func (c *Context) Get(name string) ([]byte, error) {
	obj, err := c.Stat(name)

	if err != nil {
		return nil, err
	}

	if obj.Size() == 0 {
		// Return an empty slice
		return make([]byte, 0), nil
	}

	cname := C.CString(name)
	defer C.free(unsafe.Pointer(cname))

	data := make([]byte, obj.Size())
	cdata, cdatalen := byteSliceToBuffer(data)

	if cerr := C.rados_read(c.ctx, cname, cdata, cdatalen, 0); cerr < 0 {
		return nil, fmt.Errorf("RADOS get %s: %s", name, strerror(cerr))
	}

	return data, nil
}