Table.checkAndReplace()

Replaces a document, if the document to replace satisfies conditions that are in a QueryCondition object.

Operations Performed

  1. Verifies that the document with the specified ID exists.
    • If the ID does not exist, returns false.
    • If the ID does exist, proceeds to step 2.
  2. Evaluates the specified condition on the document.
    • If the evaluation returns false, returns false.
    • If the evaluation returns true, replaces the document.

Example Document and Code

Original Document
{
	"_id" : "movie0000001",
	"title" : "Placeholder doc"
}
Replacement Document
{
	"_id" : "movie0000001",
	"title" : "OJAI -- The Documentary",
	"studio" : "MapR Technologies, Inc.",
	"release_date" : "2015-09-29",
	"trailers" : {
		"teaser" : "https://10.10.21.90/trailers/teaser",
		"theatrical" : "https://10.10.21.90/trailers/theatrical"
	},
	"characters" : [
		"Heroic Developer", 
           "Evil Release Manager", 
           "Mad Development Manager"
	],
	"box_office_gross" : 1000000000L
}
Example code
  public QueryCondition buildQueryCondition() {
    return MapRDB.newCondition()
      .is("title", Op.EQUAL, "Placeholder doc")
        .close()
      .build();
  }
  public Document buildDocument() {
    return MapRDB.newDocument()
        .setId("movie0000001")
        .set("title", "OJAI -- The Documentary")
        .set("studio", "MapR Technologies, Inc.")
        .set("release_date", Values.parseDate("2015-09-29"))
        .set("trailers.teaser", "https://10.10.21.90/trailers/teaser")
        .set("trailers.theatrical", "https://10.10.21.90/trailers/theatrical")
        .setArray("characters", ImmutableList.of("Heroic Developer", "Evil Release Manager", "Mad Development Manager"))
        .set("box_office_gross", 1000000000L);
  }
  public void replaceIfTrue(String tablePath, 
      QueryCondition condition, Document document, String _id) {
    try(Table table = MapRDB.getTable(tablePath)) {
      if (table.checkAndReplace(_id, condition, document)) {
        System.out.println("Condition matched, replacement succeeded");
      } else {
        System.out.println("Condition did not match, replacement failed");
      }
    }
  }
Parameter Description
_id The value of the document's _id field.
condition The query condition to evaluate on the row.
document The document to replace.

Permissions Required

  • The readAce and writeAce permissions on the volumes where the JSON tables that contain the documents are located. For information about how to set permissions on volumes, see Setting/Modifying Whole Volume ACEs.
  • readperm and writeperm on the column families in the row.

Link to Javadoc

MapR-DB JSON Java API