node-rados icon indicating copy to clipboard operation
node-rados copied to clipboard

Add list objects in a pool

Open wcchh opened this issue 10 years ago • 4 comments

Hi, I need a list function about specified pool, and I have added this function at my local for test. Could someone could help review it and let it be support function? Thanks in advance~

[at rados.h] ... class Ioctx : public node::ObjectWrap { ... static NAN_METHOD(aio_objects_list); }

[at rados.cc] ...

define ENOENT 2

NAN_METHOD(Ioctx::aio_objects_list) { NanScope();

if (args.Length() < 1 || !args[0]->IsString()) { return NanThrowError("Bad argument."); }

Ioctx* obj = ObjectWrap::Unwrap<Ioctx>(args.This()); if ( !obj->require_created() ) NanReturnNull();

rados_list_ctx_t h_ctx; //Start listing objects in a pool. int err = rados_objects_list_open(obj->ioctx, &h_ctx); if (err < 0) { return NanThrowError("open list failed."); }

Local<Array> ret_list = NanNew<Array>(); uint32_t array_id = 0; //Get the next object name and locator in the pool.

while(0 <= err) { const char *obj_name; err = rados_objects_list_next(h_ctx, &obj_name, NULL); if (err == 0) { ret_list->Set(array_id, NanNew<String>(obj_name)); array_id++; } } rados_objects_list_close(h_ctx);

if (err < 0 && err != -ENOENT) { return NanThrowError("list_next failed."); }

NanReturnValue(ret_list); }

wcchh avatar Jan 26 '15 07:01 wcchh

Hi, Thank you, I created a branch wip-list-objects to test the code. (https://github.com/ksperis/node-rados/tree/wip-list-objects) I will look in more detail later.

ksperis avatar Jan 26 '15 09:01 ksperis

I think obj_name is leaked here. Who's responsible for freeing obj_name? Could probably NanBufferUse to hand off the buffer to V8.

siboulet avatar Jan 26 '15 15:01 siboulet

NanNew (obj_name) isn't it enough to hand off the buffer ?

ksperis avatar Feb 17 '15 09:02 ksperis

NanNew creates a copy. For buffers you can use NanBufferUse that tells V8 it should take ownership of the buffer.

However after re-reading the code I realize obj_name is a pointer to some allocation inside h_ctx and probably freed by librados when calling rados_objects_list_close.

siboulet avatar Feb 18 '15 03:02 siboulet