npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-cypher

v0.1.4

Published

Cypher-only interface to neo4j over http, optimized for volume. Configurably batches and retries items to increase overall throughput.

Downloads

20

Readme

node-cypher

Node Cypher is a cypher-only http client factory for neo4j that allows you to configurably optimize for individual query speed or overall query volume/throughput. Queries submitted in series should be treated as indepedent transactions. They should not be interdependent, or referential.

The Interface

var cypher = require('node-cypher');

cypher.createClient('http://localhost:7474',function(err,client){
  client.query("MATCH (n:Person) RETURN count(n)",{},function(err,result){
    ...
  })
})

Why

By default, node-cypher will make an individual request per query and does not throttle the quantity of outgoing requests in any way. In the case that you are going to be hitting Neo4j with a high volume of requests, you can limit the amount of concurrent outgoing requests, and optimize the number of queries run in a batch per request.

In any situation where the client is committing a single statement, the normal cypher endpoint will be used instead of the batch endpoint. With the default configuration, calling "query" 1000 times will result in 1000 individual concurrent requests, each committing a single cypher statement. This number of small concurrent http transactions can create load on Neo4j that can negatively impact the speed of each query individualy. This is due to a combination of locking and Jetty's (neo4j's http server) need to keep a thread and file descriptor allocated for each pending request during these consistency checks.

Normally, one would directly use the 'batch' or 'transactional' rest APIs in order to optimize insertion at volume. However, these come with some caveats when your queries are meant to be independent transactions, and not one large transaction as a whole.

  1. You could manually bundle up your requests into a batch, or 2) iteratively hit the transactional cypher endpoint. Either way, in the context of a batch or an open transaction, you are subject to a single failed request causing the entire lot to be rolled back. This isn't always desired, should your small transactions not rely on each other.

node-cypher allows you to limit the number of outgoing requests and queue up pending queries against a pool of concurrent batch transactions. Meanwhile, if a batch fails, it intelligently reorders and retries uncommitted (but unproblematic) components of the batch in order to guarantee that each query that can be committed successfully, will be committed. Every failing component of the batch will be handled appropriately with its associated exception.

In the following example, a barrage of incoming requests will be distributed among a pool of ten concurrent batch operations, each submitting a maximum of 100 queries per batch.

var connOpts = {
  host: 'http://localhost:7474', 
  base: '/db/data'
};
var clientOpts = {
  concurrency: 10, // default=Infinity
  batchLimit: 100   // default=1
};
cypher.createClient(connOpts, clientOpts, function(err,client){
  
  for ( var i=0; i<= 10000; i++ ) {
    client.query("CREATE (n:Person{ct:{ct}}) RETURN count(n)",{ct:i},function(err,result){
      ...
    })
  }
  
})

It is important to remember that each individual query will not complete any faster than a single http request would under normal volume. However, under high volume, your overall throughput will increase dramatically.

ClientFactory

createClient(connectionOptions, [clientOptions,] callback )

create a cypher client with specified options. To batch or not to batch...

Arguments

  • connectionOptions - A URL string or object. Defaults: { host:'http://localhost:7474', base: '/db/data' }
  • clientOptions - An optional object containing batching and concurrency options. Defaults: { concurrency: Infinity, batchLimit: 1 }
  • callback(err, results) - An optional callback to run once the client has been initialized or an error should the handshake with neo not complete successfully.

Example

var connOpts = {
  host: 'http://localhost:7474', 
  base: '/db/data'
};
var clientOpts = {
  concurrency: 10, // default=Infinity
  batchLimit: 100   // default=1
};
cypher.createClient(connOpts, clientOpts, function(err,client){
  
  for ( var i=0; i<= 10000; i++ ) {
    client.query("CREATE (n:Person{ct:{ct}}) RETURN count(n)",{ct:i},function(err,result){
      ...
    })
  }
  
})

CypherClient

query(cypherQuery, parameters, [callback])

Run a query against the current client. The query will either execute immediately or will be queued against the client's pool of batch operations, depending on the client's configuration.

Arguments

  • cypherQuery - A string or Buffer reflecting a cypher statement
  • parameters - An object containing parameters passed to the cypher query. Must be provided, even if empty/null. Argument order is implied.
  • callback(err, results) - An optional callback to run once the query has been committed or has failed.

Example

client.query("CREATE (n:Person{ct:{ct}}) RETURN n",{ct:i},function(err,result){
  ...
})

multi([statements])

Explicitly execute a number of statements in a single batch commit if possible. Additional attempts may be made in order to handle failing components. Again, each query should be considered an independent transaction. The api is similar to redisClient.multi, and functions similarly as well.

Arguments

  • statements - An optional array of statements to initialize the multi. Ex: [{query:"",params:{},cb:fn}]

Example

cypher.createClient(connOpts, clientOpts, function(err,client){
  
  var multi = client.multi();
  multi.query("CREATE (n:Person{ct:{ct}}) RETURN n",{ct:1},function(err,result){
    ...
  })
  multi.query("CREATE (n:Person{ct:{ct}}) RETURN n",{ct:2},function(err,result){
    ...
  })
  multi.exec(function(err){
    // will fire once all the queries associated with this multi have been resolved as committed or failed.
  })
  
});