FRepo.m 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <Foundation/Foundation.h>
  17. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  18. #import "FirebaseDatabase/Sources/Api/Private/FIRDataSnapshot_Private.h"
  19. #import "FirebaseDatabase/Sources/Api/Private/FIRDatabaseQuery_Private.h"
  20. #import "FirebaseDatabase/Sources/Api/Private/FIRDatabase_Private.h"
  21. #import "FirebaseDatabase/Sources/Api/Private/FIRMutableData_Private.h"
  22. #import "FirebaseDatabase/Sources/Api/Private/FIRTransactionResult_Private.h"
  23. #import "FirebaseDatabase/Sources/Constants/FConstants.h"
  24. #import "FirebaseDatabase/Sources/Core/FListenProvider.h"
  25. #import "FirebaseDatabase/Sources/Core/FQuerySpec.h"
  26. #import "FirebaseDatabase/Sources/Core/FRepo.h"
  27. #import "FirebaseDatabase/Sources/Core/FRepoManager.h"
  28. #import "FirebaseDatabase/Sources/Core/FRepo_Private.h"
  29. #import "FirebaseDatabase/Sources/Core/FServerValues.h"
  30. #import "FirebaseDatabase/Sources/Core/FSnapshotHolder.h"
  31. #import "FirebaseDatabase/Sources/Core/FSyncTree.h"
  32. #import "FirebaseDatabase/Sources/Core/FWriteRecord.h"
  33. #import "FirebaseDatabase/Sources/Core/Utilities/FTree.h"
  34. #import "FirebaseDatabase/Sources/Core/View/FEventRaiser.h"
  35. #import "FirebaseDatabase/Sources/Core/View/FEventRegistration.h"
  36. #import "FirebaseDatabase/Sources/Core/View/FValueEventRegistration.h"
  37. #import "FirebaseDatabase/Sources/FClock.h"
  38. #import "FirebaseDatabase/Sources/FIRDatabaseConfig_Private.h"
  39. #import "FirebaseDatabase/Sources/Persistence/FCachePolicy.h"
  40. #import "FirebaseDatabase/Sources/Persistence/FLevelDBStorageEngine.h"
  41. #import "FirebaseDatabase/Sources/Persistence/FPersistenceManager.h"
  42. #import "FirebaseDatabase/Sources/Public/FirebaseDatabase/FIRDataSnapshot.h"
  43. #import "FirebaseDatabase/Sources/Public/FirebaseDatabase/FIRMutableData.h"
  44. #import "FirebaseDatabase/Sources/Public/FirebaseDatabase/FIRTransactionResult.h"
  45. #import "FirebaseDatabase/Sources/Snapshot/FEmptyNode.h"
  46. #import "FirebaseDatabase/Sources/Snapshot/FSnapshotUtilities.h"
  47. #import "FirebaseDatabase/Sources/Utilities/FAtomicNumber.h"
  48. #import "FirebaseDatabase/Sources/Utilities/Tuples/FTupleNodePath.h"
  49. #import "FirebaseDatabase/Sources/Utilities/Tuples/FTupleSetIdPath.h"
  50. #import "FirebaseDatabase/Sources/Utilities/Tuples/FTupleTransaction.h"
  51. #import <dlfcn.h>
  52. #if TARGET_OS_IOS || TARGET_OS_TV
  53. #import <UIKit/UIKit.h>
  54. #endif
  55. @interface FRepo ()
  56. @property(nonatomic, strong) FOffsetClock *serverClock;
  57. @property(nonatomic, strong) FPersistenceManager *persistenceManager;
  58. @property(nonatomic, strong) FIRDatabase *database;
  59. @property(nonatomic, strong, readwrite) FAuthenticationManager *auth;
  60. @property(nonatomic, strong) FSyncTree *infoSyncTree;
  61. @property(nonatomic) NSInteger writeIdCounter;
  62. @property(nonatomic) BOOL hijackHash;
  63. @property(nonatomic, strong) FTree *transactionQueueTree;
  64. @property(nonatomic) BOOL loggedTransactionPersistenceWarning;
  65. /**
  66. * Test only. For load testing the server.
  67. */
  68. @property(nonatomic, strong) id (^interceptServerDataCallback)
  69. (NSString *pathString, id data);
  70. @end
  71. @implementation FRepo
  72. - (id)initWithRepoInfo:(FRepoInfo *)info
  73. config:(FIRDatabaseConfig *)config
  74. database:(FIRDatabase *)database {
  75. self = [super init];
  76. if (self) {
  77. self.repoInfo = info;
  78. self.config = config;
  79. self.database = database;
  80. // Access can occur outside of shared queue, so the clock needs to be
  81. // initialized here
  82. self.serverClock =
  83. [[FOffsetClock alloc] initWithClock:[FSystemClock clock] offset:0];
  84. self.connection = [[FPersistentConnection alloc]
  85. initWithRepoInfo:self.repoInfo
  86. dispatchQueue:[FIRDatabaseQuery sharedQueue]
  87. config:self.config];
  88. // Needs to be called before authentication manager is instantiated
  89. self.eventRaiser =
  90. [[FEventRaiser alloc] initWithQueue:self.config.callbackQueue];
  91. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  92. [self deferredInit];
  93. });
  94. }
  95. return self;
  96. }
  97. - (void)deferredInit {
  98. // TODO: cleanup on dealloc
  99. __weak FRepo *weakSelf = self;
  100. [self.config.contextProvider listenForAuthTokenChanges:^(NSString *token) {
  101. [weakSelf.connection refreshAuthToken:token];
  102. }];
  103. [self.config.contextProvider
  104. listenForAppCheckTokenChanges:^(NSString *token) {
  105. [weakSelf.connection refreshAppCheckToken:token];
  106. }];
  107. // Open connection now so that by the time we are connected the deferred
  108. // init has run This relies on the fact that all callbacks run on repos
  109. // queue
  110. self.connection.delegate = self;
  111. [self.connection open];
  112. self.dataUpdateCount = 0;
  113. self.rangeMergeUpdateCount = 0;
  114. self.interceptServerDataCallback = nil;
  115. if (self.config.persistenceEnabled) {
  116. NSString *repoHashString =
  117. [NSString stringWithFormat:@"%@_%@", self.repoInfo.host,
  118. self.repoInfo.namespace];
  119. NSString *persistencePrefix =
  120. [NSString stringWithFormat:@"%@/%@", self.config.sessionIdentifier,
  121. repoHashString];
  122. id<FCachePolicy> cachePolicy = [[FLRUCachePolicy alloc]
  123. initWithMaxSize:self.config.persistenceCacheSizeBytes];
  124. id<FStorageEngine> engine;
  125. if (self.config.forceStorageEngine != nil) {
  126. engine = self.config.forceStorageEngine;
  127. } else {
  128. FLevelDBStorageEngine *levelDBEngine =
  129. [[FLevelDBStorageEngine alloc] initWithPath:persistencePrefix];
  130. // We need the repo info to run the legacy migration. Future
  131. // migrations will be managed by the database itself Remove this
  132. // once we are confident that no-one is using legacy migration
  133. // anymore...
  134. [levelDBEngine runLegacyMigration:self.repoInfo];
  135. engine = levelDBEngine;
  136. }
  137. self.persistenceManager =
  138. [[FPersistenceManager alloc] initWithStorageEngine:engine
  139. cachePolicy:cachePolicy];
  140. } else {
  141. self.persistenceManager = nil;
  142. }
  143. [self initTransactions];
  144. // A list of data pieces and paths to be set when this client disconnects
  145. self.onDisconnect = [[FSparseSnapshotTree alloc] init];
  146. self.infoData = [[FSnapshotHolder alloc] init];
  147. FListenProvider *infoListenProvider = [[FListenProvider alloc] init];
  148. infoListenProvider.startListening =
  149. ^(FQuerySpec *query, NSNumber *tagId, id<FSyncTreeHash> hash,
  150. fbt_nsarray_nsstring onComplete) {
  151. NSArray *infoEvents = @[];
  152. FRepo *strongSelf = weakSelf;
  153. id<FNode> node = [strongSelf.infoData getNode:query.path];
  154. // This is possibly a hack, but we have different semantics for .info
  155. // endpoints. We don't raise null events on initial data...
  156. if (![node isEmpty]) {
  157. infoEvents =
  158. [strongSelf.infoSyncTree applyServerOverwriteAtPath:query.path
  159. newData:node];
  160. [strongSelf.eventRaiser raiseCallback:^{
  161. onComplete(kFWPResponseForActionStatusOk);
  162. }];
  163. }
  164. return infoEvents;
  165. };
  166. infoListenProvider.stopListening = ^(FQuerySpec *query, NSNumber *tagId) {
  167. };
  168. self.infoSyncTree =
  169. [[FSyncTree alloc] initWithListenProvider:infoListenProvider];
  170. FListenProvider *serverListenProvider = [[FListenProvider alloc] init];
  171. serverListenProvider.startListening =
  172. ^(FQuerySpec *query, NSNumber *tagId, id<FSyncTreeHash> hash,
  173. fbt_nsarray_nsstring onComplete) {
  174. [weakSelf.connection listen:query
  175. tagId:tagId
  176. hash:hash
  177. onComplete:^(NSString *status) {
  178. NSArray *events = onComplete(status);
  179. [weakSelf.eventRaiser raiseEvents:events];
  180. }];
  181. // No synchronous events for network-backed sync trees
  182. return @[];
  183. };
  184. serverListenProvider.stopListening = ^(FQuerySpec *query, NSNumber *tag) {
  185. [weakSelf.connection unlisten:query tagId:tag];
  186. };
  187. self.serverSyncTree =
  188. [[FSyncTree alloc] initWithPersistenceManager:self.persistenceManager
  189. listenProvider:serverListenProvider];
  190. [self restoreWrites];
  191. [self updateInfo:kDotInfoConnected withValue:@NO];
  192. [self setupNotifications];
  193. }
  194. - (void)restoreWrites {
  195. NSArray *writes = self.persistenceManager.userWrites;
  196. NSDictionary *serverValues =
  197. [FServerValues generateServerValues:self.serverClock];
  198. __block NSInteger lastWriteId = NSIntegerMin;
  199. [writes enumerateObjectsUsingBlock:^(FWriteRecord *write, NSUInteger idx,
  200. BOOL *stop) {
  201. NSInteger writeId = write.writeId;
  202. fbt_void_nsstring_nsstring callback =
  203. ^(NSString *status, NSString *errorReason) {
  204. [self warnIfWriteFailedAtPath:write.path
  205. status:status
  206. message:@"Persisted write"];
  207. [self ackWrite:writeId
  208. rerunTransactionsAtPath:write.path
  209. status:status];
  210. };
  211. if (lastWriteId >= writeId) {
  212. [NSException raise:NSInternalInconsistencyException
  213. format:@"Restored writes were not in order!"];
  214. }
  215. lastWriteId = writeId;
  216. self.writeIdCounter = writeId + 1;
  217. if ([write isOverwrite]) {
  218. FFLog(@"I-RDB038001", @"Restoring overwrite with id %ld",
  219. (long)write.writeId);
  220. [self.connection putData:[write.overwrite valForExport:YES]
  221. forPath:[write.path toString]
  222. withHash:nil
  223. withCallback:callback];
  224. id<FNode> resolved =
  225. [FServerValues resolveDeferredValueSnapshot:write.overwrite
  226. withSyncTree:self.serverSyncTree
  227. atPath:write.path
  228. serverValues:serverValues];
  229. [self.serverSyncTree applyUserOverwriteAtPath:write.path
  230. newData:resolved
  231. writeId:writeId
  232. isVisible:YES];
  233. } else {
  234. FFLog(@"I-RDB038002", @"Restoring merge with id %ld",
  235. (long)write.writeId);
  236. [self.connection mergeData:[write.merge valForExport:YES]
  237. forPath:[write.path toString]
  238. withCallback:callback];
  239. FCompoundWrite *resolved = [FServerValues
  240. resolveDeferredValueCompoundWrite:write.merge
  241. withSyncTree:self.serverSyncTree
  242. atPath:write.path
  243. serverValues:serverValues];
  244. [self.serverSyncTree applyUserMergeAtPath:write.path
  245. changedChildren:resolved
  246. writeId:writeId];
  247. }
  248. }];
  249. }
  250. - (NSString *)name {
  251. return self.repoInfo.namespace;
  252. }
  253. - (NSString *)description {
  254. return [self.repoInfo description];
  255. }
  256. - (void)interrupt {
  257. [self.connection interruptForReason:kFInterruptReasonRepoInterrupt];
  258. }
  259. - (void)resume {
  260. [self.connection resumeForReason:kFInterruptReasonRepoInterrupt];
  261. }
  262. // NOTE: Typically if you're calling this, you should be in an @autoreleasepool
  263. // block to make sure that ARC kicks in and cleans up things no longer
  264. // referenced (i.e. pendingPutsDB).
  265. - (void)dispose {
  266. [self.connection interruptForReason:kFInterruptReasonRepoInterrupt];
  267. // We need to nil out any references to LevelDB, to make sure the
  268. // LevelDB exclusive locks are released.
  269. [self.persistenceManager close];
  270. }
  271. - (NSInteger)nextWriteId {
  272. return self->_writeIdCounter++;
  273. }
  274. - (NSTimeInterval)serverTime {
  275. return [self.serverClock currentTime];
  276. }
  277. - (void)set:(FPath *)path
  278. withNode:(id<FNode>)node
  279. withCallback:(fbt_void_nserror_ref)onComplete {
  280. id value = [node valForExport:YES];
  281. FFLog(@"I-RDB038003", @"Setting: %@ with %@ pri: %@", [path toString],
  282. [value description], [[node getPriority] val]);
  283. // TODO: Optimize this behavior to either (a) store flag to skip resolving
  284. // where possible and / or (b) store unresolved paths on JSON parse
  285. NSDictionary *serverValues =
  286. [FServerValues generateServerValues:self.serverClock];
  287. id<FNode> existing = [self.serverSyncTree calcCompleteEventCacheAtPath:path
  288. excludeWriteIds:@[]];
  289. id<FNode> newNode =
  290. [FServerValues resolveDeferredValueSnapshot:node
  291. withExisting:existing
  292. serverValues:serverValues];
  293. NSInteger writeId = [self nextWriteId];
  294. [self.persistenceManager saveUserOverwrite:node
  295. atPath:path
  296. writeId:writeId];
  297. NSArray *events = [self.serverSyncTree applyUserOverwriteAtPath:path
  298. newData:newNode
  299. writeId:writeId
  300. isVisible:YES];
  301. [self.eventRaiser raiseEvents:events];
  302. [self.connection putData:value
  303. forPath:[path toString]
  304. withHash:nil
  305. withCallback:^(NSString *status, NSString *errorReason) {
  306. [self warnIfWriteFailedAtPath:path
  307. status:status
  308. message:@"setValue: or removeValue:"];
  309. [self ackWrite:writeId
  310. rerunTransactionsAtPath:path
  311. status:status];
  312. [self callOnComplete:onComplete
  313. withStatus:status
  314. errorReason:errorReason
  315. andPath:path];
  316. }];
  317. FPath *affectedPath = [self abortTransactionsAtPath:path
  318. error:kFTransactionSet];
  319. [self rerunTransactionsForPath:affectedPath];
  320. }
  321. - (void)update:(FPath *)path
  322. withNodes:(FCompoundWrite *)nodes
  323. withCallback:(fbt_void_nserror_ref)callback {
  324. NSDictionary *values = [nodes valForExport:YES];
  325. FFLog(@"I-RDB038004", @"Updating: %@ with %@", [path toString],
  326. [values description]);
  327. NSDictionary *serverValues =
  328. [FServerValues generateServerValues:self.serverClock];
  329. FCompoundWrite *resolved =
  330. [FServerValues resolveDeferredValueCompoundWrite:nodes
  331. withSyncTree:self.serverSyncTree
  332. atPath:path
  333. serverValues:serverValues];
  334. if (!resolved.isEmpty) {
  335. NSInteger writeId = [self nextWriteId];
  336. [self.persistenceManager saveUserMerge:nodes
  337. atPath:path
  338. writeId:writeId];
  339. NSArray *events = [self.serverSyncTree applyUserMergeAtPath:path
  340. changedChildren:resolved
  341. writeId:writeId];
  342. [self.eventRaiser raiseEvents:events];
  343. [self.connection mergeData:values
  344. forPath:[path description]
  345. withCallback:^(NSString *status, NSString *errorReason) {
  346. [self warnIfWriteFailedAtPath:path
  347. status:status
  348. message:@"updateChildValues:"];
  349. [self ackWrite:writeId
  350. rerunTransactionsAtPath:path
  351. status:status];
  352. [self callOnComplete:callback
  353. withStatus:status
  354. errorReason:errorReason
  355. andPath:path];
  356. }];
  357. [nodes enumerateWrites:^(FPath *childPath, id<FNode> node, BOOL *stop) {
  358. FPath *pathFromRoot = [path child:childPath];
  359. FFLog(@"I-RDB038005", @"Cancelling transactions at path: %@",
  360. pathFromRoot);
  361. FPath *affectedPath = [self abortTransactionsAtPath:pathFromRoot
  362. error:kFTransactionSet];
  363. [self rerunTransactionsForPath:affectedPath];
  364. }];
  365. } else {
  366. FFLog(@"I-RDB038006", @"update called with empty data. Doing nothing");
  367. // Do nothing, just call the callback
  368. [self callOnComplete:callback
  369. withStatus:@"ok"
  370. errorReason:nil
  371. andPath:path];
  372. }
  373. }
  374. - (void)onDisconnectCancel:(FPath *)path
  375. withCallback:(fbt_void_nserror_ref)callback {
  376. [self.connection
  377. onDisconnectCancelPath:path
  378. withCallback:^(NSString *status, NSString *errorReason) {
  379. BOOL success =
  380. [status isEqualToString:kFWPResponseForActionStatusOk];
  381. if (success) {
  382. [self.onDisconnect forgetPath:path];
  383. } else {
  384. FFLog(@"I-RDB038007",
  385. @"cancelDisconnectOperations: at %@ failed: %@",
  386. path, status);
  387. }
  388. [self callOnComplete:callback
  389. withStatus:status
  390. errorReason:errorReason
  391. andPath:path];
  392. }];
  393. }
  394. - (void)onDisconnectSet:(FPath *)path
  395. withNode:(id<FNode>)node
  396. withCallback:(fbt_void_nserror_ref)callback {
  397. [self.connection
  398. onDisconnectPutData:[node valForExport:YES]
  399. forPath:path
  400. withCallback:^(NSString *status, NSString *errorReason) {
  401. BOOL success =
  402. [status isEqualToString:kFWPResponseForActionStatusOk];
  403. if (success) {
  404. [self.onDisconnect rememberData:node onPath:path];
  405. } else {
  406. FFWarn(@"I-RDB038008",
  407. @"onDisconnectSetValue: or "
  408. @"onDisconnectRemoveValue: at %@ failed: %@",
  409. path, status);
  410. }
  411. [self callOnComplete:callback
  412. withStatus:status
  413. errorReason:errorReason
  414. andPath:path];
  415. }];
  416. }
  417. - (void)onDisconnectUpdate:(FPath *)path
  418. withNodes:(FCompoundWrite *)nodes
  419. withCallback:(fbt_void_nserror_ref)callback {
  420. if (!nodes.isEmpty) {
  421. NSDictionary *values = [nodes valForExport:YES];
  422. [self.connection
  423. onDisconnectMergeData:values
  424. forPath:path
  425. withCallback:^(NSString *status, NSString *errorReason) {
  426. BOOL success = [status
  427. isEqualToString:kFWPResponseForActionStatusOk];
  428. if (success) {
  429. [nodes enumerateWrites:^(FPath *relativePath,
  430. id<FNode> nodeUnresolved,
  431. BOOL *stop) {
  432. FPath *childPath = [path child:relativePath];
  433. [self.onDisconnect rememberData:nodeUnresolved
  434. onPath:childPath];
  435. }];
  436. } else {
  437. FFWarn(@"I-RDB038009",
  438. @"onDisconnectUpdateChildValues: at %@ "
  439. @"failed %@",
  440. path, status);
  441. }
  442. [self callOnComplete:callback
  443. withStatus:status
  444. errorReason:errorReason
  445. andPath:path];
  446. }];
  447. } else {
  448. // Do nothing, just call the callback
  449. [self callOnComplete:callback
  450. withStatus:@"ok"
  451. errorReason:nil
  452. andPath:path];
  453. }
  454. }
  455. - (void)purgeOutstandingWrites {
  456. FFLog(@"I-RDB038010", @"Purging outstanding writes");
  457. NSArray *events = [self.serverSyncTree removeAllWrites];
  458. [self.eventRaiser raiseEvents:events];
  459. // Abort any transactions
  460. [self abortTransactionsAtPath:[FPath empty] error:kFErrorWriteCanceled];
  461. // Remove outstanding writes from connection
  462. [self.connection purgeOutstandingWrites];
  463. }
  464. - (void)getData:(FIRDatabaseQuery *)query
  465. withCompletionBlock:(void (^)(NSError *__nullable error,
  466. FIRDataSnapshot *__nullable snapshot))block {
  467. FQuerySpec *querySpec = [query querySpec];
  468. id<FNode> node = [self.serverSyncTree getServerValue:[query querySpec]];
  469. if (node != nil) {
  470. [self.eventRaiser raiseCallback:^{
  471. block(nil, [[FIRDataSnapshot alloc]
  472. initWithRef:query.ref
  473. indexedNode:[FIndexedNode
  474. indexedNodeWithNode:node
  475. index:querySpec.index]]);
  476. }];
  477. return;
  478. }
  479. NSNumber *tag = [self.serverSyncTree registerQuery:querySpec];
  480. [self.connection
  481. getDataAtPath:[query.path toString]
  482. withParams:querySpec.params.wireProtocolParams
  483. withCallback:^(NSString *status, id data, NSString *errorReason) {
  484. id<FNode> node;
  485. if (![status isEqualToString:kFWPResponseForActionStatusOk]) {
  486. FFLog(@"I-RDB038024",
  487. @"getValue for query %@ falling back to disk cache",
  488. [querySpec.path toString]);
  489. FIndexedNode *node =
  490. [self.serverSyncTree persistenceServerCache:querySpec];
  491. if (node == nil) {
  492. NSDictionary *errorDict = @{
  493. NSLocalizedFailureReasonErrorKey : errorReason,
  494. NSLocalizedDescriptionKey : [NSString
  495. stringWithFormat:
  496. @"Unable to get latest value for query %@, "
  497. @"client offline with no active listeners "
  498. @"and no matching disk cache entries",
  499. querySpec]
  500. };
  501. [self.eventRaiser raiseCallback:^{
  502. block([NSError errorWithDomain:kFirebaseCoreErrorDomain
  503. code:1
  504. userInfo:errorDict],
  505. nil);
  506. }];
  507. [self.serverSyncTree unregisterQuery:[query querySpec]];
  508. return;
  509. }
  510. [self.eventRaiser raiseCallback:^{
  511. block(nil, [[FIRDataSnapshot alloc] initWithRef:query.ref
  512. indexedNode:node]);
  513. }];
  514. } else {
  515. node = [FSnapshotUtilities nodeFrom:data];
  516. if ([query.querySpec loadsAllData]) {
  517. [self.eventRaiser
  518. raiseEvents:[self.serverSyncTree
  519. applyServerOverwriteAtPath:[query path]
  520. newData:node]];
  521. } else {
  522. [self.eventRaiser
  523. raiseEvents:[self.serverSyncTree
  524. applyTaggedQueryOverwriteAtPath:[query
  525. path]
  526. newData:node
  527. tagId:tag]];
  528. }
  529. [self.eventRaiser raiseCallback:^{
  530. block(
  531. nil,
  532. [[FIRDataSnapshot alloc]
  533. initWithRef:query.ref
  534. indexedNode:[FIndexedNode
  535. indexedNodeWithNode:node
  536. index:querySpec.index]]);
  537. }];
  538. }
  539. [self.serverSyncTree unregisterQuery:querySpec];
  540. }];
  541. }
  542. - (void)addEventRegistration:(id<FEventRegistration>)eventRegistration
  543. forQuery:(FQuerySpec *)query {
  544. NSArray *events = nil;
  545. if ([[query.path getFront] isEqualToString:kDotInfoPrefix]) {
  546. events = [self.infoSyncTree addEventRegistration:eventRegistration
  547. forQuery:query];
  548. } else {
  549. events = [self.serverSyncTree addEventRegistration:eventRegistration
  550. forQuery:query];
  551. }
  552. [self.eventRaiser raiseEvents:events];
  553. }
  554. - (void)removeEventRegistration:(id<FEventRegistration>)eventRegistration
  555. forQuery:(FQuerySpec *)query {
  556. // These are guaranteed not to raise events, since we're not passing in a
  557. // cancelError. However we can future-proof a little bit by handling the
  558. // return values anyways.
  559. FFLog(@"I-RDB038011", @"Removing event registration with hande: %lu",
  560. (unsigned long)eventRegistration.handle);
  561. NSArray *events = nil;
  562. if ([[query.path getFront] isEqualToString:kDotInfoPrefix]) {
  563. events = [self.infoSyncTree removeEventRegistration:eventRegistration
  564. forQuery:query
  565. cancelError:nil];
  566. } else {
  567. events = [self.serverSyncTree removeEventRegistration:eventRegistration
  568. forQuery:query
  569. cancelError:nil];
  570. }
  571. [self.eventRaiser raiseEvents:events];
  572. }
  573. - (void)keepQuery:(FQuerySpec *)query synced:(BOOL)synced {
  574. NSAssert(![[query.path getFront] isEqualToString:kDotInfoPrefix],
  575. @"Can't keep .info tree synced!");
  576. [self.serverSyncTree keepQuery:query synced:synced];
  577. }
  578. - (void)updateInfo:(NSString *)pathString withValue:(id)value {
  579. // hack to make serverTimeOffset available in a threadsafe way. Property is
  580. // marked as atomic
  581. if ([pathString isEqualToString:kDotInfoServerTimeOffset]) {
  582. NSTimeInterval offset = [(NSNumber *)value doubleValue] / 1000.0;
  583. self.serverClock =
  584. [[FOffsetClock alloc] initWithClock:[FSystemClock clock]
  585. offset:offset];
  586. }
  587. FPath *path = [[FPath alloc]
  588. initWith:[NSString
  589. stringWithFormat:@"%@/%@", kDotInfoPrefix, pathString]];
  590. id<FNode> newNode = [FSnapshotUtilities nodeFrom:value];
  591. [self.infoData updateSnapshot:path withNewSnapshot:newNode];
  592. NSArray *events = [self.infoSyncTree applyServerOverwriteAtPath:path
  593. newData:newNode];
  594. [self.eventRaiser raiseEvents:events];
  595. }
  596. - (void)callOnComplete:(fbt_void_nserror_ref)onComplete
  597. withStatus:(NSString *)status
  598. errorReason:(NSString *)errorReason
  599. andPath:(FPath *)path {
  600. if (onComplete) {
  601. FIRDatabaseReference *ref =
  602. [[FIRDatabaseReference alloc] initWithRepo:self path:path];
  603. BOOL statusOk = [status isEqualToString:kFWPResponseForActionStatusOk];
  604. NSError *err = nil;
  605. if (!statusOk) {
  606. err = [FUtilities errorForStatus:status andReason:errorReason];
  607. }
  608. [self.eventRaiser raiseCallback:^{
  609. onComplete(err, ref);
  610. }];
  611. }
  612. }
  613. - (void)ackWrite:(NSInteger)writeId
  614. rerunTransactionsAtPath:(FPath *)path
  615. status:(NSString *)status {
  616. if ([status isEqualToString:kFErrorWriteCanceled]) {
  617. // This write was already removed, we just need to ignore it...
  618. } else {
  619. BOOL success = [status isEqualToString:kFWPResponseForActionStatusOk];
  620. NSArray *clearEvents =
  621. [self.serverSyncTree ackUserWriteWithWriteId:writeId
  622. revert:!success
  623. persist:YES
  624. clock:self.serverClock];
  625. if ([clearEvents count] > 0) {
  626. [self rerunTransactionsForPath:path];
  627. }
  628. [self.eventRaiser raiseEvents:clearEvents];
  629. }
  630. }
  631. - (void)warnIfWriteFailedAtPath:(FPath *)path
  632. status:(NSString *)status
  633. message:(NSString *)message {
  634. if (!([status isEqualToString:kFWPResponseForActionStatusOk] ||
  635. [status isEqualToString:kFErrorWriteCanceled])) {
  636. FFWarn(@"I-RDB038012", @"%@ at %@ failed: %@", message, path, status);
  637. }
  638. }
  639. #pragma mark -
  640. #pragma mark FPersistentConnectionDelegate methods
  641. - (void)onDataUpdate:(FPersistentConnection *)fpconnection
  642. forPath:(NSString *)pathString
  643. message:(id)data
  644. isMerge:(BOOL)isMerge
  645. tagId:(NSNumber *)tagId {
  646. FFLog(@"I-RDB038013", @"onDataUpdateForPath: %@ withMessage: %@",
  647. pathString, data);
  648. // For testing.
  649. self.dataUpdateCount++;
  650. FPath *path = [[FPath alloc] initWith:pathString];
  651. data = self.interceptServerDataCallback
  652. ? self.interceptServerDataCallback(pathString, data)
  653. : data;
  654. NSArray *events = nil;
  655. if (tagId != nil) {
  656. if (isMerge) {
  657. NSDictionary *message = data;
  658. FCompoundWrite *taggedChildren =
  659. [FCompoundWrite compoundWriteWithValueDictionary:message];
  660. events =
  661. [self.serverSyncTree applyTaggedQueryMergeAtPath:path
  662. changedChildren:taggedChildren
  663. tagId:tagId];
  664. } else {
  665. id<FNode> taggedSnap = [FSnapshotUtilities nodeFrom:data];
  666. events =
  667. [self.serverSyncTree applyTaggedQueryOverwriteAtPath:path
  668. newData:taggedSnap
  669. tagId:tagId];
  670. }
  671. } else if (isMerge) {
  672. NSDictionary *message = data;
  673. FCompoundWrite *changedChildren =
  674. [FCompoundWrite compoundWriteWithValueDictionary:message];
  675. events = [self.serverSyncTree applyServerMergeAtPath:path
  676. changedChildren:changedChildren];
  677. } else {
  678. id<FNode> snap = [FSnapshotUtilities nodeFrom:data];
  679. events = [self.serverSyncTree applyServerOverwriteAtPath:path
  680. newData:snap];
  681. }
  682. if ([events count] > 0) {
  683. // Since we have a listener outstanding for each transaction, receiving
  684. // any events is a proxy for some change having occurred.
  685. [self rerunTransactionsForPath:path];
  686. }
  687. [self.eventRaiser raiseEvents:events];
  688. }
  689. - (void)onRangeMerge:(NSArray *)ranges
  690. forPath:(NSString *)pathString
  691. tagId:(NSNumber *)tag {
  692. FFLog(@"I-RDB038014", @"onRangeMerge: %@ => %@", pathString, ranges);
  693. // For testing
  694. self.rangeMergeUpdateCount++;
  695. FPath *path = [[FPath alloc] initWith:pathString];
  696. NSArray *events;
  697. if (tag != nil) {
  698. events = [self.serverSyncTree applyTaggedServerRangeMergeAtPath:path
  699. updates:ranges
  700. tagId:tag];
  701. } else {
  702. events = [self.serverSyncTree applyServerRangeMergeAtPath:path
  703. updates:ranges];
  704. }
  705. if (events.count > 0) {
  706. // Since we have a listener outstanding for each transaction, receiving
  707. // any events is a proxy for some change having occurred.
  708. [self rerunTransactionsForPath:path];
  709. }
  710. [self.eventRaiser raiseEvents:events];
  711. }
  712. - (void)onConnect:(FPersistentConnection *)fpconnection {
  713. [self updateInfo:kDotInfoConnected withValue:@YES];
  714. }
  715. - (void)onDisconnect:(FPersistentConnection *)fpconnection {
  716. [self updateInfo:kDotInfoConnected withValue:@NO];
  717. [self runOnDisconnectEvents];
  718. }
  719. - (void)onServerInfoUpdate:(FPersistentConnection *)fpconnection
  720. updates:(NSDictionary *)updates {
  721. for (NSString *key in updates) {
  722. id val = [updates objectForKey:key];
  723. [self updateInfo:key withValue:val];
  724. }
  725. }
  726. - (void)setupNotifications {
  727. NSString *const *backgroundConstant = (NSString *const *)dlsym(
  728. RTLD_DEFAULT, "UIApplicationDidEnterBackgroundNotification");
  729. if (backgroundConstant) {
  730. FFLog(@"I-RDB038015", @"Registering for background notification.");
  731. [[NSNotificationCenter defaultCenter]
  732. addObserver:self
  733. selector:@selector(didEnterBackground)
  734. name:*backgroundConstant
  735. object:nil];
  736. } else {
  737. FFLog(@"I-RDB038016",
  738. @"Skipped registering for background notification.");
  739. }
  740. }
  741. - (void)didEnterBackground {
  742. if (!self.config.persistenceEnabled)
  743. return;
  744. // Targetted compilation is ONLY for testing. UIKit is weak-linked in actual
  745. // release build.
  746. #if TARGET_OS_IOS || TARGET_OS_TV
  747. // The idea is to wait until any outstanding sets get written to disk. Since
  748. // the sets might still be in our dispatch queue, we wait for the dispatch
  749. // queue to catch up and for persistence to catch up. This may be
  750. // undesirable though. The dispatch queue might just be processing a bunch
  751. // of incoming data or something. We might want to keep track of whether
  752. // there are any unpersisted sets or something.
  753. FFLog(@"I-RDB038017",
  754. @"Entering background. Starting background task to finish work.");
  755. Class uiApplicationClass = NSClassFromString(@"UIApplication");
  756. assert(uiApplicationClass); // If we are here, we should be on iOS and
  757. // UIApplication should be available.
  758. UIApplication *application = [uiApplicationClass sharedApplication];
  759. __block UIBackgroundTaskIdentifier bgTask =
  760. [application beginBackgroundTaskWithExpirationHandler:^{
  761. [application endBackgroundTask:bgTask];
  762. }];
  763. NSDate *start = [NSDate date];
  764. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  765. NSTimeInterval finishTime = [start timeIntervalSinceNow] * -1;
  766. FFLog(@"I-RDB038018", @"Background task completed. Queue time: %f",
  767. finishTime);
  768. [application endBackgroundTask:bgTask];
  769. });
  770. #endif
  771. }
  772. #pragma mark -
  773. #pragma mark Internal methods
  774. /**
  775. * Applies all the changes stored up in the onDisconnect tree
  776. */
  777. - (void)runOnDisconnectEvents {
  778. FFLog(@"I-RDB038019", @"Running onDisconnectEvents");
  779. NSDictionary *serverValues =
  780. [FServerValues generateServerValues:self.serverClock];
  781. NSMutableArray *events = [[NSMutableArray alloc] init];
  782. [self.onDisconnect
  783. forEachTreeAtPath:[FPath empty]
  784. do:^(FPath *path, id<FNode> node) {
  785. id<FNode> existing = [self.serverSyncTree
  786. calcCompleteEventCacheAtPath:path
  787. excludeWriteIds:@[]];
  788. id<FNode> resolved = [FServerValues
  789. resolveDeferredValueSnapshot:node
  790. withExisting:existing
  791. serverValues:serverValues];
  792. [events addObjectsFromArray:
  793. [self.serverSyncTree
  794. applyServerOverwriteAtPath:path
  795. newData:resolved]];
  796. FPath *affectedPath =
  797. [self abortTransactionsAtPath:path
  798. error:kFTransactionSet];
  799. [self rerunTransactionsForPath:affectedPath];
  800. }];
  801. self.onDisconnect = [[FSparseSnapshotTree alloc] init];
  802. [self.eventRaiser raiseEvents:events];
  803. }
  804. - (NSDictionary *)dumpListens {
  805. return [self.connection dumpListens];
  806. }
  807. #pragma mark -
  808. #pragma mark Transactions
  809. /**
  810. * Setup the transaction data structures
  811. */
  812. - (void)initTransactions {
  813. self.transactionQueueTree = [[FTree alloc] init];
  814. self.hijackHash = NO;
  815. self.loggedTransactionPersistenceWarning = NO;
  816. }
  817. /**
  818. * Creates a new transaction, add its to the transactions we're tracking, and
  819. * sends it to the server if possible
  820. */
  821. - (void)startTransactionOnPath:(FPath *)path
  822. update:(fbt_transactionresult_mutabledata)update
  823. onComplete:(fbt_void_nserror_bool_datasnapshot)onComplete
  824. withLocalEvents:(BOOL)applyLocally {
  825. if (self.config.persistenceEnabled &&
  826. !self.loggedTransactionPersistenceWarning) {
  827. self.loggedTransactionPersistenceWarning = YES;
  828. FFInfo(@"I-RDB038020",
  829. @"runTransactionBlock: usage detected while persistence is "
  830. @"enabled. Please be aware that transactions "
  831. @"*will not* be persisted across app restarts. "
  832. @"See "
  833. @"https://www.firebase.com/docs/ios/guide/"
  834. @"offline-capabilities.html#section-handling-transactions-"
  835. @"offline for more details.");
  836. }
  837. FIRDatabaseReference *watchRef =
  838. [[FIRDatabaseReference alloc] initWithRepo:self path:path];
  839. // make sure we're listening on this node
  840. // Note: we can't do this asynchronously. To preserve event ordering, it has
  841. // to be done in this block. This is ok, this block is guaranteed to be our
  842. // own event loop
  843. NSUInteger handle = [[FUtilities LUIDGenerator] integerValue];
  844. fbt_void_datasnapshot cb = ^(FIRDataSnapshot *snapshot) {
  845. };
  846. FValueEventRegistration *registration =
  847. [[FValueEventRegistration alloc] initWithRepo:self
  848. handle:handle
  849. callback:cb
  850. cancelCallback:nil];
  851. [watchRef.repo addEventRegistration:registration
  852. forQuery:watchRef.querySpec];
  853. fbt_void_void unwatcher = ^{
  854. [watchRef removeObserverWithHandle:handle];
  855. };
  856. // Save all the data that represents this transaction
  857. FTupleTransaction *transaction = [[FTupleTransaction alloc] init];
  858. transaction.path = path;
  859. transaction.update = update;
  860. transaction.onComplete = onComplete;
  861. transaction.status = FTransactionInitializing;
  862. transaction.order = [FUtilities LUIDGenerator];
  863. transaction.applyLocally = applyLocally;
  864. transaction.retryCount = 0;
  865. transaction.unwatcher = unwatcher;
  866. transaction.currentWriteId = nil;
  867. transaction.currentInputSnapshot = nil;
  868. transaction.currentOutputSnapshotRaw = nil;
  869. transaction.currentOutputSnapshotResolved = nil;
  870. // Run transaction initially
  871. id<FNode> currentState = [self latestStateAtPath:path excludeWriteIds:nil];
  872. transaction.currentInputSnapshot = currentState;
  873. FIRMutableData *mutableCurrent =
  874. [[FIRMutableData alloc] initWithNode:currentState];
  875. FIRTransactionResult *result = transaction.update(mutableCurrent);
  876. if (!result.isSuccess) {
  877. // Abort the transaction
  878. transaction.unwatcher();
  879. transaction.currentOutputSnapshotRaw = nil;
  880. transaction.currentOutputSnapshotResolved = nil;
  881. if (transaction.onComplete) {
  882. FIRDatabaseReference *ref =
  883. [[FIRDatabaseReference alloc] initWithRepo:self
  884. path:transaction.path];
  885. FIndexedNode *indexedNode = [FIndexedNode
  886. indexedNodeWithNode:transaction.currentInputSnapshot];
  887. FIRDataSnapshot *snap =
  888. [[FIRDataSnapshot alloc] initWithRef:ref
  889. indexedNode:indexedNode];
  890. [self.eventRaiser raiseCallback:^{
  891. transaction.onComplete(nil, NO, snap);
  892. }];
  893. }
  894. } else {
  895. // Note: different from js. We don't need to validate, FIRMutableData
  896. // does validation. We also don't have to worry about priorities. Just
  897. // mark as run and add to queue.
  898. transaction.status = FTransactionRun;
  899. FTree *queueNode = [self.transactionQueueTree subTree:transaction.path];
  900. NSMutableArray *nodeQueue = [queueNode getValue];
  901. if (nodeQueue == nil) {
  902. nodeQueue = [[NSMutableArray alloc] init];
  903. }
  904. [nodeQueue addObject:transaction];
  905. [queueNode setValue:nodeQueue];
  906. // Update visibleData and raise events
  907. // Note: We intentionally raise events after updating all of our
  908. // transaction state, since the user could start new transactions from
  909. // the event callbacks
  910. NSDictionary *serverValues =
  911. [FServerValues generateServerValues:self.serverClock];
  912. id<FNode> newValUnresolved = [result.update nodeValue];
  913. id<FNode> newVal =
  914. [FServerValues resolveDeferredValueSnapshot:newValUnresolved
  915. withExisting:currentState
  916. serverValues:serverValues];
  917. transaction.currentOutputSnapshotRaw = newValUnresolved;
  918. transaction.currentOutputSnapshotResolved = newVal;
  919. transaction.currentWriteId =
  920. [NSNumber numberWithInteger:[self nextWriteId]];
  921. NSArray *events = [self.serverSyncTree
  922. applyUserOverwriteAtPath:path
  923. newData:newVal
  924. writeId:[transaction.currentWriteId integerValue]
  925. isVisible:transaction.applyLocally];
  926. [self.eventRaiser raiseEvents:events];
  927. [self sendAllReadyTransactions];
  928. }
  929. }
  930. /**
  931. * @param writeIdsToExclude A specific set to exclude
  932. */
  933. - (id<FNode>)latestStateAtPath:(FPath *)path
  934. excludeWriteIds:(NSArray *)writeIdsToExclude {
  935. id<FNode> latestState =
  936. [self.serverSyncTree calcCompleteEventCacheAtPath:path
  937. excludeWriteIds:writeIdsToExclude];
  938. return latestState ? latestState : [FEmptyNode emptyNode];
  939. }
  940. /**
  941. * Sends any already-run transactions that aren't waiting for outstanding
  942. * transactions to complete.
  943. *
  944. * Externally, call the version with no arguments.
  945. * Internally, calls itself recursively with a particular transactionQueueTree
  946. * node to recurse through the tree
  947. */
  948. - (void)sendAllReadyTransactions {
  949. FTree *node = self.transactionQueueTree;
  950. [self pruneCompletedTransactionsBelowNode:node];
  951. [self sendReadyTransactionsForTree:node];
  952. }
  953. - (void)sendReadyTransactionsForTree:(FTree *)node {
  954. NSMutableArray *queue = [node getValue];
  955. if (queue != nil) {
  956. queue = [self buildTransactionQueueAtNode:node];
  957. NSAssert([queue count] > 0, @"Sending zero length transaction queue");
  958. NSUInteger notRunIndex = [queue
  959. indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
  960. return ((FTupleTransaction *)obj).status != FTransactionRun;
  961. }];
  962. // If they're all run (and not sent), we can send them. Else, we must
  963. // wait.
  964. if (notRunIndex == NSNotFound) {
  965. [self sendTransactionQueue:queue atPath:node.path];
  966. }
  967. } else if ([node hasChildren]) {
  968. [node forEachChild:^(FTree *child) {
  969. [self sendReadyTransactionsForTree:child];
  970. }];
  971. }
  972. }
  973. /**
  974. * Given a list of run transactions, send them to the server and then handle the
  975. * result (success or failure).
  976. */
  977. - (void)sendTransactionQueue:(NSMutableArray *)queue atPath:(FPath *)path {
  978. // Mark transactions as sent and bump the retry count
  979. NSMutableArray *writeIdsToExclude = [[NSMutableArray alloc] init];
  980. for (FTupleTransaction *transaction in queue) {
  981. [writeIdsToExclude addObject:transaction.currentWriteId];
  982. }
  983. id<FNode> latestState = [self latestStateAtPath:path
  984. excludeWriteIds:writeIdsToExclude];
  985. id<FNode> snapToSend = latestState;
  986. NSString *latestHash = [latestState dataHash];
  987. for (FTupleTransaction *transaction in queue) {
  988. NSAssert(
  989. transaction.status == FTransactionRun,
  990. @"[FRepo sendTransactionQueue:] items in queue should all be run.");
  991. FFLog(@"I-RDB038021", @"Transaction at %@ set to SENT",
  992. transaction.path);
  993. transaction.status = FTransactionSent;
  994. transaction.retryCount++;
  995. FPath *relativePath = [FPath relativePathFrom:path to:transaction.path];
  996. // If we've gotten to this point, the output snapshot must be defined.
  997. snapToSend =
  998. [snapToSend updateChild:relativePath
  999. withNewChild:transaction.currentOutputSnapshotRaw];
  1000. }
  1001. id dataToSend = [snapToSend valForExport:YES];
  1002. NSString *pathToSend = [path description];
  1003. latestHash = self.hijackHash ? @"badhash" : latestHash;
  1004. // Send the put
  1005. [self.connection
  1006. putData:dataToSend
  1007. forPath:pathToSend
  1008. withHash:latestHash
  1009. withCallback:^(NSString *status, NSString *errorReason) {
  1010. FFLog(@"I-RDB038022", @"Transaction put response: %@ : %@",
  1011. pathToSend, status);
  1012. NSMutableArray *events = [[NSMutableArray alloc] init];
  1013. if ([status isEqualToString:kFWPResponseForActionStatusOk]) {
  1014. // Queue up the callbacks and fire them after cleaning up all of
  1015. // our transaction state, since the callback could trigger more
  1016. // transactions or sets.
  1017. NSMutableArray *callbacks = [[NSMutableArray alloc] init];
  1018. for (FTupleTransaction *transaction in queue) {
  1019. transaction.status = FTransactionCompleted;
  1020. [events addObjectsFromArray:
  1021. [self.serverSyncTree
  1022. ackUserWriteWithWriteId:
  1023. [transaction.currentWriteId integerValue]
  1024. revert:NO
  1025. persist:NO
  1026. clock:self.serverClock]];
  1027. if (transaction.onComplete) {
  1028. // We never unset the output snapshot, and given that this
  1029. // transaction is complete, it should be set
  1030. id<FNode> node =
  1031. transaction.currentOutputSnapshotResolved;
  1032. FIndexedNode *indexedNode =
  1033. [FIndexedNode indexedNodeWithNode:node];
  1034. FIRDatabaseReference *ref = [[FIRDatabaseReference alloc]
  1035. initWithRepo:self
  1036. path:transaction.path];
  1037. FIRDataSnapshot *snapshot =
  1038. [[FIRDataSnapshot alloc] initWithRef:ref
  1039. indexedNode:indexedNode];
  1040. fbt_void_void cb = ^{
  1041. transaction.onComplete(nil, YES, snapshot);
  1042. };
  1043. [callbacks addObject:[cb copy]];
  1044. }
  1045. transaction.unwatcher();
  1046. }
  1047. // Now remove the completed transactions.
  1048. [self
  1049. pruneCompletedTransactionsBelowNode:[self.transactionQueueTree
  1050. subTree:path]];
  1051. // There may be pending transactions that we can now send.
  1052. [self sendAllReadyTransactions];
  1053. // Finally, trigger onComplete callbacks
  1054. [self.eventRaiser raiseCallbacks:callbacks];
  1055. } else {
  1056. // transactions are no longer sent. Update their status
  1057. // appropriately.
  1058. if ([status
  1059. isEqualToString:kFWPResponseForActionStatusDataStale]) {
  1060. for (FTupleTransaction *transaction in queue) {
  1061. if (transaction.status == FTransactionSentNeedsAbort) {
  1062. transaction.status = FTransactionNeedsAbort;
  1063. } else {
  1064. transaction.status = FTransactionRun;
  1065. }
  1066. }
  1067. } else {
  1068. FFWarn(@"I-RDB038023",
  1069. @"runTransactionBlock: at %@ failed: %@", path,
  1070. status);
  1071. for (FTupleTransaction *transaction in queue) {
  1072. transaction.status = FTransactionNeedsAbort;
  1073. [transaction setAbortStatus:status reason:errorReason];
  1074. }
  1075. }
  1076. }
  1077. [self rerunTransactionsForPath:path];
  1078. [self.eventRaiser raiseEvents:events];
  1079. }];
  1080. }
  1081. /**
  1082. * Finds all transactions dependent on the data at changed Path and reruns them.
  1083. *
  1084. * Should be called any time cached data changes.
  1085. *
  1086. * Return the highest path that was affected by rerunning transactions. This is
  1087. * the path at which events need to be raised for.
  1088. */
  1089. - (FPath *)rerunTransactionsForPath:(FPath *)changedPath {
  1090. // For the common case that there are no transactions going on, skip all
  1091. // this!
  1092. if ([self.transactionQueueTree isEmpty]) {
  1093. return changedPath;
  1094. } else {
  1095. FTree *rootMostTransactionNode =
  1096. [self getAncestorTransactionNodeForPath:changedPath];
  1097. FPath *path = rootMostTransactionNode.path;
  1098. NSArray *queue =
  1099. [self buildTransactionQueueAtNode:rootMostTransactionNode];
  1100. [self rerunTransactionQueue:queue atPath:path];
  1101. return path;
  1102. }
  1103. }
  1104. /**
  1105. * Does all the work of rerunning transactions (as well as cleans up aborted
  1106. * transactions and whatnot).
  1107. */
  1108. - (void)rerunTransactionQueue:(NSArray *)queue atPath:(FPath *)path {
  1109. if (queue.count == 0) {
  1110. return; // nothing to do
  1111. }
  1112. // Queue up the callbacks and fire them after cleaning up all of our
  1113. // transaction state, since the callback could trigger more transactions or
  1114. // sets.
  1115. NSMutableArray *events = [[NSMutableArray alloc] init];
  1116. NSMutableArray *callbacks = [[NSMutableArray alloc] init];
  1117. // Ignore, by default, all of the sets in this queue, since we're re-running
  1118. // all of them. However, we want to include the results of new sets
  1119. // triggered as part of this re-run, so we don't want to ignore a range,
  1120. // just these specific sets.
  1121. NSMutableArray *writeIdsToExclude = [[NSMutableArray alloc] init];
  1122. for (FTupleTransaction *transaction in queue) {
  1123. [writeIdsToExclude addObject:transaction.currentWriteId];
  1124. }
  1125. for (FTupleTransaction *transaction in queue) {
  1126. FPath *relativePath __unused =
  1127. [FPath relativePathFrom:path to:transaction.path];
  1128. BOOL abortTransaction = NO;
  1129. NSAssert(relativePath != nil, @"[FRepo rerunTransactionsQueue:] "
  1130. @"relativePath should not be null.");
  1131. if (transaction.status == FTransactionNeedsAbort) {
  1132. abortTransaction = YES;
  1133. if (![transaction.abortStatus
  1134. isEqualToString:kFErrorWriteCanceled]) {
  1135. NSArray *ackEvents = [self.serverSyncTree
  1136. ackUserWriteWithWriteId:[transaction.currentWriteId
  1137. integerValue]
  1138. revert:YES
  1139. persist:NO
  1140. clock:self.serverClock];
  1141. [events addObjectsFromArray:ackEvents];
  1142. }
  1143. } else if (transaction.status == FTransactionRun) {
  1144. if (transaction.retryCount >= kFTransactionMaxRetries) {
  1145. abortTransaction = YES;
  1146. [transaction setAbortStatus:kFTransactionTooManyRetries
  1147. reason:nil];
  1148. [events
  1149. addObjectsFromArray:
  1150. [self.serverSyncTree
  1151. ackUserWriteWithWriteId:[transaction.currentWriteId
  1152. integerValue]
  1153. revert:YES
  1154. persist:NO
  1155. clock:self.serverClock]];
  1156. } else {
  1157. // This code reruns a transaction
  1158. id<FNode> currentNode =
  1159. [self latestStateAtPath:transaction.path
  1160. excludeWriteIds:writeIdsToExclude];
  1161. transaction.currentInputSnapshot = currentNode;
  1162. FIRMutableData *mutableCurrent =
  1163. [[FIRMutableData alloc] initWithNode:currentNode];
  1164. FIRTransactionResult *result =
  1165. transaction.update(mutableCurrent);
  1166. if (result.isSuccess) {
  1167. NSNumber *oldWriteId = transaction.currentWriteId;
  1168. NSDictionary *serverValues =
  1169. [FServerValues generateServerValues:self.serverClock];
  1170. id<FNode> newVal = [result.update nodeValue];
  1171. id<FNode> newValResolved = [FServerValues
  1172. resolveDeferredValueSnapshot:newVal
  1173. withExisting:transaction
  1174. .currentInputSnapshot
  1175. serverValues:serverValues];
  1176. transaction.currentOutputSnapshotRaw = newVal;
  1177. transaction.currentOutputSnapshotResolved = newValResolved;
  1178. transaction.currentWriteId =
  1179. [NSNumber numberWithInteger:[self nextWriteId]];
  1180. // Mutates writeIdsToExclude in place
  1181. [writeIdsToExclude removeObject:oldWriteId];
  1182. [events
  1183. addObjectsFromArray:
  1184. [self.serverSyncTree
  1185. applyUserOverwriteAtPath:transaction.path
  1186. newData:
  1187. transaction
  1188. .currentOutputSnapshotResolved
  1189. writeId:
  1190. [transaction.currentWriteId
  1191. integerValue]
  1192. isVisible:transaction
  1193. .applyLocally]];
  1194. [events addObjectsFromArray:
  1195. [self.serverSyncTree
  1196. ackUserWriteWithWriteId:[oldWriteId
  1197. integerValue]
  1198. revert:YES
  1199. persist:NO
  1200. clock:self.serverClock]];
  1201. } else {
  1202. abortTransaction = YES;
  1203. // The user aborted the transaction. JS treats ths as a
  1204. // "nodata" abort, but it's not an error, so we don't send
  1205. // them an error.
  1206. [transaction setAbortStatus:nil reason:nil];
  1207. [events
  1208. addObjectsFromArray:
  1209. [self.serverSyncTree
  1210. ackUserWriteWithWriteId:
  1211. [transaction.currentWriteId integerValue]
  1212. revert:YES
  1213. persist:NO
  1214. clock:self.serverClock]];
  1215. }
  1216. }
  1217. }
  1218. [self.eventRaiser raiseEvents:events];
  1219. events = nil;
  1220. if (abortTransaction) {
  1221. // Abort
  1222. transaction.status = FTransactionCompleted;
  1223. transaction.unwatcher();
  1224. if (transaction.onComplete) {
  1225. FIRDatabaseReference *ref = [[FIRDatabaseReference alloc]
  1226. initWithRepo:self
  1227. path:transaction.path];
  1228. FIndexedNode *lastInput = [FIndexedNode
  1229. indexedNodeWithNode:transaction.currentInputSnapshot];
  1230. FIRDataSnapshot *snap =
  1231. [[FIRDataSnapshot alloc] initWithRef:ref
  1232. indexedNode:lastInput];
  1233. fbt_void_void cb = ^{
  1234. // Unlike JS, no need to check for "nodata" because ObjC has
  1235. // abortError = nil
  1236. transaction.onComplete(transaction.abortError, NO, snap);
  1237. };
  1238. [callbacks addObject:[cb copy]];
  1239. }
  1240. }
  1241. }
  1242. // Note: unlike current js client, we don't need to preserve priority. Users
  1243. // can set priority via FIRMutableData
  1244. // Clean up completed transactions.
  1245. [self pruneCompletedTransactionsBelowNode:self.transactionQueueTree];
  1246. // Now fire callbacks, now that we're in a good, known state.
  1247. [self.eventRaiser raiseCallbacks:callbacks];
  1248. // Try to send the transaction result to the server
  1249. [self sendAllReadyTransactions];
  1250. }
  1251. - (FTree *)getAncestorTransactionNodeForPath:(FPath *)path {
  1252. FTree *transactionNode = self.transactionQueueTree;
  1253. while (![path isEmpty] && [transactionNode getValue] == nil) {
  1254. NSString *front = [path getFront];
  1255. transactionNode =
  1256. [transactionNode subTree:[[FPath alloc] initWith:front]];
  1257. path = [path popFront];
  1258. }
  1259. return transactionNode;
  1260. }
  1261. - (NSMutableArray *)buildTransactionQueueAtNode:(FTree *)node {
  1262. NSMutableArray *queue = [[NSMutableArray alloc] init];
  1263. [self aggregateTransactionQueuesForNode:node andQueue:queue];
  1264. [queue sortUsingComparator:^NSComparisonResult(FTupleTransaction *obj1,
  1265. FTupleTransaction *obj2) {
  1266. return [obj1.order compare:obj2.order];
  1267. }];
  1268. return queue;
  1269. }
  1270. - (void)aggregateTransactionQueuesForNode:(FTree *)node
  1271. andQueue:(NSMutableArray *)queue {
  1272. NSArray *nodeQueue = [node getValue];
  1273. [queue addObjectsFromArray:nodeQueue];
  1274. [node forEachChild:^(FTree *child) {
  1275. [self aggregateTransactionQueuesForNode:child andQueue:queue];
  1276. }];
  1277. }
  1278. /**
  1279. * Remove COMPLETED transactions at or below this node in the
  1280. * transactionQueueTree
  1281. */
  1282. - (void)pruneCompletedTransactionsBelowNode:(FTree *)node {
  1283. NSMutableArray *queue = [node getValue];
  1284. if (queue != nil) {
  1285. int i = 0;
  1286. // remove all of the completed transactions from the queue
  1287. while (i < queue.count) {
  1288. FTupleTransaction *transaction = [queue objectAtIndex:i];
  1289. if (transaction.status == FTransactionCompleted) {
  1290. [queue removeObjectAtIndex:i];
  1291. } else {
  1292. i++;
  1293. }
  1294. }
  1295. if (queue.count > 0) {
  1296. [node setValue:queue];
  1297. } else {
  1298. [node setValue:nil];
  1299. }
  1300. }
  1301. [node forEachChildMutationSafe:^(FTree *child) {
  1302. [self pruneCompletedTransactionsBelowNode:child];
  1303. }];
  1304. }
  1305. /**
  1306. * Aborts all transactions on ancestors or descendants of the specified path.
  1307. * Called when doing a setValue: or updateChildValues: since we consider them
  1308. * incompatible with transactions
  1309. *
  1310. * @param path path for which we want to abort related transactions.
  1311. */
  1312. - (FPath *)abortTransactionsAtPath:(FPath *)path error:(NSString *)error {
  1313. // For the common case that there are no transactions going on, skip all
  1314. // this!
  1315. if ([self.transactionQueueTree isEmpty]) {
  1316. return path;
  1317. } else {
  1318. FPath *affectedPath =
  1319. [self getAncestorTransactionNodeForPath:path].path;
  1320. FTree *transactionNode = [self.transactionQueueTree subTree:path];
  1321. [transactionNode forEachAncestor:^BOOL(FTree *ancestor) {
  1322. [self abortTransactionsAtNode:ancestor error:error];
  1323. return NO;
  1324. }];
  1325. [self abortTransactionsAtNode:transactionNode error:error];
  1326. [transactionNode forEachDescendant:^(FTree *child) {
  1327. [self abortTransactionsAtNode:child error:error];
  1328. }];
  1329. return affectedPath;
  1330. }
  1331. }
  1332. /**
  1333. * Abort transactions stored in this transactions queue node.
  1334. *
  1335. * @param node Node to abort transactions for.
  1336. */
  1337. - (void)abortTransactionsAtNode:(FTree *)node error:(NSString *)error {
  1338. NSMutableArray *queue = [node getValue];
  1339. if (queue != nil) {
  1340. // Queue up the callbacks and fire them after cleaning up all of our
  1341. // transaction state, since can be immediately aborted and removed.
  1342. NSMutableArray *callbacks = [[NSMutableArray alloc] init];
  1343. // Go through queue. Any already-sent transactions must be marked for
  1344. // abort, while the unsent ones can be immediately aborted and removed
  1345. NSMutableArray *events = [[NSMutableArray alloc] init];
  1346. int lastSent = -1;
  1347. // Note: all of the sent transactions will be at the front of the queue,
  1348. // so safe to increment lastSent
  1349. for (FTupleTransaction *transaction in queue) {
  1350. if (transaction.status == FTransactionSentNeedsAbort) {
  1351. // No-op. already marked.
  1352. } else if (transaction.status == FTransactionSent) {
  1353. // Mark this transaction for abort when it returns
  1354. lastSent++;
  1355. transaction.status = FTransactionSentNeedsAbort;
  1356. [transaction setAbortStatus:error reason:nil];
  1357. } else {
  1358. // we can abort this immediately
  1359. transaction.unwatcher();
  1360. if ([error isEqualToString:kFTransactionSet]) {
  1361. [events
  1362. addObjectsFromArray:
  1363. [self.serverSyncTree
  1364. ackUserWriteWithWriteId:
  1365. [transaction.currentWriteId integerValue]
  1366. revert:YES
  1367. persist:NO
  1368. clock:self.serverClock]];
  1369. } else {
  1370. // If it was cancelled it was already removed from the sync
  1371. // tree, no need to ack
  1372. NSAssert([error isEqualToString:kFErrorWriteCanceled], nil);
  1373. }
  1374. if (transaction.onComplete) {
  1375. NSError *abortReason = [FUtilities errorForStatus:error
  1376. andReason:nil];
  1377. FIRDataSnapshot *snapshot = nil;
  1378. fbt_void_void cb = ^{
  1379. transaction.onComplete(abortReason, NO, snapshot);
  1380. };
  1381. [callbacks addObject:[cb copy]];
  1382. }
  1383. }
  1384. }
  1385. if (lastSent == -1) {
  1386. // We're not waiting for any sent transactions. We can clear the
  1387. // queue.
  1388. [node setValue:nil];
  1389. } else {
  1390. // Remove the transactions we aborted
  1391. NSRange theRange;
  1392. theRange.location = lastSent + 1;
  1393. theRange.length = queue.count - theRange.location;
  1394. [queue removeObjectsInRange:theRange];
  1395. }
  1396. // Now fire the callbacks
  1397. [self.eventRaiser raiseEvents:events];
  1398. [self.eventRaiser raiseCallbacks:callbacks];
  1399. }
  1400. }
  1401. @end