-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathFilePathDatabase.java
562 lines (511 loc) · 20.5 KB
/
FilePathDatabase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
package org.telegram.messenger;
import android.os.Looper;
import android.util.LongSparseArray;
import org.telegram.SQLite.SQLiteCursor;
import org.telegram.SQLite.SQLiteDatabase;
import org.telegram.SQLite.SQLiteException;
import org.telegram.SQLite.SQLitePreparedStatement;
import org.telegram.ui.Storage.CacheModel;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
public class FilePathDatabase {
public final static int FLAG_LOCALLY_CREATED = 1; //file is locally created, skip file size check in FileLoader
private DispatchQueue dispatchQueue;
private final int currentAccount;
private SQLiteDatabase database;
private File cacheFile;
private File shmCacheFile;
private final ConcurrentHashMap<String, String> cache = new ConcurrentHashMap<>();
private final static int LAST_DB_VERSION = 7;
private final static String DATABASE_NAME = "file_to_path";
private final static String DATABASE_BACKUP_NAME = "file_to_path_backup";
public final static int MESSAGE_TYPE_VIDEO_MESSAGE = 0;
private final FileMeta metaTmp = new FileMeta();
boolean databaseCreated;
public FilePathDatabase(int currentAccount) {
this.currentAccount = currentAccount;
}
public void createDatabase(int tryCount, boolean fromBackup) {
File filesDir = ApplicationLoader.getFilesDirFixed();
if (currentAccount != 0) {
filesDir = new File(filesDir, "account" + currentAccount + "/");
filesDir.mkdirs();
}
cacheFile = new File(filesDir, DATABASE_NAME + ".db");
shmCacheFile = new File(filesDir, DATABASE_NAME + ".db-shm");
boolean createTable = false;
if (!cacheFile.exists()) {
createTable = true;
}
try {
database = new SQLiteDatabase(cacheFile.getPath());
database.executeFast("PRAGMA secure_delete = ON").stepThis().dispose();
database.executeFast("PRAGMA temp_store = MEMORY").stepThis().dispose();
if (createTable) {
database.executeFast("CREATE TABLE paths(document_id INTEGER, dc_id INTEGER, type INTEGER, path TEXT, flags INTEGER, PRIMARY KEY(document_id, dc_id, type));").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS path_in_paths ON paths(path);").stepThis().dispose();
database.executeFast("CREATE TABLE paths_by_dialog_id(path TEXT PRIMARY KEY, dialog_id INTEGER, message_id INTEGER, message_type INTEGER);").stepThis().dispose();
database.executeFast("PRAGMA user_version = " + LAST_DB_VERSION).stepThis().dispose();
} else {
int version = database.executeInt("PRAGMA user_version");
if (BuildVars.LOGS_ENABLED) {
FileLog.d("current files db version = " + version);
}
if (version == 0) {
throw new Exception("malformed");
}
migrateDatabase(version);
//migration
}
if (!fromBackup) {
createBackup();
}
FileLog.d("files db created from_backup= " + fromBackup);
} catch (Exception e) {
if (tryCount < 4) {
if (!fromBackup && restoreBackup()) {
createDatabase(tryCount + 1, true);
return;
} else {
cacheFile.delete();
shmCacheFile.delete();
createDatabase(tryCount + 1, false);
}
}
if (BuildVars.DEBUG_VERSION) {
FileLog.e(e);
}
}
}
private void migrateDatabase(int version) throws SQLiteException {
if (version == 1) {
database.executeFast("CREATE INDEX IF NOT EXISTS path_in_paths ON paths(path);").stepThis().dispose();
database.executeFast("PRAGMA user_version = " + 2).stepThis().dispose();
version = 2;
}
if (version == 2) {
database.executeFast("CREATE TABLE paths_by_dialog_id(path TEXT PRIMARY KEY, dialog_id INTEGER);").stepThis().dispose();
database.executeFast("PRAGMA user_version = " + 3).stepThis().dispose();
version = 3;
}
if (version == 3) {
database.executeFast("ALTER TABLE paths_by_dialog_id ADD COLUMN message_id INTEGER default 0").stepThis().dispose();
database.executeFast("ALTER TABLE paths_by_dialog_id ADD COLUMN message_type INTEGER default 0").stepThis().dispose();
database.executeFast("PRAGMA user_version = " + 4).stepThis().dispose();
version = 4;
}
if (version == 4 || version == 5 || version == 6) {
try {
database.executeFast("ALTER TABLE paths ADD COLUMN flags INTEGER default 0").stepThis().dispose();
} catch (Throwable ignore) {
FileLog.e(ignore);
}
database.executeFast("PRAGMA user_version = " + 7).stepThis().dispose();
}
}
private void createBackup() {
File filesDir = ApplicationLoader.getFilesDirFixed();
if (currentAccount != 0) {
filesDir = new File(filesDir, "account" + currentAccount + "/");
filesDir.mkdirs();
}
File backupCacheFile = new File(filesDir, DATABASE_BACKUP_NAME + ".db");
try {
AndroidUtilities.copyFile(cacheFile, backupCacheFile);
FileLog.d("file db backup created " + backupCacheFile.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
}
private boolean restoreBackup() {
File filesDir = ApplicationLoader.getFilesDirFixed();
if (currentAccount != 0) {
filesDir = new File(filesDir, "account" + currentAccount + "/");
filesDir.mkdirs();
}
File backupCacheFile = new File(filesDir, DATABASE_BACKUP_NAME + ".db");
if (!backupCacheFile.exists()) {
return false;
}
try {
return AndroidUtilities.copyFile(backupCacheFile, cacheFile);
} catch (IOException e) {
FileLog.e(e);
}
return false;
}
public String getPath(long documentId, int dc, int type, boolean useQueue) {
final long start = System.currentTimeMillis();
final String key = documentId + "_" + dc + "_" + type;
String path = cache.get(key);
if (path != null) {
if (BuildVars.DEBUG_VERSION) {
FileLog.d("get file path cached id=" + documentId + " dc=" + dc + " type=" + type + " path=" + path + " in " + (System.currentTimeMillis() - start) + "ms");
}
return path;
}
if (dispatchQueue != null && dispatchQueue.getHandler() != null && Thread.currentThread() == dispatchQueue.getHandler().getLooper().getThread()) {
useQueue = false;
}
if (useQueue) {
CountDownLatch syncLatch = new CountDownLatch(1);
String[] res = new String[1];
postRunnable(() -> {
ensureDatabaseCreated();
if (database != null) {
SQLiteCursor cursor = null;
try {
cursor = database.queryFinalized("SELECT path FROM paths WHERE document_id = " + documentId + " AND dc_id = " + dc + " AND type = " + type);
if (cursor.next()) {
res[0] = cursor.stringValue(0);
if (BuildVars.DEBUG_VERSION) {
FileLog.d("get file path id=" + documentId + " dc=" + dc + " type=" + type + " path=" + res[0] + " in " + (System.currentTimeMillis() - start) + "ms");
}
}
} catch (Throwable e) {
FileLog.e(e);
} finally {
if (cursor != null) {
cursor.dispose();
}
}
}
syncLatch.countDown();
});
try {
syncLatch.await();
} catch (Exception ignore) {
}
if (res[0] != null) {
cache.put(key, res[0]);
}
return res[0];
} else {
if (database == null) {
return null;
}
SQLiteCursor cursor = null;
String res = null;
try {
cursor = database.queryFinalized("SELECT path FROM paths WHERE document_id = " + documentId + " AND dc_id = " + dc + " AND type = " + type);
if (cursor.next()) {
res = cursor.stringValue(0);
if (BuildVars.DEBUG_VERSION) {
FileLog.d("get file path id=" + documentId + " dc=" + dc + " type=" + type + " path=" + res + " in " + (System.currentTimeMillis() - start) + "ms");
}
}
} catch (SQLiteException e) {
FileLog.e(e);
} finally {
if (cursor != null) {
cursor.dispose();
}
}
if (res != null) {
cache.put(key, res);
}
return res;
}
}
public void ensureDatabaseCreated() {
if (!databaseCreated) {
if (!NativeLoader.loaded()) {
int tryCount = 0;
while (!NativeLoader.loaded()) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
tryCount++;
if (tryCount > 5) {
break;
}
}
}
createDatabase(0, false);
databaseCreated = true;
}
}
public void putPath(long id, int dc, int type, int flags, String path) {
postRunnable(() -> {
if (BuildVars.DEBUG_VERSION) {
FileLog.d("put file path id=" + id + " dc=" + dc + " type=" + type + " path=" + path);
}
ensureDatabaseCreated();
if (database == null) {
return;
}
SQLitePreparedStatement state = null;
SQLitePreparedStatement deleteState = null;
try {
if (path != null) {
deleteState = database.executeFast("DELETE FROM paths WHERE path = ?");
deleteState.bindString(1, path);
deleteState.step();
state = database.executeFast("REPLACE INTO paths VALUES(?, ?, ?, ?, ?)");
state.requery();
state.bindLong(1, id);
state.bindInteger(2, dc);
state.bindInteger(3, type);
state.bindString(4, path);
state.bindInteger(5, flags);
state.step();
state.dispose();
cache.put(id + "_" + dc + "_" + type, path);
} else {
database.executeFast("DELETE FROM paths WHERE document_id = " + id + " AND dc_id = " + dc + " AND type = " + type).stepThis().dispose();
cache.remove(id + "_" + dc + "_" + type);
}
} catch (SQLiteException e) {
FileLog.e(e);
} finally {
if (deleteState != null) {
deleteState.dispose();
}
if (state != null) {
state.dispose();
}
}
});
}
public void checkMediaExistance(ArrayList<MessageObject> messageObjects) {
if (messageObjects.isEmpty()) {
return;
}
ArrayList<MessageObject> arrayListFinal = new ArrayList<>(messageObjects);
CountDownLatch syncLatch = new CountDownLatch(1);
long time = System.currentTimeMillis();
long[] threadTime = new long[1];
postToFrontRunnable(() -> {
long threadTimeLocal = System.currentTimeMillis();
ensureDatabaseCreated();
try {
for (int i = 0; i < arrayListFinal.size(); i++) {
MessageObject messageObject = arrayListFinal.get(i);
messageObject.checkMediaExistance(false);
}
threadTime[0] = System.currentTimeMillis() - threadTimeLocal;
} catch (Throwable e) {
FileLog.e(e);
} finally {
syncLatch.countDown();
}
});
try {
syncLatch.await();
} catch (InterruptedException e) {
FileLog.e(e);
}
FileLog.d("checkMediaExistance size=" + messageObjects.size() + " time=" + (System.currentTimeMillis() - time) + " thread_time=" + threadTime[0]);
if (BuildVars.DEBUG_VERSION) {
if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
FileLog.e(new Exception("warning, not allowed in main thread"));
}
}
}
public void clear() {
cache.clear();
postRunnable(() -> {
ensureDatabaseCreated();
try {
database.executeFast("DELETE FROM paths WHERE 1").stepThis().dispose();
database.executeFast("DELETE FROM paths_by_dialog_id WHERE 1").stepThis().dispose();
} catch (Exception e) {
FileLog.e(e);
}
});
}
public boolean hasAnotherRefOnFile(String path) {
CountDownLatch syncLatch = new CountDownLatch(1);
boolean[] res = new boolean[]{false};
postRunnable(() -> {
ensureDatabaseCreated();
try {
SQLiteCursor cursor = database.queryFinalized("SELECT document_id FROM paths WHERE path = '" + path + "'");
if (cursor.next()) {
res[0] = true;
}
} catch (Exception e) {
FileLog.e(e);
} finally {
syncLatch.countDown();
}
});
try {
syncLatch.await();
} catch (InterruptedException e) {
FileLog.e(e);
}
return res[0];
}
public void saveFileDialogId(File file, FileMeta fileMeta) {
if (file == null || fileMeta == null) {
return;
}
postRunnable(() -> {
ensureDatabaseCreated();
SQLitePreparedStatement state = null;
try {
state = database.executeFast("REPLACE INTO paths_by_dialog_id VALUES(?, ?, ?, ?)");
state.requery();
state.bindString(1, shield(file.getPath()));
state.bindLong(2, fileMeta.dialogId);
state.bindInteger(3, fileMeta.messageId);
state.bindInteger(4, fileMeta.messageType);
state.step();
} catch (Exception e) {
FileLog.e(e);
} finally {
if (state != null) {
state.dispose();
}
}
});
}
public FileMeta getFileDialogId(File file, FileMeta metaTmp) {
if (file == null) {
return null;
}
if (metaTmp == null) {
metaTmp = this.metaTmp;
}
long dialogId = 0;
int messageId = 0;
int messageType = 0;
SQLiteCursor cursor = null;
try {
cursor = database.queryFinalized("SELECT dialog_id, message_id, message_type FROM paths_by_dialog_id WHERE path = '" + shield(file.getPath()) + "'");
if (cursor.next()) {
dialogId = cursor.longValue(0);
messageId = cursor.intValue(1);
messageType = cursor.intValue(2);
}
} catch (Exception e) {
FileLog.e(e);
} finally {
if (cursor != null) {
cursor.dispose();
}
}
metaTmp.dialogId = dialogId;
metaTmp.messageId = messageId;
metaTmp.messageType = messageType;
return metaTmp;
}
private String shield(String path) {
return path.replace("'","").replace("\"","");
}
public DispatchQueue getQueue() {
ensureQueueExist();
return dispatchQueue;
}
public void removeFiles(List<CacheModel.FileInfo> filesToRemove) {
postRunnable(() -> {
try {
ensureDatabaseCreated();
database.beginTransaction();
for (int i = 0; i < filesToRemove.size(); i++) {
database.executeFast("DELETE FROM paths_by_dialog_id WHERE path = '" + shield(filesToRemove.get(i).file.getPath()) + "'").stepThis().dispose();
}
} catch (Throwable e) {
FileLog.e(e);
} finally {
database.commitTransaction();
}
});
}
public LongSparseArray<ArrayList<CacheByChatsController.KeepMediaFile>> lookupFiles(ArrayList<? extends CacheByChatsController.KeepMediaFile> keepMediaFiles) {
CountDownLatch syncLatch = new CountDownLatch(1);
LongSparseArray<ArrayList<CacheByChatsController.KeepMediaFile>> filesByDialogId = new LongSparseArray<>();
postRunnable(() -> {
try {
ensureDatabaseCreated();
FileMeta fileMetaTmp = new FileMeta();
for (int i = 0; i < keepMediaFiles.size(); i++) {
FileMeta fileMeta = getFileDialogId(keepMediaFiles.get(i).file, fileMetaTmp);
if (fileMeta != null && fileMeta.dialogId != 0) {
ArrayList<CacheByChatsController.KeepMediaFile> list = filesByDialogId.get(fileMeta.dialogId);
if (list == null) {
list = new ArrayList<>();
filesByDialogId.put(fileMeta.dialogId, list);
}
keepMediaFiles.get(i).isStory = fileMeta.messageType == MessageObject.TYPE_STORY;
list.add(keepMediaFiles.get(i));
}
}
} catch (Throwable e) {
FileLog.e(e);
} finally {
syncLatch.countDown();
}
});
try {
syncLatch.await();
} catch (InterruptedException e) {
FileLog.e(e);
}
return filesByDialogId;
}
private void postRunnable(Runnable runnable) {
ensureQueueExist();
dispatchQueue.postRunnable(runnable);
}
private void postToFrontRunnable(Runnable runnable) {
ensureQueueExist();
dispatchQueue.postToFrontRunnable(runnable);
}
private void ensureQueueExist() {
if (dispatchQueue == null) {
synchronized (this) {
if (dispatchQueue == null) {
dispatchQueue = new DispatchQueue("files_database_queue_" + currentAccount);
dispatchQueue.setPriority(Thread.MAX_PRIORITY);
}
}
}
}
public boolean isLocallyCreated(String path) {
CountDownLatch syncLatch = new CountDownLatch(1);
boolean[] res = new boolean[]{false};
postRunnable(() -> {
ensureDatabaseCreated();
try {
SQLiteCursor cursor = database.queryFinalized("SELECT flags FROM paths WHERE path = '" + path + "'");
if (cursor.next()) {
res[0] = (cursor.intValue(0) & FLAG_LOCALLY_CREATED) != 0;
}
} catch (Exception e) {
FileLog.e(e);
} finally {
syncLatch.countDown();
}
});
try {
syncLatch.await();
} catch (InterruptedException e) {
FileLog.e(e);
}
return res[0];
}
public static class PathData {
public final long id;
public final int dc;
public final int type;
public PathData(long documentId, int dcId, int type) {
this.id = documentId;
this.dc = dcId;
this.type = type;
}
}
public static class FileMeta {
public long dialogId;
public int messageId;
public int messageType;
public long messageSize;
}
}