Skip to content
This repository has been archived by the owner on Jan 15, 2019. It is now read-only.

Commit

Permalink
Merge pull request #15 from gierschv/feat-stat
Browse files Browse the repository at this point in the history
ioctx: Stat added
  • Loading branch information
dotnwat committed Apr 27, 2015
2 parents dc2948d + 1779dc7 commit c7a0450
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
32 changes: 32 additions & 0 deletions rados/ioctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package rados
import "C"

import "unsafe"
import "time"

// PoolStat represents Ceph pool statistics.
type PoolStat struct {
Expand All @@ -31,6 +32,14 @@ type PoolStat struct {
Num_wr_kb uint64
}

// ObjectStat represents an object stat information
type ObjectStat struct {
// current length in bytes
Size uint64
// last modification time
ModTime time.Time
}

// IOContext represents a context for performing I/O within a pool.
type IOContext struct {
ioctx C.rados_ioctx_t
Expand Down Expand Up @@ -192,6 +201,29 @@ func (ioctx *IOContext) ListObjects(listFn ObjectListFunc) error {
panic("invalid state")
}

// Stat returns the size of the object and its last modification time
func (ioctx *IOContext) Stat(object string) (stat ObjectStat, err error) {
var c_psize C.uint64_t
var c_pmtime C.time_t
c_object := C.CString(object)
defer C.free(unsafe.Pointer(c_object))

ret := C.rados_stat(
ioctx.ioctx,
c_object,
&c_psize,
&c_pmtime)

if ret < 0 {
return ObjectStat{}, RadosError(int(ret))
} else {
return ObjectStat{
Size: uint64(c_psize),
ModTime: time.Unix(int64(c_pmtime), 0),
}, nil
}
}

// GetXattr gets an xattr with key `name`, it returns the length of
// the key read or an error if not successful
func (ioctx *IOContext) GetXattr(object string, name string, data []byte) (int, error) {
Expand Down
24 changes: 24 additions & 0 deletions rados/rados_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,30 @@ func TestReadWrite(t *testing.T) {
pool.Destroy()
}

func TestObjectStat(t *testing.T) {
conn, _ := rados.NewConn()
conn.ReadDefaultConfigFile()
conn.Connect()

pool_name := GetUUID()
err := conn.MakePool(pool_name)
assert.NoError(t, err)

pool, err := conn.OpenIOContext(pool_name)
assert.NoError(t, err)

bytes_in := []byte("input data")
err = pool.Write("obj", bytes_in, 0)
assert.NoError(t, err)

stat, err := pool.Stat("obj")
assert.Equal(t, uint64(len(bytes_in)), stat.Size)
assert.NotNil(t, stat.ModTime)

pool.Destroy()
conn.Shutdown()
}

func TestGetPoolStats(t *testing.T) {
conn, _ := rados.NewConn()
conn.ReadDefaultConfigFile()
Expand Down

0 comments on commit c7a0450

Please sign in to comment.