Exemplo n.º 1
0
def recreate_db(chat_history_dir):
    """
    """
    global _LocalStorage
    try:
        _LocalStorage.close()
    except Exception as exc:
        lg.warn('failed closing local storage : %r' % exc)
    _LocalStorage = None
    dbs = Database(chat_history_dir)
    dbs.custom_header = message_index.make_custom_header()
    temp_dir = os.path.join(settings.ChatHistoryDir(), 'tmp')
    if os.path.isdir(temp_dir):
        bpio._dir_remove(temp_dir)
    orig_dir = os.path.join(settings.ChatHistoryDir(), 'orig')
    if os.path.isdir(orig_dir):
        bpio._dir_remove(orig_dir)
    dbt = Database(temp_dir)
    dbt.custom_header = message_index.make_custom_header()
    source_opened = False
    try:
        dbs.open()
        source_opened = True
    except Exception as exc:
        lg.warn('failed open local storage : %r' % exc)
    # patch_flush_fsync(dbs)
    dbt.create()
    dbt.close()
    refresh_indexes(dbt, reindex=False)
    dbt.open()
    # patch_flush_fsync(dbt)
    if source_opened:
        for c in dbs.all('id'):
            del c['_rev']
            dbt.insert(c)
    dbt.close()
    if source_opened:
        dbs.close()
    os.rename(dbs.path, orig_dir)
    os.rename(dbt.path, dbs.path)
    _LocalStorage = Database(chat_history_dir)
    _LocalStorage.custom_header = message_index.make_custom_header()
    db().open()
    # patch_flush_fsync(db())
    if refresh_indexes(db(), rewrite=False, reindex=False):
        bpio._dir_remove(orig_dir)
        lg.info('local DB re-created in %r' % chat_history_dir)
    else:
        lg.err('local DB is broken !!!')
Exemplo n.º 2
0
def init():
    global _LocalStorage
    if _LocalStorage is not None:
        lg.warn('local storage already initialized')
        return
    chat_history_dir = os.path.join(settings.ChatHistoryDir(), 'current')
    _LocalStorage = Database(chat_history_dir)
    _LocalStorage.custom_header = message_index.make_custom_header()
    if _Debug:
        lg.out(_DebugLevel, 'message_db.init in %s' % chat_history_dir)
    if db().exists():
        try:
            db().open()
        except:
            temp_dir = os.path.join(settings.ChatHistoryDir(), 'tmp')
            if os.path.isdir(temp_dir):
                bpio._dir_remove(temp_dir)
            tmpdb = regenerate_indexes(temp_dir)
            rewrite_indexes(db(), tmpdb)
            bpio._dir_remove(temp_dir)
            db().open()
            db().reindex()
    else:
        db().create()
    refresh_indexes(db())
Exemplo n.º 3
0
def recreate_db(chat_history_dir):
    """
    """
    global _LocalStorage
    temp_dir = os.path.join(settings.ChatHistoryDir(), 'tmp')
    if os.path.isdir(temp_dir):
        bpio._dir_remove(temp_dir)
    tmpdb = regenerate_indexes(temp_dir)
    try:
        db().close()
    except:
        pass
    rewrite_indexes(db(), tmpdb)
    bpio._dir_remove(temp_dir)
    try:
        db().open()
        db().reindex()
    except:
        # really bad... we will lose whole data
        _LocalStorage = Database(chat_history_dir)
        _LocalStorage.custom_header = message_index.make_custom_header()
        try:
            _LocalStorage.destroy()
        except:
            pass
        try:
            _LocalStorage.create()
        except Exception as exc:
            lg.warn('failed to create local storage: %r' % exc)
Exemplo n.º 4
0
def init(reindex=True, recreate=True):
    global _LocalStorage
    if _LocalStorage is not None:
        lg.warn('local storage already initialized')
        return
    chat_history_dir = os.path.join(settings.ChatHistoryDir(), 'current')
    _LocalStorage = Database(chat_history_dir)
    _LocalStorage.custom_header = message_index.make_custom_header()
    if _Debug:
        lg.out(_DebugLevel, 'message_db.init in %s' % chat_history_dir)
    if db().exists():
        try:
            db().open()
            # patch_flush_fsync(db())
        except Exception as exc:
            lg.err('failed to open local database : %r' % exc)
            if not recreate:
                raise Exception('failed to open database')
            lg.info('local DB will be recreated now')
            recreate_db(chat_history_dir)
    else:
        lg.info('create fresh local DB')
        db().create()
    if reindex:
        if not refresh_indexes(db(), rewrite=False, reindex=True):
            lg.err('failed to refresh indexes')
            if not recreate:
                raise Exception('failed to refresh indexes')
            lg.info('local DB will be recreated now')
            recreate_db(chat_history_dir)
Exemplo n.º 5
0
def regenerate_indexes(temp_dir):
    """
    """
    tmpdb = Database(temp_dir)
    tmpdb.custom_header = message_index.make_custom_header()
    tmpdb.create()
    refresh_indexes(tmpdb)
    tmpdb.close()
    return tmpdb
Exemplo n.º 6
0
def init():
    global _LocalStorage
    if _LocalStorage is not None:
        lg.warn('local storage already initialized')
        return
    chat_history_dir = os.path.join(settings.ChatHistoryDir(), 'current')
    _LocalStorage = Database(chat_history_dir)
    _LocalStorage.custom_header = message_index.make_custom_header()
    if _Debug:
        lg.out(_DebugLevel, 'message_db.init in %s' % chat_history_dir)
    if db().exists():
        try:
            db().open()
        except:
            lg.err('failed to open database, local DB will be recreated')
            recreate_db(chat_history_dir)
    else:
        db().create()
    if not refresh_indexes(db()):
        lg.err('failed to refresh indexes, local DB will be recreated')
        recreate_db(chat_history_dir)
        refresh_indexes(db())