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

Commit391b30a

Browse files
author
Nikita Glukhov
committed
Add generic json cache support and jsonbc key cache
1 parent8952f1b commit391b30a

File tree

3 files changed

+148
-3
lines changed

3 files changed

+148
-3
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
#include"utils/memutils.h"
1717
#include"utils/builtins.h"
1818

19-
staticJson*JsonExpand(Json*tmp,Datumvalue,boolfreeValue,
20-
JsonContainerOps*ops,CompressionOptionsoptions);
2119

20+
JsonCacheContextJsonCache;
2221
JsonContainerOpsjsonvContainerOps;
2322

23+
staticJson*JsonExpand(Json*tmp,Datumvalue,boolfreeValue,
24+
JsonContainerOps*ops,CompressionOptionsoptions);
25+
2426
JsonValue*
2527
JsonValueCopy(JsonValue*res,constJsonValue*val)
2628
{

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

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,97 @@ typedef struct JsonbcPair
120120
staticJEntryjsonbcEncodeValue(StringInfobuffer,constJsonbValue*val,
121121
intlevel,JsonbcDictIddict);
122122

123+
typedefstructJsonbcKeyCacheEntry
124+
{
125+
char*key;
126+
intlen;
127+
JsonbcDictIddict;
128+
JsonbcKeyIdid;
129+
}JsonbcKeyCacheEntry;
130+
131+
#defineJSONBC_KEY_CACHE_ENTRIES 4
132+
133+
typedefstructJsonbcKeyCache
134+
{
135+
JsonbcKeyCacheEntryentries[JSONBC_KEY_CACHE_ENTRIES];
136+
intlastEntry;
137+
}JsonbcKeyCache;
138+
139+
staticinlineJsonbcKeyCache*
140+
jsonbcKeyCacheGet()
141+
{
142+
returnJsonCacheGet(1/* FIXME */,sizeof(JsonbcKeyCache));
143+
}
144+
145+
staticinlineJsonbcKeyCacheEntry*
146+
jsonbcKeyCacheGetIdByName(JsonbcDictIddict,JsonbcKeyNamekeyName)
147+
{
148+
JsonbcKeyCache*cache=jsonbcKeyCacheGet();
149+
inti;
150+
151+
if (!cache)
152+
returnNULL;
153+
154+
for (i=0;i<JSONBC_KEY_CACHE_ENTRIES;i++)
155+
{
156+
JsonbcKeyCacheEntry*entry=&cache->entries[i];
157+
158+
if (!entry->dict)
159+
break;
160+
161+
if (entry->dict==dict&&
162+
entry->key[0]==keyName.s[0]&&
163+
entry->len==keyName.len&&
164+
!memcmp(entry->key,keyName.s,entry->len))
165+
returnentry;
166+
}
167+
168+
returnNULL;
169+
}
170+
171+
staticinlinevoid
172+
jsonbcKeyCacheAdd(JsonbcDictIddict,JsonbcKeyNamekey,JsonbcKeyIdid)
173+
{
174+
JsonbcKeyCache*cache=jsonbcKeyCacheGet();
175+
JsonbcKeyCacheEntry*entry;
176+
177+
if (!cache)
178+
return;
179+
180+
entry=&cache->entries[cache->lastEntry];
181+
182+
if (entry->key)
183+
pfree(entry->key);
184+
185+
entry->key=MemoryContextAlloc(JsonCache.mcxt,key.len);
186+
memcpy(entry->key,key.s,key.len);
187+
entry->len=key.len;
188+
entry->dict=dict;
189+
entry->id=id;
190+
191+
cache->lastEntry= (cache->lastEntry+1)& (JSONBC_KEY_CACHE_ENTRIES-1);
192+
}
193+
123194
staticJsonbcKeyId
124195
jsonbcConvertKeyNameToId(JsonbcDictIddict,constJsonValue*string,
125196
boolinsert)
126197
{
127198
JsonbcKeyNamekeyName;
128199
JsonbcKeyIdkeyId;
200+
JsonbcKeyCacheEntry*entry;
129201

130202
keyName.s=string->val.string.val;
131203
keyName.len=string->val.string.len;
132204

133-
keyId=jsonbcDictGetIdByName(dict,keyName,insert);
205+
entry=jsonbcKeyCacheGetIdByName(dict,keyName);
206+
207+
if (entry)
208+
keyId=entry->id;
209+
else
210+
{
211+
keyId=jsonbcDictGetIdByName(dict,keyName,insert);
212+
jsonbcKeyCacheAdd(dict,keyName,keyId);
213+
}
134214

135215
if (insert&&keyId==JsonbcInvalidKeyId)
136216
elog(ERROR,"could not insert key \"%.*s\" into jsonbc dictionary",

‎src/include/utils/json_generic.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,4 +548,67 @@ extern void JsonbEncode(StringInfo, const JsonValue *, CompressionOptions);
548548

549549
externintlengthCompareJsonbStringValue(constvoid*a,constvoid*b);
550550

551+
typedefstructJsonCacheData
552+
{
553+
intid;
554+
chardata[FLEXIBLE_ARRAY_MEMBER];
555+
}JsonCacheData;
556+
557+
typedefstructJsonCacheContext
558+
{
559+
JsonCacheData**data;
560+
MemoryContextmcxt;
561+
}JsonCacheContext;
562+
563+
externPGDLLIMPORTJsonCacheContextJsonCache;
564+
565+
staticinlineJsonCacheContext
566+
JsonCacheSwitchTo(JsonCacheContextnewcxt)
567+
{
568+
JsonCacheContextoldcxt=JsonCache;
569+
JsonCache=newcxt;
570+
returnoldcxt;
571+
}
572+
573+
staticinlineJsonCacheContext
574+
JsonCacheSwitchTo2(void**data,MemoryContextmcxt)
575+
{
576+
JsonCacheContextoldcxt=JsonCache;
577+
578+
JsonCache.data= (JsonCacheData**)data;
579+
JsonCache.mcxt=mcxt;
580+
581+
returnoldcxt;
582+
}
583+
584+
staticinlineJsonCacheContext
585+
JsonCacheSwitchToFunc(FunctionCallInfofcinfo)
586+
{
587+
JsonCacheContextnewcxt= {
588+
fcinfo->flinfo ? (JsonCacheData**)&fcinfo->flinfo->fn_extra :NULL,
589+
fcinfo->flinfo ?fcinfo->flinfo->fn_mcxt :NULL
590+
};
591+
returnJsonCacheSwitchTo(newcxt);
592+
}
593+
594+
staticinlinevoid*
595+
JsonCacheGet(intid,size_tsize)
596+
{
597+
JsonCacheContext*cache=&JsonCache;
598+
599+
if (!cache->mcxt|| !cache->data)
600+
returnNULL;
601+
602+
if (!*cache->data|| (*cache->data)->id!=id)
603+
{
604+
if (*cache->data)
605+
pfree(*cache->data);
606+
*cache->data=MemoryContextAllocZero(cache->mcxt,
607+
offsetof(JsonCacheData,data)+size);
608+
(*cache->data)->id=id;
609+
}
610+
611+
return (void*)(*cache->data)->data;
612+
}
613+
551614
#endif/* UTILS_JSON_GENERIC_H */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp