├── .gitignore ├── ChangeLog ├── Readme.md ├── config └── test │ ├── dbConfig.json │ └── serverConfig.json ├── examples ├── connect_create_drop.js └── open_size_close.js ├── lib └── orientdb │ ├── commands │ ├── Command.js │ ├── CommandBase.js │ ├── ConfigGet.js │ ├── ConfigList.js │ ├── ConfigSet.js │ ├── Connect.js │ ├── Count.js │ ├── DataclusterAdd.js │ ├── DataclusterCount.js │ ├── DataclusterDatarange.js │ ├── DataclusterDrop.js │ ├── DataclusterLHClusterIsUsed.js │ ├── DatasegmentAdd.js │ ├── DatasegmentDrop.js │ ├── DbClose.js │ ├── DbCountrecords.js │ ├── DbCreate.js │ ├── DbDrop.js │ ├── DbExist.js │ ├── DbFreeze.js │ ├── DbList.js │ ├── DbOpen.js │ ├── DbRelease.js │ ├── DbReload.js │ ├── DbSize.js │ ├── ErrorCommand.js │ ├── PositionsHigher.js │ ├── PositionsLower.js │ ├── RecordCreate.js │ ├── RecordDelete.js │ ├── RecordLoad.js │ ├── RecordUpdate.js │ ├── Shutdown.js │ ├── TxCommit.js │ └── operation_types.js │ ├── connection │ ├── debug.js │ ├── long.js │ ├── manager.js │ ├── parser.js │ └── server.js │ ├── db.js │ ├── graphdb.js │ └── index.js ├── package.json └── test ├── disabled_freeze.js ├── errors └── test_db_open_failure.js ├── parser ├── test_configuration.js ├── test_deep_clone.js ├── test_doc_serialize_deserialize.js ├── test_hash_to_sql_sets_0.js ├── test_hash_to_sql_sets_1.js ├── test_hash_to_sql_sets_2.js ├── test_long_conversion.js └── test_writeString.js ├── setup_db.js ├── test_cascading_save.js ├── test_command_collection_result.js ├── test_command_collection_result_after_reopen.js ├── test_command_collection_result_object.js ├── test_command_flat_result.js ├── test_command_query_alias.js ├── test_command_query_params.js ├── test_command_record_result.js ├── test_command_select_from_index.js ├── test_command_very_long_result.js ├── test_config_get.js ├── test_config_list.js ├── test_config_set.js ├── test_connect_disconnect.js ├── test_data_cluster_add_remove_memory.js ├── test_data_cluster_add_remove_physical_memory.js ├── test_data_cluster_count.js ├── test_data_cluster_datarange.js ├── test_data_segment.js ├── test_date.js ├── test_db_countrecords.js ├── test_db_create_drop.js ├── test_db_exist.js ├── test_db_list.js ├── test_db_open_3x.js ├── test_db_open_close.js ├── test_db_open_close_3x.js ├── test_db_open_close_twice.js ├── test_db_size.js ├── test_document_save.js ├── test_embedded_map.js ├── test_embedded_types.js ├── test_error_parsing.js ├── test_fetch_plans.js ├── test_graph.js ├── test_graph_lots_of_edges.js ├── test_graph_with_additional_mandatory_fields.js ├── test_large_document_save_load.js ├── test_lh_cluster_used.js ├── test_multiple_connections.js ├── test_positions.js ├── test_record_create_document.js ├── test_record_create_flat_data.js ├── test_record_create_raw_bytes.js ├── test_record_document_create_load_update_delete.js ├── test_record_load.js ├── test_records_in_cluster.js ├── test_select_flatten.js ├── test_transactions.js └── test_z_shutdown.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | .DS_Store 3 | *.swp 4 | .idea 5 | npm-debug.log 6 | -------------------------------------------------------------------------------- /ChangeLog: -------------------------------------------------------------------------------- 1 | 0.9.9 (in progress) 2 | 3 | * new commands: db.positionsLower() and db.positionsHigher() 4 | * new command: db.isLHClustersUsed() 5 | * new commands: db.addDataSegment(), db.dropDataSegment() 6 | * new commands: server.configList(), server.configGet(), server.configSet() 7 | * db.query() is now an alias of db.command() 8 | * added support to query parameters (ex: "SELECT FROM OUser where name = :name") 9 | 10 | 0.9.8 (2013/02/22) 11 | 12 | * added support to SELECT from indexes 13 | * improved support to orientdb 1.3.0 14 | * added support to transactions 15 | * improved "long" type handling 16 | * improved RID parsing 17 | * errors given to callbacks are now instances of Error 18 | * new command: Server.listDatabases 19 | * various minor improvements and bug fixes and contributions -------------------------------------------------------------------------------- /config/test/dbConfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "user_name" : "admin", 3 | "user_password" : "admin" 4 | } 5 | 6 | -------------------------------------------------------------------------------- /config/test/serverConfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "host" : "localhost", 3 | "port" : 2424, 4 | "user_name" : "root", 5 | "user_password" : "83CACE21A23DB46F93BFD58A3CE48C8D29926C6EF424D7DA9BD725AE070CCDC0" 6 | } 7 | 8 | -------------------------------------------------------------------------------- /examples/connect_create_drop.js: -------------------------------------------------------------------------------- 1 | var Db = require('../lib/orientdb').Db, 2 | Server = require('../lib/orientdb').Server; 3 | 4 | 5 | var dbConfig = { 6 | user_name: "admin", 7 | user_password: "admin" 8 | }; 9 | var serverConfig = { 10 | host: 'localhost', 11 | port: 2424, 12 | user_name: "root", 13 | user_password: "83CACE21A23DB46F93BFD58A3CE48C8D29926C6EF424D7DA9BD725AE070CCDC0" 14 | }; 15 | 16 | var server = new Server(serverConfig); 17 | var db = new Db('test', server, dbConfig); 18 | 19 | 20 | server.connect(function(err, sessionId) { 21 | 22 | if (err) { console.log(err); return; } 23 | 24 | console.log("Connected on session: " + sessionId); 25 | 26 | db.create(function(err) { 27 | 28 | if (err) { console.log(err); return; } 29 | 30 | console.log("Created database: " + db.databaseName); 31 | 32 | db.drop(function(err) { 33 | 34 | if (err) { console.log(err); return; } 35 | 36 | console.log("Deleted database"); 37 | 38 | db.close(function(err) { 39 | 40 | if (err) { console.log(err); return; } 41 | 42 | console.log("Closed connection"); 43 | }); 44 | }); 45 | }); 46 | }); 47 | 48 | -------------------------------------------------------------------------------- /examples/open_size_close.js: -------------------------------------------------------------------------------- 1 | var orient = require("../lib/orientdb"), 2 | Db = orient.Db, 3 | Server = orient.Server; 4 | 5 | 6 | var dbConfig = { 7 | user_name: "admin", 8 | user_password: "admin" 9 | }; 10 | var serverConfig = { 11 | host: "localhost", 12 | port: 2424, 13 | user_name: "root", 14 | user_password: "83CACE21A23DB46F93BFD58A3CE48C8D29926C6EF424D7DA9BD725AE070CCDC0" 15 | }; 16 | 17 | var server = new Server(serverConfig); 18 | var db = new Db("temp", server, dbConfig); 19 | 20 | 21 | db.open(function(err, result) { 22 | 23 | if (err) { console.log(err); return; } 24 | 25 | console.log("Opened database on session: " + result.sessionId); 26 | console.log("Database '" + db.databaseName + "' has " + db.clusters.length + " clusters"); 27 | 28 | db.size(function(err, size) { 29 | 30 | if (err) { console.log(err); return; } 31 | 32 | console.log("Database size: " + size); 33 | 34 | db.close(function(err) { 35 | 36 | if (err) { console.log(err); return; } 37 | 38 | console.log("Closed database"); 39 | }); 40 | }); 41 | }); 42 | 43 | -------------------------------------------------------------------------------- /lib/orientdb/commands/Command.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | base = require("./CommandBase"), 5 | parser = require("../connection/parser"), 6 | _ = require("lodash"), 7 | OperationTypes = require("./operation_types"), 8 | 9 | command = function() { 10 | base.call(this); 11 | 12 | this.result = {}; 13 | this.error = null; 14 | 15 | this.steps.push(readPayloadStatus); 16 | this.steps.push(readPayloadString); 17 | this.steps.push(readPayloadCollection); 18 | this.steps.push(readPayloadRecord); 19 | this.steps.push(readPayloadResultSetFirstRecord); 20 | this.steps.push(readPayloadResultSetPreFetchedRecordInitialShort); 21 | this.steps.push(readPayloadResultSetPreFetchedRecordRecordType); 22 | this.steps.push(readPayloadResultSetPreFetchedRecordRID); 23 | this.steps.push(readPayloadResultSetPreFetchedRecordVersion); 24 | this.steps.push(readPayloadResultSetPreFetchedRecord); 25 | }, 26 | 27 | markers = { 28 | readPayloadStatus: 2, 29 | readPayloadString: 3, 30 | readPayloadCollection: 4, 31 | readPayloadRecord: 5, 32 | readPayloadResultSetFirstRecord: 6, 33 | readPayloadResultSetPreFetchedRecordInitialShort: 7 34 | }; 35 | 36 | util.inherits(command, base); 37 | 38 | module.exports = command; 39 | 40 | command.operation = OperationTypes.COMMAND; 41 | 42 | command.write = function(socket, sessionId, data, callback) { 43 | 44 | // operation type 45 | socket.write(parser.writeByte(command.operation)); 46 | 47 | // invoke callback immediately when the operation is sent to the server 48 | callback(); 49 | 50 | // session ID 51 | socket.write(parser.writeInt(sessionId)); 52 | 53 | // mode 54 | socket.write(parser.writeByte(data.mode.charCodeAt())); 55 | 56 | // serialized command 57 | var buffers = []; 58 | 59 | buffers.push(parser.writeString(data.className)); 60 | 61 | // query text 62 | buffers.push(parser.writeString(data.queryText)); 63 | 64 | function writeOCommandRequestTextAbstract(data, buffers) { 65 | if (data.params) { 66 | buffers.push(parser.writeBoolean(true)); 67 | buffers.push(parser.writeString(parser.serializeDocument(data.params))); 68 | } else { 69 | buffers.push(parser.writeBoolean(false)); 70 | } 71 | 72 | // TODO compositeKeyParamsPresent 73 | buffers.push(parser.writeBoolean(false)); 74 | } 75 | 76 | function writeOSQLQuery(data, buffers) { 77 | // non text limit 78 | buffers.push(parser.writeInt(data.nonTextLimit)); 79 | 80 | // fetchplan 81 | buffers.push(parser.writeString(data.fetchPlan)); 82 | 83 | if (data.params) { 84 | buffers.push(parser.writeString(parser.serializeDocument(data.params))); 85 | } else { 86 | buffers.push(parser.writeInt(0)); 87 | } 88 | } 89 | 90 | switch (data.className) { 91 | case "com.orientechnologies.orient.core.sql.query.OSQLSynchQuery": 92 | case "com.orientechnologies.orient.core.sql.query.OSQLAsynchQuery": 93 | writeOSQLQuery(data, buffers); 94 | break; 95 | default: 96 | writeOCommandRequestTextAbstract(data, buffers); 97 | } 98 | 99 | // append all buffers into one 100 | var length = buffers.length; 101 | for (var idx = 0, size = 0; idx < length; idx++) { 102 | size += buffers[idx].length; 103 | } 104 | var buffer = new Buffer(size); 105 | for (var idx = 0, size = 0; idx < length; idx++) { 106 | size += buffers[idx].copy(buffer, size); 107 | } 108 | 109 | // serialized command 110 | socket.write(parser.writeBytes(buffer)); 111 | }; 112 | 113 | 114 | function readPayloadCollection(buf, offset) { 115 | var result = this.result, 116 | objOffset = { offset: offset, limit: result.count }, 117 | records = parser.readCollection(buf, objOffset); 118 | 119 | // null check is NOT missing 120 | if (_.isUndefined(records)) { 121 | return 0; 122 | } 123 | 124 | result.count = result.count || objOffset.limit; 125 | result.content = result.content || []; 126 | result.content = result.content.concat(records); 127 | 128 | if (result.content.length === result.count) { 129 | this.step = this.steps.length; 130 | } 131 | 132 | return objOffset.offset - offset; 133 | } 134 | 135 | 136 | function readPayloadRecord(buf, offset) { 137 | var result = this.result, 138 | objOffset = { offset: offset }; 139 | 140 | var record = parser.readRecord(buf, objOffset); 141 | // null check is NOT missing 142 | if (_.isUndefined(record)) { 143 | return 0; 144 | } 145 | result.content = result.content || []; 146 | result.content.push(record); 147 | 148 | this.step = this.steps.length; 149 | 150 | return objOffset.offset - offset; 151 | } 152 | 153 | 154 | function readPayloadResultSetFirstRecord(buf, offset) { 155 | var readBytes = this.steps[markers.readPayloadRecord].call(this, buf, offset); 156 | if (readBytes === 0) { 157 | return 0; 158 | } 159 | 160 | this.step = markers.readPayloadStatus; 161 | 162 | return readBytes; 163 | } 164 | 165 | 166 | function readPayloadResultSetPreFetchedRecordInitialShort(buf, offset) { 167 | if (!parser.canReadShort(buf, offset)) { 168 | return 0; 169 | } 170 | 171 | if (parser.isNullOrUndefined(this.result.subcontents)) { 172 | this.result.subcontents = []; 173 | } 174 | 175 | this.result.subcontents.push({}); 176 | 177 | this.step++; 178 | 179 | return parser.BYTES_SHORT; 180 | } 181 | 182 | 183 | function readPayloadResultSetPreFetchedRecordRecordType(buf, offset) { 184 | if (!parser.canReadByte(buf, offset)) { 185 | return 0; 186 | } 187 | 188 | var subcontent = _.last(this.result.subcontents); 189 | subcontent.type = String.fromCharCode(parser.readByte(buf, offset)); 190 | 191 | this.step++; 192 | 193 | return parser.BYTES_BYTE; 194 | } 195 | 196 | 197 | function readPayloadResultSetPreFetchedRecordRID(buf, offset) { 198 | if (!parser.canReadShort(buf, offset)) { 199 | return 0; 200 | } 201 | if (!parser.canReadLong(buf, offset + parser.BYTES_SHORT)) { 202 | return 0; 203 | } 204 | 205 | var subcontent = _.last(this.result.subcontents); 206 | subcontent.rid = parser.toRid(parser.readShort(buf, offset), parser.readLong(buf, offset + parser.BYTES_SHORT)); 207 | 208 | this.step++; 209 | 210 | return parser.BYTES_SHORT + parser.BYTES_LONG; 211 | } 212 | 213 | 214 | function readPayloadResultSetPreFetchedRecordVersion(buf, offset) { 215 | if (!parser.canReadInt(buf, offset)) { 216 | return 0; 217 | } 218 | 219 | var subcontent = _.last(this.result.subcontents); 220 | subcontent.version = parser.readInt(buf, offset); 221 | 222 | this.step++; 223 | 224 | return parser.BYTES_INT; 225 | } 226 | 227 | 228 | function readPayloadResultSetPreFetchedRecord(buf, offset) { 229 | if (!parser.canReadBytes(buf, offset)) { 230 | return 0; 231 | } 232 | 233 | var subcontent = _.last(this.result.subcontents); 234 | subcontent.content = parser.readBytes(buf, offset); 235 | 236 | this.step = markers.readPayloadStatus; 237 | 238 | return parser.BYTES_INT + subcontent.content.length; 239 | } 240 | 241 | 242 | function readPayloadStatus(buf, offset) { 243 | if (!parser.canReadByte(buf, offset)) { 244 | return 0; 245 | } 246 | var status = parser.readByte(buf, offset); 247 | 248 | switch (status) { 249 | case 0: 250 | case 110: // 'n' 251 | // nothing to do 252 | this.step = this.steps.length; 253 | break; 254 | case 1: 255 | this.step = markers.readPayloadResultSetFirstRecord; 256 | break; 257 | case 2: 258 | this.step = markers.readPayloadResultSetPreFetchedRecordInitialShort; 259 | break; 260 | case 97: // 'a' 261 | this.step = markers.readPayloadString; 262 | break; 263 | case 108: // 'l' 264 | this.step = markers.readPayloadCollection; 265 | break; 266 | case 114: // 'r' 267 | this.step = markers.readPayloadRecord; 268 | break; 269 | default: 270 | this.step = this.steps.length; 271 | } 272 | 273 | if (_.isUndefined(this.result.status)) { 274 | this.result.status = status; 275 | } 276 | 277 | return parser.BYTES_BYTE; // byte read 278 | } 279 | 280 | 281 | function readPayloadString(buf, offset) { 282 | if (!parser.canReadString(buf, offset)) { 283 | return 0; 284 | } 285 | 286 | var readString = parser.readString(buf, offset); 287 | this.result.content = readString.value; 288 | this.result.type = "f"; 289 | 290 | this.step = this.steps.length; 291 | 292 | return parser.BYTES_INT + readString.lengthInBytes; 293 | } 294 | -------------------------------------------------------------------------------- /lib/orientdb/commands/CommandBase.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var parser = require("../connection/parser"); 4 | 5 | var command = function() { 6 | this.step = 0; 7 | 8 | this.steps = []; 9 | this.steps.push(command.skipByte); 10 | this.steps.push(command.skipInt); 11 | 12 | this.result = {}; 13 | this.error = null; 14 | }; 15 | 16 | command.prototype.done = function() { 17 | return this.error !== null || this.step >= this.steps.length; 18 | }; 19 | 20 | command.prototype.read = function(buf) { 21 | var bytesRead = 0, 22 | bytesLingering = (this.lingeringBuffer && this.lingeringBuffer.length) || 0, 23 | totalBytesRead = bytesRead, 24 | localBuffer = new Buffer(buf.length + bytesLingering); 25 | 26 | if (bytesLingering) { 27 | this.lingeringBuffer.copy(localBuffer); 28 | } 29 | 30 | buf.copy(localBuffer, bytesLingering); 31 | 32 | while (!this.done() && (bytesRead = command.callStep(this, localBuffer, totalBytesRead))) { 33 | totalBytesRead += bytesRead; 34 | } 35 | 36 | this.lingeringBuffer = new Buffer(localBuffer.length - totalBytesRead); 37 | 38 | localBuffer.copy(this.lingeringBuffer, 0, totalBytesRead); 39 | 40 | // Give back the remaining buffer to the caller 41 | return this.lingeringBuffer; 42 | }; 43 | 44 | command.callStep = function(self, localBuffer, totalBytesRead) { 45 | return self.steps[self.step].call(self, localBuffer, totalBytesRead); 46 | }; 47 | 48 | command.skipByte = function(buf, offset) { 49 | if (!parser.canReadByte(buf, offset)) { 50 | return 0; 51 | } 52 | this.step++; 53 | return parser.BYTES_BYTE; 54 | }; 55 | 56 | 57 | command.skipInt = function(buf, offset) { 58 | if (!parser.canReadInt(buf, offset)) { 59 | return 0; 60 | } 61 | this.step++; 62 | return parser.BYTES_INT; 63 | }; 64 | 65 | 66 | module.exports = command; -------------------------------------------------------------------------------- /lib/orientdb/commands/ConfigGet.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | base = require("./CommandBase"), 5 | parser = require("../connection/parser"), 6 | OperationTypes = require("./operation_types"), 7 | 8 | command = function() { 9 | base.call(this); 10 | 11 | this.steps.push(readConfigValue); 12 | }; 13 | 14 | util.inherits(command, base); 15 | 16 | module.exports = command; 17 | 18 | command.operation = OperationTypes.CONFIG_GET; 19 | 20 | function readConfigValue(buf, offset) { 21 | if (!parser.canReadString(buf, offset)) { 22 | return 0; 23 | } 24 | 25 | var readConfigValue = parser.readString(buf, offset); 26 | 27 | this.result = readConfigValue.value; 28 | this.step++; 29 | 30 | return parser.BYTES_INT + readConfigValue.lengthInBytes; 31 | } 32 | 33 | command.write = function(socket, sessionId, configKey, callback) { 34 | 35 | // operation type 36 | socket.write(parser.writeByte(command.operation)); 37 | 38 | // invoke callback immediately when the operation is sent to the server 39 | callback(); 40 | 41 | // session ID 42 | socket.write(parser.writeInt(sessionId)); 43 | 44 | // config key 45 | socket.write(parser.writeString(configKey)); 46 | }; 47 | 48 | -------------------------------------------------------------------------------- /lib/orientdb/commands/ConfigList.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | base = require("./CommandBase"), 5 | parser = require("../connection/parser"), 6 | OperationTypes = require("./operation_types"), 7 | 8 | command = function() { 9 | base.call(this); 10 | 11 | this.steps.push(readConfigListCount); 12 | this.steps.push(readConfigList); 13 | }; 14 | 15 | util.inherits(command, base); 16 | 17 | module.exports = command; 18 | 19 | command.operation = OperationTypes.CONFIG_LIST; 20 | 21 | function readConfigListCount(buf, offset) { 22 | if (!parser.canReadShort(buf, offset)) { 23 | return 0; 24 | } 25 | this.result.numberOfConfigEntries = parser.readShort(buf, offset); 26 | this.result.configEntries = []; 27 | 28 | if (this.result.numberOfConfigEntries > 0) { 29 | this.step++; 30 | } else { 31 | this.step = this.steps.length; 32 | } 33 | 34 | return parser.BYTES_SHORT; 35 | } 36 | 37 | function readConfigList(buf, offset) { 38 | if (!parser.canReadString(buf, offset)) { 39 | return 0; 40 | } 41 | 42 | var readKey = parser.readString(buf, offset); 43 | 44 | if (!parser.canReadString(buf, offset + parser.BYTES_INT + readKey.lengthInBytes)) { 45 | return 0; 46 | } 47 | 48 | var readValue = parser.readString(buf, offset + parser.BYTES_INT + readKey.lengthInBytes); 49 | 50 | this.result.configEntries.push({ 51 | key: readKey.value, 52 | value: readValue.value 53 | }); 54 | 55 | if (this.result.configEntries.length === this.result.numberOfConfigEntries) { 56 | this.step++; 57 | } 58 | 59 | return parser.BYTES_INT * 2 + readKey.lengthInBytes + readValue.lengthInBytes; 60 | } 61 | 62 | command.write = function(socket, sessionId, data, callback) { 63 | 64 | // operation type 65 | socket.write(parser.writeByte(command.operation)); 66 | 67 | // invoke callback immediately when the operation is sent to the server 68 | callback(); 69 | 70 | // session ID 71 | socket.write(parser.writeInt(sessionId)); 72 | }; 73 | 74 | -------------------------------------------------------------------------------- /lib/orientdb/commands/ConfigSet.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | base = require("./CommandBase"), 5 | parser = require("../connection/parser"), 6 | OperationTypes = require("./operation_types"), 7 | 8 | command = function() { 9 | base.call(this); 10 | }; 11 | 12 | util.inherits(command, base); 13 | 14 | module.exports = command; 15 | 16 | command.operation = OperationTypes.CONFIG_SET; 17 | 18 | command.write = function(socket, sessionId, data, callback) { 19 | 20 | // operation type 21 | socket.write(parser.writeByte(command.operation)); 22 | 23 | // invoke callback immediately when the operation is sent to the server 24 | callback(); 25 | 26 | // session ID 27 | socket.write(parser.writeInt(sessionId)); 28 | 29 | // config key 30 | socket.write(parser.writeString(data.configKey)); 31 | 32 | // config value 33 | socket.write(parser.writeString(data.configValue)); 34 | }; 35 | 36 | -------------------------------------------------------------------------------- /lib/orientdb/commands/Connect.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | packageInfo = require("../../../package.json"), 5 | base = require("./CommandBase"), 6 | parser = require("../connection/parser"), 7 | OperationTypes = require("./operation_types"), 8 | 9 | command = function() { 10 | base.call(this); 11 | 12 | this.steps.push(readSessionId); 13 | }; 14 | 15 | util.inherits(command, base); 16 | 17 | module.exports = command; 18 | 19 | command.operation = OperationTypes.CONNECT; 20 | 21 | function readSessionId(buf, offset) { 22 | if (!parser.canReadInt(buf, offset)) { 23 | return 0; 24 | } 25 | this.result.sessionId = parser.readInt(buf, offset); 26 | this.step++; 27 | return parser.BYTES_INT; 28 | } 29 | 30 | command.write = function(socket, sessionId, data, callback, manager) { 31 | 32 | // operation type 33 | socket.write(parser.writeByte(command.operation)); 34 | 35 | // invoke callback immediately when the operation is sent to the server 36 | callback(); 37 | 38 | // session ID 39 | socket.write(parser.writeInt(sessionId)); 40 | 41 | // driver name 42 | socket.write(parser.writeString(packageInfo.description)); 43 | // driver version 44 | socket.write(parser.writeString(packageInfo.version)); 45 | // protocol version 46 | socket.write(parser.writeShort(manager._PROTOCOL_VERSION)); 47 | // client id 48 | socket.write(parser.writeString("")); 49 | // user name 50 | socket.write(parser.writeString(data.userName)); 51 | // user password 52 | socket.write(parser.writeString(data.userPassword)); 53 | }; 54 | -------------------------------------------------------------------------------- /lib/orientdb/commands/Count.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | base = require("./CommandBase"), 5 | parser = require("../connection/parser"), 6 | OperationTypes = require("./operation_types"), 7 | 8 | command = function() { 9 | base.call(this); 10 | 11 | this.steps.push(readDocumentCount); 12 | }; 13 | 14 | util.inherits(command, base); 15 | 16 | module.exports = command; 17 | 18 | command.operation = OperationTypes.COUNT; 19 | 20 | function readDocumentCount(buf, offset) { 21 | if (!parser.canReadLong(buf, offset)) { 22 | return 0; 23 | } 24 | this.result.count = parser.readLong(buf, offset); 25 | this.step++; 26 | return parser.BYTES_LONG; 27 | } 28 | 29 | command.write = function(socket, sessionId, clusterName, callback) { 30 | 31 | // operation type 32 | socket.write(parser.writeByte(command.operation)); 33 | 34 | // invoke callback imidiately when the operation is sent to the server 35 | callback(); 36 | 37 | // session ID 38 | socket.write(parser.writeInt(sessionId)); 39 | 40 | // clusters list length 41 | socket.write(parser.writeString(clusterName)); 42 | }; 43 | 44 | -------------------------------------------------------------------------------- /lib/orientdb/commands/DataclusterAdd.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | base = require("./CommandBase"), 5 | parser = require("../connection/parser"), 6 | OperationTypes = require("./operation_types"), 7 | 8 | command = function() { 9 | base.call(this); 10 | 11 | this.steps.push(readNumber); 12 | }; 13 | 14 | util.inherits(command, base); 15 | 16 | module.exports = command; 17 | 18 | command.operation = OperationTypes.DATACLUSTER_ADD; 19 | 20 | function readNumber(buf, offset) { 21 | if (!parser.canReadShort(buf, offset)) { 22 | return 0; 23 | } 24 | this.result.number = parser.readShort(buf, offset); 25 | this.step++; 26 | return parser.BYTES_SHORT; 27 | } 28 | 29 | command.write = function(socket, sessionId, data, callback) { 30 | 31 | // operation type 32 | socket.write(parser.writeByte(command.operation)); 33 | 34 | // invoke callback imidiately when the operation is sent to the server 35 | callback(); 36 | 37 | // session ID 38 | socket.write(parser.writeInt(sessionId)); 39 | 40 | // cluster type 41 | socket.write(parser.writeString(data.type)); 42 | 43 | // cluster name 44 | socket.write(parser.writeString(data.name)); 45 | 46 | // filename and size 47 | if (data.type === "PHYSICAL") { 48 | socket.write(parser.writeString(data.fileName)); 49 | socket.write(parser.writeString(data.dataSegmentName)); 50 | } else { 51 | socket.write(parser.writeString(null)); 52 | socket.write(parser.writeString(null)); 53 | } 54 | }; 55 | 56 | -------------------------------------------------------------------------------- /lib/orientdb/commands/DataclusterCount.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | base = require("./CommandBase"), 5 | parser = require("../connection/parser"), 6 | OperationTypes = require("./operation_types"), 7 | 8 | command = function() { 9 | base.call(this); 10 | 11 | this.steps.push(readClusterCount); 12 | }; 13 | 14 | util.inherits(command, base); 15 | 16 | module.exports = command; 17 | 18 | command.operation = OperationTypes.DATACLUSTER_COUNT; 19 | 20 | function readClusterCount(buf, offset) { 21 | if (!parser.canReadLong(buf, offset)) { 22 | return 0; 23 | } 24 | this.result.clusterCount = parser.readLong(buf, offset); 25 | this.step++; 26 | return parser.BYTES_LONG; 27 | } 28 | 29 | command.write = function(socket, sessionId, data, callback) { 30 | 31 | // operation type 32 | socket.write(parser.writeByte(command.operation)); 33 | 34 | // invoke callback imidiately when the operation is sent to the server 35 | callback(); 36 | 37 | // session ID 38 | socket.write(parser.writeInt(sessionId)); 39 | 40 | // clusters array length 41 | socket.write(parser.writeShort(data.clustersId.length)); 42 | 43 | // clusters ids 44 | for (var idx = 0, length = data.clustersId.length; idx < length; idx++) { 45 | socket.write(parser.writeShort(data.clustersId[idx])); 46 | } 47 | }; 48 | 49 | -------------------------------------------------------------------------------- /lib/orientdb/commands/DataclusterDatarange.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | base = require("./CommandBase"), 5 | parser = require("../connection/parser"), 6 | OperationTypes = require("./operation_types"), 7 | 8 | command = function() { 9 | base.call(this); 10 | 11 | this.steps.push(readBegin); 12 | this.steps.push(readEnd); 13 | }; 14 | 15 | util.inherits(command, base); 16 | 17 | module.exports = command; 18 | 19 | command.operation = OperationTypes.DATACLUSTER_DATARANGE; 20 | 21 | function readBegin(buf, offset) { 22 | if (!parser.canReadLong(buf, offset)) { 23 | return 0; 24 | } 25 | this.result.begin = parser.readLong(buf, offset); 26 | this.step++; 27 | return parser.BYTES_LONG; 28 | } 29 | 30 | function readEnd(buf, offset) { 31 | if (!parser.canReadLong(buf, offset)) { 32 | return 0; 33 | } 34 | this.result.end = parser.readLong(buf, offset); 35 | this.step++; 36 | return parser.BYTES_LONG; 37 | } 38 | 39 | command.write = function(socket, sessionId, data, callback) { 40 | 41 | // operation type 42 | socket.write(parser.writeByte(command.operation)); 43 | 44 | // invoke callback imidiately when the operation is sent to the server 45 | callback(); 46 | 47 | // session ID 48 | socket.write(parser.writeInt(sessionId)); 49 | 50 | // cluster ID 51 | socket.write(parser.writeShort(data.clusterId)); 52 | }; 53 | 54 | -------------------------------------------------------------------------------- /lib/orientdb/commands/DataclusterDrop.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | base = require("./CommandBase"), 5 | parser = require("../connection/parser"), 6 | OperationTypes = require("./operation_types"), 7 | 8 | command = function() { 9 | base.call(this); 10 | 11 | this.steps.push(readByte); 12 | }; 13 | 14 | util.inherits(command, base); 15 | 16 | module.exports = command; 17 | 18 | command.operation = OperationTypes.DATACLUSTER_DROP; 19 | 20 | function readByte(buf, offset) { 21 | if (!parser.canReadByte(buf, offset)) { 22 | return 0; 23 | } 24 | this.result.result = parser.readByte(buf, offset); 25 | this.step++; 26 | return parser.BYTES_BYTE; 27 | } 28 | 29 | command.write = function(socket, sessionId, data, callback) { 30 | 31 | // operation type 32 | socket.write(parser.writeByte(command.operation)); 33 | 34 | // invoke callback imidiately when the operation is sent to the server 35 | callback(); 36 | 37 | // session ID 38 | socket.write(parser.writeInt(sessionId)); 39 | 40 | // cluster number 41 | socket.write(parser.writeShort(data.clusterNumber)); 42 | }; 43 | 44 | -------------------------------------------------------------------------------- /lib/orientdb/commands/DataclusterLHClusterIsUsed.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | base = require("./CommandBase"), 5 | parser = require("../connection/parser"), 6 | OperationTypes = require("./operation_types"), 7 | 8 | command = function() { 9 | base.call(this); 10 | 11 | this.steps.push(readIsUsedByte); 12 | }; 13 | 14 | util.inherits(command, base); 15 | 16 | module.exports = command; 17 | 18 | command.operation = OperationTypes.DATACLUSTER_LH_CLUSTER_IS_USED; 19 | 20 | function readIsUsedByte(buf, offset) { 21 | if (!parser.canReadBoolean(buf, offset)) { 22 | return 0; 23 | } 24 | this.result = parser.readBoolean(buf, offset); 25 | this.step++; 26 | return parser.BYTES_BYTE; 27 | } 28 | 29 | command.write = function(socket, sessionId, segmentName, callback) { 30 | 31 | // operation type 32 | socket.write(parser.writeByte(command.operation)); 33 | 34 | // invoke callback imidiately when the operation is sent to the server 35 | callback(); 36 | 37 | // session ID 38 | socket.write(parser.writeInt(sessionId)); 39 | 40 | }; 41 | -------------------------------------------------------------------------------- /lib/orientdb/commands/DatasegmentAdd.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | base = require("./CommandBase"), 5 | parser = require("../connection/parser"), 6 | OperationTypes = require("./operation_types"), 7 | 8 | command = function() { 9 | base.call(this); 10 | 11 | this.steps.push(readNumber); 12 | }; 13 | 14 | util.inherits(command, base); 15 | 16 | module.exports = command; 17 | 18 | command.operation = OperationTypes.DATASEGMENT_ADD; 19 | 20 | function readNumber(buf, offset) { 21 | if (!parser.canReadInt(buf, offset)) { 22 | return 0; 23 | } 24 | this.result.segmentNumber = parser.readInt(buf, offset); 25 | this.step++; 26 | return parser.BYTES_INT; 27 | } 28 | 29 | command.write = function(socket, sessionId, data, callback) { 30 | 31 | // operation type 32 | socket.write(parser.writeByte(command.operation)); 33 | 34 | // invoke callback imidiately when the operation is sent to the server 35 | callback(); 36 | 37 | // session ID 38 | socket.write(parser.writeInt(sessionId)); 39 | 40 | // segment name 41 | socket.write(parser.writeString(data.segmentName)); 42 | 43 | // segment location 44 | socket.write(parser.writeString(data.segmentLocation)); 45 | 46 | }; 47 | -------------------------------------------------------------------------------- /lib/orientdb/commands/DatasegmentDrop.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | base = require("./CommandBase"), 5 | parser = require("../connection/parser"), 6 | OperationTypes = require("./operation_types"), 7 | 8 | command = function() { 9 | base.call(this); 10 | 11 | this.steps.push(readSuccessfulByte); 12 | }; 13 | 14 | util.inherits(command, base); 15 | 16 | module.exports = command; 17 | 18 | command.operation = OperationTypes.DATASEGMENT_DROP; 19 | 20 | function readSuccessfulByte(buf, offset) { 21 | if (!parser.canReadBoolean(buf, offset)) { 22 | return 0; 23 | } 24 | this.result = parser.readBoolean(buf, offset); 25 | this.step++; 26 | return parser.BYTES_BYTE; 27 | } 28 | 29 | command.write = function(socket, sessionId, segmentName, callback) { 30 | 31 | // operation type 32 | socket.write(parser.writeByte(command.operation)); 33 | 34 | // invoke callback imidiately when the operation is sent to the server 35 | callback(); 36 | 37 | // session ID 38 | socket.write(parser.writeInt(sessionId)); 39 | 40 | // segment name 41 | socket.write(parser.writeString(segmentName)); 42 | 43 | }; 44 | -------------------------------------------------------------------------------- /lib/orientdb/commands/DbClose.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | base = require("./CommandBase"), 5 | parser = require("../connection/parser"), 6 | OperationTypes = require("./operation_types"), 7 | 8 | command = function() { 9 | base.call(this); 10 | 11 | this.steps = []; 12 | }; 13 | 14 | util.inherits(command, base); 15 | 16 | module.exports = command; 17 | 18 | command.operation = OperationTypes.DB_CLOSE; 19 | 20 | command.write = function(socket, sessionId, data, callback) { 21 | 22 | // operation type 23 | socket.write(parser.writeByte(command.operation)); 24 | 25 | // session ID 26 | socket.write(parser.writeInt(sessionId)); 27 | 28 | // call it at the END or other functions may too soon send data over the wire 29 | callback(); 30 | }; 31 | 32 | -------------------------------------------------------------------------------- /lib/orientdb/commands/DbCountrecords.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | base = require("./CommandBase"), 5 | parser = require("../connection/parser"), 6 | OperationTypes = require("./operation_types"), 7 | 8 | command = function() { 9 | base.call(this); 10 | 11 | this.steps.push(readCount); 12 | }; 13 | 14 | util.inherits(command, base); 15 | 16 | module.exports = command; 17 | 18 | command.operation = OperationTypes.DB_COUNTRECORDS; 19 | 20 | function readCount(buf, offset) { 21 | if (!parser.canReadLong(buf, offset)) { 22 | return 0; 23 | } 24 | this.result.count = parser.readLong(buf, offset); 25 | this.step++; 26 | return parser.BYTES_LONG; 27 | } 28 | 29 | command.write = function(socket, sessionId, data, callback) { 30 | 31 | // operation type 32 | socket.write(parser.writeByte(command.operation)); 33 | 34 | // invoke callback immediately when the operation is sent to the server 35 | callback(); 36 | 37 | // session ID 38 | socket.write(parser.writeInt(sessionId)); 39 | }; 40 | 41 | -------------------------------------------------------------------------------- /lib/orientdb/commands/DbCreate.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | base = require("./CommandBase"), 5 | parser = require("../connection/parser"), 6 | OperationTypes = require("./operation_types"), 7 | 8 | command = function() { 9 | base.call(this); 10 | }; 11 | 12 | util.inherits(command, base); 13 | 14 | module.exports = command; 15 | 16 | command.operation = OperationTypes.DB_CREATE; 17 | 18 | command.write = function(socket, sessionId, data, callback) { 19 | 20 | // operation type 21 | socket.write(parser.writeByte(command.operation)); 22 | 23 | // invoke callback immediately when the operation is sent to the server 24 | callback(); 25 | 26 | // session ID 27 | socket.write(parser.writeInt(sessionId)); 28 | 29 | // database name 30 | socket.write(parser.writeString(data.databaseName)); 31 | 32 | // database type 33 | socket.write(parser.writeString(data.databaseType)); 34 | 35 | // storage type 36 | socket.write(parser.writeString(data.storageType)); 37 | }; 38 | 39 | -------------------------------------------------------------------------------- /lib/orientdb/commands/DbDrop.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | base = require("./CommandBase"), 5 | parser = require("../connection/parser"), 6 | OperationTypes = require("./operation_types"), 7 | 8 | command = function() { 9 | base.call(this); 10 | }; 11 | 12 | util.inherits(command, base); 13 | 14 | module.exports = command; 15 | 16 | command.operation = OperationTypes.DB_DROP; 17 | 18 | command.write = function(socket, sessionId, data, callback) { 19 | 20 | // operation type 21 | socket.write(parser.writeByte(command.operation)); 22 | 23 | // invoke callback immediately when the operation is sent to the server 24 | callback(); 25 | 26 | // session ID 27 | socket.write(parser.writeInt(sessionId)); 28 | 29 | // database name 30 | socket.write(parser.writeString(data.databaseName)); 31 | }; 32 | 33 | -------------------------------------------------------------------------------- /lib/orientdb/commands/DbExist.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | base = require("./CommandBase"), 5 | parser = require("../connection/parser"), 6 | OperationTypes = require("./operation_types"), 7 | 8 | command = function() { 9 | base.call(this); 10 | 11 | this.steps.push(readResult); 12 | }; 13 | 14 | util.inherits(command, base); 15 | 16 | module.exports = command; 17 | 18 | command.operation = OperationTypes.DB_EXIST; 19 | 20 | function readResult(buf, offset) { 21 | if (!parser.canReadByte(buf, offset)) { 22 | return 0; 23 | } 24 | this.result.result = Boolean(parser.readByte(buf, offset)); 25 | this.step++; 26 | return parser.BYTES_BYTE; 27 | } 28 | 29 | command.write = function(socket, sessionId, data, callback) { 30 | 31 | // operation type 32 | socket.write(parser.writeByte(command.operation)); 33 | 34 | // invoke callback immediately when the operation is sent to the server 35 | callback(); 36 | 37 | // session ID 38 | socket.write(parser.writeInt(sessionId)); 39 | 40 | // database name 41 | socket.write(parser.writeString(data.databaseName)); 42 | }; 43 | 44 | -------------------------------------------------------------------------------- /lib/orientdb/commands/DbFreeze.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | base = require("./CommandBase"), 5 | parser = require("../connection/parser"), 6 | OperationTypes = require("./operation_types"), 7 | 8 | command = function() { 9 | base.call(this); 10 | }; 11 | 12 | util.inherits(command, base); 13 | 14 | module.exports = command; 15 | 16 | command.operation = OperationTypes.DB_FREEZE; 17 | 18 | command.write = function(socket, sessionId, databaseName, callback) { 19 | 20 | // operation type 21 | socket.write(parser.writeByte(command.operation)); 22 | 23 | // session ID 24 | socket.write(parser.writeInt(sessionId)); 25 | 26 | // call it at the END or other functions may too soon send data over the wire 27 | callback(); 28 | 29 | // database name 30 | socket.write(parser.writeString(databaseName)); 31 | }; 32 | 33 | -------------------------------------------------------------------------------- /lib/orientdb/commands/DbList.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | base = require("./CommandBase"), 5 | parser = require("../connection/parser"), 6 | OperationTypes = require("./operation_types"), 7 | 8 | command = function() { 9 | base.call(this); 10 | 11 | this.steps.push(readDatabaseList); 12 | }; 13 | 14 | util.inherits(command, base); 15 | 16 | module.exports = command; 17 | 18 | command.operation = OperationTypes.DB_LIST; 19 | 20 | function readDatabaseList(buf, offset) { 21 | if (!parser.canReadBytes(buf, offset)) { 22 | return 0; 23 | } 24 | this.result.content = parser.readBytes(buf, offset); 25 | this.step++; 26 | return parser.BYTES_INT + this.result.content.length; 27 | } 28 | 29 | command.write = function(socket, sessionId, data, callback) { 30 | 31 | // operation type 32 | socket.write(parser.writeByte(command.operation)); 33 | 34 | // invoke callback immediately when the operation is sent to the server 35 | callback(); 36 | 37 | // session ID 38 | socket.write(parser.writeInt(sessionId)); 39 | }; 40 | 41 | -------------------------------------------------------------------------------- /lib/orientdb/commands/DbOpen.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | debug = require("../connection/debug"), 5 | packageInfo = require("../../../package.json"), 6 | base = require("./CommandBase"), 7 | parser = require("../connection/parser"), 8 | OperationTypes = require("./operation_types"), 9 | 10 | command = function() { 11 | base.call(this); 12 | 13 | this.steps.push(readSessionId); 14 | this.steps.push(readClusters); 15 | this.steps.push(readClusterConfig); 16 | }; 17 | 18 | util.inherits(command, base); 19 | 20 | module.exports = command; 21 | 22 | command.operation = OperationTypes.DB_OPEN; 23 | 24 | function readSessionId(buf, offset) { 25 | if (!parser.canReadInt(buf, offset)) { 26 | return 0; 27 | } 28 | this.result.sessionId = parser.readInt(buf, offset); 29 | this.step++; 30 | return parser.BYTES_INT; 31 | } 32 | 33 | function readClusters(buf, offset) { 34 | if (!parser.canReadShort(buf, offset)) { 35 | return 0; 36 | } 37 | 38 | var clusterCount = parser.readShort(buf, offset); 39 | var bytesRead = parser.BYTES_SHORT; 40 | 41 | var clusters = []; 42 | 43 | // clusters 44 | for (var idx = 0; idx < clusterCount; idx++) { 45 | var cluster = {}; 46 | 47 | // cluster name 48 | if (!parser.canReadString(buf, offset + bytesRead)) { 49 | return 0; 50 | } 51 | var readStringClusterName = parser.readString(buf, offset + bytesRead); 52 | cluster.name = readStringClusterName.value; 53 | bytesRead += parser.BYTES_INT + readStringClusterName.lengthInBytes; 54 | 55 | // cluster id 56 | if (!parser.canReadShort(buf, offset + bytesRead)) { 57 | return 0; 58 | } 59 | cluster.id = parser.readShort(buf, offset + bytesRead); 60 | bytesRead += parser.BYTES_SHORT; 61 | 62 | // cluster type 63 | if (!parser.canReadString(buf, offset + bytesRead)) { 64 | return 0; 65 | } 66 | var readStringClusterType = parser.readString(buf, offset + bytesRead); 67 | cluster.type = readStringClusterType.value; 68 | bytesRead += parser.BYTES_INT + readStringClusterType.lengthInBytes; 69 | 70 | // cluster dataSegment Id 71 | if (!parser.canReadShort(buf, offset + bytesRead)) { 72 | return 0; 73 | } 74 | cluster.dataSegmentId = parser.readShort(buf, offset + bytesRead); 75 | bytesRead += parser.BYTES_SHORT; 76 | 77 | // insert cluster into collection 78 | clusters.push(cluster); 79 | } 80 | 81 | this.result.clusters = clusters; 82 | this.step++; 83 | return bytesRead; 84 | } 85 | 86 | function readClusterConfig(buf, offset) { 87 | if (!parser.canReadString(buf, offset)) { 88 | return 0; 89 | } 90 | 91 | var readStringClusterConfig = parser.readString(buf, offset); 92 | this.result.config = readStringClusterConfig.value; 93 | 94 | this.step++; 95 | return parser.BYTES_INT + readStringClusterConfig.lengthInBytes; 96 | } 97 | 98 | command.write = function(socket, sessionId, data, callback, manager) { 99 | 100 | debug.log("Writing on the socket operation " + command.operation + " on session " + sessionId); 101 | 102 | // operation type 103 | socket.write(parser.writeByte(command.operation)); 104 | 105 | // invoke callback immediately when the operation is sent to the server 106 | callback(); 107 | 108 | // session ID 109 | socket.write(parser.writeInt(sessionId)); 110 | 111 | // driver name 112 | socket.write(parser.writeString(packageInfo.description)); 113 | // driver version 114 | socket.write(parser.writeString(packageInfo.version)); 115 | // protocol version 116 | socket.write(parser.writeShort(manager._PROTOCOL_VERSION)); 117 | // client id 118 | socket.write(parser.writeString("")); 119 | // database name 120 | socket.write(parser.writeString(data.databaseName)); 121 | // database type 122 | socket.write(parser.writeString(data.databaseType)); 123 | // user name 124 | socket.write(parser.writeString(data.userName)); 125 | // user password 126 | socket.write(parser.writeString(data.userPassword)); 127 | }; 128 | 129 | -------------------------------------------------------------------------------- /lib/orientdb/commands/DbRelease.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | base = require("./CommandBase"), 5 | parser = require("../connection/parser"), 6 | OperationTypes = require("./operation_types"), 7 | 8 | command = function() { 9 | base.call(this); 10 | }; 11 | 12 | util.inherits(command, base); 13 | 14 | module.exports = command; 15 | 16 | command.operation = OperationTypes.DB_RELEASE; 17 | 18 | command.write = function(socket, sessionId, databaseName, callback) { 19 | 20 | // operation type 21 | socket.write(parser.writeByte(command.operation)); 22 | 23 | // session ID 24 | socket.write(parser.writeInt(sessionId)); 25 | 26 | // call it at the END or other functions may too soon send data over the wire 27 | callback(); 28 | 29 | // database name 30 | socket.write(parser.writeString(databaseName)); 31 | }; 32 | 33 | -------------------------------------------------------------------------------- /lib/orientdb/commands/DbReload.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | base = require("./CommandBase"), 5 | parser = require("../connection/parser"), 6 | OperationTypes = require("./operation_types"), 7 | 8 | command = function() { 9 | base.call(this); 10 | 11 | this.steps.push(readClusters); 12 | }; 13 | 14 | util.inherits(command, base); 15 | 16 | module.exports = command; 17 | 18 | command.operation = OperationTypes.DB_RELOAD; 19 | 20 | function readClusters(buf, offset) { 21 | if (!parser.canReadShort(buf, offset)) { 22 | return 0; 23 | } 24 | 25 | var clusterCount = parser.readShort(buf, offset); 26 | var bytesRead = parser.BYTES_SHORT; 27 | 28 | var clusters = []; 29 | 30 | // clusters 31 | for (var idx = 0; idx < clusterCount; idx++) { 32 | var cluster = {}; 33 | 34 | // cluster name 35 | if (!parser.canReadString(buf, offset + bytesRead)) { 36 | return 0; 37 | } 38 | var readStringClusterName = parser.readString(buf, offset + bytesRead); 39 | cluster.name = readStringClusterName.value; 40 | bytesRead += parser.BYTES_INT + readStringClusterName.lengthInBytes; 41 | 42 | // cluster id 43 | if (!parser.canReadShort(buf, offset + bytesRead)) { 44 | return 0; 45 | } 46 | cluster.id = parser.readShort(buf, offset + bytesRead); 47 | bytesRead += parser.BYTES_SHORT; 48 | 49 | // cluster type 50 | if (!parser.canReadString(buf, offset + bytesRead)) { 51 | return 0; 52 | } 53 | var readStringClusterType = parser.readString(buf, offset + bytesRead); 54 | cluster.type = readStringClusterType.value; 55 | bytesRead += parser.BYTES_INT + readStringClusterType.lengthInBytes; 56 | 57 | // cluster dataSegment Id 58 | if (!parser.canReadShort(buf, offset + bytesRead)) { 59 | return 0; 60 | } 61 | cluster.dataSegmentId = parser.readShort(buf, offset + bytesRead); 62 | bytesRead += parser.BYTES_SHORT; 63 | 64 | // insert cluster into collection 65 | clusters.push(cluster); 66 | } 67 | 68 | this.result.clusters = clusters; 69 | this.step++; 70 | return bytesRead; 71 | } 72 | 73 | command.write = function(socket, sessionId, data, callback) { 74 | 75 | // operation type 76 | socket.write(parser.writeByte(command.operation)); 77 | 78 | // invoke callback immediately when the operation is sent to the server 79 | callback(); 80 | 81 | // session ID 82 | socket.write(parser.writeInt(sessionId)); 83 | }; 84 | 85 | -------------------------------------------------------------------------------- /lib/orientdb/commands/DbSize.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | base = require("./CommandBase"), 5 | parser = require("../connection/parser"), 6 | OperationTypes = require("./operation_types"), 7 | 8 | command = function() { 9 | base.call(this); 10 | 11 | this.steps.push(readSize); 12 | }; 13 | 14 | util.inherits(command, base); 15 | 16 | module.exports = command; 17 | 18 | command.operation = OperationTypes.DB_SIZE; 19 | 20 | 21 | // TODO after a DB_CREATE the server does not return a size in DB_SIZE 22 | // TODO this will cause problems if a DB_SIZE command comes concatenated with another command 23 | /*if (buf.length !== 13) { 24 | callback(new Error("Could not retrieve the database size. This happens, for example, when you just created the database.")); 25 | return; 26 | }*/ 27 | function readSize(buf, offset) { 28 | if (!parser.canReadLong(buf, offset)) { 29 | return 0; 30 | } 31 | this.result.size = parser.readLong(buf, offset); 32 | this.step++; 33 | return parser.BYTES_LONG; 34 | } 35 | 36 | command.write = function(socket, sessionId, data, callback) { 37 | 38 | // operation type 39 | socket.write(parser.writeByte(command.operation)); 40 | 41 | // invoke callback imidiately when the operation is sent to the server 42 | callback(); 43 | 44 | // session ID 45 | socket.write(parser.writeInt(sessionId)); 46 | }; 47 | 48 | -------------------------------------------------------------------------------- /lib/orientdb/commands/ErrorCommand.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | base = require("./CommandBase"), 5 | parser = require("../connection/parser"), 6 | OperationTypes = require("./operation_types"), 7 | 8 | command = function() { 9 | base.call(this); 10 | 11 | this.steps.push(readError); 12 | }; 13 | 14 | util.inherits(command, base); 15 | 16 | module.exports = command; 17 | 18 | /* 19 | This is not a real command, rather a way to correctly handle data chunk reads 20 | mimicking other commands behaviour (canRead* + read* sequences) 21 | */ 22 | command.operation = OperationTypes.ERROR_COMMAND; 23 | 24 | 25 | function readError(buf, offset) { 26 | if (!parser.canReadError(buf, offset)) { 27 | return 0; 28 | } 29 | var objOffset = { offset: offset }; 30 | this.error = new Error(JSON.stringify(parser.readError(buf, objOffset))); 31 | this.step++; 32 | return objOffset.offset - offset; 33 | } 34 | 35 | command.write = function() { 36 | throw new Error("Unsupported operation"); 37 | }; 38 | 39 | -------------------------------------------------------------------------------- /lib/orientdb/commands/PositionsHigher.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | base = require("./CommandBase"), 5 | parser = require("../connection/parser"), 6 | OperationTypes = require("./operation_types"), 7 | 8 | command = function() { 9 | base.call(this); 10 | 11 | this.steps.push(positionsAvailable); 12 | this.steps.push(readPositions); 13 | }; 14 | 15 | util.inherits(command, base); 16 | 17 | module.exports = command; 18 | 19 | command.operation = OperationTypes.POSITIONS_HIGHER; 20 | 21 | function positionsAvailable(buf, offset) { 22 | if (!parser.canReadInt(buf, offset)) { 23 | return 0; 24 | } 25 | 26 | this.positionsCount = parser.readInt(buf, offset); 27 | this.result = []; 28 | this.step++; 29 | 30 | return parser.BYTES_INT; 31 | } 32 | 33 | function readPositions(buf, offset) { 34 | var initialOffset = offset; 35 | if (!parser.canReadLong(buf, offset)) { 36 | return 0; 37 | } 38 | 39 | var clusterPosition = parser.readLong(buf, offset); 40 | offset += parser.BYTES_LONG; 41 | 42 | if (!parser.canReadInt(buf, offset)) { 43 | return 0; 44 | } 45 | 46 | var dataSegmentId = parser.readInt(buf, offset); 47 | offset += parser.BYTES_INT; 48 | 49 | if (!parser.canReadLong(buf, offset)) { 50 | return 0; 51 | } 52 | 53 | var dataSegmentPos = parser.readLong(buf, offset); 54 | offset += parser.BYTES_LONG; 55 | 56 | if (!parser.canReadInt(buf, offset)) { 57 | return 0; 58 | } 59 | 60 | var recordSize = parser.readInt(buf, offset); 61 | offset += parser.BYTES_INT; 62 | 63 | if (!parser.canReadInt(buf, offset)) { 64 | return 0; 65 | } 66 | 67 | var recordVersion = parser.readInt(buf, offset); 68 | offset += parser.BYTES_INT; 69 | 70 | var position = { 71 | clusterPosition: clusterPosition, 72 | dataSegmentId: dataSegmentId, 73 | dataSegmentPos: dataSegmentPos, 74 | recordSize: recordSize, 75 | recordVersion: recordVersion 76 | }; 77 | 78 | this.result.push(position); 79 | 80 | if (this.result.length >= this.positionsCount) { 81 | this.step++; 82 | } 83 | 84 | return offset - initialOffset; 85 | } 86 | 87 | command.write = function(socket, sessionId, data, callback) { 88 | 89 | // operation type 90 | socket.write(parser.writeByte(command.operation)); 91 | 92 | // invoke callback immediately when the operation is sent to the server 93 | callback(); 94 | 95 | // session ID 96 | socket.write(parser.writeInt(sessionId)); 97 | 98 | // cluster ID 99 | socket.write(parser.writeInt(data.clusterId)); 100 | 101 | // cluster position 102 | socket.write(parser.writeLong(data.clusterPosition)); 103 | }; 104 | 105 | -------------------------------------------------------------------------------- /lib/orientdb/commands/PositionsLower.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | base = require("./CommandBase"), 5 | parser = require("../connection/parser"), 6 | OperationTypes = require("./operation_types"), 7 | 8 | command = function() { 9 | base.call(this); 10 | 11 | this.steps.push(positionsAvailable); 12 | this.steps.push(readPositions); 13 | }; 14 | 15 | util.inherits(command, base); 16 | 17 | module.exports = command; 18 | 19 | command.operation = OperationTypes.POSITIONS_LOWER; 20 | 21 | function positionsAvailable(buf, offset) { 22 | if (!parser.canReadInt(buf, offset)) { 23 | return 0; 24 | } 25 | 26 | this.positionsCount = parser.readInt(buf, offset); 27 | this.result = []; 28 | if (this.positionsCount > 0) { 29 | this.step++; 30 | } else { 31 | this.step = this.steps.length; 32 | } 33 | 34 | return parser.BYTES_INT; 35 | } 36 | 37 | function readPositions(buf, offset) { 38 | var initialOffset = offset; 39 | 40 | if (!parser.canReadLong(buf, offset)) { 41 | return 0; 42 | } 43 | 44 | var clusterPosition = parser.readLong(buf, offset); 45 | offset += parser.BYTES_LONG; 46 | 47 | if (!parser.canReadInt(buf, offset)) { 48 | return 0; 49 | } 50 | 51 | var dataSegmentId = parser.readInt(buf, offset); 52 | offset += parser.BYTES_INT; 53 | 54 | if (!parser.canReadLong(buf, offset)) { 55 | return 0; 56 | } 57 | 58 | var dataSegmentPos = parser.readLong(buf, offset); 59 | offset += parser.BYTES_LONG; 60 | 61 | if (!parser.canReadInt(buf, offset)) { 62 | return 0; 63 | } 64 | 65 | var recordSize = parser.readInt(buf, offset); 66 | offset += parser.BYTES_INT; 67 | 68 | if (!parser.canReadInt(buf, offset)) { 69 | return 0; 70 | } 71 | 72 | var recordVersion = parser.readInt(buf, offset); 73 | offset += parser.BYTES_INT; 74 | 75 | var position = { 76 | clusterPosition: clusterPosition, 77 | dataSegmentId: dataSegmentId, 78 | dataSegmentPos: dataSegmentPos, 79 | recordSize: recordSize, 80 | recordVersion: recordVersion 81 | }; 82 | 83 | this.result.push(position); 84 | 85 | if (this.result.length >= this.positionsCount) { 86 | this.step++; 87 | } 88 | 89 | return offset - initialOffset; 90 | } 91 | 92 | command.write = function(socket, sessionId, data, callback) { 93 | 94 | // operation type 95 | socket.write(parser.writeByte(command.operation)); 96 | 97 | // invoke callback immediately when the operation is sent to the server 98 | callback(); 99 | 100 | // session ID 101 | socket.write(parser.writeInt(sessionId)); 102 | 103 | // cluster ID 104 | socket.write(parser.writeInt(data.clusterId)); 105 | 106 | // cluster position 107 | socket.write(parser.writeLong(data.clusterPosition)); 108 | }; 109 | 110 | -------------------------------------------------------------------------------- /lib/orientdb/commands/RecordCreate.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | base = require("./CommandBase"), 5 | parser = require("../connection/parser"), 6 | OperationTypes = require("./operation_types"), 7 | 8 | command = function() { 9 | base.call(this); 10 | 11 | this.steps.push(readPosition); 12 | this.steps.push(readRecordVersion); 13 | }; 14 | 15 | util.inherits(command, base); 16 | 17 | module.exports = command; 18 | 19 | command.operation = OperationTypes.RECORD_CREATE; 20 | 21 | function readPosition(buf, offset) { 22 | if (!parser.canReadLong(buf, offset)) { 23 | return 0; 24 | } 25 | this.result.position = parser.readLong(buf, offset); 26 | this.step++; 27 | return parser.BYTES_LONG; 28 | } 29 | 30 | function readRecordVersion(buf, offset) { 31 | if (!parser.canReadInt(buf, offset)) { 32 | return 0; 33 | } 34 | this.result.version = parser.readInt(buf, offset); 35 | this.step++; 36 | return parser.BYTES_INT; 37 | } 38 | 39 | command.write = function(socket, sessionId, data, callback) { 40 | 41 | // operation type 42 | socket.write(parser.writeByte(command.operation)); 43 | 44 | // invoke callback immediately when the operation is sent to the server 45 | callback(); 46 | 47 | // session ID 48 | socket.write(parser.writeInt(sessionId)); 49 | 50 | // data Segment ID 51 | socket.write(parser.writeInt(data.dataSegmentId)); 52 | 53 | // cluster ID 54 | socket.write(parser.writeShort(data.clusterId)); 55 | 56 | // record data 57 | socket.write(parser.writeBytes(data.content)); 58 | // record type 59 | socket.write(parser.writeByte(data.type)); 60 | // mode 61 | socket.write(parser.writeByte(data.mode)); 62 | }; 63 | 64 | -------------------------------------------------------------------------------- /lib/orientdb/commands/RecordDelete.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | base = require("./CommandBase"), 5 | parser = require("../connection/parser"), 6 | OperationTypes = require("./operation_types"), 7 | 8 | command = function() { 9 | base.call(this); 10 | 11 | this.steps.push(readStatus); 12 | }; 13 | 14 | util.inherits(command, base); 15 | 16 | module.exports = command; 17 | 18 | command.operation = OperationTypes.RECORD_DELETE; 19 | 20 | function readStatus(buf, offset) { 21 | if (!parser.canReadByte(buf, offset)) { 22 | return 0; 23 | } 24 | this.result.status = parser.readByte(buf, offset); 25 | this.step++; 26 | return parser.BYTES_BYTE; 27 | } 28 | 29 | command.write = function(socket, sessionId, data, callback) { 30 | 31 | // operation type 32 | socket.write(parser.writeByte(command.operation)); 33 | 34 | // invoke callback imidiately when the operation is sent to the server 35 | callback(); 36 | 37 | // session ID 38 | socket.write(parser.writeInt(sessionId)); 39 | 40 | // cluster ID 41 | socket.write(parser.writeShort(data.clusterId)); 42 | // cluster position 43 | socket.write(parser.writeLong(data.clusterPosition)); 44 | // record version 45 | socket.write(parser.writeInt(data.version)); 46 | // mode 47 | socket.write(parser.writeByte(data.mode)); 48 | }; 49 | 50 | -------------------------------------------------------------------------------- /lib/orientdb/commands/RecordLoad.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | base = require("./CommandBase"), 5 | parser = require("../connection/parser"), 6 | _ = require("lodash"), 7 | OperationTypes = require("./operation_types"), 8 | 9 | command = function() { 10 | base.call(this); 11 | 12 | this.steps.push(readPayloadStatus); 13 | this.steps.push(readRecordContent); 14 | this.steps.push(readRecordVersion); 15 | this.steps.push(readRecordType); 16 | this.steps.push(readPayloadStatus); 17 | this.steps.push(readSubRecordIdentifiable); 18 | this.steps.push(readSubRecordType); 19 | this.steps.push(readSubRecordRID); 20 | this.steps.push(readSubRecordVersion); 21 | this.steps.push(readSubRecordContent); 22 | this.steps.push(readPayloadStatus); 23 | }, 24 | 25 | markers = { 26 | readRecordContent: 3, 27 | readSubRecordIdentifiable: 7 28 | }; 29 | 30 | util.inherits(command, base); 31 | 32 | module.exports = command; 33 | 34 | command.operation = OperationTypes.RECORD_LOAD; 35 | 36 | function readPayloadStatus(buf, offset) { 37 | if (!parser.canReadByte(buf, offset)) { 38 | return 0; 39 | } 40 | var status = parser.readByte(buf, offset); 41 | 42 | switch (status) { 43 | case 0: 44 | this.step = this.steps.length; 45 | if (_.isUndefined(this.result.status)) { 46 | this.result.status = status; 47 | } 48 | break; 49 | case 1: 50 | this.result.status = status; 51 | this.step = markers.readRecordContent; 52 | break; 53 | case 2: 54 | this.step = markers.readSubRecordIdentifiable; 55 | break; 56 | default: 57 | this.error = new Error("Payload status " + status + " not implemented!"); 58 | break; 59 | } 60 | 61 | return parser.BYTES_BYTE; 62 | } 63 | 64 | function readRecordContent(buf, offset) { 65 | if (!parser.canReadBytes(buf, offset)) { 66 | return 0; 67 | } 68 | this.result.content = parser.readBytes(buf, offset); 69 | this.step++; 70 | return parser.BYTES_INT + this.result.content.length; 71 | } 72 | 73 | function readRecordVersion(buf, offset) { 74 | if (!parser.canReadInt(buf, offset)) { 75 | return 0; 76 | } 77 | this.result.version = parser.readInt(buf, offset); 78 | this.step++; 79 | return parser.BYTES_INT; 80 | } 81 | 82 | function readRecordType(buf, offset) { 83 | if (!parser.canReadByte(buf, offset)) { 84 | return 0; 85 | } 86 | this.result.type = String.fromCharCode(parser.readByte(buf, offset)); 87 | this.step++; 88 | return parser.BYTES_BYTE; 89 | } 90 | 91 | function readSubRecordIdentifiable(buf, offset) { 92 | if (!parser.canReadShort(buf, offset)) { 93 | return 0; 94 | } 95 | var classId = parser.readShort(buf, offset); 96 | 97 | switch (classId) { 98 | case -2: 99 | this.step = this.steps.length; 100 | break; 101 | case -3: 102 | throw new Error("ClassID " + classId + " not supported"); 103 | default: 104 | if (parser.isNullOrUndefined(this.result.subcontents)) { 105 | this.result.subcontents = []; 106 | } 107 | this.result.subcontents.push({}); 108 | this.step++; 109 | break; 110 | } 111 | 112 | return parser.BYTES_SHORT; 113 | } 114 | 115 | function readSubRecordType(buf, offset) { 116 | if (!parser.canReadByte(buf, offset)) { 117 | return 0; 118 | } 119 | var subcontent = this.result.subcontents[this.result.subcontents.length - 1]; 120 | subcontent.type = String.fromCharCode(parser.readByte(buf, offset)); 121 | 122 | this.step++; 123 | 124 | return parser.BYTES_BYTE; 125 | } 126 | 127 | function readSubRecordRID(buf, offset) { 128 | if (!parser.canReadShort(buf, offset)) { 129 | return 0; 130 | } 131 | if (!parser.canReadLong(buf, offset + parser.BYTES_SHORT)) { 132 | return 0; 133 | } 134 | 135 | var subcontent = this.result.subcontents[this.result.subcontents.length - 1]; 136 | subcontent.rid = parser.toRid(parser.readShort(buf, offset), parser.readLong(buf, offset + parser.BYTES_SHORT)); 137 | 138 | this.step++; 139 | 140 | return parser.BYTES_SHORT + parser.BYTES_LONG; 141 | } 142 | 143 | function readSubRecordVersion(buf, offset) { 144 | if (!parser.canReadInt(buf, offset)) { 145 | return 0; 146 | } 147 | 148 | var subcontent = this.result.subcontents[this.result.subcontents.length - 1]; 149 | subcontent.version = parser.readInt(buf, offset); 150 | 151 | this.step++; 152 | 153 | return parser.BYTES_INT; 154 | } 155 | 156 | function readSubRecordContent(buf, offset) { 157 | if (!parser.canReadBytes(buf, offset)) { 158 | return 0; 159 | } 160 | var subcontent = this.result.subcontents[this.result.subcontents.length - 1]; 161 | 162 | subcontent.content = parser.readBytes(buf, offset); 163 | 164 | this.step++; 165 | 166 | return parser.BYTES_INT + subcontent.content.length; 167 | } 168 | 169 | command.write = function(socket, sessionId, data, callback, manager) { 170 | 171 | // operation type 172 | socket.write(parser.writeByte(this.operation)); 173 | 174 | // invoke callback immediately when the operation is sent to the server 175 | callback(); 176 | 177 | // session ID 178 | socket.write(parser.writeInt(sessionId)); 179 | 180 | // cluster ID 181 | socket.write(parser.writeShort(data.clusterId)); 182 | // cluster position 183 | socket.write(parser.writeLong(data.clusterPosition)); 184 | 185 | // fetch plan 186 | socket.write(parser.writeString(data.fetchPlan)); 187 | 188 | // ignore cache 189 | socket.write(parser.writeByte(data.ignoreCache)); 190 | 191 | // https://github.com/gabipetrovay/node-orientdb/issues/105 192 | if (manager.serverProtocolVersion === 13) { 193 | // load tombstone 194 | socket.write(parser.writeByte(0)); 195 | } 196 | }; 197 | 198 | -------------------------------------------------------------------------------- /lib/orientdb/commands/RecordUpdate.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | base = require("./CommandBase"), 5 | parser = require("../connection/parser"), 6 | OperationTypes = require("./operation_types"), 7 | 8 | command = function() { 9 | base.call(this); 10 | 11 | this.steps.push(readRecordVersion); 12 | }; 13 | 14 | util.inherits(command, base); 15 | 16 | module.exports = command; 17 | 18 | command.operation = OperationTypes.RECORD_UPDATE; 19 | 20 | function readRecordVersion(buf, offset) { 21 | if (!parser.canReadInt(buf, offset)) { 22 | return 0; 23 | } 24 | this.result.version = parser.readInt(buf, offset); 25 | this.step++; 26 | return parser.BYTES_INT; 27 | } 28 | 29 | command.write = function(socket, sessionId, data, callback) { 30 | 31 | // operation type 32 | socket.write(parser.writeByte(command.operation)); 33 | 34 | // invoke callback immediately when the operation is sent to the server 35 | callback(); 36 | 37 | // session ID 38 | socket.write(parser.writeInt(sessionId)); 39 | 40 | // cluster ID 41 | socket.write(parser.writeShort(data.clusterId)); 42 | // cluster position 43 | socket.write(parser.writeLong(data.clusterPosition)); 44 | 45 | // record data 46 | socket.write(parser.writeBytes(data.content)); 47 | // record version 48 | socket.write(parser.writeInt(data.version)); 49 | // record type 50 | socket.write(parser.writeByte(data.type)); 51 | // mode 52 | socket.write(parser.writeByte(data.mode)); 53 | }; 54 | 55 | -------------------------------------------------------------------------------- /lib/orientdb/commands/Shutdown.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | base = require("./CommandBase"), 5 | parser = require("../connection/parser"), 6 | OperationTypes = require("./operation_types"), 7 | 8 | command = function() { 9 | base.call(this); 10 | }; 11 | 12 | util.inherits(command, base); 13 | 14 | module.exports = command; 15 | 16 | command.operation = OperationTypes.SHUTDOWN; 17 | 18 | command.write = function(socket, sessionId, data, callback) { 19 | // operation type 20 | socket.write(parser.writeByte(command.operation)); 21 | 22 | // invoke callback immediately when the operation is sent to the server 23 | callback(); 24 | 25 | // session ID 26 | socket.write(parser.writeInt(sessionId, true)); 27 | 28 | // user name 29 | socket.write(parser.writeString(data.userName)); 30 | // user password 31 | socket.write(parser.writeString(data.userPassword)); 32 | }; 33 | 34 | -------------------------------------------------------------------------------- /lib/orientdb/commands/TxCommit.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | base = require("./CommandBase"), 5 | parser = require("../connection/parser"), 6 | _ = require("lodash"), 7 | OperationTypes = require("./operation_types"), 8 | 9 | command = function() { 10 | base.call(this); 11 | 12 | this.steps.push(readRecordsCreatedCount); 13 | this.steps.push(readRecordsCreated); 14 | this.steps.push(readRecordsUpdatedCount); 15 | this.steps.push(readRecordsUpdated); 16 | }; 17 | 18 | util.inherits(command, base); 19 | 20 | module.exports = command; 21 | 22 | command.operation = OperationTypes.TX_COMMIT; 23 | 24 | function readRecordsCreatedCount(buf, offset) { 25 | if (!parser.canReadInt(buf, offset)) { 26 | return 0; 27 | } 28 | 29 | this.result.numberOfRecordsCreated = parser.readInt(buf, offset); 30 | this.result.recordsCreated = []; 31 | 32 | if (this.result.numberOfRecordsCreated > 0) { 33 | this.step++; 34 | } else { 35 | this.step += 2; 36 | } 37 | 38 | return parser.BYTES_INT; 39 | } 40 | 41 | function readRecordsCreated(buf, offset) { 42 | var bytesToRead = (parser.BYTES_SHORT * 2) + (parser.BYTES_LONG * 2); 43 | if (offset + bytesToRead > buf.length) { 44 | return 0; 45 | } 46 | 47 | var createdRecordRIDs = { 48 | fromClusterId: parser.readShort(buf, offset), 49 | fromClusterPosition: parser.readLong(buf, offset + parser.BYTES_SHORT), 50 | toClusterId: parser.readShort(buf, offset + parser.BYTES_SHORT + parser.BYTES_LONG), 51 | toClusterPosition: parser.readLong(buf, offset + parser.BYTES_SHORT + parser.BYTES_LONG + parser.BYTES_SHORT) 52 | }; 53 | 54 | this.result.recordsCreated.push(createdRecordRIDs); 55 | if (this.result.recordsCreated.length === this.result.numberOfRecordsCreated) { 56 | this.step++; 57 | } 58 | 59 | return bytesToRead; 60 | } 61 | 62 | function readRecordsUpdatedCount(buf, offset) { 63 | if (!parser.canReadInt(buf, offset)) { 64 | return 0; 65 | } 66 | 67 | this.result.numberOfRecordsUpdated = parser.readInt(buf, offset); 68 | this.result.recordsUpdated = []; 69 | 70 | if (this.result.numberOfRecordsUpdated > 0) { 71 | this.step++; 72 | } else { 73 | this.step += 2; 74 | } 75 | 76 | return parser.BYTES_INT; 77 | } 78 | 79 | function readRecordsUpdated(buf, offset) { 80 | var bytesToRead = parser.BYTES_SHORT + parser.BYTES_LONG + parser.BYTES_INT; 81 | if (offset + bytesToRead > buf.length) { 82 | return 0; 83 | } 84 | 85 | var updatedRecordRIDs = { 86 | clusterId: parser.readShort(buf, offset), 87 | clusterPosition: parser.readLong(buf, offset + parser.BYTES_SHORT), 88 | version: parser.readInt(buf, offset + parser.BYTES_SHORT + parser.BYTES_LONG) 89 | }; 90 | 91 | this.result.recordsUpdated.push(updatedRecordRIDs); 92 | if (this.result.recordsUpdated.length === this.result.numberOfRecordsUpdated) { 93 | this.step++; 94 | } 95 | 96 | return bytesToRead; 97 | } 98 | 99 | command.write = function(socket, sessionId, data, callback) { 100 | 101 | // operation type 102 | socket.write(parser.writeByte(this.operation)); 103 | 104 | // invoke callback immediately when the operation is sent to the server 105 | callback(); 106 | 107 | // session ID 108 | socket.write(parser.writeInt(sessionId)); 109 | 110 | // transaction ID 111 | socket.write(parser.writeInt(data.id)); 112 | 113 | // use transaction log to recover 114 | socket.write(parser.writeByte(1)); 115 | 116 | for (var idx = 0, length = data.docs.length; idx < length; idx++) { 117 | // === 1 means: one more doc is part of this transaction 118 | socket.write(parser.writeByte(1)); 119 | 120 | var doc = data.docs[idx]; 121 | 122 | // operation type 123 | switch (data.actions[idx]) { 124 | case "create": 125 | socket.write(parser.writeByte(3)); 126 | break; 127 | case "update": 128 | socket.write(parser.writeByte(1)); 129 | break; 130 | case "delete": 131 | socket.write(parser.writeByte(2)); 132 | break; 133 | default: 134 | callback(new Error("Unknown action: " + data.actions[idx])); 135 | return; 136 | } 137 | 138 | // cluster id and position 139 | var rid = parser.parseRid(doc["@rid"]); 140 | socket.write(parser.writeShort(rid.clusterId)); 141 | socket.write(parser.writeLong(rid.clusterPosition)); 142 | 143 | // record type 144 | var type; 145 | if (doc["@type"]) { 146 | type = doc["@type"]; 147 | } else { 148 | type = "d"; 149 | } 150 | type = parser.decodeRecordType(type); 151 | socket.write(parser.writeByte(type)); 152 | 153 | switch (data.actions[idx]) { 154 | case "create": 155 | socket.write(parser.writeBytes(parser.stringToBuffer(parser.serializeDocument(doc)))); 156 | break; 157 | case "update": 158 | socket.write(parser.writeInt(doc["@version"])); 159 | socket.write(parser.writeBytes(parser.stringToBuffer(parser.serializeDocument(doc)))); 160 | break; 161 | case "delete": 162 | socket.write(parser.writeInt(doc["@version"])); 163 | break; 164 | default: 165 | callback(new Error("Unknown action: " + data.actions[idx])); 166 | return; 167 | } 168 | 169 | } 170 | // !== 1 means: no more docs 171 | socket.write(parser.writeByte(2)); 172 | 173 | socket.write(parser.writeString("")); 174 | }; 175 | 176 | -------------------------------------------------------------------------------- /lib/orientdb/commands/operation_types.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | /* 4 | * See com.orientechnologies.orient.enterprise.channel.binary.OChannelBinaryProtocol 5 | */ 6 | module.exports = { 7 | // OUTGOING 8 | SHUTDOWN: 1, 9 | CONNECT: 2, 10 | 11 | DB_OPEN: 3, 12 | DB_CREATE: 4, 13 | DB_CLOSE: 5, 14 | DB_EXIST: 6, 15 | DB_DROP: 7, 16 | DB_SIZE: 8, 17 | DB_COUNTRECORDS: 9, 18 | 19 | DATACLUSTER_ADD: 10, 20 | DATACLUSTER_DROP: 11, 21 | DATACLUSTER_COUNT: 12, 22 | DATACLUSTER_DATARANGE: 13, 23 | DATACLUSTER_LH_CLUSTER_IS_USED: 16, // since 1.2.0 24 | 25 | DATASEGMENT_ADD: 20, 26 | DATASEGMENT_DROP: 21, 27 | 28 | RECORD_LOAD: 30, 29 | RECORD_CREATE: 31, 30 | RECORD_UPDATE: 32, 31 | RECORD_DELETE: 33, 32 | RECORD_CHANGE_IDENTITY: 35, // since 1.2.0 33 | POSITIONS_HIGHER: 36, // since 1.3.0 34 | POSITIONS_LOWER: 37, // since 1.3.0 35 | RECORD_CLEAN_OUT: 38, // since 1.3.0 36 | POSITIONS_FLOOR: 39, // since 1.3.0 37 | 38 | COUNT: 40, 39 | COMMAND: 41, 40 | POSITIONS_CEILING: 42, // since 1.3.0 41 | 42 | TX_COMMIT: 60, 43 | 44 | CONFIG_GET: 70, 45 | CONFIG_SET: 71, 46 | CONFIG_LIST: 72, 47 | DB_RELOAD: 73, // SINCE 1.0rc4 48 | DB_LIST: 74, // SINCE 1.0rc6 49 | 50 | PUSH_RECORD: 79, 51 | PUSH_DISTRIB_CONFIG: 80, 52 | 53 | // DISTRIBUTED 54 | DB_COPY: 90, // SINCE 1.0rc8 55 | REPLICATION: 91, // SINCE 1.0 56 | CLUSTER: 92, // SINCE 1.0 57 | 58 | // Lock + sync 59 | DB_FREEZE: 94, // SINCE 1.1.0 60 | DB_RELEASE: 95, // SINCE 1.1.0 61 | 62 | ERROR_COMMAND: -999 // driver internal: not a real command 63 | }; -------------------------------------------------------------------------------- /lib/orientdb/connection/debug.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | parser = require("./parser"), 5 | _ = require("lodash"), 6 | options; 7 | 8 | /*------------------------------------------------------------------------------ 9 | (public) options 10 | 11 | + options 12 | - void 13 | 14 | Constructor - set up debug. 15 | ------------------------------------------------------------------------------*/ 16 | var initOptions = function(optionsArg) { 17 | options = { 18 | logOperations: optionsArg.logOperations || false, 19 | logErrors: optionsArg.logErrors || false, 20 | color: 1 21 | }; 22 | }; 23 | 24 | /*------------------------------------------------------------------------------ 25 | (public) log 26 | 27 | + message 28 | 29 | Output message to stdout. 30 | ------------------------------------------------------------------------------*/ 31 | var log = function(message) { 32 | if (options.logOperations) { 33 | util.log( 34 | "OrientDB debug: " + 35 | message 36 | ); 37 | } 38 | }; 39 | 40 | /* recursive through objects */ 41 | 42 | var look = function(obj) { 43 | var A = [], tem, p; 44 | for (p in obj) { 45 | if (obj.hasOwnProperty(p)) { 46 | tem = obj[p]; 47 | if (tem && _.isObject(tem)) { 48 | A[A.length] = p + ':{ ' + look(tem).join(', ') + '}'; 49 | } else { 50 | A[A.length] = ["\t" + p + ':' + tem.toString() + "\n"]; 51 | } 52 | } 53 | } 54 | return A; 55 | }; 56 | 57 | module.exports.look = look; 58 | module.exports.log = log; 59 | module.exports.options = initOptions; -------------------------------------------------------------------------------- /lib/orientdb/connection/server.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var Manager = require("./manager"), 4 | OperationTypes = require("../commands/operation_types"), 5 | parser = require("./parser"), 6 | _ = require("lodash"); 7 | 8 | function EMPTY_FUNCTION() { 9 | } 10 | 11 | var Server = exports.Server = function(options) { 12 | this.sessionId = -1; 13 | 14 | this.options = options || {}; 15 | this.host = this.options.host || "localhost"; 16 | this.port = this.options.port || 2424; 17 | this.userName = this.options.user_name || "root"; 18 | this.userPassword = this.options.user_password || ""; 19 | this.logOperations = this.options.logOperations || false; 20 | this.logError = this.options.logError || false; 21 | 22 | this.manager = new Manager(options); 23 | 24 | // Set up logger if any set 25 | if (!parser.isNullOrUndefined(this.options.logger) && _.isFunction(this.options.logger.debug) && _.isFunction(this.options.logger.error)) { 26 | this.logger = this.options.logger; 27 | } else { 28 | this.logger = { 29 | error: function(message, object) { 30 | }, 31 | log: function(message, object) { 32 | }, 33 | debug: function(message, object) { 34 | } 35 | }; 36 | } 37 | }; 38 | 39 | 40 | Server.prototype.send = function(operation, options, callback) { 41 | var self = this; 42 | if (_.isFunction(options)) { 43 | callback = options; 44 | options = null; 45 | } 46 | 47 | var callbackToCall = callback; 48 | if (operation === OperationTypes.DB_CLOSE) { 49 | // this will reset the session ID such that one can reopen a closed connection 50 | callbackToCall = function() { 51 | self.sessionId = -1; 52 | callback(); 53 | }; 54 | } 55 | self.manager.writeRequest(self.sessionId, operation, options, callbackToCall); 56 | }; 57 | 58 | 59 | Server.prototype.connect = function(callback) { 60 | var self = this; 61 | callback = callback || EMPTY_FUNCTION; 62 | 63 | self.init(function(err) { 64 | 65 | if (err) { return callback(err); } 66 | 67 | var data = { 68 | userName: self.userName, 69 | userPassword: self.userPassword 70 | }; 71 | 72 | self.send(OperationTypes.CONNECT, data, function(err, result) { 73 | 74 | if (err) { return callback(err); } 75 | 76 | result = result || {}; 77 | self.sessionId = result.sessionId || -1; 78 | 79 | callback(err, self.sessionId); 80 | }); 81 | }); 82 | }; 83 | 84 | 85 | Server.prototype.disconnect = function(callback) { 86 | var self = this; 87 | callback = callback || EMPTY_FUNCTION; 88 | 89 | self.send(OperationTypes.DB_CLOSE, callback); 90 | }; 91 | 92 | 93 | Server.prototype.listDatabases = function(callback) { 94 | callback = callback || EMPTY_FUNCTION; 95 | 96 | this.send(OperationTypes.DB_LIST, function(err, result) { 97 | if (err) return callback(err); 98 | 99 | var doc = parser.deserializeDocument(result.content.toString()); 100 | 101 | callback(null, doc.databases); 102 | }); 103 | }; 104 | 105 | 106 | Server.prototype.configList = function(callback) { 107 | callback = callback || EMPTY_FUNCTION; 108 | 109 | // disabled due to https://github.com/nuvolabase/orientdb/issues/1291 110 | if (this.manager.serverProtocolVersion === 13) { 111 | return callback(new Error("configList unsupported with OrientDB 1.3.0")); 112 | } 113 | 114 | this.send(OperationTypes.CONFIG_LIST, function(err, result) { 115 | if (err) return callback(err); 116 | 117 | var config = {}; 118 | for (var key in result.configEntries) { 119 | var entry = result.configEntries[key]; 120 | config[entry.key] = entry.value; 121 | } 122 | 123 | callback(null, config); 124 | }); 125 | }; 126 | 127 | 128 | Server.prototype.configGet = function(configKey, callback) { 129 | callback = callback || EMPTY_FUNCTION; 130 | 131 | this.send(OperationTypes.CONFIG_GET, configKey, function(err, configValue) { 132 | if (err) return callback(err); 133 | 134 | callback(null, configValue); 135 | }); 136 | }; 137 | 138 | 139 | Server.prototype.configSet = function(configKey, configValue, callback) { 140 | callback = callback || EMPTY_FUNCTION; 141 | 142 | var data = { 143 | configKey: configKey, 144 | configValue: configValue 145 | }; 146 | 147 | this.send(OperationTypes.CONFIG_SET, data, function(err) { 148 | callback(err); 149 | }); 150 | }; 151 | 152 | 153 | Server.prototype.freeze = function(databaseName, callback) { 154 | callback = callback || EMPTY_FUNCTION; 155 | 156 | this.send(OperationTypes.DB_FREEZE, databaseName, callback); 157 | }; 158 | 159 | 160 | Server.prototype.release = function(databaseName, callback) { 161 | callback = callback || EMPTY_FUNCTION; 162 | 163 | this.send(OperationTypes.DB_RELEASE, databaseName, callback); 164 | }; 165 | 166 | 167 | Server.prototype.shutdown = function(callback) { 168 | var self = this; 169 | callback = callback || EMPTY_FUNCTION; 170 | 171 | var data = { 172 | userName: self.userName, 173 | userPassword: self.userPassword 174 | }; 175 | 176 | self.send(OperationTypes.SHUTDOWN, data, callback); 177 | }; 178 | 179 | 180 | Server.prototype.init = function(callback) { 181 | var self = this; 182 | callback = callback || EMPTY_FUNCTION; 183 | 184 | if (self.sessionId === -1) { 185 | self.manager.connect(callback); 186 | } else { 187 | callback(); 188 | } 189 | }; 190 | 191 | -------------------------------------------------------------------------------- /lib/orientdb/graphdb.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var util = require("util"), 4 | Db = require("./db").Db, 5 | parser = require("./connection/parser"), 6 | _ = require("lodash"); 7 | 8 | var VERTEX_CLASS_NAME = "OGraphVertex"; 9 | var EDGE_CLASS_NAME = "OGraphEdge"; 10 | var PERSISTENT_CLASS_NAME = "ORIDs"; 11 | var FIELD_IN = "in"; 12 | var FIELD_OUT = "out"; 13 | 14 | var GraphDb = exports.GraphDb = function(databaseName, server, options) { 15 | Db.call(this, databaseName, server, options); 16 | }; 17 | 18 | util.inherits(GraphDb, Db); 19 | 20 | function checkForGraphSchema(self, callback) { 21 | 22 | var checkORIDs = function(callback) { 23 | var cluster = self.getClusterByClass(PERSISTENT_CLASS_NAME); 24 | if (cluster === null) { 25 | self.createClass("ORIDs", callback); 26 | } else { 27 | return callback(); 28 | } 29 | }; 30 | 31 | var checkClassWithShortName = function(fieldName, className, classShortName, inOutFieldType, callback) { 32 | self[fieldName] = self.getClassByName(className); 33 | if (parser.isNullOrUndefined(self[fieldName])) { 34 | self.createClass(className, function(err) { 35 | if (err) { return callback(err); } 36 | 37 | self.command("ALTER CLASS " + className + " SHORTNAME " + classShortName, function(err) { 38 | if (err) { return callback(err); } 39 | 40 | self.command("CREATE PROPERTY " + className + ".in " + inOutFieldType, function(err) { 41 | if (err) { return callback(err); } 42 | 43 | self.command("CREATE PROPERTY " + className + ".out " + inOutFieldType, callback); 44 | }); 45 | }); 46 | }); 47 | } else { 48 | return callback(); 49 | } 50 | }; 51 | 52 | var checkVertexClass = function(callback) { 53 | checkClassWithShortName("vertexClass", VERTEX_CLASS_NAME, "V", "LINKSET", function(err) { 54 | if (err) { return callback(err); } 55 | 56 | self.command("ALTER CLASS " + VERTEX_CLASS_NAME + " OVERSIZE 2", callback); 57 | }); 58 | }; 59 | 60 | checkORIDs(function(err) { 61 | if (err) { return callback(err); } 62 | 63 | checkVertexClass(function(err) { 64 | if (err) { return callback(err); } 65 | 66 | checkClassWithShortName("edgeClass", EDGE_CLASS_NAME, "E", "LINK", function(err) { 67 | if (err) { return callback(err); } 68 | 69 | self.reload(callback); 70 | }); 71 | }); 72 | }); 73 | } 74 | 75 | GraphDb.prototype.open = function(callback) { 76 | var self = this; 77 | Db.prototype.open.call(self, function(err) { 78 | 79 | if (err) { return callback(err); } 80 | 81 | checkForGraphSchema(self, callback); 82 | }); 83 | }; 84 | 85 | GraphDb.prototype.createVertex = function(hash, options, callback) { 86 | if (_.isFunction(hash)) { 87 | callback = hash; 88 | hash = undefined; 89 | options = {}; 90 | } else if (_.isFunction(options)) { 91 | callback = options; 92 | options = {}; 93 | } 94 | 95 | var clazz = options["class"] || VERTEX_CLASS_NAME; 96 | 97 | var self = this; 98 | 99 | var cluster = self.getClusterByClass(clazz); 100 | 101 | var sqlsets = parser.hashToSQLSets(hash); 102 | 103 | var command = "CREATE VERTEX " + clazz + " CLUSTER " + cluster.name; 104 | if (sqlsets.sqlsets !== "") { 105 | command = command.concat(" ", sqlsets.sqlsets); 106 | } 107 | 108 | self.command(command, function(err, results) { 109 | if (err) { return callback(err); } 110 | 111 | var vertex = results[0]; 112 | 113 | if (_.isEmpty(sqlsets.remainingHash)) { 114 | callback(null, vertex); 115 | } else { 116 | _.extend(vertex, sqlsets.remainingHash); 117 | self.save(vertex, callback); 118 | } 119 | }); 120 | }; 121 | 122 | GraphDb.prototype.createEdge = function(sourceVertexOrRID, destVertexOrRID, hash, options, callback) { 123 | if (_.isFunction(hash)) { 124 | callback = hash; 125 | hash = undefined; 126 | options = {}; 127 | } else if (_.isFunction(options)) { 128 | callback = options; 129 | options = {}; 130 | } 131 | 132 | function ridFrom(obj) { 133 | if (!_.isString(obj)) { 134 | return obj["@rid"]; 135 | } 136 | return obj; 137 | } 138 | 139 | function pushRIDInto(rid, obj, field) { 140 | if (_.isString(obj)) { 141 | return; 142 | } 143 | 144 | if (obj[field] === null || typeof obj[field] === "undefined") { 145 | obj[field] = []; 146 | } 147 | obj[field].push(rid); 148 | } 149 | 150 | var sourceRID = ridFrom(sourceVertexOrRID); 151 | var destRID = ridFrom(destVertexOrRID); 152 | 153 | var clazz = options["class"] || EDGE_CLASS_NAME; 154 | 155 | var self = this; 156 | 157 | var cluster = self.getClusterByClass(clazz); 158 | 159 | var sqlsets = parser.hashToSQLSets(hash); 160 | 161 | var command = "CREATE EDGE " + clazz + " CLUSTER " + cluster.name + " FROM " + sourceRID + " TO " + destRID; 162 | if (sqlsets.sqlsets !== "") { 163 | command = command.concat(" ", sqlsets.sqlsets); 164 | } 165 | 166 | self.command(command, function(err, results) { 167 | if (err) { return callback(err); } 168 | 169 | var edge = results[0]; 170 | 171 | pushRIDInto(edge["@rid"], sourceVertexOrRID, FIELD_OUT); 172 | pushRIDInto(edge["@rid"], destVertexOrRID, FIELD_IN); 173 | 174 | if (_.isEmpty(sqlsets.remainingHash)) { 175 | callback(null, edge); 176 | } else { 177 | _.extend(edge, sqlsets.remainingHash); 178 | self.save(edge, callback); 179 | } 180 | }); 181 | }; 182 | 183 | function getEdgesByDirection(self, sourceVertex, direction, label, callback) { 184 | if (_.isFunction(label)) { 185 | callback = label; 186 | label = undefined; 187 | } 188 | var edgesRids = sourceVertex[direction]; 189 | if (!edgesRids || edgesRids.length === 0) { 190 | return callback(null, []); 191 | } 192 | 193 | var edges = []; 194 | var loadedEdges = 0; 195 | var edgesRidsLength = edgesRids.length; 196 | for (var idx = 0; idx < edgesRidsLength; idx++) { 197 | self.loadRecord(edgesRids[idx], function(err, edge) { 198 | if (err) { return callback(err); } 199 | loadedEdges++; 200 | if (!label || label === edge.label) { 201 | edges.push(edge); 202 | } 203 | 204 | if (loadedEdges === edgesRidsLength) { 205 | return callback(null, edges); 206 | } 207 | }); 208 | } 209 | } 210 | 211 | GraphDb.prototype.getOutEdges = function(sourceVertex, label, callback) { 212 | getEdgesByDirection(this, sourceVertex, FIELD_OUT, label, callback); 213 | }; 214 | 215 | GraphDb.prototype.getInEdges = function(sourceVertex, label, callback) { 216 | getEdgesByDirection(this, sourceVertex, FIELD_IN, label, callback); 217 | }; 218 | 219 | GraphDb.prototype.getInVertex = function(sourceEdge, callback) { 220 | this.loadRecord(sourceEdge[FIELD_IN], callback); 221 | }; 222 | 223 | GraphDb.prototype.getOutVertex = function(sourceEdge, callback) { 224 | this.loadRecord(sourceEdge[FIELD_OUT], callback); 225 | }; 226 | 227 | function fieldOfRecords(records, field) { 228 | var rids = []; 229 | for (var idx = 0, length = records.length; idx < length; idx++) { 230 | rids.push(records[idx][field]); 231 | } 232 | return rids; 233 | } 234 | 235 | GraphDb.prototype.fromVertex = function(sourceVertex) { 236 | var self = this; 237 | 238 | return { 239 | inVertexes: function(label, callback) { 240 | if (_.isFunction(label)) { 241 | callback = label; 242 | label = undefined; 243 | } 244 | self.getInEdges(sourceVertex, label, function(err, edges) { 245 | if (err) { return callback(err); } 246 | 247 | self.loadRecords(fieldOfRecords(edges, FIELD_OUT), callback); 248 | }); 249 | }, 250 | outVertexes: function(label, callback) { 251 | if (_.isFunction(label)) { 252 | callback = label; 253 | label = undefined; 254 | } 255 | self.getOutEdges(sourceVertex, label, function(err, edges) { 256 | if (err) { return callback(err); } 257 | 258 | self.loadRecords(fieldOfRecords(edges, FIELD_IN), callback); 259 | }); 260 | } 261 | }; 262 | }; 263 | -------------------------------------------------------------------------------- /lib/orientdb/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | // Map all the classes 4 | var modules = [ 5 | 'connection/server', 6 | 'db', 7 | 'graphdb' 8 | ]; 9 | 10 | modules.forEach(function(path) { 11 | var module = require('./' + path); 12 | for (var idx in module) { 13 | exports[idx] = module[idx]; 14 | } 15 | }); 16 | 17 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name" : "orientdb", 3 | "description" : "A node.js driver for OrientDB", 4 | "keywords" : ["orientdb", "orient", "driver", "db", "node", "node.js"], 5 | "version" : "1.4.0-alpha.1", 6 | "author" : "Gabriel Petrovay ", 7 | "contributors" : [ 8 | { "name": "Ryan Fields", "email": "ryan.fields@twoleftbeats.com" }, 9 | { "name": "Federico Fissore", "email": "federico@fissore.org" }, 10 | { "name": "Navin Parray", "email": "navinparray@gmail.com" } 11 | ], 12 | "repository" : { 13 | "type" : "git", 14 | "url" : "http://github.com/gabipetrovay/node-orientdb.git" }, 15 | "dependencies" : { 16 | "lodash" : "*" 17 | }, 18 | "devDependencies" : { 19 | "tap" : "0.4", 20 | "async" : "*" 21 | }, 22 | "main" : "./lib/orientdb/index", 23 | "directories" : { "lib" : "./lib/orientdb" }, 24 | "engines" : { "node" : ">=0.6.0" }, 25 | "scripts" : { 26 | "test" : "node ./node_modules/tap/bin/tap.js ./test/errors/test_* ./test/parser/test_* ./test/test_*" 27 | }, 28 | "licenses" : [ { 29 | "type" : "Apache License, Version 2.0", 30 | "url" : "http://www.apache.org/licenses/LICENSE-2.0" 31 | } ] 32 | } 33 | 34 | -------------------------------------------------------------------------------- /test/disabled_freeze.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"), 2 | _ = require("lodash"); 3 | 4 | var orient = require("../lib/orientdb"), 5 | Db = orient.Db, 6 | Server = orient.Server; 7 | 8 | var serverConfig = require("../config/test/serverConfig"); 9 | var dbConfig = require("../config/test/dbConfig"); 10 | 11 | var server = new Server(serverConfig); 12 | var db = new Db("test_create_drop", server, dbConfig); 13 | 14 | 15 | server.connect(function(err, sessionId) { 16 | 17 | assert(!err, "Error while connecting to the server: " + err); 18 | 19 | db.create(function(err) { 20 | 21 | assert(!err, "Error while creating db: " + err); 22 | 23 | var databaseName = db.databaseName; 24 | console.log("Created database: " + databaseName); 25 | 26 | server.freeze(databaseName, function(err) { 27 | 28 | assert(!err, err); 29 | 30 | console.log("Freezed database " + db.databaseName); 31 | 32 | server.disconnect(function(err) { 33 | 34 | db.open(function(err) { 35 | 36 | assert(!err, err); 37 | 38 | assert(_.isNull(db.getClusterByName("new_user"))); 39 | assert(_.isNull(db.getClassByName("new_user"))); 40 | 41 | var user = { 42 | "@class": "new_user", 43 | name: "a user name" 44 | }; 45 | db.save(user, function(err) { 46 | 47 | assert(err); 48 | 49 | db.query("select from new_user", function(err, results) { 50 | assert(!err, err); 51 | 52 | console.log(arguments); 53 | 54 | db.close(function(err) { 55 | assert(!err, err); 56 | 57 | server.connect(function(err) { 58 | assert(!err, err); 59 | 60 | server.release(databaseName, function(err) { 61 | assert(!err, err); 62 | 63 | server.disconnect(function(err) { 64 | assert(!err, err); 65 | 66 | db.open(function(err) { 67 | assert(!err, err); 68 | 69 | assert(!_.isNull(db.getClusterByName("new_user"))); 70 | //assert(!_.isNull(db.getClassByName("new_user"))); 71 | 72 | db.save(user, function(err) { 73 | assert(!err, err); 74 | 75 | db.query("select from new_user", function(err) { 76 | assert(!err, err); 77 | 78 | console.log(arguments); 79 | 80 | }); 81 | }); 82 | }); 83 | }); 84 | }); 85 | }); 86 | }); 87 | }); 88 | }); 89 | }); 90 | }); 91 | }); 92 | }); 93 | }); 94 | -------------------------------------------------------------------------------- /test/errors/test_db_open_failure.js: -------------------------------------------------------------------------------- 1 | var root = "../../", 2 | assert = require("assert"), 3 | orient = require(root + "lib/orientdb"), 4 | Db = orient.Db, 5 | Server = orient.Server, 6 | serverConfig = require(root + "config/test/serverConfig"), 7 | dbConfig = require(root + "config/test/dbConfig"), 8 | server = new Server(serverConfig), 9 | db = new Db("ah463qar;wh5", server, dbConfig); 10 | 11 | db.open(function(err) { 12 | assert(err, "An error should have occurred since the database should not exist."); 13 | 14 | db.close(); 15 | }); 16 | -------------------------------------------------------------------------------- /test/parser/test_configuration.js: -------------------------------------------------------------------------------- 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].path); 38 | 39 | configAsString = "3| |#2:0| |#0:1|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|"; 40 | config = parser.parseConfiguration(configAsString); 41 | 42 | assert.equal(3, config.version); 43 | assert.equal(null, config.name); 44 | assert.equal("#2:0", config.schemaRecordId); 45 | assert.equal(null, config.dictionaryRecordId); 46 | assert.equal("#0:1", config.indexMgrRecordId); 47 | assert.equal("it", config.localeLanguage); 48 | assert.equal("IT", config.localeCountry); 49 | assert.equal("yyyy-MM-dd", config.dateFormat); 50 | assert.equal("yyyy-MM-dd HH:mm:ss", config.dateTimeFormat); 51 | assert.equal("0", config.fileTemplate.maxSize); 52 | assert.equal("mmap", config.fileTemplate.fileType); 53 | assert.equal("500Kb", config.fileTemplate.fileStartSize); 54 | assert.equal("500Mb", config.fileTemplate.fileMaxSize); 55 | assert.equal("50%", config.fileTemplate.fileIncrementSize); 56 | assert.equal("auto", config.fileTemplate.defrag); 57 | assert.equal(0, config.fileTemplate.infoFiles); 58 | assert.equal(0, config.clusters.length); 59 | assert.equal(0, config.dataSegments.length); 60 | assert.equal(null, config.txSegment.path); 61 | assert.equal("mmap", config.txSegment.type); 62 | assert.equal("512mb", config.txSegment.maxSize); 63 | assert.equal(false, config.txSegment.synchRecord); 64 | assert.equal(true, config.txSegment.synchTx); 65 | assert.equal(0, config.properties.length); 66 | 67 | configAsString = "3| |#2:0| |#0:1|it|IT|yyyy-MM-dd|yyyy-MM-dd HH:mm:ss| |0|mmap|500Kb|500Mb|50%|auto|0|6|0|internal|0|p| |0|mmap|1Mb|500Mb|50%|auto|1|${STORAGE_PATH}/internal.0.ocl|mmap|500Mb|${STORAGE_PATH}/internal.och|mmap|500Mb|1|index|0|p| |0|mmap|1Mb|500Mb|50%|auto|1|${STORAGE_PATH}/index.0.ocl|mmap|500Mb|${STORAGE_PATH}/index.och|mmap|500Mb|2|default|0|p| |0|mmap|1Mb|500Mb|50%|auto|1|${STORAGE_PATH}/default.0.ocl|mmap|500Mb|${STORAGE_PATH}/default.och|mmap|500Mb|3|orole|0|p| |0|mmap|1Mb|500Mb|50%|auto|1|${STORAGE_PATH}/orole.0.ocl|mmap|500Mb|${STORAGE_PATH}/orole.och|mmap|500Mb|4|ouser|0|p| |0|mmap|1Mb|500Mb|50%|auto|1|${STORAGE_PATH}/ouser.0.ocl|mmap|500Mb|${STORAGE_PATH}/ouser.och|mmap|500Mb|5|orids|0|p| |0|mmap|1Mb|500Mb|50%|auto|1|${STORAGE_PATH}/orids.0.ocl|mmap|500Mb|${STORAGE_PATH}/orids.och|mmap|500Mb|1|0|default| |0|mmap|1Mb|500Mb|100%|auto|1|${STORAGE_PATH}/default.0.oda|mmap|500Mb|/home/federico/materiale/works_My/orientdb-graphed-1.0/databases/presentz/default.odh|mmap|0|${STORAGE_PATH}/txlog.otx|mmap|512mb|false|true|0|"; 68 | config = parser.parseConfiguration(configAsString); 69 | assert.equal(3, config.version); 70 | assert.equal(6, config.clusters.length); 71 | assert.equal(5, config.clusters[5].clusterId); 72 | assert.equal("${STORAGE_PATH}/internal.0.ocl", config.clusters[0].fileTemplate.infoFiles[0].path); 73 | 74 | configAsString = "4| |#0:1| |#0:2|it|IT|yyyy-MM-dd|yyyy-MM-dd HH:mm:ss|UTC|UTF-8| |0|mmap|500Kb|500Mb|50%|auto|0|0|0| |mmap|512mb|false|true|0|"; 75 | config = parser.parseConfiguration(configAsString); 76 | assert.equal("UTC", config.timeZone); 77 | assert.equal("UTF-8", config.charset); 78 | 79 | configAsString = "5| |#0:1| |#0:2|it|IT|yyyy-MM-dd|yyyy-MM-dd HH:mm:ss|UTC|UTF-8| |0|mmap|500Kb|500Mb|50%|auto|0|9|0|internal|0|p| |0|mmap|1Mb|500Mb|50%|auto|1|${STORAGE_PATH}/internal.0.ocl|mmap|500Mb|f|${STORAGE_PATH}/internal.och|mmap|500Mb|1|index|0|p| |0|mmap|1Mb|500Mb|50%|auto|1|${STORAGE_PATH}/index.0.ocl|mmap|500Mb|f|${STORAGE_PATH}/index.och|mmap|500Mb|2|manindex|0|p| |0|mmap|1Mb|500Mb|50%|auto|1|${STORAGE_PATH}/manindex.0.ocl|mmap|500Mb|f|${STORAGE_PATH}/manindex.och|mmap|500Mb|3|default|0|p| |0|mmap|1Mb|500Mb|50%|auto|1|${STORAGE_PATH}/default.0.ocl|mmap|500Mb|f|${STORAGE_PATH}/default.och|mmap|500Mb|4|orole|0|p| |0|mmap|1Mb|500Mb|50%|auto|1|${STORAGE_PATH}/orole.0.ocl|mmap|500Mb|f|${STORAGE_PATH}/orole.och|mmap|500Mb|5|ouser|0|p| |0|mmap|1Mb|500Mb|50%|auto|1|${STORAGE_PATH}/ouser.0.ocl|mmap|500Mb|f|${STORAGE_PATH}/ouser.och|mmap|500Mb|6|ofunction|0|p| |0|mmap|1Mb|500Mb|50%|auto|1|${STORAGE_PATH}/ofunction.0.ocl|mmap|500Mb|f|${STORAGE_PATH}/ofunction.och|mmap|500Mb|7|oschedule|0|p| |0|mmap|1Mb|500Mb|50%|auto|1|${STORAGE_PATH}/oschedule.0.ocl|mmap|500Mb|f|${STORAGE_PATH}/oschedule.och|mmap|500Mb|8|orids|0|p| |0|mmap|1Mb|500Mb|50%|auto|1|${STORAGE_PATH}/orids.0.ocl|mmap|500Mb|f|${STORAGE_PATH}/orids.och|mmap|500Mb|1|0|default| |0|mmap|1Mb|500Mb|100%|auto|1|${STORAGE_PATH}/default.0.oda|mmap|500Mb|/home/federico/materiale/works_My/orientdb-graphed-1.4.0-SNAPSHOT/databases/test_create_drop/default.odh|mmap|0|${STORAGE_PATH}/txlog.otx|mmap|512mb|false|true|0|"; 80 | config = parser.parseConfiguration(configAsString); 81 | assert.equal(1, config.dataSegments.length); 82 | assert.equal(0, config.dataSegments[0].dataId); 83 | assert.equal("${STORAGE_PATH}/txlog.otx", config.txSegment.path); 84 | assert.equal("mmap", config.txSegment.type); 85 | assert.equal("512mb", config.txSegment.maxSize); 86 | assert.equal(false, config.txSegment.synchRecord); 87 | assert.equal(true, config.txSegment.synchTx); -------------------------------------------------------------------------------- /test/parser/test_deep_clone.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var parser = require("../../lib/orientdb/connection/parser"); 3 | 4 | var obj = { 5 | name: "obj", 6 | sub_obj: { 7 | name: "sub_obj", 8 | list: [ 9 | [10], 10 | [20], 11 | [30] 12 | ] 13 | }, 14 | list: [1, 2, 3], 15 | date: new Date() 16 | }; 17 | 18 | var newObj = parser.deepClone(obj); 19 | 20 | newObj.sub_obj.name = "new_sub_obj"; 21 | newObj.sub_obj.list[0].push(15); 22 | newObj.list.push(4); 23 | newObj.date.setHours(23); 24 | newObj.date.setMinutes(59); 25 | newObj.date.setSeconds(59); 26 | newObj.date.setMilliseconds(999); 27 | 28 | assert(newObj.sub_obj.name !== obj.sub_obj.name); 29 | assert(newObj.sub_obj.list[0].length > obj.sub_obj.list[0].length); 30 | assert(newObj.list.length > obj.list.length); 31 | assert(newObj.date.getTime() !== obj.date.getTime()); -------------------------------------------------------------------------------- /test/parser/test_doc_serialize_deserialize.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var parser = require("../../lib/orientdb/connection/parser"); 3 | 4 | var document = { 5 | "@class": "FantasyPerson", 6 | "@type": "d", 7 | name: "it's me, \\ \"", 8 | birthday: new Date(Math.floor(new Date().getTime() / 1000) * 1000), 9 | fingers: 20, 10 | a_float: 123.4, 11 | like_it: true, 12 | linked_to: "#1:1", 13 | last_time_in: { "@type": "d", name: "Turin, Italy", when: new Date(Math.floor(new Date().getTime() / 1000) * 1000) }, 14 | cities: [ 15 | { "@type": "d", name: "Turin, Italy" } 16 | ], 17 | known_os_list: [ "linux" ], 18 | zero_is: null, 19 | value_with_hash: "#NoSql", 20 | embedded_map: { 21 | key: "value" 22 | } 23 | }; 24 | 25 | var serialized_document = "FantasyPerson@name:\"it's me, \\\\ \\\"\",birthday:" + document.birthday.getTime() + "t,fingers:20,a_float:123.4f,like_it:true,linked_to:#1:1,last_time_in:(name:\"Turin, Italy\",when:" + document.last_time_in.when.getTime() + "t),cities:[(name:\"Turin, Italy\")],known_os_list:[\"linux\"],zero_is:,value_with_hash:\"#NoSql\",embedded_map:{\"key\":\"value\"}"; 26 | 27 | assert.equal(serialized_document, parser.serializeDocument(document)); 28 | assert.equal(JSON.stringify(document), JSON.stringify(parser.deserializeDocument(serialized_document))); 29 | 30 | serialized_document = "schemaVersion:4,classes:[(name:\"OUser\",shortName:,defaultClusterId:4,clusterIds:[4],overSize:0.0f,strictMode:false,properties:[(name:\"password\",type:7,mandatory:true,notNull:true,min:,max:,regexp:,linkedClass:,linkedType:),(name:\"name\",type:7,mandatory:true,notNull:true,min:,max:,regexp:,linkedClass:,linkedType:),(name:\"roles\",type:15,mandatory:false,notNull:false,min:,max:,regexp:,linkedClass:\"ORole\",linkedType:)]),(name:\"ORole\",shortName:,defaultClusterId:3,clusterIds:[3],overSize:0.0f,strictMode:false,properties:[(name:\"mode\",type:17,mandatory:false,notNull:false,min:,max:,regexp:,linkedClass:,linkedType:),(name:\"rules\",type:12,mandatory:false,notNull:false,min:,max:,regexp:,linkedClass:,linkedType:17),(name:\"name\",type:7,mandatory:true,notNull:true,min:,max:,regexp:,linkedClass:,linkedType:)])]"; 31 | assert.equal(serialized_document.length, parser.serializeDocument(parser.deserializeDocument(serialized_document)).length + 6); //this is because the db is returning 0.0f that javascript parseFloat trasform to a simple 0 (integer), so we are missing tailing ".0f" (3 chars) for 2 times 32 | 33 | serialized_document = "EUsesInstanceOf@out:#8:0,in:#18:1,html:{\"path\":\"html/layout\"},config:{\"title\":\"Github Admin\",\"modules\":(githubDisplay:\"github_display\")},complex:(simple1:\"string1\",one_level1:(simple2:\"string2\"),two_levels:(simple3:\"string3\",one_level2:(simple4:\"string4\")))"; 34 | assert.equal(serialized_document, parser.serializeDocument(parser.deserializeDocument(serialized_document))); 35 | 36 | // we loose sets when serializing/deserializing 37 | serialized_document = "DocWithSets@a_set:<\"one\",\"two\",3>"; 38 | assert.equal("DocWithSets@a_set:[\"one\",\"two\",3]", parser.serializeDocument(parser.deserializeDocument(serialized_document))); 39 | 40 | serialized_document = "V@"; 41 | assert.equal("V", parser.deserializeDocument(serialized_document)["@class"]); -------------------------------------------------------------------------------- /test/parser/test_hash_to_sql_sets_0.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"), 2 | parser = require("../../lib/orientdb/connection/parser"); 3 | 4 | var sqlsets = parser.hashToSQLSets({}); 5 | 6 | assert.equal("", sqlsets.sqlsets); 7 | assert.deepEqual({}, sqlsets.remainingHash); 8 | 9 | -------------------------------------------------------------------------------- /test/parser/test_hash_to_sql_sets_1.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"), 2 | parser = require("../../lib/orientdb/connection/parser"); 3 | 4 | var hash = { 5 | name: "federico", 6 | "@class": "V", 7 | when: new Date(1347608670283), 8 | bool: true, 9 | escape: "\"ciao\"", 10 | embed: { 11 | key: "value" 12 | } 13 | }; 14 | 15 | var expectedResult = "SET name = \"federico\", @class = \"V\", when = date(\"2012-09-14T07:44:30.283Z\", \"yyyy-MM-dd\'T\'HH:mm:ss.SSS'Z'\"), bool = true, escape = \"\\\"ciao\\\"\""; 16 | var expectedRemainigHash = { 17 | embed: { 18 | key: "value" 19 | } 20 | }; 21 | 22 | var sqlsets = parser.hashToSQLSets(hash); 23 | assert.equal(expectedResult, sqlsets.sqlsets); 24 | assert.deepEqual(expectedRemainigHash, sqlsets.remainingHash); 25 | -------------------------------------------------------------------------------- /test/parser/test_hash_to_sql_sets_2.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"), 2 | parser = require("../../lib/orientdb/connection/parser"); 3 | 4 | var hash = { config: { modules: { mod2: 'modval2' } } }; 5 | 6 | var expectedResult = ""; 7 | var expectedRemainigHash = hash; 8 | 9 | var sqlsets = parser.hashToSQLSets(hash); 10 | assert.equal(expectedResult, sqlsets.sqlsets); 11 | 12 | assert.deepEqual(expectedRemainigHash, sqlsets.remainingHash); 13 | 14 | -------------------------------------------------------------------------------- /test/parser/test_long_conversion.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"), 2 | parser = require("../../lib/orientdb/connection/parser"); 3 | 4 | function assertBufferContainsLong(long, buf) { 5 | for (var longIdx = 0, bufIdx = 0; longIdx < long.length; longIdx += 2, bufIdx++) { 6 | if (parseInt(long[longIdx] + long[longIdx + 1], 16) !== buf[bufIdx]) { 7 | console.log("Expected - Found"); 8 | console.log(long); 9 | console.log(buf); 10 | throw new Error(); 11 | } 12 | } 13 | } 14 | 15 | var buf; 16 | 17 | buf = parser.writeLong(0); 18 | assertBufferContainsLong("0000000000000000", buf); 19 | 20 | buf = parser.writeLong(200); 21 | assertBufferContainsLong("00000000000000c8", buf); 22 | 23 | buf = parser.writeLong(4294967295); 24 | assertBufferContainsLong("00000000ffffffff", buf); 25 | 26 | buf = parser.writeLong(8589934590); 27 | assertBufferContainsLong("00000001fffffffe", buf); 28 | 29 | buf = parser.writeLong(-200); 30 | assertBufferContainsLong("ffffffffffffff38", buf); 31 | 32 | buf = parser.writeLong(-1); 33 | assertBufferContainsLong("ffffffffffffffff", buf); 34 | 35 | buf = parser.writeLong(-4294967295); 36 | assertBufferContainsLong("ffffffff00000001", buf); 37 | 38 | buf = parser.writeLong(-8589934590); 39 | assertBufferContainsLong("fffffffe00000002", buf); 40 | 41 | assert.equal(0, parser.readLong(parser.writeLong(0), 0)); 42 | assert.equal(200, parser.readLong(parser.writeLong(200), 0)); 43 | assert.equal(4294967295, parser.readLong(parser.writeLong(4294967295), 0)); 44 | assert.equal(8589934590, parser.readLong(parser.writeLong(8589934590), 0)); 45 | assert.equal(-200, parser.readLong(parser.writeLong(-200), 0)); 46 | assert.equal(-1, parser.readLong(parser.writeLong(-1), 0)); 47 | assert.equal(-4294967295, parser.readLong(parser.writeLong(-4294967295), 0)); 48 | assert.equal(-8589934590, parser.readLong(parser.writeLong(-8589934590), 0)); -------------------------------------------------------------------------------- /test/parser/test_writeString.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"), 2 | parser = require("../../lib/orientdb/connection/parser"); 3 | 4 | var string = "This is a test string (èç€) with some special chars"; 5 | var buf = parser.writeString(string); 6 | 7 | assert.equal(parser.readString(buf, 0).value, string); 8 | assert.equal(parser.readString(buf, 0).lengthInBytes, string.length + encodeURIComponent(string).match(/%[89ABab]/g).length); 9 | 10 | -------------------------------------------------------------------------------- /test/setup_db.js: -------------------------------------------------------------------------------- 1 | var orient = require("../lib/orientdb"), 2 | Db = orient.Db, 3 | Server = orient.Server; 4 | 5 | var serverConfig = require("../config/test/serverConfig"); 6 | var dbConfig = require("../config/test/dbConfig"); 7 | 8 | var server = new Server(serverConfig); 9 | var db = new Db("temp", server, dbConfig); 10 | 11 | exports.db = db; 12 | exports.server = server; -------------------------------------------------------------------------------- /test/test_cascading_save.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var _ = require("lodash"); 3 | 4 | var db = require("./setup_db.js").db; 5 | 6 | db.open(function(err) { 7 | 8 | assert(!err, "Error while opening the database: " + err); 9 | 10 | var doc = { 11 | "@class": "mainClass", 12 | field: "field value", 13 | embeddded_list_of_maps: [ 14 | { 15 | key1: "value1", 16 | key2: "value2" 17 | } 18 | ], 19 | sub_document: { 20 | "@class": "subClass", 21 | sub_field: 1, 22 | "@type": "d", 23 | sub_sub_document: { 24 | "@class": "subClass", 25 | sub_field: 50, 26 | "@type": "d" 27 | } 28 | }, 29 | sub_documents: [ 30 | { 31 | "@class": "subClass", 32 | sub_field: 2, 33 | "@type": "d", 34 | sub_sub_document: { 35 | "@class": "subClass", 36 | sub_field: 99, 37 | "@type": "d" 38 | } 39 | }, 40 | { 41 | "@class": "subClass", 42 | sub_field: 3, 43 | "@type": "d" 44 | } 45 | ], 46 | linked_map: { 47 | link1: { 48 | "@class": "subClass", 49 | another_field: "another_value", 50 | "@type": "d" 51 | } 52 | }, 53 | "@type": "d" 54 | }; 55 | 56 | prepareDatabase(function(err) { 57 | assert(!err, err); 58 | 59 | db.cascadingSave(doc, function(err, savedDoc) { 60 | assert(!err, err); 61 | assert(!_.isUndefined(savedDoc["@rid"])); 62 | assert(!_.isUndefined(savedDoc["@type"])); 63 | assert(!_.isUndefined(savedDoc["@class"])); 64 | assert(!_.isUndefined(savedDoc["@version"])); 65 | assert(!_.isUndefined(savedDoc.sub_document["@rid"])); 66 | assert(!_.isUndefined(savedDoc.sub_document["@type"])); 67 | assert(!_.isUndefined(savedDoc.sub_document["@class"])); 68 | assert(!_.isUndefined(savedDoc.sub_document["@version"])); 69 | assert(!_.isUndefined(savedDoc.sub_document.sub_sub_document["@rid"])); 70 | assert(!_.isUndefined(savedDoc.sub_document.sub_sub_document["@type"])); 71 | assert(!_.isUndefined(savedDoc.sub_document.sub_sub_document["@class"])); 72 | assert(!_.isUndefined(savedDoc.sub_document.sub_sub_document["@version"])); 73 | assert(!_.isUndefined(savedDoc.sub_documents[0]["@rid"])); 74 | assert(!_.isUndefined(savedDoc.sub_documents[0]["@type"])); 75 | assert(!_.isUndefined(savedDoc.sub_documents[0]["@class"])); 76 | assert(!_.isUndefined(savedDoc.sub_documents[0]["@version"])); 77 | assert(!_.isUndefined(savedDoc.sub_documents[0].sub_sub_document["@rid"])); 78 | assert(!_.isUndefined(savedDoc.sub_documents[0].sub_sub_document["@type"])); 79 | assert(!_.isUndefined(savedDoc.sub_documents[0].sub_sub_document["@class"])); 80 | assert(!_.isUndefined(savedDoc.sub_documents[0].sub_sub_document["@version"])); 81 | assert(!_.isUndefined(savedDoc.sub_documents[1]["@rid"])); 82 | assert(!_.isUndefined(savedDoc.sub_documents[1]["@type"])); 83 | assert(!_.isUndefined(savedDoc.sub_documents[1]["@class"])); 84 | assert(!_.isUndefined(savedDoc.sub_documents[1]["@version"])); 85 | assert(!_.isUndefined(savedDoc.linked_map.link1["@rid"])); 86 | assert(!_.isUndefined(savedDoc.linked_map.link1["@type"])); 87 | assert(!_.isUndefined(savedDoc.linked_map.link1["@class"])); 88 | assert(!_.isUndefined(savedDoc.linked_map.link1["@version"])); 89 | 90 | unprepareDatabase(function(err) { 91 | assert(!err, err); 92 | 93 | db.close(); 94 | }); 95 | }); 96 | }); 97 | }); 98 | 99 | function prepareDatabase(callback) { 100 | db.createClass("mainClass", function(err) { 101 | if (err) return callback(err); 102 | 103 | db.createClass("subClass", function(err) { 104 | if (err) return callback(err); 105 | 106 | db.command("CREATE PROPERTY mainClass.sub_document link subClass", function(err) { 107 | if (err) return callback(err); 108 | 109 | db.command("CREATE PROPERTY mainClass.sub_documents linklist subClass", function(err) { 110 | if (err) return callback(err); 111 | 112 | 113 | db.command("CREATE PROPERTY mainClass.linked_map linkmap subClass", function(err) { 114 | if (err) return callback(err); 115 | 116 | callback(); 117 | }); 118 | }); 119 | }); 120 | }); 121 | }); 122 | } 123 | 124 | 125 | function unprepareDatabase(callback) { 126 | db.dropClass("subClass", function(err) { 127 | if (err) return callback(err); 128 | 129 | db.dropClass("mainClass", callback); 130 | }); 131 | } -------------------------------------------------------------------------------- /test/test_command_collection_result.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var _ = require("lodash"); 3 | 4 | var db = require("./setup_db.js").db; 5 | 6 | db.open(function(err, result) { 7 | 8 | assert(!err, "Error while opening the database: " + err); 9 | 10 | db.command("SELECT FROM OUser", function(err, results) { 11 | 12 | assert(!err, "Error while executing a SELECT command: " + err); 13 | 14 | assert.equal(results.length, 3, "Weren't there 3 users in this database?"); 15 | 16 | for (var idx = 0, length = results.length; idx < length; idx++) { 17 | assert(_.isString(results[idx]["@rid"])); 18 | } 19 | 20 | console.log("Received results: " + JSON.stringify(results)); 21 | 22 | db.close(); 23 | }); 24 | }); 25 | 26 | -------------------------------------------------------------------------------- /test/test_command_collection_result_after_reopen.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var _ = require("lodash"); 3 | 4 | var db = require("./setup_db.js").db; 5 | 6 | db.open(function(err, result) { 7 | 8 | assert(!err, "Error while opening the database: " + err); 9 | 10 | db.command("SELECT FROM OUser", function(err, results) { 11 | 12 | assert(!err, "Error while executing a SELECT command: " + err); 13 | 14 | db.close(function(err) { 15 | 16 | assert(!err, "Error while closing the database: " + err); 17 | 18 | db.open(function(err, result) { 19 | 20 | assert(!err, "Error while opening the database: " + err); 21 | 22 | db.command("SELECT FROM OUser", function(err, results) { 23 | 24 | assert(!err, "Error while executing a SELECT command: " + err); 25 | 26 | assert.equal(results.length, 3, "Weren't there 3 users in this database?"); 27 | 28 | db.close(); 29 | }); 30 | }); 31 | 32 | }) 33 | }); 34 | }); 35 | 36 | -------------------------------------------------------------------------------- /test/test_command_collection_result_object.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var _ = require("lodash"); 3 | 4 | var db = require("./setup_db.js").db; 5 | 6 | var className = "TestObjectRecord", 7 | obj = { "one": 1 }; 8 | 9 | db.open(function(err, result) { 10 | 11 | assert(!err, "Error while opening the database: " + err); 12 | 13 | db.command("CREATE CLASS " + className, function(err, results) { 14 | 15 | assert(!err, "Error while executing an INSERT command: " + err); 16 | assert.equal(results.length, 1, "The new class ID should be returned."); 17 | 18 | console.log("Created class " + className + " with ID: " + results[0]); 19 | 20 | db.command("INSERT INTO " + className + " (obj) values (" + JSON.stringify(obj) + ")", function(err, results) { 21 | 22 | assert(!err, "Error while executing an INSERT command: " + err); 23 | assert.equal(results.length, 1, "The inserted user should be returned."); 24 | 25 | assert.equal(JSON.stringify(results[0].obj), JSON.stringify(obj), "The inserted object is somehow different from the database result"); 26 | 27 | console.log("Inserted object value: " + JSON.stringify(obj)); 28 | 29 | db.command("SELECT FROM " + className, function(err, results) { 30 | 31 | assert(!err, "Error while executing a SELECT command: " + err); 32 | assert.equal(results.length, 1, "Where is the inserted record?"); 33 | 34 | assert.equal(JSON.stringify(results[0].obj), JSON.stringify(obj), "Does the database parse object values?"); 35 | 36 | console.log("Retrieved object value: " + JSON.stringify(results[0].obj)); 37 | db.close(); 38 | }); 39 | }); 40 | }); 41 | }); 42 | 43 | -------------------------------------------------------------------------------- /test/test_command_flat_result.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var _ = require("lodash"); 3 | 4 | var db = require("./setup_db.js").db; 5 | 6 | var className = "TestFlatResult"; 7 | 8 | db.open(function(err, result) { 9 | 10 | assert(!err, "Error while opening the database: " + err); 11 | 12 | var classCount = db.classes.length; 13 | console.log("The databse has " + classCount + " classes."); 14 | 15 | db.command("CREATE CLASS " + className, function(err, results) { 16 | 17 | if (err) { 18 | assert(!err, "Error while executing a CREATE CLASS command: " + (err.message || JSON.stringify(err))); 19 | } 20 | 21 | assert.equal(results.length, 1, "The new class count should be returned."); 22 | assert.equal(classCount, results[0] - 1, "The class count should have been incremented."); 23 | // TODO check issue: https://github.com/gabipetrovay/node-orientdb/issues/81 24 | //assert.equal(classCount, db.classes.length - 1, "The Db object classes has not been updated."); 25 | 26 | console.log("Created class " + className); 27 | console.log("The database has now " + results[0] + " classes."); 28 | 29 | db.command("DROP CLASS " + className, function(err, results) { 30 | 31 | if (err) { 32 | assert(!err, "Error while executing a DROP CLASS command: " + (err.message || JSON.stringify(err))); 33 | } 34 | 35 | assert.equal(results.length, 1, "The result should contain the boolean status of the DROP operation."); 36 | // TODO check issue: https://github.com/gabipetrovay/node-orientdb/issues/81 37 | //assert.equal(classCount, db.classes.length - 1, "The Db object classes has not been updated."); 38 | 39 | console.log("Dropped class " + className); 40 | 41 | db.close(); 42 | }); 43 | }); 44 | }); 45 | 46 | -------------------------------------------------------------------------------- /test/test_command_query_alias.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var _ = require("lodash"); 3 | 4 | var db = require("./setup_db.js").db; 5 | 6 | db.open(function(err, result) { 7 | 8 | assert(!err, "Error while opening the database: " + err); 9 | 10 | db.query("SELECT FROM OUser", function(err, results) { 11 | 12 | assert(!err, "Error while executing a SELECT command: " + err); 13 | 14 | assert.equal(results.length, 3, "Weren't there 3 users in this database?"); 15 | 16 | db.close(); 17 | }); 18 | }); 19 | 20 | -------------------------------------------------------------------------------- /test/test_command_query_params.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var _ = require("lodash"); 3 | 4 | var db = require("./setup_db.js").db; 5 | 6 | db.open(function(err, result) { 7 | 8 | assert(!err, "Error while opening the database: " + err); 9 | 10 | db.query("SELECT FROM OUser where name = :name", { params: { name: "admin" } }, function(err, results) { 11 | 12 | assert(!err, "Error while executing a SELECT command: " + err); 13 | 14 | assert.equal(results.length, 1); 15 | 16 | var admin = results[0]; 17 | assert(_.isString(admin["@rid"])); 18 | 19 | console.log("Received results: " + JSON.stringify(results)); 20 | 21 | db.query("SELECT FROM OUser where name = :name", { fetchPlan: "*:-1", params: { name: "admin" } }, function(err, results) { 22 | 23 | assert(!err, "Error while executing a SELECT command: " + err); 24 | 25 | assert.equal(results.length, 1); 26 | 27 | var admin = results[0]; 28 | assert(_.isString(admin["@rid"])); 29 | 30 | console.log("Received results: " + JSON.stringify(results)); 31 | 32 | db.query("SELECT FROM OUser where name = ?", { params: [ "admin" ] }, function(err, results) { 33 | 34 | assert(!err, "Error while executing a SELECT command: " + err); 35 | 36 | assert.equal(results.length, 1); 37 | 38 | var admin = results[0]; 39 | assert(_.isString(admin["@rid"])); 40 | 41 | console.log("Received results: " + JSON.stringify(results)); 42 | 43 | db.query("SELECT FROM OUser where name = ?", { fetchPlan: "*:-1", params: [ "admin" ] }, function(err, results) { 44 | 45 | assert(!err, "Error while executing a SELECT command: " + err); 46 | 47 | assert.equal(results.length, 1); 48 | 49 | var admin = results[0]; 50 | assert(_.isString(admin["@rid"])); 51 | 52 | console.log("Received results: " + JSON.stringify(results)); 53 | 54 | db.close(); 55 | }); 56 | }); 57 | }); 58 | }); 59 | }); 60 | 61 | -------------------------------------------------------------------------------- /test/test_command_record_result.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var _ = require("lodash"); 3 | 4 | var db = require("./setup_db.js").db; 5 | 6 | var name = "guest"; 7 | 8 | db.open(function(err, result) { 9 | 10 | assert(!err, "Error while opening the database: " + err); 11 | 12 | db.command("INSERT INTO OUser (name, password, status) values (\"" + name + "\", \"\", \"ACTIVE\")", function(err, results) { 13 | 14 | assert(!err, "Error while executing an INSERT command: " + err); 15 | assert.equal(results.length, 1, "The inserted user should be returned."); 16 | 17 | var user = results[0]; 18 | 19 | assert.equal(user.name, name); 20 | 21 | console.log("Inserted record: " + JSON.stringify(user)); 22 | 23 | db.command("DELETE FROM OUser WHERE name = \"" + name + "\"", function(err, results) { 24 | 25 | assert(!err, "Error while executing an DELETE command: " + err); 26 | assert.equal(results.length, 1, "The result should contain the number of deleted records."); 27 | 28 | var num = results[0]; 29 | 30 | console.log("Deleted " + num + " record" + (num === "1" ? "" : "s")); 31 | 32 | db.close(); 33 | }); 34 | }); 35 | }); 36 | 37 | -------------------------------------------------------------------------------- /test/test_command_select_from_index.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var _ = require("lodash"); 3 | 4 | var db = require("./setup_db.js").db; 5 | 6 | db.open(function(err) { 7 | 8 | assert(!err, "Error while opening the database: " + err); 9 | 10 | prepareDatabase(function(err) { 11 | assert(!err, err); 12 | 13 | db.save({name: "john", surname: "doe", "@class": "ClassWithIndex" }, function(err) { 14 | assert(!err, err); 15 | 16 | db.command("SELECT FROM index:ClassWithIndex.surname where key = 'non_existent'", function(err, results) { 17 | assert(!err, err); 18 | 19 | assert.equal(0, results.length); 20 | 21 | db.command("SELECT FROM index:ClassWithIndex.surname where key = 'doe'", function(err, results) { 22 | assert(!err, err); 23 | 24 | assert.equal(1, results.length); 25 | 26 | db.save({name: "john", surname: "doe", "@class": "ClassWithIndex" }, function(err) { 27 | assert(err, "unique index violeted?!?"); 28 | 29 | db.save({name: "john m", surname: "smith", "@class": "ClassWithIndex" }, function(err) { 30 | assert(!err, err); 31 | 32 | db.command("SELECT FROM index:ClassWithIndex.name where key containstext 'john'", { fetchPlan: "*:-1" }, function(err, results) { 33 | assert(!err, err); 34 | 35 | assert.equal(2, results.length); 36 | 37 | assert.equal("john", results[0].rid.name); 38 | assert.equal("doe", results[0].rid.surname); 39 | assert.equal("john m", results[1].rid.name); 40 | assert.equal("smith", results[1].rid.surname); 41 | 42 | db.command("DELETE FROM ClassWithIndex", function(err) { 43 | assert(!err, err); 44 | 45 | db.dropClass("ClassWithIndex", function(err) { 46 | assert(!err, err); 47 | 48 | db.close(); 49 | }); 50 | }); 51 | }); 52 | }); 53 | }); 54 | }); 55 | }); 56 | }); 57 | }); 58 | }); 59 | 60 | function prepareDatabase(callback) { 61 | db.createClass("ClassWithIndex", function(err) { 62 | if (err) return callback(err); 63 | 64 | db.command("CREATE PROPERTY ClassWithIndex.name STRING", function(err) { 65 | if (err) return callback(err); 66 | 67 | db.command("CREATE INDEX ClassWithIndex.name FULLTEXT", function(err) { 68 | if (err) return callback(err); 69 | 70 | db.command("CREATE PROPERTY ClassWithIndex.surname STRING", function(err) { 71 | if (err) return callback(err); 72 | 73 | db.command("CREATE INDEX ClassWithIndex.surname UNIQUE", function(err) { 74 | if (err) return callback(err); 75 | 76 | callback(); 77 | }); 78 | }); 79 | }); 80 | }); 81 | }); 82 | } -------------------------------------------------------------------------------- /test/test_command_very_long_result.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var _ = require("lodash"); 3 | 4 | var db = require("./setup_db.js").db; 5 | 6 | var LONG_STRING_LENGTH = 100000; 7 | 8 | 9 | db.open(function(err, result) { 10 | 11 | assert(!err, "Error while opening the database: " + err); 12 | 13 | // create one class for this test 14 | db.command("CREATE CLASS TestLongValues", function(err, results) { 15 | 16 | assert(!err, "Error while executing a CREATE CLASS command: " + JSON.stringify(err)); 17 | assert.equal(results.length, 1, "The ID of the created class should be returned."); 18 | 19 | console.log("Class TestLongValues created with ID: " + results[0]); 20 | 21 | // insert a record that will be later updated to a long string 22 | db.command("INSERT INTO TestLongValues (val) values ('')", function(err, results) { 23 | 24 | assert(!err, "Error while executing an INSERT command: " + err); 25 | assert.equal(results.length, 1, "The inserted user should be returned."); 26 | 27 | var user = results[0]; 28 | 29 | console.log("Inserted record: " + JSON.stringify(user)); 30 | 31 | var longString = createLongString(LONG_STRING_LENGTH); 32 | 33 | // update the record to contain a very long string 34 | db.command("UPDATE TestLongValues SET value = '" + longString + "'", function(err, results) { 35 | 36 | assert(!err, "Error while executing an UPDATE command: " + err); 37 | assert.equal(results.length, 1, "The number of modified records should be returned."); 38 | 39 | var count = results[0]; 40 | 41 | console.log("Updated " + count + " records: " + LONG_STRING_LENGTH + " bytes"); 42 | 43 | // select the long record 44 | db.command("SELECT FROM TestLongValues", function(err, results) { 45 | 46 | assert(!err, "Error while executing a SELECT command: " + err); 47 | assert.equal(results.length, 1, "There must be only one record returned."); 48 | 49 | var longRecord = results[0]; 50 | 51 | assert.equal(longRecord.value.length, LONG_STRING_LENGTH, "The returned value must have the same length as the inserted one."); 52 | console.log("Received " + results.length + " long record: " + longRecord.value.length + " bytes"); 53 | 54 | db.command("DROP CLASS TestLongValues", function(err, results) { 55 | 56 | assert(!err, "Error while executing a DROP CLASS command: " + JSON.stringify(err)); 57 | assert.equal(results.length, 1, "The result should contain the boolean status of the DROP operation."); 58 | 59 | console.log("Class dropped."); 60 | 61 | db.close(); 62 | }); 63 | }); 64 | }); 65 | }); 66 | }); 67 | }); 68 | 69 | function createLongString(length) { 70 | var string = ""; 71 | for (var idx = 0; idx < length; idx++) { 72 | string += "."; 73 | } 74 | return string; 75 | } 76 | 77 | -------------------------------------------------------------------------------- /test/test_config_get.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var parser = require("../lib/orientdb/connection/parser"); 3 | var _ = require("lodash"); 4 | 5 | var orient = require("../lib/orientdb"), 6 | Db = orient.Db, 7 | Server = orient.Server; 8 | 9 | var serverConfig = require("../config/test/serverConfig"); 10 | var dbConfig = require("../config/test/dbConfig"); 11 | 12 | var server = new Server(serverConfig); 13 | 14 | server.connect(function(err) { 15 | 16 | assert(!err, err); 17 | 18 | server.configGet("log.file.level", function(err, value) { 19 | assert(!err, err); 20 | 21 | assert(_.isString(value)); 22 | assert(value !== ""); 23 | 24 | server.configGet("mvrbtree.loadFactor", function(err, value) { 25 | assert(!err, err); 26 | 27 | assert(_.isString(value)); 28 | assert(value !== ""); 29 | 30 | server.disconnect(); 31 | }) 32 | }) 33 | }); -------------------------------------------------------------------------------- /test/test_config_list.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var parser = require("../lib/orientdb/connection/parser"); 3 | var _ = require("lodash"); 4 | 5 | var orient = require("../lib/orientdb"), 6 | Db = orient.Db, 7 | Server = orient.Server; 8 | 9 | var serverConfig = require("../config/test/serverConfig"); 10 | var dbConfig = require("../config/test/dbConfig"); 11 | 12 | var server = new Server(serverConfig); 13 | 14 | server.connect(function(err) { 15 | 16 | assert(!err, err); 17 | 18 | server.configList(function(err, config) { 19 | if (server.manager.serverProtocolVersion === 13) { 20 | assert(err); 21 | } else { 22 | assert(!err, err); 23 | 24 | assert(!_.isEmpty(config)); 25 | assert(config["log.file.level"]); 26 | assert(config["log.console.level"]); 27 | } 28 | 29 | server.disconnect(); 30 | }) 31 | 32 | }); -------------------------------------------------------------------------------- /test/test_config_set.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var parser = require("../lib/orientdb/connection/parser"); 3 | var _ = require("lodash"); 4 | 5 | var orient = require("../lib/orientdb"), 6 | Db = orient.Db, 7 | Server = orient.Server; 8 | 9 | var serverConfig = require("../config/test/serverConfig"); 10 | var dbConfig = require("../config/test/dbConfig"); 11 | 12 | var server = new Server(serverConfig); 13 | 14 | server.connect(function(err) { 15 | 16 | assert(!err, err); 17 | 18 | server.configGet("log.file.level", function(err, value) { 19 | assert(!err, err); 20 | 21 | assert(_.isString(value)); 22 | assert(value !== ""); 23 | 24 | server.configSet("log.file.level", "info", function(err) { 25 | assert(!err, err); 26 | 27 | server.configGet("log.file.level", function(err, value) { 28 | assert(!err, err); 29 | 30 | assert.equal("info", value); 31 | 32 | server.configSet("non.existent.config.entry", "some value", function(err) { 33 | assert(!err, err); 34 | 35 | server.configGet("non.existent.config.entry", function(err, value) { 36 | assert(!err, err); 37 | 38 | assert(_.isString(value)); 39 | assert(value === ""); 40 | 41 | server.disconnect(); 42 | }); 43 | }); 44 | }); 45 | }); 46 | }); 47 | }); -------------------------------------------------------------------------------- /test/test_connect_disconnect.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | 3 | var Server = require("../lib/orientdb").Server; 4 | 5 | var serverConfig = require("../config/test/serverConfig"); 6 | 7 | var server = new Server(serverConfig); 8 | 9 | 10 | server.connect(function(err) { 11 | 12 | assert(!err, "Error while connecting to the server: " + JSON.stringify(err)); 13 | 14 | assert(server.sessionId > -1, "The session ID is not valid."); 15 | 16 | console.log("Connected on session: " + server.sessionId); 17 | 18 | server.disconnect(function(err) { 19 | 20 | assert(!err, "Error while disconnecting from the server: " + err); 21 | 22 | console.log("Closed connection"); 23 | }); 24 | }); 25 | 26 | -------------------------------------------------------------------------------- /test/test_data_cluster_add_remove_memory.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var _ = require("lodash"); 3 | 4 | var db = require("./setup_db.js").db; 5 | 6 | db.open(function(err, result) { 7 | 8 | assert(!err, "Error while opening the database: " + err); 9 | 10 | var clusterOptions = { 11 | type: "MEMORY", 12 | name: "test_memory" 13 | }; 14 | 15 | db.addDataCluster(clusterOptions, function(err, clusterNumber) { 16 | 17 | assert(!err, "Error while adding a data cluster: " + err); 18 | 19 | assert(_.isNumber(clusterNumber), "The result must be a number value. Received: " + clusterNumber); 20 | 21 | console.log("New MEMORY cluster with number " + clusterNumber); 22 | 23 | db.dropDataCluster(clusterNumber, function(err) { 24 | 25 | assert(!err, "Error while removing a data cluster: " + err); 26 | 27 | console.log("MEMORY cluster removed"); 28 | 29 | db.close(); 30 | }); 31 | }); 32 | }); 33 | 34 | -------------------------------------------------------------------------------- /test/test_data_cluster_add_remove_physical_memory.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var parser = require("../lib/orientdb/connection/parser"); 3 | var _ = require("lodash"); 4 | 5 | var orient = require("../lib/orientdb"), 6 | Db = orient.Db, 7 | Server = orient.Server; 8 | 9 | var serverConfig = require("../config/test/serverConfig"); 10 | var dbConfig = require("../config/test/dbConfig"); 11 | 12 | var server = new Server(serverConfig); 13 | var db = new Db("temp", server, dbConfig); 14 | 15 | 16 | db.open(function(err, result) { 17 | 18 | assert(!err, "Error while opening the database: " + err); 19 | 20 | var clusterOptions = { 21 | type: "PHYSICAL", 22 | name: "test_physical", 23 | file_name: "a_filename" 24 | }; 25 | 26 | db.addDataCluster(clusterOptions, function(err, physicalClusterNumber) { 27 | 28 | assert(!err, "Error while adding the PHYSICAL data cluster: " + JSON.stringify(err)); 29 | 30 | assert(_.isNumber(physicalClusterNumber), "The result must be a number value. Received: " + physicalClusterNumber); 31 | 32 | console.log("New PHYSICAL cluster with number: " + physicalClusterNumber); 33 | 34 | var clusterOptions = { 35 | type: "MEMORY", 36 | name: "test_memory" 37 | }; 38 | 39 | db.addDataCluster(clusterOptions, function(err, memoryClusterNumber) { 40 | 41 | assert(!err, "Error while adding the MEMORY data cluster: " + err); 42 | 43 | assert(_.isNumber(memoryClusterNumber), "The result must be a number value. Received: " + memoryClusterNumber); 44 | 45 | console.log("New MEMORY cluster with number: " + memoryClusterNumber); 46 | 47 | db.dropDataCluster(memoryClusterNumber, function(err) { 48 | 49 | assert(!err, "Error while removing the MEMORY data cluster: " + err); 50 | 51 | console.log("MEMORY cluster removed"); 52 | 53 | db.dropDataCluster(physicalClusterNumber, function(err) { 54 | 55 | assert(!err, "Error while removing the PHYSICAL data cluster: " + err); 56 | 57 | console.log("PHYSICAL cluster removed"); 58 | 59 | server.disconnect(); 60 | }); 61 | }); 62 | }); 63 | }); 64 | }); 65 | 66 | -------------------------------------------------------------------------------- /test/test_data_cluster_count.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var _ = require("lodash"); 3 | 4 | var db = require("./setup_db.js").db; 5 | 6 | db.open(function(err, result) { 7 | 8 | assert(!err, "Error while opening the database: " + err); 9 | 10 | var clusterIds = []; 11 | 12 | for (var idx = 0, length = db.clusters.length; idx < length; idx++) { 13 | clusterIds.push(db.clusters[idx].id); 14 | } 15 | 16 | db.countDataClusters(clusterIds, function(err, result) { 17 | 18 | assert(!err, "Error while counting data clusters: " + err); 19 | 20 | assert(_.isNumber(result.clusterCount), "Was expecting numeric value. Received " + result.clusterCount); 21 | 22 | console.log("Database \"" + db.databaseName + "\" has " + result.clusterCount + " clusters"); 23 | 24 | db.close(); 25 | }); 26 | }); 27 | 28 | -------------------------------------------------------------------------------- /test/test_data_cluster_datarange.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var _ = require("lodash"); 3 | 4 | var db = require("./setup_db.js").db; 5 | 6 | db.open(function(err, result) { 7 | 8 | assert(!err, "Error while opening the database: " + err); 9 | 10 | var cluster = db.clusters[0]; 11 | 12 | db.rangeDataClusters(cluster.id, function(err, result) { 13 | 14 | assert(!err, "Error while retrieving data cluster range: " + err); 15 | 16 | assert(result.begin === 0 && result.end > 0, "Was expecting cluster '" + cluster.name + "' to begin at 0 and end at something > 0 but found " + JSON.stringify(result)); 17 | 18 | console.log("Found data range for cluster '" + cluster.name + "' from " + result.begin + " to " + result.end); 19 | 20 | db.close(); 21 | }); 22 | }); 23 | 24 | -------------------------------------------------------------------------------- /test/test_data_segment.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var path = require("path"); 3 | 4 | var orient = require("../lib/orientdb"), 5 | Db = orient.Db, 6 | Server = orient.Server; 7 | 8 | var serverConfig = require("../config/test/serverConfig"); 9 | var dbConfig = require("../config/test/dbConfig"); 10 | 11 | var server = new Server(serverConfig); 12 | var db = new Db("test_create_drop", server, dbConfig); 13 | 14 | 15 | server.connect(function(err, sessionId) { 16 | 17 | assert(!err, "Error while connecting to the server: " + err); 18 | 19 | db.create(function(err) { 20 | 21 | assert(!err, err); 22 | 23 | console.log("Created database: " + db.databaseName); 24 | 25 | server.disconnect(function(err) { 26 | 27 | assert(!err, err); 28 | 29 | db.open(function(err) { 30 | 31 | assert(!err, err); 32 | 33 | var location = path.dirname(db.configuration.dataSegments[0].holeFile.path); 34 | var segmentCount = db.configuration.dataSegments.length; 35 | 36 | db.addDataSegment("test_create_drop_new_data_segment", location, function(err, segmentNumber) { 37 | 38 | assert(!err, err); 39 | 40 | assert.equal(db.configuration.dataSegments[segmentNumber].dataName, "test_create_drop_new_data_segment"); 41 | assert.equal(db.configuration.dataSegments[segmentNumber].holeFile.path, path.join(location, "test_create_drop_new_data_segment.odh")); 42 | 43 | db.dropDataSegment("test_create_drop_new_data_segment", function(err, successful) { 44 | 45 | assert(!err, err); 46 | 47 | assert(successful); 48 | // starting with Orient 1.4.0 there is an additional "index" segment with ID 1 49 | assert.equal(db.configuration.dataSegments.length, segmentCount); 50 | assert.equal(db.configuration.dataSegments[0].dataId, 0); 51 | assert.equal(db.configuration.dataSegments[0].dataName, "default"); 52 | 53 | db.close(function(err) { 54 | 55 | assert(!err, err); 56 | 57 | server.connect(function(err, sessionId) { 58 | 59 | assert(!err, err); 60 | 61 | db.drop(function(err) { 62 | 63 | assert(!err, "Error while dropping the database: " + err); 64 | 65 | console.log("Dropped database " + db.databaseName); 66 | 67 | server.disconnect(); 68 | }); 69 | }); 70 | }); 71 | }); 72 | }); 73 | }); 74 | }); 75 | }); 76 | }); 77 | -------------------------------------------------------------------------------- /test/test_date.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | 3 | var orient = require("../lib/orientdb"), 4 | GraphDb = orient.GraphDb, 5 | Server = orient.Server; 6 | 7 | var serverConfig = require("../config/test/serverConfig"); 8 | var dbConfig = require("../config/test/dbConfig"); 9 | 10 | var server = new Server(serverConfig); 11 | var graphdb = new GraphDb("temp", server, dbConfig); 12 | 13 | graphdb.open(function(err) { 14 | 15 | assert(!err, err); 16 | 17 | graphdb.createClass("testSchema", "OGraphVertex", function(err) { 18 | assert(!err, err); 19 | 20 | graphdb.command("CREATE PROPERTY testSchema.label string", function(err) { 21 | assert(!err, err); 22 | 23 | graphdb.command("CREATE PROPERTY testSchema.a_date date", function(err) { 24 | assert(!err, err); 25 | 26 | graphdb.command("CREATE PROPERTY testSchema.a_datetime datetime", function(err) { 27 | assert(!err, err); 28 | 29 | graphdb.createVertex({ label: "This date should be 2012-12-21", a_date: "2012-12-21", a_datetime: 1296279468123 }, { class: "testSchema" }, function(err, vertex) { 30 | assert(!err, err); 31 | 32 | assert.equal(2012, vertex.a_date.getFullYear()); 33 | assert.equal(11, vertex.a_date.getMonth()); 34 | assert.equal(21, vertex.a_date.getDate()); 35 | 36 | assert.equal(0, vertex.a_date.getMilliseconds()); 37 | assert.equal(123, vertex.a_datetime.getMilliseconds()); 38 | 39 | graphdb.dropClass("testSchema", function(err) { 40 | assert(!err, err); 41 | 42 | graphdb.close(); 43 | }); 44 | }); 45 | }); 46 | }); 47 | }); 48 | }); 49 | }); 50 | -------------------------------------------------------------------------------- /test/test_db_countrecords.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var _ = require("lodash"); 3 | 4 | var db = require("./setup_db.js").db; 5 | 6 | db.open(function(err, result) { 7 | 8 | assert(!err, "Error while opening the database: " + err); 9 | 10 | db.countRecords(function(err, count) { 11 | 12 | assert(!err, "Error while counting records: " + err); 13 | 14 | assert(_.isNumber(count), "The result must be a boolean value. Received: " + count); 15 | 16 | console.log("Record count: " + count); 17 | 18 | db.close(); 19 | }); 20 | }); 21 | 22 | -------------------------------------------------------------------------------- /test/test_db_create_drop.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | 3 | var orient = require("../lib/orientdb"), 4 | Db = orient.Db, 5 | Server = orient.Server; 6 | 7 | var serverConfig = require("../config/test/serverConfig"); 8 | var dbConfig = require("../config/test/dbConfig"); 9 | 10 | var server = new Server(serverConfig); 11 | var db = new Db("test_create_drop", server, dbConfig); 12 | 13 | 14 | server.connect(function(err, sessionId) { 15 | 16 | assert(!err, "Error while connecting to the server: " + err); 17 | 18 | db.create(function(err) { 19 | 20 | assert(!err, "Error while creating db: " + err); 21 | 22 | console.log("Created database: " + db.databaseName); 23 | 24 | db.drop(function(err) { 25 | 26 | assert(!err, "Error while dropping the database: " + err); 27 | 28 | console.log("Dropped database " + db.databaseName); 29 | 30 | server.disconnect(); 31 | }); 32 | }); 33 | }); 34 | 35 | -------------------------------------------------------------------------------- /test/test_db_exist.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var parser = require("../lib/orientdb/connection/parser"); 3 | var _ = require("lodash"); 4 | 5 | var orient = require("../lib/orientdb"), 6 | Db = orient.Db, 7 | Server = orient.Server; 8 | 9 | var serverConfig = require("../config/test/serverConfig"); 10 | var dbConfig = require("../config/test/dbConfig"); 11 | 12 | var server = new Server(serverConfig); 13 | var db = new Db("temp", server, dbConfig); 14 | 15 | 16 | server.connect(function(err, sessionId) { 17 | 18 | assert(!err, "Error while connecting to the server: " + err); 19 | 20 | db.exist(function(err, result) { 21 | 22 | assert(!err, "Error while checking if database exists: " + err); 23 | 24 | assert(_.isBoolean(result), "The result must be a boolean value. Received: " + result); 25 | 26 | assert(result, "The \"temp\" database should be present if you managed to open it."); 27 | 28 | console.log("Database \"" + db.databaseName + "\" exists"); 29 | 30 | server.disconnect(); 31 | }); 32 | }); 33 | 34 | -------------------------------------------------------------------------------- /test/test_db_list.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var parser = require("../lib/orientdb/connection/parser"); 3 | var _ = require("lodash"); 4 | 5 | var orient = require("../lib/orientdb"), 6 | Db = orient.Db, 7 | Server = orient.Server; 8 | 9 | var serverConfig = require("../config/test/serverConfig"); 10 | var dbConfig = require("../config/test/dbConfig"); 11 | 12 | var server = new Server(serverConfig); 13 | 14 | server.connect(function(err) { 15 | 16 | assert(!err, err); 17 | 18 | server.listDatabases(function(err, databases) { 19 | assert(!err, err); 20 | 21 | assert.equal(0, databases.temp.indexOf("memory:")); 22 | assert.equal(0, databases.tinkerpop.indexOf("local:")); 23 | 24 | server.disconnect(); 25 | }) 26 | 27 | }); 28 | 29 | -------------------------------------------------------------------------------- /test/test_db_open_3x.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var _ = require("lodash"); 3 | 4 | var db = require("./setup_db.js").db; 5 | 6 | // ************************************************************************* 7 | // Do not use this functionality!!! 8 | // Check this issue: https://github.com/gabipetrovay/node-orientdb/issues/76 9 | // ************************************************************************* 10 | var connectionsToOpen = 3, 11 | openedConnections = 0; 12 | 13 | for (var idx = 0; idx < connectionsToOpen; idx++) { 14 | 15 | console.log("Opening connection " + idx); 16 | 17 | db.open(function(err) { 18 | 19 | assert(!err, "Error while opening the database: " + err); 20 | 21 | if (++openedConnections === connectionsToOpen) { 22 | 23 | for (var jdx = 0; jdx < openedConnections; jdx++) { 24 | 25 | console.log("Closing connection."); 26 | db.close(); 27 | } 28 | } 29 | }); 30 | } 31 | 32 | -------------------------------------------------------------------------------- /test/test_db_open_close.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var _ = require("lodash"); 3 | 4 | var db = require("./setup_db.js").db; 5 | 6 | db.open(function(err) { 7 | 8 | assert(!err, "Error while opening the database: " + err); 9 | 10 | assert(db.server.sessionId, "There must be a session ID after a call to open a database. The current session is: " + db.server.sessionId); 11 | 12 | console.log("Opened database: " + db.databaseName); 13 | console.log("Session ID: " + db.server.sessionId); 14 | console.log("Database clusters: " + JSON.stringify(db.clusters)); 15 | console.log("Database classes: " + JSON.stringify(db.classes)); 16 | console.log("Database configuration: " + JSON.stringify(db.configuration)); 17 | 18 | // clusters 19 | assert(db.clusters.length >= 5); 20 | assert.equal(0, db.getClusterByName("Internal").id); 21 | // cluster name case should not matter 22 | var userCluster = db.getClusterByName("OuSeR"); 23 | assert(userCluster.id > 0); 24 | assert.equal("MEMORY", userCluster.type); 25 | assert.equal("internal", db.getClusterById(0).name); 26 | 27 | // classes 28 | assert(_.isArray(db.classes)); 29 | assert(db.classes.length >= 3); 30 | assert.equal("OUser", db.getClassByName("OUser").name); 31 | assert.equal(null, db.getClassByName("ouser")); 32 | 33 | assert.equal("ouser", db.getClusterByClass("OUser").name); 34 | assert.equal("MEMORY", db.getClusterByClass("OUser").type); 35 | assert.equal(null, db.getClusterByClass("ouser")); 36 | 37 | // memory storage doesn't have data segments 38 | assert.equal(null, db.getDataSegmentById(1)); 39 | 40 | db.close(function(err) { 41 | 42 | assert(!err, "Error while closing the database: " + err); 43 | 44 | console.log("Closed database"); 45 | }); 46 | }); 47 | 48 | -------------------------------------------------------------------------------- /test/test_db_open_close_3x.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var _ = require("lodash"); 3 | 4 | var db = require("./setup_db.js").db; 5 | 6 | console.log("Opening connection (1st time)"); 7 | db.open(function(err) { 8 | 9 | assert(!err, "Error while opening the database (1st time): " + err); 10 | 11 | console.log("Closing connection (1st time)"); 12 | db.close(function(err) { 13 | 14 | assert(!err, "Error while closing the database (1st time): " + err); 15 | 16 | console.log("Opening connection (2nd time)"); 17 | db.open(function(err) { 18 | 19 | assert(!err, "Error while opening the database (2st time): " + err); 20 | 21 | console.log("Closing connection (2nd time)"); 22 | db.close(function(err) { 23 | 24 | assert(!err, "Error while closing the database (2nd time): " + err); 25 | 26 | console.log("Opening connection (3rd time)"); 27 | db.open(function(err) { 28 | 29 | assert(!err, "Error while opening the database (3rd time): " + err); 30 | 31 | console.log("Closing connection (3rd time)"); 32 | db.close(function(err) { 33 | assert(!err, "Error while closing the database (3rd time): " + err); 34 | }); 35 | }); 36 | }); 37 | }); 38 | }); 39 | }); 40 | 41 | -------------------------------------------------------------------------------- /test/test_db_open_close_twice.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var _ = require("lodash"); 3 | 4 | var db = require("./setup_db.js").db; 5 | 6 | db.open(function(err) { 7 | 8 | assert(!err, "Error while opening the database: " + err); 9 | 10 | db.close(function(err) { 11 | 12 | assert(!err, "Error while closing the database: " + err); 13 | 14 | db.close(function(err) { 15 | 16 | assert(!err, "Error while closing the database: " + err); 17 | 18 | console.log("Closed database"); 19 | }); 20 | }); 21 | }); 22 | 23 | -------------------------------------------------------------------------------- /test/test_db_size.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var _ = require("lodash"); 3 | 4 | var db = require("./setup_db.js").db; 5 | 6 | db.open(function(err, result) { 7 | 8 | assert(!err, "Error while opening the database: " + err); 9 | 10 | db.size(function(err, size) { 11 | 12 | assert(!err, "Error while retrieving the size of the database: " + err); 13 | 14 | assert(_.isNumber(size), "The result must be a numeric value. Received: " + size); 15 | 16 | console.log("Database size: " + size); 17 | 18 | db.close(); 19 | }); 20 | }); 21 | 22 | -------------------------------------------------------------------------------- /test/test_document_save.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var _ = require("lodash"); 3 | 4 | var db = require("./setup_db.js").db; 5 | 6 | //var binary_data = new Buffer(1); 7 | //binary_data.writeUInt8(42, 0); 8 | 9 | var name1 = "IIIIIIIIIIIIIII", 10 | name2 = "OOOOOOOOOOOOOOO", 11 | clazz = "FantasyPerson"; 12 | 13 | var document = { 14 | "@class": clazz, 15 | name: name1, 16 | birthday: new Date(), 17 | fingers: 20, 18 | //fav_binary_number: binary_data, 19 | like_it: true, 20 | linked_to: "#4:0", 21 | last_time_in: { name: "Turin", when: new Date() }, 22 | known_os_list: [ "linux" ], 23 | zero_is: null 24 | }; 25 | 26 | db.open(function(err, result) { 27 | 28 | assert(!err, "Error while opening the database: " + err); 29 | 30 | // save the first version of the document 31 | db.save(document, function(err, document) { 32 | 33 | console.log("Saved document: " + JSON.stringify(document)); 34 | 35 | var doc_id = document["@rid"]; 36 | 37 | assert.equal(clazz, document["@class"]); 38 | assert(doc_id); 39 | assert.equal(name1, document.name); 40 | 41 | var version = document["@version"]; 42 | 43 | // change the name 44 | document.name = name2; 45 | 46 | // save the sexond version of the document 47 | db.save(document, function(err, document) { 48 | 49 | console.log("Updated document: " + JSON.stringify(document)); 50 | 51 | assert.equal(doc_id, document["@rid"]); 52 | assert.equal(version, document["@version"] - 1); 53 | assert.equal(name2, document.name); 54 | 55 | db.delete(document, function(err, result) { 56 | assert(!err); 57 | 58 | assert.equal(1, result.status); 59 | 60 | console.log("document deleted"); 61 | 62 | db.close(); 63 | }); 64 | }); 65 | }); 66 | }); 67 | 68 | -------------------------------------------------------------------------------- /test/test_embedded_map.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | 3 | var orient = require("../lib/orientdb"), 4 | GraphDb = orient.GraphDb, 5 | Server = orient.Server; 6 | 7 | var serverConfig = require("../config/test/serverConfig"); 8 | var dbConfig = require("../config/test/dbConfig"); 9 | 10 | var server = new Server(serverConfig); 11 | var graphdb = new GraphDb("temp", server, dbConfig); 12 | 13 | var edgeHash = { 14 | config: { 15 | modules: { 16 | mod2: "modval2" 17 | } 18 | } 19 | }; 20 | 21 | var edgeOptions = { 22 | "@class": "VEdge" 23 | }; 24 | 25 | 26 | graphdb.open(function(err, result) { 27 | 28 | assert(!err, "Error while opening the database: " + err); 29 | 30 | prepareDatabase(function(err, sourceRID, destRID) { 31 | 32 | assert(!err, err); 33 | 34 | // save the first version of the document 35 | edge(sourceRID, destRID, edgeHash, edgeOptions, function(err, edgeDoc) { 36 | assert(!err, err); 37 | 38 | console.log("Created edge: " + JSON.stringify(edgeDoc)); 39 | 40 | // remove now the created class to leave a clean environement 41 | unprepareDatabase(function(err) { 42 | assert(!err, err); 43 | graphdb.close(); 44 | }); 45 | }); 46 | }) 47 | }); 48 | 49 | 50 | function edge(srid, drid, hash, options, callback) { 51 | 52 | console.log("I have to create edge with hash: " + JSON.stringify(hash)); 53 | 54 | graphdb.loadRecord(srid, function(err, srecord) { 55 | if (err) return callback(err); 56 | 57 | graphdb.loadRecord(drid, function(err, drecord) { 58 | if (err) return callback(err); 59 | graphdb.createEdge(srecord, drecord, hash, options, callback); 60 | }); 61 | }); 62 | } 63 | 64 | function prepareDatabase(callback) { 65 | graphdb.createClass("VNode", "OGraphVertex", function(err) { 66 | if (err) return callback(err); 67 | 68 | graphdb.command("CREATE PROPERTY VNode.name STRING", function(err) { 69 | if (err) return callback(err); 70 | 71 | graphdb.command("ALTER PROPERTY VNode.name MANDATORY true", function(err) { 72 | if (err) return callback(err); 73 | 74 | graphdb.command("ALTER PROPERTY VNode.name NOTNULL true", function(err) { 75 | if (err) return callback(err); 76 | 77 | graphdb.createClass("VEdge", "OGraphEdge", function(err) { 78 | if (err) return callback(err); 79 | 80 | graphdb.command("CREATE PROPERTY VEdge.config EMBEDDEDMAP", function(err) { 81 | if (err) return callback(err); 82 | 83 | graphdb.command("ALTER PROPERTY VEdge.config NOTNULL true", function(err) { 84 | if (err) return callback(err); 85 | 86 | var doc = { 87 | "@class": "VNode", 88 | name: "source" 89 | }; 90 | 91 | graphdb.save(doc, function(err, savedDoc) { 92 | if (err) return callback(err); 93 | 94 | var sourceRID = savedDoc["@rid"]; 95 | doc = { 96 | "@class": "VNode", 97 | name: "destination" 98 | }; 99 | 100 | graphdb.save(doc, function(err, savedDoc) { 101 | if (err) return callback(err); 102 | 103 | var destRID = savedDoc["@rid"]; 104 | 105 | callback(null, sourceRID, destRID); 106 | }); 107 | }); 108 | }); 109 | }); 110 | }); 111 | }); 112 | }); 113 | }); 114 | }); 115 | } 116 | 117 | function unprepareDatabase(callback) { 118 | graphdb.dropClass("VEdge", function(err) { 119 | if (err) return callback(err); 120 | 121 | graphdb.dropClass("VNode", callback); 122 | }); 123 | } 124 | 125 | -------------------------------------------------------------------------------- /test/test_embedded_types.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var _ = require("lodash"); 3 | 4 | var db = require("./setup_db.js").db; 5 | 6 | db.open(function(err) { 7 | 8 | assert(!err, "Error while opening the database: " + err); 9 | 10 | var doc = { "@class": "userInfo", 11 | firstName: "pippo", 12 | lastName: "pappo", 13 | cellNumber: undefined, 14 | gender: "m", 15 | deviceList: [], 16 | socialTokens: [ 17 | { sn: "google2", 18 | token: "reqrbqipwuebrq5ipu4bwr9b1w40rqwor47br8qwo467brfoqiw4bfoibqw4i7f" } 19 | ], 20 | commonProperties: { objType: "userInfo", 21 | "@class": "objProperties", 22 | deletedAt: null, 23 | "@type": "d" }, 24 | linked_map: { 25 | link1: { 26 | '@class': 'objProperties', 27 | key: "value" 28 | } 29 | }, 30 | "@type": "d" }; 31 | 32 | prepareDatabase(function(err) { 33 | assert(!err, err); 34 | 35 | db.save(doc.commonProperties, function(err, savedCommonProperties) { 36 | assert(!err, err); 37 | doc.commonProperties = savedCommonProperties["@rid"]; 38 | 39 | db.save(doc.linked_map.link1, function(err, savedLink1) { 40 | assert(!err, err); 41 | 42 | doc.linked_map.link1 = savedLink1["@rid"]; 43 | 44 | db.save(doc, function(err, savedDoc) { 45 | assert(!err, err); 46 | 47 | db.loadRecord(savedDoc["@rid"], function(err, newDoc) { 48 | 49 | assert.equal(doc.linked_map.link1, newDoc.linked_map.link1); 50 | assert.equal(doc.commonProperties, newDoc.commonProperties); 51 | 52 | // remove now the created class to leave a clean environement 53 | unprepareDatabase(function(err) { 54 | assert(!err, err); 55 | 56 | db.close(); 57 | }); 58 | }); 59 | }); 60 | }); 61 | }); 62 | }); 63 | }); 64 | 65 | function prepareDatabase(callback) { 66 | db.createClass("userInfo", function(err) { 67 | if (err) { return callback(err); } 68 | 69 | db.createClass("objProperties", function(err) { 70 | if (err) { return callback(err); } 71 | 72 | db.command("CREATE PROPERTY userInfo.commonProperties link objProperties", function(err) { 73 | if (err) { return callback(err); } 74 | 75 | db.command("CREATE PROPERTY userInfo.linked_map linkmap objProperties", function(err) { 76 | callback(err); 77 | }); 78 | }); 79 | }); 80 | }); 81 | } 82 | 83 | function unprepareDatabase(callback) { 84 | db.dropClass("userInfo", function(err) { 85 | if (err) { return callback(err); } 86 | 87 | db.dropClass("objProperties", callback); 88 | }); 89 | } 90 | 91 | -------------------------------------------------------------------------------- /test/test_error_parsing.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var _ = require("lodash"); 3 | 4 | var db = require("./setup_db.js").db; 5 | 6 | db.open(function(err, result) { 7 | 8 | assert(!err, "Error while opening the database: " + err); 9 | 10 | var count = 0; 11 | for (var idx = 0; idx < 1000; idx++) { 12 | db.command("SELECT FROM OUser", function() { 13 | count++; 14 | }); 15 | db.command("CREATE CLASS OUser", function(err, results) { 16 | count++ 17 | assert(err); 18 | if (count === 2000) { 19 | db.close(); 20 | } 21 | }); 22 | } 23 | }); 24 | 25 | -------------------------------------------------------------------------------- /test/test_fetch_plans.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var _ = require("lodash"); 3 | 4 | var orient = require("../lib/orientdb"), 5 | GraphDb = orient.GraphDb, 6 | Server = orient.Server; 7 | 8 | var serverConfig = require("../config/test/serverConfig"); 9 | var dbConfig = require("../config/test/dbConfig"); 10 | 11 | var server = new Server(serverConfig); 12 | var db = new GraphDb("temp", server, dbConfig); 13 | 14 | 15 | db.open(function(err) { 16 | assert(!err, err); 17 | 18 | prepareDatabase(function() { 19 | prepareDatabase(function(err, first, second, third, first_to_second, second_to_third, third_to_first) { 20 | assert(!err, err); 21 | 22 | db.loadRecord(first["@rid"], { fetchPlan: "*:1" }, function(err, vertex) { 23 | assert(!err, err); 24 | 25 | assert.equal("first", vertex.name); 26 | 27 | assert.equal("first_to_second", vertex.out[0].label); 28 | assert.equal(first["@rid"], vertex.out[0].out); 29 | assert.equal(second["@rid"], vertex.out[0].in); 30 | 31 | assert.equal(third["@rid"], vertex.in[0].out); 32 | assert.equal(first["@rid"], vertex.in[0].in); 33 | 34 | db.loadRecord(first["@rid"], { fetchPlan: "*:2" }, function(err, vertex) { 35 | assert(!err, err); 36 | 37 | assert.equal("first", vertex.name); 38 | 39 | assert.equal("first_to_second", vertex.out[0].label); 40 | assert.equal(first["@rid"], vertex.out[0].out); 41 | assert.equal("second", vertex.out[0].in.name); 42 | assert.equal(first_to_second["@rid"], vertex.out[0].in.in[0]); 43 | assert.equal(second_to_third["@rid"], vertex.out[0].in.out[0]); 44 | 45 | assert.equal("third_to_first", vertex.in[0].label); 46 | assert.equal(first["@rid"], vertex.in[0].in); 47 | assert.equal("third", vertex.in[0].out.name); 48 | assert.equal(second_to_third["@rid"], vertex.in[0].out.in[0]); 49 | assert.equal(third_to_first["@rid"], vertex.in[0].out.out[0]); 50 | 51 | db.loadRecord(first["@rid"], { fetchPlan: "*:-1" }, function(err, vertex) { 52 | assert(!err, err); 53 | 54 | assertVertexHierarchyIsComplete(vertex, first, first_to_second, second, second_to_third, third, third_to_first); 55 | 56 | db.command("select from " + first["@rid"], { fetchPlan: "*:-1" }, function(err, results) { 57 | assert(!err, err); 58 | 59 | assert.equal(1, results.length); 60 | 61 | var vertex = results[0]; 62 | 63 | assertVertexHierarchyIsComplete(vertex, first, first_to_second, second, second_to_third, third, third_to_first); 64 | 65 | db.command("select from V where name = 'first'", { fetchPlan: "*:-1" }, function(err, results) { 66 | assert(!err, err); 67 | 68 | assert(results.length > 1); 69 | 70 | var vertex = _.last(results); 71 | 72 | assertVertexHierarchyIsComplete(vertex, first, first_to_second, second, second_to_third, third, third_to_first); 73 | 74 | db.command("insert into V (name) values ('other')", { fetchPlan: "*:-1" }, function(err, results) { 75 | assert(err, "Cannot execute async queries with other than selects"); 76 | 77 | db.close(); 78 | }); 79 | }); 80 | }); 81 | }); 82 | }); 83 | }); 84 | }); 85 | }); 86 | }); 87 | 88 | function assertVertexHierarchyIsComplete(vertex, first, first_to_second, second, second_to_third, third, third_to_first) { 89 | assert.equal("first", vertex.name); 90 | 91 | assert.equal("first_to_second", vertex.out[0].label); 92 | assert.equal("second", vertex.out[0].in.name); 93 | assert.equal(first_to_second["@rid"], vertex.out[0].in.in[0]); 94 | assert.equal("second_to_third", vertex.out[0].in.out[0].label); 95 | assert.equal(second["@rid"], vertex.out[0].in.out[0].out); 96 | assert.equal("third", vertex.out[0].in.out[0].in.name); 97 | assert.equal(second_to_third["@rid"], vertex.out[0].in.out[0].in.in[0]); 98 | assert.equal("third_to_first", vertex.out[0].in.out[0].in.out[0].label); 99 | assert.equal(third["@rid"], vertex.out[0].in.out[0].in.out[0].out); 100 | assert.equal(first["@rid"], vertex.out[0].in.out[0].in.out[0].in); 101 | 102 | assert.equal("third_to_first", vertex.in[0].label); 103 | assert.equal(first["@rid"], vertex.in[0].in); 104 | assert.equal("third", vertex.in[0].out.name); 105 | assert.equal("second_to_third", vertex.in[0].out.in[0].label); 106 | assert.equal(third["@rid"], vertex.in[0].out.in[0].in); 107 | assert.equal("second", vertex.in[0].out.in[0].out.name); 108 | assert.equal(second_to_third["@rid"], vertex.in[0].out.in[0].out.out[0]); 109 | assert.equal("first_to_second", vertex.in[0].out.in[0].out.in[0].label); 110 | assert.equal(second["@rid"], vertex.in[0].out.in[0].out.in[0].in); 111 | assert.equal(first["@rid"], vertex.in[0].out.in[0].out.in[0].out); 112 | assert.equal(third_to_first["@rid"], vertex.in[0].out.out[0]); 113 | } 114 | 115 | function prepareDatabase(callback) { 116 | db.createVertex({ name: "first" }, function(err, first) { 117 | if (err) { return callback(err); } 118 | 119 | db.createVertex({ name: "second" }, function(err, second) { 120 | if (err) { return callback(err); } 121 | 122 | db.createVertex({ name: "third" }, function(err, third) { 123 | if (err) { return callback(err); } 124 | 125 | db.createEdge(first, second, { label: "first_to_second" }, function(err, first_to_second) { 126 | if (err) { return callback(err); } 127 | 128 | db.createEdge(second, third, { label: "second_to_third" }, function(err, second_to_third) { 129 | if (err) { return callback(err); } 130 | 131 | db.createEdge(third, first, { label: "third_to_first" }, function(err, third_to_first) { 132 | if (err) { return callback(err); } 133 | 134 | callback(null, first, second, third, first_to_second, second_to_third, third_to_first); 135 | }); 136 | }) 137 | }); 138 | }); 139 | }); 140 | }); 141 | } -------------------------------------------------------------------------------- /test/test_graph.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | 3 | var orient = require("../lib/orientdb"), 4 | GraphDb = orient.GraphDb, 5 | Server = orient.Server; 6 | 7 | var serverConfig = require("../config/test/serverConfig"); 8 | var dbConfig = require("../config/test/dbConfig"); 9 | 10 | var server = new Server(serverConfig); 11 | var graphdb = new GraphDb("temp", server, dbConfig); 12 | 13 | function createVertexes(graphdb, callback) { 14 | graphdb.createVertex({ id: 0 }, function(err, rootNode) { 15 | assert(!err, err); 16 | 17 | graphdb.createVertex({ name: "first node" }, function(err, childNode) { 18 | assert(!err, err); 19 | 20 | graphdb.createEdge(rootNode, childNode, function(err, edge) { 21 | assert(!err, err); 22 | 23 | assert.equal(rootNode["out"][0], edge["@rid"]); 24 | assert.equal(childNode["in"][0], edge["@rid"]); 25 | 26 | assert.equal(rootNode["@rid"], edge["out"]); 27 | assert.equal(childNode["@rid"], edge["in"]); 28 | 29 | graphdb.createEdge(childNode, rootNode, { label: "child_of" }, function(err, edge) { 30 | assert(!err, err); 31 | graphdb.createEdge(childNode["@rid"], rootNode["@rid"], function(err, edge) { 32 | assert(!err, err); 33 | 34 | childNode["out"].push(edge["@rid"]); 35 | rootNode["in"].push(edge["@rid"]); 36 | 37 | callback(rootNode, childNode); 38 | }); 39 | }); 40 | 41 | }); 42 | }); 43 | }); 44 | } 45 | 46 | graphdb.open(function(err) { 47 | 48 | assert(!err, "Error while opening the database: " + err); 49 | 50 | assert.equal("OGraphVertex", graphdb.getClassByName("OGraphVertex").name); 51 | assert.equal("OGraphVertex", graphdb.getClassByName("V").name); 52 | assert.equal("OGraphEdge", graphdb.getClassByName("OGraphEdge").name); 53 | assert.equal("OGraphEdge", graphdb.getClassByName("E").name); 54 | 55 | createVertexes(graphdb, function(rootNode, childNode) { 56 | graphdb.getOutEdges(rootNode, function(err, outEdges) { 57 | assert(!err); 58 | 59 | assert.equal(1, outEdges.length); 60 | 61 | graphdb.getInVertex(outEdges[0], function(err, vertex) { 62 | assert(!err); 63 | 64 | assert.equal(childNode["@rid"], vertex["@rid"]); 65 | 66 | graphdb.getInEdges(childNode, function(err, inEdges) { 67 | assert(!err); 68 | 69 | assert.equal(1, inEdges.length); 70 | 71 | graphdb.getOutVertex(inEdges[0], function(err, vertex) { 72 | assert(!err); 73 | 74 | assert.equal(rootNode["@rid"], vertex["@rid"]); 75 | 76 | graphdb.getOutEdges(childNode, function(err, outEdges) { 77 | assert(!err); 78 | 79 | assert.equal(2, outEdges.length); 80 | 81 | graphdb.getOutEdges(childNode, "child_of", function(err, outEdges) { 82 | assert(!err); 83 | assert.equal(1, outEdges.length); 84 | 85 | graphdb.fromVertex(childNode).outVertexes("child_of", function(err, vertexes) { 86 | assert(!err); 87 | 88 | assert.equal(1, vertexes.length); 89 | 90 | assert.equal(rootNode["@rid"], vertexes[0]["@rid"]); 91 | 92 | graphdb.fromVertex(childNode).outVertexes(function(err, vertexes) { 93 | assert(!err); 94 | 95 | assert.equal(2, vertexes.length); 96 | 97 | graphdb.fromVertex(childNode).inVertexes(function(err, vertexes) { 98 | assert(!err); 99 | 100 | assert.equal(1, vertexes.length); 101 | assert.equal(rootNode["@rid"], vertexes[0]["@rid"]); 102 | 103 | graphdb.close(); 104 | }); 105 | }); 106 | }); 107 | }); 108 | }); 109 | }); 110 | }); 111 | }); 112 | }); 113 | }); 114 | }); -------------------------------------------------------------------------------- /test/test_graph_lots_of_edges.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | 3 | var orient = require("../lib/orientdb"), 4 | GraphDb = orient.GraphDb, 5 | Server = orient.Server; 6 | 7 | var serverConfig = require("../config/test/serverConfig"); 8 | var dbConfig = require("../config/test/dbConfig"); 9 | 10 | var server = new Server(serverConfig); 11 | var graphdb = new GraphDb("temp", server, dbConfig); 12 | 13 | 14 | graphdb.open(function(err) { 15 | assert(!err, err); 16 | 17 | graphdb.createVertex({ name: "from vertex" }, function(err, fromVertex) { 18 | assert(!err, err); 19 | 20 | graphdb.createVertex({ name: "to vertex" }, function(err, toVertex) { 21 | assert(!err, err); 22 | 23 | var edges = []; 24 | for (var idx = 0; idx < 50; idx++) { 25 | graphdb.createEdge(fromVertex["@rid"], toVertex["@rid"], function(err, edge) { 26 | assert(!err, err); 27 | 28 | edges.push(edge["@rid"]); 29 | 30 | if (edges.length === 50) { 31 | graphdb.loadRecord(fromVertex["@rid"], function(err, fromVertex) { 32 | assert(!err, err); 33 | 34 | assert.equal(50, fromVertex.out.length); 35 | 36 | graphdb.command("select from " + fromVertex["@rid"], function(err, results) { 37 | assert(!err, err); 38 | 39 | assert.equal(50, results[0].out.length); 40 | 41 | graphdb.close(); 42 | }); 43 | }); 44 | } 45 | }); 46 | } 47 | }); 48 | }); 49 | }); 50 | 51 | -------------------------------------------------------------------------------- /test/test_graph_with_additional_mandatory_fields.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var parser = require("../lib/orientdb/connection/parser"); 3 | var _ = require("lodash"); 4 | 5 | var orient = require("../lib/orientdb"), 6 | GraphDb = orient.GraphDb, 7 | Server = orient.Server; 8 | 9 | var serverConfig = require("../config/test/serverConfig"); 10 | var dbConfig = require("../config/test/dbConfig"); 11 | 12 | var server = new Server(serverConfig); 13 | var graphdb = new GraphDb("temp", server, dbConfig); 14 | 15 | var vertex = function(idx) { 16 | return { 17 | v_additional: "value" + idx + "_of_v_additional", 18 | embed: { 19 | key: "value" + idx 20 | } 21 | } 22 | }; 23 | 24 | var edge = function(idx) { 25 | return { 26 | e_additional: "value" + idx + "_of_e_additional", 27 | embed: { 28 | key: "value" + idx 29 | } 30 | }; 31 | }; 32 | 33 | graphdb.open(function(err) { 34 | assert(!err, err); 35 | 36 | prepareDatabase(function(err) { 37 | assert(!err, err); 38 | 39 | graphdb.createVertex(vertex(1), { "class": "VertexWithMandatoryFields" }, function(err, vertex1) { 40 | assert(!err, err); 41 | 42 | assert.equal("value1_of_v_additional", vertex1.v_additional); 43 | assert.equal("value1", vertex1.embed.key); 44 | 45 | assert(!_.isUndefined(vertex1["@rid"])); 46 | 47 | graphdb.createVertex(vertex(2), { "class": "VertexWithMandatoryFields" }, function(err, vertex2) { 48 | assert(!err, err); 49 | 50 | assert.equal("value2_of_v_additional", vertex2.v_additional); 51 | assert.equal("value2", vertex2.embed.key); 52 | assert(!_.isUndefined(vertex2["@rid"])); 53 | 54 | graphdb.createEdge(vertex1, vertex2, edge(1), { "class": "EdgeWithMandatoryFields" }, function(err, edge) { 55 | assert(!err, err); 56 | 57 | assert.equal("value1_of_e_additional", edge.e_additional); 58 | assert.equal("value1", edge.embed.key); 59 | assert(!_.isUndefined(edge["@rid"])); 60 | 61 | unprepareDatabase(function(err) { 62 | assert(!err, err); 63 | 64 | graphdb.close() 65 | }); 66 | }); 67 | }); 68 | }); 69 | }) 70 | 71 | }); 72 | 73 | function prepareDatabase(callback) { 74 | graphdb.createClass("VertexWithMandatoryFields", "OGraphVertex", function(err) { 75 | if (err) return callback(err); 76 | 77 | graphdb.command("ALTER CLASS VertexWithMandatoryFields SHORTNAME VWMF", function(err) { 78 | if (err) { return callback(err); } 79 | 80 | graphdb.command("ALTER CLASS VertexWithMandatoryFields OVERSIZE 2", function(err) { 81 | if (err) { return callback(err); } 82 | 83 | graphdb.createClass("EdgeWithMandatoryFields", "OGraphEdge", function(err) { 84 | if (err) return callback(err); 85 | 86 | graphdb.command("ALTER CLASS EdgeWithMandatoryFields SHORTNAME EWMF", function(err) { 87 | if (err) { return callback(err); } 88 | 89 | graphdb.command("CREATE PROPERTY EWMF.e_additional string", function(err) { 90 | if (err) return callback(err); 91 | 92 | graphdb.command("ALTER PROPERTY EWMF.e_additional MANDATORY true", function(err) { 93 | if (err) return callback(err); 94 | 95 | graphdb.command("CREATE PROPERTY VWMF.v_additional string", function(err) { 96 | if (err) return callback(err); 97 | 98 | graphdb.command("ALTER PROPERTY VWMF.v_additional MANDATORY true", function(err) { 99 | if (err) return callback(err); 100 | 101 | graphdb.reload(callback); 102 | }); 103 | }); 104 | }); 105 | }); 106 | }); 107 | }); 108 | }); 109 | }); 110 | }); 111 | } 112 | 113 | function unprepareDatabase(callback) { 114 | graphdb.dropClass("VertexWithMandatoryFields", function(err) { 115 | if (err) return callback(err); 116 | 117 | graphdb.dropClass("EdgeWithMandatoryFields", callback); 118 | }); 119 | } -------------------------------------------------------------------------------- /test/test_large_document_save_load.js: -------------------------------------------------------------------------------- 1 | var document = 2 | { 3 | "@class": "TestDocument", 4 | "web-app": { 5 | "servlet": [ 6 | { 7 | "servlet-name": "cofaxCDS", 8 | "servlet-class": "org.cofax.cds.CDSServlet", 9 | "init-param": { 10 | "configGlossary:installationAt": "Philadelphia, PA", 11 | "configGlossary:adminEmail": "ksm@pobox.com", 12 | "configGlossary:poweredBy": "Cofax", 13 | "configGlossary:poweredByIcon": "/images/cofax.gif", 14 | "configGlossary:staticPath": "/content/static", 15 | "templateProcessorClass": "org.cofax.WysiwygTemplate", 16 | "templateLoaderClass": "org.cofax.FilesTemplateLoader", 17 | "templatePath": "templates", 18 | "templateOverridePath": "", 19 | "defaultListTemplate": "listTemplate.htm", 20 | "defaultFileTemplate": "articleTemplate.htm", 21 | "useJSP": false, 22 | "jspListTemplate": "listTemplate.jsp", 23 | "jspFileTemplate": "articleTemplate.jsp", 24 | "cachePackageTagsTrack": 200, 25 | "cachePackageTagsStore": 200, 26 | "cachePackageTagsRefresh": 60, 27 | "cacheTemplatesTrack": 100, 28 | "cacheTemplatesStore": 50, 29 | "cacheTemplatesRefresh": 15, 30 | "cachePagesTrack": 200, 31 | "cachePagesStore": 100, 32 | "cachePagesRefresh": 10, 33 | "cachePagesDirtyRead": 10, 34 | "searchEngineListTemplate": "forSearchEnginesList.htm", 35 | "searchEngineFileTemplate": "forSearchEngines.htm", 36 | "searchEngineRobotsDb": "WEB-INF/robots.db", 37 | "useDataStore": true, 38 | "dataStoreClass": "org.cofax.SqlDataStore", 39 | "redirectionClass": "org.cofax.SqlRedirection", 40 | "dataStoreName": "cofax", 41 | "dataStoreDriver": "com.microsoft.jdbc.sqlserver.SQLServerDriver", 42 | "dataStoreUrl": "jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon", 43 | "dataStoreUser": "sa", 44 | "dataStorePassword": "dataStoreTestQuery", 45 | "dataStoreTestQuery": "SET NOCOUNT ON;select test='test';", 46 | "dataStoreLogFile": "/usr/local/tomcat/logs/datastore.log", 47 | "dataStoreInitConns": 10, 48 | "dataStoreMaxConns": 100, 49 | "dataStoreConnUsageLimit": 100, 50 | "dataStoreLogLevel": "debug", 51 | "maxUrlLength": 500} 52 | }, 53 | { 54 | "servlet-name": "cofaxEmail", 55 | "servlet-class": "org.cofax.cds.EmailServlet", 56 | "init-param": { 57 | "mailHost": "mail1", 58 | "mailHostOverride": "mail2"} 59 | }, 60 | { 61 | "servlet-name": "cofaxAdmin", 62 | "servlet-class": "org.cofax.cds.AdminServlet"}, 63 | 64 | { 65 | "servlet-name": "fileServlet", 66 | "servlet-class": "org.cofax.cds.FileServlet"}, 67 | { 68 | "servlet-name": "cofaxTools", 69 | "servlet-class": "org.cofax.cms.CofaxToolsServlet", 70 | "init-param": { 71 | "templatePath": "toolstemplates/", 72 | "log": 1, 73 | "logLocation": "/usr/local/tomcat/logs/CofaxTools.log", 74 | "logMaxSize": "", 75 | "dataLog": 1, 76 | "dataLogLocation": "/usr/local/tomcat/logs/dataLog.log", 77 | "dataLogMaxSize": "", 78 | "removePageCache": "/content/admin/remove?cache=pages&id=", 79 | "removeTemplateCache": "/content/admin/remove?cache=templates&id=", 80 | "fileTransferFolder": "/usr/local/tomcat/webapps/content/fileTransferFolder", 81 | "lookInContext": 1, 82 | "adminGroupID": 4, 83 | "betaServer": true} 84 | } 85 | ], 86 | "servlet-mapping": { 87 | "cofaxCDS": "/", 88 | "cofaxEmail": "/cofaxutil/aemail/*", 89 | "cofaxAdmin": "/admin/*", 90 | "fileServlet": "/static/*", 91 | "cofaxTools": "/tools/*" }, 92 | "taglib": { 93 | "taglib-uri": "cofax.tld", 94 | "taglib-location": "/WEB-INF/tlds/cofax.tld" 95 | } 96 | } 97 | }; 98 | 99 | for (var idx = 0; idx < 100; idx++) { 100 | document["web-app-" + idx] = document["web-app"]; 101 | } 102 | 103 | console.log("document length is " + JSON.stringify(document).length + " chars"); 104 | 105 | var assert = require("assert"); 106 | var _ = require("lodash"); 107 | 108 | var db = require("./setup_db.js").db; 109 | 110 | db.open(function(err) { 111 | 112 | assert(!err, "Error while opening the database: " + err); 113 | 114 | db.save(document, function(err, document) { 115 | 116 | var rid = document["@rid"]; 117 | 118 | db.loadRecord(rid, function(err, record) { 119 | 120 | assert.equal("TestDocument", record["@class"]); 121 | assert(rid); 122 | assert.equal(0, record["@version"]); 123 | assert.equal("cofax.tld", record["web-app"]["taglib"]["taglib-uri"]); 124 | 125 | db.close(); 126 | 127 | }); 128 | }); 129 | }); 130 | -------------------------------------------------------------------------------- /test/test_lh_cluster_used.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var _ = require("lodash"); 3 | 4 | var testDb = require("./setup_db.js"); 5 | var server = testDb.server; 6 | var db = testDb.db; 7 | 8 | db.open(function(err) { 9 | 10 | assert(!err, "Error while opening the database: " + err); 11 | 12 | db.isLHClustersUsed(function(err, used) { 13 | 14 | if (server.manager.serverProtocolVersion < 13) { 15 | assert(err); 16 | } else { 17 | assert(!err, err); 18 | 19 | assert(!used); 20 | } 21 | 22 | db.close(); 23 | }); 24 | }); 25 | 26 | -------------------------------------------------------------------------------- /test/test_multiple_connections.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var parser = require("../lib/orientdb/connection/parser"); 3 | var _ = require("lodash"); 4 | 5 | var orient = require("../lib/orientdb"), 6 | Db = orient.Db, 7 | Server = orient.Server; 8 | 9 | var serverConfig = require('../config/test/serverConfig'); 10 | var dbConfig = require('../config/test/dbConfig'); 11 | 12 | var server1 = new Server(serverConfig); 13 | var db1 = new Db('temp', server1, dbConfig); 14 | 15 | var server2 = new Server(serverConfig); 16 | var db2 = new Db('temp', server2, dbConfig); 17 | 18 | 19 | db1.open(function(err, result) { 20 | 21 | assert(!err, "Error while opening the database: " + err); 22 | 23 | if (err) { console.log(err); return; } 24 | 25 | console.log('Connection 1 established'); 26 | 27 | db2.open(function(err, result) { 28 | 29 | assert(!err, "Error while opening the database: " + err); 30 | 31 | if (err) { console.log(err); return; } 32 | 33 | console.log('Connection 2 established'); 34 | 35 | db1.countRecords(function(err, count) { 36 | 37 | if (err) { console.log(err); return; } 38 | 39 | assert(_.isNumber(count), "The result must be a number value. Received: " + count); 40 | 41 | console.log('Record count through connection 1: ' + count); 42 | 43 | db2.countRecords(function(err, count) { 44 | 45 | if (err) { console.log(err); return; } 46 | 47 | assert(_.isNumber(count), "The result must be a number value. Received: " + count); 48 | 49 | console.log('Record count through connection 2: ' + count); 50 | 51 | db1.close(function(err) { 52 | 53 | if (err) { console.log(err); return; } 54 | 55 | console.log('Connection 1 closed'); 56 | 57 | db2.close(function(err) { 58 | 59 | if (err) { console.log(err); return; } 60 | 61 | console.log('Connection 2 closed'); 62 | }); 63 | }); 64 | }); 65 | }); 66 | }); 67 | }); 68 | 69 | -------------------------------------------------------------------------------- /test/test_positions.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var _ = require("lodash"); 3 | var parser = require("../lib/orientdb/connection/parser"); 4 | 5 | var db = require("./setup_db.js").db; 6 | var server = db.server; 7 | 8 | db.open(function(err) { 9 | 10 | assert(!err, err); 11 | 12 | var userCluster = db.getClusterByName("ouser"); 13 | 14 | db.query("select from OUser", function(err, users) { 15 | assert(!err, err); 16 | 17 | assert(users.length >= 1); 18 | 19 | var firstUser = users[0]; 20 | var firstUserRID = parser.parseRid(firstUser["@rid"]); 21 | 22 | db.positionsHigher(firstUser["@rid"], function(err, positions) { 23 | 24 | if (server.manager.serverProtocolVersion < 13) { 25 | assert(err); 26 | 27 | db.close(); 28 | } else { 29 | 30 | assert(!err, err); 31 | 32 | assert(_.isArray(positions)); 33 | var position = positions[0]; 34 | 35 | assert.equal(position.clusterPosition, firstUserRID.clusterPosition + 1); 36 | assert.equal(position.dataSegmentId, userCluster.dataSegmentId); 37 | assert.equal(position.dataSegmentPos, userCluster.id + 1); 38 | assert.equal(position.recordSize, 0); 39 | assert.equal(position.recordVersion, 0); 40 | 41 | var secondUser = users[1]; 42 | var secondUserRID = parser.parseRid(secondUser["@rid"]); 43 | 44 | db.positionsLower(secondUser["@rid"], function(err, positions) { 45 | 46 | assert(!err, err); 47 | 48 | assert(_.isArray(positions)); 49 | var position = positions[0]; 50 | 51 | assert.equal(position.clusterPosition, secondUserRID.clusterPosition - 1); 52 | assert.equal(position.dataSegmentId, userCluster.dataSegmentId); 53 | assert.equal(position.dataSegmentPos, userCluster.id - 1); 54 | assert.equal(position.recordSize, 0); 55 | assert.equal(position.recordVersion, 0); 56 | 57 | db.close(); 58 | }); 59 | } 60 | }); 61 | 62 | }); 63 | }); 64 | 65 | -------------------------------------------------------------------------------- /test/test_record_create_document.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var _ = require("lodash"); 3 | 4 | var db = require("./setup_db.js").db; 5 | 6 | db.open(function(err, result) { 7 | 8 | assert(!err, "Error while opening the database: " + err); 9 | 10 | assert(db.clusters && db.clusters.length, "The \"" + db.databaseName + "\" database must have at least one cluster if not more"); 11 | 12 | var cluster = db.clusters[0]; 13 | console.log("Using cluster: " + cluster.id + " \"" + db.clusters[0].name + "\""); 14 | 15 | // insert a first record 16 | var firstDocData = "TestClass@nick:\"ThePresident\",subdoc:(name:\"subdoc name\",id_field:42),follows:[],followers:[],name:\"Barack\",surname:\"Obama\",location:#3:2,invitedBy:,salary_cloned:,salary:120.3f"; 17 | var data = new Buffer(firstDocData.length); 18 | data.write(firstDocData); 19 | 20 | var recordData = { 21 | clusterId: cluster.id, 22 | content: data, 23 | type: "d", 24 | dataSegmentId: cluster.dataSegmentId 25 | }; 26 | 27 | console.log("Inserting 1st record..."); 28 | 29 | db.createRecord(recordData, function(err, result) { 30 | 31 | assert(!err, "Error while creating the 1st record: " + err); 32 | 33 | var firstRecord = result.position; 34 | console.log("Created 1st record on position: " + firstRecord); 35 | 36 | // insert a second record 37 | var secondDocData = firstDocData.replace("followers:[]", "followers:[#" + cluster.id + ":" + firstRecord + "]"); 38 | var data = new Buffer(secondDocData.length); 39 | data.write(secondDocData); 40 | 41 | recordData.content = data; 42 | 43 | console.log("Inserting 2nd record..."); 44 | 45 | db.createRecord(recordData, function(err, result) { 46 | 47 | assert(!err, "Error while creating the 2nd record: " + err); 48 | 49 | var secondRecord = result.position; 50 | 51 | console.log("Created 2nd record on position: " + secondRecord); 52 | assert(result.position === (firstRecord + 1)); 53 | 54 | db.close(); 55 | }); 56 | }); 57 | }); 58 | 59 | -------------------------------------------------------------------------------- /test/test_record_create_flat_data.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var _ = require("lodash"); 3 | 4 | var db = require("./setup_db.js").db; 5 | 6 | db.open(function(err, result) { 7 | 8 | assert(!err, "Error while opening the database: " + err); 9 | 10 | var data = new Buffer(14); 11 | data.write("this is a test"); 12 | 13 | var cluster = db.clusters[0]; 14 | var recordData = { 15 | clusterId: cluster.id, 16 | content: data, 17 | type: "f", 18 | dataSegmentId: cluster.dataSegmentId 19 | }; 20 | 21 | db.createRecord(recordData, function(err, result) { 22 | 23 | var firstRecord = result.position; 24 | 25 | db.createRecord(recordData, function(err, result) { 26 | 27 | assert(result.position === (firstRecord + 1)); 28 | 29 | db.close(); 30 | }); 31 | 32 | }); 33 | 34 | }); 35 | 36 | -------------------------------------------------------------------------------- /test/test_record_create_raw_bytes.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var _ = require("lodash"); 3 | 4 | var db = require("./setup_db.js").db; 5 | 6 | db.open(function(err, result) { 7 | 8 | assert(!err, "Error while opening the database: " + err); 9 | 10 | var data = new Buffer(14); 11 | data.write("this is a test"); 12 | 13 | var cluster = db.clusters[0]; 14 | var recordData = { 15 | clusterId: cluster.id, 16 | content: data, 17 | type: "b", 18 | dataSegmentId: cluster.dataSegmentId 19 | }; 20 | 21 | db.createRecord(recordData, function(err, result) { 22 | 23 | var firstRecord = result.position; 24 | 25 | db.createRecord(recordData, function(err, result) { 26 | 27 | assert(result.position === (firstRecord + 1)); 28 | 29 | db.close(); 30 | }); 31 | 32 | }); 33 | 34 | }); 35 | 36 | -------------------------------------------------------------------------------- /test/test_record_document_create_load_update_delete.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var _ = require("lodash"); 3 | 4 | var db = require("./setup_db.js").db; 5 | 6 | db.open(function(err, result) { 7 | 8 | assert(!err, "Error while opening the database: " + err); 9 | 10 | var clusterOptions = { 11 | type: "MEMORY", 12 | name: "TestClass" 13 | }; 14 | 15 | var previousClustersLength = db.clusters.length; 16 | 17 | db.addDataCluster(clusterOptions, function(err, clusterId) { 18 | 19 | assert(!err, "Error while adding the data cluster: " + err); 20 | 21 | db.reload(function(err, result) { 22 | 23 | assert(!err, "Error while reloading the database: " + err); 24 | 25 | assert.equal(previousClustersLength + 1, db.clusters.length); 26 | 27 | db.command("create class TestClass cluster " + db.clusters[5].id, function(err, result) { 28 | 29 | assert(!err, "Error while creating class: " + err); 30 | 31 | var firstDocData = "TestClass@nick:\"ThePresident\",subdoc:(name:\"subdoc name\",id_field:42),follows:[],followers:[],name:\"Barack\",surname:\"Obama\",location:#3:2,invitedBy:,salary_cloned:,salary:120.3f"; 32 | var data = new Buffer(firstDocData.length); 33 | data.write(firstDocData); 34 | 35 | var recordData = { 36 | clusterId: clusterId, 37 | content: data, 38 | type: "d", 39 | dataSegmentId: db.getClusterById(clusterId).dataSegmentId 40 | }; 41 | 42 | console.log("Creating record: " + firstDocData); 43 | 44 | db.createRecord(recordData, function(err, result) { 45 | 46 | assert(!err, "Error while creating record: " + err); 47 | 48 | var clusterPosition = result.position; 49 | var rid = "#" + clusterId + ":" + clusterPosition; 50 | 51 | console.log("Loading record " + rid); 52 | 53 | db.loadRecord(rid, function(err, result) { 54 | 55 | assert(!err, "Error while loading record: " + err); 56 | 57 | console.log("Loaded record " + JSON.stringify(result)); 58 | 59 | var first_version = result["@version"]; 60 | 61 | // assert.equal(firstDocData, result.content.toString().trim()); 62 | 63 | var secondDocData = "TestClass@nick:\"TheVicePresident\",subdoc:(name:\"subdoc name\",id_field:42),follows:[],followers:[],name:\"Joe\",surname:\"Biden\",location:#3:2,invitedBy:,salary_cloned:,salary:120.3f"; 64 | var data = new Buffer(secondDocData.length); 65 | data.write(secondDocData); 66 | 67 | var updateRecordPreviousVersion = { 68 | clusterId: clusterId, 69 | clusterPosition: clusterPosition, 70 | content: data, 71 | type: "d", 72 | version: first_version 73 | }; 74 | 75 | console.log("Updating record: " + secondDocData); 76 | 77 | db.updateRecord(updateRecordPreviousVersion, function(err, result) { 78 | 79 | assert(!err, "Error while updating record (1st time): " + JSON.stringify(err)); 80 | 81 | assert.equal(result.version, updateRecordPreviousVersion.version + 1); 82 | 83 | console.log("Updated record to version " + result.version); 84 | 85 | var currentVersion = result.version; 86 | delete updateRecordPreviousVersion.version; 87 | 88 | console.log("Updating record again: " + secondDocData); 89 | 90 | db.updateRecord(updateRecordPreviousVersion, function(err, result) { 91 | 92 | assert(!err, "Error while updating record (2nd time): " + JSON.stringify(err)); 93 | 94 | console.log("Updated record to version " + result.version); 95 | 96 | var deleteRecord = { 97 | clusterId: updateRecordPreviousVersion.clusterId, 98 | clusterPosition: updateRecordPreviousVersion.clusterPosition, 99 | version: currentVersion 100 | }; 101 | 102 | var rid = "#" + deleteRecord.clusterId + ":" + deleteRecord.clusterPosition; 103 | console.log("Deleting record " + rid + "..."); 104 | 105 | db.deleteRecord(deleteRecord, function(err, result) { 106 | 107 | assert(!err, "Error while deleting record: " + JSON.stringify(err)); 108 | 109 | assert.equal(1, result.status); 110 | 111 | console.log("Deleted record."); 112 | db.command("drop class TestClass", function(err, result) { 113 | assert(result); 114 | 115 | 116 | db.dropDataCluster(clusterId, function() { 117 | db.close(); 118 | }); 119 | }); 120 | }); 121 | }); 122 | }); 123 | }); 124 | }); 125 | }); 126 | }); 127 | }); 128 | }); 129 | 130 | -------------------------------------------------------------------------------- /test/test_record_load.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var _ = require("lodash"); 3 | 4 | var db = require("./setup_db.js").db; 5 | 6 | db.open(function(err, result) { 7 | 8 | assert(!err, "Error while opening the database: " + err); 9 | 10 | var userCluster = db.getClusterByClass("OUser"); 11 | var roleCluster = db.getClusterByClass("ORole"); 12 | 13 | assert(userCluster.id > 0); 14 | assert(roleCluster.id > 0); 15 | 16 | var urid = "#" + userCluster.id + ":" + 0; 17 | var rrid = "#" + roleCluster.id + ":" + 0; 18 | 19 | db.loadRecord(urid, function(err, record) { 20 | 21 | assert(!err, "Error while loading record: " + err); 22 | assert(record, "Null record? Where is the OUser " + urid); 23 | console.log("User loaded: " + JSON.stringify(record)); 24 | 25 | var newUser = { 26 | "@type": "d", 27 | "@class": "OUser", 28 | "name": "anotheruser", 29 | "password": "password", 30 | "status": "ACTIVE", 31 | "roles": [rrid] 32 | }; 33 | 34 | console.log("Saving new user: " + JSON.stringify(newUser)); 35 | db.save(newUser, function(err, newUser) { 36 | 37 | assert(!err, "Error while saving record " + err); 38 | console.log("Saved new record: " + JSON.stringify(newUser)); 39 | 40 | var newUserRid = newUser["@rid"]; 41 | 42 | console.log("Deleting record: " + newUserRid); 43 | db.delete(newUser, function(err) { 44 | 45 | assert(!err, "Error while deleting record " + err); 46 | console.log("Deleted record: " + newUserRid); 47 | 48 | console.log("Trying to load the deleted record: " + newUserRid); 49 | db.loadRecord(newUserRid, function(err, record) { 50 | 51 | // expect error 52 | assert(err, "I should have received an error at this point."); 53 | // and an empty result 54 | assert(!record); 55 | console.log("Just perfect: no more trace of record " + newUserRid); 56 | 57 | db.close(); 58 | }); 59 | }); 60 | }); 61 | }); 62 | }); 63 | 64 | -------------------------------------------------------------------------------- /test/test_records_in_cluster.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var _ = require("lodash"); 3 | 4 | var db = require("./setup_db.js").db; 5 | 6 | db.open(function(err, result) { 7 | 8 | assert(!err, "Error while opening the database: " + err); 9 | 10 | var cluster = db.getClusterByClass("OUser"); 11 | 12 | db.countRecordsInCluster(cluster.name, function(err, count) { 13 | 14 | assert.equal(3, count); 15 | 16 | db.close(); 17 | }); 18 | 19 | }); -------------------------------------------------------------------------------- /test/test_select_flatten.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var parser = require("../lib/orientdb/connection/parser"); 3 | var _ = require("lodash"); 4 | 5 | var orient = require("../lib/orientdb"), 6 | GraphDb = orient.GraphDb, 7 | Server = orient.Server; 8 | 9 | var serverConfig = require("../config/test/serverConfig"); 10 | var dbConfig = require("../config/test/dbConfig"); 11 | 12 | var server = new Server(serverConfig); 13 | var graphdb = new GraphDb("temp", server, dbConfig); 14 | 15 | graphdb.open(function(err) { 16 | assert(!err, err); 17 | 18 | graphdb.createVertex({}, function(err, vertex1) { 19 | assert(!err, err); 20 | 21 | assert(!_.isUndefined(vertex1["@rid"])); 22 | 23 | graphdb.createVertex({}, function(err, vertex2) { 24 | assert(!err, err); 25 | 26 | assert(!_.isUndefined(vertex2["@rid"])); 27 | 28 | graphdb.createEdge(vertex1, vertex2, { label: "select_flatten" }, function(err, edge) { 29 | assert(!err, err); 30 | 31 | assert(!_.isUndefined(edge["@rid"])); 32 | 33 | graphdb.command("select flatten(out[label = \"select_flatten\"].in) from V", function(err, results) { 34 | assert(!err, err); 35 | 36 | assert.equal(1, results.length); 37 | assert(!_.isUndefined(results[0]["@rid"])); 38 | assert(!_.isUndefined(results[0]["@type"])); 39 | 40 | graphdb.close() 41 | }); 42 | }); 43 | }); 44 | }); 45 | }); 46 | 47 | -------------------------------------------------------------------------------- /test/test_transactions.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var _ = require("lodash"); 3 | var async = require("async"); 4 | 5 | var db = require("./setup_db.js").db; 6 | 7 | var userClusterId, linkClusterId, transaction, 8 | firstExistingDoc, firstExistingDocRID, firstExistingDocVersion, 9 | secondExistingDoc, secondExistingDocRID, 10 | firstNewDoc, firstNewDocRID, 11 | secondNewDoc, secondNewDocRID, 12 | newLink, newLinkRID; 13 | 14 | async.waterfall([ 15 | function(callback) { 16 | db.open(callback); 17 | }, 18 | function(callback) { 19 | prepareDatabase(callback); 20 | }, 21 | function(_userClusterId, _linkClusterId, callback) { 22 | userClusterId = _userClusterId; 23 | linkClusterId = _linkClusterId; 24 | 25 | callback(); 26 | }, 27 | function(callback) { 28 | firstExistingDoc = { 29 | "@class": "user", 30 | name: "first existing user name", 31 | surname: "first existing user surname" 32 | }; 33 | 34 | db.save(firstExistingDoc, callback); 35 | }, 36 | function(_firstExistingDoc, callback) { 37 | firstExistingDoc = _firstExistingDoc; 38 | firstExistingDocRID = _firstExistingDoc["@rid"]; 39 | firstExistingDocVersion = _firstExistingDoc["@version"]; 40 | 41 | assert.equal(0, firstExistingDoc["@version"]); 42 | 43 | callback(); 44 | }, 45 | function(callback) { 46 | secondExistingDoc = { 47 | "@class": "user", 48 | name: "second existing user name", 49 | surname: "second existing user surname" 50 | }; 51 | 52 | db.save(secondExistingDoc, callback); 53 | }, 54 | function(_secondExistingDoc, callback) { 55 | secondExistingDoc = _secondExistingDoc; 56 | secondExistingDocRID = _secondExistingDoc["@rid"]; 57 | 58 | assert.equal(0, secondExistingDoc["@version"]); 59 | 60 | callback(); 61 | }, 62 | function(callback) { 63 | transaction = db.begin(); 64 | 65 | assert.equal(-1, transaction.clusterPosition); 66 | assert.equal(0, transaction.docs.length); 67 | 68 | firstNewDoc = { 69 | "@class": "user", 70 | name: "first", 71 | surname: "first" 72 | }; 73 | 74 | db.save(firstNewDoc, transaction, callback); 75 | }, 76 | function(_firstNewDoc, callback) { 77 | firstNewDoc = _firstNewDoc; 78 | 79 | assert.equal(-2, transaction.clusterPosition); 80 | assert.equal(1, transaction.docs.length); 81 | 82 | firstNewDoc.name = "first doc name"; 83 | firstNewDoc.surname = "first doc surname"; 84 | 85 | db.save(firstNewDoc, transaction, callback); 86 | }, 87 | function(_firstNewDoc, callback) { 88 | firstNewDoc = _firstNewDoc; 89 | firstNewDocRID = _firstNewDoc["@rid"]; 90 | 91 | assert.equal(-2, transaction.clusterPosition); 92 | assert.equal(1, transaction.docs.length); 93 | 94 | secondNewDoc = { 95 | "@class": "user", 96 | name: "second doc name", 97 | surname: "second doc surname" 98 | }; 99 | db.save(secondNewDoc, transaction, callback); 100 | }, 101 | function(_secondNewDoc, callback) { 102 | secondNewDoc = _secondNewDoc; 103 | secondNewDocRID = _secondNewDoc["@rid"]; 104 | 105 | assert.equal(-3, transaction.clusterPosition); 106 | assert.equal(2, transaction.docs.length); 107 | 108 | newLink = { 109 | "@class": "link", 110 | link_to_first: firstNewDoc["@rid"], 111 | link_to_second: secondNewDoc["@rid"], 112 | sub_section: { 113 | section_name: "sub link to first doc", 114 | linked_doc: firstNewDoc["@rid"] 115 | } 116 | }; 117 | 118 | firstNewDoc.link_to_second = secondNewDoc["@rid"]; 119 | secondNewDoc.link_to_first = firstNewDoc["@rid"]; 120 | 121 | db.save(newLink, transaction, callback); 122 | }, 123 | function(_newLink, callback) { 124 | newLink = _newLink; 125 | newLinkRID = _newLink["@rid"]; 126 | 127 | callback(); 128 | }, 129 | function(callback) { 130 | firstExistingDoc.name = "new " + firstExistingDoc.name; 131 | firstExistingDoc.surname = "new " + firstExistingDoc.surname; 132 | 133 | db.save(firstExistingDoc, transaction, callback); 134 | }, 135 | function(_firstExistingDoc, callback) { 136 | firstExistingDoc = _firstExistingDoc; 137 | 138 | callback(); 139 | }, 140 | function(callback) { 141 | db.delete(secondExistingDoc, transaction, callback); 142 | }, 143 | function(callback) { 144 | assert.equal(-4, transaction.clusterPosition); 145 | assert.equal(5, transaction.docs.length); 146 | 147 | db.commit(transaction, callback); 148 | }, 149 | function(txResult, callback) { 150 | 151 | assert.equal(3, txResult.numberOfRecordsCreated); 152 | 153 | txResult.recordsCreated.sort(function(a, b) { 154 | return ("" + a.fromClusterId + a.fromClusterPosition).localeCompare("" + b.fromClusterId + b.fromClusterPosition); 155 | }); 156 | assert.equal(userClusterId, txResult.recordsCreated[0].fromClusterId); 157 | assert.equal(-2, txResult.recordsCreated[0].fromClusterPosition); 158 | assert.equal(userClusterId, txResult.recordsCreated[0].toClusterId); 159 | assert.equal(2, txResult.recordsCreated[0].toClusterPosition); 160 | assert.equal(userClusterId, txResult.recordsCreated[1].fromClusterId); 161 | assert.equal(-3, txResult.recordsCreated[1].fromClusterPosition); 162 | assert.equal(userClusterId, txResult.recordsCreated[1].toClusterId); 163 | assert.equal(3, txResult.recordsCreated[1].toClusterPosition); 164 | assert.equal(linkClusterId, txResult.recordsCreated[2].fromClusterId); 165 | assert.equal(-4, txResult.recordsCreated[2].fromClusterPosition); 166 | assert.equal(linkClusterId, txResult.recordsCreated[2].toClusterId); 167 | assert.equal(0, txResult.recordsCreated[2].toClusterPosition); 168 | 169 | assert.equal(2, txResult.numberOfRecordsUpdated); 170 | 171 | txResult.recordsUpdated.sort(function(a, b) { 172 | return ("" + a.clusterId + a.clusterPosition).localeCompare("" + b.clusterId + b.clusterPosition); 173 | }); 174 | 175 | assert.equal(userClusterId, txResult.recordsUpdated[0].clusterId); 176 | assert.equal(0, txResult.recordsUpdated[0].clusterPosition); 177 | assert.equal(1, txResult.recordsUpdated[0].version); 178 | assert.equal(userClusterId, txResult.recordsUpdated[1].clusterId); 179 | assert.equal(2, txResult.recordsUpdated[1].clusterPosition); 180 | assert.equal(1, txResult.recordsUpdated[1].version); 181 | 182 | assert.equal(firstExistingDoc["@rid"], firstExistingDocRID); 183 | assert.equal(firstExistingDoc["@version"], firstExistingDocVersion + 1); 184 | assert(firstNewDoc["@rid"] !== firstNewDocRID); 185 | assert.equal(firstNewDoc["@version"], 1); 186 | assert(secondNewDoc["@rid"] !== secondNewDoc); 187 | assert.equal(secondNewDoc["@version"], 0); 188 | assert(newLink["@rid"] !== newLinkRID); 189 | assert.equal(newLink["@version"], 0); 190 | 191 | assert.equal(newLink.link_to_first, firstNewDoc["@rid"]); 192 | assert.equal(newLink.sub_section.linked_doc, firstNewDoc["@rid"]); 193 | assert.equal(newLink.link_to_second, secondNewDoc["@rid"]); 194 | assert.equal(firstNewDoc.link_to_second, secondNewDoc["@rid"]); 195 | assert.equal(secondNewDoc.link_to_first, firstNewDoc["@rid"]); 196 | 197 | db.loadRecord(secondExistingDoc["@rid"], function(err) { 198 | assert(err); 199 | }); 200 | 201 | unprepareDatabase(callback); 202 | }, 203 | function() { 204 | db.close(); 205 | } 206 | ], function(err) { 207 | assert(!err, err); 208 | 209 | db.close() 210 | }); 211 | 212 | function prepareDatabase(callback) { 213 | db.createClass("user", function(err, userClusterId) { 214 | if (err) return callback(err); 215 | 216 | db.createClass("link", function(err, linkClusterId) { 217 | if (err) return callback(err); 218 | 219 | callback(null, userClusterId, linkClusterId); 220 | }); 221 | }); 222 | } 223 | 224 | function unprepareDatabase(callback) { 225 | db.dropClass("user", function(err) { 226 | if (err) return callback(err); 227 | 228 | db.dropClass("link", callback); 229 | }); 230 | } -------------------------------------------------------------------------------- /test/test_z_shutdown.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | 3 | var orient = require("../lib/orientdb"), 4 | Server = orient.Server; 5 | 6 | var serverConfig = require('../config/test/serverConfig'); 7 | 8 | var server = new Server(serverConfig); 9 | 10 | 11 | server.connect(function(err, sessionId) { 12 | 13 | assert(!err, "Error while connecting to the server: " + JSON.stringify(err)); 14 | 15 | server.shutdown(function(err) { 16 | 17 | assert(!err, "Error while disconnecting from the server: " + err); 18 | 19 | console.log('Server shut down'); 20 | }); 21 | }); 22 | 23 | --------------------------------------------------------------------------------