1

I am figuring out the structure of ArangoDB to be sure whether it could be my long term solution.

One of my important questions is, what parts are made by node-gyp (or Node-Addon) - and how does the query-builder transforms JavaScript generated AQL queries to be fast as native?

I know I also could go deeper into the code myself, but I think it is much faster if some of the core developers would answer this (or explain how they expose functionality to JavaScript and AQL).

I think it is interesting for many people are responsible to analyze ArangoDB. (Our job is not to belief marketing phrases and benchmarks... we have to understand how it works).

My major goal is to look at ArangoDB as an JavaScript developer.

Danny
  • 1,078
  • 7
  • 22
  • 1
    looking at https://groups.google.com/forum/?hl=de#!topic/arangodb/mdn8OPJP9io it seams you can fire AQL out of JavaScript `db._query(AQL)` and parse the response by `res.json(results.toArray())`. But I am not sure whether it is only usable in FOXX or also in Node.js as a module !?? (needs clarification) –  Jul 21 '15 at 09:05
  • 1
    yes, it looks like it is executed 4x faster out of JavaScript. That's the reason I try to understand how this works - and I assume they use node-gyp maybe. – Danny Jul 21 '15 at 09:09

1 Answers1

4

ArangoDB is not made of node or gyp. But ArangoDB uses Google's V8 JavaScript engine to execute JavaScript code, the same as node.js does.

That means you can run user-defined JavaScript code inside ArangoDB, and it will be compiled to native code by V8 on the fly. Some of ArangoDB's own modules are written in JavaScript, too. Several third-party JavaScript modules, including a few modules used by or written for node.js and npm, are bundled with ArangoDB, too.

Regarding compatibility with node.js and npm modules:

modules from node.js and npm work in ArangoDB too as long as they do not rely on node.js internals or code from other modules that do. That means all modules that are JavaScript only and do not require any node specific stuff should work in ArangoDB. joi is a good example for this. node.js/npm modules that rely on node.js-specific object or C++ extensions for node.js will not work in ArangoDB.

ArangoDB itself is written in C++, with some its modules being written in JavaScript. ArangoDB's internals are made accessible to user-defined JavaScript code and the bundled ArangoDB JavaScript modules by V8 wrapper objects and functions. These functions are C++ functions that are exposed to JavaScript by telling V8 that they exist.

Here's an example: ArangoDB exposes a JavaScript object named db. This object has some predefined functions, e.g. _collection(<name>). When this function is called, this will effectively be a call to a C++ function, which can then handle its (JavaScript) arguments, which in this case should be a collection name string. Inside the C++ function there will then be a lookup for the collection of the specified name. If not found, the function will return a JavaScript null object. If found, the function will return a collection object, wrapped in a so-called V8 external. For the JavaScript code, this will look like a regular object, but this object has some C++ bindings again.

In order for all this to work, the server will register the db object in a V8 context at start, and also register the wrapper functions for all the object's methods. It will do so for other objects and functions, so there is a full JavaScript API for the server internals.

AQL, ArangoDB's query language, is written in C++ and will be executed as such. Some AQL functions and operators however are implemented in JavaScript. If an AQL query makes use of such function or operator, a query-specific JavaScript snippet will be generated, compiled on the fly using V8 and executed. Additionally, AQL queries can make use of user-defined JavaScript functions for calculations. These functions are regular JavaScript functions, which must be registered with a command before they can be used in a query. Invocation of these functions is as above, with a piece of dynamic JavaScript code being generated and executed to call the user-defined function.

Finally, ArangoDB's Foxx framework is written in JavaScript and allows defining HTTP routes in the ArangoDB server. The actions behind these routes are user-defined, and can have access to the server internals and database data via the beforementioned way.

stj
  • 9,037
  • 19
  • 33
  • 2
    Also, JavaScript in ArangoDB is written synchronously, which can be surprising to JS programmers new to ArangoDB. – Alan Plum Jul 21 '15 at 09:42
  • 2
    The query builder module (AQB) is just a string builder under the hood. The ArangoDB internals never see the query builder itself, just its output (an AQL string). – Alan Plum Jul 21 '15 at 09:44
  • +1 Awesome answer and important additional comments. Thats much stuff I can work with for getting deeper into things. Since asking, I tried to get into the graph also - I will create an additional question about that. – Danny Jul 21 '15 at 12:09
  • Also see offical docs about [Javascript Modules](https://docs.arangodb.com/ModuleJavaScript/index.html). `joi` is not the best example actually, because it's not pure JS - it [doesn't run in browsers](https://github.com/hapijs/joi/issues/154) out of the box. @pluma: to NodeJS developers synchronous execution can be surprising, but to JS programmers? – CodeManX Aug 03 '15 at 09:52
  • Also note this related blog article about the similarities and differences between ArangoDB and node.js, and how ArangoDB uses V8: https://www.arangodb.com/2015/08/running-v8-isolates-in-a-multi-threaded-arangodb-database/ – stj Aug 17 '15 at 20:13