Example #1
0
  /**
   * 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));
  }
Example #2
0
 /**
  * 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());
 }
Example #3
0
 /**
  * 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);
 }
Example #4
0
  /**
   * 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));
  }
Example #5
0
 /** 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));
   }
 }
Example #6
0
 /**
  * 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));
   }
 }
Example #7
0
 /**
  * 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) {
   }
 }
Example #8
0
 /**
  * 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;
 }
Example #9
0
 /**
  * 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());
 }
Example #10
0
 /**
  * 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());
 }
Example #11
0
 /**
  * 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());
 }
Example #12
0
 /** 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));
   }
 }
Example #13
0
  /**
   * 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));
  }
Example #14
0
 /**
  * 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());
 }
Example #15
0
 /**
  * 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());
 }
Example #16
0
 /**
  * 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'"));
 }
Example #17
0
 /**
  * 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(""));
 }
Example #18
0
 /**
  * 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("!"));
 }
Example #19
0
  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();
    }
  }
Example #20
0
 /**
  * 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"));
 }
Example #21
0
 /**
  * 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/>+''");
 }
Example #22
0
 /**
  * Runs an erroneous query command.
  *
  * @throws IOException I/O exception
  */
 @Test(expected = BaseXException.class)
 public final void commandError() throws IOException {
   session.execute("xquery (");
 }
Example #23
0
 /**
  * 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'));
 }
Example #24
0
 /**
  * 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/>"));
 }
Example #25
0
 /**
  * Creates or opens the test database.
  *
  * @throws IOException I/O exception
  */
 private static void check() throws IOException {
   session.execute(new Check(INPUT));
 }
Example #26
0
 /**
  * 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 ()"));
 }