1 //  Copyright © 2019 650 Industries. All rights reserved.
2 
3 #import <ABI47_0_0EXUpdates/ABI47_0_0EXUpdatesAsset.h>
4 #import <ABI47_0_0EXUpdates/ABI47_0_0EXUpdatesConfig.h>
5 #import <ABI47_0_0EXUpdates/ABI47_0_0EXUpdatesUpdate.h>
6 
7 NS_ASSUME_NONNULL_BEGIN
8 
9 typedef NS_ENUM(NSInteger, ABI47_0_0EXUpdatesDatabaseHashType) {
10   ABI47_0_0EXUpdatesDatabaseHashTypeSha1 = 0
11 };
12 
13 /**
14  * SQLite database that keeps track of updates currently loaded/loading to disk, including the
15  * update manifest and metadata, status, and the individual assets (including bundles/bytecode) that
16  * comprise the update. (Assets themselves are stored on the device's file system, and a relative
17  * path is kept in SQLite.)
18  *
19  * SQLite allows a many-to-many relationship between updates and assets, which means we can keep
20  * only one copy of each asset on disk at a time while also being able to clear unused assets with
21  * relative ease (see ABI47_0_0EXUpdatesReaper).
22  *
23  * Occasionally it's necessary to add migrations when the data structures for updates or assets must
24  * change. Extra care must be taken here, since these migrations will happen on users' devices for
25  * apps we do not control. See
26  * https://github.com/expo/expo/blob/main/packages/expo-updates/guides/migrations.md for step by
27  * step instructions.
28  *
29  * ABI47_0_0EXUpdatesDatabase provides a serial queue on which all database operations must be run (methods
30  * in this class will assert). This is primarily for control over what high-level operations
31  * involving the database can occur simultaneously - e.g. we don't want to be trying to download a
32  * new update at the same time ABI47_0_0EXUpdatesReaper is running.
33  *
34  * The `scopeKey` field in various methods here is only relevant in environments such as Expo Go in
35  * which updates from multiple scopes can be launched.
36  */
37 @interface ABI47_0_0EXUpdatesDatabase : NSObject
38 
39 @property (nonatomic, strong) dispatch_queue_t databaseQueue;
40 
41 - (BOOL)openDatabaseInDirectory:(NSURL *)directory withError:(NSError ** _Nullable)error;
42 - (void)closeDatabase;
43 
44 - (void)addUpdate:(ABI47_0_0EXUpdatesUpdate *)update error:(NSError ** _Nullable)error;
45 - (void)addNewAssets:(NSArray<ABI47_0_0EXUpdatesAsset *> *)assets toUpdateWithId:(NSUUID *)updateId error:(NSError ** _Nullable)error;
46 /**
47  * This method may return NO if a matching entry for the existing asset cannot be found in the database.
48  * In this case, the error pointer will not be set.
49  */
50 - (BOOL)addExistingAsset:(ABI47_0_0EXUpdatesAsset *)asset toUpdateWithId:(NSUUID *)updateId error:(NSError ** _Nullable)error;
51 - (void)updateAsset:(ABI47_0_0EXUpdatesAsset *)asset error:(NSError ** _Nullable)error;
52 - (void)mergeAsset:(ABI47_0_0EXUpdatesAsset *)asset withExistingEntry:(ABI47_0_0EXUpdatesAsset *)existingAsset error:(NSError ** _Nullable)error;
53 - (void)markUpdateFinished:(ABI47_0_0EXUpdatesUpdate *)update error:(NSError ** _Nullable)error;
54 - (void)markUpdateAccessed:(ABI47_0_0EXUpdatesUpdate *)update error:(NSError ** _Nullable)error;
55 - (void)incrementSuccessfulLaunchCountForUpdate:(ABI47_0_0EXUpdatesUpdate *)update error:(NSError ** _Nullable)error;
56 - (void)incrementFailedLaunchCountForUpdate:(ABI47_0_0EXUpdatesUpdate *)update error:(NSError ** _Nullable)error;
57 - (void)setScopeKey:(NSString *)scopeKey onUpdate:(ABI47_0_0EXUpdatesUpdate *)update error:(NSError ** _Nullable)error;
58 - (void)markMissingAssets:(NSArray<ABI47_0_0EXUpdatesAsset *> *)assets error:(NSError ** _Nullable)error;
59 
60 - (void)deleteUpdates:(NSArray<ABI47_0_0EXUpdatesUpdate *> *)updates error:(NSError ** _Nullable)error;
61 - (nullable NSArray<ABI47_0_0EXUpdatesAsset *> *)deleteUnusedAssetsWithError:(NSError ** _Nullable)error;
62 
63 /**
64  * This method ignores the scopeKey field in the config object and returns ALL updates in the database.
65  * The config object is passed along to each update object, even if its scopeKey doesn't match.
66  *
67  * Updates returned from this method should not be used to launch.
68  */
69 - (nullable NSArray<ABI47_0_0EXUpdatesUpdate *> *)allUpdatesWithConfig:(ABI47_0_0EXUpdatesConfig *)config error:(NSError ** _Nullable)error;
70 - (nullable NSArray<ABI47_0_0EXUpdatesUpdate *> *)allUpdatesWithStatus:(ABI47_0_0EXUpdatesUpdateStatus)status config:(ABI47_0_0EXUpdatesConfig *)config error:(NSError ** _Nullable)error;
71 - (nullable NSArray<NSUUID *> *)allUpdateIdsWithStatus:(ABI47_0_0EXUpdatesUpdateStatus)status error:(NSError ** _Nullable)error;
72 - (nullable NSArray<ABI47_0_0EXUpdatesUpdate *> *)launchableUpdatesWithConfig:(ABI47_0_0EXUpdatesConfig *)config error:(NSError ** _Nullable)error;
73 - (nullable ABI47_0_0EXUpdatesUpdate *)updateWithId:(NSUUID *)updateId config:(ABI47_0_0EXUpdatesConfig *)config error:(NSError ** _Nullable)error;
74 - (nullable NSArray<ABI47_0_0EXUpdatesAsset *> *)allAssetsWithError:(NSError ** _Nullable)error;
75 - (nullable NSArray<ABI47_0_0EXUpdatesAsset *> *)assetsWithUpdateId:(NSUUID *)updateId error:(NSError ** _Nullable)error;
76 - (nullable ABI47_0_0EXUpdatesAsset *)assetWithKey:(nullable NSString *)key error:(NSError ** _Nullable)error;
77 
78 - (nullable NSDictionary *)serverDefinedHeadersWithScopeKey:(NSString *)scopeKey error:(NSError ** _Nullable)error;
79 - (nullable NSDictionary *)manifestFiltersWithScopeKey:(NSString *)scopeKey error:(NSError ** _Nullable)error;
80 - (nullable NSDictionary *)staticBuildDataWithScopeKey:(NSString *)scopeKey error:(NSError ** _Nullable)error;
81 - (void)setServerDefinedHeaders:(NSDictionary *)serverDefinedHeaders withScopeKey:(NSString *)scopeKey error:(NSError ** _Nullable)error;
82 - (void)setManifestFilters:(NSDictionary *)manifestFilters withScopeKey:(NSString *)scopeKey error:(NSError ** _Nullable)error;
83 - (void)setMetadataWithManifest:(ABI47_0_0EXUpdatesUpdate *)updateManifest error:(NSError ** _Nullable)error;
84 - (void)setStaticBuildData:(NSDictionary *)staticBuildData withScopeKey:(NSString *)scopeKey error:(NSError ** _Nullable)error;
85 
86 @end
87 
88 NS_ASSUME_NONNULL_END
89