ericnorris / node-cdb Goto Github PK
View Code? Open in Web Editor NEWA cdb implementation for node.js
License: MIT License
A cdb implementation for node.js
License: MIT License
I don't think that the original djb hash function was intended to work on arrays with values > 256.
Best solution is to convert the key to a buffer first.
In lines https://github.com/ericnorris/node-cdb/blob/master/src/readable-cdb.js#L122-L128
The offset should be reduced only if the payload is the same, i.e.
if (buffer.toString() == key) {
if (offset === 0) {
fs.read(self.fd, new Buffer(dataLength), 0, dataLength, recordPosition + 8 + keyLength, returnData);
} else {
offset--;
readSlot(++slot);
}
} else {
readSlot(++slot);
}
In line https://github.com/ericnorris/node-cdb/blob/master/src/readable-cdb.js#L92
To assume that the slot is empty, it is better to check that recordPosition === 0
First, thank you for making available what appears to be the only node module for writing cdb files!
I'm having some trouble with writing/reading characters beyond ASCII.
Here's my code for writing two values to the same key, the first being in Chinese:
var cdb = require('constant-db');
var writer = new cdb.writable('./test.cdb');
writer.open(function (err) {
if (err) {
throw err;
}
writer.put('myKey', '你好');
writer.put('myKey', 'test value!');
writer.close(function (err) {
if (err) {
throw err;
}
});
});
And here's my code for reading the values:
var cdb = require('constant-db');
var reader = new cdb.readable('./test.cdb');
reader.open(function (err) {
if (err) {
throw err;
}
reader.get('myKey', function (err, data) {
if (err) {
throw err;
}
console.log(data);
var buf = new Buffer(data);
console.log(buf.toString('utf8'));
reader.getNext(function (err, data) {
if (err) {
throw err;
}
console.log(data);
var buf = new Buffer(data);
console.log(buf.toString('utf8'));
reader.close(function (err) {
if (err) {
throw err;
}
});
});
});
});
And here's the output:
<Buffer 00 00>
<Buffer 74 65 73 74 21>
test value!
Notice that the buffer for the first value is all zeroes. Am I doing something wrong, or is this a bug?
This is probably a bit misplaced and you are not a newbee JS tutor. So sorry.
But:
Is it troublesome to extend your excellent examples to the typical user case where you call it from inside a function and the data part is not simply printed to the console.log, but passed back through the callback chain to the calling function? I guess it is easy if you know how to do it, but I am pulling my hairs on these freaky callback chains 📦
var readable = require('constant-db').readable;
var getTheRecord = function(key){
var reader = new readable('./cdbfile');
reader.open(function cdbOpened(err) {
reader.get( key , function gotRecord(err, data) {
return data; // instead of console.log(data);
reader.close(function cdbClosed(err) {
console.log('awesome!');
});
});
});
});
Which gives you "hello world!" when calling
console.log( getTheRecord('meow'));
I am banging my head against the wall :) This exercise is very informative about the intention of nonblocking code - that it does not block and not wait... whatever I do I get returned "undefined" since the engine simply does not wait for the variable to return.... I looked at promises, but I think one would rather use callbacks.
Sorry for bugging you, but I suspect it is an easy issue for a node pro?
Thanks anyway for your brilliant tool and I will figure it out anyway anyway :)
R
Hi,
I am new to js, node and cdb. I started writing my first small scripts and want to read from a cdb.
When pasting your small examples for writable and readable into say "WritableCDB.js" and "ReadableCDB.js" and executing them in the shell with "node Writable.js" and "node Readable.js" the former is working correctly and the cdbfile is correctly created - I validated the file with freecdb.
But:
node ReadableCDB.js
returns "undefined" and not "hello world".
I changed "console.log(data)" to "console.log(err,data)" and the error code is
[Error: EBADF, read] errno: 9, code: 'EBADF'
I did some search on this one and it seems that close() is called before the data is read. I have not understood the callback structure good enough to know how to correct it, but commenting out the close() line returns the data.
But: in hex encoding :)
So:
reader.open(function cdbOpened(err) {
reader.get('meow', function gotRecord(err, data) {
console.log( data.toString() );
});
});
Produces "hello world", but then the connection is not closed.
Would be curious how to get the callback right.
Thanks,
Reinhard
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.