示例#1
0
def create_database(conn, dbname):
    if r.db_list().contains(dbname).run(conn):
        raise exceptions.DatabaseAlreadyExists(
            'Database `{}` already exists'.format(dbname))

    logger.info('Create database `%s`.', dbname)
    r.db_create(dbname).run(conn)
示例#2
0
def init():
    # Try to access the keypair, throws an exception if it does not exist
    b = bigchaindb.Bigchain()

    conn = get_conn()
    dbname = bigchaindb.config['database']['name']

    if r.db_list().contains(dbname).run(conn):
        raise exceptions.DatabaseAlreadyExists(
            'Database `{}` already exists'.format(dbname))

    logger.info('Create:')
    logger.info(' - database `%s`', dbname)
    r.db_create(dbname).run(conn)

    logger.info(' - tables')
    # create the tables
    r.db(dbname).table_create('bigchain').run(conn)
    r.db(dbname).table_create('backlog').run(conn)

    logger.info(' - indexes')
    # create the secondary indexes
    # to order blocks by timestamp
    r.db(dbname).table('bigchain').index_create(
        'block_timestamp', r.row['block']['timestamp']).run(conn)
    # to order blocks by block number
    r.db(dbname).table('bigchain').index_create(
        'block_number', r.row['block']['block_number']).run(conn)
    # to order transactions by timestamp
    r.db(dbname).table('backlog').index_create(
        'transaction_timestamp', r.row['transaction']['timestamp']).run(conn)
    # to query the bigchain for a transaction id
    r.db(dbname).table('bigchain').index_create(
        'transaction_id', r.row['block']['transactions']['id'],
        multi=True).run(conn)
    # compound index to read transactions from the backlog per assignee
    r.db(dbname).table('backlog')\
        .index_create('assignee__transaction_timestamp', [r.row['assignee'], r.row['transaction']['timestamp']])\
        .run(conn)
    # secondary index for payload hash
    r.db(dbname).table('bigchain')\
        .index_create('payload_uuid', r.row['block']['transactions']['transaction']['data']['uuid'], multi=True)\
        .run(conn)

    # wait for rethinkdb to finish creating secondary indexes
    r.db(dbname).table('backlog').index_wait().run(conn)
    r.db(dbname).table('bigchain').index_wait().run(conn)

    logger.info(' - genesis block')
    b.create_genesis_block()
    logger.info('Done, have fun!')