Search in sources :

Example 1 with ForeignKeyId

use of org.activityinfo.store.query.shared.join.ForeignKeyId in project activityinfo by bedatadriven.

the class FormScan method prepare.

/**
 * Prepares a column query based on the requested fields and formulas.
 */
public void prepare(ColumnQueryBuilder columnQueryBuilder) {
    // check to see if we still need to hit the database after being populated by the cache
    if (columnMap.isEmpty() && foreignKeyMap.isEmpty() && rowCount == null) {
        return;
    }
    // Build the query
    ExprQueryBuilder queryBuilder = new ExprQueryBuilder(columnFactory, formClass, columnQueryBuilder);
    for (Map.Entry<FormulaNode, PendingSlot<ColumnView>> column : columnMap.entrySet()) {
        if (column.getKey().equals(PK_COLUMN_KEY)) {
            queryBuilder.addResourceId(new IdColumnBuilder(column.getValue()));
        } else {
            queryBuilder.addExpr(column.getKey(), column.getValue());
        }
    }
    // Only add a row count observer IF it has been requested AND
    // it hasn't been loaded from the cache.
    RowCountBuilder rowCountBuilder = null;
    if (rowCount != null && !rowCount.isSet()) {
        rowCountBuilder = new RowCountBuilder(rowCount);
        queryBuilder.addResourceId(rowCountBuilder);
    }
    for (Map.Entry<ForeignKeyId, PendingSlot<ForeignKey>> fk : foreignKeyMap.entrySet()) {
        queryBuilder.addField(fk.getKey().getFieldId(), columnFactory.newForeignKeyBuilder(fk.getKey().getRightFormId(), fk.getValue()));
    }
}
Also used : FormulaNode(org.activityinfo.model.formula.FormulaNode) ForeignKeyId(org.activityinfo.store.query.shared.join.ForeignKeyId) IdColumnBuilder(org.activityinfo.store.query.shared.columns.IdColumnBuilder) RowCountBuilder(org.activityinfo.store.query.shared.columns.RowCountBuilder)

Example 2 with ForeignKeyId

use of org.activityinfo.store.query.shared.join.ForeignKeyId 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;
}
Also used : ForeignKeyId(org.activityinfo.store.query.shared.join.ForeignKeyId) ForeignKey(org.activityinfo.store.query.shared.columns.ForeignKey)

Example 3 with ForeignKeyId

use of org.activityinfo.store.query.shared.join.ForeignKeyId in project activityinfo by bedatadriven.

the class FormScan method getValuesToCache.

public Map<String, Object> getValuesToCache() {
    Map<String, Object> toPut = new HashMap<>();
    for (Map.Entry<FormulaNode, PendingSlot<ColumnView>> column : columnMap.entrySet()) {
        ColumnView value;
        try {
            value = column.getValue().get();
        } catch (IllegalStateException e) {
            throw new IllegalStateException(column.getKey().toString(), e);
        }
        toPut.put(fieldCacheKey(column.getKey()), value);
    }
    for (Map.Entry<ForeignKeyId, PendingSlot<ForeignKey>> fk : foreignKeyMap.entrySet()) {
        toPut.put(fkCacheKey(fk.getKey()), fk.getValue().get());
    }
    if (!columnMap.isEmpty()) {
        toPut.put(rowCountKey(), rowCountFromColumn(columnMap));
    } else if (rowCount != null) {
        toPut.put(rowCountKey(), rowCount.get());
    }
    return toPut;
}
Also used : FormulaNode(org.activityinfo.model.formula.FormulaNode) ForeignKeyId(org.activityinfo.store.query.shared.join.ForeignKeyId) ColumnView(org.activityinfo.model.query.ColumnView)

Example 4 with ForeignKeyId

use of org.activityinfo.store.query.shared.join.ForeignKeyId in project activityinfo by bedatadriven.

the class FormScan method updateFromCache.

public void updateFromCache(Map<String, Object> cached) {
    // See which columns we could retrieve from cache
    for (FormulaNode fieldId : Lists.newArrayList(columnMap.keySet())) {
        ColumnView view = (ColumnView) cached.get(fieldCacheKey(fieldId));
        if (view != null) {
            // populate the pending result slot with the view from the cache
            columnMap.get(fieldId).set(view);
            // remove this column from the list of columns to fetch
            columnMap.remove(fieldId);
            // resolve the rowCount slot if still needed
            if (rowCount != null) {
                rowCount.set(view.numRows());
                rowCount = null;
            }
        }
    }
    // And which foreign keys...
    for (ForeignKeyId keyId : Lists.newArrayList(foreignKeyMap.keySet())) {
        ForeignKey map = (ForeignKey) cached.get(fkCacheKey(keyId));
        if (map != null) {
            foreignKeyMap.get(keyId).set(map);
            foreignKeyMap.remove(keyId);
        }
    }
    // Do we need a row count?
    if (rowCount != null) {
        Integer count = (Integer) cached.get(rowCountKey());
        if (count != null) {
            rowCount.set(count);
        }
    }
}
Also used : FormulaNode(org.activityinfo.model.formula.FormulaNode) ForeignKeyId(org.activityinfo.store.query.shared.join.ForeignKeyId) ColumnView(org.activityinfo.model.query.ColumnView) ForeignKey(org.activityinfo.store.query.shared.columns.ForeignKey)

Aggregations

ForeignKeyId (org.activityinfo.store.query.shared.join.ForeignKeyId)4 FormulaNode (org.activityinfo.model.formula.FormulaNode)3 ColumnView (org.activityinfo.model.query.ColumnView)2 ForeignKey (org.activityinfo.store.query.shared.columns.ForeignKey)2 IdColumnBuilder (org.activityinfo.store.query.shared.columns.IdColumnBuilder)1 RowCountBuilder (org.activityinfo.store.query.shared.columns.RowCountBuilder)1