Skip to content
This repository

parseConfiguration #8

Closed
wants to merge 1 commit into from

1 participant

Federico Fissore
Federico Fissore
Collaborator

please take a look at ffissore/node-orientdb@a1d80ed#L1R92

the callback was called with the result of the last function, thus NOT giving back the sessionId. now the sessionId is the only var got back by the callback

Federico Fissore ffissore closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Mar 22, 2012
Federico Fissore ffissore parseConfiguration. removed hard coded schema document RID, now fetch…
… it from config
a1d80ed
This page is out of date. Refresh to see the latest.
129 lib/orientdb/connection/parser.js
@@ -501,6 +501,134 @@ var stringToBytes = function(data) {
501 501 };
502 502
503 503
  504 +// See OStorageConfiguration.fromStream(byte[]) method in the java implementation
  505 +var parseConfiguration = function(configString) {
  506 + var values = configString.split("|");
  507 +
  508 + var read = function(value, offset) {
  509 + if (value === " ") {
  510 + return null;
  511 + }
  512 + if (typeof offset !== "undefined") {
  513 + return value.substr(offset);
  514 + }
  515 + return value;
  516 + };
  517 +
  518 + var phySegmentFromStream = function(config, values, index) {
  519 + var fileTemplate = { };
  520 + fileTemplate.maxSize = read(values[index++]);
  521 + fileTemplate.fileType = read(values[index++]);
  522 + fileTemplate.fileStartSize = read(values[index++]);
  523 + fileTemplate.fileMaxSize = read(values[index++]);
  524 + fileTemplate.fileIncrementSize = read(values[index++]);
  525 + fileTemplate.defrag = read(values[index++]);
  526 +
  527 + fileTemplate.infoFiles = [];
  528 + var size = parseInt(read(values[index++]));
  529 + for (var i = 0; i < size; i++) {
  530 + var infoFile = { };
  531 + infoFile.fileName = read(values[index++]);
  532 + infoFile.path = read(values[index++]);
  533 + infoFile.type = read(values[index++]);
  534 + fileTemplate.infoFiles.push(infoFile);
  535 + }
  536 +
  537 + config.fileTemplate = fileTemplate;
  538 +
  539 + return index;
  540 + };
  541 +
  542 + var clustersFromStream = function(config, values, index) {
  543 + config.clusters = [];
  544 + var size = parseInt(read(values[index++]));
  545 + for (var i = 0; i < size; i++) {
  546 + var clusterId = parseInt(read(values[index++]));
  547 + if (clusterId == -1) {
  548 + continue;
  549 + }
  550 +
  551 + var cluster = { };
  552 + cluster.clusterId = clusterId;
  553 + cluster.clusterName = read(values[index++]);
  554 + cluster.clusterType = read(values[index++]);
  555 +
  556 + if (cluster.clusterType === "p") {
  557 + index = phySegmentFromStream(cluster, values, index);
  558 + cluster.holeFile = {}
  559 + cluster.holeFile.path = read(values[index++]);
  560 + cluster.holeFile.type = read(values[index++]);
  561 + cluster.holeFile.maxSize = read(values[index++]);
  562 + } else if (cluster.clusterType === "l") {
  563 + cluster.physicalClusterId = parseInt(read(values[index++]));
  564 + cluster.map = read(values[index++]);
  565 + }
  566 +
  567 + config.clusters.push(cluster);
  568 + }
  569 + return index;
  570 + };
  571 +
  572 + var dataSegmentsFromStream = function(config, values, index) {
  573 + config.dataSegments = [];
  574 + var size = parseInt(read(values[index++]));
  575 + for (var i = 0; i < size; i++) {
  576 + var dataSegment = { };
  577 + dataSegment.dataId = parseInt(read(values[index++]));
  578 + dataSegment.dataName = read(values[index++]);
  579 + index = phySegmentFromStream(dataSegment, values, index);
  580 + dataSegment.holeFile = {}
  581 + dataSegment.holeFile.path = read(values[index++]);
  582 + dataSegment.holeFile.type = read(values[index++]);
  583 + dataSegment.holeFile.maxSize = read(values[index++]);
  584 + config.dataSegments.push(dataSegment);
  585 + }
  586 + return index;
  587 + };
  588 +
  589 + var propertiesFromStream = function(config, values, index) {
  590 + config.properties = [];
  591 + var size = parseInt(read(values[index++]));
  592 + for (var i = 0; i < size; i++) {
  593 + var property = { };
  594 + property.name = read(values[index++]);
  595 + property.value = read(values[index++]);
  596 + config.properties.push(property);
  597 + }
  598 + return index;
  599 + };
  600 +
  601 + var index = 0;
  602 + var config = { };
  603 + config.version = parseInt(read(values[index++]));
  604 + config.name = read(values[index++]);
  605 + config.schemaRecordId = read(values[index++], 1);
  606 + config.dictionaryRecordId = read(values[index++], 1);
  607 + if (config.version > 0) {
  608 + config.indexMgrRecordId = read(values[index++], 1);
  609 + } else {
  610 + config.indexMgrRecordId = null;
  611 + }
  612 + config.localeLanguage = read(values[index++]);
  613 + config.localeCountry = read(values[index++]);
  614 + config.dateFormat = read(values[index++]);
  615 + config.dateTimeFormat = read(values[index++]);
  616 + if (config.version > 1) {
  617 + index = phySegmentFromStream(config, values, index);
  618 + }
  619 + index = clustersFromStream(config, values, index);
  620 + index = dataSegmentsFromStream(config, values, index);
  621 + config.txSegment = { };
  622 + config.txSegment.path = read(values[index++]);
  623 + config.txSegment.type = read(values[index++]);
  624 + config.txSegment.maxSize = read(values[index++]);
  625 + config.txSegment.synchRecord = read(values[index++]) === "true";
  626 + config.txSegment.synchTx = read(values[index++]) === "true";
  627 + index = propertiesFromStream(config, values, index);
  628 +
  629 + return config;
  630 +};
  631 +
504 632 exports.readByte = readByte;
505 633 exports.readBytes = readBytes;
506 634 exports.readShort = readShort;
@@ -522,4 +650,5 @@ exports.writeShort = writeShort;
522 650 exports.writeBytes = writeBytes;
523 651 exports.writeString = writeString;
524 652 exports.stringToBytes = stringToBytes;
  653 +exports.parseConfiguration = parseConfiguration;
525 654
28 lib/orientdb/db.js
@@ -70,13 +70,27 @@ Db.prototype.open = function (callback) {
70 70 // retreive the DB schema and save it
71 71 // TODO the schema also needs to be update if this changes
72 72 // (I am not sure if this is currently supported in the protocol)
73   - self.loadDocument("0:1", function(err, result) {
74   -
75   - self.schema = result.schema;
76   - self.classes = result.classes;
77   -
78   - // only now call the open callback
79   - callback(err, result);
  73 + var configDoc = {
  74 + clusterId: 0,
  75 + clusterPosition: 0
  76 + }
  77 +
  78 + self.loadRecord(configDoc, function(err, result) {
  79 +
  80 + if (err) { return callback(err); }
  81 +
  82 + self.configuration = parser.parseConfiguration(result.content.toString());
  83 +
  84 + self.loadDocument(self.configuration.schemaRecordId, function(err, result) {
  85 +
  86 + if (err) { return callback(err); }
  87 +
  88 + self.schema = result.schema;
  89 + self.classes = result.classes;
  90 +
  91 + // only now call the open callback
  92 + callback(err, self.server.sessionId);
  93 + });
80 94 });
81 95 };
82 96
8 test/db_open_close.js
@@ -10,17 +10,19 @@ var server = new Server(serverConfig);
10 10 var db = new Db("temp", server, dbConfig);
11 11
12 12
13   -db.open(function(err, result) {
  13 +db.open(function(err, sessionId) {
14 14
15 15 assert(!err, "Error while opening the database: " + err);
16 16
17   - assert(!err, "There must be a session ID after a call to open a database. Received: " + result.sessionId);
  17 + assert(!err, "There must be a session ID after a call to open a database. Received: " + sessionId);
18 18
19 19 console.log("Opened database: " + db.databaseName);
20   - console.log("Session ID:" + result.sessionId);
  20 + console.log("Session ID:" + sessionId);
21 21 console.log("Database clusters: " + JSON.stringify(db.clusters));
22 22 console.log("Database classes: " + JSON.stringify(db.classes));
  23 + console.log("Database configuration: " + JSON.stringify(db.configuration));
23 24
  25 + assert(typeof sessionId !== "undefined");
24 26 assert.equal(5, db.clusters.length);
25 27 assert.equal(2, db.classes.length);
26 28 assert.equal(0, db.getClusterIdByName("Internal"));
37 test/parser/configuration.js
... ... @@ -0,0 +1,37 @@
  1 +var assert = require("assert");
  2 +var parser = require("../../lib/orientdb/connection/parser");
  3 +
  4 +var configAsString = "2| |#0:1| |#0:2|it|IT|yyyy-MM-dd|yyyy-MM-dd HH:mm:ss|0|mmap|500Kb|500Mb|50%|auto|0|0|0| |mmap|512mb|false|true|0|"
  5 +
  6 +var config = parser.parseConfiguration(configAsString);
  7 +
  8 +assert.equal(2, config.version);
  9 +assert.equal(null, config.name);
  10 +assert.equal("0:1", config.schemaRecordId);
  11 +assert.equal(null, config.dictionaryRecordId);
  12 +assert.equal("0:2", config.indexMgrRecordId);
  13 +assert.equal("it", config.localeLanguage);
  14 +assert.equal("IT", config.localeCountry);
  15 +assert.equal("yyyy-MM-dd", config.dateFormat);
  16 +assert.equal("yyyy-MM-dd HH:mm:ss", config.dateTimeFormat);
  17 +assert.equal("0", config.fileTemplate.maxSize);
  18 +assert.equal("mmap", config.fileTemplate.fileType);
  19 +assert.equal("500Kb", config.fileTemplate.fileStartSize);
  20 +assert.equal("500Mb", config.fileTemplate.fileMaxSize);
  21 +assert.equal("50%", config.fileTemplate.fileIncrementSize);
  22 +assert.equal("auto", config.fileTemplate.defrag);
  23 +assert.equal(0, config.fileTemplate.infoFiles);
  24 +assert.equal(0, config.clusters.length);
  25 +assert.equal(0, config.dataSegments.length);
  26 +assert.equal(null, config.txSegment.path);
  27 +assert.equal("mmap", config.txSegment.type);
  28 +assert.equal("512mb", config.txSegment.maxSize);
  29 +assert.equal(false, config.txSegment.synchRecord);
  30 +assert.equal(true, config.txSegment.synchTx);
  31 +assert.equal(0, config.properties.length);
  32 +
  33 +configAsString = "2| |#0:1| |#0:2|it|IT|yyyy-MM-dd|yyyy-MM-dd HH:mm:ss|0|mmap|500Kb|500Mb|50%|auto|0|6|0|internal|p|0|mmap|1Mb|500Mb|50%|auto|1|${STORAGE_PATH}/internal.0.ocl|mmap|500Mb|${STORAGE_PATH}/internal.och|mmap|500Mb|1|index|p|0|mmap|1Mb|500Mb|50%|auto|1|${STORAGE_PATH}/index.0.ocl|mmap|500Mb|${STORAGE_PATH}/index.och|mmap|500Mb|2|default|p|0|mmap|1Mb|500Mb|50%|auto|1|${STORAGE_PATH}/default.0.ocl|mmap|500Mb|${STORAGE_PATH}/default.och|mmap|500Mb|3|orole|p|0|mmap|1Mb|500Mb|50%|auto|1|${STORAGE_PATH}/orole.0.ocl|mmap|500Mb|${STORAGE_PATH}/orole.och|mmap|500Mb|4|ouser|p|0|mmap|1Mb|500Mb|50%|auto|1|${STORAGE_PATH}/ouser.0.ocl|mmap|500Mb|${STORAGE_PATH}/ouser.och|mmap|500Mb|5|fantasyperson|p|0|mmap|1Mb|500Mb|50%|auto|1|${STORAGE_PATH}/fantasyperson.0.ocl|mmap|500Mb|${STORAGE_PATH}/fantasyperson.och|mmap|500Mb|1|0|default|0|mmap|1Mb|500Mb|100%|auto|1|${STORAGE_PATH}/default.0.oda|mmap|500Mb|${STORAGE_PATH}/default.odh|mmap|0|${STORAGE_PATH}/txlog.otx.otd.otd.otd|mmap|512mb|false|true|0|"
  34 +config = parser.parseConfiguration(configAsString);
  35 +assert.equal(6, config.clusters.length);
  36 +assert.equal(5, config.clusters[5].clusterId);
  37 +assert.equal("${STORAGE_PATH}/internal.0.ocl", config.clusters[0].fileTemplate.infoFiles[0].fileName);

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.