/Pods/YapDatabase/YapDatabase/Extensions/Hooks/YapDatabaseHooksTransaction.m

https://gitlab.com/Mr.Tomato/MeituanDemo · Objective C · 442 lines · 199 code · 46 blank · 197 comment · 15 complexity · dec67a60107171df83233623b9104b25 MD5 · raw file

  1. #import "YapDatabaseHooksTransaction.h"
  2. #import "YapDatabaseHooksPrivate.h"
  3. @implementation YapDatabaseHooksTransaction
  4. - (id)initWithParentConnection:(YapDatabaseHooksConnection *)inParentConnection
  5. databaseTransaction:(YapDatabaseReadTransaction *)inDatabaseTransaction
  6. {
  7. if ((self = [super init]))
  8. {
  9. parentConnection = inParentConnection;
  10. databaseTransaction = inDatabaseTransaction;
  11. }
  12. return self;
  13. }
  14. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  15. #pragma mark Creation
  16. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  17. /**
  18. * YapDatabaseExtensionTransaction subclasses MUST implement this method.
  19. **/
  20. - (BOOL)createIfNeeded
  21. {
  22. // Nothing to do here
  23. return YES;
  24. }
  25. /**
  26. * YapDatabaseExtensionTransaction subclasses MUST implement this method.
  27. **/
  28. - (BOOL)prepareIfNeeded
  29. {
  30. // Nothing to do here
  31. return YES;
  32. }
  33. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  34. #pragma mark Commit & Rollback
  35. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  36. /**
  37. * YapDatabaseExtensionTransaction subclasses MUST implement this method.
  38. **/
  39. - (void)didCommitTransaction
  40. {
  41. parentConnection = nil;
  42. databaseTransaction = nil;
  43. }
  44. /**
  45. * YapDatabaseExtensionTransaction subclasses MUST implement this method.
  46. **/
  47. - (void)didRollbackTransaction
  48. {
  49. parentConnection = nil;
  50. databaseTransaction = nil;
  51. }
  52. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  53. #pragma mark Generic Accessors
  54. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  55. /**
  56. * YapDatabaseExtensionTransaction subclasses MUST implement these methods.
  57. * They are needed by various utility methods.
  58. **/
  59. - (YapDatabaseReadTransaction *)databaseTransaction
  60. {
  61. return databaseTransaction;
  62. }
  63. /**
  64. * YapDatabaseExtensionTransaction subclasses MUST implement these methods.
  65. * They are needed by various utility methods.
  66. **/
  67. - (YapDatabaseExtensionConnection *)extensionConnection
  68. {
  69. return parentConnection;
  70. }
  71. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  72. #pragma mark Hooks
  73. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  74. /**
  75. * Subclasses MUST implement this method.
  76. * YapDatabaseReadWriteTransaction Hook, invoked post-op.
  77. *
  78. * Corresponds to the following method(s) in YapDatabaseReadWriteTransaction:
  79. * - setObject:forKey:inCollection:
  80. * - setObject:forKey:inCollection:withMetadata:
  81. * - setObject:forKey:inCollection:withMetadata:serializedObject:serializedMetadata:
  82. *
  83. * The row is being inserted, meaning there is not currently an entry for the collection/key tuple.
  84. **/
  85. - (void)handleInsertObject:(id)object
  86. forCollectionKey:(YapCollectionKey *)ck
  87. withMetadata:(id)metadata
  88. rowid:(int64_t)rowid
  89. {
  90. if (parentConnection->parent->didInsertObject)
  91. {
  92. __unsafe_unretained YapDatabaseReadWriteTransaction *transaction =
  93. (YapDatabaseReadWriteTransaction *)databaseTransaction;
  94. parentConnection->parent->didInsertObject(transaction, ck.collection, ck.key, object, metadata);
  95. }
  96. }
  97. /**
  98. * Subclasses MUST implement this method.
  99. * YapDatabaseReadWriteTransaction Hook, invoked post-op.
  100. *
  101. * Corresponds to the following method(s) in YapDatabaseReadWriteTransaction:
  102. * - setObject:forKey:inCollection:
  103. * - setObject:forKey:inCollection:withMetadata:
  104. * - setObject:forKey:inCollection:withMetadata:serializedObject:serializedMetadata:
  105. *
  106. * The row is being modified, meaning there is already an entry for the collection/key tuple which is being modified.
  107. **/
  108. - (void)handleUpdateObject:(id)object
  109. forCollectionKey:(YapCollectionKey *)ck
  110. withMetadata:(id)metadata
  111. rowid:(int64_t)rowid
  112. {
  113. if (parentConnection->parent->didUpdateObject)
  114. {
  115. __unsafe_unretained YapDatabaseReadWriteTransaction *transaction =
  116. (YapDatabaseReadWriteTransaction *)databaseTransaction;
  117. parentConnection->parent->didUpdateObject(transaction, ck.collection, ck.key, object, metadata);
  118. }
  119. }
  120. /**
  121. * Subclasses MUST implement this method.
  122. * YapDatabaseReadWriteTransaction Hook, invoked post-op.
  123. *
  124. * Corresponds to the following method(s) in YapDatabaseReadWriteTransaction:
  125. * - replaceObject:forKey:inCollection:
  126. * - replaceObject:forKey:inCollection:withSerializedObject:
  127. *
  128. * There is already a row for the collection/key tuple, and only the object is being modified (metadata untouched).
  129. **/
  130. - (void)handleReplaceObject:(id)object
  131. forCollectionKey:(YapCollectionKey *)ck
  132. withRowid:(int64_t)rowid
  133. {
  134. if (parentConnection->parent->didReplaceObject)
  135. {
  136. __unsafe_unretained YapDatabaseReadWriteTransaction *transaction =
  137. (YapDatabaseReadWriteTransaction *)databaseTransaction;
  138. parentConnection->parent->didReplaceObject(transaction, ck.collection, ck.key, object);
  139. }
  140. }
  141. /**
  142. * Subclasses MUST implement this method.
  143. * YapDatabaseReadWriteTransaction Hook, invoked post-op.
  144. *
  145. * Corresponds to the following method(s) in YapDatabaseReadWriteTransaction:
  146. * - replaceMetadata:forKey:inCollection:
  147. * - replaceMetadata:forKey:inCollection:withSerializedMetadata:
  148. *
  149. * There is already a row for the collection/key tuple, and only the metadata is being modified (object untouched).
  150. **/
  151. - (void)handleReplaceMetadata:(id)metadata
  152. forCollectionKey:(YapCollectionKey *)ck
  153. withRowid:(int64_t)rowid
  154. {
  155. if (parentConnection->parent->didReplaceMetadata)
  156. {
  157. __unsafe_unretained YapDatabaseReadWriteTransaction *transaction =
  158. (YapDatabaseReadWriteTransaction *)databaseTransaction;
  159. parentConnection->parent->didReplaceMetadata(transaction, ck.collection, ck.key, metadata);
  160. }
  161. }
  162. /**
  163. * Subclasses MUST implement this method.
  164. * YapDatabaseReadWriteTransaction Hook, invoked post-op.
  165. *
  166. * Corresponds to the following method(s) in YapDatabaseReadWriteTransaction:
  167. * - touchObjectForKey:inCollection:collection:
  168. **/
  169. - (void)handleTouchObjectForCollectionKey:(YapCollectionKey *)ck withRowid:(int64_t)rowid
  170. {
  171. // Nothing to do here
  172. }
  173. /**
  174. * Subclasses MUST implement this method.
  175. * YapDatabaseReadWriteTransaction Hook, invoked post-op.
  176. *
  177. * Corresponds to the following method(s) in YapDatabaseReadWriteTransaction:
  178. * - touchMetadataForKey:inCollection:
  179. **/
  180. - (void)handleTouchMetadataForCollectionKey:(YapCollectionKey *)ck withRowid:(int64_t)rowid
  181. {
  182. // Nothing to do here
  183. }
  184. /**
  185. * Subclasses MUST implement this method.
  186. * YapDatabaseReadWriteTransaction Hook, invoked post-op.
  187. *
  188. * Corresponds to the following method(s) in YapDatabaseReadWriteTransaction:
  189. * - touchRowForKey:inCollection:
  190. **/
  191. - (void)handleTouchRowForCollectionKey:(YapCollectionKey *)collectionKey withRowid:(int64_t)rowid
  192. {
  193. // Nothing to do here
  194. }
  195. /**
  196. * Subclasses MUST implement this method.
  197. * YapDatabaseReadWriteTransaction Hook, invoked post-op.
  198. *
  199. * Corresponds to the following method(s) in YapDatabaseReadWriteTransaction
  200. * - removeObjectForKey:inCollection:
  201. **/
  202. - (void)handleRemoveObjectForCollectionKey:(YapCollectionKey *)ck withRowid:(int64_t)rowid
  203. {
  204. if (parentConnection->parent->didRemoveObject)
  205. {
  206. __unsafe_unretained YapDatabaseReadWriteTransaction *transaction =
  207. (YapDatabaseReadWriteTransaction *)databaseTransaction;
  208. parentConnection->parent->didRemoveObject(transaction, ck.collection, ck.key);
  209. }
  210. }
  211. /**
  212. * Subclasses MUST implement this method.
  213. * YapDatabaseReadWriteTransaction Hook, invoked post-op.
  214. *
  215. * Corresponds to the following method(s) in YapDatabaseReadWriteTransaction:
  216. * - removeObjectsForKeys:inCollection:
  217. * - removeAllObjectsInCollection:
  218. *
  219. * IMPORTANT:
  220. * The number of items passed to this method has the following guarantee:
  221. * count <= (SQLITE_LIMIT_VARIABLE_NUMBER - 1)
  222. *
  223. * The YapDatabaseReadWriteTransaction will inspect the list of keys that are to be removed,
  224. * and then loop over them in "chunks" which are readily processable for extensions.
  225. **/
  226. - (void)handleRemoveObjectsForKeys:(NSArray *)keys inCollection:(NSString *)collection withRowids:(NSArray *)rowids
  227. {
  228. if (parentConnection->parent->didRemoveObjects)
  229. {
  230. __unsafe_unretained YapDatabaseReadWriteTransaction *transaction =
  231. (YapDatabaseReadWriteTransaction *)databaseTransaction;
  232. parentConnection->parent->didRemoveObjects(transaction, collection, keys);
  233. }
  234. }
  235. /**
  236. * Subclasses MUST implement this method.
  237. * YapDatabaseReadWriteTransaction Hook, invoked post-op.
  238. *
  239. * Corresponds to [transaction removeAllObjectsInAllCollections].
  240. **/
  241. - (void)handleRemoveAllObjectsInAllCollections
  242. {
  243. if (parentConnection->parent->didRemoveAllObjectsInAllCollections)
  244. {
  245. __unsafe_unretained YapDatabaseReadWriteTransaction *transaction =
  246. (YapDatabaseReadWriteTransaction *)databaseTransaction;
  247. parentConnection->parent->didRemoveAllObjectsInAllCollections(transaction);
  248. }
  249. }
  250. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  251. #pragma mark Pre-Hooks
  252. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  253. /**
  254. * Subclasses may OPTIONALLY implement this method.
  255. * YapDatabaseReadWriteTransaction Hook, invoked pre-op.
  256. *
  257. * Corresponds to the following method(s) in YapDatabaseReadWriteTransaction:
  258. * - setObject:forKey:inCollection:
  259. * - setObject:forKey:inCollection:withMetadata:
  260. * - setObject:forKey:inCollection:withMetadata:serializedObject:serializedMetadata:
  261. *
  262. * The row is being inserted, meaning there is not currently an entry for the collection/key tuple.
  263. **/
  264. - (void)handleWillInsertObject:(id)object
  265. forCollectionKey:(YapCollectionKey *)ck
  266. withMetadata:(id)metadata
  267. {
  268. if (parentConnection->parent->willInsertObject)
  269. {
  270. __unsafe_unretained YapDatabaseReadWriteTransaction *transaction =
  271. (YapDatabaseReadWriteTransaction *)databaseTransaction;
  272. parentConnection->parent->willInsertObject(transaction, ck.collection, ck.key, object, metadata);
  273. }
  274. }
  275. /**
  276. * Subclasses may OPTIONALLY implement this method.
  277. * YapDatabaseReadWriteTransaction Hook, invoked pre-op.
  278. *
  279. * Corresponds to the following method(s) in YapDatabaseReadWriteTransaction:
  280. * - setObject:forKey:inCollection:
  281. * - setObject:forKey:inCollection:withMetadata:
  282. * - setObject:forKey:inCollection:withMetadata:serializedObject:serializedMetadata:
  283. *
  284. * The row is being modified, meaning there is already an entry for the collection/key tuple which is being modified.
  285. **/
  286. - (void)handleWillUpdateObject:(id)object
  287. forCollectionKey:(YapCollectionKey *)ck
  288. withMetadata:(id)metadata
  289. rowid:(int64_t)rowid
  290. {
  291. if (parentConnection->parent->willUpdateObject)
  292. {
  293. __unsafe_unretained YapDatabaseReadWriteTransaction *transaction =
  294. (YapDatabaseReadWriteTransaction *)databaseTransaction;
  295. parentConnection->parent->willUpdateObject(transaction, ck.collection, ck.key, object, metadata);
  296. }
  297. }
  298. /**
  299. * Subclasses may OPTIONALLY implement this method.
  300. * YapDatabaseReadWriteTransaction Hook, invoked pre-op.
  301. *
  302. * Corresponds to the following method(s) in YapDatabaseReadWriteTransaction:
  303. * - replaceObject:forKey:inCollection:
  304. * - replaceObject:forKey:inCollection:withSerializedObject:
  305. *
  306. * There is already a row for the collection/key tuple, and only the object is being modified (metadata untouched).
  307. **/
  308. - (void)handleWillReplaceObject:(id)object
  309. forCollectionKey:(YapCollectionKey *)ck
  310. withRowid:(int64_t)rowid
  311. {
  312. if (parentConnection->parent->willReplaceObject)
  313. {
  314. __unsafe_unretained YapDatabaseReadWriteTransaction *transaction =
  315. (YapDatabaseReadWriteTransaction *)databaseTransaction;
  316. parentConnection->parent->willReplaceObject(transaction, ck.collection, ck.key, object);
  317. }
  318. }
  319. /**
  320. * Subclasses may OPTIONALLY implement this method.
  321. * YapDatabaseReadWriteTransaction Hook, invoked pre-op.
  322. *
  323. * Corresponds to the following method(s) in YapDatabaseReadWriteTransaction:
  324. * - replaceMetadata:forKey:inCollection:
  325. * - replaceMetadata:forKey:inCollection:withSerializedMetadata:
  326. *
  327. * There is already a row for the collection/key tuple, and only the metadata is being modified (object untouched).
  328. **/
  329. - (void)handleWillReplaceMetadata:(id)metadata
  330. forCollectionKey:(YapCollectionKey *)ck
  331. withRowid:(int64_t)rowid
  332. {
  333. if (parentConnection->parent->willReplaceMetadata)
  334. {
  335. __unsafe_unretained YapDatabaseReadWriteTransaction *transaction =
  336. (YapDatabaseReadWriteTransaction *)databaseTransaction;
  337. parentConnection->parent->willReplaceMetadata(transaction, ck.collection, ck.key, metadata);
  338. }
  339. }
  340. /**
  341. * Subclasses may OPTIONALLY implement this method.
  342. * YapDatabaseReadWriteTransaction Hook, invoked pre-op.
  343. *
  344. * Corresponds to the following method(s) in YapDatabaseReadWriteTransaction:
  345. * - removeObjectForKey:inCollection:
  346. **/
  347. - (void)handleWillRemoveObjectForCollectionKey:(YapCollectionKey *)ck withRowid:(int64_t)rowid
  348. {
  349. if (parentConnection->parent->willRemoveObject)
  350. {
  351. __unsafe_unretained YapDatabaseReadWriteTransaction *transaction =
  352. (YapDatabaseReadWriteTransaction *)databaseTransaction;
  353. parentConnection->parent->willRemoveObject(transaction, ck.collection, ck.key);
  354. }
  355. }
  356. /**
  357. * Subclasses may OPTIONALLY implement this method.
  358. * YapDatabaseReadWriteTransaction Hook, invoked pre-op.
  359. *
  360. * Corresponds to the following method(s) in YapDatabaseReadWriteTransaction:
  361. * - removeObjectsForKeys:inCollection:
  362. * - removeAllObjectsInCollection:
  363. *
  364. * IMPORTANT:
  365. * The number of items passed to this method has the following guarantee:
  366. * count <= (SQLITE_LIMIT_VARIABLE_NUMBER - 1)
  367. *
  368. * The YapDatabaseReadWriteTransaction will inspect the list of keys that are to be removed,
  369. * and then loop over them in "chunks" which are readily processable for extensions.
  370. **/
  371. - (void)handleWillRemoveObjectsForKeys:(NSArray *)keys inCollection:(NSString *)collection withRowids:(NSArray *)rowids
  372. {
  373. if (parentConnection->parent->willRemoveObjects)
  374. {
  375. __unsafe_unretained YapDatabaseReadWriteTransaction *transaction =
  376. (YapDatabaseReadWriteTransaction *)databaseTransaction;
  377. parentConnection->parent->willRemoveObjects(transaction, collection, keys);
  378. }
  379. }
  380. /**
  381. * Subclasses may OPTIONALLY implement this method.
  382. * YapDatabaseReadWriteTransaction Hook, invoked pre-op.
  383. *
  384. * Corresponds to the following method(s) in YapDatabaseReadWriteTransaction:
  385. * - removeAllObjectsInAllCollections
  386. **/
  387. - (void)handleWillRemoveAllObjectsInAllCollections
  388. {
  389. if (parentConnection->parent->willRemoveAllObjectsInAllCollections)
  390. {
  391. __unsafe_unretained YapDatabaseReadWriteTransaction *transaction =
  392. (YapDatabaseReadWriteTransaction *)databaseTransaction;
  393. parentConnection->parent->willRemoveAllObjectsInAllCollections(transaction);
  394. }
  395. }
  396. @end