void MaterializedViewMetadata::processTupleDelete(TableTuple &oldTuple) { // don't change the view if this tuple doesn't match the predicate if (m_filterPredicate && (m_filterPredicate->eval(&oldTuple, NULL).isFalse())) return; // this will assert if the tuple isn't there as param expected is true findExistingTuple(oldTuple, true); // clear the tuple that will be built to insert or overwrite memset(m_updatedTupleBackingStore, 0, m_target->schema()->tupleLength() + 1); //printf(" Existing tuple: %s.\n", m_existingTuple.debugNoHeader().c_str()); //fflush(stdout); // set up the first column, which is a count NValue count = m_existingTuple.getNValue(m_groupByColumnCount).op_decrement(); //printf(" Count is: %d.\n", (int)(m_existingTuple.getSlimValue(m_groupByColumnCount).getBigInt())); //fflush(stdout); // check if we should remove the tuple if (count.isZero()) { m_target->deleteTuple(m_existingTuple, true); return; } // assume from here that we're just updating the existing row int colindex = 0; // set up the first n columns, based on group-by columns for (colindex = 0; colindex < m_groupByColumnCount; colindex++) { m_updatedTuple.setNValue(colindex, oldTuple.getNValue(m_groupByColumns[colindex])); } m_updatedTuple.setNValue(colindex, count); colindex++; // set values for the other columns for (int i = colindex; i < m_outputColumnCount; i++) { NValue oldValue = oldTuple.getNValue(m_outputColumnSrcTableIndexes[i]); NValue existingValue = m_existingTuple.getNValue(i); if (m_outputColumnAggTypes[i] == EXPRESSION_TYPE_AGGREGATE_SUM) { m_updatedTuple.setNValue(i, existingValue.op_subtract(oldValue)); } else if (m_outputColumnAggTypes[i] == EXPRESSION_TYPE_AGGREGATE_COUNT) { m_updatedTuple.setNValue(i, existingValue.op_decrement()); } else { throw SerializableEEException(VOLT_EE_EXCEPTION_TYPE_EEEXCEPTION, "Error in materialized view table" " update."); } } // update the row // shouldn't need to update indexes as this shouldn't ever change the key m_target->updateTuple(m_updatedTuple, m_existingTuple, false); }
void MaterializedViewMetadata::processTupleInsert(TableTuple &newTuple) { // don't change the view if this tuple doesn't match the predicate if (m_filterPredicate && (m_filterPredicate->eval(&newTuple, NULL).isFalse())) return; bool exists = findExistingTuple(newTuple); if (!exists) { // create a blank tuple m_existingTuple.move(m_emptyTupleBackingStore); } // clear the tuple that will be built to insert or overwrite memset(m_updatedTupleBackingStore, 0, m_target->schema()->tupleLength() + 1); int colindex = 0; // set up the first n columns, based on group-by columns for (colindex = 0; colindex < m_groupByColumnCount; colindex++) { m_updatedTuple.setNValue(colindex, newTuple.getNValue(m_groupByColumns[colindex])); } // set up the next column, which is a count m_updatedTuple.setNValue(colindex, m_existingTuple.getNValue(colindex).op_increment()); colindex++; // set values for the other columns for (int i = colindex; i < m_outputColumnCount; i++) { NValue newValue = newTuple.getNValue(m_outputColumnSrcTableIndexes[i]); NValue existingValue = m_existingTuple.getNValue(i); if (m_outputColumnAggTypes[i] == EXPRESSION_TYPE_AGGREGATE_SUM) { m_updatedTuple.setNValue(i, newValue.op_add(existingValue)); } else if (m_outputColumnAggTypes[i] == EXPRESSION_TYPE_AGGREGATE_COUNT) { m_updatedTuple.setNValue(i, existingValue.op_increment()); } else { char message[128]; sprintf(message, "Error in materialized view table update for" " col %d. Expression type %d", i, m_outputColumnAggTypes[i]); throw SerializableEEException(VOLT_EE_EXCEPTION_TYPE_EEEXCEPTION, message); } } // update or insert the row if (exists) { // shouldn't need to update indexes as this shouldn't ever change the // key m_target->updateTuple(m_updatedTuple, m_existingTuple, false); } else { m_target->insertTuple(m_updatedTuple); } }
void MaterializedViewMetadata::processTupleInsert(TableTuple &newTuple, bool fallible) { // don't change the view if this tuple doesn't match the predicate if (m_filterPredicate && (m_filterPredicate->eval(&newTuple, NULL).isFalse())) { return; } bool exists = findExistingTuple(newTuple); if (!exists) { // create a blank tuple m_existingTuple.move(m_emptyTupleBackingStore); } // clear the tuple that will be built to insert or overwrite memset(m_updatedTupleBackingStore, 0, m_target->schema()->tupleLength() + 1); int colindex = 0; // set up the first n columns, based on group-by columns for (colindex = 0; colindex < m_groupByColumnCount; colindex++) { // note that if the tuple is in the mv's target table, // tuple values should be pulled from the existing tuple in // that table. This works around a memory ownership issue // related to out-of-line strings. if (exists) { m_updatedTuple.setNValue(colindex, m_existingTuple.getNValue(colindex)); } else { m_updatedTuple.setNValue(colindex, newTuple.getNValue(m_groupByColumns[colindex])); } } // set up the next column, which is a count m_updatedTuple.setNValue(colindex, m_existingTuple.getNValue(colindex).op_increment()); colindex++; // set values for the other columns for (int i = colindex; i < m_outputColumnCount; i++) { NValue newValue = newTuple.getNValue(m_outputColumnSrcTableIndexes[i]); NValue existingValue = m_existingTuple.getNValue(i); if (m_outputColumnAggTypes[i] == EXPRESSION_TYPE_AGGREGATE_SUM) { m_updatedTuple.setNValue(i, newValue.op_add(existingValue)); } else if (m_outputColumnAggTypes[i] == EXPRESSION_TYPE_AGGREGATE_COUNT) { m_updatedTuple.setNValue(i, existingValue.op_increment()); } else { char message[128]; snprintf(message, 128, "Error in materialized view table update for" " col %d. Expression type %d", i, m_outputColumnAggTypes[i]); throw SerializableEEException(VOLT_EE_EXCEPTION_TYPE_EEEXCEPTION, message); } } // update or insert the row if (exists) { // Shouldn't need to update group-key-only indexes such as the primary key // since their keys shouldn't ever change, but do update other indexes. m_target->updateTupleWithSpecificIndexes(m_existingTuple, m_updatedTuple, m_updatableIndexList, fallible); } else { m_target->insertPersistentTuple(m_updatedTuple, fallible); } }