sophia icon indicating copy to clipboard operation
sophia copied to clipboard

Unable to find document using cursor

Open kk00ss opened this issue 10 years ago • 7 comments

I probably don't understand something about Sophia, could anyone please explain what is wrong with the following code (except that it's written in D) :

    auto env = sophia.sp_env();
    sophia.sp_setstring(env,toStringz("sophia.path"),toStringz("/home/kos/"),0);
    sophia.sp_setint(env,toStringz("scheduler.threads"),0);
    sophia.sp_setstring(env,toStringz("log.path"),toStringz("/home/kos/sophiaLogs"),0);
    sophia.sp_open(env);

    sophia.sp_setstring(env, toStringz("db"), toStringz("test"), 0);
    sophia.sp_setstring(env, toStringz("db.test.path"), toStringz("/home/kos/sophiaDB"), 0);
    sophia.sp_setstring(env, toStringz("db.test.index.key"), toStringz("u32"), 0);
    sophia.sp_setint(env,toStringz("db.test.sync"),0);
    auto db = sophia.sp_getobject(env,"db.test");
    if(db == null)
        writefln("cannot open database test");
    sophia.sp_open(db);

    for(int i = 0; i < 10000; i++){
        auto doc = sophia.sp_document(db);
        if(doc == null)writeln("failed to create document");
        sophia.sp_setstring(doc,toStringz("key"),&i,0);
        sophia.sp_setstring(doc,toStringz("value"),&i,0);
        writeln(i);
        sophia.sp_set(db,doc);
        //sophia.sp_destroy(doc);
    }

    auto cursor = sophia.sp_cursor(env);
    auto doc = sophia.sp_document(db);
    auto key = 9999;
    sophia.sp_setstring(doc,toStringz("key"),&key,0);
    auto res = sophia.sp_get(cursor,doc);
    if(res == null)
        writeln("no such object");
    else writeln(res.stringof);
    sophia.sp_destroy(cursor);
    //uint value = *cast(uint*)sophia.sp_getstring(res,toStringz("value"),null);
    //printf("result value %d",value);

    sophia.sp_destroy(env);

kk00ss avatar Nov 12 '15 15:11 kk00ss

Hi, sp_setstring() accepts following arguments: sp_setstring(object, path, data_pointer, data_size).

When data_size is 0, sp_setstring() tries to do strlen(data_pointer), last argument should be exact key size. Probably that could be the cause.

Are you making D bindings for Sophia? :)

pmwkaa avatar Nov 12 '15 16:11 pmwkaa

I've already tried to run it with key_size = 4 bytes - same problem. I'm not proficient in D, but it seems easiest way to use low level storage engine with garbage-collected language. I'm evaluating different storage-engines for my side project.

kk00ss avatar Nov 12 '15 16:11 kk00ss

I've tried to repeat the case, it really should work when the size is passed correctly: https://github.com/pmwkaa/sophia/blob/master/test/generic/github.test.c#L19

Are you sure you tried this with sp_set() and sp_setststring() for cursor key as well?

pmwkaa avatar Nov 12 '15 16:11 pmwkaa

sp_set and get instead of sp_setstring ? I'll try cpp version as well, do you test it on master, or particular version of sophiadb ?

kk00ss avatar Nov 12 '15 16:11 kk00ss

   for(int i = 0; i < 10000; i++){
        auto doc = sophia.sp_document(db);
        if(doc == null)writeln("failed to create document");
        sophia.sp_setstring(doc,toStringz("key"),&i, sizeof(i) );    // <<
        sophia.sp_setstring(doc,toStringz("value"),&i, sizeof(i) ); // <<
        writeln(i);
        sophia.sp_set(db,doc);
        //sophia.sp_destroy(doc);
    }
   ...
   auto key = 9999;
   sophia.sp_setstring(doc,toStringz("key"),&key, sizeof(key) ); // and size here

Also, i'm not sure. Does D correctly converts autho key = 9999 as int key? Maybe it has a different size.

pmwkaa avatar Nov 12 '15 16:11 pmwkaa

that doesn't work for me.

kk00ss avatar Nov 12 '15 16:11 kk00ss

Master is development branch and contains changes for a next release. Current release is 1.2.3, on the same branch. It might have slightly different API.

pmwkaa avatar Nov 12 '15 16:11 pmwkaa