-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmgclient.h
More file actions
1475 lines (1261 loc) · 59.8 KB
/
Copy pathmgclient.h
File metadata and controls
1475 lines (1261 loc) · 59.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2016-2020 Memgraph Ltd. [https://memgraph.com]
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef MGCLIENT_MGCLIENT_H
#define MGCLIENT_MGCLIENT_H
#ifdef __EMSCRIPTEN__
#define MGCLIENT_EXPORT EMSCRIPTEN_KEEPALIVE
#include "emscripten.h"
#else
#include "mgclient-export.h"
#endif
#ifdef __cplusplus
extern "C" {
#endif
/// \file mgclient.h
///
/// Provides \ref mg_session, a data type representing a connection to Bolt
/// server, along with functions for connecting to Bolt database and
/// executing queries against it, and \ref mg_value, a data type representing a
/// value in Bolt protocol along with supporting types and manipulation
/// functions for them.
///
/// \ref mg_session is an opaque data type representing a connection to Bolt
/// server. Commands can be submitted for execution using \ref mg_session_run
/// and results can be obtained using \ref mg_session_pull. A \ref mg_session
/// can execute at most one command at a time, and all results should be
/// consumed before trying to execute the next query.
///
/// The usual flow for execution of a single query would be the following:
///
/// 1. Submit the command for execution using \ref mg_session_run.
///
/// 2. Call \ref mg_session_pull until it returns 0 to consume result rows and
/// access result values using \ref mg_result_row.
///
/// 3. If necessary, access command execution summary using \ref
/// mg_result_summary.
///
/// If any of the functions returns an error exit code, more detailed error
/// message can be obtained by calling \ref mg_session_error.
///
/// \ref mg_value is an opaque data type representing an arbitrary value of any
/// of the types specified by the Bolt protocol. It can encapsulate any of its
/// supporting types: \ref mg_string, \ref mg_list, \ref mg_map, \ref mg_node,
/// \ref mg_relationship, \ref mg_unbound_relationship and \ref mg_path.
/// Provided along with them are basic manipulation functions for those data
/// types. The API for most of data types is quite rudimentary, and as such is
/// not usable for complex operations on strings, maps, lists, etc. It is only
/// supposed to be used to construct data to be sent to the Bolt server, and
/// read data obtained from the Bolt server.
///
/// Each object has a corresponding \c mg_*_destroy function that should be
/// invoked on the object at the end of its lifetime to free the resources
/// allocated for its storage. Each object has an owner, that is responsible for
/// its destruction. Object can be owned by the API client or by another
/// object. When being destroyed, an object will also destroy all other
/// objects it owns. Therefore, API client is only responsible for
/// destroying the object it directly owns. For example, if the API client
/// constructed a \ref mg_list value and inserted some other \ref mg_value
/// objects into it, they must only invoke \ref mg_list_destroy on the list and
/// all of its members will be properly destroyed, because the list owns all of
/// its elements. Invoking \c mg_*_destroy on objects that are not owned by the
/// caller will usually result in memory corruption, double freeing, nuclear
/// apocalypse and similar unwanted behaviors. Therefore, object ownership
/// should be tracked carefully.
///
/// Invoking certain functions on objects might cause ownership changes.
/// Obviously, you shouldn't pass objects you don't own to functions that steal
/// ownership.
///
/// Function signatures are of big help in ownership tracking. Now follow two
/// simple rules, all functions that do not conform to those rules (if any) will
/// explicitly specify that in their documentation.
///
/// 1. Return values
///
/// Functions that return a non-const pointer to an object give
/// ownership of the returned object to the caller. Examples are:
/// - creation functions (e.g. \ref mg_list_make_empty).
/// - copy functions (e.g. \ref mg_value_copy).
/// - \ref mg_connect has a `mg_session **` output parameter because the
/// API client becomes the owner of the \ref mg_session object
///
/// Functions that return a const pointer to a object provide
/// read-only access to the returned object that is valid only while the
/// owning object is alive. Examples are:
/// - access functions on `mg_value` (e.g. \ref mg_value_list).
/// - member access functions on containers (e.g. \ref mg_map_key_at,
/// \ref mg_list_at, \ref mg_map_at).
/// - field access functions on graph types (e.g. \ref
/// mg_node_properties).
/// - \ref mg_session_pull has a `const mg_result **` output parameter,
/// because the \ref mg_session object keeps ownership of the returned
/// result and destroys it on next pull
///
/// 2. Function arguments
///
/// Functions that take a non-const pointer to a object either modify
/// it or change its ownership (it is usually obvious what happens).
/// Examples are:
/// - member insert functions on containers transfer the ownership of
/// inserted values to the container. They also take a non-const pointer
/// to the container because they modify it. Ownership of the container
/// is not changed (e.g. \ref mg_map_insert takes ownership of the
/// passed key and value).
/// - \ref mg_session_run takes a non-const pointer to the session because
/// it modifies it internal state, but there is no ownership change
///
/// An obvious exception here are \c mg_*_destroy functions which do not
/// change ownership of the object.
///
/// Functions that take a const pointer to a object do not change the
/// owner of the passed object nor they modify it. Examples are:
/// - member access functions on containers take const pointer to the
/// container (e.g. \ref mg_list_at, \ref mg_map_at, ...).
/// - member access functions on graph types take const pointer to the
/// container (e.g. \ref mg_path_node_at, \ref mg_node_label_count,
/// ...).
/// - copy functions.
#include <stdint.h>
/// Client software version.
///
/// \return Client version in the major.minor.patch format.
MGCLIENT_EXPORT const char *mg_client_version(void);
/// Initializes the client (the whole process).
/// Should be called at the beginning of each process using the client.
///
/// \return Zero if initialization was successful.
MGCLIENT_EXPORT int mg_init(void);
/// Finalizes the client (the whole process).
/// Should be called at the end of each process using the client.
MGCLIENT_EXPORT void mg_finalize(void);
/// An enum listing all the types as specified by Bolt protocol.
enum mg_value_type {
MG_VALUE_TYPE_NULL,
MG_VALUE_TYPE_BOOL,
MG_VALUE_TYPE_INTEGER,
MG_VALUE_TYPE_FLOAT,
MG_VALUE_TYPE_STRING,
MG_VALUE_TYPE_LIST,
MG_VALUE_TYPE_MAP,
MG_VALUE_TYPE_NODE,
MG_VALUE_TYPE_RELATIONSHIP,
MG_VALUE_TYPE_UNBOUND_RELATIONSHIP,
MG_VALUE_TYPE_PATH,
MG_VALUE_TYPE_DATE,
MG_VALUE_TYPE_TIME,
MG_VALUE_TYPE_LOCAL_TIME,
MG_VALUE_TYPE_DATE_TIME,
MG_VALUE_TYPE_DATE_TIME_ZONE_ID,
MG_VALUE_TYPE_LOCAL_DATE_TIME,
MG_VALUE_TYPE_DURATION,
MG_VALUE_TYPE_POINT_2D,
MG_VALUE_TYPE_POINT_3D,
MG_VALUE_TYPE_UNKNOWN
};
/// A Bolt value, encapsulating all other values.
typedef struct mg_value mg_value;
/// An UTF-8 encoded string.
///
/// Note that the length of the string is the byte count of the UTF-8 encoded
/// data. It is guaranteed that the bytes of the string are stored contiguously,
/// and they can be accessed through a pointer to first element returned by
/// \ref mg_string_data.
///
/// Note that the library doesn't perform any checks whatsoever to see if the
/// provided data is a valid UTF-8 encoded string when constructing instances of
/// \ref mg_string.
///
/// Maximum possible string length allowed by Bolt protocol is \c UINT32_MAX.
typedef struct mg_string mg_string;
/// An ordered sequence of values.
///
/// List may contain a mixture of different types as its elements. A list owns
/// all values stored in it.
///
/// Maximum possible list length allowed by Bolt is \c UINT32_MAX.
typedef struct mg_list mg_list;
/// Sized sequence of pairs of keys and values.
///
/// Map may contain a mixture of different types as values. A map owns all keys
/// and values stored in it.
///
/// Maximum possible map size allowed by Bolt protocol is \c UINT32_MAX.
typedef struct mg_map mg_map;
/// Represents a node from a labeled property graph.
///
/// Consists of a unique identifier (withing the scope of its origin graph), a
/// list of labels and a map of properties. A node owns its labels and
/// properties.
///
/// Maximum possible number of labels allowed by Bolt protocol is \c UINT32_MAX.
typedef struct mg_node mg_node;
/// Represents a relationship from a labeled property graph.
///
/// Consists of a unique identifier (within the scope of its origin graph),
/// identifiers for the start and end nodes of that relationship, a type and a
/// map of properties. A relationship owns its type string and property map.
typedef struct mg_relationship mg_relationship;
/// Represents a relationship from a labeled property graph.
///
/// Like \ref mg_relationship, but without identifiers for start and end nodes.
/// Mainly used as a supporting type for \ref mg_path. An unbound relationship
/// owns its type string and property map.
typedef struct mg_unbound_relationship mg_unbound_relationship;
/// Represents a sequence of alternating nodes and relationships
/// corresponding to a walk in a labeled property graph.
///
/// A path of length L consists of L + 1 nodes indexed from 0 to L, and L
/// unbound relationships, indexed from 0 to L - 1. Each relationship has a
/// direction. A relationship is said to be reversed if it was traversed in the
/// direction opposite of the direction of the underlying relationship in the
/// data graph.
typedef struct mg_path mg_path;
/// \brief Represents a date.
///
/// Date is defined with number of days since the Unix epoch.
typedef struct mg_date mg_date;
/// \brief Represents time with its time zone.
///
/// Time is defined with nanoseconds since midnight.
/// Timezone is defined with seconds from UTC.
typedef struct mg_time mg_time;
/// \brief Represents local time.
///
/// Time is defined with nanoseconds since midnight.
typedef struct mg_local_time mg_local_time;
/// \brief Represents date and time with its time zone.
///
/// Date is defined with seconds since the adjusted Unix epoch.
/// Time is defined with nanoseconds since midnight.
/// Time zone is defined with minutes from UTC.
typedef struct mg_date_time mg_date_time;
/// \brief Represents date and time with its time zone.
///
/// Date is defined with seconds since the adjusted Unix epoch.
/// Time is defined with nanoseconds since midnight.
/// Timezone is defined with an identifier for a specific time zone.
typedef struct mg_date_time_zone_id mg_date_time_zone_id;
/// \brief Represents date and time without its time zone.
///
/// Date is defined with seconds since the Unix epoch.
/// Time is defined with nanoseconds since midnight.
typedef struct mg_local_date_time mg_local_date_time;
/// \brief Represents a temporal amount which captures the difference in time
/// between two instants.
///
/// Duration is defined with months, days, seconds, and nanoseconds.
/// \note
/// Duration can be negative.
typedef struct mg_duration mg_duration;
/// \brief Represents a single location in 2-dimensional space.
///
/// Contains SRID along with its x and y coordinates.
typedef struct mg_point_2d mg_point_2d;
/// \brief Represents a single location in 3-dimensional space.
///
/// Contains SRID along with its x, y and z coordinates.
typedef struct mg_point_3d mg_point_3d;
/// Constructs a nil \ref mg_value.
///
/// \return Pointer to the newly constructed value or NULL if error occurred.
MGCLIENT_EXPORT mg_value *mg_value_make_null(void);
/// Constructs a boolean \ref mg_value.
///
/// \param val If the parameter is zero, constructed value will be false.
/// Otherwise, it will be true.
/// \return Pointer to the newly constructed value or NULL if error occurred.
MGCLIENT_EXPORT mg_value *mg_value_make_bool(int val);
/// Constructs an integer \ref mg_value with the given underlying value.
///
/// \return Pointer to the newly constructed value or NULL if error occurred.
MGCLIENT_EXPORT mg_value *mg_value_make_integer(int64_t val);
/// Constructs a float \ref mg_value with the given underlying value.
///
/// \return Pointer to the newly constructed value or NULL if error occurred.
MGCLIENT_EXPORT mg_value *mg_value_make_float(double val);
/// Constructs a string \ref mg_value given a null-terminated string.
///
/// A new \ref mg_string instance will be created from the null-terminated
/// string as the underlying value.
///
/// \param str A null-terminated UTF-8 string.
///
/// \return Pointer to the newly constructed value or NULL if error occurred.
MGCLIENT_EXPORT mg_value *mg_value_make_string(const char *str);
/// Construct a string \ref mg_value given the underlying \ref mg_string.
///
/// \return Pointer to the newly constructed value or NULL if error occurred.
MGCLIENT_EXPORT mg_value *mg_value_make_string2(mg_string *str);
/// Constructs a list \ref mg_value given the underlying \ref mg_list.
///
/// \return Pointer to the newly constructed value or NULL if error occurred.
MGCLIENT_EXPORT mg_value *mg_value_make_list(mg_list *list);
/// Constructs a map \ref mg_value given the underlying \ref mg_map.
///
/// \return Pointer to the newly constructed value or NULL if error occurred.
MGCLIENT_EXPORT mg_value *mg_value_make_map(mg_map *map);
/// Constructs a node \ref mg_value given the underlying \ref mg_node.
///
/// \return Pointer to the newly constructed value or NULL if error occurred.
MGCLIENT_EXPORT mg_value *mg_value_make_node(mg_node *node);
/// Constructs a relationship \ref mg_value given the underlying
/// \ref mg_relationship.
///
/// \return Pointer to the newly constructed value or NULL if error occurred.
MGCLIENT_EXPORT mg_value *mg_value_make_relationship(mg_relationship *rel);
/// Constructs an unbound relationship \ref mg_value given the underlying
/// \ref mg_unbound_relationship.
///
/// \return Pointer to the newly constructed value or NULL if error occurred.
MGCLIENT_EXPORT mg_value *mg_value_make_unbound_relationship(
mg_unbound_relationship *rel);
/// Constructs a path \ref mg_value given the underlying \ref mg_path.
///
/// \return Pointer to the newly constructed value or NULL if error occurred.
MGCLIENT_EXPORT mg_value *mg_value_make_path(mg_path *path);
/// Constructs a date \ref mg_value given the underlying \ref mg_date.
///
/// \return Pointer to the newly constructed value or NULL if error occurred.
MGCLIENT_EXPORT mg_value *mg_value_make_date(mg_date *date);
/// Constructs a time \ref mg_value given the underlying \ref mg_time.
///
/// \return Pointer to the newly constructed value or NULL if error occurred.
MGCLIENT_EXPORT mg_value *mg_value_make_time(mg_time *time);
/// Constructs a local time \ref mg_value given the underlying \ref
/// mg_local_time.
///
/// \return Pointer to the newly constructed value or NULL if error occurred.
MGCLIENT_EXPORT mg_value *mg_value_make_local_time(mg_local_time *local_time);
/// Constructs a date and time \ref mg_value given the underlying \ref
/// mg_date_time.
///
/// \return Pointer to the newly constructed value or NULL if error occurred.
MGCLIENT_EXPORT mg_value *mg_value_make_date_time(mg_date_time *date_time);
/// Constructs a date and time \ref mg_value given the underlying \ref
/// mg_date_time_zone_id.
///
/// \return Pointer to the newly constructed value or NULL if error occurred.
MGCLIENT_EXPORT mg_value *mg_value_make_date_time_zone_id(
mg_date_time_zone_id *date_time_zone_id);
/// Constructs a local date and time \ref mg_value given the underlying \ref
/// mg_local_date_time.
///
/// \return Pointer to the newly constructed value or NULL if error occurred.
MGCLIENT_EXPORT mg_value *mg_value_make_local_date_time(
mg_local_date_time *local_date_time);
/// Constructs a duration \ref mg_value given the underlying \ref mg_duration.
///
/// \return Pointer to the newly constructed value or NULL if error occurred.
MGCLIENT_EXPORT mg_value *mg_value_make_duration(mg_duration *duration);
/// Constructs a 2D point \ref mg_value given the underlying \ref mg_point_2d.
///
/// \return Pointer to the newly constructed value or NULL if error occurred.
MGCLIENT_EXPORT mg_value *mg_value_make_point_2d(mg_point_2d *point_2d);
/// Constructs a 3D point \ref mg_value given the underlying \ref mg_point_3d.
///
/// \return Pointer to the newly constructed value or NULL if error occurred.
MGCLIENT_EXPORT mg_value *mg_value_make_point_3d(mg_point_3d *point_3d);
/// Returns the type of the given \ref mg_value.
MGCLIENT_EXPORT enum mg_value_type mg_value_get_type(const mg_value *val);
/// Returns non-zero value if value contains true, zero otherwise.
///
/// Type check should be made first. Accessing the wrong value results in
/// undefined behavior.
MGCLIENT_EXPORT int mg_value_bool(const mg_value *val);
/// Returns the underlying integer value.
///
/// Type check should be made first. Accessing the wrong value results in
/// undefined behavior.
MGCLIENT_EXPORT int64_t mg_value_integer(const mg_value *val);
/// Returns the underlying float value.
///
/// Type check should be made first. Accessing the wrong value results in
/// undefined behavior.
MGCLIENT_EXPORT double mg_value_float(const mg_value *val);
/// Returns the underlying \ref mg_string value.
///
/// Type check should be made first. Accessing the wrong value results in
/// undefined behavior.
MGCLIENT_EXPORT const mg_string *mg_value_string(const mg_value *val);
/// Returns the underlying \ref mg_list value.
///
/// Type check should be made first. Accessing the wrong value results in
/// undefined behavior.
MGCLIENT_EXPORT const mg_list *mg_value_list(const mg_value *val);
/// Returns the underlying \ref mg_map value.
///
/// Type check should be made first. Accessing the wrong value results in
/// undefined behavior.
MGCLIENT_EXPORT const mg_map *mg_value_map(const mg_value *val);
/// Returns the underlying \ref mg_node value.
///
/// Type check should be made first. Accessing the wrong value results in
/// undefined behavior.
MGCLIENT_EXPORT const mg_node *mg_value_node(const mg_value *val);
/// Returns the underlying \ref mg_relationship value.
///
/// Type check should be made first. Accessing the wrong value results in
/// undefined behavior.
MGCLIENT_EXPORT const mg_relationship *mg_value_relationship(
const mg_value *val);
/// Returns the underlying \ref mg_unbound_relationship value.
///
/// Type check should be made first. Accessing the wrong value results in
/// undefined behavior.
MGCLIENT_EXPORT const mg_unbound_relationship *mg_value_unbound_relationship(
const mg_value *val);
/// Returns the underlying \ref mg_path value.
///
/// Type check should be made first. Accessing the wrong value results in
/// undefined behavior.
MGCLIENT_EXPORT const mg_path *mg_value_path(const mg_value *val);
/// Returns the underlying \ref mg_date value.
///
/// Type check should be made first. Accessing the wrong value results in
/// undefined behavior.
MGCLIENT_EXPORT const mg_date *mg_value_date(const mg_value *val);
/// Returns the underlying \ref mg_time value.
///
/// Type check should be made first. Accessing the wrong value results in
/// undefined behavior.
MGCLIENT_EXPORT const mg_time *mg_value_time(const mg_value *val);
/// Returns the underlying \ref mg_local_time value.
///
/// Type check should be made first. Accessing the wrong value results in
/// undefined behavior.
MGCLIENT_EXPORT const mg_local_time *mg_value_local_time(const mg_value *val);
/// Returns the underlying \ref mg_date_time value.
///
/// Type check should be made first. Accessing the wrong value results in
/// undefined behavior.
MGCLIENT_EXPORT const mg_date_time *mg_value_date_time(const mg_value *val);
/// Returns the underlying \ref mg_date_time_zone_id value.
///
/// Type check should be made first. Accessing the wrong value results in
/// undefined behavior.
MGCLIENT_EXPORT const mg_date_time_zone_id *mg_value_date_time_zone_id(
const mg_value *val);
/// Returns the underlying \ref mg_local_date_time value.
///
/// Type check should be made first. Accessing the wrong value results in
/// undefined behavior.
MGCLIENT_EXPORT const mg_local_date_time *mg_value_local_date_time(
const mg_value *val);
/// Returns the underlying \ref mg_duration value.
///
/// Type check should be made first. Accessing the wrong value results in
/// undefined behavior.
MGCLIENT_EXPORT const mg_duration *mg_value_duration(const mg_value *val);
/// Returns the underlying \ref mg_point_2d value.
///
/// Type check should be made first. Accessing the wrong value results in
/// undefined behavior.
MGCLIENT_EXPORT const mg_point_2d *mg_value_point_2d(const mg_value *val);
/// Returns the underlying \ref mg_point_3d value.
///
/// Type check should be made first. Accessing the wrong value results in
/// undefined behavior.
MGCLIENT_EXPORT const mg_point_3d *mg_value_point_3d(const mg_value *val);
/// Creates a copy of the given value.
///
/// \return Pointer to the copy or NULL if error occurred.
MGCLIENT_EXPORT mg_value *mg_value_copy(const mg_value *val);
/// Destroys the given value.
MGCLIENT_EXPORT void mg_value_destroy(mg_value *val);
/// Constructs a string given a null-terminated string.
///
/// A new buffer of appropriate length will be allocated and the given string
/// will be copied there.
///
/// \param str A null-terminated UTF-8 string.
///
/// \return A pointer to the newly constructed `mg_string` object or \c NULL
/// if an error occurred.
MGCLIENT_EXPORT mg_string *mg_string_make(const char *str);
/// Constructs a string given its length (in bytes) and contents.
///
/// A new buffer of will be allocated and the given data will be copied there.
///
/// \param len Number of bytes in the data buffer.
/// \param data The string contents.
///
/// \return A pointer to the newly constructed `mg_string` object or \c NULL
/// if an error occurred.
MGCLIENT_EXPORT mg_string *mg_string_make2(uint32_t len, const char *data);
/// Returns a pointer to the beginning of data buffer of string \p str.
MGCLIENT_EXPORT const char *mg_string_data(const mg_string *str);
/// Returns the length (in bytes) of string \p str.
MGCLIENT_EXPORT uint32_t mg_string_size(const mg_string *str);
/// Creates a copy of the given string.
///
/// \return A pointer to the copy or NULL if an error occurred.
MGCLIENT_EXPORT mg_string *mg_string_copy(const mg_string *str);
/// Destroys the given string.
MGCLIENT_EXPORT void mg_string_destroy(mg_string *str);
/// Constructs a list that can hold at most \p capacity elements.
///
/// Elements should be constructed and then inserted using \ref mg_list_append.
///
/// \param capacity The maximum number of elements that the newly constructed
/// list can hold.
///
/// \return A pointer to the newly constructed empty list or NULL if an error
/// occurred.
MGCLIENT_EXPORT mg_list *mg_list_make_empty(uint32_t capacity);
/// Appends an element at the end of the list \p list.
///
/// Insertion will fail if the list capacity is already exhausted. If the
/// insertion fails, the map doesn't take ownership of \p value.
///
/// \param list The list instance to be modified.
/// \param value The value to be appended.
///
/// \return The function returns non-zero value if insertion failed, zero
/// otherwise.
MGCLIENT_EXPORT int mg_list_append(mg_list *list, mg_value *value);
/// Returns the number of elements in list \p list.
MGCLIENT_EXPORT uint32_t mg_list_size(const mg_list *list);
/// Retrieves the element at position \p pos in list \p list.
///
/// \return A pointer to required list element. If \p pos is outside of list
/// bounds, \c NULL is returned.
MGCLIENT_EXPORT const mg_value *mg_list_at(const mg_list *list, uint32_t pos);
/// Creates a copy of the given list.
///
/// \return A pointer to the copy or NULL if an error occurred.
MGCLIENT_EXPORT mg_list *mg_list_copy(const mg_list *list);
/// Destroys the given list.
MGCLIENT_EXPORT void mg_list_destroy(mg_list *list);
/// Constructs an empty map that can hold at most \p capacity key-value pairs.
///
/// Key-value pairs should be constructed and then inserted using
/// \ref mg_map_insert, \ref mg_map_insert_unsafe and similar.
///
/// \param capacity The maximum number of key-value pairs that the newly
/// constructed list can hold.
///
/// \return A pointer to the newly constructed empty map or NULL if an error
/// occurred.
MGCLIENT_EXPORT mg_map *mg_map_make_empty(uint32_t capacity);
/// Inserts the given key-value pair into the map.
///
/// A check is performed to see if the given key is unique in the map which
/// means that a number of key comparisons equal to the current number of
/// elements in the map is made.
///
/// If key length is greater that \c UINT32_MAX, or the key already exists in
/// map, or the map's capacity is exhausted, the insertion will fail. If
/// insertion fails, the map doesn't take ownership of \p value.
///
/// If the insertion is successful, a new \ref mg_string is constructed for
/// the storage of the key and the map takes ownership of \p value.
///
/// \param map The map instance to be modifed.
/// \param key_str A null-terminated string to be used as key.
/// \param value Value to be inserted.
///
/// \return The function returns non-zero value if insertion failed, zero
/// otherwise.
MGCLIENT_EXPORT int mg_map_insert(mg_map *map, const char *key_str,
mg_value *value);
/// Inserts the given key-value pair into the map.
///
/// A check is performed to see if the given key is unique in the map which
/// means that a number of key comparisons equal to the current number of
/// elements in the map is made.
///
/// If the key already exists in map, or the map's capacity is exhausted, the
/// insertion will fail. If insertion fails, the map doesn't take ownership of
/// \p key and \p value.
///
/// If the insertion is successful, map takes ownership of \p key and \p value.
///
/// \param map The map instance to be modifed.
/// \param key A \ref mg_string to be used as key.
/// \param value Value to be inserted.
///
/// \return The function returns non-zero value if insertion failed, zero
/// otherwise.
MGCLIENT_EXPORT int mg_map_insert2(mg_map *map, mg_string *key,
mg_value *value);
/// Inserts the given key-value pair into the map.
///
/// No check is performed for key uniqueness. Note that map containing duplicate
/// keys is considered invalid in Bolt protocol.
///
/// If key length is greated than \c UINT32_MAX or or the map's capacity is
/// exhausted, the insertion will fail. If insertion fails, the map doesn't take
/// ownership of \p value.
///
/// If the insertion is successful, a new \ref mg_string is constructed for the
/// storage of the key and the map takes ownership of \p value.
///
/// \param map The map instance to be modifed.
/// \param key_str A null-terminated string to be used as key.
/// \param value Value to be inserted.
///
/// \return The function returns non-zero value if insertion failed, zero
/// otherwise.
MGCLIENT_EXPORT int mg_map_insert_unsafe(mg_map *map, const char *key_str,
mg_value *value);
/// Inserts the given key-value pair into the map.
///
/// No check is performed for key uniqueness. Note that map containing duplicate
/// keys is considered invalid in Bolt protocol.
///
/// If the map's capacity is exhausted, the insertion will fail. If insertion
/// fails, the map doesn't take ownership of \p key and \p value.
///
/// If the insertion is successful, map takes ownership of \p key and \p value.
///
/// \param map The map instance to be modifed.
/// \param key A \ref mg_string to be used as key.
/// \param value Value to be inserted.
///
/// \return The function returns non-zero value if insertion failed, zero
/// otherwise.
MGCLIENT_EXPORT int mg_map_insert_unsafe2(mg_map *map, mg_string *key,
mg_value *value);
/// Looks up a map value with the given key.
///
/// \param map The map instance to be queried.
/// \param key_str A null-terminated string representing the key to be looked-up
/// in the map.
///
/// \return If the key is found in the map, the pointer to the corresponding
/// \ref mg_value is returned. Otherwise, \c NULL is returned.
MGCLIENT_EXPORT const mg_value *mg_map_at(const mg_map *map,
const char *key_str);
/// Looks up a map value with the given key.
///
/// \param map The map instance to be queried.
/// \param key_size The length of the string representing the key to be
/// looked-up in the map.
/// \param key_data Bytes constituting the key string.
///
/// \return If the key is found in the map, the pointer to the corresponding
/// \ref mg_value is returned. Otherwise, \c NULL is returned.
MGCLIENT_EXPORT const mg_value *mg_map_at2(const mg_map *map, uint32_t key_size,
const char *key_data);
/// Returns the number of key-value pairs in map \p map.
MGCLIENT_EXPORT uint32_t mg_map_size(const mg_map *map);
/// Retrieves the key at position \p pos in map \p map.
///
/// \return A pointer to required key. If \p pos is outside of map bounds, \c
/// NULL is returned.
MGCLIENT_EXPORT const mg_string *mg_map_key_at(const mg_map *, uint32_t pos);
/// Retrieves the value at position \p pos in map \p map.
///
/// \return A pointer to required value. If \p pos is outside of map bounds,
/// \c NULL is returned.
MGCLIENT_EXPORT const mg_value *mg_map_value_at(const mg_map *, uint32_t pos);
/// Creates a copy of the given map.
///
/// \return A pointer to the copy or NULL if an error occurred.
MGCLIENT_EXPORT mg_map *mg_map_copy(const mg_map *map);
/// Destroys the given map.
MGCLIENT_EXPORT void mg_map_destroy(mg_map *map);
/// Returns the ID of node \p node.
MGCLIENT_EXPORT int64_t mg_node_id(const mg_node *node);
/// Returns the number of labels of node \p node.
MGCLIENT_EXPORT uint32_t mg_node_label_count(const mg_node *node);
/// Returns the label at position \p pos in node \p node's label list.
///
/// \return A pointer to the required label. If \p pos is outside of label list
/// bounds, \c NULL is returned.
MGCLIENT_EXPORT const mg_string *mg_node_label_at(const mg_node *node,
uint32_t pos);
/// Returns property map of node \p node.
MGCLIENT_EXPORT const mg_map *mg_node_properties(const mg_node *node);
/// Creates a copy of the given node.
///
/// \return A pointer to the copy or NULL if an error occurred.
MGCLIENT_EXPORT mg_node *mg_node_copy(const mg_node *node);
/// Destroys the given node.
MGCLIENT_EXPORT void mg_node_destroy(mg_node *node);
/// Returns the ID of the relationship \p rel.
MGCLIENT_EXPORT int64_t mg_relationship_id(const mg_relationship *rel);
/// Returns the ID of the start node of relationship \p rel.
MGCLIENT_EXPORT int64_t mg_relationship_start_id(const mg_relationship *rel);
/// Returns the ID of the end node of relationship \p rel.
MGCLIENT_EXPORT int64_t mg_relationship_end_id(const mg_relationship *rel);
/// Returns the type of the relationship \p rel.
MGCLIENT_EXPORT const mg_string *mg_relationship_type(
const mg_relationship *rel);
/// Returns the property map of the relationship \p rel.
MGCLIENT_EXPORT const mg_map *mg_relationship_properties(
const mg_relationship *rel);
/// Creates a copy of the given relationship.
///
/// \return A pointer to the copy or NULL if an error occurred.
MGCLIENT_EXPORT mg_relationship *mg_relationship_copy(
const mg_relationship *rel);
/// Destroys the given relationship.
MGCLIENT_EXPORT void mg_relationship_destroy(mg_relationship *rel);
/// Returns the ID of the unbound relationship \p rel.
MGCLIENT_EXPORT int64_t
mg_unbound_relationship_id(const mg_unbound_relationship *rel);
/// Returns the type of the unbound relationship \p rel.
MGCLIENT_EXPORT const mg_string *mg_unbound_relationship_type(
const mg_unbound_relationship *rel);
/// Returns the property map of the unbound relationship \p rel.
MGCLIENT_EXPORT const mg_map *mg_unbound_relationship_properties(
const mg_unbound_relationship *rel);
/// Creates a copy of the given unbound relationship.
///
/// \return A pointer to the copy or NULL if an error occurred.
MGCLIENT_EXPORT mg_unbound_relationship *mg_unbound_relationship_copy(
const mg_unbound_relationship *rel);
/// Destroys the given unbound relationship.
MGCLIENT_EXPORT void mg_unbound_relationship_destroy(
mg_unbound_relationship *rel);
/// Returns the length (the number of edges) of path \p path.
MGCLIENT_EXPORT uint32_t mg_path_length(const mg_path *path);
/// Returns the node at position \p pos in the traversal of path \p path.
///
/// Nodes are indexed from 0 to path length.
///
/// \return A pointer to the required node. If \p pos is out of path bounds, \c
/// NULL is returned.
MGCLIENT_EXPORT const mg_node *mg_path_node_at(const mg_path *path,
uint32_t pos);
/// Returns the relationship at position \p pos in traversal of path \p path.
///
/// Relationships are indexed from 0 to path length - 1.
///
/// \return A pointer to the required relationship. If \p pos is outside of
/// path bounds, \c NULL is returned.
MGCLIENT_EXPORT const mg_unbound_relationship *mg_path_relationship_at(
const mg_path *path, uint32_t pos);
/// Checks if the relationship at position \p pos in traversal of path \p path
/// is reversed.
///
/// Relationships are indexed from 0 to path length - 1.
///
/// \return Returns 0 if relationships is traversed in the same direction as the
/// underlying relationship in the data graph, and 1 if it is traversed
/// in the opposite direction. If \p pos is outside of path bounds, -1
/// is returned.
MGCLIENT_EXPORT int mg_path_relationship_reversed_at(const mg_path *path,
uint32_t pos);
/// Creates a copy of the given path.
///
/// \return A pointer to the copy or NULL if an error occurred.
MGCLIENT_EXPORT mg_path *mg_path_copy(const mg_path *path);
/// Destroys the given path.
MGCLIENT_EXPORT void mg_path_destroy(mg_path *path);
/// Creates mg_date from days.
/// \return a pointer to mg_date or NULL if an error occurred.
MGCLIENT_EXPORT mg_date *mg_date_make(int64_t days);
/// Returns days since the Unix epoch.
MGCLIENT_EXPORT int64_t mg_date_days(const mg_date *date);
/// Creates a copy of the given date.
///
/// \return A pointer to the copy or NULL if an error occured.
MGCLIENT_EXPORT mg_date *mg_date_copy(const mg_date *date);
/// Destroys the given date.
MGCLIENT_EXPORT void mg_date_destroy(mg_date *date);
/// Returns nanoseconds since midnight.
MGCLIENT_EXPORT int64_t mg_time_nanoseconds(const mg_time *time);
/// Returns time zone offset in seconds from UTC.
MGCLIENT_EXPORT int64_t mg_time_tz_offset_seconds(const mg_time *time);
/// Creates a copy of the given time.
///
/// \return A pointer to the copy or NULL if an error occured.
MGCLIENT_EXPORT mg_time *mg_time_copy(const mg_time *time);
/// Destroys the given time.
MGCLIENT_EXPORT void mg_time_destroy(mg_time *time);
/// Returns nanoseconds since midnight.
MGCLIENT_EXPORT int64_t
mg_local_time_nanoseconds(const mg_local_time *local_time);
/// Creates mg_local_time from nanoseconds.
/// \return a pointer to mg_local_time or NULL if an error occurred.
MGCLIENT_EXPORT mg_local_time *mg_local_time_make(int64_t nanoseconds);
/// Creates a copy of the given local time.
///
/// \return A pointer to the copy or NULL if an error occured.
MGCLIENT_EXPORT mg_local_time *mg_local_time_copy(
const mg_local_time *local_time);
/// Destroys the given local time.
MGCLIENT_EXPORT void mg_local_time_destroy(mg_local_time *local_time);
/// Returns seconds since Unix epoch.
MGCLIENT_EXPORT int64_t mg_date_time_seconds(const mg_date_time *date_time);
/// Returns nanoseconds since midnight.
MGCLIENT_EXPORT int64_t mg_date_time_nanoseconds(const mg_date_time *date_time);
/// Returns time zone offset in minutes from UTC.
MGCLIENT_EXPORT int64_t
mg_date_time_tz_offset_minutes(const mg_date_time *date_time);
/// Creates a copy of the given date and time.
///
/// \return A pointer to the copy or NULL if an error occured.
MGCLIENT_EXPORT mg_date_time *mg_date_time_copy(const mg_date_time *date_time);
/// Destroys the given date and time.
MGCLIENT_EXPORT void mg_date_time_destroy(mg_date_time *date_time);
/// Returns seconds since Unix epoch.
MGCLIENT_EXPORT int64_t
mg_date_time_zone_id_seconds(const mg_date_time_zone_id *date_time_zone_id);
/// Returns nanoseconds since midnight.
MGCLIENT_EXPORT int64_t
mg_date_time_zone_id_nanoseconds(const mg_date_time_zone_id *date_time_zone_id);
/// Returns time zone name.
MGCLIENT_EXPORT const mg_string *mg_date_time_zone_id_timezone_name(
const mg_date_time_zone_id *date_time_zone_id);
/// Creates a copy of the given date and time.
///
/// \return A pointer to the copy or NULL if an error occured.
MGCLIENT_EXPORT mg_date_time_zone_id *mg_date_time_zone_id_copy(
const mg_date_time_zone_id *date_time_zone_id);
/// Destroys the given date and time.
MGCLIENT_EXPORT void mg_date_time_zone_id_destroy(
mg_date_time_zone_id *date_time_zone_id);
/// Creates mg_date_time from seconds, nanoseconds and timezone offset.
/// \return a pointer to mg_date_time or NULL if an error occurred.
MGCLIENT_EXPORT mg_date_time *mg_date_time_make(int64_t seconds,
int64_t nanoseconds,
int32_t tz_offset_minutes);
/// Creates mg_date_time_zone_id from seconds, nanoseconds, and timezone name.
/// \return a pointer to mg_date_time_zone_id or NULL if an error occurred.
MGCLIENT_EXPORT mg_date_time_zone_id *mg_date_time_zone_id_make(
int64_t seconds, int64_t nanoseconds, const char *timezone_name);
/// Creates mg_local_date_time from seconds and nanoseconds.
/// \return a pointer to mg_local_date_time or NULL if an error occurred.
MGCLIENT_EXPORT mg_local_date_time *mg_local_date_time_make(
int64_t seconds, int64_t nanoseconds);
//
/// Returns seconds since Unix epoch. This includes the hours, minutes, seconds
/// fields of the local_time.
MGCLIENT_EXPORT int64_t
mg_local_date_time_seconds(const mg_local_date_time *local_date_time);
/// Returns subseconds of the local_time field as nanoseconds.
MGCLIENT_EXPORT int64_t
mg_local_date_time_nanoseconds(const mg_local_date_time *local_date_time);
/// Creates a copy of the given local date and time.
///
/// \return A pointer to the copy or NULL if an error occured.
MGCLIENT_EXPORT mg_local_date_time *mg_local_date_time_copy(
const mg_local_date_time *local_date_time);
/// Destroy the given local date and time.
MGCLIENT_EXPORT void mg_local_date_time_destroy(
mg_local_date_time *local_date_time);
/// Creates mg_duration from months, days, seconds and nanoseconds.
/// \return a pointer to mg_duration or NULL if an error occurred.
MGCLIENT_EXPORT mg_duration *mg_duration_make(int64_t months, int64_t days,