Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

11.0 KB
Raw Blame
import "package:collection/collection.dart";
import "package:logging/logging.dart";
import "package:photos/db/common/base.dart";
import "package:photos/models/photo_review/photo_review_models.dart";
import "package:sqlite_async/sqlite_async.dart";

class PhotoReviewDB with SqlDbBase {
  static const _databaseName = "ente.photo_review.db";
  static const decisionsTable = "review_decisions";
  static const monthsTable = "review_months";

  static const columnFileKey = "file_key";
  static const columnUploadedFileID = "uploaded_file_id";
  static const columnGeneratedID = "generated_id";
  static const columnLocalID = "local_id";
  static const columnMonthKey = "month_key";
  static const columnDecision = "decision";
  static const columnUpdatedAt = "updated_at";
  static const columnMarkedReviewed = "marked_reviewed";
  static const columnFreedBytes = "freed_bytes";
  static const columnFreedCount = "freed_count";

  static const _lookupChunkSize = 500;

  static final Logger _logger = Logger("PhotoReviewDB");

  static final _migrationScripts = [
    '''
    CREATE TABLE IF NOT EXISTS $decisionsTable (
      $columnFileKey TEXT PRIMARY KEY,
      $columnUploadedFileID INTEGER,
      $columnGeneratedID INTEGER,
      $columnLocalID TEXT,
      $columnMonthKey TEXT NOT NULL,
      $columnDecision INTEGER NOT NULL,
      $columnUpdatedAt INTEGER NOT NULL
    );
    CREATE INDEX IF NOT EXISTS review_decisions_month_index
    ON $decisionsTable($columnMonthKey);
    CREATE INDEX IF NOT EXISTS review_decisions_local_id_index
    ON $decisionsTable($columnLocalID);
    CREATE INDEX IF NOT EXISTS review_decisions_generated_id_index
    ON $decisionsTable($columnGeneratedID);
    CREATE TABLE IF NOT EXISTS $monthsTable (
      $columnMonthKey TEXT PRIMARY KEY,
      $columnMarkedReviewed INTEGER NOT NULL DEFAULT 0,
      $columnFreedBytes INTEGER NOT NULL DEFAULT 0,
      $columnFreedCount INTEGER NOT NULL DEFAULT 0,
      $columnUpdatedAt INTEGER NOT NULL
    );
    ''',
  ];

  PhotoReviewDB._privateConstructor();

  static final PhotoReviewDB instance = PhotoReviewDB._privateConstructor();

  Future<SqliteDatabase> get database => getOrOpenDatabase(
    () => openMigratedDatabase(
      _databaseName,
      _migrationScripts,
      logPath: (path) => _logger.info("DB path $path"),
    ),
  );

  Future<List<ReviewDecisionRecord>> getAllDecisions() async {
    final db = await database;
    final rows = await db.getAll("SELECT * FROM $decisionsTable");
    return rows.map(_decisionFromRow).toList();
  }

  Future<List<ReviewDecisionRecord>> getDecisionsForFiles(
    List<String> fileKeys,
    List<String> localIDs, {
    List<int> generatedIDs = const [],
  }) async {
    final db = await database;
    return db.readTransaction((tx) async {
      final records = <ReviewDecisionRecord>[];
      for (final (column, values) in [
        (columnFileKey, fileKeys),
        (columnLocalID, localIDs),
        (columnGeneratedID, generatedIDs),
      ]) {
        for (final chunk in values.slices(_lookupChunkSize)) {
          final placeholders = SqlDbBase.getParams(chunk.length);
          final rows = await tx.getAll(
            "SELECT * FROM $decisionsTable WHERE $column IN ($placeholders)",
            chunk,
          );
          records.addAll(rows.map(_decisionFromRow));
        }
      }
      return records;
    });
  }

  Future<Map<String, ReviewMonthRecord>> getMonthRecords() async {
    final db = await database;
    final rows = await db.getAll("SELECT * FROM $monthsTable");
    return {
      for (final row in rows) row[columnMonthKey] as String: _monthFromRow(row),
    };
  }

  Future<ReviewMonthRecord?> getMonthRecord(String monthKey) async {
    final db = await database;
    final row = await db.getOptional(
      "SELECT * FROM $monthsTable WHERE $columnMonthKey = ?",
      [monthKey],
    );
    return row == null ? null : _monthFromRow(row);
  }

  Future<void> saveDecisions(
    List<ReviewDecisionRecord> records,
    String monthKey,
    int updatedAt,
  ) async {
    if (records.isEmpty) return;
    final db = await database;
    await db.writeTransaction((tx) async {
      await _upsertDecisions(tx, records);
      await _touchMonth(tx, monthKey, updatedAt);
    });
  }

  Future<void> rekeyDecisions(Map<String, ReviewDecisionRecord> records) async {
    if (records.isEmpty) return;
    final db = await database;
    await db.writeTransaction((tx) async {
      await tx.executeBatch(
        '''
        UPDATE OR IGNORE $decisionsTable
        SET $columnFileKey = ?, $columnUploadedFileID = ?,
            $columnLocalID = ?, $columnMonthKey = ?, $columnGeneratedID = ?
        WHERE $columnFileKey = ?
          AND ($columnGeneratedID IS NULL OR $columnGeneratedID = ?)
        ''',
        [
          for (final entry in records.entries)
            [
              entry.value.fileKey,
              entry.value.uploadedFileID,
              entry.value.localID,
              entry.value.monthKey,
              entry.value.generatedID,
              entry.key,
              entry.value.generatedID,
            ],
        ],
      );
      await tx.executeBatch(
        '''
        DELETE FROM $decisionsTable WHERE $columnFileKey = ?
          AND ($columnGeneratedID IS NULL OR $columnGeneratedID = ?)
        ''',
        [
          for (final entry in records.entries)
            if (entry.key != entry.value.fileKey)
              [entry.key, entry.value.generatedID],
        ],
      );
    });
  }

  Future<void> deleteDecision(
    String fileKey,
    String monthKey,
    int updatedAt, {
    int? generatedID,
    int? uploadedFileID,
  }) async {
    final db = await database;
    await db.writeTransaction((tx) async {
      final record = uploadedFileID == null
          ? null
          : await tx.getOptional(
              "SELECT $columnGeneratedID FROM $decisionsTable WHERE $columnFileKey = ?",
              [fileKey],
            );
      await tx.execute(
        '''
        DELETE FROM $decisionsTable
        WHERE $columnFileKey = ?
          OR ($columnGeneratedID IN (?, ?)
              AND ($columnUploadedFileID IS NULL OR ? IS NULL))
        ''',
        [fileKey, generatedID, record?[columnGeneratedID], uploadedFileID],
      );
      await _touchMonth(tx, monthKey, updatedAt);
    });
  }

  Future<void> recordDeletion(
    List<ReviewDecisionRecord> records,
    String monthKey,
    int freedBytes,
    int updatedAt,
  ) async {
    if (records.isEmpty) return;
    final db = await database;
    await db.writeTransaction((tx) async {
      await tx.executeBatch(
        '''
        DELETE FROM $decisionsTable
        WHERE $columnFileKey != ?
          AND ($columnGeneratedID = ? OR $columnGeneratedID = (
            SELECT $columnGeneratedID FROM $decisionsTable
            WHERE $columnFileKey = ?
          ))
          AND ($columnUploadedFileID IS NULL OR ? IS NULL)
        ''',
        [
          for (final record in records)
            [
              record.fileKey,
              record.generatedID,
              record.fileKey,
              record.uploadedFileID,
            ],
        ],
      );
      await _upsertDecisions(tx, records);
      await tx.execute(
        '''
        INSERT INTO $monthsTable (
          $columnMonthKey, $columnFreedBytes, $columnFreedCount, $columnUpdatedAt
        ) VALUES (?, ?, ?, ?)
        ON CONFLICT($columnMonthKey) DO UPDATE SET
          $columnFreedBytes = $columnFreedBytes + excluded.$columnFreedBytes,
          $columnFreedCount = $columnFreedCount + excluded.$columnFreedCount,
          $columnUpdatedAt = excluded.$columnUpdatedAt
        ''',
        [monthKey, freedBytes, records.length, updatedAt],
      );
    });
  }

  Future<void> markMonthsReviewed(List<String> monthKeys, int updatedAt) async {
    if (monthKeys.isEmpty) return;
    final db = await database;
    await db.executeBatch(
      '''
      INSERT INTO $monthsTable (
        $columnMonthKey, $columnMarkedReviewed, $columnUpdatedAt
      ) VALUES (?, 1, ?)
      ON CONFLICT($columnMonthKey) DO UPDATE SET
        $columnMarkedReviewed = 1,
        $columnUpdatedAt = excluded.$columnUpdatedAt
      ''',
      [
        for (final key in monthKeys) [key, updatedAt],
      ],
    );
  }

  Future<void> unmarkMonthReviewed(String monthKey, int updatedAt) async {
    final db = await database;
    await db.execute(
      '''
      UPDATE $monthsTable
      SET $columnMarkedReviewed = 0, $columnUpdatedAt = ?
      WHERE $columnMonthKey = ?
      ''',
      [updatedAt, monthKey],
    );
  }

  Future<void> resetMonths(List<String> monthKeys) async {
    if (monthKeys.isEmpty) return;
    final db = await database;
    await db.writeTransaction((tx) async {
      await tx.executeBatch(
        "DELETE FROM $decisionsTable WHERE $columnMonthKey = ?",
        [
          for (final key in monthKeys) [key],
        ],
      );
      await tx.executeBatch(
        "DELETE FROM $monthsTable WHERE $columnMonthKey = ?",
        [
          for (final key in monthKeys) [key],
        ],
      );
    });
  }

  Future<void> clearTable() async {
    final db = await database;
    await db.writeTransaction((tx) async {
      await tx.execute("DELETE FROM $decisionsTable");
      await tx.execute("DELETE FROM $monthsTable");
    });
  }

  Future<void> _upsertDecisions(
    SqliteWriteContext tx,
    List<ReviewDecisionRecord> records,
  ) {
    return tx.executeBatch(
      '''
      INSERT OR REPLACE INTO $decisionsTable (
        $columnFileKey, $columnUploadedFileID, $columnLocalID,
        $columnMonthKey, $columnDecision, $columnUpdatedAt, $columnGeneratedID
      ) VALUES (?, ?, ?, ?, ?, ?, ?)
      ''',
      [
        for (final record in records)
          [
            record.fileKey,
            record.uploadedFileID,
            record.localID,
            record.monthKey,
            record.decision.storedValue,
            record.updatedAt,
            record.generatedID,
          ],
      ],
    );
  }

  Future<void> _touchMonth(
    SqliteWriteContext tx,
    String monthKey,
    int updatedAt,
  ) {
    return tx.execute(
      '''
      INSERT INTO $monthsTable ($columnMonthKey, $columnUpdatedAt)
      VALUES (?, ?)
      ON CONFLICT($columnMonthKey) DO UPDATE SET
        $columnUpdatedAt = excluded.$columnUpdatedAt
      ''',
      [monthKey, updatedAt],
    );
  }

  ReviewDecisionRecord _decisionFromRow(Map<String, dynamic> row) {
    return ReviewDecisionRecord(
      fileKey: row[columnFileKey] as String,
      uploadedFileID: row[columnUploadedFileID] as int?,
      generatedID: row[columnGeneratedID] as int?,
      localID: row[columnLocalID] as String?,
      monthKey: row[columnMonthKey] as String,
      decision: ReviewDecision.fromStoredValue(row[columnDecision] as int),
      updatedAt: row[columnUpdatedAt] as int,
    );
  }

  ReviewMonthRecord _monthFromRow(Map<String, dynamic> row) {
    return ReviewMonthRecord(
      monthKey: row[columnMonthKey] as String,
      markedReviewed: (row[columnMarkedReviewed] as int) == 1,
      freedBytes: row[columnFreedBytes] as int,
      freedCount: row[columnFreedCount] as int,
      updatedAt: row[columnUpdatedAt] as int,
    );
  }
}