forked from SoftlySpoken/biggraph_class_project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValue.cpp
1540 lines (1430 loc) · 50.1 KB
/
Value.cpp
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
#include "Value.h"
#include <cassert>
#include <math.h>
#include <set>
#include <sstream>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <chrono>
#include <ctime>
#include <iomanip>
#include <limits.h>
using namespace std;
/**
* @brief Constructs a Value object which is of type NO_VALUE.
* This default constructor will not allocate any memory, and the type of the Value object is NO_VALUE.
* @remark 默认构造函数
*/
GPStore::Value::Value(){
type_ = NO_VALUE;
}
/**
* @brief Constructs a Value object from a pointer to another Value.
*
* If the provided pointer is null, the type of this Value is set to NO_VALUE.
* Otherwise, it initializes this Value using the data from the pointed Value.
*
* @param value_ptr A pointer to a GPStore::Value object. If null, the type is set to NO_VALUE.
*
* @remark 从指向另一个Value的指针构造
*/
GPStore::Value::Value(GPStore::Value *value_ptr) {
if (!value_ptr) {type_ = NO_VALUE;}
else ConstructFrom(*value_ptr);
}
/**
* @brief Constructs a Value object with the given type.
*
* This constructor is used to construct a Value object with a specific type.
* If the type is STRING, PATH, LIST, MAP, DATE_TIME, the object will allocate
* memory for that type. Otherwise, the type is set to NO_VALUE.
*
* @param _type_ The type of the Value object to be constructed.
*
* @remark 根据类型构造
*
*/
GPStore::Value::Value(Type _type_):type_(_type_){
if(_type_ == STRING){
data_.String = new string();
} else if(_type_ == PATH){
data_.Path = new PathContent();
}else if(_type_ == LIST){
data_.List = new vector<Value*>;
}else if(_type_ == MAP){
data_.Map = new map<std::string, Value *>();
}else if(_type_ == DATE_TIME){
data_.Datetime = new DateTime();
}else if(_type_ == NO_VALUE){
data_.Long = 0;
}
}
/**
* @brief Copy constructor for GPStore::Value.
*
* This constructor initializes a new Value object as a copy of another Value object.
* It utilizes the ConstructFrom method to copy the data from the provided Value object.
*
* @param other The Value object to be copied.
* @remark 拷贝构造函数
*/
GPStore::Value::Value(const Value &other){
ConstructFrom(other);
}
/**
* @brief Constructs a Value object from a 32-bit integer.
*
* This constructor is used to construct a Value object from a 32-bit integer.
* The type of this Value object is set to INTEGER.
*
* @param int_ The 32-bit integer value used to construct the Value object.
*
* @remark 从32位整数构造
*/
GPStore::Value::Value(int_32 int_){
type_ = INTEGER;
data_.Int = int_;
}
/**
* @brief Constructs a Value object from a 64-bit integer.
*
* This constructor is used to construct a Value object from a 64-bit integer.
* The type of this Value object is set to LONG.
*
* @param long_ The 64-bit integer value used to construct the Value object.
*
* @remark 从64位整数构造
*/
GPStore::Value::Value(int_64 long_){
type_ = LONG;
data_.Long = long_;
}
/**
* @brief Constructs a Value object from a floating point number.
*
* This constructor is used to construct a Value object from a floating point number.
* The type of this Value object is set to FLOAT.
*
* @param float_ The floating point number used to construct the Value object.
*
* @remark 从浮点数构造
*/
GPStore::Value::Value(double float_):type_(FLOAT){
data_.Float = float_;
}
/**
* @brief Constructs a Value object from a string.
*
* This constructor is used to construct a Value object from a given string.
* The type of this Value object is set to STRING.
*
* @param str The string used to construct the Value object.
*
* @remark 从字符串构造
*/
GPStore::Value::Value(const std::string& str):type_(STRING){
data_.String = new string(str);
}
/**
* @brief Constructs a Value object from a C-style string.
*
* This constructor is used to construct a Value object from a C-style string.
* The type of this Value object is set to STRING.
*
* @param s The C-style string used to construct the Value object.
*
* @remark 从C风格字符串构造
*/
GPStore::Value::Value(const char* s):type_(STRING){
data_.String = new string(s);
}
/**
* @brief Constructs a Value object from a boolean value.
*
* This constructor is used to construct a Value object from a boolean value.
* The type of this Value object is set to BOOLEAN.
*
* @param b The boolean value used to construct the Value object.
*
* @remark 从布尔值构造
*/
GPStore::Value::Value(bool b):type_(BOOLEAN){
data_.Boolean = b;
}
/**
* @brief Constructs a Value object from a type and a 32-bit identifier.
*
* This constructor is used to construct a Value object from a specified type
* and a 32-bit identifier. Depending on the type, the identifier is either
* stored as a Node or an Edge.
*
* @param _type The type of the Value object, which determines how the identifier is stored.
* @param _id The 32-bit identifier used to construct the Value object.
*
* @remark 根据类型和32位标识符构造
*/
GPStore::Value::Value(Type _type, uint_32 _id):type_(_type){
if(_type == NODE)
data_.Node = (uint_32)_id;
else
data_.Edge = (uint_64)_id;
}
/**
* @brief Constructs a Value object from a type and a 64-bit identifier.
*
* This constructor is used to construct a Value object from a specified type
* and a 64-bit identifier. Depending on the type, the identifier is either
* stored as a Node or an Edge.
*
* @param _type The type of the Value object, which determines how the identifier is stored.
* @param _id The 64-bit identifier used to construct the Value object.
*
* @remark 根据类型和64位标识符构造
*/
GPStore::Value::Value(Type _type, uint_64 _id):type_(_type){
if(_type == NODE)
data_.Node = (uint_32)_id;
else
data_.Edge = (uint_64)_id;
}
/**
* @brief Constructs a Value object from a PathContent instance.
*
* This constructor initializes a Value object of type PATH by deep copying
* the node and edge identifiers from the provided PathContent instance.
*
* @param path_ The PathContent instance containing node and edge identifiers
* to be copied into the new Value object.
* @remark 从路径构造
*/
GPStore::Value::Value(const PathContent &path_):type_(PATH){
data_.Path = new PathContent();
data_.Path->node_id_ = path_.node_id_;
data_.Path->edge_id_ = path_.edge_id_;
}
/**
* @brief Constructs a Value object from node and edge identifiers.
*
* This constructor initializes a Value object of type PATH by assigning
* the provided vectors of node and edge identifiers to the internal
* PathContent structure.
*
* @param node_id A vector of unsigned integers representing node identifiers.
* @param edge_id A vector of 64-bit integers representing edge identifiers.
* @remark 从节点和边标识符构造
*/
GPStore::Value::Value(const std::vector<unsigned >& node_id, const std::vector<uint_64> &edge_id):type_(PATH){
data_.Path = new PathContent();
data_.Path->node_id_ = node_id;
data_.Path->edge_id_ = edge_id;
}
/**
* @brief Constructs a Value object from a vector of Value pointers.
*
* This constructor is used to construct a Value object from a vector of Value
* pointers. If the deep_copy parameter is true, the Value pointers in the
* vector are deep copied into the new Value object. Otherwise, only the
* pointers are copied, meaning that the Value pointers in the vector and the
* new Value object point to the same objects.
*
* @param list_ The vector of Value pointers used to construct the new Value
* object.
* @param deep_copy If true, the Value pointers in the vector are deep copied,
* otherwise only the pointers are copied.
*
* @remark 构造列表
*/
GPStore::Value::Value(const std::vector<Value *> &list_, bool deep_copy):type_(LIST){
data_.List = new vector<Value *>();
for(Value *v : list_){
if(deep_copy)
data_.List->push_back(ValueDeepCopy(v));
else
data_.List->push_back(v);
}
}
/**
* @brief Constructs a Value object from a vector of strings and a vector of Value pointers.
*
* This constructor is used to construct a Value object from a vector of strings
* and a vector of Value pointers. If the deep_copy parameter is true, the Value
* pointers in the vector are deep copied into the new Value object. Otherwise,
* only the pointers are copied, meaning that the Value pointers in the vector
* and the new Value object point to the same objects.
*
* @param keys The vector of strings used as keys in the map.
* @param values The vector of Value pointers used as values in the map.
* @param deep_copy If true, the Value pointers in the vector are deep copied,
* otherwise only the pointers are copied.
*
* @remark 构造字典
*/
GPStore::Value::Value(const std::vector<std::string> &keys, const std::vector<Value *> &values, bool deep_copy):type_(MAP) {
data_.Map = new map<std::string, Value *>();
uint_64 n = keys.size();
assert(values.size() == n);
for(uint_64 i = 0; i < n; ++i){
if(deep_copy)
data_.Map->insert(make_pair(keys[i], ValueDeepCopy(values[i])));
else
data_.Map->insert(make_pair(keys[i], values[i]));
}
}
/**
* @brief Constructs a GPStore::Value object with a DateTime parameter.
* Initializes the internal Datetime structure by copying values
* from the provided DateTime object.
*
* @param datetime_ The DateTime object used to initialize the new
* GPStore::Value instance.
* @remark 日期时间构造函数
*/
GPStore::Value::Value(const DateTime &datetime_):type_(DATE_TIME){
data_.Datetime = new DateTime();
data_.Datetime->year = datetime_.year;
data_.Datetime->month = datetime_.month;
data_.Datetime->day = datetime_.day;
data_.Datetime->hour = datetime_.hour;
data_.Datetime->minute = datetime_.minute;
data_.Datetime->second = datetime_.second;
data_.Datetime->millisecond = datetime_.millisecond;
data_.Datetime->microsecond = datetime_.microsecond;
data_.Datetime->nanosecond = datetime_.nanosecond;
data_.Datetime->timezone = datetime_.timezone;
}
/**
* @brief Sets the datetime value by converting the given time parameters.
* The function extracts various time units (year, month, day, hour, etc.)
* from the provided `sumtime` and `nanosecond_` using chrono utilities.
* Adjusts for timezone differences, with specific handling for hour shifts.
*
* @param sumtime The total milliseconds since epoch.
* @param nanosecond_ Additional nanoseconds to be added.
* @remark 设置日期时间
*/
void GPStore::Value::SetDatetime(long long sumtime, long long nanosecond_){
std::chrono::milliseconds timestamp(sumtime);
std::chrono::nanoseconds nanosecondsPart(nanosecond_);
std::time_t timestampSec = std::chrono::duration_cast<std::chrono::seconds>(timestamp).count();
// std::tm* localTime = std::localtime(×tampSec); // ! localtime is not threadsafe: The result is stored in static storage and a pointer to that static storage is returned
// https://stackoverflow.com/questions/38034033/c-localtime-this-function-or-variable-may-be-unsafe
// https://en.cppreference.com/w/c/chrono/localtime
std::tm localTime;
localtime_r(×tampSec, &localTime); // uses user-provided storage buf for the result.
data_.Datetime->year = localTime.tm_year + 1900;
data_.Datetime->month = localTime.tm_mon + 1;
if(localTime.tm_hour >= 8){
data_.Datetime->day = localTime.tm_mday;
data_.Datetime->hour = localTime.tm_hour - 8 ;
}
else{
data_.Datetime->day = localTime.tm_mday-1;
data_.Datetime->hour = localTime.tm_hour + 16 ;
}
data_.Datetime->minute = localTime.tm_min;
data_.Datetime->second = localTime.tm_sec;
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(timestamp);
data_.Datetime->millisecond = duration.count() % 1000;
data_.Datetime->microsecond = nanosecondsPart.count() / 1000;
data_.Datetime->nanosecond = nanosecondsPart.count() % 1000;
}
/**
* @brief Sets the datetime value using another GPStore::Value object.
* The function retrieves the total milliseconds and nanoseconds from
* the `other` object. If `epochMillis` is not found, it falls back
* to using `epochSeconds` and converts it to milliseconds.
*
* @param other The source GPStore::Value object from which datetime
* information is retrieved.
* @remark 从另一个对象设置日期时间
*/
void GPStore::Value::SetDatetime(GPStore::Value &other){
// other is Map
long long sumtime = 0 ,nanosecond_ = 0;
auto iter = other.search("nanosecond");
if(iter != nullptr ) nanosecond_ = iter->toLLong() ;
iter = other.search("epochMillis");
if(iter != nullptr ) sumtime = iter -> toLLong() ;
else sumtime = (other.search("epochSeconds") -> toLLong()) *1000 ;
SetDatetime(sumtime, nanosecond_);
}
std::pair<GPStore::uint_16, GPStore::uint_16> GPStore::Value::getMonthDay()
{
if(type_ != Type::DATE_TIME) return {};
return {data_.Datetime->month, data_.Datetime->day};
}
/**
* @brief Destroys the Value object.
*
* The destructor simply calls Destruct() to free any allocated memory.
*
* @remark 析构
*/
GPStore::Value::~Value(){
Destruct();
}
/**
* @brief Returns true if the Value object is null or represents an invalid entity or edge.
*
* Null values are represented by the special value NO_VALUE.
* Invalid entities and edges are represented by the special value INVALID_ENTITY_LITERAL_ID and INVALID_EDGE_ID respectively.
*
* @return true if the Value object is null or represents an invalid entity or edge.
* @remark 返回值是否为空
*/
bool GPStore::Value::isNull() const{
return type_ == NO_VALUE || (type_ == NODE && data_.Node == UINT_MAX) || (type_ == EDGE && data_.Edge == 0xffffffffffffffff);
}
bool GPStore::Value::isErrorValue() const {
return type_ == ERROR_VALUE;
}
bool GPStore::Value::isNumber() const{
return type_ == FLOAT || type_ == INTEGER || type_ == LONG;
}
bool GPStore::Value::isInteger() const {
return type_ == INTEGER || type_ == LONG;
}
bool GPStore::Value::storable() const{
return type_ == INTEGER || type_ == FLOAT || type_ == STRING || type_ == BOOLEAN || type_ == LONG ||
isIntArray() || isLongArray() || isFloatArray() || isBooleanArray() || isStringArray();
}
bool GPStore::Value::isIntArray() const{
if(type_ != LIST) return false;
for(const Value *v : *data_.List){
if(v->type_ != INTEGER)
return false;
}
return true;
}
bool GPStore::Value::isLongArray() const{
if(type_ != LIST) return false;
for(const Value *v : *data_.List){
if(v->type_ != INTEGER && v->type_ != LONG)
return false;
}
return true;
}
bool GPStore::Value::isFloatArray() const{
if(type_ != LIST) return false;
for(const Value *v : *data_.List){
if(v->type_ != INTEGER && v->type_ != LONG && v->type_ != FLOAT)
return false;
}
return true;
}
bool GPStore::Value::isStringArray() const{
if(type_ != LIST) return false;
for(const Value *v : *data_.List){
if(v->type_ != STRING)
return false;
}
return true;
}
bool GPStore::Value::isBooleanArray() const{
if(type_ != LIST) return false;
for(const Value *v : *data_.List){
if(v->type_ != BOOLEAN)
return false;
}
return true;
}
GPStore::uint_64 GPStore::Value::convertToBytes(void * ptr){
//TODO
return 0;
}
void GPStore::Value::constructFromBytes(const void * ptr, uint_64 n){
// TODO
return;
}
GPStore::Value::Type GPStore::Value::getType() const{
return type_;
}
GPStore::int_64 GPStore::Value::hashCode() const {
int_64 hash = 0, seed = 131, mask = 0x7FFFFFFFFFFFFFFF;
uint_64 n = 0;
switch (type_)
{
case Value::INTEGER:
return data_.Int & mask;
case Value::FLOAT:
return *(int_64 *) (&data_.Float) & mask;
case Value::STRING:
n = data_.String->size();
for(uint_64 i = 0; i < n; ++i){
hash = hash * seed + (data_.String->at(i));
}
return (hash & mask);
case Value::BOOLEAN:
return data_.Boolean ? 1 : 0;
case Value::NODE:
return data_.Node & mask;
case Value::EDGE:
return data_.Edge & mask;
case Value::PATH:
return 7; // Unable to hash a path.
case Value::LIST:
n = data_.List->size();
for(uint_64 i = 0; i < n; ++i){
hash = hash * seed + (data_.List->at(i)->hashCode());
}
return (hash & mask);
case Value::MAP:
return 13; // Unable to hash a map
case Value::NO_VALUE:
return 17;
case Value::DATE_TIME:
return 19;
}
return 131; //make g++ happy
}
/**
* @brief Compare two Value objects
* This function compares two Value objects. Currently, it checks whether the two
* Value objects are equal based on their type and content. For example, if the two
* Value objects are of type INTEGER, it will compare the integer values. If the two
* Value objects are of type STRING, it will compare the string values.
*
* @return true if the two Value objects are equal, false otherwise
*
* @remark 比较两个值
*/
bool GPStore::Value::operator==(const Value& other) const{
switch (type_)
{
case Value::INTEGER:
return IntEqualTo(other);
case Value::LONG:
return LongEqualTo(other);
case Value::FLOAT:
return FloatEqualTo(other);
case Value::STRING:
return StringEqualTo(other);
case Value::BOOLEAN:
return BooleanEqualTo(other);
case Value::NODE:
return NodeEqualTo(other);
case Value::EDGE:
return EdgeEqualTo(other);
case Value::PATH:
return PathEqualTo(other);
case Value::DATE_TIME:
return DatetimeEqualTo(other);
case Value::LIST:
return ListEqualTo(other);
case Value::MAP:
return MapEqualTo(other);
case Value::NO_VALUE:
return NoValueEqualTo(other);
}
return false; //make g++ happy
}
/**
* @brief Compare two Value objects
* This function compares two Value objects. It will return true if the first value
* is less than the second value, false otherwise.
*
* @param other The second Value object to compare with.
*
* @return true if the first Value object is less than the second Value object, false otherwise
*
* @remark 对两个值进行比较
*/
bool GPStore::Value::operator<(const Value& other) const{
switch (type_)
{
case Value::INTEGER:
return CompareIntWith(other) == -1;
case Value::LONG:
return CompareLongWith(other) == -1;
case Value::FLOAT:
return CompareFloatWith(other) == -1;
case Value::STRING:
return CompareStringWith(other) == -1;
case Value::BOOLEAN:
return CompareBooleanWith(other) == -1;
case Value::NODE:
return CompareNodeWith(other) == -1;
case Value::EDGE:
return CompareEdgeWith(other)== -1;
case Value::PATH:
return ComparePathWith(other) == -1;
case Value::DATE_TIME:
return CompareDatetimeWith(other) == -1;
case Value::LIST:
return CompareListWith(other) == -1;
case Value::MAP:
return CompareMapWith(other) == -1;
case Value::NO_VALUE:
return CompareNoValueWith(other) == -1;
}
return false; //make g++ happy
}
/**
* @brief Compare two Value objects.
* This function compares two Value objects. It will return a negative integer if
* the first Value object is less than the second Value object, 0 if they are equal,
* and a positive integer otherwise.
*
* @param other The second Value object to compare with.
*
* @return -1 if the first Value object is less than the second Value object,
* 0 if they are equal, 1 otherwise.
*
* @remark 两个值的比较
*/
int GPStore::Value::comp(const Value & other) const{
switch (type_)
{
case Value::INTEGER:
return CompareIntWith(other);
case Value::LONG:
return CompareLongWith(other);
case Value::FLOAT:
return CompareFloatWith(other);
case Value::STRING:
return CompareStringWith(other);
case Value::BOOLEAN:
return CompareBooleanWith(other);
case Value::NODE:
return CompareNodeWith(other);
case Value::EDGE:
return CompareEdgeWith(other);
case Value::PATH:
return ComparePathWith(other);
case Value::DATE_TIME:
return CompareDatetimeWith(other);
case Value::LIST:
return CompareListWith(other);
case Value::MAP:
return CompareMapWith(other);
case Value::NO_VALUE:
return CompareNoValueWith(other);
}
return 1;
}
/**
* @brief Copy assignment operator for class Value.
* This function copies the content of the other Value object to the current
* Value object.
* @param other The other Value object to copy from.
* @remark 赋值运算符
*/
GPStore::Value& GPStore::Value::operator=(const Value&other){
if(this == &other){
return *this;
}
Destruct();
ConstructFrom(other);
return *this;
}
/**
* @brief Gets the content of a list-typed Value object.
*
* This function returns the content of a list-typed Value object. If the Value
* object is not of type LIST, this function returns nullptr.
*
* @return The content of the list-typed Value object, or nullptr if the Value
* object is not of type LIST.
*
* @remark 得到列表内容
*/
const std::vector<GPStore::Value*>* GPStore::Value::getListContent(){
if(type_ != LIST) return nullptr;
return this->data_.List;
}
/**
* @brief Appends a Value object to the list-typed Value object.
*
* This function appends a Value object to the list-typed Value object. If the
* Value object is not of type LIST, this function does nothing.
*
* @param value The Value object to append.
*
* @remark 向列表中添加元素
*/
void GPStore::Value::append(const GPStore::Value& value){
data_.List->push_back(ValueDeepCopy(&value));
return;
}
/**
* @brief Appends a Value pointer to the list-typed Value object.
*
* This function appends a Value pointer to the list-typed Value object. If the
* Value object is not of type LIST, this function does nothing.
*
* @param value The Value pointer to append.
*
* @remark 向列表中添加Value指针
*/
void GPStore::Value::append(Value * value){
data_.List->push_back(value);
}
/**
* @brief Accesses an element in the list-typed Value object.
*
* This function returns a reference to the element at the specified index in the
* list-typed Value object. If the Value object is not of type LIST, this function
* throws an exception.
*
* @param index The index of the element to access.
* @return A reference to the element at the specified index.
* @remark 访问列表中的元素
*/
GPStore::Value& GPStore::Value::operator[](uint_64 index){
assert(type_ == LIST and data_.List->size() > index);
return *data_.List->at(index);
}
/**
* @brief Inserts a key-value pair into the map-typed Value object.
*
* This function inserts a key-value pair into the map-typed Value object. If the
* Value object is not of type MAP, this function does nothing.
*
* @param key The key of the pair to insert.
* @param value The value of the pair to insert.
*
* @remark 插入键值对
*/
void GPStore::Value::insert(const std::string &key, const GPStore::Value &value){
assert(type_ == MAP);
data_.Map->insert(make_pair(key, ValueDeepCopy(&value)));
}
/**
* @brief Inserts a key-value pair into the map-typed Value object.
*
* This function inserts a key-value pair into the map-typed Value object. If the
* Value object is not of type MAP, this function does nothing. The value is stored
* by pointer, so the caller is responsible for ensuring the lifetime of the
* pointed-to object.
*
* @param key The key of the pair to insert.
* @param value The value of the pair to insert.
*
* @remark 插入键值对
*/
void GPStore::Value::insert(const std::string &key, Value *val_ptr){
assert(type_ == MAP);
data_.Map->insert(std::make_pair(key, val_ptr));
}
/**
* @brief Searches for a key in the map-typed Value object.
*
* This function searches for the specified key in the map-typed Value object.
* If the key is found, it returns a pointer to the associated Value object.
* If the key is not found or the Value object is not of type MAP, it returns nullptr.
*
* @param key The key to search for in the map.
* @return A pointer to the Value object associated with the key, or nullptr if the key is not found.
*
* @remark 在映射中搜索键
*/
GPStore::Value * GPStore::Value::search(const std::string &key){
assert(type_ == MAP);
auto end = data_.Map->end();
auto it = data_.Map->find(key);
if(it != end){
return it->second;
} else {
return nullptr;
}
}
/**
* @brief Returns the size of the list or map-typed Value object.
*
* This function returns the size of the list or map-typed Value object. If the
* Value object is not of type LIST or MAP, this function returns 0.
* @return The size of the list or map-typed Value object.
* @remark 获取列表或映射的大小
*/
GPStore::uint_64 GPStore::Value::size(){
if(type_ == STRING){
return data_.String->size();
}else if(type_ == LIST){
return data_.List->size();
}else if(type_ == MAP){
return data_.Map->size();
}else{
assert(false);
}
}
/**
* @brief Converts the Value object to an int.
*
* This function will convert the Value object to an int if it can be converted.
* If the Value object is of type INTEGER, FLOAT, LONG, or STRING, it will be converted
* to int. If the Value object is of any other type, 0 will be returned.
*
* @return The integer representation of the Value object.
*
* @remark 将Value对象转换为32位整数
*/
int GPStore::Value::toInt() const{
switch (type_){
case INTEGER:
return data_.Int;
break;
case FLOAT:
return static_cast<int>(data_.Float);
break;
case LONG:
return static_cast<int>(data_.Long);
break;
case STRING:
return std::stoi(*data_.String);
break;
default:
return 0;
break;
}
return 0;
}
/**
* @brief Converts the Value object to a long long.
*
* This function will convert the Value object to a long long if it can be converted.
* If the Value object is of type INTEGER, FLOAT, LONG, or STRING, it will be converted
* to long long. If the Value object is of any other type, 0 will be returned.
*
* @return The long long representation of the Value object.
*
* @remark 将Value对象转换为64位整数
*/
long long GPStore::Value::toLLong() const{
switch (type_){
case INTEGER:
return static_cast<long long>(data_.Int);
break;
case FLOAT:
return static_cast<long long>(data_.Float);
break;
case LONG:
return data_.Long;
break;
case STRING:
return std::stoll(*data_.String);
break;
default:
return 0;
break;
}
return 0;
}
/**
* @brief Converts the Value object to a string.
*
* This function will convert the Value object to a string if it can be converted.
* If the Value object is of type INTEGER, FLOAT, LONG, STRING, BOOLEAN, NODE,
* EDGE, PATH, LIST, or MAP, it will be converted to a string. If the Value object
* is of any other type, an empty string will be returned.
*
* @return The string representation of the Value object.
*
* @remark 将Value对象转换为字符串
*/
std::string GPStore::Value::toString() const {
std::stringstream buf;
bool first = true;
switch (type_)
{
case GPStore::Value::INTEGER:
return to_string(data_.Int);
case GPStore::Value::LONG:
return to_string(data_.Long);
case GPStore::Value::FLOAT:
return to_string(data_.Float);
case GPStore::Value::STRING:
return *data_.String;
case GPStore::Value::BOOLEAN:
return data_.Boolean ? "true" : "false";
case GPStore::Value::NODE:
return string("Node(") + to_string(data_.Node) + ")";
case GPStore::Value::EDGE:
return string("Edge(") + to_string(data_.Edge) + ")";
case GPStore::Value::PATH:
return "PATH";
case GPStore::Value::LIST:
buf << "[";
first = true;
for(auto v : *data_.List){
if(!first){buf << ", ";}
else first = false;
buf << v->toString();
}
buf << "]";
return buf.str();
case GPStore::Value::MAP:
buf << "{";
first = true;
for(auto & p : *data_.Map){
if(!first){buf << ", ";}
else first = false;
buf << p.first << ": " << p.second->toString();
}
buf << "}";
return buf.str();
case GPStore::Value::DATE_TIME:
buf << std::setfill('0');
buf << std::setw(4) << data_.Datetime->year << '-';
buf << std::setw(2) << data_.Datetime->month << '-';
buf << std::setw(2) << data_.Datetime->day << 'T';
buf << std::setw(2) << data_.Datetime->hour << ':';
buf << std::setw(2) << data_.Datetime->minute << ':';
buf << std::setw(2) << data_.Datetime->second << '.';
buf << std::setw(3) << data_.Datetime->millisecond ;
buf << std::setw(3) << data_.Datetime->microsecond ;
buf << std::setw(3) << data_.Datetime->nanosecond << 'Z';
return buf.str();
case GPStore::Value::NO_VALUE:
return "null";
}
return ""; //make g++ happy
}
/**
* @brief Creates a deep copy of a Value object.
*
* This function creates a deep copy of the given Value object. The function
* handles various types of Value objects, such as INTEGER, LONG, FLOAT,
* STRING, BOOLEAN, NODE, EDGE, PATH, DATE_TIME, LIST, and MAP. For LIST and
* MAP types, it recursively deep copies the elements or pairs contained
* within them. If the Value object is of type NO_VALUE, a new Value object
* of the same type is created.
*
* @param value The Value object to be deep copied.
* @return A pointer to the newly created deep copy of the Value object.
*
* @remark 生成Value对象的深拷贝
*/
GPStore::Value * GPStore::Value::ValueDeepCopy(const Value *value){
Value::Type ty = value->type_;
Value *new_elem = nullptr;
switch (ty)
{
case GPStore::Value::INTEGER:
new_elem = new GPStore::Value(value->data_.Int);
break;
case GPStore::Value::LONG:
new_elem = new GPStore::Value(value->data_.Long);
break;
case GPStore::Value::FLOAT:
new_elem = new GPStore::Value(value->data_.Float);
break;
case GPStore::Value::STRING:
new_elem = new GPStore::Value(*value->data_.String);
break;
case GPStore::Value::BOOLEAN:
new_elem = new GPStore::Value(value->data_.Boolean);
break;
case GPStore::Value::NODE:
new_elem = new GPStore::Value(Value::NODE, value->data_.Node);
break;
case GPStore::Value::EDGE:
new_elem = new GPStore::Value(Value::EDGE, value->data_.Edge);
break;
case GPStore::Value::PATH:
new_elem = new GPStore::Value(value->data_.Path->node_id_, value->data_.Path->edge_id_);
break;
case GPStore::Value::DATE_TIME:
new_elem = new GPStore::Value(value->data_.Datetime);
break;
case GPStore::Value::LIST:
new_elem = new GPStore::Value(Value::LIST);