Skip to content

Commit

Permalink
basic object status functionality
Browse files Browse the repository at this point in the history
Signed-off-by: R.I.Pienaar <rip@devco.net>
  • Loading branch information
ripienaar committed Oct 11, 2021
1 parent 1655009 commit 108f022
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
31 changes: 31 additions & 0 deletions object.go
Expand Up @@ -89,6 +89,8 @@ type ObjectStore interface {

// List will list all the objects in this store.
List(opts ...WatchOpt) ([]*ObjectInfo, error)

Status() (*ObjectStoreStatus, error)
}

type ObjectOpt interface {
Expand Down Expand Up @@ -132,6 +134,13 @@ type ObjectStoreConfig struct {
Replicas int
}

// ObjectStoreStatus is the status of the object store
type ObjectStoreStatus struct {
ObjectStoreConfig
Sealed bool
Size uint64
}

// ObjectMetaOptions
type ObjectMetaOptions struct {
Link *ObjectLink `json:"link,omitempty"`
Expand Down Expand Up @@ -857,6 +866,28 @@ func (obs *obs) List(opts ...WatchOpt) ([]*ObjectInfo, error) {
return objs, nil
}

// Status retrieves run-time status about a bucket
func (obs *obs) Status() (*ObjectStoreStatus, error) {
nfo, err := obs.js.StreamInfo(obs.stream)
if err != nil {
return nil, err
}

status := &ObjectStoreStatus{
Sealed: nfo.Config.Sealed,
Size: nfo.State.Bytes,
ObjectStoreConfig: ObjectStoreConfig{
Bucket: obs.name,
Description: nfo.Config.Description,
TTL: nfo.Config.MaxAge,
Storage: nfo.Config.Storage,
Replicas: nfo.Config.Replicas,
},
}

return status, nil
}

// Read impl.
func (o *objResult) Read(p []byte) (n int, err error) {
o.Lock()
Expand Down
17 changes: 16 additions & 1 deletion test/object_test.go
Expand Up @@ -34,7 +34,7 @@ func TestObjectBasics(t *testing.T) {
nc, js := jsClient(t, s)
defer nc.Close()

obs, err := js.CreateObjectStore(&nats.ObjectStoreConfig{Bucket: "OBJS"})
obs, err := js.CreateObjectStore(&nats.ObjectStoreConfig{Bucket: "OBJS", Description: "testing"})
expectOk(t, err)

// Create ~16MB object.
Expand Down Expand Up @@ -67,6 +67,21 @@ func TestObjectBasics(t *testing.T) {
t.Fatalf("Expected the object stream to be sealed, got %+v", si)
}

status, err := obs.Status()
expectOk(t, err)
if !status.Sealed {
t.Fatalf("exected sealed status")
}
if status.Size == 0 {
t.Fatalf("size is 0")
}
if status.Storage != nats.FileStorage {
t.Fatalf("stauts reports %d storage", status.Storage)
}
if status.Description != "testing" {
t.Fatalf("invalid description: '%s'", status.Description)
}

// Check simple errors.
_, err = obs.Get("FOO")
expectErr(t, err)
Expand Down

0 comments on commit 108f022

Please sign in to comment.