def build_to_document(topic: Topic) -> MongoDocument: if is_raw_topic(topic): return build_by_raw(topic) elif is_aggregation_topic(topic): return build_by_aggregation(topic) else: return build_by_regular(topic)
def deserialize(self, row: EntityRow) -> Dict[str, Any]: data = self.deserialize_fix_columns(row) if is_aggregation_topic(self.get_schema().get_topic()): data[TopicDataColumnNames.AGGREGATE_ASSIST.value] = row.get( TopicDataColumnNames.AGGREGATE_ASSIST.value) data[TopicDataColumnNames.VERSION.value] = row.get( TopicDataColumnNames.VERSION.value) ArrayHelper(self.get_mapper().get_column_names()).each( lambda x: self.deserialize_column(row, x, data)) return data
def serialize(self, data: Dict[str, Any]) -> EntityRow: row = self.serialize_fix_columns(data) if is_aggregation_topic(self.get_schema().get_topic()): row[TopicDataColumnNames.AGGREGATE_ASSIST.value] = data.get( TopicDataColumnNames.AGGREGATE_ASSIST.value) row[TopicDataColumnNames.VERSION.value] = data.get( TopicDataColumnNames.VERSION.value) ArrayHelper(self.get_mapper().get_factor_names()).each( lambda x: self.serialize_factor(data, x, row)) return row
def build_columns_script(topic: Topic, original_topic: Topic) -> List[str]: entity_name = as_table_name(topic) original_factors: Dict[str, Factor] = ArrayHelper(original_topic.factors) \ .to_map(lambda x: x.name.strip().lower(), lambda x: x) # noinspection SqlResolve def build_column_script(factor: Tuple[Factor, Optional[Factor]]) -> str: current_factor, original_factor = factor if original_factor is None: return f'ALTER TABLE {entity_name} ADD COLUMN {ask_column_name(factor[0])} {ask_column_type(factor[0])}' elif current_factor.flatten and not original_factor.flatten: return f'ALTER TABLE {entity_name} ADD COLUMN {ask_column_name(factor[0])} {ask_column_type(factor[0])}' else: return f'ALTER TABLE {entity_name} ALTER COLUMN {ask_column_name(factor[0])} {ask_column_type(factor[0])}' if is_raw_topic(topic): factors = ArrayHelper(topic.factors) \ .filter(lambda x: x.flatten) \ .to_list() else: factors = topic.factors columns = ArrayHelper(factors) \ .map(lambda x: (x, original_factors.get(x.name.strip().lower()))) \ .map(build_column_script) \ .to_list() if is_raw_topic(topic) and not is_raw_topic(original_topic): columns.append( f'ALTER TABLE {entity_name} ADD COLUMN data_ NVARCHAR(MAX)') if is_aggregation_topic( topic) and not is_aggregation_topic(original_topic): columns.append( f'ALTER TABLE {entity_name} ADD COLUMN aggregate_assist_ NVARCHAR(1024)' ) columns.append( f'ALTER TABLE {entity_name} ADD COLUMN version_ DECIMAL(8)') return columns
def register_table(topic: Topic) -> None: existing = topic_tables.get(topic.topicId) if existing is not None: last_modified_at = existing[1] if last_modified_at >= topic.lastModifiedAt: # do nothing return if is_raw_topic(topic): topic_tables[topic.topicId] = (build_by_raw(topic), topic.lastModifiedAt) elif is_aggregation_topic(topic): topic_tables[topic.topicId] = (build_by_aggregation(topic), topic.lastModifiedAt) else: topic_tables[topic.topicId] = (build_by_regular(topic), topic.lastModifiedAt)
def build_version_column(topic: Topic) -> str: return f'\tversion_ DECIMAL(8),' if is_aggregation_topic(topic) else ''
def build_aggregate_assist_column(topic: Topic) -> str: return f'\taggregate_assist_ JSON,' if is_aggregation_topic(topic) else ''
def build_aggregate_assist_column(topic: Topic) -> str: return f'\taggregate_assist_ VARCHAR2(1024),' if is_aggregation_topic( topic) else ''
def assign_version(self, data: Dict[str, Any], version: int): if is_aggregation_topic(self.get_schema().get_topic()): data[TopicDataColumnNames.VERSION.value] = version
def find_version(self, data: Dict[str, Any]) -> int: if is_aggregation_topic(self.get_schema().get_topic()): return data.get(TopicDataColumnNames.VERSION.value) else: return -1
def is_versioned(self) -> bool: return is_aggregation_topic(self.get_schema().get_topic())