use of org.activityinfo.store.query.shared.columns.ForeignKey in project activityinfo by bedatadriven.
the class FormScan method addForeignKey.
/**
* Includes the given foreign key in the table scan
*
* @return a slot where the value can be found after the query completes
*/
public Slot<ForeignKey> addForeignKey(String fieldName, ResourceId rightFormId) {
// create the key builder if it doesn't exist
ForeignKeyId fkId = new ForeignKeyId(fieldName, rightFormId);
PendingSlot<ForeignKey> builder = foreignKeyMap.get(fkId);
if (builder == null) {
builder = new PendingSlot<>();
foreignKeyMap.put(fkId, builder);
}
return builder;
}
use of org.activityinfo.store.query.shared.columns.ForeignKey in project activityinfo by bedatadriven.
the class ForeignKeyTest method filtering.
@Test
public void filtering() {
// Build the unfiltered foreign key map
// That maps row indexes to foreign keys
ResourceId formId = ResourceId.valueOf("a00001");
ForeignKeyBuilder builder = new ForeignKeyBuilder(formId, new PendingSlot<ForeignKey>());
builder.onNext(new ReferenceValue(new RecordRef(formId, ResourceId.valueOf("s0272548382"))));
builder.onNext(new ReferenceValue(new RecordRef(formId, ResourceId.valueOf("s0272548382"))));
builder.onNext(new ReferenceValue(new RecordRef(formId, ResourceId.valueOf("s0362622291"))));
builder.onNext(new ReferenceValue(new RecordRef(formId, ResourceId.valueOf("s0362622291"))));
builder.onNext(new ReferenceValue(new RecordRef(formId, ResourceId.valueOf("s0890848243"))));
builder.onNext(new ReferenceValue(new RecordRef(formId, ResourceId.valueOf("s0890848243"))));
ForeignKey fkMap = builder.build();
// Now define a filter that includes only the 4th and 5th rows
BitSet bitSet = new BitSet();
bitSet.set(4);
bitSet.set(5);
TableFilter filter = new TableFilter(bitSet);
// Apply the filter to the ForeignKey map and verify the results
ForeignKey filteredKey = filter.apply(fkMap);
assertThat(filteredKey.getKey(0), equalTo("s0890848243"));
assertThat(filteredKey.getKey(1), equalTo("s0890848243"));
}
use of org.activityinfo.store.query.shared.columns.ForeignKey in project activityinfo by bedatadriven.
the class ForeignKeyTest method emptyValues.
@Test
public void emptyValues() {
// Build the unfiltered foreign key map
// That maps row indexes to foreign keys
ResourceId formId = ResourceId.valueOf("a00001");
ForeignKeyBuilder builder = new ForeignKeyBuilder(formId, new PendingSlot<ForeignKey>());
builder.onNext(null);
builder.onNext(new ReferenceValue(new RecordRef(formId, ResourceId.valueOf("s0272548382"))));
builder.onNext(null);
builder.onNext(null);
ForeignKey fkMap = builder.build();
assertThat(fkMap.numRows(), equalTo(4));
}
use of org.activityinfo.store.query.shared.columns.ForeignKey in project activityinfo by bedatadriven.
the class ReferenceJoin method mapping.
/**
* @return builds an array which maps each row in the left table
* to the corresponding row in the right table.
*/
public int[] mapping() {
if (mapping == null) {
ForeignKey fk = foreignKey.get();
PrimaryKeyMap pk = primaryKeyMap.get();
mapping = fk.buildMapping(pk);
}
return Arrays.copyOf(mapping, mapping.length);
}
use of org.activityinfo.store.query.shared.columns.ForeignKey in project activityinfo by bedatadriven.
the class ForeignKeyTest method multipleReferencedForms.
@Test
public void multipleReferencedForms() {
// Build the unfiltered foreign key map
// That maps row indexes to foreign keys
ResourceId province = ResourceId.valueOf("g00001");
ResourceId territory = ResourceId.valueOf("g00002");
ForeignKeyBuilder builder = new ForeignKeyBuilder(province, new PendingSlot<ForeignKey>());
// Row 0: Missing
builder.onNext(null);
// Row 1: Multiple Provinces
builder.onNext(new ReferenceValue(new RecordRef(province, ResourceId.valueOf("P1")), new RecordRef(province, ResourceId.valueOf("P2"))));
// Row 2: One province, one territory
builder.onNext(new ReferenceValue(new RecordRef(province, ResourceId.valueOf("P1")), new RecordRef(territory, ResourceId.valueOf("T1"))));
// Row 3: Two territories
builder.onNext(new ReferenceValue(new RecordRef(territory, ResourceId.valueOf("T1")), new RecordRef(territory, ResourceId.valueOf("T2"))));
// Row 4: Missing
builder.onNext(null);
// Row 5: Missing
builder.onNext(null);
ForeignKey fkMap = builder.build();
assertThat(fkMap.numRows(), equalTo(6));
assertThat(fkMap.getKey(0), nullValue());
assertThat(fkMap.getKey(1), nullValue());
assertThat(fkMap.getKey(2), equalTo("P1"));
assertThat(fkMap.getKey(3), nullValue());
assertThat(fkMap.getKey(4), nullValue());
assertThat(fkMap.getKey(5), nullValue());
}
Aggregations