Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node wrapper #70

Open
wshager opened this issue Feb 5, 2016 · 19 comments
Open

Node wrapper #70

wshager opened this issue Feb 5, 2016 · 19 comments

Comments

@wshager
Copy link

wshager commented Feb 5, 2016

I'm trying to create a node wrapper using node-ffi. It seems pretty straightforward, but I get a segfault when trying to work with cursors. Please consider the code: https://gist.github.com/wshager/edf7bb5d58d4873fea88

@cruppstahl
Copy link
Owner

Can you tell me how i can run this? I have always been curious about
node.js, but have zero knowledge about it.

2016-02-05 15:18 GMT+01:00 Wouter Hager [email protected]:

I'm trying to create a node wrapper using node-ffi It seems pretty
straightforward, but I get a segfault when trying to work with cursors
Please consider the code:
https://gistgithubcom/wshager/edf7bb5d58d4873fea88


Reply to this email directly or view it on GitHub
#70.

@wshager
Copy link
Author

wshager commented Feb 5, 2016

Simply install node and run node ups.js
You'll need to install the node-ffi dependency using npm:
npm install ffi

https://github.com/node-ffi/node-ffi

@wshager
Copy link
Author

wshager commented Feb 5, 2016

Docs for node-ref: https://tootallnate.github.io/ref/

@cruppstahl
Copy link
Owner

I got it running.

When I insert a lot of "console.log()" statements, then I see that it
crashes in the function check_error, called by open_env. More precisely, it
crashes when calling ups.ups_strerror(st);

I tried to change the declaration of ups_strerror to

'ups_strerror': ['string', [ups_status_t]]

but this still did not work. When I remove the call to ups_strerror then
everything seems to work, although it runs into a different error:

opened env <Buffer@0xa87170 00 00 00 00 00 00 00 00>

/mnt/f139c910-8da7-4153-a314-ca180468b45b/prj/nodejs/node_modules/ffi/lib/_foreign_function.js:55
throw e
^
TypeError: error setting argument 2 - value is out of bounds
at TypeError ()
at checkInt (buffer.js:784:11)
at Buffer.writeInt32LE (buffer.js:916:5)
at Object.set
(/mnt/f139c910-8da7-4153-a314-ca180468b45b/prj/nodejs/node_modules/ffi/node_modules/ref/lib/ref.js:923:52)
at Object.set
(/mnt/f139c910-8da7-4153-a314-ca180468b45b/prj/nodejs/node_modules/ffi/node_modules/ref/lib/ref.js:482:10)
at Object.alloc
(/mnt/f139c910-8da7-4153-a314-ca180468b45b/prj/nodejs/node_modules/ffi/node_modules/ref/lib/ref.js:516:13)
at Object.ForeignFunction.proxy as ups_env_open_db
at open_db
(/mnt/f139c910-8da7-4153-a314-ca180468b45b/prj/nodejs/ups.js:110:16)
at Object.
(/mnt/f139c910-8da7-4153-a314-ca180468b45b/prj/nodejs/ups.js:66:10)
at Module._compile (module.js:456:26)

Do you know how to fix ups_strerror? It looks to me like node is not able
to convert the temporary char * pointer to a javascript string.

2016-02-05 15:36 GMT+01:00 Wouter Hager [email protected]:

Docs for node-ref: https://tootallnate.github.io/ref/


Reply to this email directly or view it on GitHub
#70 (comment)
.

@wshager
Copy link
Author

wshager commented Feb 8, 2016

@cruppstahl Ah yes, I was looking in the wrong place. You're right, string seems to be the correct type for anywhere that I've used char_ptr. I get a lot of 'unknow errors' now, unfortunately. I'll update the gist.

@wshager
Copy link
Author

wshager commented Feb 8, 2016

@cruppstahl the status is stringified in the line before the actual call to ups_strerror and its subsequent output. Perhaps the contents of the status pointer could tell you something?

@wshager
Copy link
Author

wshager commented Feb 8, 2016

@cruppstahl Ah of course, I think it says 'unknown error' because there is none. Please ignore the previous comment, the data of status is empty every time!

@wshager
Copy link
Author

wshager commented Feb 8, 2016

@cruppstahl I've updated the gist to a working script, but I don't understand the values of the buffers. It's getting late, I'll continue tomorrow.

https://gist.github.com/wshager/edf7bb5d58d4873fea88

@wshager
Copy link
Author

wshager commented Mar 28, 2017

@cruppstahl Are you still interested in supporting node? I would like to give this another try, but I'm having trouble with doing inserts, as I don't have the correct key/record types.

@cruppstahl
Copy link
Owner

If you want to work on it then go ahead, but I'm so swamped right now that I won't be able to help much. It does not have high priority for me, though.

@wshager
Copy link
Author

wshager commented Mar 29, 2017

@cruppstahl Ok, thanks for your reply, I figured you would be. I hope and assume your hard work will pay off.

I made little progress, but it seems I was able to remove some data. One of the problems is that node can't seem to write an empty db file on its own, so I wondered if that is something you could help with.

@wshager
Copy link
Author

wshager commented Mar 29, 2017

Ah my bad, I was using the wrong function to create a db file.

@cruppstahl
Copy link
Owner

cruppstahl commented Mar 30, 2017 via email

@wshager
Copy link
Author

wshager commented Mar 30, 2017

Hi, I got something working! I've updated the gist (https://gist.github.com/wshager/edf7bb5d58d4873fea88).

Some things to consider:

  • I tried to create keys/records with generic types, but I couldn't get it to work. I'm not sure, as I don't know enough about ref-struct, but I guess the problem is that CString pointers need to be allocated as such. I have the same problem with key/record pointers in cursors: I need to pass a CString or it will give an 'invalid parameter' error (-8).
  • When clearing all keys with cursor_erase, the database is larger than when first created. I can understand space is allocated for the btree, but I can see some record garbage in the file.

Thanks in advance for your response.

@wshager
Copy link
Author

wshager commented Mar 30, 2017

Aargh I'm not sharp enough for this job... Ignore the last bit, I forgot to close the db. Will update the gist now.

@wshager
Copy link
Author

wshager commented May 10, 2017

@cruppstahl I can't set parameters on ups_env_create_db, as I can't reproduce the correct struct array using ref-struct and ref-array. I really want to avoid having to write a generic connector in C.

@wshager
Copy link
Author

wshager commented May 10, 2017

Persistence pays off once again. I got the parameter array working. The trick was to pass params.buffer.

var Struct = require('ref-struct');
var ArrayType = require('ref-array');
var ups_parameter_t = Struct({
    'name': ref.types.uint32,
    'value': ref.types.uint64
});
const UPS_PARAM_KEY_TYPE = 0x00000104;
const UPS_TYPE_UINT32 = 7;
var Params = ArrayType(ups_parameter_t);
var db = ref.alloc('pointer');
var params = new Params(2);
params[0] = new ups_parameter_t({
    name: UPS_PARAM_KEY_TYPE,
    value: UPS_TYPE_UINT32 
});
params[1] = new ups_parameter_t({
    name: 0,
    value:0
});
var st = ups.ups_env_create_db(env.deref(), db, nr, 0, params.buffer);

@paulocoghi
Copy link

Hello Wouter and Christoph, sorry to "revive" this old issue.

I would like to ask Wouter if the Node wrapper worked and ask Christoph if you are still using UpscaleDB in production, because it seems really mature.

Thanks!

@cruppstahl
Copy link
Owner

Hi @paulocoghi - mature yes, but I'm no longer actively maintaining upscaledb. Using it is at your own risk!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants