00001
00002
00003
00004
00005
00006
00007
00008
00009 var Emitter = require("events").EventEmitter;
00010 var fs = require("fs");
00011 var stream = require("stream");
00012 var child_process = require("child_process");
00013 var Utils = require("./Utils");
00014 var Connector = require("./DBConnector");
00015 var path_module = require("path");
00016 var db = new Emitter();
00017
00018
00019
00020 var DefaultColumns = [
00021 {
00022 name: "name",
00023 type: "string",
00024 editable: false,
00025 display: true
00026 },
00027 {
00028 name: "value",
00029 type: "string",
00030 editable: true,
00031 display: true
00032 },
00033 {
00034 name: "annotation",
00035 title: "User Comment",
00036 type: "string",
00037 editable: true,
00038 display: true
00039 },
00040 {
00041 name: "comment",
00042 type: "comment",
00043 editable: false,
00044 display: false
00045 },
00046 {
00047 name: "type",
00048 type: "string",
00049 editable: false,
00050 display: false
00051 },
00052 {
00053 name: "values",
00054 type: "array",
00055 editable: false,
00056 display: false
00057 }, {
00058 name: "children",
00059 type: "array",
00060 editable: false,
00061 display: false
00062 }
00063 ];
00064
00065 var MakeDbConfig = function (configFile) {
00066 var defaultConfig = {
00067 dbprovider: "filesystem"
00068 , configNameFilter: "*"
00069 , baseDir: process.env.ARTDAQ_DATABASE_URI ? process.env["ARTDAQ_DATABASE_URI"] : ""
00070 , mongoConfig: {
00071 dbUser: "anonymous"
00072 , dbHost: "localhost"
00073 , dbPort: 27017
00074 }
00075 , instanceName: configFile.instanceName ? configFile.instanceName : "test_db"
00076 };
00077
00078 if (defaultConfig.baseDir.indexOf("filesystemdb://") === 0) {
00079 console.log("Parsing filesystemdb URI: " + defaultConfig.baseDir);
00080 defaultConfig.baseDir = defaultConfig.baseDir.replace("filesystemdb://", "").replace(/\/$/, "");
00081 var tempArr = defaultConfig.baseDir.split('/');
00082 defaultConfig.instanceName = tempArr.splice(-1);
00083 defaultConfig.baseDir = tempArr.slice(0,-1).join('/');
00084 } else if (defaultConfig.baseDir.indexOf("mongodb://") === 0) {
00085 console.log("Parsing mongodb URI: " + defaultConfig.baseDir);
00086 var matches = defaultConfig.baseDir.match(/mongodb:\/\/(?:([^@:]*)(?::([^@]*))?@)?([^:]*):([0-9]+)\/([^/]*)\/?/);
00087 defaultConfig.baseDir = "";
00088 defaultConfig.dbprovider = "mongo";
00089 console.log(JSON.stringify(matches, null, 4));
00090 defaultConfig.mongoConfig.dbUser = matches[1] ? matches[1] : "";
00091 var pass = matches[2];
00092 if (pass !== null && pass !== undefined) {
00093 fs.writeFileSync(".dbpasswd", pass);
00094 }
00095 defaultConfig.mongoConfig.dbHost = matches[3];
00096 defaultConfig.mongoConfig.dbPort = matches[4];
00097 defaultConfig.instanceName = matches[5];
00098 }
00099
00100 if (defaultConfig.baseDir === "") {
00101 defaultConfig.baseDir = process.env["ARTDAQ_DATABASE_DIR"];
00102 }
00103
00104 console.log("DBConfig: " + JSON.stringify(defaultConfig, null, 4));
00105 return defaultConfig;
00106 };
00107
00108 function GetFileBase(configName, collection, entity) {
00109 return path_module.join(configName, collection, entity + ".gui.json");
00110 }
00111
00112 function GetTempFilePath(configName, collection, entity, dirs) {
00113 var filebase = GetFileBase(configName, collection, entity);
00114
00115 if (!fs.existsSync(path_module.join(dirs.tmp, configName))) fs.mkdirSync(path_module.join(dirs.tmp, configName));
00116 if (!fs.existsSync(path_module.join(dirs.tmp, configName, collection))) fs.mkdirSync(path_module.join(dirs.tmp, configName, collection));
00117 return path_module.join(dirs.tmp, filebase);
00118 }
00119
00120 function GetFilePath(configName, collection, entity, dirs, dbOnly) {
00121 var filebase = GetFileBase(configName, collection, entity);
00122 console.log("GetFilePath: filebase is " + filebase + ", dirs: " + JSON.stringify(dirs, null, 4));
00123 if (!dbOnly && fs.existsSync(path_module.join(dirs.tmp, filebase))) {
00124 return path_module.join(dirs.tmp, filebase);
00125 }
00126
00127 if (!fs.existsSync(path_module.join(dirs.db, configName))) fs.mkdirSync(path_module.join(dirs.db, configName));
00128 if (!fs.existsSync(path_module.join(dirs.db, configName, collection))) fs.mkdirSync(path_module.join(dirs.db, configName, collection));
00129 return path_module.join(dirs.db, filebase);
00130 }
00131
00137 function LoadFile(fileInfo, dirs, dbConfig) {
00138 console.log("LoadFile: fileInfo: " + JSON.stringify(fileInfo, null, 4) + ", dirs: " + JSON.stringify(dirs, null, 4) + ", dbConfig: " + JSON.stringify(dbConfig, null, 4));
00139 var output = {
00140 data: {}
00141 , filePath: ""
00142 , entity: ""
00143 , collection: ""
00144 , configName: ""
00145 };
00146
00147 output.entity = fileInfo.query.filter["entities.name"];
00148 output.collection = fileInfo.query.collection;
00149 output.configName = fileInfo.query.filter["configurations.name"];
00150
00151 console.log("File info: " + JSON.stringify(fileInfo, null, 4));
00152 output.filePath = GetFilePath(output.configName, output.collection, output.entity, dirs, true);
00153
00154 if (!fs.existsSync(output.filePath)) {
00155 console.log("Loading file from database");
00156 output.data = Connector.RunLoadQuery(dbConfig, fileInfo.query);
00157 console.log("Writing file " + output.filePath);
00158 fs.writeFileSync(output.filePath, JSON.stringify(output.data, null, 4));
00159 } else {
00160 output.data = JSON.parse("" + fs.readFileSync(output.filePath));
00161 }
00162 console.log("File Data: " + JSON.stringify(output, null, 4));
00163 console.log("Done Loading file");
00164 return output;
00165 }
00166
00178 function FetchFile(fileInfo, dataFormat, dbdirectory, dbConfig) {
00179 console.log("FetchFile: fileInfo: " + JSON.stringify(fileInfo, null, 4) + ", dataFormat: " + dataFormat + ", dbDirectory: " + dbdirectory);
00180 var fileName = path_module.join(fileInfo.collection + "_" + fileInfo.name + "_" + fileInfo.version);
00181 if (dataFormat === "fhicl") {
00182 fileName += ".fcl";
00183 } else if (dataFormat === "gui") {
00184 fileName += ".gui.json";
00185 } else {
00186 fileName += ".json";
00187 }
00188 var filePath = path_module.join(dbdirectory, fileName);
00189 var query = {
00190 filter: {
00191 "entities.name": fileInfo.name,
00192 version: fileInfo.version
00193 },
00194 collection: fileInfo.collection,
00195 configurable_entity: fileInfo.name,
00196 dbprovider: dbConfig.dbprovider,
00197 operation: "load",
00198 dataformat: dataFormat
00199 };
00200 var fhiclData = Connector.RunLoadQuery(dbConfig, query, true);
00201 fs.writeFileSync(filePath, JSON.stringify(fhiclData, null, 4));
00202
00203 var stat = fs.statSync(filePath);
00204 return { fileName: fileName, filePath: filePath, size: stat.size }
00205 };
00206
00207 function MakeColumns(rowIn, index, columns, columnGroups, group) {
00208
00209 var rowOut = {
00210 children: []
00211 };
00212
00213 for (var p in rowIn) {
00214 if (rowIn.hasOwnProperty(p)) {
00215 var value = rowIn[p];
00216 if (value !== Object(value) && Utils.ContainsName(columns, p, "name") < 0) {
00217 columns.push({ name: p, type: "string", editable: true, display: true, columnGroup: group });
00218 rowOut[p] = value;
00219 } else if (value === Object(value)) {
00220 var res;
00221 if (Utils.ContainsName(columnGroups, p, "name") < 0) {
00222 columnGroups.push({ name: "" + p, text: "" + p, parentGroup: group });
00223 }
00224 if (value.constructor === Array) {
00225 for (var i in value) {
00226 if (value.hasOwnProperty(i)) {
00227 res = MakeColumns(value[i], index + "." + i, columns, columnGroups, p);
00228 columns = res.columns;
00229 columnGroups = res.columnGroups;
00230 res.row.name = rowIn.name + "/" + p + "___" + i;
00231 res.row.index = index + "." + i;
00232 rowOut.children.push(res.row);
00233 }
00234 }
00235 } else {
00236 res = MakeColumns(value, index, columns, columnGroups, p);
00237 columns = res.columns;
00238 columnGroups = res.columnGroups;
00239 for (var pp in res.row) {
00240 if (res.row.hasOwnProperty(pp)) {
00241 rowOut[pp] = res.row[pp];
00242 }
00243 }
00244
00245 }
00246 } else {
00247 rowOut[p] = value;
00248 }
00249 }
00250 }
00251 if (rowOut.children.length === 0) delete rowOut.children;
00252 else if (Utils.ContainsName(columns, "children", "name") < 0) { columns.push({ name: "children", type: "array", editable: false, display: false }); }
00253
00254 return { row: rowOut, columns: columns, columnGroups: columnGroups };
00255 }
00256
00263 function ParseSequence(sequence, name) {
00264 console.log("ParseSequence: sequence: " + JSON.stringify(sequence, null, 4) + ", name: " + name);
00265 var children = [];
00266 var hasTable = false;
00267 var rows = [];
00268 var columns = [];
00269 var columnGroups = [];
00270 columns.push({ name: "index", type: "number", editable: false, display: true, columnGroup: name });
00271 columns.push({ name: "name", type: "string", editable: false, display: true, columnGroup: name });
00272
00273 for (var i = 0; i < sequence.length; ++i) {
00274 if (sequence[i] === Object(sequence[i])) {
00275
00276 if (sequence[i].constructor === Array) {
00277
00278 var arr = ParseSequence(sequence[i], name + "___" + i);
00279 if (arr.children !== undefined && arr.children.length > 0) {
00280 children.push({ name: name + "___" + i, children: arr.children });
00281 rows.push({ index: i, name: name + "___" + i, children: arr.children });
00282 if (Utils.ContainsName(columns, "children", "name") < 0) {
00283 columns.push({ name: "children", type: "array", editable: false, display: false });
00284 }
00285 }
00286 if (arr.rows !== undefined && arr.rows.length > 0) {
00287 if (arr.hasTable) {
00288 hasTable = true;
00289 }
00290 for (var r in arr.rows) {
00291 if (arr.rows.hasOwnProperty(r)) {
00292 rows.push(arr.rows[r]);
00293 }
00294 }
00295 for (var c in arr.columns) {
00296 if (arr.columns.hasOwnProperty(c) && Utils.ContainsName(columns, arr.columns[c].name, "name") < 0) {
00297 columns.push(arr.columns[c]);
00298 }
00299 }
00300 }
00301 } else {
00302
00303 hasTable = true;
00304 console.log("Parsing as table: " + JSON.stringify(sequence[i], null, 4));
00305 if (!sequence[i].hasOwnProperty("name") || sequence[i].name.length === 0) {
00306 sequence[i].name = name + "___" + i;
00307 }
00308 if (Utils.ContainsName(columnGroups, name, "name") < 0) {
00309 columnGroups.push({ name: "" + name, text: "" + name });
00310 }
00311 var res = MakeColumns(sequence[i], i, columns, columnGroups, name);
00312 res.row.index = i;
00313 rows.push(res.row);
00314 columns = res.columns;
00315 columnGroups = res.columnGroups;
00316 }
00317 } else {
00318 var vname = name + "___" + i;
00319 var value = sequence[i];
00320 children.push({ name: vname, value: value });
00321 rows.push({ index: i, name: vname, value: value });
00322 if (Utils.ContainsName(columns, "value", "name") < 0) {
00323 columns.push({ name: "value", type: "string", editable: true, display: true, columnGroup: name });
00324 }
00325 }
00326 }
00327 var comment = sequence.comment ? sequence.comment : " ";
00328
00329 if (hasTable) {
00330 var table = {
00331 name: name
00332 , comment: comment
00333 , type: "table"
00334 , isSequence: true
00335 , table: {
00336 rows: rows
00337 , columns: columns
00338 , columnGroups: columnGroups
00339 }
00340 };
00341 console.log("\nParseSequence returning table: " + JSON.stringify(table, null, 4));
00342 return table;
00343 } else {
00344 var output = { name: name, comment: comment, children: children };
00345 console.log("\nParseSequence returning: " + JSON.stringify(output, null, 4));
00346 return output;
00347 }
00348 };
00349
00356 function ParseFhiclTable(table, sub) {
00357 console.log("ParseFhiclTable: table: " + JSON.stringify(table, null, 4) + ", sub: " + sub);
00358 var children = [];
00359 var subtables = [];
00360 var hasSubtables = false;
00361 var comment = table.comment ? table.comment : " ";
00362
00363
00364 for (var e in table.children) {
00365 if (table.children.hasOwnProperty(e)) {
00366 var element = table.children[e];
00367 if (element.type === "table" && element.isSequence) {
00368 element.type = "sequence";
00369 }
00370
00371 switch (element.type) {
00372 case "table":
00373
00374 hasSubtables = true;
00375 children.push(ParseFhiclTable(element, 1));
00376 break;
00377 case "sequence":
00378 children.push(ParseSequence(element.children, element.name));
00379 break;
00380 case "number":
00381 case "string":
00382 case "bool":
00383 children.push(element);
00384 break;
00385 default:
00386 console.log("Unknown type " + element.type + " encountered!");
00387 break;
00388 }
00389 }
00390 }
00391
00392 var obj = { name: table.name, hasSubtables: hasSubtables, children: children, subtables: subtables, type: "table", comment: comment, columns: DefaultColumns };
00393 if (sub === 0 || sub === undefined) {
00394
00395 }
00396 return obj;
00397 };
00398
00399 function FindConfigs(configNameFilter, dbConfig) {
00400 console.log("\nFindConfigs: Request for Named Configurations received");
00401 var configsOutput = { Success: false, data: {}, configs: {} };
00402 try {
00403 var configs = Connector.RunGetConfigsQuery(dbConfig).search;
00404 console.log("CONFIGS: " + JSON.stringify(configs));
00405 for (var conf in configs) {
00406 if (configs.hasOwnProperty(conf)) {
00407 var config = configs[conf];
00408 if (config.name.search(configNameFilter) >= 0) {
00409 var prefix = config.name;
00410 var configNumber = 0;
00411 var matches = config.name.match(/(.*?)[_\-]*(\d*)$/);
00412 if (matches) {
00413 if(matches.length > 1) prefix = matches[1];
00414 if(matches.length > 2) configNumber = parseInt(matches[2]);
00415 }
00416
00417 if (!configsOutput.data.hasOwnProperty(prefix)) {
00418 configsOutput.data[prefix] = [];
00419 }
00420
00421 configsOutput.data[prefix].push({ version: configNumber, data: JSON.stringify(config.query), name: config.name });
00422 configsOutput.configs[config.name] = config.query;
00423 }
00424 }
00425 }
00426 if (configsOutput.data.length === 0) {
00427 configsOutput.data["null"] = {version: 0, data: ""};
00428 configsOutput.configs[configNameFilter] = {};
00429 }
00430 configsOutput.Success = true;
00431 } catch (e) {
00432 console.log("Exception caught: " + e.name + ": " + e.message);
00433 }
00434 console.log("NamedConfigs complete");
00435 return configsOutput;
00436 }
00437
00438 function FindInstances(dbConfig) {
00439 console.log("FindInstances: Request for Database Instances received");
00440 var instancesOutput = { Success: false, data: [] };
00441 instancesOutput.data.push("<option value=\"NONE\">Create New Database Instance</option>");
00442 try {
00443 var dbs = Connector.RunListDatabasesQuery(dbConfig, {}).search;
00444 for (var db in dbs) {
00445 if (dbs.hasOwnProperty(db)) {
00446 instancesOutput.data.push("<option value=" + dbs[db].name + ">" + dbs[db].name + "</option>");
00447 }
00448 }
00449 instancesOutput.Success = true;
00450 } catch (e) {
00451 console.log("Exception caught: " + e.name + ": " + e.message);
00452 }
00453 console.log("FindInstances complete");
00454 return instancesOutput;
00455 }
00456
00464 function LoadConfigFiles(configName, dirs, query, dbConfig) {
00465 console.log("LoadConfigFiles: configName: " + configName + ", dirs: " + JSON.stringify(dirs, null, 4) + ", query: " + JSON.stringify(query, null, 4));
00466 var retval = {
00467 collections: {},
00468 Success: false
00469 };
00470 var error = false;
00471 var configFiles = [];
00472 var e;
00473 try {
00474 configFiles = Connector.RunBuildFilterQuery(dbConfig, configName, query).search;
00475 } catch (e) {
00476 error = true;
00477 console.log("Exception occurred: " + e.name + ": " + e.message);
00478 }
00479 if (!error) {
00480 try {
00481 for (var file in configFiles) {
00482 if (configFiles.hasOwnProperty(file)) {
00483 console.log("File info: " + JSON.stringify(configFiles[file], null, 4));
00484 var entity = LoadFile(configFiles[file], dirs, dbConfig).entity;
00485 var collection = configFiles[file].query.collection;
00486 if (!retval.collections.hasOwnProperty(collection)) {
00487 retval.collections[collection] = {
00488 name: collection
00489 , files: []
00490 };
00491 }
00492 console.log("Adding " + entity + " to output list");
00493 retval.collections[collection].files.push(entity);
00494 }
00495 }
00496 retval.Success = true;
00497 } catch (e) {
00498 console.log("Exception occurred: " + e.name + ": " + e.message);
00499 }
00500 }
00501 return retval;
00502 };
00503
00504 function SearchFile(fileData, searchKey) {
00505 console.log("\nSearching for " + searchKey + " in object " + JSON.stringify(fileData, null, 4));
00506 var retval = { name: fileData.name, children: [], columns: fileData.columns };
00507 for (var ii in fileData.children) {
00508 if (fileData.children.hasOwnProperty(ii)) {
00509 var child = fileData.children[ii];
00510 console.log("Key name: " + child.name);
00511 if (child.name.indexOf(searchKey) !== -1) {
00512 console.log("Adding child to output");
00513 retval.children.push(child);
00514 } else if (child.children && child.children.length > 0) {
00515 console.log("Searching child's children");
00516 var table = SearchFile(child, searchKey);
00517 if (table.children.length > 0) {
00518 retval.children.push(table);
00519 }
00520 }
00521 }
00522 }
00523 console.log("\nSearchFile returning " + JSON.stringify(retval, null, 4));
00524 return retval;
00525 }
00526
00527 function SearchConfigFiles(searchKey, configName, dirs, dbConfig) {
00528 console.log("\nSearchConfigFiles: keySearch: " + searchKey + ", configName: " + configName + ", dirs: " + JSON.stringify(dirs, null, 4));
00529 var retval = {
00530 collectionsTemp: {},
00531 collections: [],
00532 columns: DefaultColumns,
00533 Success: false
00534 };
00535 var error = false;
00536 var configFiles = [];
00537 var query = FindConfigs(configName, dbConfig).configs[configName];
00538 var e;
00539 try {
00540 configFiles = Connector.RunBuildFilterQuery(dbConfig, configName, query).search;
00541 } catch (e) {
00542 error = true;
00543 console.log("Exception occurred: " + e.name + ": " + e.message);
00544 }
00545 if (!error) {
00546 try {
00547 for (var file in configFiles) {
00548 if (configFiles.hasOwnProperty(file)) {
00549 console.log("File info: " + JSON.stringify(configFiles[file], null, 4));
00550 var entity = LoadFile(configFiles[file], dirs, dbConfig).entity;
00551 var collection = configFiles[file].query.collection;
00552 if (!retval.collectionsTemp.hasOwnProperty(collection)) {
00553 retval.collectionsTemp[collection] = {
00554 name: collection
00555 , children: []
00556 };
00557 }
00558 var fileData = GetData(configName, collection, entity, dirs);
00559 fileData.name = entity;
00560 var fileMatches = SearchFile(fileData, searchKey);
00561 if (fileMatches.children.length > 0) {
00562 console.log("Adding " + entity + " to output list");
00563 retval.collectionsTemp[collection].children.push(fileMatches);
00564 }
00565 }
00566 }
00567 retval.Success = true;
00568 } catch (e) {
00569 console.log("Exception occurred: " + e.name + ": " + e.message);
00570 }
00571 }
00572
00573 for (var ii in retval.collectionsTemp) {
00574 if (retval.collectionsTemp.hasOwnProperty(ii)) {
00575 retval.collections.push(retval.collectionsTemp[ii]);
00576 }
00577 }
00578 delete retval.collectionsTemp;
00579 console.log("\nSearchConfigFiles returning: " + JSON.stringify(retval, null, 4));
00580 return retval;
00581 }
00582
00595 function GetData(configName, collectionName, entity, dirs) {
00596 console.log("GetData: configName: " + configName + ", collectionName: " + collectionName + ", entity: " + entity + ", dirs: " + JSON.stringify(dirs, null, 4));
00597 var fileName = GetFilePath(configName, collectionName, entity, dirs, false);
00598 if (!fs.existsSync(fileName)) { throw { name: "FileNotFoundException", message: "The requested file was not found" }; }
00599 var jsonFile = JSON.parse("" + fs.readFileSync(fileName));
00600 var jsonBase = ParseFhiclTable({ children: jsonFile.document.converted.guidata, name: entity }, 0);
00601
00602 return jsonBase;
00603 };
00604
00605 function ReplaceByPath(obj, pathArr, data) {
00606 console.log("Utils.ReplaceByPath: obj: " + JSON.stringify(obj, null, 4) + ", pathArr: " + JSON.stringify(pathArr, null, 4) + ", data: " + JSON.stringify(data, null, 4));
00607 if (pathArr.length === 0 && (!obj.name || obj.name === data.name)) {
00608 if (data.column && data.value) {
00609 obj[data.column] = data.value;
00610 } else if (data.type) {
00611 for (var i in data) {
00612 if (data.hasOwnProperty(i)) {
00613 obj[i] = data[i];
00614 }
00615 }
00616 }
00617 return obj;
00618 }
00619
00620 var thisName = pathArr.shift();
00621 var index = -1;
00622 if (obj.type === "table") {
00623 index = Utils.ContainsName(obj.children, thisName, "name");
00624 } else if (obj.type === "sequence") {
00625 index = thisName.slice(thisName.indexOf("___") + 3);
00626 }
00627
00628 obj.children[index] = ReplaceByPath(obj.children[index], pathArr, data);
00629 return obj;
00630 }
00631
00641 function UpdateTable(configName, tablePath, data, dirs) {
00642 console.log("UpdateTable: tablePath:" + tablePath + ", configName: " + configName + ", data: " + JSON.stringify(data, null, 4) + ", dirs: " + JSON.stringify(dirs, null, 4));
00643 var tableArray = tablePath.split('/');
00644 var collection = tableArray.shift();
00645 var entity = tableArray.shift();
00646 var fileName = GetFilePath(configName, collection, entity, dirs, false);
00647 if (!fs.existsSync(fileName)) { throw { name: "FileNotFoundException", message: "The requested file: \"" + fileName + "\" was not found" }; }
00648 console.log("Reading from file " + fileName);
00649 var jsonFile = JSON.parse("" + fs.readFileSync(fileName));
00650 var oldFile = jsonFile.document.converted.guidata;
00651
00652 var curName = tableArray.shift();
00653 var dataIdx = Utils.ContainsName(oldFile, curName, "name");
00654 if (dataIdx < 0) {
00655 console.log("Cannot find data in file!");
00656 return;
00657 }
00658 var oldData = oldFile[dataIdx];
00659 console.log("oldData: " + JSON.stringify(oldData, null, 4));
00660 ReplaceByPath(oldData, tableArray, data);
00661
00662 jsonFile.document.converted.guidata[dataIdx] = oldData;
00663 console.log("After replacement, table data is " + JSON.stringify(oldData, null, 4));
00664
00665 var filePath = GetTempFilePath(configName, collection, entity, dirs);
00666 console.log("Writing to file " + filePath);
00667 fs.writeFileSync(filePath, JSON.stringify(jsonFile, null, 4));
00668 };
00669
00677 function DiscardWorkingDir(dirs) {
00678 console.log("Deleting existing trash dir (if any)");
00679 Utils.ExecSync("rm -rf " + dirs.trash);
00680 console.log("DiscardWorkingDir: Moving db temp to TRASH: mv " + dirs.db + " " + dirs.trash);
00681 Utils.ExecSync("mv " + dirs.db + " " + dirs.trash);
00682 console.log("DiscardWorkingDir: Moving temp files to TRASH: mv " + dirs.tmp + "/* " + dirs.trash);
00683 Utils.ExecSync("mv " + dirs.tmp + "/* " + dirs.trash);
00684 console.log("DiscardWorkingDir: Deleting temp directory: rmdir " + dirs.tmp);
00685 Utils.ExecSync("rmdir " + dirs.tmp);
00686 };
00687
00700 function SaveConfigurationChanges(oldConfig, newConfig, files, dirs, dbConfig) {
00701 console.log("Saving Configuration Changes, oldConfig: " + oldConfig + ", newConfig: " + newConfig + ", files: " + JSON.stringify(files, null, 4) + ", dirs: " + JSON.stringify(dirs, null, 4));
00702 var fileInfo = Connector.RunBuildFilterQuery(dbConfig, oldConfig).search;
00703
00704 var f;
00705 for (f in files) {
00706 if (files.hasOwnProperty(f)) {
00707 console.log("Current file information: " + JSON.stringify(files[f], null, 4));
00708 var collectionName = files[f].collection;
00709 var entities = files[f].entities;
00710 var entity = files[f].entity ? files[f].entity : "notprovided";
00711 var version = files[f].version;
00712 var thisFileInfo = {};
00713 for (var fi in fileInfo) {
00714 if (fileInfo.hasOwnProperty(fi)) {
00715 if (collectionName === fileInfo[fi].query.collection && Utils.ContainsString(entities, fileInfo[fi].query.filter["entities.name"]) != -1) {
00716 console.log("Matched file information to Document: " + JSON.stringify(fileInfo[fi], null, 4));
00717 thisFileInfo = fileInfo[fi];
00718 entity = fileInfo[fi].query.filter["entities.name"];
00719 fileInfo.splice(fi, 1);
00720 }
00721 }
00722 }
00723
00724 console.log("Getting metadata from original and changed files");
00725 var modified = GetFilePath(oldConfig, collectionName, entity, dirs, false);
00726 var newMetadata = ReadFileMetadata(dirs, thisFileInfo, dbConfig);
00727
00728
00729
00730 newMetadata.version = version;
00731 console.log("newMetadata: " + JSON.stringify(newMetadata, null, 4));
00732
00733 console.log("Checking metadata version strings");
00734 while (VersionExists(entity, collectionName, newMetadata.version, dbConfig)) {
00735 console.log("Inferring new version string...");
00736 version = Utils.Uniquify(newMetadata.version);
00737 console.log("Changing version from " + newMetadata.version + " to " + version);
00738 newMetadata.version = version;
00739 console.log("OK");
00740 }
00741 console.log("Prepending changelog");
00742 newMetadata.changelog = files[f].changelog + newMetadata.changelog;
00743
00744 console.log("Writing new metadata to file");
00745 if (WriteFileMetadata(newMetadata, modified)) {
00746
00747 console.log("Running store query");
00748 var data = "" + fs.readFileSync(modified);
00749 console.log("Writing " + data + " to database");
00750 Connector.RunStoreQuery(dbConfig, data, collectionName, newMetadata.version, entity, "gui", newConfig);
00751 } else {
00752 console.log("ERROR: Could not find file " + modified);
00753 console.log("Please check if it exists.");
00754 }
00755 }
00756 }
00757
00758 console.log("Running addconfig for unmodified files: " + JSON.stringify(fileInfo, null, 4));
00759 for (f in fileInfo) {
00760 if (fileInfo.hasOwnProperty(f)) {
00761 var unmodifiedVersion = GetVersion(fileInfo[f], dirs, dbConfig);
00762 Connector.RunAddConfigQuery(dbConfig, newConfig, unmodifiedVersion, fileInfo[f].query.collection, { name: fileInfo[f].query.filter["entities.name"] });
00763 }
00764 }
00765
00766 DiscardWorkingDir(dirs);
00767 };
00768
00777 function CreateNewConfiguration(configName, configData, dbConfig) {
00778 console.log("CreateNewConfigration: configName: " + configName + ", configData: " + JSON.stringify(configData, null, 4));
00779 var query = {
00780 operations: []
00781 };
00782
00783 for (var d in configData.entities) {
00784 if (configData.entities.hasOwnProperty(d)) {
00785 var entityData = configData.entities[d];
00786 console.log("Entity: " + JSON.stringify(entityData, null, 4));
00787 query.operations.push({
00788 filter: { version: entityData.version, "entities.name": entityData.name },
00789 configuration: configName,
00790 collection: entityData.collection,
00791 dbprovider: dbConfig.dbprovider,
00792 operation: "addconfig",
00793 dataformat: "gui"
00794 });
00795 }
00796 }
00797
00798 Connector.RunNewConfigQuery(dbConfig, query);
00799 };
00800
00807 function ReadConfigurationMetadata(configName, dirs, dbConfig) {
00808 console.log("ReadConfigurationMetadata: configname: " + configName + ", dirs: " + JSON.stringify(dirs, null, 4));
00809
00810 var data = Connector.RunBuildFilterQuery(dbConfig, configName ).search;
00811
00812 var metadata = {
00813 entities: []
00814 };
00815 for (var i in data) {
00816 if (data.hasOwnProperty(i)) {
00817 console.log("Loading metadata: File " + i + " of " + data.length);
00818 var version = GetVersion(data[i], dirs, dbConfig);
00819 metadata.entities.push({ name: data[i].query.filter["entities.name"], file: data[i].name + "_" + data[i].query.collection, version: version, collection: data[i].query.collection });
00820 }
00821 }
00822
00823 console.log("Returning entity list: " + JSON.stringify(metadata.entities, null, 4));
00824 return metadata;
00825 };
00826
00835 function GetVersion(query, dirs, dbConfig) {
00836 console.log("\"GetVersion\": {\"query\":" + JSON.stringify(query, null, 4) + ", \"dirs\":" + JSON.stringify(dirs, null, 4) + "},");
00837 var ver = LoadFile(query, dirs, dbConfig).data.version;
00838 console.log("GetVersion Returning " + ver);
00839 return ver;
00840 };
00841
00851 function ReadFileMetadata(dirs, query, dbConfig) {
00852 console.log("ReadFileMetadata: query=" + JSON.stringify(query, null, 4) + ", dirs=" + JSON.stringify(dirs, null, 4) + ", dbConfig: " + JSON.stringify(dbConfig, null, 4));
00853
00854 var jsonFile = LoadFile(query, dirs, dbConfig).data;
00855 if (jsonFile.changelog === undefined) {
00856 jsonFile.changelog = "";
00857 }
00858 var metadata = {
00859 entities: jsonFile.entities,
00860 bookkeeping: jsonFile.bookkeeping,
00861 aliases: jsonFile.aliases,
00862 configurations: jsonFile.configurations,
00863 version: jsonFile.version,
00864 changelog: jsonFile.changelog,
00865 collection: query.query.collection
00866 };
00867
00868 console.log("ReadFileMetadata returning: " + JSON.stringify(metadata, null, 4));
00869 return metadata;
00870 };
00871
00878 function WriteFileMetadata(newMetadata, fileName) {
00879 console.log("WriteFileMetadata: newMetadata=" + JSON.stringify(newMetadata, null, 4) + ", fileName=" + fileName);
00880
00881 console.log("Reading file: " + fileName);
00882 if (!fs.existsSync(fileName)) return false;
00883 var jsonFile = JSON.parse("" + fs.readFileSync(fileName));
00884
00885
00886 console.log("Setting fields: " + JSON.stringify(newMetadata, null, 4));
00887 jsonFile.configurable_entity = newMetadata.configurable_entity;
00888 jsonFile.bookkeeping = newMetadata.bookkeeping;
00889 jsonFile.aliases = newMetadata.aliases;
00890 jsonFile.configurations = newMetadata.configurations;
00891 jsonFile.version = newMetadata.version;
00892 jsonFile.document.converted.changelog = newMetadata.changelog;
00893
00894 console.log("Writing data to file");
00895
00896 fs.writeFileSync(fileName, JSON.stringify(jsonFile, null, 4));
00897
00898 return true;
00899 };
00900
00906 function GetDirectories(userId, dbConfig) {
00907 console.log("GetDirectories: userid=" + userId);
00908 if (dbConfig.baseDir === "" || !dbConfig.baseDir) {
00909 dbConfig.baseDir = process.env["HOME"] + "/databases";
00910 console.log("WARNING: ARTDAQ_DATABASE_DATADIR not set. Using $HOME/databases instead!!!");
00911 }
00912
00913 if (!fs.existsSync(dbConfig.baseDir)) {
00914 console.log("ERROR: Base Directory " +dbConfig.baseDir + " doesn't exist!!!");
00915 throw { name: "BaseDirectoryMissingException", message: "ERROR: Base Directory doesn't exist!!!" };
00916 }
00917 if (!fs.existsSync(path_module.join(dbConfig.baseDir, "db"))) {
00918 fs.mkdirSync(path_module.join(dbConfig.baseDir, "db"));
00919 }
00920 if (!fs.existsSync(path_module.join(dbConfig.baseDir, "tmp"))) {
00921 fs.mkdirSync(path_module.join(dbConfig.baseDir, "tmp"));
00922 }
00923 if (!fs.existsSync(path_module.join(dbConfig.baseDir, "TRASH"))) {
00924 fs.mkdirSync(path_module.join(dbConfig.baseDir, "TRASH"));
00925 }
00926
00927
00928 var db = path_module.join(dbConfig.baseDir, "db", userId);
00929 var tmp = path_module.join(dbConfig.baseDir, "tmp", userId);
00930 var trash = path_module.join(dbConfig.baseDir, "TRASH", userId);
00931
00932
00933 if (!fs.existsSync(db)) {
00934 fs.mkdirSync(db);
00935 }
00936 if (!fs.existsSync(tmp)) {
00937 fs.mkdirSync(tmp);
00938 }
00939 if (!fs.existsSync(trash)) {
00940 fs.mkdirSync(trash);
00941 }
00942
00943 return { db: db, tmp: tmp, trash: trash };
00944 };
00945
00954 function VersionExists(entity, collection, version, dbConfig) {
00955 console.log("\"VersionExists\": { version:\"" + version + "\", entity:" + JSON.stringify(entity, null, 4) + ", collection: \"" + collection + "\"}");
00956 var query = {
00957 filter: {
00958 "entities.name": entity
00959 },
00960 collection: collection,
00961 dbprovider: dbConfig.dbprovider,
00962 operation: "findversions",
00963 dataformat: "gui"
00964 };
00965 var vers = Connector.RunGetVersionsQuery(dbConfig, query).search;
00966 console.log("Search returned: " + JSON.stringify(vers, null, 4));
00967 return Utils.ContainsName(vers, version, "name") >= 0;
00968 };
00969
00974 function Lock() {
00975 console.log("Lock");
00976 if (fs.existsSync("/tmp/node_db_lockfile")) {
00977 if (Date.now() - fs.fstatSync("/tmp/node_db_lockfile").ctime.getTime() > 1000) {
00978 console.log("Stale Lockfile detected, deleting...");
00979 return Unlock();
00980 } else {
00981 console.log("Lockfile detected and is not stale, aborting...");
00982 }
00983 return false;
00984 }
00985
00986 fs.writeFileSync("/tmp/node_db_lockfile", "locked");
00987 return true;
00988 }
00989
00994 function Unlock() {
00995 console.log("Unlock");
00996 fs.unlinkSync("/tmp/node_db_lockfile");
00997 return true;
00998 }
00999
01000
01001 db.RO_GetData = function (post, dbConfig) {
01002 console.log("RO_GetData: " + JSON.stringify(post, null, 4));
01003 var ret = { Success: false, data: {} };
01004 try {
01005 ret.data = GetData(post.configName, post.collection, post.entity, GetDirectories(post.user, dbConfig));
01006 ret.Success = true;
01007 } catch (e) {
01008 console.log("Exception occurred: " + e.name + ": " + e.message);
01009 }
01010
01011 console.log("GetData complete");
01012 return ret;
01013 };
01014
01015 db.RW_MakeNewConfig = function (post, dbConfig) {
01016 if (Lock()) {
01017 console.log("RW_MakeNewConfig: Request to make new configuration received: " + JSON.stringify(post, null, 4));
01018 var res = { Success: false };
01019 var error = false;
01020 var e;
01021 try {
01022 var configs = Connector.RunGetConfigsQuery(dbConfig).search;
01023 if (!Utils.ValidatePath(post.name)) {
01024 console.log("Invalid name detected!");
01025 error = true;
01026 }
01027 while (Utils.ContainsName(configs, post.name, "name") >= 0) {
01028 console.log("Inferring new configuration name");
01029 post.name = Utils.Uniquify(post.name);
01030 }
01031 } catch (e) {
01032 error = true;
01033 console.log("Exception occurred: " + e.name + ": " + e.message);
01034 }
01035 if (!error) {
01036 console.log("Creating Configuration");
01037 try {
01038 CreateNewConfiguration(post.name, JSON.parse(post.config), dbConfig);
01039 res.Success = true;
01040 } catch (e) {
01041 console.log("Exception occurred: " + e.name + ": " + e.message);
01042 }
01043 }
01044 Unlock();
01045 console.log("MakeNewConfig completed");
01046 return res;
01047 }
01048 return null;
01049 };
01050
01051 db.RW_saveConfig = function (post, dbConfig) {
01052 if (Lock()) {
01053 console.log("RW_saveConfig: Request to save configuration recieved. Configuration data: " + JSON.stringify(post, null, 4));
01054 var res = { Success: false };
01055 var error = false;
01056 var e;
01057 try {
01058 console.log("Checking for unique Configuration name");
01059 var configs = Connector.RunGetConfigsQuery(dbConfig).search;
01060 if (!Utils.ValidatePath(post.newConfigName) || Utils.ContainsName(configs, post.oldConfigName, "name") < 0) {
01061 console.log("Invalid name detected!");
01062 error = true;
01063 }
01064 while (Utils.ContainsName(configs, post.newConfigName, "name") >= 0) {
01065 console.log("Inferring new configuration name");
01066 post.newConfigName = Utils.Uniquify(post.newConfigName);
01067 }
01068 } catch (e) {
01069 error = true;
01070 console.log("Exception occurred: " + e.name + ": " + e.message);
01071 }
01072 if (!error) {
01073 try {
01074 console.log("Updating Configuration Files");
01075 SaveConfigurationChanges(post.oldConfigName, post.newConfigName, post.files, GetDirectories(post.user, dbConfig), dbConfig);
01076 res.Success = true;
01077 } catch (e) {
01078 console.log("Exception occurred: " + e.name + ": " + e.message);
01079 }
01080 }
01081 Unlock();
01082 console.log("SaveConfig completed");
01083 return res;
01084 }
01085 return null;
01086 };
01087
01088 db.RO_LoadNamedConfig = function (post, dbConfig) {
01089 console.log("RO_LoadNamedConfig: Request for configuration with name \"" + post.configName + "\" and search query \"" + post.query + "\" received.");
01090 if (post.query.length === 0 || post.configName === "No Configurations Found") {
01091 return { collections: [] };
01092 }
01093 return LoadConfigFiles(post.configName, GetDirectories(post.user, dbConfig), JSON.parse(post.query), dbConfig);
01094 };
01095
01096 db.RW_discardConfig = function (post, dbConfig) {
01097 console.log("RW_discardConfig: Discarding configuration with parameters: " + JSON.stringify(post, null, 4));
01098 DiscardWorkingDir(GetDirectories(post.user, dbConfig));
01099 return { Success: true };
01100 };
01101
01102 db.RO_AddOrUpdate = function (post, dbConfig) {
01103 console.log("RO_AddOrUpdate: Request to update table row recieved: " + JSON.stringify(post, null, 4));
01104 UpdateTable(post.configName, post.table, post.row, GetDirectories(post.user, dbConfig));
01105 return { Success: true };
01106 }
01107
01108 db.RO_Update = function (post, dbConfig) {
01109 console.log("RO_Update: Request to update table received: " + JSON.stringify(post, null, 4));
01110 UpdateTable(post.configName, post.table, { id: post.id, name: post.name, column: post.column, value: post.value }, GetDirectories(post.user, dbConfig));
01111 console.log("Update Complete");
01112 return { Success: true };
01113 };
01114
01115 db.RO_LoadConfigMetadata = function (post, dbConfig) {
01116 console.log("RO_LoadConfigMetadata: Request to load configuration metadata received: " + JSON.stringify(post, null, 4) + ", dbConfig: " + JSON.stringify(dbConfig, null, 4));
01117 var ret = { Success: false, data: {} };
01118 try {
01119 ret.data = ReadConfigurationMetadata(post.configName, GetDirectories(post.user, dbConfig), dbConfig);
01120 ret.Success = true;
01121 } catch (e) {
01122 console.log("Exception caught: " + e.name + ": " + e.message);
01123 }
01124 console.log("LoadConfigMetadata complete");
01125 return ret;
01126 };
01127
01128 db.RO_LoadFileMetadata = function (post, dbConfig) {
01129 console.log("RO_LoadFileMetadata: Request to load file metadata received: " + JSON.stringify(post, null, 4));
01130 var ret = { Success: false, data: {} };
01131 var dirs = GetDirectories(post.user, dbConfig);
01132 var error = false;
01133 var query = {};
01134 var e;
01135 try {
01136 var search = Connector.RunBuildFilterQuery(dbConfig, post.configName).search;
01137 for (var s in search) {
01138 if (search.hasOwnProperty(s)) {
01139 if (search[s].query.collection === post.collection && search[s].query.filter["entities.name"] === post.entity) {
01140 query = search[s];
01141 }
01142 }
01143 }
01144 } catch (e) {
01145 error = true;
01146 console.log("Exception caught: " + e.name + ": " + e.message);
01147 }
01148 if (!error) {
01149 try {
01150 ret.data = ReadFileMetadata(dirs, query, dbConfig);
01151 ret.Success = true;
01152 } catch (e) {
01153 console.log("Exception caught: " + e.name + ": " + e.message);
01154 }
01155 }
01156 console.log("LoadFileMetadata complete");
01157 return ret;
01158 };
01159
01160 db.RW_UploadConfigurationFile = function (post, dbConfig) {
01161 if (Lock()) {
01162 console.log("RW_UploadConfigurationFile: Recieved request to upload file: " + JSON.stringify(post, null, 4));
01163 var e;
01164 var error = false;
01165 var ret = { Success: false };
01166 try {
01167 while (VersionExists(post.entity, post.collection, post.version, dbConfig)) {
01168 console.log("Version already exists. Running uniquifier...");
01169 post.version = Utils.Uniquify(post.version);
01170 }
01171 } catch (e) {
01172 error = true;
01173 console.log("Exception caught: " + e.name + ": " + e.message);
01174 }
01175
01176 if (!error) {
01177 console.log("Running store fhicl query");
01178 try {
01179 Connector.RunStoreQuery(dbConfig, post.file, post.collection, post.version, post.entity, post.type);
01180 ret.Success = true;
01181 } catch (e) {
01182 console.log("Exception caught: " + e.name + ": " + e.message);
01183 }
01184 }
01185 Unlock();
01186 console.log("UploadConfigurationFile complete");
01187 return ret;
01188 }
01189 return null;
01190 };
01191
01192 db.RO_DownloadConfigurationFile = function (post, dbConfig) {
01193 console.log("RO_DownloadConfigurationFile: Request to download file(s) received: " + JSON.stringify(post, null, 4));
01194 var dirs = GetDirectories(post.user, dbConfig);
01195 var configObj = JSON.parse(post.config);
01196 try {
01197 if (configObj.entities.length === 1) {
01198 console.log("Single file mode: Fetching file...");
01199 var fileInfo = FetchFile(configObj.entities[0], post.type, dirs.db, dbConfig);
01200
01201 var fclhdrs = {
01202 'Content-Type': 'text/plain',
01203 'Content-Length': fileInfo.size,
01204 'Content-Disposition': 'attachment filename=' + fileInfo.fileName
01205 }
01206 console.log("Headers: " + JSON.stringify(fclhdrs, null, 4) + ", fileInfo: " + JSON.stringify(fileInfo, null, 4));
01207
01208 var fclStream = fs.createReadStream(fileInfo.filePath);
01209 db.emit("stream", fclStream, fclhdrs, 200);
01210 } else if (configObj.entities.length > 1) {
01211 var args = ['cz'];
01212 for (var e in configObj.entities) {
01213 if (configObj.entities.hasOwnProperty(e)) {
01214 args.push(FetchFile(configObj.entities[e], post.type, dirs.db, dbConfig).fileName);
01215 }
01216 }
01217 var fileName = post.tarFileName + ".tar.gz";
01218 var tarhdrs = {
01219 'Content-Type': "application/x-gzip",
01220 'Content-Disposition': 'attachment filename=' + fileName
01221 }
01222
01223 console.log("Spawning: tar " + args.join(" "));
01224 var tar = child_process.spawn("tar", args, { cwd: dirs.db, stdio: [0, 'pipe', 0] });
01225 db.emit("stream", tar.stdout, tarhdrs, 200);
01226
01227 }
01228 } catch (err) {
01229 console.log("Exception caught: " + err.name + ": " + err.message);
01230
01231 var s = new stream.Readable();
01232 s._read = function noop() { };
01233 s.push("ERROR");
01234 s.push(null);
01235
01236 var errhdrs = {
01237 'Content-Type': 'text/plain'
01238 }
01239 db.emit("stream", s, errhdrs, 500);
01240 }
01241
01242 };
01243
01244 db.RO_NamedConfigs = function (post, dbConfig) {
01245 console.log("RO_NamedConfigs: Request for Named Configurations received");
01246 var filter = "" + post.configFilter;
01247 return FindConfigs(filter, dbConfig);
01248 };
01249
01250 db.RW_updateDbConfig = function (post, dbConfig) {
01251 console.log("RW_updateDbConfig: Request to update module configuration received: " + JSON.stringify(post, null, 4));
01252 var output = { Success: false };
01253 if (fs.existsSync(post.baseDir) && (post.dbprovider === "filesystem" || post.dbprovider === "mongo")) {
01254 if (dbConfig.baseDir !== post.baseDir) {
01255 dbConfig.baseDir = post.baseDir;
01256 db.emit("message", { name: "db", target: "baseDir", data: post.baseDir });
01257 }
01258 if (dbConfig.dbprovider !== post.dbprovider) {
01259 dbConfig.dbprovider = post.dbprovider;
01260 db.emit("message", { name: "db", target: "dbprovider", data: post.dbprovider });
01261 }
01262 if (dbConfig.instanceName !== post.instanceName) {
01263 dbConfig.instanceName = post.instanceName;
01264 db.emit("message", { name: "db", target: "instanceName", data: post.instanceName });
01265 }
01266 console.log("DB Config is now: " + JSON.stringify(dbConfig, null, 4));
01267 output.Success = true;
01268 }
01269 return output;
01270 }
01271
01272 db.RO_SearchLoadedConfig = function (post, dbConfig) {
01273 console.log("RO_SearchLoadedConfig: Searching for keys containing name \"" + post.searchKey + "\" from configuration name \"" + post.configName + "\" received.");
01274 if (post.configName === "No Configurations Found") {
01275 return { Success: false, collections: [] };
01276 }
01277 return SearchConfigFiles(post.searchKey, post.configName, GetDirectories(post.user, dbConfig), dbConfig);
01278 }
01279
01280 db.RW_makeNewDBInstance = function (post, dbConfig) {
01281 console.log("RW_makeNewDBInstance: Creating new database instance \"" + post.name + "\".");
01282 dbConfig.instanceName = post.name;
01283 return { Success: true };
01284 }
01285
01286 db.RW_AddEntityToFile = function (post, dbConfig) {
01287 console.log("RW_AddEntityToFile: request to add entity name \"" + post.name + "\" to configuration file " + post.configName + "/" + post.collection + "/" + post.entity);
01288 return { Success: true };
01289 }
01290
01291
01292 db.GET_EntitiesAndVersions = function (dbConfig) {
01293 console.log("GET_EntitiesAndVersions: Request for current Entities and Versions received");
01294 var output = {
01295 Success: false,
01296 collections: []
01297 };
01298 try {
01299 var entities = Connector.RunGetEntitiesQuery(dbConfig).search;
01300 console.log("Returned entities: " + JSON.stringify(entities, null, 4));
01301 for (var ent in entities) {
01302 if (entities.hasOwnProperty(ent)) {
01303 var entity = entities[ent];
01304 var versions = Connector.RunGetVersionsQuery(dbConfig, entity.query);
01305 if (Utils.ContainsName(output.collections, entity.query.collection, "name") < 0) {
01306 output.collections.push({ name: entity.query.collection, entities: [] });
01307 }
01308
01309 var index = Utils.ContainsName(output.collections, entity.query.collection, "name");
01310
01311 var entityObj = {
01312 collection: entity.query.collection,
01313 name: entity.name,
01314 versions: versions
01315 };
01316 output.collections[index].entities.push(entityObj);
01317 }
01318 }
01319 output.Success = true;
01320 } catch (e) {
01321 console.log("Exception caught: " + e.name + ": " + e.message);
01322 }
01323 console.log("EntitiesAndVersions complete");
01324 return output;
01325 };
01326
01327 db.GET_getDbConfig = function (dbConfig) {
01328 console.log("GET_getDbConfig Request for Database Module Configuration received");
01329
01330 var instances = FindInstances(dbConfig);
01331 var output = {
01332 Success: true,
01333 baseDir: dbConfig.baseDir,
01334 dbprovider: dbConfig.dbprovider,
01335 instanceName: dbConfig.instanceName,
01336 data: instances.data
01337 };
01338 return output;
01339 }
01340
01341
01342 db.MasterInitFunction = function (workerData, config) {
01343 var dbConfig = MakeDbConfig(config);
01344 workerData["db"] = dbConfig;
01345 GetDirectories("", dbConfig);
01346 };
01347
01348 module.exports = function (moduleHolder) {
01349 moduleHolder["db"] = db;
01350 };