memgraph/tests/drivers/java/v1/MaxQueryLength.java
antonio2368 0bcc1d67bc
Add support for Bolt v4(.1) (#10)
* Added handshake support
* Add support for v4 hello and goodbye
* Add support for pulling n results
* Add support for transactions
* Add pull n for the dump
* Add support for NOOP
* Add support for multiple queries
* Update bolt session to support qid
* Update drivers test with multiple versions and go
* Extract failure handling into a function
* Use unique ptr instead of optional for query execution
* Destroy stream before query execution

Co-authored-by: Antonio Andelic <antonio.andelic@memgraph.io>
2020-10-16 12:49:33 +02:00

60 lines
2.0 KiB
Java

/**
* Determines how long could be a query executed
* from Java driver.
*
* Performs binary search until the maximum possible
* query size has found.
*/
import java.util.*;
import org.neo4j.driver.v1.*;
import org.neo4j.driver.v1.types.*;
import static org.neo4j.driver.v1.Values.parameters;
public class MaxQueryLength {
public static void main(String[] args) {
// init driver
Config config = Config.build().withoutEncryption().toConfig();
Driver driver = GraphDatabase.driver("bolt://localhost:7687",
AuthTokens.basic( "", "" ),
config);
// init query
int property_size = 0;
int min_len = 1;
int max_len = 100000;
String query_template = "CREATE (n {name:\"%s\"})";
int template_size = query_template.length() - 2; // because of %s
// binary search
while (true) {
property_size = (max_len + min_len) / 2;
try (Session session = driver.session()) {
String property_value = new String(new char[property_size])
.replace('\0', 'a');
String query = String.format(query_template, property_value);
session.run(query).consume();
if (min_len == max_len || property_size + 1 > max_len) {
break;
}
min_len = property_size + 1;
}
catch (Exception e) {
System.out.println(String.format(
"Query length: %d; Error: %s",
property_size + template_size, e));
max_len = property_size - 1;
}
}
// final result
System.out.println(
String.format("\nThe max length of a query executed from " +
"Java driver is: %s\n",
property_size + template_size));
// cleanup
driver.close();
}
}