// Basic create-update-delete
//
{
// Create a record
log.debug("Begin test NV1");
Record record = defaultTable.newRecord();
record.setRecordType(nvRecordType1.getName());
record.setField(nvfield1.getName(), "nvapple");
record.setField(nvTag.getName(), 0L);
expectEvent(CREATE, Table.RECORD.name, record.getId(), nvfield1.getId(), nvTag.getId());
record = defaultTable.create(record);
commitIndex();
verifyResultCount("lily.id:" + record.getId().toString(), 1);
verifyResultCount("nv_field1:nvapple", 1);
// Update the record
log.debug("Begin test NV2");
record.setField(nvfield1.getName(), "nvpear");
expectEvent(UPDATE, Table.RECORD.name, record.getId(), nvfield1.getId());
defaultTable.update(record);
System.out.println("Updated " + record.getId());
commitIndex();
verifyResultCount("nv_field1:nvpear", 1);
verifyResultCount("nv_field1:nvapple", 0);
// Do as if field2 changed, while field2 is not present in the document.
// Such situations can occur if the record is modified before earlier events are processed.
log.debug("Begin test NV3");
// TODO send event directly to the Indexer
// sendEvent(EVENT_RECORD_UPDATED, record.getId(), nvfield2.getId());
verifyResultCount("nv_field1:nvpear", 1);
verifyResultCount("nv_field1:nvapple", 0);
// Add a vtag field pointing to a version. For versionless records, this should have no effect
log.debug("Begin test NV4");
record.setField(liveTag.getName(), new Long(1));
expectEvent(UPDATE, Table.RECORD.name, record.getId(), liveTag.getId());
defaultTable.update(record);
commitIndex();
verifyResultCount("nv_field1:nvpear", 1);
verifyResultCount("nv_field1:nvapple", 0);
// Delete the record
log.debug("Begin test NV5");
expectEvent(DELETE, Table.RECORD.name, record.getId());
defaultTable.delete(record.getId());
commitIndex();
verifyResultCount("nv_field1:nvpear", 0);
}
//
// Deref
//
{
log.debug("Begin test NV6");
Record record1 = defaultTable.newRecord();
record1.setRecordType(nvRecordType1.getName());
record1.setField(nvfield1.getName(), "nvpear");
record1.setField(nvTag.getName(), 0L);
expectEvent(CREATE, Table.RECORD.name, record1.getId(), nvfield1.getId(), nvTag.getId());
record1 = defaultTable.create(record1);
Record record2 = defaultTable.newRecord();
record2.setRecordType(nvRecordType1.getName());
record2.setField(nvLinkField1.getName(), new Link(record1.getId()));
record2.setField(nvTag.getName(), 0L);
expectEvent(CREATE, Table.RECORD.name, record2.getId(), nvLinkField1.getId(), nvTag.getId());
record2 = defaultTable.create(record2);
commitIndex();
verifyResultCount("nv_deref1:nvpear", 1);
}
//
// Variant deref
//
{
log.debug("Begin test NV7");
Record masterRecord = defaultTable.newRecord();
masterRecord.setRecordType(nvRecordType1.getName());
masterRecord.setField(nvfield1.getName(), "yellow");
masterRecord.setField(nvTag.getName(), 0L);
expectEvent(CREATE, Table.RECORD.name, masterRecord.getId(), nvfield1.getId(), nvTag.getId());
masterRecord = defaultTable.create(masterRecord);
RecordId var1Id = idGenerator.newRecordId(masterRecord.getId(), Collections.singletonMap("lang", "en"));
Record var1Record = defaultTable.newRecord(var1Id);
var1Record.setRecordType(nvRecordType1.getName());
var1Record.setField(nvfield1.getName(), "green");
var1Record.setField(nvTag.getName(), 0L);
expectEvent(CREATE, Table.RECORD.name, var1Id, nvfield1.getId(), nvTag.getId());
defaultTable.create(var1Record);
Map<String, String> varProps = new HashMap<String, String>();
varProps.put("lang", "en");
varProps.put("branch", "dev");
RecordId var2Id = idGenerator.newRecordId(masterRecord.getId(), varProps);
Record var2Record = defaultTable.newRecord(var2Id);
var2Record.setRecordType(nvRecordType1.getName());
var2Record.setField(nvfield1.getName(), "blue");
var2Record.setField(nvTag.getName(), 0L);
expectEvent(CREATE, Table.RECORD.name, var2Id, nvfield1.getId(), nvTag.getId());
defaultTable.create(var2Record);
commitIndex();
verifyResultCount("nv_deref2:yellow", 1);
verifyResultCount("nv_deref3:yellow", 2);
verifyResultCount("nv_deref4:green", 1);
verifyResultCount("nv_deref3:green", 0);
verifyResultCount("nv_deref5:blue", 1);
verifyResultCount("nv_deref5:green", 0);
verifyResultCount("nv_deref5:yellow", 0);
verifyResultCount("nv_deref6:blue", 2);
verifyResultCount("nv_deref7:blue", 1);
verifyResultCount("nv_deref8:blue", 0);
}
//
// Update denormalized data
//
{
log.debug("Begin test NV8");
Record record1 = defaultTable.newRecord(idGenerator.newRecordId("boe"));
record1.setRecordType(nvRecordType1.getName());
record1.setField(nvfield1.getName(), "cucumber");
record1.setField(nvTag.getName(), 0L);
expectEvent(CREATE, Table.RECORD.name, record1.getId(), nvfield1.getId(), nvTag.getId());
record1 = defaultTable.create(record1);
// Create a record which will contain denormalized data through linking
Record record2 = defaultTable.newRecord();
record2.setRecordType(nvRecordType1.getName());
record2.setField(nvLinkField1.getName(), new Link(record1.getId()));
record2.setField(nvfield1.getName(), "mushroom");
record2.setField(nvTag.getName(), 0L);
expectEvent(CREATE, Table.RECORD.name, record2.getId(), nvLinkField1.getId(), nvfield1.getId(), nvTag.getId());
record2 = defaultTable.create(record2);
// Create a record which will contain denormalized data through master-dereferencing and forward-variant-dereferencing
RecordId record3Id = idGenerator.newRecordId(record1.getId(), Collections.singletonMap("lang", "en"));
Record record3 = defaultTable.newRecord(record3Id);
record3.setRecordType(nvRecordType1.getName());
record3.setField(nvfield1.getName(), "eggplant");
record3.setField(nvTag.getName(), 0L);
expectEvent(CREATE, Table.RECORD.name, record3.getId(), nvfield1.getId(), nvTag.getId());
record3 = defaultTable.create(record3);
// Create a record which will contain denormalized data through variant-dereferencing
Map<String, String> varprops = new HashMap<String, String>();
varprops.put("lang", "en");
varprops.put("branch", "dev");
RecordId record4Id = idGenerator.newRecordId(record1.getId(), varprops);
Record record4 = defaultTable.newRecord(record4Id);
record4.setRecordType(nvRecordType1.getName());
record4.setField(nvfield1.getName(), "broccoli");
record4.setField(nvTag.getName(), 0L);
expectEvent(CREATE, Table.RECORD.name, record4.getId(), nvfield1.getId(), nvTag.getId());
record4 = defaultTable.create(record4);
commitIndex();
verifyResultCount("nv_deref1:cucumber", 1); // record2[nv:linkField1] = record1
verifyResultCount("nv_deref2:cucumber", 1); // record4{-branch,-lang} = record1
verifyResultCount("nv_deref3:cucumber", 2); // record3{master} and record4{master} = record1
verifyResultCount("nv_deref4:eggplant", 1); // record4{-branch} = record3
verifyResultCount("nv_deref5:broccoli", 1); // record3{+branch} = record4
verifyResultCount("nv_deref6:broccoli", 2); // record1{+branch,+lang} = record4 (2 tags!)
verifyResultCount("nv_deref7:broccoli", 1); // record3{+branch=dev} = record4
// Update record1, check if the others are updated in the index
log.debug("Begin test NV9");
record1.setField(nvfield1.getName(), "tomato");
expectEvent(UPDATE, Table.RECORD.name, record1.getId(), nvfield1.getId());
record1 = defaultTable.update(record1);
commitIndex();
verifyResultCount("nv_deref1:tomato", 1); // record2[ns:nvLinkField1] = record1
verifyResultCount("nv_deref2:tomato", 1); // record4{-branch,-lang} = record1
verifyResultCount("nv_deref3:tomato", 2); // record3{master} and record4{master} = record1
verifyResultCount("nv_deref1:cucumber", 0); // old value should be removed from index (non versioned field!)
verifyResultCount("nv_deref2:cucumber", 0); // old value should be removed from index (non versioned field!)
verifyResultCount("nv_deref3:cucumber", 0); // old value should be removed from index (non versioned field!)
verifyResultCount("nv_deref4:eggplant", 1); // record4(-branch) = record3
// Update record3, index for record4 should be updated
log.debug("Begin test NV10");
record3.setField(nvfield1.getName(), "courgette");
expectEvent(UPDATE, Table.RECORD.name, record3.getId(), nvfield1.getId());
defaultTable.update(record3);
commitIndex();
verifyResultCount("nv_deref4:courgette", 1); // record4(-branch) = record3
verifyResultCount("nv_deref4:eggplant", 0); // old value should be removed from index (non versioned field!)
// Update record4, index for record3 and record1 should be updated
log.debug("Begin test NV10.1");
record4.setField(nvfield1.getName(), "cauliflower"); //FIXME: 2nd courgette; use something else here
expectEvent(UPDATE, Table.RECORD.name, record4.getId(), nvfield1.getId());
defaultTable.update(record4);
commitIndex();
verifyResultCount("nv_deref5:cauliflower", 1); // record3{+branch} produces record4
verifyResultCount("nv_deref5:broccoli", 0); // old value should be removed from index
verifyResultCount("nv_deref5:courgette", 0); // sanity check, nothing could match
verifyResultCount("nv_deref6:cauliflower", 2); // record1{+branch,+lang} = record4 (2 vtags!)
verifyResultCount("nv_deref6:broccoli", 0); // old value should be removed from index
verifyResultCount("nv_deref7:cauliflower", 1); // record3{+branch=dev} = record4
verifyResultCount("nv_deref7:broccoli", 0); // old value should be removed from index
// Delete record 3: index for record 4 should be updated
log.debug("Begin test NV11");
verifyResultCount("lily.id:" + ClientUtils.escapeQueryChars(record3.getId().toString()), 1);
expectEvent(DELETE, Table.RECORD.name, record3.getId());
defaultTable.delete(record3.getId());
commitIndex();
verifyResultCount("nv_deref4:cauliflower", 0);
verifyResultCount("nv_deref3:tomato", 1);
verifyResultCount("lily.id:" + ClientUtils.escapeQueryChars(record3.getId().toString()), 0);
// Delete record 4
log.debug("Begin test NV12");
expectEvent(DELETE, Table.RECORD.name, record4.getId());
defaultTable.delete(record4.getId());
commitIndex();
verifyResultCount("nv_deref3:tomato", 0);
verifyResultCount("nv_field1:broccoli", 0);
verifyResultCount("lily.id:" + ClientUtils.escapeQueryChars(record4.getId().toString()), 0);
// Delete record 1: index of record 2 should be updated
log.debug("Begin test NV13");
expectEvent(DELETE, Table.RECORD.name, record1.getId());
defaultTable.delete(record1.getId());