Skip to content

Session importer API

locallore.indexing.importer

Incrementally import session files into SQLite.

logger = logging.getLogger(__name__) module-attribute

ImportResult(files_seen=0, files_changed=0, files_added=0, files_removed=0, messages_added=0, messages_removed=0, errors=0) dataclass

files_seen = 0 class-attribute instance-attribute

files_changed = 0 class-attribute instance-attribute

files_added = 0 class-attribute instance-attribute

files_removed = 0 class-attribute instance-attribute

messages_added = 0 class-attribute instance-attribute

messages_removed = 0 class-attribute instance-attribute

errors = 0 class-attribute instance-attribute

import_sessions(connection, root)

Source code in src/locallore/indexing/importer.py
def import_sessions(connection: sqlite3.Connection, root: Path) -> ImportResult:
    sources = discover(root)
    checkpoints = {
        row["path"]: row
        for row in connection.execute("SELECT * FROM import_files").fetchall()
    }
    changed = added = errors = files_added = 0
    files_removed = messages_removed = 0
    source_paths = {source.relative_path for source in sources}
    with connection:
        missing_paths = sorted(set(checkpoints) - source_paths)
        for path in missing_paths:
            removed = connection.execute(
                "SELECT count(*) FROM messages m "
                "JOIN sessions s ON s.id = m.session_id "
                "WHERE s.source_path = ?",
                (path,),
            ).fetchone()[0]
            connection.execute("DELETE FROM sessions WHERE source_path = ?", (path,))
            connection.execute("DELETE FROM import_files WHERE path = ?", (path,))
            files_removed += 1
            messages_removed += removed
        for source in sources:
            checkpoint = checkpoints.get(source.relative_path)
            is_changed = not checkpoint or (
                checkpoint["identity"],
                checkpoint["size_bytes"],
                checkpoint["mtime_ns"],
            ) != (source.identity, source.size_bytes, source.mtime_ns)
            if checkpoint is None:
                files_added += 1
            file_added, file_errors, file_removed_messages = _import_file(
                connection, source, checkpoint
            )
            changed += int(is_changed)
            added += file_added
            errors += file_errors
            messages_removed += file_removed_messages
    return ImportResult(
        files_seen=len(sources),
        files_changed=changed,
        files_added=files_added,
        files_removed=files_removed,
        messages_added=added,
        messages_removed=messages_removed,
        errors=errors,
    )