def delete_from_all_tables():
    table_names = get_all_table_names()
    conn = sqlite3.connect(flask_utilities.get_db_name())
    cur = conn.cursor()
    with conn:
        for table in table_names:
            print(f"Deleting table: {table}")
            cur.execute(f"DELETE FROM {table};")
    conn.close()
def update_replication_table(filename, replicated_node):
    sql_stmt = f"""
        INSERT INTO replication_data (filename, replicated_node)
        VALUES ("{filename}", "{replicated_node}");
    """
    conn = sqlite3.connect(flask_utilities.get_db_name())
    with conn:
        conn.execute(sql_stmt)
    conn.close()
def update_master_table(filename, primary_node):
    sql_stmt = f"""
        INSERT INTO master_node (filename, primary_node)
        VALUES ("{filename}", "{primary_node}");
    """
    conn = sqlite3.connect(flask_utilities.get_db_name())
    with conn:
        conn.execute(sql_stmt)
    conn.close()
Exemple #4
0
def main():
    db_name = flask_utilities.get_db_name()
    conn = sqlite3.connect(db_name)
    print(f"Created database {db_name}.")
    conn.execute(get_sql_create_master_table())
    print("Table created for master node.")
    conn.execute(get_sql_create_replication_table())
    print("Table created for storing replication data.")
    conn.close()
    print("Setup done!")
def get_all_table_names():
    table_names = []
    sql_stmt = """
        SELECT name FROM sqlite_master WHERE type='table';
    """
    conn = sqlite3.connect(flask_utilities.get_db_name())
    cur = conn.cursor()
    with conn:
        cur.execute(sql_stmt)
        data = cur.fetchall() # -> [('master_node',), ('sn__sn1__5000',), ...]
        table_names = [row[0] for row in data] # -> ['master_node', 'sn__sn1__5000', ...]
    conn.close()
    return table_names
def return_pnode_of_file(filename):
    pnode = None
    sql_stmt = f"""
        SELECT primary_node FROM master_node
        WHERE filename="{filename}";
    """
    conn = sqlite3.connect(flask_utilities.get_db_name())
    cur = conn.cursor()
    with conn:
        cur.execute(sql_stmt)
        data = cur.fetchone()
        if data:
            pnode = data[0]
    conn.close()
    return pnode
def generate_unique_filename(filename):
    sql_stmt = f"""
        SELECT filename FROM master_node
        WHERE filename="{filename}";
    """
    conn = sqlite3.connect(flask_utilities.get_db_name())
    cur = conn.cursor()
    with conn:
        cur.execute(sql_stmt)
        data = cur.fetchone()
        if data:
            filename, ext = filename.rsplit('.', 1)
            filename = f"{filename}_{flask_utilities.generate_random_str(5)}.{ext}"
    conn.close()
    return filename
def get_sns_with_file_copy(filename):
    all_sns_with_replica = []
    sql_stmt = f"""
        SELECT replicated_node FROM replication_data
        WHERE filename="{filename}";
    """
    conn = sqlite3.connect(flask_utilities.get_db_name())
    cur = conn.cursor()
    with conn:
        cur.execute(sql_stmt)
        data = cur.fetchall()  # -> [('sn0:5000',), ('sn3:6050',)]
        if data:
            all_sns_with_replica = [row[0]
                                    for row in data]  # -> ['sn0', 'sn3']
    conn.close()
    return all_sns_with_replica