def create_table_indexes(self, index_attributes=[]): """Create index on tables in a particular ledger.""" logger.info('Creating index on all tables in a single transaction...') with qldb.session(self.ledger_name) as session: for index_attribute in index_attributes: session.execute_lambda( lambda x: DdlServices.do_create_table_index(x, self.hash_table_name, index_attribute), lambda retry_attempt: logger.info('Retrying due to OCC conflict...')) logger.info('Index created successfully.')
def get_table_data(ledger_name, table_name, where='1=1', limit=10, offset=0): """ Scan for all the documents in a table.""" try: with qldb.session(ledger_name) as session: # Scan all the tables and print their documents. tables = session.list_tables() for table in tables: cursor = session.execute_lambda( lambda executor: DmlServices.scan_table(executor, table_name, where, limit, offset), retry_indicator=lambda retry_attempt: logger.info('Retrying due to OCC conflict...')) logger.info('Scan successful!') return parse_result(cursor) except Exception as e: logger.exception('Unable to scan tables. {}'.format(e))
def table_exist(self): """ Connect to a session for a given ledger using default settings. """ try: qldb_session = qldb.session(self.ledger_name) logger.info('Listing table names ') tables = qldb_session.list_tables() _tables = [] for table in tables: _tables.append(table) return self.hash_table_name in _tables except Exception as e: logger.exception('Unable to create session.') return False
def drop_table(ledger_name=None, table_name=None): """Create a Table""" try: with qldb.session(ledger_name) as session: session.execute_lambda( lambda x: DdlServices.do_drop_table(x, table_name), lambda retry_attempt: logger.info( 'Retrying due to OCC conflict...')) logger.info('Table dropped successfully.') return success_response('Table dropped successfully.', HTTPStatus.CREATED) except Exception: logger.exception('Error creating table.') return fail_response( 'Unable to create the table. Please try again.', HTTPStatus.UNPROCESSABLE_ENTITY)
def insert_documents(ledger_name, table_name, documents): """ Insert documents into a table in a QLDB ledger. """ try: with qldb.session(ledger_name) as session: # An INSERT statement creates the initial revision of a document with a version number of zero. # QLDB also assigns a unique document identifier in GUID format as part of the metadata. session.execute_lambda( lambda executor: DmlServices.do_insert_documents(executor, table_name, documents), lambda retry_attempt: logger.info('Retrying due to OCC conflict...')) logger.info('Documents inserted successfully!') return success_response('Ledger is active and ready to use.', HTTPStatus.CREATED) except Exception as e: logger.exception('Error inserting or updating documents.') return fail_response('Error inserting or updating documents. error: {}. Please try again.'.format(e), HTTPStatus.UNPROCESSABLE_ENTITY)
def list_tables(ledger_name): """ Connect to a session for a given ledger using default settings. """ try: qldb_session = qldb.session(ledger_name) logger.info('Listing table names ') tables = qldb_session.list_tables() _tables = [] for table in tables: _tables.append(table) return _tables except Exception as e: logger.exception('Unable to create session.') return fail_response( 'Unable to connect ledgers!. Please try again.', HTTPStatus.UNPROCESSABLE_ENTITY)
def create_table_index(ledger_name=None, table_name=None, index_attribute=None): """Create index on tables in a particular ledger.""" logger.info('Creating index on all tables in a single transaction...') try: with qldb.session(ledger_name) as session: session.execute_lambda( lambda x: DdlServices.do_create_table_index( x, table_name, index_attribute), lambda retry_attempt: logger.info('Retrying due to OCC conflict...')) logger.info('Index created successfully.') return success_response('Index created successfully.', HTTPStatus.CREATED) except Exception: logger.exception('Unable to create index.') return fail_response( 'Unable to create the index. Please try again.', HTTPStatus.UNPROCESSABLE_ENTITY)
def verify_block(self): """ Get a journal block from a QLDB ledger. After getting the block, we get the digest of the ledger and validate the proof returned in the getBlock response. """ try: data = get_requested_data() with qldb.session(data["ledger_name"]) as session: cursor = session.execute_lambda(lambda executor: query_revision_history(executor, data["table_name"], data["condition_str"], data["condition_value"]), lambda retry_indicator: logger.info('Retrying due to OCC conflict...')) row = next(cursor) block_address = row.get('blockAddress') verify_block(data["ledger_name"], block_address) except Exception: logger.exception('Unable to query vehicle registration by Vin.')
def drop_table(self): """Create a Table""" with qldb.session(self.ledger_name) as session: session.execute_lambda(lambda x: DdlServices.do_drop_table(x, self.hash_table_name), lambda retry_attempt: logger.info('Retrying due to OCC conflict...')) logger.info('Table dropped successfully.')