Rename FaceRecognition table

This commit is contained in:
Ming Ming 2023-07-05 01:34:39 +08:00
parent 383e38a579
commit c920a6bc36
12 changed files with 103 additions and 92 deletions

View file

@ -207,12 +207,6 @@ Future<void> _initDiContainer(InitIsolateType isolateType) async {
const AlbumRemoteDataSource2(), AlbumSqliteDbDataSource2(c.sqliteDb));
c.albumRepo2Remote = const BasicAlbumRepo2(AlbumRemoteDataSource2());
c.albumRepo2Local = BasicAlbumRepo2(AlbumSqliteDbDataSource2(c.sqliteDb));
c.faceRecognitionPersonRepo = const BasicFaceRecognitionPersonRepo(
FaceRecognitionPersonRemoteDataSource());
c.faceRecognitionPersonRepoRemote = const BasicFaceRecognitionPersonRepo(
FaceRecognitionPersonRemoteDataSource());
c.faceRecognitionPersonRepoLocal = BasicFaceRecognitionPersonRepo(
FaceRecognitionPersonSqliteDbDataSource(c.sqliteDb));
c.fileRepo = FileRepo(FileCachedDataSource(c));
c.fileRepoRemote = const FileRepo(FileWebdavDataSource());
c.fileRepoLocal = FileRepo(FileSqliteDbDataSource(c));
@ -228,6 +222,12 @@ Future<void> _initDiContainer(InitIsolateType isolateType) async {
const NcAlbumRemoteDataSource(), NcAlbumSqliteDbDataSource(c.sqliteDb));
c.ncAlbumRepoRemote = const BasicNcAlbumRepo(NcAlbumRemoteDataSource());
c.ncAlbumRepoLocal = BasicNcAlbumRepo(NcAlbumSqliteDbDataSource(c.sqliteDb));
c.faceRecognitionPersonRepo = const BasicFaceRecognitionPersonRepo(
FaceRecognitionPersonRemoteDataSource());
c.faceRecognitionPersonRepoRemote = const BasicFaceRecognitionPersonRepo(
FaceRecognitionPersonRemoteDataSource());
c.faceRecognitionPersonRepoLocal = BasicFaceRecognitionPersonRepo(
FaceRecognitionPersonSqliteDbDataSource(c.sqliteDb));
c.touchManager = TouchManager(c);

View file

@ -434,8 +434,4 @@ extension DiContainerExtension on DiContainer {
DiContainer withLocalFileRepo() => copyWith(fileRepo: OrNull(fileRepoLocal));
DiContainer withRemoteTagRepo() => copyWith(tagRepo: OrNull(tagRepoRemote));
DiContainer withLocalTagRepo() => copyWith(tagRepo: OrNull(tagRepoLocal));
DiContainer withRemoteFaceRecognitionPersonRepo() => copyWith(
faceRecognitionPersonRepo: OrNull(faceRecognitionPersonRepoRemote));
DiContainer withLocalFaceRecognitionPersonRepo() => copyWith(
faceRecognitionPersonRepo: OrNull(faceRecognitionPersonRepoLocal));
}

View file

@ -72,7 +72,7 @@ class FaceRecognitionPersonSqliteDbDataSource
Future<List<FaceRecognitionPerson>> getPersons(Account account) async {
_log.info("[getPersons] $account");
final dbPersons = await sqliteDb.use((db) async {
return await db.allPersons(account: sql.ByAccount.app(account));
return await db.allFaceRecognitionPersons(account: sql.ByAccount.app(account));
});
return dbPersons
.map((p) {

View file

@ -164,7 +164,7 @@ class SearchSqliteDbDataSource implements SearchDataSource {
// "Ada" will return results from "Ada Crook" but NOT "Adabelle"
try {
final dbPersons = await _c.sqliteDb.use((db) async {
return await db.personsByName(
return await db.faceRecognitionPersonsByName(
appAccount: account,
name: criteria.input,
);
@ -172,7 +172,7 @@ class SearchSqliteDbDataSource implements SearchDataSource {
if (dbPersons.isEmpty) {
return [];
}
final persons = (await dbPersons.convertToAppPerson())
final persons = (await dbPersons.convertToAppFaceRecognitionPerson())
.map((p) => PersonBuilder.byFaceRecognitionPerson(account, p))
.toList();
_log.info(

View file

@ -18,8 +18,8 @@ import 'package:nc_photos/platform/k.dart' as platform_k;
import 'package:np_codegen/np_codegen.dart';
part 'database.g.dart';
part 'database_extension.dart';
part 'database/nc_album_extension.dart';
part 'database_extension.dart';
// remember to also update the truncate method after adding a new table
@npLog
@ -36,7 +36,7 @@ part 'database/nc_album_extension.dart';
Albums,
AlbumShares,
Tags,
Persons,
FaceRecognitionPersons,
NcAlbums,
NcAlbumItems,
],
@ -47,7 +47,7 @@ class SqliteDb extends _$SqliteDb {
}) : super(executor ?? platform.openSqliteConnection());
@override
get schemaVersion => 5;
get schemaVersion => 6;
@override
get migration => MigrationStrategy(
@ -86,7 +86,7 @@ class SqliteDb extends _$SqliteDb {
await transaction(() async {
if (from < 2) {
await m.createTable(tags);
await m.createTable(persons);
await m.createTable(faceRecognitionPersons);
await _createIndexV2(m);
}
if (from < 3) {
@ -100,6 +100,11 @@ class SqliteDb extends _$SqliteDb {
await m.createTable(ncAlbums);
await m.createTable(ncAlbumItems);
}
if (from < 6) {
if (from >= 2) {
await m.renameTable(faceRecognitionPersons, "persons");
}
}
});
} catch (e, stackTrace) {
_log.shout("[onUpgrade] Failed upgrading sqlite db", e, stackTrace);
@ -119,8 +124,8 @@ class SqliteDb extends _$SqliteDb {
Future<void> _createIndexV2(Migrator m) async {
await m.createIndex(Index("tags_server_index",
"CREATE INDEX tags_server_index ON tags(server);"));
await m.createIndex(Index("persons_account_index",
"CREATE INDEX persons_account_index ON persons(account);"));
await m.createIndex(Index("face_recognition_persons_account_index",
"CREATE INDEX face_recognition_persons_account_index ON face_recognition_persons(account);"));
}
Future<void> _createIndexV3(Migrator m) async {

View file

@ -3905,11 +3905,12 @@ class TagsCompanion extends UpdateCompanion<Tag> {
}
}
class $PersonsTable extends Persons with TableInfo<$PersonsTable, Person> {
class $FaceRecognitionPersonsTable extends FaceRecognitionPersons
with TableInfo<$FaceRecognitionPersonsTable, FaceRecognitionPerson> {
@override
final GeneratedDatabase attachedDatabase;
final String? _alias;
$PersonsTable(this.attachedDatabase, [this._alias]);
$FaceRecognitionPersonsTable(this.attachedDatabase, [this._alias]);
static const VerificationMeta _rowIdMeta = const VerificationMeta('rowId');
@override
late final GeneratedColumn<int> rowId = GeneratedColumn<int>(
@ -3948,11 +3949,12 @@ class $PersonsTable extends Persons with TableInfo<$PersonsTable, Person> {
List<GeneratedColumn> get $columns =>
[rowId, account, name, thumbFaceId, count];
@override
String get aliasedName => _alias ?? 'persons';
String get aliasedName => _alias ?? 'face_recognition_persons';
@override
String get actualTableName => 'persons';
String get actualTableName => 'face_recognition_persons';
@override
VerificationContext validateIntegrity(Insertable<Person> instance,
VerificationContext validateIntegrity(
Insertable<FaceRecognitionPerson> instance,
{bool isInserting = false}) {
final context = VerificationContext();
final data = instance.toColumns(true);
@ -3996,9 +3998,9 @@ class $PersonsTable extends Persons with TableInfo<$PersonsTable, Person> {
{account, name},
];
@override
Person map(Map<String, dynamic> data, {String? tablePrefix}) {
FaceRecognitionPerson map(Map<String, dynamic> data, {String? tablePrefix}) {
final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : '';
return Person(
return FaceRecognitionPerson(
rowId: attachedDatabase.typeMapping
.read(DriftSqlType.int, data['${effectivePrefix}row_id'])!,
account: attachedDatabase.typeMapping
@ -4013,18 +4015,19 @@ class $PersonsTable extends Persons with TableInfo<$PersonsTable, Person> {
}
@override
$PersonsTable createAlias(String alias) {
return $PersonsTable(attachedDatabase, alias);
$FaceRecognitionPersonsTable createAlias(String alias) {
return $FaceRecognitionPersonsTable(attachedDatabase, alias);
}
}
class Person extends DataClass implements Insertable<Person> {
class FaceRecognitionPerson extends DataClass
implements Insertable<FaceRecognitionPerson> {
final int rowId;
final int account;
final String name;
final int thumbFaceId;
final int count;
const Person(
const FaceRecognitionPerson(
{required this.rowId,
required this.account,
required this.name,
@ -4041,8 +4044,8 @@ class Person extends DataClass implements Insertable<Person> {
return map;
}
PersonsCompanion toCompanion(bool nullToAbsent) {
return PersonsCompanion(
FaceRecognitionPersonsCompanion toCompanion(bool nullToAbsent) {
return FaceRecognitionPersonsCompanion(
rowId: Value(rowId),
account: Value(account),
name: Value(name),
@ -4051,10 +4054,10 @@ class Person extends DataClass implements Insertable<Person> {
);
}
factory Person.fromJson(Map<String, dynamic> json,
factory FaceRecognitionPerson.fromJson(Map<String, dynamic> json,
{ValueSerializer? serializer}) {
serializer ??= driftRuntimeOptions.defaultSerializer;
return Person(
return FaceRecognitionPerson(
rowId: serializer.fromJson<int>(json['rowId']),
account: serializer.fromJson<int>(json['account']),
name: serializer.fromJson<String>(json['name']),
@ -4074,13 +4077,13 @@ class Person extends DataClass implements Insertable<Person> {
};
}
Person copyWith(
FaceRecognitionPerson copyWith(
{int? rowId,
int? account,
String? name,
int? thumbFaceId,
int? count}) =>
Person(
FaceRecognitionPerson(
rowId: rowId ?? this.rowId,
account: account ?? this.account,
name: name ?? this.name,
@ -4089,7 +4092,7 @@ class Person extends DataClass implements Insertable<Person> {
);
@override
String toString() {
return (StringBuffer('Person(')
return (StringBuffer('FaceRecognitionPerson(')
..write('rowId: $rowId, ')
..write('account: $account, ')
..write('name: $name, ')
@ -4104,7 +4107,7 @@ class Person extends DataClass implements Insertable<Person> {
@override
bool operator ==(Object other) =>
identical(this, other) ||
(other is Person &&
(other is FaceRecognitionPerson &&
other.rowId == this.rowId &&
other.account == this.account &&
other.name == this.name &&
@ -4112,20 +4115,21 @@ class Person extends DataClass implements Insertable<Person> {
other.count == this.count);
}
class PersonsCompanion extends UpdateCompanion<Person> {
class FaceRecognitionPersonsCompanion
extends UpdateCompanion<FaceRecognitionPerson> {
final Value<int> rowId;
final Value<int> account;
final Value<String> name;
final Value<int> thumbFaceId;
final Value<int> count;
const PersonsCompanion({
const FaceRecognitionPersonsCompanion({
this.rowId = const Value.absent(),
this.account = const Value.absent(),
this.name = const Value.absent(),
this.thumbFaceId = const Value.absent(),
this.count = const Value.absent(),
});
PersonsCompanion.insert({
FaceRecognitionPersonsCompanion.insert({
this.rowId = const Value.absent(),
required int account,
required String name,
@ -4135,7 +4139,7 @@ class PersonsCompanion extends UpdateCompanion<Person> {
name = Value(name),
thumbFaceId = Value(thumbFaceId),
count = Value(count);
static Insertable<Person> custom({
static Insertable<FaceRecognitionPerson> custom({
Expression<int>? rowId,
Expression<int>? account,
Expression<String>? name,
@ -4151,13 +4155,13 @@ class PersonsCompanion extends UpdateCompanion<Person> {
});
}
PersonsCompanion copyWith(
FaceRecognitionPersonsCompanion copyWith(
{Value<int>? rowId,
Value<int>? account,
Value<String>? name,
Value<int>? thumbFaceId,
Value<int>? count}) {
return PersonsCompanion(
return FaceRecognitionPersonsCompanion(
rowId: rowId ?? this.rowId,
account: account ?? this.account,
name: name ?? this.name,
@ -4189,7 +4193,7 @@ class PersonsCompanion extends UpdateCompanion<Person> {
@override
String toString() {
return (StringBuffer('PersonsCompanion(')
return (StringBuffer('FaceRecognitionPersonsCompanion(')
..write('rowId: $rowId, ')
..write('account: $account, ')
..write('name: $name, ')
@ -5297,7 +5301,8 @@ abstract class _$SqliteDb extends GeneratedDatabase {
late final $AlbumsTable albums = $AlbumsTable(this);
late final $AlbumSharesTable albumShares = $AlbumSharesTable(this);
late final $TagsTable tags = $TagsTable(this);
late final $PersonsTable persons = $PersonsTable(this);
late final $FaceRecognitionPersonsTable faceRecognitionPersons =
$FaceRecognitionPersonsTable(this);
late final $NcAlbumsTable ncAlbums = $NcAlbumsTable(this);
late final $NcAlbumItemsTable ncAlbumItems = $NcAlbumItemsTable(this);
@override
@ -5316,7 +5321,7 @@ abstract class _$SqliteDb extends GeneratedDatabase {
albums,
albumShares,
tags,
persons,
faceRecognitionPersons,
ncAlbums,
ncAlbumItems
];
@ -5411,7 +5416,7 @@ abstract class _$SqliteDb extends GeneratedDatabase {
on: TableUpdateQuery.onTableName('accounts',
limitUpdateKind: UpdateKind.delete),
result: [
TableUpdate('persons', kind: UpdateKind.delete),
TableUpdate('face_recognition_persons', kind: UpdateKind.delete),
],
),
WritePropagation(

View file

@ -526,17 +526,18 @@ extension SqliteDbExtension on SqliteDb {
}
}
Future<List<Person>> allPersons({
Future<List<FaceRecognitionPerson>> allFaceRecognitionPersons({
required ByAccount account,
}) {
assert((account.sqlAccount != null) != (account.appAccount != null));
if (account.sqlAccount != null) {
final query = select(persons)
final query = select(faceRecognitionPersons)
..where((t) => t.account.equals(account.sqlAccount!.rowId));
return query.get();
} else {
final query = select(persons).join([
innerJoin(accounts, accounts.rowId.equalsExp(persons.account),
final query = select(faceRecognitionPersons).join([
innerJoin(
accounts, accounts.rowId.equalsExp(faceRecognitionPersons.account),
useColumns: false),
innerJoin(servers, servers.rowId.equalsExp(accounts.server),
useColumns: false),
@ -544,18 +545,18 @@ extension SqliteDbExtension on SqliteDb {
..where(servers.address.equals(account.appAccount!.url))
..where(accounts.userId
.equals(account.appAccount!.userId.toCaseInsensitiveString()));
return query.map((r) => r.readTable(persons)).get();
return query.map((r) => r.readTable(faceRecognitionPersons)).get();
}
}
Future<List<Person>> personsByName({
Future<List<FaceRecognitionPerson>> faceRecognitionPersonsByName({
Account? sqlAccount,
app.Account? appAccount,
required String name,
}) {
assert((sqlAccount != null) != (appAccount != null));
if (sqlAccount != null) {
final query = select(persons)
final query = select(faceRecognitionPersons)
..where((t) => t.account.equals(sqlAccount.rowId))
..where((t) =>
t.name.like(name) |
@ -563,8 +564,9 @@ extension SqliteDbExtension on SqliteDb {
t.name.like("$name %"));
return query.get();
} else {
final query = select(persons).join([
innerJoin(accounts, accounts.rowId.equalsExp(persons.account),
final query = select(faceRecognitionPersons).join([
innerJoin(
accounts, accounts.rowId.equalsExp(faceRecognitionPersons.account),
useColumns: false),
innerJoin(servers, servers.rowId.equalsExp(accounts.server),
useColumns: false),
@ -572,10 +574,10 @@ extension SqliteDbExtension on SqliteDb {
..where(servers.address.equals(appAccount!.url))
..where(
accounts.userId.equals(appAccount.userId.toCaseInsensitiveString()))
..where(persons.name.like(name) |
persons.name.like("% $name") |
persons.name.like("$name %"));
return query.map((r) => r.readTable(persons)).get();
..where(faceRecognitionPersons.name.like(name) |
faceRecognitionPersons.name.like("% $name") |
faceRecognitionPersons.name.like("$name %"));
return query.map((r) => r.readTable(faceRecognitionPersons)).get();
}
}
@ -638,7 +640,7 @@ extension SqliteDbExtension on SqliteDb {
await delete(albums).go();
await delete(albumShares).go();
await delete(tags).go();
await delete(persons).go();
await delete(faceRecognitionPersons).go();
await delete(ncAlbums).go();
await delete(ncAlbumItems).go();

View file

@ -219,7 +219,7 @@ class Tags extends Table {
];
}
class Persons extends Table {
class FaceRecognitionPersons extends Table {
IntColumn get rowId => integer().autoIncrement()();
IntColumn get account =>
integer().references(Accounts, #rowId, onDelete: KeyAction.cascade)();

View file

@ -35,19 +35,20 @@ extension AppTagListExtension on List<Tag> {
}
}
extension SqlPersonListExtension on List<sql.Person> {
Future<List<FaceRecognitionPerson>> convertToAppPerson() {
extension SqlFaceRecognitionPersonListExtension
on List<sql.FaceRecognitionPerson> {
Future<List<FaceRecognitionPerson>> convertToAppFaceRecognitionPerson() {
return computeAll(SqliteFaceRecognitionPersonConverter.fromSql);
}
}
extension AppPersonListExtension on List<FaceRecognitionPerson> {
Future<List<sql.PersonsCompanion>> convertToPersonCompanion(
sql.Account? dbAccount) {
extension AppFaceRecognitionPersonListExtension on List<FaceRecognitionPerson> {
Future<List<sql.FaceRecognitionPersonsCompanion>>
convertToFaceRecognitionPersonCompanion(sql.Account? dbAccount) {
return map((p) => {
"account": dbAccount,
"person": p,
}).computeAll(_convertAppPerson);
}).computeAll(_convertAppFaceRecognitionPerson);
}
}
@ -239,16 +240,16 @@ class SqliteTagConverter {
}
class SqliteFaceRecognitionPersonConverter {
static FaceRecognitionPerson fromSql(sql.Person person) =>
static FaceRecognitionPerson fromSql(sql.FaceRecognitionPerson person) =>
FaceRecognitionPerson(
name: person.name,
thumbFaceId: person.thumbFaceId,
count: person.count,
);
static sql.PersonsCompanion toSql(
static sql.FaceRecognitionPersonsCompanion toSql(
sql.Account? dbAccount, FaceRecognitionPerson person) =>
sql.PersonsCompanion(
sql.FaceRecognitionPersonsCompanion(
account:
dbAccount == null ? const Value.absent() : Value(dbAccount.rowId),
name: Value(person.name),
@ -331,7 +332,7 @@ sql.TagsCompanion _convertAppTag(Map map) {
return SqliteTagConverter.toSql(account, tag);
}
sql.PersonsCompanion _convertAppPerson(Map map) {
sql.FaceRecognitionPersonsCompanion _convertAppFaceRecognitionPerson(Map map) {
final account = map["account"] as sql.Account?;
final person = map["person"] as FaceRecognitionPerson;
return SqliteFaceRecognitionPersonConverter.toSql(account, person);

View file

@ -56,26 +56,26 @@ class SyncFaceRecognitionPerson {
await db.batch((batch) {
for (final d in deletes) {
batch.deleteWhere(
db.persons,
(sql.$PersonsTable p) =>
db.faceRecognitionPersons,
(sql.$FaceRecognitionPersonsTable p) =>
p.account.equals(dbAccount.rowId) & p.name.equals(d.name),
);
}
for (final u in updates) {
batch.update(
db.persons,
sql.PersonsCompanion(
db.faceRecognitionPersons,
sql.FaceRecognitionPersonsCompanion(
name: sql.Value(u.name),
thumbFaceId: sql.Value(u.thumbFaceId),
count: sql.Value(u.count),
),
where: (sql.$PersonsTable p) =>
where: (sql.$FaceRecognitionPersonsTable p) =>
p.account.equals(dbAccount.rowId) & p.name.equals(u.name),
);
}
for (final i in inserts) {
batch.insert(
db.persons,
db.faceRecognitionPersons,
SqliteFaceRecognitionPersonConverter.toSql(dbAccount, i),
mode: sql.InsertMode.insertOrIgnore,
);

View file

@ -319,7 +319,7 @@ Future<void> _truncate() async {
"albums",
"album_shares",
"tags",
"persons",
"face_recognition_persons",
"nc_albums",
"nc_album_items",
});

View file

@ -43,8 +43,8 @@ Future<void> _new() async {
await c.sqliteDb.insertAccountOf(account);
await c.sqliteDb.batch((batch) {
batch.insert(
c.sqliteDb.persons,
sql.PersonsCompanion.insert(
c.sqliteDb.faceRecognitionPersons,
sql.FaceRecognitionPersonsCompanion.insert(
account: 1, name: "test1", thumbFaceId: 1, count: 1),
);
});
@ -83,12 +83,12 @@ Future<void> _remove() async {
await c.sqliteDb.transaction(() async {
await c.sqliteDb.insertAccountOf(account);
await c.sqliteDb.batch((batch) {
batch.insertAll(c.sqliteDb.persons, [
sql.PersonsCompanion.insert(
batch.insertAll(c.sqliteDb.faceRecognitionPersons, [
sql.FaceRecognitionPersonsCompanion.insert(
account: 1, name: "test1", thumbFaceId: 1, count: 1),
sql.PersonsCompanion.insert(
sql.FaceRecognitionPersonsCompanion.insert(
account: 1, name: "test2", thumbFaceId: 2, count: 10),
sql.PersonsCompanion.insert(
sql.FaceRecognitionPersonsCompanion.insert(
account: 1, name: "test3", thumbFaceId: 3, count: 100),
]);
});
@ -127,10 +127,10 @@ Future<void> _update() async {
await c.sqliteDb.transaction(() async {
await c.sqliteDb.insertAccountOf(account);
await c.sqliteDb.batch((batch) {
batch.insertAll(c.sqliteDb.persons, [
sql.PersonsCompanion.insert(
batch.insertAll(c.sqliteDb.faceRecognitionPersons, [
sql.FaceRecognitionPersonsCompanion.insert(
account: 1, name: "test1", thumbFaceId: 1, count: 1),
sql.PersonsCompanion.insert(
sql.FaceRecognitionPersonsCompanion.insert(
account: 1, name: "test2", thumbFaceId: 2, count: 10),
]);
});
@ -150,11 +150,13 @@ Future<void> _update() async {
Future<Map<String, Set<FaceRecognitionPerson>>> _listSqliteDbPersons(
sql.SqliteDb db) async {
final query = db.select(db.persons).join([
sql.innerJoin(db.accounts, db.accounts.rowId.equalsExp(db.persons.account)),
final query = db.select(db.faceRecognitionPersons).join([
sql.innerJoin(db.accounts,
db.accounts.rowId.equalsExp(db.faceRecognitionPersons.account)),
]);
final result = await query
.map((r) => Tuple2(r.readTable(db.accounts), r.readTable(db.persons)))
.map((r) => Tuple2(
r.readTable(db.accounts), r.readTable(db.faceRecognitionPersons)))
.get();
final product = <String, Set<FaceRecognitionPerson>>{};
for (final r in result) {