/Pods/YapDatabase/YapDatabase/Extensions/Relationships/YapDatabaseRelationshipTransaction.h

https://gitlab.com/Mr.Tomato/MeituanDemo · C Header · 437 lines · 67 code · 31 blank · 339 comment · 0 complexity · e6f0c2e32fc23e824a34ec18ea894ea4 MD5 · raw file

  1. #import <Foundation/Foundation.h>
  2. #import "YapDatabaseExtensionTransaction.h"
  3. #import "YapDatabaseRelationshipEdge.h"
  4. #import "YapDatabaseRelationshipNode.h"
  5. /**
  6. * Welcome to YapDatabase!
  7. *
  8. * The project page has a wealth of documentation if you have any questions.
  9. * https://github.com/yapstudios/YapDatabase
  10. *
  11. * If you're new to the project you may want to visit the wiki.
  12. * https://github.com/yapstudios/YapDatabase/wiki
  13. *
  14. * The YapDatabaseRelationship extension allow you to create relationships between objects,
  15. * and configure automatic deletion rules.
  16. *
  17. * For tons of information about this extension, see the wiki article:
  18. * https://github.com/yapstudios/YapDatabase/wiki/Relationships
  19. **/
  20. @interface YapDatabaseRelationshipTransaction : YapDatabaseExtensionTransaction
  21. #pragma mark Node Fetch
  22. /**
  23. * Shortcut for fetching the source object for the given edge.
  24. * Equivalent to:
  25. *
  26. * [transaction objectForKey:edge.sourceKey inCollection:edge.sourceCollection];
  27. **/
  28. - (id)sourceNodeForEdge:(YapDatabaseRelationshipEdge *)edge;
  29. /**
  30. * Shortcut for fetching the destination object for the given edge.
  31. * Equivalent to:
  32. *
  33. * [transaction objectForKey:edge.destinationKey inCollection:edge.destinationCollection];
  34. **/
  35. - (id)destinationNodeForEdge:(YapDatabaseRelationshipEdge *)edge;
  36. #pragma mark Enumerate
  37. /**
  38. * Enumerates every edge in the graph with the given name.
  39. *
  40. * @param name
  41. * The name of the edge (case sensitive).
  42. **/
  43. - (void)enumerateEdgesWithName:(NSString *)name
  44. usingBlock:(void (^)(YapDatabaseRelationshipEdge *edge, BOOL *stop))block;
  45. /**
  46. * Enumerates every edge that matches any parameters you specify.
  47. * You can specify any combination of the following:
  48. *
  49. * - name only
  50. * - sourceKey & sourceCollection only
  51. * - name + sourceKey & sourceCollection
  52. *
  53. * @param name (optional)
  54. * The name of the edge (case sensitive).
  55. *
  56. * @param sourceKey (optional)
  57. * The edge.sourceKey to match.
  58. *
  59. * @param sourceCollection (optional)
  60. * The edge.sourceCollection to match.
  61. *
  62. * If you pass a non-nil sourceKey, and sourceCollection is nil,
  63. * then the sourceCollection is treated as the empty string, just like the rest of the YapDatabase framework.
  64. **/
  65. - (void)enumerateEdgesWithName:(NSString *)name
  66. sourceKey:(NSString *)sourceKey
  67. collection:(NSString *)sourceCollection
  68. usingBlock:(void (^)(YapDatabaseRelationshipEdge *edge, BOOL *stop))block;
  69. /**
  70. * Enumerates every edge that matches any parameters you specify.
  71. * You can specify any combination of the following:
  72. *
  73. * - name only
  74. * - destinationKey & destinationCollection only
  75. * - name + destinationKey & destinationCollection
  76. *
  77. * @param name (optional)
  78. * The name of the edge (case sensitive).
  79. *
  80. * @param destinationKey (optional)
  81. * The edge.destinationKey to match.
  82. *
  83. * @param destinationCollection (optional)
  84. * The edge.destinationCollection to match.
  85. *
  86. * If you pass a non-nil destinationKey, and destinationCollection is nil,
  87. * then the destinationCollection is treated as the empty string, just like the rest of the YapDatabase framework.
  88. **/
  89. - (void)enumerateEdgesWithName:(NSString *)name
  90. destinationKey:(NSString *)destinationKey
  91. collection:(NSString *)destinationCollection
  92. usingBlock:(void (^)(YapDatabaseRelationshipEdge *edge, BOOL *stop))block;
  93. /**
  94. * Enumerates every edge that matches any parameters you specify.
  95. * You can specify any combination of the following:
  96. *
  97. * - name only
  98. * - destinationFilePath
  99. * - name + destinationFilePath
  100. *
  101. * @param name (optional)
  102. * The name of the edge (case sensitive).
  103. *
  104. * @param destinationFilePath (optional)
  105. * The edge.destinationFilePath to match.
  106. **/
  107. - (void)enumerateEdgesWithName:(NSString *)name
  108. destinationFilePath:(NSString *)destinationFilePath
  109. usingBlock:(void (^)(YapDatabaseRelationshipEdge *edge, BOOL *stop))block;
  110. /**
  111. * Enumerates every edge that matches any parameters you specify.
  112. * You can specify any combination of the following:
  113. *
  114. * - name only
  115. * - sourceKey & sourceCollection only
  116. * - destinationKey & destinationCollection only
  117. * - name + sourceKey & sourceCollection
  118. * - name + destinationKey & destinationCollection
  119. * - name + sourceKey & sourceCollection + destinationKey & destinationCollection
  120. *
  121. * @param name (optional)
  122. * The name of the edge (case sensitive).
  123. *
  124. * @param sourceKey (optional)
  125. * The edge.sourceKey to match.
  126. *
  127. * @param sourceCollection (optional)
  128. * The edge.sourceCollection to match.
  129. *
  130. * @param destinationKey (optional)
  131. * The edge.destinationKey to match.
  132. *
  133. * @param destinationCollection (optional)
  134. * The edge.destinationCollection to match.
  135. *
  136. * If you pass a non-nil sourceKey, and sourceCollection is nil,
  137. * then the sourceCollection is treated as the empty string, just like the rest of the YapDatabase framework.
  138. *
  139. * If you pass a non-nil destinationKey, and destinationCollection is nil,
  140. * then the destinationCollection is treated as the empty string, just like the rest of the YapDatabase framework.
  141. **/
  142. - (void)enumerateEdgesWithName:(NSString *)name
  143. sourceKey:(NSString *)sourceKey
  144. collection:(NSString *)sourceCollection
  145. destinationKey:(NSString *)destinationKey
  146. collection:(NSString *)destinationCollection
  147. usingBlock:(void (^)(YapDatabaseRelationshipEdge *edge, BOOL *stop))block;
  148. /**
  149. * Enumerates every edge that matches any parameters you specify.
  150. * You can specify any combination of the following:
  151. *
  152. * - name only
  153. * - sourceKey & sourceCollection only
  154. * - destinationKey & destinationCollection only
  155. * - name + sourceKey & sourceCollection
  156. * - name + destinationKey & destinationCollection
  157. * - name + sourceKey & sourceCollection + destinationKey & destinationCollection
  158. *
  159. * @param name (optional)
  160. * The name of the edge (case sensitive).
  161. *
  162. * @param sourceKey (optional)
  163. * The edge.sourceKey to match.
  164. *
  165. * @param sourceCollection (optional)
  166. * The edge.sourceCollection to match.
  167. *
  168. * @param destinationFilePath (optional)
  169. * The edge.destinationFilePath to match.
  170. *
  171. * If you pass a non-nil sourceKey, and sourceCollection is nil,
  172. * then the sourceCollection is treated as the empty string, just like the rest of the YapDatabase framework.
  173. **/
  174. - (void)enumerateEdgesWithName:(NSString *)name
  175. sourceKey:(NSString *)sourceKey
  176. collection:(NSString *)sourceCollection
  177. destinationFilePath:(NSString *)destinationFilePath
  178. usingBlock:(void (^)(YapDatabaseRelationshipEdge *edge, BOOL *stop))block;
  179. #pragma mark Count
  180. /**
  181. * Returns a count of every edge in the graph with the given name.
  182. *
  183. * @param name
  184. * The name of the edge (case sensitive).
  185. **/
  186. - (NSUInteger)edgeCountWithName:(NSString *)name;
  187. /**
  188. * Returns a count of every edge that matches any parameters you specify.
  189. * You can specify any combination of the following:
  190. *
  191. * - name only
  192. * - sourceKey & sourceCollection only
  193. * - name + sourceKey & sourceCollection
  194. *
  195. * @param name (optional)
  196. * The name of the edge (case sensitive).
  197. *
  198. * @param sourceKey (optional)
  199. * The edge.sourceKey to match.
  200. *
  201. * @param sourceCollection (optional)
  202. * The edge.sourceCollection to match.
  203. *
  204. * If you pass a non-nil sourceKey, and sourceCollection is nil,
  205. * then the sourceCollection is treated as the empty string, just like the rest of the YapDatabase framework.
  206. **/
  207. - (NSUInteger)edgeCountWithName:(NSString *)name
  208. sourceKey:(NSString *)sourceKey
  209. collection:(NSString *)sourceCollection;
  210. /**
  211. * Returns a count of every edge that matches any parameters you specify.
  212. * You can specify any combination of the following:
  213. *
  214. * - name only
  215. * - destinationKey & destinationCollection only
  216. * - name + destinationKey & destinationCollection
  217. *
  218. * @param name (optional)
  219. * The name of the edge (case sensitive).
  220. *
  221. * @param destinationKey (optional)
  222. * The edge.destinationKey to match.
  223. *
  224. * @param destinationCollection (optional)
  225. * The edge.destinationCollection to match.
  226. *
  227. * If you pass a non-nil destinationKey, and destinationCollection is nil,
  228. * then the destinationCollection is treated as the empty string, just like the rest of the YapDatabase framework.
  229. **/
  230. - (NSUInteger)edgeCountWithName:(NSString *)name
  231. destinationKey:(NSString *)destinationKey
  232. collection:(NSString *)destinationCollection;
  233. /**
  234. * Returns a count of every edge that matches any parameters you specify.
  235. * You can specify any combination of the following:
  236. *
  237. * - name only
  238. * - destinationFilePath
  239. * - name + destinationFilePath
  240. *
  241. * @param name (optional)
  242. * The name of the edge (case sensitive).
  243. *
  244. * @param destinationFilePath (optional)
  245. * The edge.destinationFilePath to match.
  246. **/
  247. - (NSUInteger)edgeCountWithName:(NSString *)name
  248. destinationFilePath:(NSString *)destinationFilePath;
  249. /**
  250. * Returns a count of every edge that matches any parameters you specify.
  251. * You can specify any combination of the following:
  252. *
  253. * - name only
  254. * - sourceKey & sourceCollection only
  255. * - destinationKey & destinationCollection only
  256. * - name + sourceKey & sourceCollection
  257. * - name + destinationKey & destinationCollection
  258. * - name + sourceKey & sourceCollection + destinationKey & destinationCollection
  259. *
  260. * @param name (optional)
  261. * The name of the edge (case sensitive).
  262. *
  263. * @param sourceKey (optional)
  264. * The edge.sourceKey to match.
  265. *
  266. * @param sourceCollection (optional)
  267. * The edge.sourceCollection to match.
  268. *
  269. * @param destinationKey (optional)
  270. * The edge.destinationKey to match.
  271. *
  272. * @param destinationCollection (optional)
  273. * The edge.destinationCollection to match.
  274. *
  275. * If you pass a non-nil sourceKey, and sourceCollection is nil,
  276. * then the sourceCollection is treated as the empty string, just like the rest of the YapDatabase framework.
  277. *
  278. * If you pass a non-nil destinationKey, and destinationCollection is nil,
  279. * then the destinationCollection is treated as the empty string, just like the rest of the YapDatabase framework.
  280. **/
  281. - (NSUInteger)edgeCountWithName:(NSString *)name
  282. sourceKey:(NSString *)sourceKey
  283. collection:(NSString *)sourceCollection
  284. destinationKey:(NSString *)destinationKey
  285. collection:(NSString *)destinationCollection;
  286. /**
  287. * Returns a count of every edge that matches any parameters you specify.
  288. * You can specify any combination of the following:
  289. *
  290. * - name only
  291. * - sourceKey & sourceCollection only
  292. * - destinationFilePath
  293. * - name + sourceKey & sourceCollection
  294. * - name + destinationFilePath
  295. * - name + sourceKey & sourceCollection + destinationFilePath
  296. *
  297. * @param name (optional)
  298. * The name of the edge (case sensitive).
  299. *
  300. * @param sourceKey (optional)
  301. * The edge.sourceKey to match.
  302. *
  303. * @param sourceCollection (optional)
  304. * The edge.sourceCollection to match.
  305. *
  306. * @param destinationFilePath (optional)
  307. * The edge.destinationFilePath to match.
  308. *
  309. * If you pass a non-nil sourceKey, and sourceCollection is nil,
  310. * then the sourceCollection is treated as the empty string, just like the rest of the YapDatabase framework.
  311. **/
  312. - (NSUInteger)edgeCountWithName:(NSString *)name
  313. sourceKey:(NSString *)sourceKey
  314. collection:(NSString *)sourceCollection
  315. destinationFilePath:(NSString *)destinationFilePath;
  316. @end
  317. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  318. #pragma mark -
  319. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  320. @interface YapDatabaseRelationshipTransaction (ReadWrite)
  321. #pragma mark Manual Edge Management
  322. /**
  323. * There are 2 ways to manage edges (add/remove) using the YapDatabaseRelationship extension:
  324. *
  325. * - Manual edge management (via the methods below)
  326. * - Implement the YapDatabaseRelationshipNode protocol for some of your objects
  327. *
  328. * For more information, see the wiki section "Edge Creation":
  329. *
  330. * https://github.com/yapstudios/YapDatabase/wiki/Relationships#wiki-edge_creation
  331. **/
  332. /**
  333. * This method will add the manual edge (if it doesn't already exist).
  334. * Otherwise it will replace the the existing manual edge with the same name & srcKey/collection & dstKey/collection.
  335. **/
  336. - (void)addEdge:(YapDatabaseRelationshipEdge *)edge;
  337. /**
  338. * This method will remove the given manual edge (if it exists).
  339. *
  340. * The following properties are compared, for the purpose of checking edges to see if they match:
  341. * - name
  342. * - sourceKey & sourceCollection
  343. * - destinationKey & destinationCollection
  344. * - isManualEdge
  345. *
  346. * In other words, to manually remove an existing manual edge, you simply need to pass an edge instance which
  347. * has the same name & source & destination.
  348. *
  349. * When you manually remove an edge, you can decide how the relationship extension should process it.
  350. * That is, you can tell the relationship edge to act as if the source or destination node was deleted:
  351. *
  352. * - YDB_EdgeDeleted : Do nothing. Simply remove the edge from the database.
  353. * - YDB_SourceNodeDeleted : Act as if the source node was deleted.
  354. * - YDB_DestinationNodeDeleted : Act as if the destination node was deleted.
  355. *
  356. * This allows you to tell the relationship extension whether or not to process the nodeDeleteRules of the edge.
  357. * And, if so, in what manner.
  358. *
  359. * In other words, you can remove an edge, and tell the relationship extension
  360. * to pretend the source node was deleted (for example), even if you didn't actually delete the source node.
  361. * This allows you to execute the nodeDeleteRules that exist on an edge, without actually deleting the node.
  362. *
  363. * Please note that manual edges and protocol edges are in different domains.
  364. * A manual edge is one you create and add to the system via the addEdge: method.
  365. * A protocol edge is one created via the YapDatabaseRelationshipNode protocol.
  366. * So you cannot, for example, create an edge via the YapDatabaseRelationshipNode protocol,
  367. * and then manually delete it via the removeEdge:: method. This is what is meant by "different domains".
  368. **/
  369. - (void)removeEdgeWithName:(NSString *)edgeName
  370. sourceKey:(NSString *)sourceKey
  371. collection:(NSString *)sourceCollection
  372. destinationKey:(NSString *)destinationKey
  373. collection:(NSString *)destinationCollection
  374. withProcessing:(YDB_NotifyReason)reason;
  375. /**
  376. * This method is the same as removeEdgeWithName::::::, but allows you to pass an existing edge instance.
  377. *
  378. * The following properties of the given edge are inspected, for the purpose of checking edges to see if they match:
  379. * - name
  380. * - sourceKey & sourceCollection
  381. * - destinationKey & destinationCollection
  382. *
  383. * The given edge's nodeDeleteRules are ignored.
  384. * The nodeDeleteRules of the pre-existing edge are processed according to the given reason.
  385. *
  386. * @see removeEdgeWithName:sourceKey:collection:destinationKey:collection:withProcessing:
  387. **/
  388. - (void)removeEdge:(YapDatabaseRelationshipEdge *)edge withProcessing:(YDB_NotifyReason)reason;
  389. #pragma mark Force Processing
  390. /**
  391. * The extension automatically processes all changes to the graph at the end of a readwrite transaction.
  392. * This allows it to consolidate multiple changes into a single batch,
  393. * and also minimizes the impact of cascading delete rules, especially in the case where you'll be deleting
  394. * many of the objects manually at some later point within the transaction block.
  395. *
  396. * However, there may be certain use cases where it is preferable to have the extension execute its rules in advance.
  397. * I'm struggling to come up with a really good example, so this semi-convoluted one will have to do:
  398. *
  399. * You have a parent object, with a bunch of child objects that have edges to the parent.
  400. * You need to replace the parent, and for whatever reason the new parent has the same collection/key.
  401. * So instead of doing a setObject:forKey:inCollection:, you first delete the original parent.
  402. * At that point you can invoke this flush method, and it will properly delete any child objects.
  403. * Then you can safely set the new parent, knowing it won't accidentally inherit any children from the old parent.
  404. **/
  405. - (void)flush;
  406. @end