18
18
19
19
JsonContainerOps jsonvContainerOps ;
20
20
21
- static Json * JsonExpand (Json * tmp ,Datum value ,bool freeValue ,
22
- JsonContainerOps * ops );
23
-
24
21
JsonValue *
25
22
JsonValueCopy (JsonValue * res ,const JsonValue * val )
26
23
{
@@ -706,7 +703,6 @@ jsonvCopy(JsonContainer *jc)
706
703
JsonContainerOps
707
704
jsonvContainerOps =
708
705
{
709
- JsonContainerJsonv ,
710
706
NULL ,
711
707
jsonvIteratorInit ,
712
708
jsonvFindKeyInObject ,
@@ -729,88 +725,6 @@ JsonToJsonValue(Json *json, JsonValue *jv)
729
725
return JsonValueInitBinary (jv ,& json -> root );
730
726
}
731
727
732
- #ifdef JSON_FLATTEN_INTO_JSONEXT
733
- typedef struct varatt_extended_json
734
- {
735
- varatt_extended_hdr vaext ;
736
- JsonContainerType type ;
737
- char params [FLEXIBLE_ARRAY_MEMBER ];
738
- }varatt_extended_json ;
739
-
740
- static Size
741
- jsonGetExtendedSize (JsonContainer * jc )
742
- {
743
- return VARHDRSZ_EXTERNAL + offsetof(varatt_extended_json ,params )+ jc -> len ;
744
- }
745
-
746
- static void
747
- jsonWriteExtended (JsonContainer * jc ,void * ptr ,Size allocated_size )
748
- {
749
- varatt_extended_json extjs ,
750
- * pextjs ;
751
-
752
- Assert (allocated_size >=jsonGetExtendedSize (jc ));
753
-
754
- extjs .vaext .size = jsonGetExtendedSize (jc )- VARHDRSZ_EXTERNAL ;
755
- extjs .type = JsonContainerGetType (jc );
756
- Assert (extjs .type != JsonContainerUnknown );
757
-
758
- SET_VARTAG_EXTERNAL (ptr ,VARTAG_EXTENDED );
759
- pextjs = (varatt_extended_json * )VARDATA_EXTERNAL (ptr );
760
- memcpy (pextjs ,& extjs , offsetof(varatt_extended_json ,params ));
761
- memcpy (& pextjs -> params ,jc -> data ,jc -> len );
762
- }
763
-
764
- static Json *
765
- JsonInitExtended (Json * tmp ,struct varlena * extvalue ,bool freeValue )
766
- {
767
- JsonContainerOps * ops ;
768
- CompressionMethodRoutine * cmr ;
769
- varatt_extended_json * pextjs ,
770
- extjs ;
771
- void * val ;
772
- int len ;
773
- Datum value ;
774
-
775
- Assert (VARATT_IS_EXTERNAL_EXTENDED (extvalue ));
776
-
777
- pextjs = (varatt_extended_json * )VARDATA_EXTERNAL (extvalue );
778
- memcpy (& extjs ,pextjs , offsetof(varatt_extended_json ,data ));
779
-
780
- totalSize = extjs .vaext .size - offsetof(varatt_extended_json ,data );
781
-
782
- ops = JsonContainerGetOpsByType (extjs .type );
783
-
784
- if (ops )
785
- cmr = NULL ;
786
- else
787
- {
788
- cmr = GetCompressionMethodRoutine (extjs .type ,InvalidOid );
789
-
790
- if (!cmr )
791
- elog (ERROR ,"unrecognized json container type %d" ,extjs .type );
792
- }
793
-
794
- len = extjs .vaext .size - offsetof(varatt_extended_json ,params );
795
-
796
- val = palloc (VARHDRSZ + len );/* FIXME save value with varlena header */
797
- SET_VARSIZE (val ,VARHDRSZ + len );
798
- memcpy (VARDATA (val ),& pextjs -> params ,len );
799
-
800
- if (freeValue )
801
- pfree (extvalue );
802
-
803
- if (ops )
804
- return JsonExpand (tmp ,value , true,ops );
805
-
806
- value = cmr -> decompress (PointerGetDatum (val ),NULL );
807
-
808
- Assert (VARATT_IS_EXTERNAL_EXPANDED (DatumGetPointer (value )));
809
-
810
- return (Json * )DatumGetEOHP (value );
811
- }
812
- #endif
813
-
814
728
static void
815
729
JsonInit (Json * json )
816
730
{
@@ -829,233 +743,36 @@ JsonInit(Json *json)
829
743
json -> root .ops -> init (& json -> root ,json -> obj .value );
830
744
}
831
745
832
- static Size
833
- jsonGetFlatSizeJsont (Json * json ,void * * context )
834
- {
835
- Size size ;
836
-
837
- if (json -> root .ops == & jsontContainerOps )
838
- size = VARHDRSZ + json -> root .len ;
839
- else
840
- {
841
- char * str = JsonToCString (& json -> root );
842
- size = VARHDRSZ + strlen (str );
843
- if (context )
844
- * context = str ;
845
- else
846
- pfree (str );
847
- }
848
-
849
- return size ;
850
- }
851
-
852
- static void *
853
- jsonFlattenJsont (Json * json ,void * * context )
854
- {
855
- if (json -> root .ops == & jsontContainerOps )
856
- return cstring_to_text_with_len (json -> root .data ,json -> root .len );
857
- else
858
- {
859
- char * str = context ? (char * )* context :JsonToCString (JsonRoot (json ));
860
- text * text = cstring_to_text (str );
861
- pfree (str );
862
- return text ;
863
- }
864
- }
865
-
866
- static Size
867
- jsonGetFlatSize2 (Json * json ,void * * context )
868
- {
869
- Size size ;
870
-
871
- #ifdef JSON_FLATTEN_INTO_TARGET
872
- if (json -> is_json )
873
- #endif
874
- #if defined(JSON_FLATTEN_INTO_TARGET )|| defined(JSON_FLATTEN_INTO_JSONT )
875
- size = jsonGetFlatSizeJsont (json ,context );
876
- #endif
877
- #ifdef JSON_FLATTEN_INTO_TARGET
878
- else
879
- #endif
880
- #if defined(JSON_FLATTEN_INTO_TARGET )|| defined(JSON_FLATTEN_INTO_JSONB )
881
- {
882
- if (json -> root .ops == & jsonbContainerOps )
883
- size = VARHDRSZ + json -> root .len ;
884
- else
885
- {
886
- JsonValue val ;
887
- void * js = JsonValueToJsonb (JsonToJsonValue (json ,& val ));
888
- size = VARSIZE (js );
889
- if (context )
890
- * context = js ;
891
- else
892
- pfree (js );
893
- }
894
- }
895
- #endif
896
-
897
- return size ;
898
- }
899
-
900
- static void *
901
- jsonFlatten (Json * json ,void * * context )
902
- {
903
- #ifdef JSON_FLATTEN_INTO_TARGET
904
- if (json -> is_json )
905
- #endif
906
- #if defined(JSON_FLATTEN_INTO_TARGET )|| defined(JSON_FLATTEN_INTO_JSONT )
907
- return jsonFlattenJsont (json ,context );
908
- #endif
909
- #ifdef JSON_FLATTEN_INTO_TARGET
910
- else
911
- #endif
912
- #if defined(JSON_FLATTEN_INTO_TARGET )|| defined(JSON_FLATTEN_INTO_JSONB )
913
- {
914
- if (json -> root .ops == & jsonbContainerOps )
915
- {
916
- void * res = palloc (VARHDRSZ + json -> root .len );
917
- SET_VARSIZE (res ,VARHDRSZ + json -> root .len );
918
- memcpy (VARDATA (res ),json -> root .data ,json -> root .len );
919
- return res ;
920
- }
921
- else if (context )
922
- return * context ;
923
- else
924
- {
925
- JsonValue val ;
926
- return JsonValueToJsonb (JsonToJsonValue (json ,& val ));
927
- }
928
- }
929
- #endif
930
- }
931
-
932
- static Size
933
- jsonGetFlatSize (ExpandedObjectHeader * eoh ,void * * context )
934
- {
935
- Json * json = (Json * )eoh ;
936
-
937
- JsonInit (json );
938
-
939
- #ifdef JSON_FLATTEN_INTO_JSONEXT
940
- {
941
- JsonContainer * flat = JsonRoot (json );
942
- JsonContainerData tmp ;
943
-
944
- if (json -> root .ops == & jsonvContainerOps )
945
- {
946
- JsonValue * val = (JsonValue * )flat -> data ;
947
-
948
- if (JsonValueIsUniquified (val ))
949
- {
950
- tmp .len = jsonGetFlatSize2 (json ,context )- VARHDRSZ ;
951
- tmp .ops = flatContainerOps ;
952
- }
953
- else
954
- {
955
- tmp .len = jsonGetFlatSizeJsont (json ,context )- VARHDRSZ ;
956
- tmp .ops = & jsontContainerOps ;
957
- }
958
-
959
- tmp .data = NULL ;
960
-
961
- flat = & tmp ;
962
- }
963
-
964
- return jsonGetExtendedSize (flat );
965
- }
966
- #else
967
- return jsonGetFlatSize2 (json ,context );
968
- #endif
969
- }
970
-
971
- static void
972
- jsonFlattenInto (ExpandedObjectHeader * eoh ,void * result ,Size allocated_size ,
973
- void * * context )
974
- {
975
- Json * json = (Json * )eoh ;
976
-
977
- JsonInit (json );
978
-
979
- #ifdef JSON_FLATTEN_INTO_JSONEXT
980
- {
981
- JsonContainer * flat = JsonRoot (json );
982
- JsonContainerData tmp ;
983
- void * tmpData = NULL ;
984
-
985
- if (flat -> ops == & jsonvContainerOps )
986
- {
987
- JsonValue * val = (JsonValue * )flat -> data ;
988
-
989
- if (JsonValueIsUniquified (val ))
990
- {
991
- tmpData = jsonFlatten (json ,context );
992
- tmp .ops = flatContainerOps ;
993
- }
994
- else
995
- {
996
- tmpData = jsonFlattenJsont (json ,context );
997
- tmp .ops = & jsontContainerOps ;
998
- }
999
-
1000
- tmp .data = VARDATA (tmpData );
1001
- tmp .len = VARSIZE (tmpData )- VARHDRSZ ;
1002
-
1003
- flat = & tmp ;
1004
- }
1005
-
1006
- jsonWriteExtended (flat ,result ,allocated_size );
1007
-
1008
- if (tmpData )
1009
- pfree (tmpData );
1010
- }
1011
- #else
1012
- {
1013
- void * data = jsonFlatten (json ,context );
1014
- memcpy (result ,data ,allocated_size );
1015
- pfree (data );
1016
- }
1017
- #endif
1018
- }
1019
-
1020
- static ExpandedObjectMethods
1021
- jsonExpandedObjectMethods =
1022
- {
1023
- jsonGetFlatSize ,
1024
- jsonFlattenInto
1025
- };
1026
-
1027
746
static Json *
1028
747
JsonExpand (Json * tmp ,Datum value ,bool freeValue ,JsonContainerOps * ops )
1029
748
{
1030
- MemoryContext objcxt ;
1031
749
Json * json ;
1032
750
1033
751
if (tmp )
1034
752
{
1035
753
json = tmp ;
1036
- json -> obj .eoh . vl_len_ = 0 ;
754
+ json -> obj .isTemporary = true ;
1037
755
}
1038
756
else
1039
757
{
1040
758
#ifndef JSON_EXPANDED_OBJECT_MCXT
1041
759
json = (Json * )palloc (sizeof (Json ));
1042
- objcxt = NULL ;
1043
760
#else
1044
761
/*
1045
762
* Allocate private context for expanded object. We start by assuming
1046
763
* that the json won't be very large; but if it does grow a lot, don't
1047
764
* constrain aset.c's large-context behavior.
1048
765
*/
1049
- objcxt = AllocSetContextCreate (CurrentMemoryContext ,
1050
- "expanded json" ,
1051
- ALLOCSET_SMALL_MINSIZE ,
1052
- ALLOCSET_SMALL_INITSIZE ,
1053
- ALLOCSET_DEFAULT_MAXSIZE );
766
+ MemoryContext objcxt =
767
+ AllocSetContextCreate (CurrentMemoryContext ,
768
+ "expanded json" ,
769
+ ALLOCSET_SMALL_MINSIZE ,
770
+ ALLOCSET_SMALL_INITSIZE ,
771
+ ALLOCSET_DEFAULT_MAXSIZE );
1054
772
1055
773
json = (Json * )MemoryContextAlloc (objcxt ,sizeof (Json ));
1056
774
#endif
1057
-
1058
- EOH_init_header (& json -> obj .eoh ,& jsonExpandedObjectMethods ,objcxt );
775
+ json -> obj .isTemporary = false;
1059
776
}
1060
777
1061
778
json -> obj .value = value ;
@@ -1074,28 +791,9 @@ static Json *
1074
791
JsonExpandDatum (Datum value ,JsonContainerOps * ops ,Json * tmp )
1075
792
{
1076
793
struct varlena * toasted = (struct varlena * )DatumGetPointer (value );
1077
- Json * json ;
1078
-
1079
- if (VARATT_IS_EXTERNAL_EXPANDED (toasted ))
1080
- json = (Json * )DatumGetEOHP (value );
1081
- else
1082
- {
1083
- struct varlena * detoasted = pg_detoast_datum (toasted );
794
+ struct varlena * detoasted = pg_detoast_datum (toasted );
1084
795
1085
- /*
1086
- if (VARATT_IS_EXTERNAL_EXTENDED(detoasted))
1087
- #ifdef JSON_FLATTEN_INTO_JSONEXT
1088
- return JsonInitExtended(tmp, detoasted, toasted != detoasted);
1089
- #else
1090
- elog(ERROR, "unexpected extended json");
1091
- #endif
1092
- */
1093
-
1094
- json = JsonExpand (tmp ,PointerGetDatum (detoasted ),toasted != detoasted ,
1095
- ops );
1096
- }
1097
-
1098
- return json ;
796
+ return JsonExpand (tmp ,PointerGetDatum (detoasted ),toasted != detoasted ,ops );
1099
797
}
1100
798
1101
799
Json *
@@ -1124,8 +822,7 @@ JsonCopyTemporary(Json *tmp)
1124
822
1125
823
memcpy (json ,tmp ,sizeof (Json ));
1126
824
tmp -> obj .freeValue = false;
1127
-
1128
- EOH_init_header (& json -> obj .eoh ,& jsonExpandedObjectMethods ,NULL );
825
+ tmp -> obj .isTemporary = false;
1129
826
1130
827
return json ;
1131
828
}