Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitba0e1c2

Browse files
author
Nikita Glukhov
committed
Add compression options support to json
1 parent90d68c7 commitba0e1c2

File tree

5 files changed

+112
-49
lines changed

5 files changed

+112
-49
lines changed

‎src/backend/utils/adt/json.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272

7373
#include"postgres.h"
7474

75+
#include"access/compression.h"
7576
#include"access/htup_details.h"
7677
#include"access/transam.h"
7778
#include"catalog/pg_type.h"
@@ -2627,7 +2628,7 @@ jsontInitContainer(JsonContainerData *jc, char *json, int len,
26272628
}
26282629

26292630
staticvoid
2630-
jsontInit(JsonContainerData*jc,Datumvalue)
2631+
jsontInit(JsonContainerData*jc,Datumvalue,CompressionOptionsoptions)
26312632
{
26322633
text*json=DatumGetTextP(value);
26332634
JsonLexContext*lex=makeJsonLexContext(json, false);
@@ -3027,6 +3028,7 @@ JsonContainerOps
30273028
jsontContainerOps=
30283029
{
30293030
JsonContainerJsont,
3031+
NULL,
30303032
jsontInit,
30313033
JsontIteratorInit,
30323034
jsonFindLastKeyInObject,

‎src/backend/utils/adt/json_generic.c

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
#include"utils/json_generic.h"
1414
#include"utils/memutils.h"
1515

16-
staticJsonContainerOpsjsonvContainerOps;
16+
staticJson*JsonExpand(Datumvalue,JsonContainerOps*ops,
17+
CompressionOptionsopts);
1718

18-
staticJson*JsonExpand(Datumvalue,JsonContainerOps*ops);
19+
staticJsonContainerOpsjsonvContainerOps;
1920

2021
#if0
2122
staticJsonValue*
@@ -544,6 +545,7 @@ jsonvContainerOps =
544545
{
545546
JsonContainerJsonv,
546547
NULL,
548+
NULL,
547549
jsonvIteratorInit,
548550
jsonvFindKeyInObject,
549551
jsonvFindValueInArray,
@@ -577,20 +579,24 @@ typedef struct varatt_extended_json
577579
{
578580
varatt_extended_hdrvaext;
579581
JsonContainerTypetype;
580-
charparams[FLEXIBLE_ARRAY_MEMBER];
582+
chardata[FLEXIBLE_ARRAY_MEMBER];
581583
}varatt_extended_json;
582584

583585
staticSize
584586
jsonGetExtendedSize(JsonContainer*jc)
585587
{
586-
returnVARHDRSZ_EXTERNAL+ offsetof(varatt_extended_json,params)+jc->len;
588+
returnVARHDRSZ_EXTERNAL+ offsetof(varatt_extended_json,data)+
589+
VARHDRSZ+jc->len+
590+
(jc->ops->compressionOps ?
591+
jc->ops->compressionOps->encodeOptions(jc,NULL) :0);
587592
}
588593

589594
staticvoid
590595
jsonWriteExtended(JsonContainer*jc,void*ptr,Sizeallocated_size)
591596
{
592597
varatt_extended_jsonextjs,
593598
*pextjs;
599+
SizeoptionsSize;
594600

595601
Assert(allocated_size >=jsonGetExtendedSize(jc));
596602

@@ -600,8 +606,13 @@ jsonWriteExtended(JsonContainer *jc, void *ptr, Size allocated_size)
600606

601607
SET_VARTAG_EXTERNAL(ptr,VARTAG_EXTENDED);
602608
pextjs= (varatt_extended_json*)VARDATA_EXTERNAL(ptr);
603-
memcpy(pextjs,&extjs, offsetof(varatt_extended_json,params));
604-
memcpy(&pextjs->params,jc->data,jc->len);
609+
memcpy(pextjs,&extjs, offsetof(varatt_extended_json,data));
610+
611+
optionsSize=jc->ops->compressionOps ?
612+
jc->ops->compressionOps->encodeOptions(jc,&pextjs->data) :0;
613+
614+
SET_VARSIZE(&pextjs->data[optionsSize],VARHDRSZ+jc->len);
615+
memcpy(VARDATA(&pextjs->data[optionsSize]),jc->data,jc->len);
605616
}
606617

607618
staticJson*
@@ -612,37 +623,51 @@ JsonInitExtended(const struct varlena *toasted,
612623
CompressionMethodRoutine*cmr;
613624
varatt_extended_json*pextjs,
614625
extjs;
615-
void*val;
616-
intlen;
617626
Datumvalue;
627+
CompressionOptionsoptions;
628+
SizetotalSize;
629+
SizeoptionsSize;
630+
Size (*decodeOptions)(constvoid*buf,
631+
CompressionOptions*options);
618632

619633
Assert(VARATT_IS_EXTERNAL_EXTENDED(detoasted));
620634

621635
pextjs= (varatt_extended_json*)VARDATA_EXTERNAL(detoasted);
622-
memcpy(&extjs,pextjs, offsetof(varatt_extended_json,params));
636+
memcpy(&extjs,pextjs, offsetof(varatt_extended_json,data));
637+
638+
totalSize=extjs.vaext.size- offsetof(varatt_extended_json,data);
623639

624640
ops=JsonContainerGetOpsByType(extjs.type);
625641

626642
if (ops)
643+
{
627644
cmr=NULL;
645+
decodeOptions=ops->compressionOps ?
646+
ops->compressionOps->decodeOptions :NULL;
647+
}
628648
else
629649
{
630650
cmr=GetCompressionMethodRoutine(extjs.type,InvalidOid);
631651

632652
if (!cmr)
633653
elog(ERROR,"unrecognized json container type %d",extjs.type);
654+
655+
decodeOptions=cmr->options ?cmr->options->decode :NULL;
634656
}
635657

636-
len=extjs.vaext.size- offsetof(varatt_extended_json,params);
658+
optionsSize=decodeOptions ?decodeOptions(&pextjs->data,&options) :0;
637659

638-
val=palloc(VARHDRSZ+len);/* FIXME save value with varlena header */
639-
SET_VARSIZE(val,VARHDRSZ+len);
640-
memcpy(VARDATA(val),&pextjs->params,len);
660+
/* FIXME alignment
661+
* value = PointerGetDatum(&pextjs->data[optionsSize]);
662+
*/
663+
value=PointerGetDatum(memcpy(palloc(totalSize-optionsSize),
664+
&pextjs->data[optionsSize],
665+
totalSize-optionsSize));
641666

642667
if (ops)
643-
returnJsonExpand(PointerGetDatum(val),ops);
668+
returnJsonExpand(value,ops,options);
644669

645-
value=cmr->decompress(PointerGetDatum(val),NULL);
670+
value=cmr->decompress(value,options);
646671

647672
Assert(VARATT_IS_EXTERNAL_EXPANDED(DatumGetPointer(value)));
648673

@@ -665,7 +690,7 @@ JsonInit(Json *json)
665690
Assert(!VARATT_IS_EXTERNAL_EXTENDED(data));
666691
json->obj.compressed=PointerGetDatum(data);
667692

668-
(*json->root.ops->init)(&json->root,json->obj.compressed);
693+
json->root.ops->init(&json->root,json->obj.compressed,json->obj.options);
669694
}
670695

671696
staticSize
@@ -822,7 +847,7 @@ jsonExpandedObjectMethods =
822847
};
823848

824849
staticJson*
825-
JsonExpand(Datumvalue,JsonContainerOps*ops)
850+
JsonExpand(Datumvalue,JsonContainerOps*ops,CompressionOptionsoptions)
826851
{
827852
MemoryContextobjcxt;
828853
Json*json;
@@ -843,6 +868,7 @@ JsonExpand(Datum value, JsonContainerOps *ops)
843868
EOH_init_header(&json->obj.eoh,&jsonExpandedObjectMethods,objcxt);
844869

845870
json->obj.compressed=value;
871+
json->obj.options=options;
846872
json->root.data=NULL;
847873
json->root.len=0;
848874
json->root.ops=ops;
@@ -853,8 +879,8 @@ JsonExpand(Datum value, JsonContainerOps *ops)
853879
returnjson;
854880
}
855881

856-
Json*
857-
DatumGetJson(Datumvalue,JsonContainerOps*ops)
882+
staticJson*
883+
JsonExpandDatum(Datumvalue,JsonContainerOps*ops,CompressionOptionsoptions)
858884
{
859885
structvarlena*toasted= (structvarlena*)DatumGetPointer(value);
860886
Json*json;
@@ -872,11 +898,17 @@ DatumGetJson(Datum value, JsonContainerOps *ops)
872898
elog(ERROR,"unexpected extended json");
873899
#endif
874900

875-
json=JsonExpand(PointerGetDatum(detoasted),ops);
901+
json=JsonExpand(PointerGetDatum(detoasted),ops,options);
876902
}
877903

878-
JsonInit(json);
904+
returnjson;
905+
}
879906

907+
Json*
908+
DatumGetJson(Datumvalue,JsonContainerOps*ops,CompressionOptionsoptions)
909+
{
910+
Json*json=JsonExpandDatum(value,ops,options);
911+
JsonInit(json);
880912
returnjson;
881913
}
882914

@@ -885,13 +917,14 @@ JsonValueToJson(JsonValue *val)
885917
{
886918
if (val->type==jbvBinary)
887919
{
888-
Json*json=JsonExpand(PointerGetDatum(NULL),NULL);
889-
json->root=*val->val.binary.data;
920+
JsonContainer*jc=val->val.binary.data;
921+
Json*json=JsonExpand(PointerGetDatum(NULL),jc->ops,NULL);
922+
json->root=*jc;
890923
returnjson;
891924
}
892925
else
893926
{
894-
Json*json=JsonExpand(PointerGetDatum(NULL),&jsonvContainerOps);
927+
Json*json=JsonExpand(PointerGetDatum(NULL),&jsonvContainerOps,NULL);
895928
jsonvInitContainer(&json->root,val);
896929
returnjson;
897930
}

‎src/backend/utils/adt/jsonb_util.c

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414
#include"postgres.h"
1515

16+
#include"access/compression.h"
1617
#include"access/hash.h"
1718
#include"catalog/pg_collation.h"
1819
#include"miscadmin.h"
@@ -78,7 +79,8 @@ static void fillJsonbValue(const JsonbContainer *container, int index,
7879
char*base_addr,uint32offset,
7980
JsonbValue*result);
8081
staticintcompareJsonbScalarValue(constJsonbValue*a,constJsonbValue*b);
81-
staticvoid*convertToJsonb(constJsonbValue*val,JsonValueEncoderencoder);
82+
staticvoid*convertToJsonb(constJsonbValue*val,JsonValueEncoderencoder,
83+
CompressionOptionsoptions);
8284
staticvoidconvertJsonbValue(StringInfobuffer,JEntry*header,constJsonbValue*val,intlevel);
8385
staticvoidconvertJsonbArray(StringInfobuffer,JEntry*header,constJsonbValue*val,intlevel);
8486
staticvoidconvertJsonbObject(StringInfobuffer,JEntry*header,constJsonbValue*val,intlevel);
@@ -112,11 +114,14 @@ JsonValueUnpackBinary(const JsonValue *jbv)
112114

113115
void*
114116
JsonContainerFlatten(JsonContainer*jc,JsonValueEncoderencoder,
115-
JsonContainerOps*ops,constJsonValue*binary)
117+
JsonContainerOps*ops,CompressionOptionsoptions,
118+
constJsonValue*binary)
116119
{
117120
JsonbValuejbv;
118121

119-
if (jc->ops==ops)
122+
if (jc->ops==ops&&
123+
(!jc->ops->compressionOps||
124+
jc->ops->compressionOps->optionsAreEqual(jc,options)))
120125
{
121126
intsize=jc->len;
122127
void*out=palloc(VARHDRSZ+size);
@@ -135,7 +140,7 @@ JsonContainerFlatten(JsonContainer *jc, JsonValueEncoder encoder,
135140
binary=&jbv;
136141
}
137142

138-
returnconvertToJsonb(binary,encoder);
143+
returnconvertToJsonb(binary,encoder,options);
139144
}
140145

141146
/*
@@ -153,10 +158,11 @@ JsonContainerFlatten(JsonContainer *jc, JsonValueEncoder encoder,
153158
*/
154159
void*
155160
JsonValueFlatten(constJsonValue*val,JsonValueEncoderencoder,
156-
JsonContainerOps*ops)
161+
JsonContainerOps*ops,CompressionOptionsoptions)
157162
{
158163
if (val->type==jbvBinary)
159-
returnJsonContainerFlatten(val->val.binary.data,encoder,ops,val);
164+
returnJsonContainerFlatten(val->val.binary.data,encoder,ops,options,
165+
val);
160166

161167
if (IsAJsonbScalar(val))
162168
{
@@ -168,7 +174,7 @@ JsonValueFlatten(const JsonValue *val, JsonValueEncoder encoder,
168174
Assert(val->type==jbvObject||val->type==jbvArray);
169175
}
170176

171-
returnconvertToJsonb(val,encoder);
177+
returnconvertToJsonb(val,encoder,options);
172178
}
173179

174180
/*
@@ -1580,7 +1586,8 @@ padBufferToInt(StringInfo buffer)
15801586
}
15811587

15821588
void
1583-
JsonbEncode(StringInfoData*buffer,constJsonbValue*val)
1589+
JsonbEncode(StringInfoData*buffer,constJsonbValue*val,
1590+
CompressionOptionsoptions)
15841591
{
15851592
JEntryjentry;
15861593

@@ -1591,7 +1598,8 @@ JsonbEncode(StringInfoData *buffer, const JsonbValue *val)
15911598
* Given a JsonbValue, convert to Jsonb. The result is palloc'd.
15921599
*/
15931600
staticvoid*
1594-
convertToJsonb(constJsonbValue*val,JsonValueEncoderencoder)
1601+
convertToJsonb(constJsonbValue*val,JsonValueEncoderencoder,
1602+
CompressionOptionsoptions)
15951603
{
15961604
StringInfoDatabuffer;
15971605
void*res;
@@ -1602,7 +1610,7 @@ convertToJsonb(const JsonbValue *val, JsonValueEncoder encoder)
16021610
/* Make room for the varlena header */
16031611
reserveFromBuffer(&buffer,VARHDRSZ);
16041612

1605-
(*encoder)(&buffer,val);
1613+
(*encoder)(&buffer,val,options);
16061614

16071615
/*
16081616
* Note: the JEntry of the root is discarded. Therefore the root
@@ -2039,7 +2047,7 @@ jsonbInitContainer(JsonContainerData *jc, JsonbContainer *jbc, int len)
20392047
}
20402048

20412049
staticvoid
2042-
jsonbInit(JsonContainerData*jc,Datumvalue)
2050+
jsonbInit(JsonContainerData*jc,Datumvalue,CompressionOptionsoptions)
20432051
{
20442052
Jsonb*jb= (Jsonb*)DatumGetPointer(value);
20452053
jsonbInitContainer(jc,&jb->root,VARSIZE_ANY_EXHDR(jb));
@@ -2049,6 +2057,7 @@ JsonContainerOps
20492057
jsonbContainerOps=
20502058
{
20512059
JsonContainerJsonb,
2060+
NULL,
20522061
jsonbInit,
20532062
JsonbIteratorInit,
20542063
jsonbFindKeyInObject,

‎src/include/access/compression.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ typedef struct CompressionMethodOptionsRoutines
2727
void(*free)(CompressionOptionsoptions);
2828
CompressionOptions(*copy)(CompressionOptionsoptions);
2929
bool(*equal)(CompressionOptionso1,CompressionOptionso2);
30+
Size(*decode)(constvoid*buf,CompressionOptions*options);
3031
}CompressionMethodOptionsRoutines;
3132

3233
typedefDatum (*CompressionRoutine) (Datumvalue,CompressionOptionsoptions);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp