FRepo.m 64 KB

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