realm.h 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801
  1. /*
  2. FIXME: License, since this header may be distributed independently from
  3. other headers.
  4. */
  5. #ifndef REALM_H
  6. #define REALM_H
  7. #include <stddef.h>
  8. #include <stdint.h>
  9. #include <stdbool.h>
  10. #if defined(_WIN32) || defined(__CYGWIN__)
  11. #if defined(Realm_EXPORTS)
  12. // Exporting Win32 symbols
  13. #define RLM_EXPORT __declspec(dllexport)
  14. #else
  15. // Importing Win32 symbols. Note: Clients linking statically should define
  16. // RLM_NO_DLLIMPORT.
  17. #if !defined(RLM_NO_DLLIMPORT)
  18. #define RLM_EXPORT __declspec(dllimport)
  19. #else
  20. #define RLM_EXPORT
  21. #endif // RLM_NO_DLLIMPORT
  22. #endif // Realm_EXPORTS
  23. #else
  24. // Not Win32
  25. #define RLM_EXPORT __attribute__((visibility("default")))
  26. #endif
  27. #ifdef __cplusplus
  28. #define RLM_API extern "C" RLM_EXPORT
  29. #else
  30. #define RLM_API RLM_EXPORT
  31. #endif // __cplusplus
  32. typedef struct shared_realm realm_t;
  33. typedef struct realm_schema realm_schema_t;
  34. typedef struct realm_scheduler realm_scheduler_t;
  35. typedef struct realm_thread_safe_reference realm_thread_safe_reference_t;
  36. typedef void (*realm_free_userdata_func_t)(void*);
  37. typedef void* (*realm_clone_userdata_func_t)(const void*);
  38. /* Accessor types */
  39. typedef struct realm_object realm_object_t;
  40. typedef struct realm_list realm_list_t;
  41. typedef struct realm_set realm_set_t;
  42. typedef struct realm_dictionary realm_dictionary_t;
  43. /* Query types */
  44. typedef struct realm_query realm_query_t;
  45. typedef struct realm_results realm_results_t;
  46. /* Config types */
  47. typedef struct realm_config realm_config_t;
  48. typedef struct realm_sync_config realm_sync_config_t;
  49. typedef bool (*realm_migration_func_t)(void* userdata, realm_t* old_realm, realm_t* new_realm,
  50. const realm_schema_t* schema);
  51. typedef bool (*realm_data_initialization_func_t)(void* userdata, realm_t* realm);
  52. typedef bool (*realm_should_compact_on_launch_func_t)(void* userdata, uint64_t total_bytes, uint64_t used_bytes);
  53. typedef enum realm_schema_mode {
  54. RLM_SCHEMA_MODE_AUTOMATIC,
  55. RLM_SCHEMA_MODE_IMMUTABLE,
  56. RLM_SCHEMA_MODE_READ_ONLY_ALTERNATIVE,
  57. RLM_SCHEMA_MODE_RESET_FILE,
  58. RLM_SCHEMA_MODE_ADDITIVE_DISCOVERED,
  59. RLM_SCHEMA_MODE_ADDITIVE_EXPLICIT,
  60. RLM_SCHEMA_MODE_MANUAL,
  61. } realm_schema_mode_e;
  62. /* Key types */
  63. typedef uint32_t realm_class_key_t;
  64. typedef int64_t realm_property_key_t;
  65. typedef int64_t realm_object_key_t;
  66. typedef uint64_t realm_version_t;
  67. static const realm_class_key_t RLM_INVALID_CLASS_KEY = ((uint32_t)-1) >> 1;
  68. static const realm_property_key_t RLM_INVALID_PROPERTY_KEY = -1;
  69. static const realm_object_key_t RLM_INVALID_OBJECT_KEY = -1;
  70. /* Value types */
  71. typedef enum realm_value_type {
  72. RLM_TYPE_NULL,
  73. RLM_TYPE_INT,
  74. RLM_TYPE_BOOL,
  75. RLM_TYPE_STRING,
  76. RLM_TYPE_BINARY,
  77. RLM_TYPE_TIMESTAMP,
  78. RLM_TYPE_FLOAT,
  79. RLM_TYPE_DOUBLE,
  80. RLM_TYPE_DECIMAL128,
  81. RLM_TYPE_OBJECT_ID,
  82. RLM_TYPE_LINK,
  83. RLM_TYPE_UUID,
  84. } realm_value_type_e;
  85. typedef enum realm_schema_validation_mode {
  86. RLM_SCHEMA_VALIDATION_BASIC = 0,
  87. RLM_SCHEMA_VALIDATION_SYNC = 1,
  88. RLM_SCHEMA_VALIDATION_REJECT_EMBEDDED_ORPHANS = 2
  89. } realm_schema_validation_mode_e;
  90. typedef struct realm_string {
  91. const char* data;
  92. size_t size;
  93. } realm_string_t;
  94. typedef struct realm_binary {
  95. const uint8_t* data;
  96. size_t size;
  97. } realm_binary_t;
  98. typedef struct realm_timestamp {
  99. int64_t seconds;
  100. int32_t nanoseconds;
  101. } realm_timestamp_t;
  102. typedef struct realm_decimal128 {
  103. uint64_t w[2];
  104. } realm_decimal128_t;
  105. typedef struct realm_link {
  106. realm_class_key_t target_table;
  107. realm_object_key_t target;
  108. } realm_link_t;
  109. typedef struct realm_object_id {
  110. uint8_t bytes[12];
  111. } realm_object_id_t;
  112. typedef struct realm_uuid {
  113. uint8_t bytes[16];
  114. } realm_uuid_t;
  115. typedef struct realm_value {
  116. union {
  117. int64_t integer;
  118. bool boolean;
  119. realm_string_t string;
  120. realm_binary_t binary;
  121. realm_timestamp_t timestamp;
  122. float fnum;
  123. double dnum;
  124. realm_decimal128_t decimal128;
  125. realm_object_id_t object_id;
  126. realm_uuid_t uuid;
  127. realm_link_t link;
  128. char data[16];
  129. };
  130. realm_value_type_e type;
  131. } realm_value_t;
  132. /* Error types */
  133. typedef struct realm_async_error realm_async_error_t;
  134. typedef enum realm_errno {
  135. RLM_ERR_NONE = 0,
  136. RLM_ERR_UNKNOWN,
  137. RLM_ERR_OTHER_EXCEPTION,
  138. RLM_ERR_OUT_OF_MEMORY,
  139. RLM_ERR_NOT_CLONABLE,
  140. RLM_ERR_NOT_IN_A_TRANSACTION,
  141. RLM_ERR_WRONG_THREAD,
  142. RLM_ERR_INVALIDATED_OBJECT,
  143. RLM_ERR_INVALID_PROPERTY,
  144. RLM_ERR_MISSING_PROPERTY_VALUE,
  145. RLM_ERR_PROPERTY_TYPE_MISMATCH,
  146. RLM_ERR_MISSING_PRIMARY_KEY,
  147. RLM_ERR_UNEXPECTED_PRIMARY_KEY,
  148. RLM_ERR_WRONG_PRIMARY_KEY_TYPE,
  149. RLM_ERR_MODIFY_PRIMARY_KEY,
  150. RLM_ERR_READ_ONLY_PROPERTY,
  151. RLM_ERR_PROPERTY_NOT_NULLABLE,
  152. RLM_ERR_INVALID_ARGUMENT,
  153. RLM_ERR_LOGIC,
  154. RLM_ERR_NO_SUCH_TABLE,
  155. RLM_ERR_NO_SUCH_OBJECT,
  156. RLM_ERR_CROSS_TABLE_LINK_TARGET,
  157. RLM_ERR_UNSUPPORTED_FILE_FORMAT_VERSION,
  158. RLM_ERR_MULTIPLE_SYNC_AGENTS,
  159. RLM_ERR_ADDRESS_SPACE_EXHAUSTED,
  160. RLM_ERR_MAXIMUM_FILE_SIZE_EXCEEDED,
  161. RLM_ERR_OUT_OF_DISK_SPACE,
  162. RLM_ERR_KEY_NOT_FOUND,
  163. RLM_ERR_COLUMN_NOT_FOUND,
  164. RLM_ERR_COLUMN_ALREADY_EXISTS,
  165. RLM_ERR_KEY_ALREADY_USED,
  166. RLM_ERR_SERIALIZATION_ERROR,
  167. RLM_ERR_INVALID_PATH_ERROR,
  168. RLM_ERR_DUPLICATE_PRIMARY_KEY_VALUE,
  169. RLM_ERR_INDEX_OUT_OF_BOUNDS,
  170. RLM_ERR_INVALID_QUERY_STRING,
  171. RLM_ERR_INVALID_QUERY,
  172. RLM_ERR_CALLBACK = 1000000, /**< A user-provided callback failed. */
  173. } realm_errno_e;
  174. typedef enum realm_logic_error_kind {
  175. RLM_LOGIC_ERR_NONE = 0,
  176. RLM_LOGIC_ERR_STRING_TOO_BIG,
  177. // ...
  178. } realm_logic_error_kind_e;
  179. typedef struct realm_error {
  180. realm_errno_e error;
  181. const char* message;
  182. union {
  183. int code;
  184. realm_logic_error_kind_e logic_error_kind;
  185. } kind;
  186. } realm_error_t;
  187. /* Schema types */
  188. typedef enum realm_column_attr {
  189. // Values matching `realm::ColumnAttr`.
  190. RLM_COLUMN_ATTR_NONE = 0,
  191. RLM_COLUMN_ATTR_INDEXED = 1,
  192. RLM_COLUMN_ATTR_UNIQUE = 2,
  193. RLM_COLUMN_ATTR_RESERVED = 4,
  194. RLM_COLUMN_ATTR_STRONG_LINKS = 8,
  195. RLM_COLUMN_ATTR_NULLABLE = 16,
  196. RLM_COLUMN_ATTR_LIST = 32,
  197. RLM_COLUMN_ATTR_DICTIONARY = 64,
  198. RLM_COLUMN_ATTR_COLLECTION = 64 + 32,
  199. } realm_column_attr_e;
  200. typedef enum realm_property_type {
  201. // Values matching `realm::ColumnType`.
  202. RLM_PROPERTY_TYPE_INT = 0,
  203. RLM_PROPERTY_TYPE_BOOL = 1,
  204. RLM_PROPERTY_TYPE_STRING = 2,
  205. RLM_PROPERTY_TYPE_BINARY = 4,
  206. RLM_PROPERTY_TYPE_MIXED = 6,
  207. RLM_PROPERTY_TYPE_TIMESTAMP = 8,
  208. RLM_PROPERTY_TYPE_FLOAT = 9,
  209. RLM_PROPERTY_TYPE_DOUBLE = 10,
  210. RLM_PROPERTY_TYPE_DECIMAL128 = 11,
  211. RLM_PROPERTY_TYPE_OBJECT = 12,
  212. RLM_PROPERTY_TYPE_LINKING_OBJECTS = 14,
  213. RLM_PROPERTY_TYPE_OBJECT_ID = 15,
  214. RLM_PROPERTY_TYPE_UUID = 17,
  215. } realm_property_type_e;
  216. typedef enum realm_collection_type {
  217. RLM_COLLECTION_TYPE_NONE = 0,
  218. RLM_COLLECTION_TYPE_LIST = 1,
  219. RLM_COLLECTION_TYPE_SET = 2,
  220. RLM_COLLECTION_TYPE_DICTIONARY = 4,
  221. } realm_collection_type_e;
  222. typedef struct realm_property_info {
  223. const char* name;
  224. const char* public_name;
  225. realm_property_type_e type;
  226. realm_collection_type_e collection_type;
  227. const char* link_target;
  228. const char* link_origin_property_name;
  229. realm_property_key_t key;
  230. int flags;
  231. } realm_property_info_t;
  232. typedef struct realm_class_info {
  233. const char* name;
  234. const char* primary_key;
  235. size_t num_properties;
  236. size_t num_computed_properties;
  237. realm_class_key_t key;
  238. int flags;
  239. } realm_class_info_t;
  240. typedef enum realm_class_flags {
  241. RLM_CLASS_NORMAL = 0,
  242. RLM_CLASS_EMBEDDED = 1,
  243. } realm_class_flags_e;
  244. typedef enum realm_property_flags {
  245. RLM_PROPERTY_NORMAL = 0,
  246. RLM_PROPERTY_NULLABLE = 1,
  247. RLM_PROPERTY_PRIMARY_KEY = 2,
  248. RLM_PROPERTY_INDEXED = 4,
  249. } realm_property_flags_e;
  250. /* Notification types */
  251. typedef struct realm_notification_token realm_notification_token_t;
  252. typedef struct realm_object_changes realm_object_changes_t;
  253. typedef struct realm_collection_changes realm_collection_changes_t;
  254. typedef void (*realm_on_object_change_func_t)(void* userdata, const realm_object_changes_t*);
  255. typedef void (*realm_on_collection_change_func_t)(void* userdata, const realm_collection_changes_t*);
  256. typedef void (*realm_callback_error_func_t)(void* userdata, const realm_async_error_t*);
  257. /* Scheduler types */
  258. typedef void (*realm_scheduler_notify_func_t)(void* userdata);
  259. typedef bool (*realm_scheduler_is_on_thread_func_t)(void* userdata);
  260. typedef bool (*realm_scheduler_is_same_as_func_t)(const void* userdata1, const void* userdata2);
  261. typedef bool (*realm_scheduler_can_deliver_notifications_func_t)(void* userdata);
  262. typedef void (*realm_scheduler_set_notify_callback_func_t)(void* userdata, void* callback_userdata,
  263. realm_free_userdata_func_t, realm_scheduler_notify_func_t);
  264. typedef realm_scheduler_t* (*realm_scheduler_default_factory_func_t)(void* userdata);
  265. /* Sync types */
  266. typedef void (*realm_sync_upload_completion_func_t)(void* userdata, realm_async_error_t*);
  267. typedef void (*realm_sync_download_completion_func_t)(void* userdata, realm_async_error_t*);
  268. typedef void (*realm_sync_connection_state_changed_func_t)(void* userdata, int, int);
  269. typedef void (*realm_sync_session_state_changed_func_t)(void* userdata, int, int);
  270. typedef void (*realm_sync_progress_func_t)(void* userdata, size_t transferred, size_t total);
  271. /**
  272. * Get a string representing the version number of the Realm library.
  273. *
  274. * @return A null-terminated string.
  275. */
  276. RLM_API const char* realm_get_library_version(void);
  277. /**
  278. * Get individual components of the version number of the Realm library.
  279. *
  280. * @param out_major The major version number (X.0.0).
  281. * @param out_minor The minor version number (0.X.0).
  282. * @param out_patch The patch version number (0.0.X).
  283. * @param out_extra The extra version string (0.0.0-X).
  284. */
  285. RLM_API void realm_get_library_version_numbers(int* out_major, int* out_minor, int* out_patch,
  286. const char** out_extra);
  287. /**
  288. * Get the last error that happened on this thread.
  289. *
  290. * Errors are thread-local. Getting the error must happen on the same thread as
  291. * the call that caused the error to occur. The error is specific to the current
  292. * thread, and not the Realm instance for which the error occurred.
  293. *
  294. * Note: The error message in @a err will only be safe to use until the next API
  295. * call is made on the current thread.
  296. *
  297. * Note: The error is not cleared by subsequent successful calls to this
  298. * function, but it will be overwritten by subsequent failing calls to
  299. * other library functions.
  300. *
  301. * Note: Calling this function does not clear the current last error.
  302. *
  303. * This function does not allocate any memory.
  304. *
  305. * @param err A pointer to a `realm_error_t` struct that will be populated with
  306. * information about the last error, if there is one. May be NULL.
  307. * @return True if an error occurred.
  308. */
  309. RLM_API bool realm_get_last_error(realm_error_t* err);
  310. /**
  311. * Get information about an async error, potentially coming from another thread.
  312. *
  313. * This function does not allocate any memory.
  314. *
  315. * @param err A pointer to a `realm_error_t` struct that will be populated with
  316. * information about the error. May not be NULL.
  317. * @see realm_get_last_error()
  318. */
  319. RLM_API void realm_get_async_error(const realm_async_error_t* err, realm_error_t* out_err);
  320. /**
  321. * Convert the last error to `realm_async_error_t`, which can safely be passed
  322. * between threads.
  323. *
  324. * Note: This function does not clear the last error.
  325. *
  326. * @return A non-null pointer if there was an error on this thread.
  327. * @see realm_get_last_error()
  328. * @see realm_get_async_error()
  329. * @see realm_clear_last_error()
  330. */
  331. RLM_API realm_async_error_t* realm_get_last_error_as_async_error(void);
  332. #if defined(__cplusplus)
  333. /**
  334. * Rethrow the last exception.
  335. *
  336. * Note: This function does not have C linkage, because throwing across language
  337. * boundaries is undefined behavior. When called from C code, this should result
  338. * in a linker error. When called from C++, `std::rethrow_exception` will be
  339. * called to propagate the exception unchanged.
  340. */
  341. RLM_EXPORT void realm_rethrow_last_error(void);
  342. /**
  343. * Invoke a function that may throw an exception, and report that exception as
  344. * part of the C API error handling mechanism.
  345. *
  346. * This is used to test translation of exceptions to error codes.
  347. *
  348. * @return True if no exception was thrown.
  349. */
  350. RLM_EXPORT bool realm_wrap_exceptions(void (*)()) noexcept;
  351. #endif // __cplusplus
  352. /**
  353. * Clear the last error on the calling thread.
  354. *
  355. * Use this if the system has recovered from an error, e.g. by closing the
  356. * offending Realm and reopening it, freeing up resources, or similar.
  357. *
  358. * @return True if an error was cleared.
  359. */
  360. RLM_API bool realm_clear_last_error(void);
  361. /**
  362. * Free any Realm C Wrapper object.
  363. *
  364. * Note: Any pointer returned from a library function is owned by the caller.
  365. * The caller is responsible for calling `realm_release()`. The only
  366. * exception from this is C++ bridge functions that return `void*`, with
  367. * the prefix `_realm`.
  368. *
  369. * Note: C++ destructors are typically `noexcept`, so it is likely that an
  370. * exception will crash the process.
  371. *
  372. * @param ptr A pointer to a Realm C Wrapper object. May be NULL.
  373. */
  374. RLM_API void realm_release(void* ptr);
  375. /**
  376. * Clone a Realm C Wrapper object.
  377. *
  378. * If the object is not clonable, this function fails with RLM_ERR_NOT_CLONABLE.
  379. *
  380. * @return A pointer to an object of the same type as the input, or NULL if
  381. * cloning failed.
  382. */
  383. RLM_API void* realm_clone(const void*);
  384. /**
  385. * Return true if two API objects refer to the same underlying data. Objects
  386. * with different types are never equal.
  387. *
  388. * Note: This function cannot be used with types that have value semantics, only
  389. * opaque types that have object semantics.
  390. *
  391. * - `realm_t` objects are identical if they represent the same instance (not
  392. * just if they represent the same file).
  393. * - `realm_schema_t` objects are equal if the represented schemas are equal.
  394. * - `realm_config_t` objects are equal if the configurations are equal.
  395. * - `realm_object_t` objects are identical if they belong to the same realm
  396. * and class, and have the same object key.
  397. * - `realm_list_t` and other collection objects are identical if they come
  398. * from the same object and property.
  399. * - `realm_query_t` objects are never equal.
  400. * - `realm_scheduler_t` objects are equal if they represent the same
  401. * scheduler.
  402. * - Query descriptor objects are equal if they represent equivalent
  403. * descriptors.
  404. * - `realm_async_error_t` objects are equal if they represent the same
  405. * exception instance.
  406. *
  407. * This function cannot fail.
  408. */
  409. RLM_API bool realm_equals(const void*, const void*);
  410. /**
  411. * True if a Realm C Wrapper object is "frozen" (immutable).
  412. *
  413. * Objects, collections, and results can be frozen. For all other types, this
  414. * function always returns false.
  415. */
  416. RLM_API bool realm_is_frozen(const void*);
  417. /**
  418. * Get a thread-safe reference representing the same underlying object as some
  419. * API object.
  420. *
  421. * The thread safe reference can be passed to a different thread and resolved
  422. * against a different `realm_t` instance, which succeeds if the underlying
  423. * object still exists.
  424. *
  425. * The following types can produce thread safe references:
  426. *
  427. * - `realm_object_t`
  428. * - `realm_results_t`
  429. * - `realm_list_t`
  430. * - `realm_t`
  431. *
  432. * This does not assume ownership of the object, except for `realm_t`, where the
  433. * instance is transferred by value, and must be transferred back to the current
  434. * thread to be used. Note that the `realm_thread_safe_reference_t` object must
  435. * still be destroyed after having been converted into a `realm_t` object.
  436. *
  437. * @return A non-null pointer if no exception occurred.
  438. */
  439. RLM_API realm_thread_safe_reference_t* realm_create_thread_safe_reference(const void*);
  440. /**
  441. * Allocate a new configuration with default options.
  442. */
  443. RLM_API realm_config_t* realm_config_new(void);
  444. /**
  445. * Get the path of the realm being opened.
  446. *
  447. * This function cannot fail.
  448. */
  449. RLM_API const char* realm_config_get_path(const realm_config_t*);
  450. /**
  451. * Set the path of the realm being opened.
  452. *
  453. * This function aborts when out of memory, but otherwise cannot fail.
  454. */
  455. RLM_API void realm_config_set_path(realm_config_t*, const char* path);
  456. /**
  457. * Get the encryption key for the realm.
  458. *
  459. * The output buffer must be at least 64 bytes.
  460. *
  461. * @returns The length of the encryption key (0 or 64)
  462. */
  463. RLM_API size_t realm_config_get_encryption_key(const realm_config_t*, uint8_t* out_key);
  464. /**
  465. * Set the encryption key for the realm.
  466. *
  467. * The key must be either 64 bytes long or have length zero (in which case
  468. * encryption is disabled).
  469. *
  470. * This function may fail if the encryption key has the wrong length.
  471. */
  472. RLM_API bool realm_config_set_encryption_key(realm_config_t*, const uint8_t* key, size_t key_size);
  473. /**
  474. * Get the schema for this realm.
  475. *
  476. * Note: The caller obtains ownership of the returned value, and must manually
  477. * free it by calling `realm_release()`.
  478. *
  479. * @return A schema object, or NULL if the schema is not set (empty).
  480. */
  481. RLM_API realm_schema_t* realm_config_get_schema(const realm_config_t*);
  482. /**
  483. * Set the schema object for this realm.
  484. *
  485. * This does not take ownership of the schema object, and it should be released
  486. * afterwards.
  487. *
  488. * This function aborts when out of memory, but otherwise cannot fail.
  489. *
  490. * @param schema The schema object. May be NULL, which means an empty schema.
  491. */
  492. RLM_API void realm_config_set_schema(realm_config_t*, const realm_schema_t* schema);
  493. /**
  494. * Get the schema version of the schema.
  495. *
  496. * This function cannot fail.
  497. */
  498. RLM_API uint64_t realm_config_get_schema_version(const realm_config_t*);
  499. /**
  500. * Set the schema version of the schema.
  501. *
  502. * This function cannot fail.
  503. */
  504. RLM_API void realm_config_set_schema_version(realm_config_t*, uint64_t version);
  505. /**
  506. * Get the schema mode.
  507. *
  508. * This function cannot fail.
  509. */
  510. RLM_API realm_schema_mode_e realm_config_get_schema_mode(const realm_config_t*);
  511. /**
  512. * Set the schema mode.
  513. *
  514. * This function cannot fail.
  515. */
  516. RLM_API void realm_config_set_schema_mode(realm_config_t*, realm_schema_mode_e);
  517. /**
  518. * Set the migration callback.
  519. *
  520. * The migration function is called during a migration for schema modes
  521. * `RLM_SCHEMA_MODE_AUTOMATIC` and `RLM_SCHEMA_MODE_MANUAL`. The callback is
  522. * invoked with a realm instance before the migration and the realm instance
  523. * that is currently performing the migration.
  524. *
  525. * This function cannot fail.
  526. */
  527. RLM_API void realm_config_set_migration_function(realm_config_t*, realm_migration_func_t, void* userdata);
  528. /**
  529. * Set the data initialization function.
  530. *
  531. * The callback is invoked the first time the schema is created, such that the
  532. * user can perform one-time initialization of the data in the realm.
  533. *
  534. * The realm instance passed to the callback is in a write transaction.
  535. *
  536. * This function cannot fail.
  537. */
  538. RLM_API void realm_config_set_data_initialization_function(realm_config_t*, realm_data_initialization_func_t,
  539. void* userdata);
  540. /**
  541. * Set the should-compact-on-launch callback.
  542. *
  543. * The callback is invoked the first time a realm file is opened in this process
  544. * to decide whether the realm file should be compacted.
  545. *
  546. * Note: If another process has the realm file open, it will not be compacted.
  547. *
  548. * This function cannot fail.
  549. */
  550. RLM_API void realm_config_set_should_compact_on_launch_function(realm_config_t*,
  551. realm_should_compact_on_launch_func_t,
  552. void* userdata);
  553. /**
  554. * True if file format upgrades on open are disabled.
  555. *
  556. * This function cannot fail.
  557. */
  558. RLM_API bool realm_config_get_disable_format_upgrade(const realm_config_t*);
  559. /**
  560. * Disable file format upgrade on open (default: false).
  561. *
  562. * If a migration is needed to open the realm file with the provided schema, an
  563. * error is thrown rather than automatically performing the migration.
  564. *
  565. * This function cannot fail.
  566. */
  567. RLM_API void realm_config_set_disable_format_upgrade(realm_config_t*, bool);
  568. /**
  569. * True if automatic change notifications should be generated.
  570. *
  571. * This function cannot fail.
  572. */
  573. RLM_API bool realm_config_get_automatic_change_notifications(const realm_config_t*);
  574. /**
  575. * Automatically generated change notifications (default: true).
  576. *
  577. * This function cannot fail.
  578. */
  579. RLM_API void realm_config_set_automatic_change_notifications(realm_config_t*, bool);
  580. /**
  581. * The scheduler which this realm should be bound to (default: NULL).
  582. *
  583. * If NULL, the realm will be bound to the default scheduler for the current thread.
  584. *
  585. * This function aborts when out of memory, but otherwise cannot fail.
  586. */
  587. RLM_API void realm_config_set_scheduler(realm_config_t*, const realm_scheduler_t*);
  588. /**
  589. * Sync configuration for this realm (default: NULL).
  590. *
  591. * This function aborts when out of memory, but otherwise cannot fail.
  592. */
  593. RLM_API void realm_config_set_sync_config(realm_config_t*, realm_sync_config_t*);
  594. /**
  595. * Get whether the realm file should be forcibly initialized as a synchronized.
  596. *
  597. * This function cannot fail.
  598. */
  599. RLM_API bool realm_config_get_force_sync_history(const realm_config_t*);
  600. /**
  601. * Force the realm file to be initialized as a synchronized realm, even if no
  602. * sync config is provided (default: false).
  603. *
  604. * This function cannot fail.
  605. */
  606. RLM_API void realm_config_set_force_sync_history(realm_config_t*, bool);
  607. /**
  608. * Set the audit interface for the realm (unimplemented).
  609. */
  610. RLM_API bool realm_config_set_audit_factory(realm_config_t*, void*);
  611. /**
  612. * Get maximum number of active versions in the realm file allowed before an
  613. * exception is thrown.
  614. *
  615. * This function cannot fail.
  616. */
  617. RLM_API uint64_t realm_config_get_max_number_of_active_versions(const realm_config_t*);
  618. /**
  619. * Set maximum number of active versions in the realm file allowed before an
  620. * exception is thrown (default: UINT64_MAX).
  621. *
  622. * This function cannot fail.
  623. */
  624. RLM_API void realm_config_set_max_number_of_active_versions(realm_config_t*, uint64_t);
  625. /**
  626. * Create a custom scheduler object from callback functions.
  627. *
  628. * @param userdata Pointer passed to all callbacks.
  629. * @param notify Function to trigger a call to the registered callback on the
  630. * scheduler's event loop. This function must be thread-safe, or
  631. * NULL, in which case the scheduler is considered unable to
  632. * deliver notifications.
  633. * @param is_on_thread Function to return true if called from the same thread as
  634. * the scheduler. This function must be thread-safe.
  635. * @param can_deliver_notifications Function to return true if the scheduler can
  636. * support `notify()`. This function does not
  637. * need to be thread-safe.
  638. * @param set_notify_callback Function to accept a callback that will be invoked
  639. * by `notify()` on the scheduler's event loop. This
  640. * function does not need to be thread-safe.
  641. */
  642. RLM_API realm_scheduler_t*
  643. realm_scheduler_new(void* userdata, realm_free_userdata_func_t, realm_scheduler_notify_func_t notify,
  644. realm_scheduler_is_on_thread_func_t is_on_thread, realm_scheduler_is_same_as_func_t is_same_as,
  645. realm_scheduler_can_deliver_notifications_func_t can_deliver_notifications,
  646. realm_scheduler_set_notify_callback_func_t set_notify_callback);
  647. /**
  648. * Create an instance of the default scheduler for the current platform,
  649. * normally confined to the calling thread.
  650. */
  651. RLM_API realm_scheduler_t* realm_scheduler_make_default(void);
  652. /**
  653. * Get the scheduler used by frozen realms. This scheduler does not support
  654. * notifications, and does not perform any thread checking.
  655. *
  656. * This function is thread-safe, and cannot fail.
  657. */
  658. RLM_API const realm_scheduler_t* realm_scheduler_get_frozen(void);
  659. /**
  660. * Returns true if there is a default scheduler implementation for the current
  661. * platform, or one has been set with `realm_scheduler_set_default_factory()`.
  662. *
  663. * If there is no default factory, and no scheduler is provided in the config,
  664. * `realm_open()` will fail. Note that `realm_scheduler_get_frozen()` always
  665. * returns a valid scheduler.
  666. *
  667. * This function is thread-safe, and cannot fail.
  668. */
  669. RLM_API bool realm_scheduler_has_default_factory(void);
  670. /**
  671. * For platforms with no default scheduler implementation, register a factory
  672. * function which can produce custom schedulers. If there is a platform-specific
  673. * scheduler, this function will fail. If a custom scheduler is desired for
  674. * platforms that already have a default scheduler implementation, the caller
  675. * must call `realm_open()` with a config that indicates the desired scheduler.
  676. *
  677. * The provided callback may produce a scheduler by calling
  678. * `realm_scheduler_new()`.
  679. *
  680. * This function is thread-safe, but should generally only be called once.
  681. */
  682. RLM_API bool realm_scheduler_set_default_factory(void* userdata, realm_free_userdata_func_t,
  683. realm_scheduler_default_factory_func_t);
  684. /**
  685. * Trigger a call to the registered notifier callback on the scheduler's event loop.
  686. *
  687. * This function is thread-safe.
  688. */
  689. RLM_API void realm_scheduler_notify(realm_scheduler_t*);
  690. /**
  691. * Returns true if the caller is currently running on the scheduler's thread.
  692. *
  693. * This function is thread-safe.
  694. */
  695. RLM_API bool realm_scheduler_is_on_thread(const realm_scheduler_t*);
  696. /**
  697. * Returns true if the scheduler is able to deliver notifications.
  698. *
  699. * A false return value may indicate that notifications are not applicable for
  700. * the scheduler, not implementable, or a temporary inability to deliver
  701. * notifications.
  702. *
  703. * This function is not thread-safe.
  704. */
  705. RLM_API bool realm_scheduler_can_deliver_notifications(const realm_scheduler_t*);
  706. /**
  707. * Set the callback that will be invoked by `realm_scheduler_notify()`.
  708. *
  709. * This function is not thread-safe.
  710. */
  711. RLM_API bool realm_scheduler_set_notify_callback(realm_scheduler_t*, void* userdata, realm_free_userdata_func_t,
  712. realm_scheduler_notify_func_t);
  713. /**
  714. * Open a Realm file.
  715. *
  716. * @param config Realm configuration. If the Realm is already opened on another
  717. * thread, validate that the given configuration is compatible
  718. * with the existing one.
  719. * @return If successful, the Realm object. Otherwise, NULL.
  720. */
  721. RLM_API realm_t* realm_open(const realm_config_t* config);
  722. /**
  723. * Create a `realm_t` object from a thread-safe reference to the same realm.
  724. *
  725. * @param tsr Thread-safe reference object created by calling
  726. * `realm_get_thread_safe_reference()` with a `realm_t` instance.
  727. * @param scheduler The scheduler to use for the new `realm_t` instance. May be
  728. * NULL, in which case the default scheduler for the current
  729. * thread is used.
  730. * @return A non-null pointer if no error occurred.
  731. */
  732. RLM_API realm_t* realm_from_thread_safe_reference(realm_thread_safe_reference_t* tsr, realm_scheduler_t* scheduler);
  733. /**
  734. * Create a `realm_t*` from a `std::shared_ptr<Realm>*`.
  735. *
  736. * This is intended as a migration path for users of the C++ Object Store API.
  737. *
  738. * Call `realm_release()` on the returned `realm_t*` to decrement the refcount
  739. * on the inner `std::shared_ptr<Realm>`.
  740. *
  741. * @param pshared_ptr A pointer to an instance of `std::shared_ptr<Realm>`.
  742. * @param n Must be equal to `sizeof(std::shared_ptr<Realm>)`.
  743. * @return A `realm_t*` representing the same Realm object as the passed
  744. * `std::shared_ptr<Realm>`.
  745. */
  746. RLM_API realm_t* _realm_from_native_ptr(const void* pshared_ptr, size_t n);
  747. /**
  748. * Get a `std::shared_ptr<Realm>` from a `realm_t*`.
  749. *
  750. * This is intended as a migration path for users of the C++ Object Store API.
  751. *
  752. * @param pshared_ptr A pointer to an instance of `std::shared_ptr<Realm>`.
  753. * @param n Must be equal to `sizeof(std::shared_ptr<Realm>)`.
  754. */
  755. RLM_API void _realm_get_native_ptr(const realm_t*, void* pshared_ptr, size_t n);
  756. /**
  757. * Forcibly close a Realm file.
  758. *
  759. * Note that this invalidates all Realm instances for the same path.
  760. *
  761. * The Realm will be automatically closed when the last reference is released,
  762. * including references to objects within the Realm.
  763. *
  764. * @return True if no exception occurred.
  765. */
  766. RLM_API bool realm_close(realm_t*);
  767. /**
  768. * True if the Realm file is closed.
  769. *
  770. * This function cannot fail.
  771. */
  772. RLM_API bool realm_is_closed(realm_t*);
  773. /**
  774. * Begin a write transaction for the Realm file.
  775. *
  776. * @return True if no exception occurred.
  777. */
  778. RLM_API bool realm_begin_write(realm_t*);
  779. /**
  780. * Return true if the realm is in a write transaction.
  781. *
  782. * This function cannot fail.
  783. */
  784. RLM_API bool realm_is_writable(const realm_t*);
  785. /**
  786. * Commit a write transaction.
  787. *
  788. * @return True if the commit succeeded and no exceptions were thrown.
  789. */
  790. RLM_API bool realm_commit(realm_t*);
  791. /**
  792. * Roll back a write transaction.
  793. *
  794. * @return True if the rollback succeeded and no exceptions were thrown.
  795. */
  796. RLM_API bool realm_rollback(realm_t*);
  797. /**
  798. * Refresh the view of the realm file.
  799. *
  800. * If another process or thread has made changes to the realm file, this causes
  801. * those changes to become visible in this realm instance.
  802. *
  803. * This calls `advance_read()` at the Core layer.
  804. *
  805. * @return True if the realm was successfully refreshed and no exceptions were
  806. * thrown.
  807. */
  808. RLM_API bool realm_refresh(realm_t*);
  809. /**
  810. * Produce a frozen view of this realm.
  811. *
  812. * @return A non-NULL realm instance representing the frozen state.
  813. */
  814. RLM_API realm_t* realm_freeze(realm_t*);
  815. /**
  816. * Vacuum the free space from the realm file, reducing its file size.
  817. *
  818. * @return True if compaction was successful and no exceptions were thrown.
  819. */
  820. RLM_API bool realm_compact(realm_t*, bool* did_compact);
  821. /**
  822. * Create a new schema from classes and their properties.
  823. *
  824. * Note: This function does not validate the schema.
  825. *
  826. * Note: `realm_class_key_t` and `realm_property_key_t` values inside
  827. * `realm_class_info_t` and `realm_property_info_t` are unused when
  828. * defining the schema. Call `realm_get_schema()` to obtain the values for
  829. * these fields in an open realm.
  830. *
  831. * @return True if allocation of the schema structure succeeded.
  832. */
  833. RLM_API realm_schema_t* realm_schema_new(const realm_class_info_t* classes, size_t num_classes,
  834. const realm_property_info_t** class_properties);
  835. /**
  836. * Get the schema for this realm.
  837. *
  838. * Note: The returned value is allocated by this function, so `realm_release()`
  839. * must be called on it.
  840. */
  841. RLM_API realm_schema_t* realm_get_schema(const realm_t*);
  842. /**
  843. * Update the schema of an open realm.
  844. *
  845. * This is equivalent to calling `realm_update_schema_advanced(realm, schema, 0,
  846. * NULL, NULL, NULL, NULL, false)`.
  847. */
  848. RLM_API bool realm_update_schema(realm_t* realm, const realm_schema_t* schema);
  849. /**
  850. * Update the schema of an open realm, with options to customize certain steps
  851. * of the process.
  852. *
  853. * @param realm The realm for which the schema should be updated.
  854. * @param schema The new schema for the realm. If the schema is the same the
  855. * existing schema, this function does nothing.
  856. * @param version The version of the new schema.
  857. * @param migration_func Callback to perform the migration. Has no effect if the
  858. * Realm is opened with `RLM_SCHEMA_MODE_ADDITIVE`.
  859. * @param migration_func_userdata Userdata pointer to pass to `migration_func`.
  860. * @param data_init_func Callback to perform initialization of the data in the
  861. * Realm if it is opened for the first time (i.e., it has
  862. * no previous schema version).
  863. * @param data_init_func_userdata Userdata pointer to pass to `data_init_func`.
  864. * @param is_in_transaction Pass true if the realm is already in a write
  865. * transaction. Otherwise, if the migration requires a
  866. * write transaction, this function will perform the
  867. * migration in its own write transaction.
  868. */
  869. RLM_API bool realm_update_schema_advanced(realm_t* realm, const realm_schema_t* schema, uint64_t version,
  870. realm_migration_func_t migration_func, void* migration_func_userdata,
  871. realm_data_initialization_func_t data_init_func,
  872. void* data_init_func_userdata, bool is_in_transaction);
  873. /**
  874. * Get the `realm::Schema*` pointer for this realm.
  875. *
  876. * This is intended as a migration path for users of the C++ Object Store API.
  877. *
  878. * The returned value is owned by the `realm_t` instance, and must not be freed.
  879. */
  880. RLM_API const void* _realm_get_schema_native(const realm_t*);
  881. /**
  882. * Validate the schema.
  883. *
  884. * @param validation_mode A bitwise combination of values from the
  885. * enum realm_schema_validation_mode.
  886. *
  887. * @return True if the schema passed validation. If validation failed,
  888. * `realm_get_last_error()` will produce an error describing the
  889. * validation failure.
  890. */
  891. RLM_API bool realm_schema_validate(const realm_schema_t*, uint64_t validation_mode);
  892. /**
  893. * Return the number of classes in the Realm's schema.
  894. *
  895. * This cannot fail.
  896. */
  897. RLM_API size_t realm_get_num_classes(const realm_t*);
  898. /**
  899. * Get the table keys for classes in the schema.
  900. *
  901. * @param out_keys An array that will contain the keys of each class in the
  902. * schema. May be NULL, in which case `out_n` can be used to
  903. * determine the number of classes in the schema.
  904. * @param max The maximum number of keys to write to `out_keys`.
  905. * @param out_n The actual number of classes. May be NULL.
  906. * @return True if no exception occurred.
  907. */
  908. RLM_API bool realm_get_class_keys(const realm_t*, realm_class_key_t* out_keys, size_t max, size_t* out_n);
  909. /**
  910. * Find a by the name of @a name.
  911. *
  912. * @param name The name of the class.
  913. * @param out_found Set to true if the class was found and no error occurred.
  914. * Otherwise, false. May not be NULL.
  915. * @param out_class_info A pointer to a `realm_class_info_t` that will be
  916. * populated with information about the class. May be
  917. * NULL.
  918. * @return True if no exception occurred.
  919. */
  920. RLM_API bool realm_find_class(const realm_t*, const char* name, bool* out_found, realm_class_info_t* out_class_info);
  921. /**
  922. * Get the class with @a key from the schema.
  923. *
  924. * Passing an invalid @a key for this schema is considered an error.
  925. *
  926. * @param key The key of the class, as discovered by `realm_get_class_keys()`.
  927. * @param out_class_info A pointer to a `realm_class_info_t` that will be
  928. * populated with the information of the class. May be
  929. * NULL, though that's kind of pointless.
  930. * @return True if no exception occurred.
  931. */
  932. RLM_API bool realm_get_class(const realm_t*, realm_class_key_t key, realm_class_info_t* out_class_info);
  933. /**
  934. * Get the list of properties for the class with this @a key.
  935. *
  936. * @param out_properties A pointer to an array of `realm_property_info_t`, which
  937. * will be populated with the information about the
  938. * properties. To see all properties, the length of the
  939. * array should be at least the number of properties in
  940. * the class, as reported in the sum of persisted and
  941. * computed properties for the class. May be NULL, in
  942. * which case this function can be used to discover the
  943. * number of properties in the class.
  944. * @param max The maximum number of entries to write to `out_properties`.
  945. * @param out_n The actual number of properties written to `out_properties`.
  946. * @return True if no exception occurred.
  947. */
  948. RLM_API bool realm_get_class_properties(const realm_t*, realm_class_key_t key, realm_property_info_t* out_properties,
  949. size_t max, size_t* out_n);
  950. /**
  951. * Get the property keys for the class with this @a key.
  952. *
  953. * @param key The class key.
  954. * @param out_col_keys An array of property keys. May be NULL, in which case
  955. * this function can be used to discover the number of
  956. * properties for this class.
  957. * @param max The maximum number of keys to write to `out_col_keys`. Ignored if
  958. * `out_col_keys == NULL`.
  959. * @param out_n The actual number of properties written to `out_col_keys` (if
  960. * non-NULL), or number of properties in the class.
  961. **/
  962. RLM_API bool realm_get_property_keys(const realm_t*, realm_class_key_t key, realm_property_key_t* out_col_keys,
  963. size_t max, size_t* out_n);
  964. /**
  965. * Find a property by its column key.
  966. *
  967. * It is an error to pass a property @a key that is not present in this class.
  968. *
  969. * @param class_key The key of the class.
  970. * @param key The column key for the property.
  971. * @param out_property_info A pointer to a `realm_property_info_t` that will be
  972. * populated with information about the property.
  973. * @return True if no exception occurred.
  974. */
  975. RLM_API bool realm_get_property(const realm_t*, realm_class_key_t class_key, realm_property_key_t key,
  976. realm_property_info_t* out_property_info);
  977. /**
  978. * Find a property by the internal (non-public) name of @a name.
  979. *
  980. * @param class_key The table key for the class.
  981. * @param name The name of the property.
  982. * @param out_found Will be set to true if the property was found. May not be
  983. * NULL.
  984. * @param out_property_info A pointer to a `realm_property_info_t` that will be
  985. * populated with information about the property. May
  986. * be NULL.
  987. * @return True if no exception occurred.
  988. */
  989. RLM_API bool realm_find_property(const realm_t*, realm_class_key_t class_key, const char* name, bool* out_found,
  990. realm_property_info_t* out_property_info);
  991. /**
  992. * Find a property with the public name of @a name.
  993. *
  994. * @param class_key The table key for the class.
  995. * @param public_name The public name of the property.
  996. * @param out_found Will be set to true if the property was found. May not be
  997. * NULL.
  998. * @param out_property_info A pointer to a `realm_property_info_t` that will be
  999. * populated with information about the property. May
  1000. * be NULL.
  1001. * @return True if no exception occurred.
  1002. */
  1003. RLM_API bool realm_find_property_by_public_name(const realm_t*, realm_class_key_t class_key, const char* public_name,
  1004. bool* out_found, realm_property_info_t* out_property_info);
  1005. /**
  1006. * Find the primary key property for a class, if it has one.
  1007. *
  1008. * @param class_key The table key for this class.
  1009. * @param out_found Will be set to true if the property was found. May not be
  1010. * NULL.
  1011. * @param out_property_info A property to a `realm_property_info_t` that will be
  1012. * populated with information about the property, if it
  1013. * was found. May be NULL.
  1014. * @return True if no exception occurred.
  1015. */
  1016. RLM_API bool realm_find_primary_key_property(const realm_t*, realm_class_key_t class_key, bool* out_found,
  1017. realm_property_info_t* out_property_info);
  1018. /**
  1019. * Get the number of objects in a table (class).
  1020. *
  1021. * @param out_count A pointer to a `size_t` that will contain the number of
  1022. * objects, if successful.
  1023. * @return True if the table key was valid for this realm.
  1024. */
  1025. RLM_API bool realm_get_num_objects(const realm_t*, realm_class_key_t, size_t* out_count);
  1026. /**
  1027. * Get an object with a particular object key.
  1028. *
  1029. * @param class_key The class key.
  1030. * @param obj_key The key to the object. Passing a non-existent key is
  1031. * considered an error.
  1032. * @return A non-NULL pointer if no exception occurred.
  1033. */
  1034. RLM_API realm_object_t* realm_get_object(const realm_t*, realm_class_key_t class_key, realm_object_key_t obj_key);
  1035. /**
  1036. * Find an object with a particular primary key value.
  1037. *
  1038. * @param out_found A pointer to a boolean that will be set to true or false if
  1039. * no error occurred.
  1040. * @return A non-NULL pointer if the object was found and no exception occurred.
  1041. */
  1042. RLM_API realm_object_t* realm_object_find_with_primary_key(const realm_t*, realm_class_key_t, realm_value_t pk,
  1043. bool* out_found);
  1044. /**
  1045. * Find all objects in class.
  1046. *
  1047. * Note: This is faster than running a query matching all objects (such as
  1048. * "TRUEPREDICATE").
  1049. *
  1050. * @return A non-NULL pointer if no exception was thrown.
  1051. */
  1052. RLM_API realm_results_t* realm_object_find_all(const realm_t*, realm_class_key_t);
  1053. /**
  1054. * Create an object in a class without a primary key.
  1055. *
  1056. * @return A non-NULL pointer if the object was created successfully.
  1057. */
  1058. RLM_API realm_object_t* realm_object_create(realm_t*, realm_class_key_t);
  1059. /**
  1060. * Create an object in a class with a primary key.
  1061. *
  1062. * @return A non-NULL pointer if the object was created successfully.
  1063. */
  1064. RLM_API realm_object_t* realm_object_create_with_primary_key(realm_t*, realm_class_key_t, realm_value_t pk);
  1065. /**
  1066. * Delete a realm object.
  1067. *
  1068. * Note: This does not call `realm_release()` on the `realm_object_t` instance.
  1069. *
  1070. * @return True if no exception occurred.
  1071. */
  1072. RLM_API bool realm_object_delete(realm_object_t*);
  1073. RLM_API realm_object_t* _realm_object_from_native_copy(const void* pobj, size_t n);
  1074. RLM_API realm_object_t* _realm_object_from_native_move(void* pobj, size_t n);
  1075. RLM_API const void* _realm_object_get_native_ptr(realm_object_t*);
  1076. /**
  1077. * True if this object still exists in the realm.
  1078. *
  1079. * This function cannot fail.
  1080. */
  1081. RLM_API bool realm_object_is_valid(const realm_object_t*);
  1082. /**
  1083. * Get the key for this object.
  1084. *
  1085. * This function cannot fail.
  1086. */
  1087. RLM_API realm_object_key_t realm_object_get_key(const realm_object_t* object);
  1088. /**
  1089. * Get the table for this object.
  1090. *
  1091. * This function cannot fail.
  1092. */
  1093. RLM_API realm_class_key_t realm_object_get_table(const realm_object_t* object);
  1094. /**
  1095. * Get a `realm_link_t` representing a link to @a object.
  1096. *
  1097. * This function cannot fail.
  1098. */
  1099. RLM_API realm_link_t realm_object_as_link(const realm_object_t* object);
  1100. /**
  1101. * Subscribe to notifications for this object.
  1102. *
  1103. * @return A non-null pointer if no exception occurred.
  1104. */
  1105. RLM_API realm_notification_token_t* realm_object_add_notification_callback(realm_object_t*, void* userdata,
  1106. realm_free_userdata_func_t free,
  1107. realm_on_object_change_func_t on_change,
  1108. realm_callback_error_func_t on_error,
  1109. realm_scheduler_t*);
  1110. /**
  1111. * Get an object from a thread-safe reference, potentially originating in a
  1112. * different `realm_t` instance
  1113. */
  1114. RLM_API realm_object_t* realm_object_from_thread_safe_reference(const realm_t*, realm_thread_safe_reference_t*);
  1115. /**
  1116. * Get the value for a property.
  1117. *
  1118. * @return True if no exception occurred.
  1119. */
  1120. RLM_API bool realm_get_value(const realm_object_t*, realm_property_key_t, realm_value_t* out_value);
  1121. /**
  1122. * Get the values for several properties.
  1123. *
  1124. * This is provided as an alternative to calling `realm_get_value()` multiple
  1125. * times in a row, which is particularly useful for language runtimes where
  1126. * crossing the native bridge is comparatively expensive. In addition, it
  1127. * eliminates some parameter validation that would otherwise be repeated for
  1128. * each call.
  1129. *
  1130. * Example use cases:
  1131. *
  1132. * - Extracting all properties of an object for serialization.
  1133. * - Converting an object to some in-memory representation.
  1134. *
  1135. * @param num_values The number of elements in @a properties and @a out_values.
  1136. * @param properties The keys for the properties to fetch. May not be NULL.
  1137. * @param out_values Where to write the property values. If an error occurs,
  1138. * this array may only be partially initialized. May not be
  1139. * NULL.
  1140. * @return True if no exception occurs.
  1141. */
  1142. RLM_API bool realm_get_values(const realm_object_t*, size_t num_values, const realm_property_key_t* properties,
  1143. realm_value_t* out_values);
  1144. /**
  1145. * Set the value for a property.
  1146. *
  1147. * @param new_value The new value for the property.
  1148. * @param is_default True if this property is being set as part of setting the
  1149. * default values for a new object. This has no effect in
  1150. * non-sync'ed realms.
  1151. * @return True if no exception occurred.
  1152. */
  1153. RLM_API bool realm_set_value(realm_object_t*, realm_property_key_t, realm_value_t new_value, bool is_default);
  1154. /**
  1155. * Set the values for several properties.
  1156. *
  1157. * This is provided as an alternative to calling `realm_get_value()` multiple
  1158. * times in a row, which is particularly useful for language runtimes where
  1159. * crossing the native bridge is comparatively expensive. In addition, it
  1160. * eliminates some parameter validation that would otherwise be repeated for
  1161. * each call.
  1162. *
  1163. * Example use cases:
  1164. *
  1165. * - Initializing a new object with default values.
  1166. * - Deserializing some in-memory structure into a realm object.
  1167. *
  1168. * This operation is "atomic"; if an exception occurs due to invalid input (such
  1169. * as type mismatch, nullability mismatch, etc.), the object will remain
  1170. * unmodified.
  1171. *
  1172. * @param num_values The number of elements in @a properties and @a values.
  1173. * @param properties The keys of the properties to set. May not be NULL.
  1174. * @param values The values to assign to the properties. May not be NULL.
  1175. * @param is_default True if the properties are being set as part of setting
  1176. * default values for a new object. This has no effect in
  1177. * non-sync'ed realms.
  1178. * @return True if no exception occurred.
  1179. */
  1180. RLM_API bool realm_set_values(realm_object_t*, size_t num_values, const realm_property_key_t* properties,
  1181. const realm_value_t* values, bool is_default);
  1182. /**
  1183. * Get a list instance for the property of an object.
  1184. *
  1185. * Note: It is up to the caller to call `realm_release()` on the returned list.
  1186. *
  1187. * @return A non-null pointer if no exception occurred.
  1188. */
  1189. RLM_API realm_list_t* realm_get_list(realm_object_t*, realm_property_key_t);
  1190. /**
  1191. * Create a `realm_list_t` from a pointer to a `realm::List`, copy-constructing
  1192. * the internal representation.
  1193. *
  1194. * @param plist A pointer to an instance of `realm::List`.
  1195. * @param n Must be equal to `sizeof(realm::List)`.
  1196. * @return A non-null pointer if no exception occurred.
  1197. */
  1198. RLM_API realm_list_t* _realm_list_from_native_copy(const void* plist, size_t n);
  1199. /**
  1200. * Create a `realm_list_t` from a pointer to a `realm::List`, move-constructing
  1201. * the internal representation.
  1202. *
  1203. * @param plist A pointer to an instance of `realm::List`.
  1204. * @param n Must be equal to `sizeof(realm::List)`.
  1205. * @return A non-null pointer if no exception occurred.
  1206. */
  1207. RLM_API realm_list_t* _realm_list_from_native_move(void* plist, size_t n);
  1208. /**
  1209. * Get the size of a list, in number of elements.
  1210. *
  1211. * This function may fail if the object owning the list has been deleted.
  1212. *
  1213. * @param out_size Where to put the list size. May be NULL.
  1214. * @return True if no exception occurred.
  1215. */
  1216. RLM_API bool realm_list_size(const realm_list_t*, size_t* out_size);
  1217. /**
  1218. * Get the property that this list came from.
  1219. *
  1220. * @return True if no exception occurred.
  1221. */
  1222. RLM_API bool realm_list_get_property(const realm_list_t*, realm_property_info_t* out_property_info);
  1223. /**
  1224. * Get the value at @a index.
  1225. *
  1226. * @param out_value The resulting value, if no error occurred. May be NULL,
  1227. * though nonsensical.
  1228. * @return True if no exception occurred.
  1229. */
  1230. RLM_API bool realm_list_get(const realm_list_t*, size_t index, realm_value_t* out_value);
  1231. /**
  1232. * Set the value at @a index.
  1233. *
  1234. * @param value The value to set.
  1235. * @return True if no exception occurred.
  1236. */
  1237. RLM_API bool realm_list_set(realm_list_t*, size_t index, realm_value_t value);
  1238. /**
  1239. * Insert @a value at @a index.
  1240. *
  1241. * @param value The value to insert.
  1242. * @return True if no exception occurred.
  1243. */
  1244. RLM_API bool realm_list_insert(realm_list_t*, size_t index, realm_value_t value);
  1245. /**
  1246. * Erase the element at @a index.
  1247. *
  1248. * @return True if no exception occurred.
  1249. */
  1250. RLM_API bool realm_list_erase(realm_list_t*, size_t index);
  1251. /**
  1252. * Clear a list, removing all elements in the list. In a list of links, this
  1253. * does *NOT* delete the target objects.
  1254. *
  1255. * @return True if no exception occurred.
  1256. */
  1257. RLM_API bool realm_list_clear(realm_list_t*);
  1258. /**
  1259. * In a list of objects, delete all objects in the list and clear the list. In a
  1260. * list of values, clear the list.
  1261. *
  1262. * @return True if no exception occurred.
  1263. */
  1264. RLM_API bool realm_list_remove_all(realm_list_t*);
  1265. /**
  1266. * Replace the contents of a list with values.
  1267. *
  1268. * This is equivalent to calling `realm_list_clear()`, and then
  1269. * `realm_list_insert()` repeatedly.
  1270. *
  1271. * @return True if no exception occurred.
  1272. */
  1273. RLM_API bool realm_list_assign(realm_list_t*, const realm_value_t* values, size_t num_values);
  1274. /**
  1275. * Subscribe to notifications for this object.
  1276. *
  1277. * @return A non-null pointer if no exception occurred.
  1278. */
  1279. RLM_API realm_notification_token_t* realm_list_add_notification_callback(realm_list_t*, void* userdata,
  1280. realm_free_userdata_func_t free,
  1281. realm_on_collection_change_func_t on_change,
  1282. realm_callback_error_func_t on_error,
  1283. realm_scheduler_t*);
  1284. /**
  1285. * Get an list from a thread-safe reference, potentially originating in a
  1286. * different `realm_t` instance
  1287. */
  1288. RLM_API realm_list_t* realm_list_from_thread_safe_reference(const realm_t*, realm_thread_safe_reference_t*);
  1289. /**
  1290. * True if an object notification indicates that the object was deleted.
  1291. *
  1292. * This function cannot fail.
  1293. */
  1294. RLM_API bool realm_object_changes_is_deleted(const realm_object_changes_t*);
  1295. /**
  1296. * Get the number of properties that were modified in an object notification.
  1297. *
  1298. * This function cannot fail.
  1299. */
  1300. RLM_API size_t realm_object_changes_get_num_modified_properties(const realm_object_changes_t*);
  1301. /**
  1302. * Get the column keys for the properties that were modified in an object
  1303. * notification.
  1304. *
  1305. * This function cannot fail.
  1306. *
  1307. * @param out_modified Where the column keys should be written. May be NULL.
  1308. * @param max The maximum number of column keys to write.
  1309. * @return The number of column keys written to @a out_modified, or the number
  1310. * of modified properties if @a out_modified is NULL.
  1311. */
  1312. RLM_API size_t realm_object_changes_get_modified_properties(const realm_object_changes_t*,
  1313. realm_property_key_t* out_modified, size_t max);
  1314. /**
  1315. * Get the number of various types of changes in a collection notification.
  1316. *
  1317. * @param out_num_deletions The number of deletions. May be NULL.
  1318. * @param out_num_insertions The number of insertions. May be NULL.
  1319. * @param out_num_modifications The number of modifications. May be NULL.
  1320. * @param out_num_moves The number of moved elements. May be NULL.
  1321. */
  1322. RLM_API void realm_collection_changes_get_num_changes(const realm_collection_changes_t*, size_t* out_num_deletions,
  1323. size_t* out_num_insertions, size_t* out_num_modifications,
  1324. size_t* out_num_moves);
  1325. /**
  1326. * Get the number of various types of changes in a collection notification,
  1327. * suitable for acquiring the change indices as ranges, which is much more
  1328. * compact in memory than getting the individual indices when multiple adjacent
  1329. * elements have been modified.
  1330. *
  1331. * @param out_num_deletion_ranges The number of deleted ranges. May be NULL.
  1332. * @param out_num_insertion_ranges The number of inserted ranges. May be NULL.
  1333. * @param out_num_modification_ranges The number of modified ranges. May be
  1334. * NULL.
  1335. * @param out_num_moves The number of moved elements. May be NULL.
  1336. */
  1337. RLM_API void realm_collection_changes_get_num_ranges(const realm_collection_changes_t*,
  1338. size_t* out_num_deletion_ranges,
  1339. size_t* out_num_insertion_ranges,
  1340. size_t* out_num_modification_ranges, size_t* out_num_moves);
  1341. typedef struct realm_collection_move {
  1342. size_t from;
  1343. size_t to;
  1344. } realm_collection_move_t;
  1345. typedef struct realm_index_range {
  1346. size_t from;
  1347. size_t to;
  1348. } realm_index_range_t;
  1349. /**
  1350. * Get the indices of changes in a collection notification.
  1351. *
  1352. * Note: For moves, every `from` index will also be present among deletions, and
  1353. * every `to` index will also be present among insertions.
  1354. *
  1355. * This function cannot fail.
  1356. *
  1357. * @param out_deletion_indices Where to put the indices of deleted elements
  1358. * (*before* the deletion happened). May be NULL.
  1359. * @param max_deletion_indices The max number of indices to write to @a
  1360. * out_deletion_indices.
  1361. * @param out_insertion_indices Where the put the indices of inserted elements
  1362. * (*after* the insertion happened). May be NULL.
  1363. * @param max_insertion_indices The max number of indices to write to @a
  1364. * out_insertion_indices.
  1365. * @param out_modification_indices Where to put the indices of modified elements
  1366. * (*before* any insertions or deletions of
  1367. * other elements). May be NULL.
  1368. * @param max_modification_indices The max number of indices to write to @a
  1369. * out_modification_indices.
  1370. * @param out_modification_indices_after Where to put the indices of modified
  1371. * elements (*after* any insertions or
  1372. * deletions of other elements). May be
  1373. * NULL.
  1374. * @param max_modification_indices_after The max number of indices to write to
  1375. * @a out_modification_indices_after.
  1376. * @param out_moves Where to put the pairs of indices of moved elements. May be
  1377. * NULL.
  1378. * @param max_moves The max number of pairs to write to @a out_moves.
  1379. */
  1380. RLM_API void realm_collection_changes_get_changes(const realm_collection_changes_t*, size_t* out_deletion_indices,
  1381. size_t max_deletion_indices, size_t* out_insertion_indices,
  1382. size_t max_insertion_indices, size_t* out_modification_indices,
  1383. size_t max_modification_indices,
  1384. size_t* out_modification_indices_after,
  1385. size_t max_modification_indices_after,
  1386. realm_collection_move_t* out_moves, size_t max_moves);
  1387. RLM_API void realm_collection_changes_get_ranges(
  1388. const realm_collection_changes_t*, realm_index_range_t* out_deletion_ranges, size_t max_deletion_ranges,
  1389. realm_index_range_t* out_insertion_ranges, size_t max_insertion_ranges,
  1390. realm_index_range_t* out_modification_ranges, size_t max_modification_ranges,
  1391. realm_index_range_t* out_modification_ranges_after, size_t max_modification_ranges_after,
  1392. realm_collection_move_t* out_moves, size_t max_moves);
  1393. RLM_API realm_set_t* _realm_set_from_native_copy(const void* pset, size_t n);
  1394. RLM_API realm_set_t* _realm_set_from_native_move(void* pset, size_t n);
  1395. RLM_API realm_set_t* realm_get_set(const realm_object_t*, realm_property_key_t);
  1396. RLM_API size_t realm_set_size(const realm_set_t*);
  1397. RLM_API bool realm_set_get(const realm_set_t*, size_t index, realm_value_t* out_value);
  1398. RLM_API bool realm_set_find(const realm_set_t*, realm_value_t value, size_t* out_index);
  1399. RLM_API bool realm_set_insert(realm_set_t*, realm_value_t value, size_t out_index);
  1400. RLM_API bool realm_set_erase(realm_set_t*, realm_value_t value, bool* out_erased);
  1401. RLM_API bool realm_set_clear(realm_set_t*);
  1402. RLM_API bool realm_set_assign(realm_set_t*, realm_value_t values, size_t num_values);
  1403. RLM_API realm_notification_token_t* realm_set_add_notification_callback(realm_object_t*, void* userdata,
  1404. realm_free_userdata_func_t free,
  1405. realm_on_collection_change_func_t on_change,
  1406. realm_callback_error_func_t on_error,
  1407. realm_scheduler_t*);
  1408. RLM_API realm_dictionary_t* _realm_dictionary_from_native_copy(const void* pdict, size_t n);
  1409. RLM_API realm_dictionary_t* _realm_dictionary_from_native_move(void* pdict, size_t n);
  1410. RLM_API realm_dictionary_t* realm_get_dictionary(const realm_object_t*, realm_property_key_t);
  1411. RLM_API size_t realm_dictionary_size(const realm_dictionary_t*);
  1412. RLM_API bool realm_dictionary_get(const realm_dictionary_t*, realm_value_t key, realm_value_t* out_value,
  1413. bool* out_found);
  1414. RLM_API bool realm_dictionary_insert(realm_dictionary_t*, realm_value_t key, realm_value_t value, bool* out_inserted,
  1415. size_t* out_index);
  1416. RLM_API bool realm_dictionary_erase(realm_dictionary_t*, realm_value_t key, bool* out_erased);
  1417. RLM_API bool realm_dictionary_clear(realm_dictionary_t*);
  1418. typedef realm_value_t realm_key_value_pair_t[2];
  1419. RLM_API bool realm_dictionary_assign(realm_dictionary_t*, const realm_key_value_pair_t* pairs, size_t num_pairs);
  1420. RLM_API realm_notification_token_t*
  1421. realm_dictionary_add_notification_callback(realm_object_t*, void* userdata, realm_free_userdata_func_t free,
  1422. realm_on_collection_change_func_t on_change,
  1423. realm_callback_error_func_t on_error, realm_scheduler_t*);
  1424. /**
  1425. * Parse a query string and bind it to a table.
  1426. *
  1427. * If the query failed to parse, the parser error is available from
  1428. * `realm_get_last_error()`.
  1429. *
  1430. * @param target_table The table on which to run this query.
  1431. * @param query_string A zero-terminated string in the Realm Query Language,
  1432. * optionally containing argument placeholders (`$0`, `$1`,
  1433. * etc.).
  1434. * @param num_args The number of arguments for this query.
  1435. * @param args A pointer to a list of argument values.
  1436. * @return A non-null pointer if the query was successfully parsed and no
  1437. * exception occurred.
  1438. */
  1439. RLM_API realm_query_t* realm_query_parse(const realm_t*, realm_class_key_t target_table, const char* query_string,
  1440. size_t num_args, const realm_value_t* args);
  1441. /**
  1442. * Parse a query string and bind it to a list.
  1443. *
  1444. * If the query failed to parse, the parser error is available from
  1445. * `realm_get_last_error()`.
  1446. *
  1447. * @param target_list The list on which to run this query.
  1448. * @param query_string A string in the Realm Query Language, optionally
  1449. * containing argument placeholders (`$0`, `$1`, etc.).
  1450. * @param num_args The number of arguments for this query.
  1451. * @param args A pointer to a list of argument values.
  1452. * @return A non-null pointer if the query was successfully parsed and no
  1453. * exception occurred.
  1454. */
  1455. RLM_API realm_query_t* realm_query_parse_for_list(const realm_list_t* target_list, const char* query_string,
  1456. size_t num_args, const realm_value_t* args);
  1457. /**
  1458. * Parse a query string and bind it to another query result.
  1459. *
  1460. * If the query failed to parse, the parser error is available from
  1461. * `realm_get_last_error()`.
  1462. *
  1463. * @param target_results The results on which to run this query.
  1464. * @param query_string A zero-terminated string in the Realm Query Language,
  1465. * optionally containing argument placeholders (`$0`, `$1`,
  1466. * etc.).
  1467. * @param num_args The number of arguments for this query.
  1468. * @param args A pointer to a list of argument values.
  1469. * @return A non-null pointer if the query was successfully parsed and no
  1470. * exception occurred.
  1471. */
  1472. RLM_API realm_query_t* realm_query_parse_for_results(const realm_results_t* target_results, const char* query_string,
  1473. size_t num_args, const realm_value_t* args);
  1474. /**
  1475. * Count the number of objects found by this query.
  1476. */
  1477. RLM_API bool realm_query_count(const realm_query_t*, size_t* out_count);
  1478. /**
  1479. * Return the first object matched by this query.
  1480. *
  1481. * Note: This function can only produce objects, not values. Use the
  1482. * `realm_results_t` returned by `realm_query_find_all()` to retrieve
  1483. * values from a list of primitive values.
  1484. *
  1485. * @param out_value Where to write the result, if any object matched the query.
  1486. * May be NULL.
  1487. * @param out_found Where to write whether the object was found. May be NULL.
  1488. * @return True if no exception occurred.
  1489. */
  1490. RLM_API bool realm_query_find_first(realm_query_t*, realm_value_t* out_value, bool* out_found);
  1491. /**
  1492. * Produce a results object for this query.
  1493. *
  1494. * Note: This does not actually run the query until the results are accessed in
  1495. * some way.
  1496. *
  1497. * @return A non-null pointer if no exception occurred.
  1498. */
  1499. RLM_API realm_results_t* realm_query_find_all(realm_query_t*);
  1500. /**
  1501. * Delete all objects matched by a query.
  1502. */
  1503. RLM_API bool realm_query_delete_all(const realm_query_t*);
  1504. /**
  1505. * Count the number of results.
  1506. *
  1507. * If the result is "live" (not a snapshot), this may rerun the query if things
  1508. * have changed.
  1509. *
  1510. * @return True if no exception occurred.
  1511. */
  1512. RLM_API bool realm_results_count(realm_results_t*, size_t* out_count);
  1513. /**
  1514. * Get the matching element at @a index in the results.
  1515. *
  1516. * If the result is "live" (not a snapshot), this may rerun the query if things
  1517. * have changed.
  1518. *
  1519. * Note: The bound returned by `realm_results_count()` for a non-snapshot result
  1520. * is not a reliable way to iterate over elements in the result, because
  1521. * the result will be live-updated if changes are made in each iteration
  1522. * that may change the number of query results or even change the
  1523. * ordering. In other words, this method should probably only be used with
  1524. * snapshot results.
  1525. *
  1526. * @return True if no exception occurred (including out-of-bounds).
  1527. */
  1528. RLM_API bool realm_results_get(realm_results_t*, size_t index, realm_value_t* out_value);
  1529. /**
  1530. * Get the matching object at @a index in the results.
  1531. *
  1532. * If the result is "live" (not a snapshot), this may rerun the query if things
  1533. * have changed.
  1534. *
  1535. * Note: The bound returned by `realm_results_count()` for a non-snapshot result
  1536. * is not a reliable way to iterate over elements in the result, because
  1537. * the result will be live-updated if changes are made in each iteration
  1538. * that may change the number of query results or even change the
  1539. * ordering. In other words, this method should probably only be used with
  1540. * snapshot results.
  1541. *
  1542. * @return An instance of `realm_object_t` if no exception occurred.
  1543. */
  1544. RLM_API realm_object_t* realm_results_get_object(realm_results_t*, size_t index);
  1545. /**
  1546. * Delete all objects in the result.
  1547. *
  1548. * If the result if "live" (not a snapshot), this may rerun the query if things
  1549. * have changed.
  1550. *
  1551. * @return True if no exception occurred.
  1552. */
  1553. RLM_API bool realm_results_delete_all(realm_results_t*);
  1554. /**
  1555. * Return a snapshot of the results that never automatically updates.
  1556. *
  1557. * The returned result is suitable for use with `realm_results_count()` +
  1558. * `realm_results_get()`.
  1559. */
  1560. RLM_API realm_results_t* realm_results_snapshot(const realm_results_t*);
  1561. /**
  1562. * Map the results into a frozen realm instance.
  1563. */
  1564. RLM_API realm_results_t* realm_results_freeze(const realm_results_t*, const realm_t* frozen_realm);
  1565. /**
  1566. * Compute the minimum value of a property in the results.
  1567. *
  1568. * @param out_min Where to write the result, if there were matching rows.
  1569. * @param out_found Set to true if there are matching rows.
  1570. * @return True if no exception occurred.
  1571. */
  1572. RLM_API bool realm_results_min(realm_results_t*, realm_property_key_t, realm_value_t* out_min, bool* out_found);
  1573. /**
  1574. * Compute the maximum value of a property in the results.
  1575. *
  1576. * @param out_max Where to write the result, if there were matching rows.
  1577. * @param out_found Set to true if there are matching rows.
  1578. * @return True if no exception occurred.
  1579. */
  1580. RLM_API bool realm_results_max(realm_results_t*, realm_property_key_t, realm_value_t* out_max, bool* out_found);
  1581. /**
  1582. * Compute the sum value of a property in the results.
  1583. *
  1584. * @param out_sum Where to write the result. Zero if no rows matched.
  1585. * @param out_found Set to true if there are matching rows.
  1586. * @return True if no exception occurred.
  1587. */
  1588. RLM_API bool realm_results_sum(realm_results_t*, realm_property_key_t, realm_value_t* out_sum, bool* out_found);
  1589. /**
  1590. * Compute the average value of a property in the results.
  1591. *
  1592. * Note: For numeric columns, the average is always converted to double.
  1593. *
  1594. * @param out_average Where to write the result.
  1595. * @param out_found Set to true if there are matching rows.
  1596. * @return True if no exception occurred.
  1597. */
  1598. RLM_API bool realm_results_average(realm_results_t*, realm_property_key_t, realm_value_t* out_average,
  1599. bool* out_found);
  1600. RLM_API realm_notification_token_t* realm_results_add_notification_callback(realm_results_t*, void* userdata,
  1601. realm_free_userdata_func_t,
  1602. realm_on_collection_change_func_t,
  1603. realm_callback_error_func_t,
  1604. realm_scheduler_t*);
  1605. /**
  1606. * Get an results object from a thread-safe reference, potentially originating
  1607. * in a different `realm_t` instance
  1608. */
  1609. RLM_API realm_results_t* realm_results_from_thread_safe_reference(const realm_t*, realm_thread_safe_reference_t*);
  1610. #endif // REALM_H