def find(self, name: str, tenant_id: TenantId) -> List[FactorIndex]: if not ask_engine_index_enabled(): return [] # noinspection PyTypeChecker return self.storage.find( EntityFinder( name=FACTOR_INDEX_ENTITY_NAME, shaper=FACTOR_INDEX_ENTITY_SHAPER, criteria=[ EntityCriteriaExpression( left=ColumnNameLiteral(columnName='tenant_id'), right=tenant_id), EntityCriteriaJoint( conjunction=EntityCriteriaJointConjunction.AND, children=[ EntityCriteriaExpression( left=ColumnNameLiteral( columnName='factor_name'), operator=EntityCriteriaOperator.LIKE, right=name), EntityCriteriaExpression( left=ColumnNameLiteral( columnName='topic_name'), operator=EntityCriteriaOperator.LIKE, right=name) ]) ]))
def get_entity_finder(self, criteria: EntityCriteria, sort: Optional[EntitySort] = None) -> EntityFinder: return EntityFinder(name=self.get_entity_name(), shaper=self.get_entity_shaper(), criteria=criteria, sort=sort)
def get_entity_finder(self, criteria: EntityCriteria, sort: Optional[EntitySort] = None) -> EntityFinder: entity_helper = self.get_entity_helper() return EntityFinder(name=entity_helper.name, shaper=entity_helper.shaper, criteria=criteria, sort=sort)
def find_by_id(self, entity_id: EntityId, helper: EntityIdHelper) -> Optional[Entity]: return self.find_one( EntityFinder(name=helper.name, shaper=helper.shaper, criteria=[ EntityCriteriaExpression(left=ColumnNameLiteral( columnName=helper.idColumnName), right=entity_id) ]))
def find_by_type(self, key_type: str, tenant_id: TenantId) -> Optional[KeyStore]: return self.storage.find_one(EntityFinder( name=self.get_entity_name(), shaper=self.get_entity_shaper(), criteria=[ EntityCriteriaExpression( left=ColumnNameLiteral(columnName='key_type'), right=key_type), EntityCriteriaExpression(left=ColumnNameLiteral(columnName='tenant_id'), right=tenant_id) ] ))
def delete_only_and_pull(self, deleter: EntityDeleter) -> Optional[Entity]: entity = self.find_one( EntityFinder(name=deleter.name, shaper=deleter.shaper, criteria=deleter.criteria)) if entity is None: raise EntityNotFoundException( f'Entity not found by deleter[{deleter}].') else: self.delete_only(deleter) return entity
def update_only_and_pull(self, updater: EntityUpdater) -> Optional[Entity]: entity = self.find_one( EntityFinder(name=updater.name, shaper=updater.shaper, criteria=updater.criteria)) if entity is None: raise EntityNotFoundException( f'Entity not found by updater[{updater}]') else: self.update_only(updater) return entity
def find_by_user_id(self, user_id: UserId, tenant_id: TenantId) -> Optional[LastSnapshot]: return self.storage.find_one( EntityFinder(name=self.get_entity_name(), shaper=self.get_entity_shaper(), criteria=[ EntityCriteriaExpression( left=ColumnNameLiteral(columnName='user_id'), right=user_id), EntityCriteriaExpression(left=ColumnNameLiteral( columnName='tenant_id'), right=tenant_id) ]))
def find_by_user_id(self, user_id: UserId, tenant_id: TenantId) -> List[PersonalAccessToken]: # noinspection PyTypeChecker return self.storage.find( EntityFinder(name=self.get_entity_name(), shaper=self.get_entity_shaper(), criteria=[ EntityCriteriaExpression( left=ColumnNameLiteral(columnName='user_id'), right=user_id), EntityCriteriaExpression(left=ColumnNameLiteral( columnName='tenant_id'), right=tenant_id) ]))
def build_index(self, topic: Topic) -> None: if not ask_engine_index_enabled(): return # noinspection PyTypeChecker factor_index_list: List[FactorIndex] = self.storage.find( EntityFinder(name=FACTOR_INDEX_ENTITY_NAME, shaper=FACTOR_INDEX_ENTITY_SHAPER, criteria=[ EntityCriteriaExpression( left=ColumnNameLiteral(columnName='topic_id'), right=topic.topicId) ])) index_map: Dict[FactorId, FactorIndex] = ArrayHelper(factor_index_list) \ .to_map(lambda x: x.factorId, lambda x: x) now = get_current_time_in_seconds() current_index_list = ArrayHelper(topic.factors).map( lambda x: self.build_factor_index(x, topic, now)).to_list() entity_id_helper = EntityIdHelper(name=FACTOR_INDEX_ENTITY_NAME, shaper=FACTOR_INDEX_ENTITY_SHAPER, idColumnName='factor_index_id') for factor_index in current_index_list: if factor_index.factorId in index_map: old_factor_index = index_map[factor_index.factorId] factor_index.factorIndexId = old_factor_index.factorIndexId factor_index.createdAt = old_factor_index.createdAt self.storage.update_one(factor_index, entity_id_helper) else: self.storage.insert_one(factor_index, entity_id_helper) current_factor_ids = ArrayHelper(current_index_list).map( lambda x: x.factorId).to_list() to_remove_list: List[FactorIndex] = ArrayHelper(list(index_map.values())) \ .filter(lambda x: x.factorId not in current_factor_ids).to_list() if len(to_remove_list) != 0: self.storage.delete( EntityDeleter( name=FACTOR_INDEX_ENTITY_NAME, shaper=FACTOR_INDEX_ENTITY_SHAPER, criteria=[ EntityCriteriaExpression( left=ColumnNameLiteral(columnName='topic_id'), right=topic.topicId), EntityCriteriaExpression( left=ColumnNameLiteral( columnName='factor_index_id'), operator=EntityCriteriaOperator.IN, right=ArrayHelper(to_remove_list).map( lambda x: x.factorIndexId).to_list()) ]))
def delete_and_pull(self, deleter: EntityDeleter) -> EntityList: entity_list = self.find( EntityFinder(name=deleter.name, shaper=deleter.shaper, criteria=deleter.criteria)) found_count = len(entity_list) if found_count == 0: return [] else: deleted_count = self.delete(deleter) if deleted_count != found_count: logger.warning( f'Delete count[{deleted_count}] does not match pull count[{found_count}].' ) return entity_list
def update_and_pull(self, updater: EntityUpdater) -> EntityList: entity_list = self.find( EntityFinder(name=updater.name, shaper=updater.shaper, criteria=updater.criteria)) found_count = len(entity_list) if found_count == 0: # not found, no need to update return [] else: updated_count = self.update(updater) if updated_count != found_count: logger.warning( f'Update count[{updated_count}] does not match pull count[{found_count}].' ) return entity_list
def find_by_scheduler_and_process_date( self, scheduler_id: TopicSnapshotSchedulerId, frequency: TopicSnapshotFrequency, process_date: date) -> Optional[TopicSnapshotJobLock]: return self.storage.find_one( EntityFinder( name=self.get_entity_name(), shaper=self.get_entity_shaper(), criteria=[ EntityCriteriaExpression( left=ColumnNameLiteral(columnName='scheduler_id'), right=scheduler_id), EntityCriteriaExpression( left=ColumnNameLiteral(columnName='frequency'), right=frequency), EntityCriteriaExpression( left=ColumnNameLiteral(columnName='process_date'), right=process_date), ]))
def find_user_by_name(storage: TransactionalStorageSPI, username: str, clear_pwd: bool) -> Optional[User]: if username is None or len(username.strip()) == 0: return None storage.begin() try: # noinspection PyTypeChecker user: User = storage.find_one( EntityFinder(name=USER_ENTITY_NAME, shaper=USER_ENTITY_SHAPER, criteria=[ EntityCriteriaExpression( left=ColumnNameLiteral(columnName='name'), right=username) ])) return redress_user(user, clear_pwd) finally: storage.close()
def find_by_id(self, pat_id: PatId, user_id: UserId, tenant_id: TenantId) -> Optional[PersonalAccessToken]: # noinspection PyTypeChecker pat: PersonalAccessToken = self.storage.find_one( EntityFinder(name=self.get_entity_name(), shaper=self.get_entity_shaper(), criteria=[ EntityCriteriaExpression(left=ColumnNameLiteral( columnName=self.get_pat_id_column_name()), right=pat_id), EntityCriteriaExpression( left=ColumnNameLiteral(columnName='user_id'), right=user_id), EntityCriteriaExpression(left=ColumnNameLiteral( columnName='tenant_id'), right=tenant_id) ])) if pat is None: return None return pat
def find_pat_by_token(storage: TransactionalStorageSPI, pat_token: str) -> Optional[PersonalAccessToken]: if pat_token is None or len(pat_token.strip()) == 0: return None # noinspection PyTypeChecker pat: PersonalAccessToken = storage.find_one( EntityFinder(name=PAT_ENTITY_NAME, shaper=PAT_ENTITY_SHAPER, criteria=[ EntityCriteriaExpression( left=ColumnNameLiteral(columnName='token'), right=pat_token) ])) if pat is None: return None if pat.expired is None: # no expired date return pat if pat.expired < datetime.now().replace(tzinfo=None): # pat is expired return None return pat
def find_all(self, helper: EntityHelper) -> EntityList: return self.find(EntityFinder(name=helper.name, shaper=helper.shaper))