What parts of ArangoDB are done with Node-GYP

空扰寡人 提交于 2019-12-23 01:21:31

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/31534200/what-parts-of-arangodb-are-done-with-node-gyp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!