/** * Delete document or folder. * * @throws IOException I/O exception */ protected void del() throws IOException { final Session session = http.session(); session.execute(new Open(db)); session.execute(new Delete(path)); // create dummy, if parent is an empty folder final int ix = path.lastIndexOf(SEP); if (ix > 0) createDummy(path.substring(0, ix)); }
/** * Queries empty content. * * @throws IOException I/O exception */ @Test public void queryEmptyBinary() throws IOException { session.execute("create db " + NAME); session.store("X", new ArrayInput("")); assertEqual("", session.execute("xquery " + RAW + _DB_RETRIEVE.args(NAME, "X"))); assertEqual("", session.query(RAW + _DB_RETRIEVE.args(NAME, "X")).execute()); final Query q = session.query(RAW + _DB_RETRIEVE.args(NAME, "X")); assertTrue(q.more()); assertEqual("", q.next()); assertNull(q.next()); }
/** * Stores binary content in the database. * * @throws IOException I/O exception */ @Test public final void store() throws IOException { session.execute("create db " + NAME); session.store("X", new ArrayInput("!")); assertEqual("true", session.query(_DB_IS_RAW.args(NAME, "X")).execute()); session.store("X", new ArrayInput("")); assertEqual("", session.query(_DB_RETRIEVE.args(NAME, "X")).execute()); session.store("X", new ArrayInput(new byte[] {0, 1, -1})); assertEqual( "0001FF", session.query("xs:hexBinary(" + _DB_RETRIEVE.args(NAME, "X") + ')').execute()); session.execute("drop db " + NAME); }
/** * Rename document or folder. * * @param n new name * @throws IOException I/O exception */ protected void rename(final String n) throws IOException { final Session session = http.session(); session.execute(new Open(db)); session.execute(new Rename(path, n)); // create dummy, if old parent is an empty folder final int i1 = path.lastIndexOf(SEP); if (i1 > 0) createDummy(path.substring(0, i1)); // delete dummy, if new parent is an empty folder final int i2 = n.lastIndexOf(SEP); if (i2 > 0) deleteDummy(n.substring(0, i2)); }
/** Clean up method. */ @After public void cleanUp() { try { testSession.close(); adminSession.execute(new DropDB(RENAMED)); adminSession.execute(new DropDB(NAME)); adminSession.close(); // give the server some time to clean up the sessions before next test Performance.sleep(100); } catch (final Exception ex) { fail(Util.message(ex)); } }
/** * Assumes that this command is successful. * * @param cmd command reference * @param s session */ private static void ok(final Command cmd, final Session s) { try { s.execute(cmd); } catch (final IOException ex) { fail(Util.message(ex)); } }
/** * Assumes that this command fails. * * @param cmd command reference * @param s session */ private static void no(final Command cmd, final Session s) { try { s.execute(cmd); fail("\"" + cmd + "\" was supposed to fail."); } catch (final IOException ignored) { } }
/** * Performs the specified query n times and and returns the result. * * @param query query to be evaluated * @param n number of runs * @return resulting string * @throws IOException I/O exception */ protected static String eval(final int n, final String query) throws IOException { // loop through number of runs for a single query check(); String result = ""; for (int rn = 0; rn < n; ++rn) result = session.execute(new XQuery(query)); return result; }
/** * Runs a query with additional serialization parameters. * * @throws IOException I/O exception */ @Test public void querySerial1() throws IOException { session.execute("set serializer wrap-prefix=db,wrap-uri=ns"); final Query query = session.query(WRAPPER + "()"); assertTrue("Result expected.", query.more()); assertEqual("<db:results xmlns:db=\"ns\"/>", query.next()); assertFalse("No result expected.", query.more()); }
/** * Stores binary content. * * @throws IOException I/O exception */ @Test public void storeBinary() throws IOException { session.execute("create db " + NAME); session.store("X", new ArrayInput(new byte[] {-128, -2, -1, 0, 1, 127})); assertEqual( "-128 -2 -1 0 1 127", session.query(_CONVERT_BINARY_TO_BYTES.args(_DB_RETRIEVE.args(NAME, "X"))).execute()); }
/** * Adds documents to a database. * * @throws IOException I/O exception */ @Test public final void add() throws IOException { session.execute("create db " + NAME); session.add(NAME, new ArrayInput("<X/>")); assertEqual("1", session.query("count(" + _DB_OPEN.args(NAME) + ')').execute()); for (int i = 0; i < 9; i++) session.add(NAME, new ArrayInput("<X/>")); assertEqual("10", session.query("count(" + _DB_OPEN.args(NAME) + ')').execute()); }
/** Stops a session. */ @After public final void stopSession() { try { if (cleanup) session.execute(new DropDB(NAME)); session.close(); } catch (final IOException ex) { fail(Util.message(ex)); } }
/** * Initializes the benchmark. * * @throws IOException I/O exception */ @BeforeClass public static void init() throws IOException { // check if server is (not) running final int sp = context.soptions.get(StaticOptions.SERVERPORT); server = local || BaseXServer.ping(S_LOCALHOST, sp) ? null : createServer(); session = local ? new LocalSession(context) : createClient(); // create test database session.execute(new Set(MainOptions.QUERYINFO, true)); }
/** * Replaces documents in a database. * * @throws IOException I/O exception */ @Test public final void replace() throws IOException { session.execute("create db " + NAME); assertEqual("0", session.query("count(" + _DB_OPEN.args(NAME) + ')').execute()); session.replace(NAME, new ArrayInput("<X/>")); assertEqual("1", session.query("count(" + _DB_OPEN.args(NAME) + ')').execute()); session.replace(NAME + '2', new ArrayInput("<X/>")); assertEqual("2", session.query("count(" + _DB_OPEN.args(NAME) + ')').execute()); session.replace(NAME + '2', new ArrayInput("<X/>")); assertEqual("2", session.query("count(" + _DB_OPEN.args(NAME) + ')').execute()); }
/** * Queries binary content (works only if output stream is specified). * * @throws IOException I/O exception */ @Test public void queryBinary() throws IOException { if (out == null) return; session.execute("create db " + NAME); final byte[] tmp = {0, 1, 2, 127, 0, -1, -2, -128}; session.store("X", new ArrayInput(tmp)); final String retr = _DB_RETRIEVE.args(NAME, "X"); // check command session.execute("xquery " + RAW + retr + ',' + retr); assertTrue(eq(out.toArray(), concat(tmp, tmp))); out.reset(); // check query execution session.query(RAW + retr + ',' + retr).execute(); assertTrue(eq(out.toArray(), concat(tmp, tmp))); out.reset(); // check iterator final Query q = session.query(RAW + retr + ',' + retr); q.next(); assertTrue(eq(out.toArray(), tmp)); out.reset(); q.next(); assertTrue(eq(out.toArray(), tmp)); assertNull(q.next()); }
/** * Runs a query command and retrieves the result as string. * * @throws IOException I/O exception */ @Test public final void command() throws IOException { session.execute("set serializer wrap-prefix=,wrap-uri="); assertEqual("A", session.execute("xquery 'A'")); }
/** * Adds a file with missing input. * * @throws IOException I/O exception */ @Test(expected = BaseXException.class) public final void replaceNoInput() throws IOException { session.execute("create db " + NAME); session.replace("", new ArrayInput("")); }
/** * Stores binary content in the database. * * @throws IOException I/O exception */ @Test(expected = BaseXException.class) public final void storeInvalid() throws IOException { session.execute("create db " + NAME); session.store("..", new ArrayInput("!")); }
public static void main(String[] args) { for (int i = 0; i < args.length; ++i) { if ("-endpoint".equals(args[i])) { endpoint = args[++i]; } else if ("-keyspace".equals(args[i])) { keyspace = args[++i]; } else if ("-table".equals(args[i])) { table = args[++i]; } else { System.err.println("ConsistencyTest -endpoint hostname -keyspace keyspace -table table"); } } if (endpoint == null || keyspace == null || table == null) { System.err.println("ConsistencyTest -endpoint hostname -keyspace keyspace -table table"); System.exit(1); } String[] endpoints = endpoint.split(":"); try { cluster = Cluster.builder() .addContactPoints(endpoints) .withRetryPolicy(DefaultRetryPolicy.INSTANCE) .withLoadBalancingPolicy(new TokenAwarePolicy(new DCAwareRoundRobinPolicy())) .build(); session = cluster.connect(keyspace); // work with ONE for both write and read long s1 = System.currentTimeMillis(); Statement insert = QueryBuilder.insertInto(table) .value("user_id", 100) .value("fname", "john") .value("lname", "yamada") .value("number", 100); insert.setConsistencyLevel(ConsistencyLevel.ONE); session.execute(insert); long e1 = System.currentTimeMillis(); System.out.println("write ONE time taken (ms) : " + (e1 - s1)); long s2 = System.currentTimeMillis(); Statement select = QueryBuilder.select().all().from(table).where(QueryBuilder.eq("user_id", 100)); select.setConsistencyLevel(ConsistencyLevel.ONE); results = session.execute(select); for (Row row : results) { System.out.format( "%d %s %s %d\n", row.getInt("user_id"), row.getString("fname"), row.getString("lname"), row.getInt("number")); } long e2 = System.currentTimeMillis(); System.out.println("read ONE time taken (ms) : " + (e2 - s2)); Statement delete = QueryBuilder.delete().from(table).where(QueryBuilder.eq("user_id", 100)); results = session.execute(delete); // work with QUORUM for both write and read long s3 = System.currentTimeMillis(); Statement insert2 = QueryBuilder.insertInto(table) .value("user_id", 200) .value("fname", "john") .value("lname", "yamada") .value("number", 100); insert2.setConsistencyLevel(ConsistencyLevel.QUORUM); session.execute(insert2); long e3 = System.currentTimeMillis(); System.out.println("write QUORUM time taken (ms) : " + (e3 - s3)); long s4 = System.currentTimeMillis(); Statement select2 = QueryBuilder.select().all().from(table).where(QueryBuilder.eq("user_id", 200)); select2.setConsistencyLevel(ConsistencyLevel.QUORUM); results = session.execute(select2); for (Row row : results) { System.out.format( "%d %s %s %d\n", row.getInt("user_id"), row.getString("fname"), row.getString("lname"), row.getInt("number")); } long e4 = System.currentTimeMillis(); System.out.println("read QUORUM time taken (ms) : " + (e4 - s4)); Statement delete2 = QueryBuilder.delete().from(table).where(QueryBuilder.eq("user_id", 200)); results = session.execute(delete2); // Clean up the connection by closing it cluster.close(); } catch (Exception e) { e.printStackTrace(); } }
/** * Retrieves empty content. * * @throws IOException I/O exception */ @Test public void retrieveEmpty() throws IOException { session.execute("create db " + NAME); session.store("X", new ArrayInput("")); assertEqual("", session.execute("retrieve X")); }
/** * Runs a query command and retrieves the result as string. * * @throws IOException I/O exception */ @Test(expected = BaseXException.class) public final void commandErr() throws IOException { session.execute("1,<a/>+''"); }
/** * Runs an erroneous query command. * * @throws IOException I/O exception */ @Test(expected = BaseXException.class) public final void commandError() throws IOException { session.execute("xquery ("); }
/** * Runs a query command and wraps the result. * * @throws IOException I/O exception */ @Test public final void commandSerial2() throws IOException { assertEqual( "<db:results xmlns:db=\"ns\">" + " <db:result>1</db:result>" + "</db:results>", session.execute("xquery " + WRAPPER + '1')); }
/** * Adds a file with an invalid file name. * * @throws IOException I/O exception */ @Test(expected = BaseXException.class) public final void addNameErr() throws IOException { session.execute("create db " + NAME); session.add("", new ArrayInput("<X/>")); }
/** * Creates or opens the test database. * * @throws IOException I/O exception */ private static void check() throws IOException { session.execute(new Check(INPUT)); }
/** * Runs a query command and wraps the result. * * @throws IOException I/O exception */ @Test public final void commandSerial1() throws IOException { session.execute("set serializer wrap-prefix=db,wrap-uri=ns"); assertEqual("<db:results xmlns:db=\"ns\"/>", session.execute("xquery ()")); }