Ejemplo n.º 1
0
  @Test
  public void updateDocuments() throws IOException {

    // System.out.println("\t\tAdd address and status to jdoe");
    // System.out.println("before :\t" + mainColl.findById(new HValue("jdoe")));

    Document doc = mainColl.findById(new HValue("jdoe"));
    assertNull(doc.getMap("address"));

    // create a mutation
    DocumentMutation mutation =
        new HDocumentMutation()
            .set("active", true)
            .set("address.line", "1015 15th Avenue")
            .set("address.city", "Redwood City")
            .set("address.zip", 94065);

    mainColl.update(new HValue("jdoe"), mutation);
    mainColl.flush();

    // System.out.println("after :\t\t" + mainColl.findById(new HValue("jdoe")));

    doc = mainColl.findById(new HValue("jdoe"));
    assertEquals(3, doc.getMap("address").size());

    // System.out.println("\n\n\t\tAppend new interests to users");

    doc = mainColl.findById(new HValue("jdoe"));
    assertNull(doc.getList("interests"));
    doc = mainColl.findById(new HValue("mdupont"));
    assertEquals(3, doc.getList("interests").size());

    // create a mutation
    mutation =
        new HDocumentMutation().append("interests", Arrays.asList(new String[] {"development"}));

    mainColl.update(new HValue("jdoe"), mutation);
    mainColl.update(new HValue("mdupont"), mutation);
    mainColl.flush();

    // System.out.println("after :\t\t" + mainColl.findById(new HValue("jdoe"), "first_name",
    // "last_name", "interests"));
    // System.out.println("after :\t\t" + mainColl.findById(new HValue("mdupont"), "first_name",
    // "last_name", "interests"));

    doc = mainColl.findById(new HValue("jdoe"));
    assertEquals(1, doc.getList("interests").size());
    doc = mainColl.findById(new HValue("mdupont"));
    assertEquals(4, doc.getList("interests").size());

    // System.out.println("\n\n\t\tRemove attributes (dob)");
    // System.out.println("before :\t" + mainColl.findById(new HValue("jdoe")));

    doc = mainColl.findById(new HValue("jdoe"));
    assertTrue(doc.getDate("dob").compareTo(ODate.parse("1970-06-23")) == 0);

    // create a mutation
    mutation = new HDocumentMutation().delete("dob");
    mainColl.update(new HValue("jdoe"), mutation);
    mainColl.flush();

    // System.out.println("after :\t\t" + mainColl.findById(new HValue("jdoe")));

    doc = mainColl.findById(new HValue("jdoe"));
    assertNull(doc.getDate("dob"));
  }
Ejemplo n.º 2
0
  @Test
  public void queryDocuments() throws Exception {

    // get a single document
    Document record = mainColl.findById(new HValue("mdupont"));
    assertEquals("mdupont", record.getIdString());
    assertEquals("Maxime", record.getString("first_name"));
    assertEquals("Dupont", record.getString("last_name"));

    // get a single document with project
    record = mainColl.findById(new HValue("mdupont"), "last_name");
    assertEquals("mdupont", record.getIdString());
    assertEquals(null, record.getString("first_name"));
    assertEquals("Dupont", record.getString("last_name"));

    // get single document and map it to the bean
    User user = mainColl.findById(new HValue("alehmann")).toJavaBean(User.class);
    assertEquals("alehmann", user.getId());
    assertEquals("Andrew", user.getFirstName());
    assertEquals("Lehmann", user.getLastName());

    // all records in the table
    DocumentStream rs = mainColl.find();
    Iterator<? extends Document> itrs = rs.iterator();
    Document readRecord;
    Set<Document> readRecords = Sets.newHashSet();
    while (itrs.hasNext()) {
      readRecord = itrs.next();
      readRecords.add(readRecord);
      // System.out.println("\t" + readRecord);
    }
    assertEquals(5, readRecords.size());
    rs.close();

    // all records in the table with projection
    int cnt = 0;
    try (DocumentStream documentStream = mainColl.find("first_name", "last_name")) {
      for (Document doc : documentStream) {
        cnt++;
        assertEquals(3, doc.size());
        // System.out.println("\t" + doc);
      }
    }
    assertEquals(5, cnt);

    // all records and use a POJO
    // it is interesting to see how you can ignore unknown attributes with the JSON Annotations
    cnt = 0;
    try (DocumentStream documentStream = mainColl.find()) {
      for (Document doc : documentStream) {
        cnt++;
        User u = doc.toJavaBean(User.class);
        assertTrue(
            ImmutableList.<String>of("Doe", "Simon", "Smith", "Dupont", "Lehmann")
                .contains(u.getLastName()));
        // System.out.println("\t" + doc.toJavaBean(User.class));
      }
    }
    assertEquals(5, cnt);

    // find with condition
    // Condition equals a string
    QueryCondition condition =
        new HQueryCondition().is("last_name", QueryCondition.Op.EQUAL, "Doe").build();
    // System.out.println("\n\nCondition: " + condition);
    cnt = 0;
    try (DocumentStream documentStream = mainColl.find(condition)) {
      ((HDocumentStream) documentStream).explain();
      for (Document doc : documentStream) {
        cnt++;
        assertEquals("Doe", doc.getString("last_name"));
        // System.out.println("\t" + doc);
      }
    }
    assertEquals(1, cnt);

    // find with condition and projection
    // Condition equals a string
    condition = new HQueryCondition().is("last_name", QueryCondition.Op.EQUAL, "Doe").build();
    // System.out.println("\n\nCondition: " + condition);
    cnt = 0;
    try (DocumentStream documentStream = mainColl.find(condition, "last_name")) {
      for (Document doc : documentStream) {
        cnt++;
        assertEquals("Doe", doc.getString("last_name"));
        // System.out.println("\t" + doc);
      }
    }
    assertEquals(1, cnt);

    // Condition as date range
    condition =
        new HQueryCondition()
            .and()
            .is("dob", QueryCondition.Op.GREATER_OR_EQUAL, ODate.parse("1980-01-01"))
            .is("dob", QueryCondition.Op.LESS, ODate.parse("1981-01-01"))
            .close()
            .build();
    // System.out.println("\n\nCondition: " + condition);
    cnt = 0;
    try (DocumentStream documentStream = mainColl.find(condition)) {
      for (Document doc : documentStream) {
        cnt++;
        assertTrue(doc.getDate("dob").compareTo(ODate.parse("1980-01-01")) > 0);
        assertTrue(doc.getDate("dob").compareTo(ODate.parse("1981-01-01")) < 0);
        // System.out.println("\t" + doc);
      }
    }
    assertEquals(2, cnt);

    // Condition in sub document
    condition = new HQueryCondition().is("address.zip", QueryCondition.Op.EQUAL, 95109).build();
    // System.out.println("\n\nCondition: " + condition);
    cnt = 0;
    try (DocumentStream documentStream = mainColl.find(condition)) {
      for (Document doc : documentStream) {
        cnt++;
        assertEquals(95109, doc.getInt("address.zip"));
        // System.out.println("\t" + doc);
      }
    }
    assertEquals(1, cnt);

    // Contains a specific value in an array
    condition = new HQueryCondition().is("interests[]", QueryCondition.Op.EQUAL, "sports").build();
    // System.out.println("\n\nCondition: " + condition);
    cnt = 0;
    try (DocumentStream documentStream = mainColl.find(condition)) {
      for (Document doc : documentStream) {
        cnt++;
        assertTrue(doc.getList("interests").contains("sports"));
        // System.out.println("\t" + doc);
      }
    }
    assertEquals(2, cnt);

    // Contains a value at a specific index
    condition = new HQueryCondition().is("interests[0]", QueryCondition.Op.EQUAL, "sports").build();
    // System.out.println("\n\nCondition: " + condition);
    cnt = 0;
    try (DocumentStream documentStream =
        mainColl.find(condition, "first_name", "last_name", "interests")) {
      for (Document doc : documentStream) {
        cnt++;
        assertEquals("sports", doc.getString("interests[0]"));
        // System.out.println("\t" + doc);
      }
    }
    assertEquals(1, cnt);
  }