Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 61e8839

Browse files
author
Nikita Glukhov
committed
Add jsonbzDecompressTo()
1 parent 8a5b92d commit 61e8839

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

contrib/jsonb_toaster/jsonb_toaster.c

+29-16
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ static JsonbValue *fillCompressedJsonbValue(CompressedJsonb *cjb,
189189
int index, char *base_addr,
190190
uint32 offset, JsonValue *result);
191191
static JsonbContainerHeader *jsonxzDecompress(JsonContainer *jc);
192+
static void jsonxzDecompressTo(CompressedJsonb *cjb, Size offset);
192193
static bool JsonContainerIsToasted(JsonContainer *jc,
193194
JsonbToastedContainerPointerData *jbcptr);
194195
static bool JsonContainerIsCompressed(JsonContainer *jc,
@@ -747,7 +748,7 @@ JsonxIteratorInit(JsonContainer *cont, const JsonbContainerHeader *container,
747748

748749
/* decompress container header */
749750
if (cjb)
750-
PG_DETOAST_ITERATE(cjb->iter, cjb->iter->buf->buf + cjb->offset + offsetof(JsonbDatum, root.children));
751+
jsonxzDecompressTo(cjb, cjb->offset + offsetof(JsonbContainerHeader, children));
751752

752753
type = container->header & JBC_TMASK;
753754

@@ -792,7 +793,7 @@ JsonxIteratorInit(JsonContainer *cont, const JsonbContainerHeader *container,
792793
}
793794

794795
if (it->dataProper && cjb)
795-
PG_DETOAST_ITERATE(cjb->iter, it->dataProper);
796+
jsonxzDecompressTo(cjb, cjb->offset + (it->dataProper - (char *) container));
796797

797798
return (JsonIterator *) it;
798799
}
@@ -1616,14 +1617,26 @@ jsonxzInitContainer(JsonContainerData *jc, CompressedJsonb *cjb,
16161617
jsonxInitContainerFromHeader(jc, header);
16171618
}
16181619

1620+
static void
1621+
jsonxzDecompressAll(CompressedJsonb *cjb)
1622+
{
1623+
PG_DETOAST_ITERATE(cjb->iter, cjb->iter->buf->capacity);
1624+
}
1625+
1626+
static void
1627+
jsonxzDecompressTo(CompressedJsonb *cjb, Size offset)
1628+
{
1629+
PG_DETOAST_ITERATE(cjb->iter, cjb->iter->buf->buf + offset);
1630+
}
1631+
16191632
static JsonbContainerHeader *
16201633
jsonxzDecompress(JsonContainer *jc)
16211634
{
16221635
CompressedJsonb *cjb = jsonbzGetCompressedJsonb(jc);
16231636
JsonbDatum *jb = (JsonbDatum *) cjb->iter->buf->buf;
16241637
JsonbContainerHeader *container = (JsonbContainerHeader *)((char *) jb + cjb->offset);
16251638

1626-
PG_DETOAST_ITERATE(cjb->iter, cjb->iter->buf->buf + cjb->offset + jc->len);
1639+
jsonxzDecompressTo(cjb, cjb->offset + jc->len);
16271640

16281641
return container;
16291642
}
@@ -1658,15 +1671,13 @@ fillCompressedJsonbValue(CompressedJsonb *cjb, const JsonbContainerHeader *conta
16581671

16591672
len -= INTALIGN(offset) - offset;
16601673

1661-
PG_DETOAST_ITERATE(cjb->iter, cjb->iter->buf->buf + cjb2.offset +
1662-
offsetof(JsonbContainerHeader, children));
1663-
1674+
jsonxzDecompressTo(cjb, cjb2.offset + offsetof(JsonbContainerHeader, children));
16641675
jsonxzInitContainer(cont, &cjb2, NULL, len);
16651676
JsonValueInitBinary(result, cont);
16661677
}
16671678
else
16681679
{
1669-
PG_DETOAST_ITERATE(cjb->iter, cjb->iter->buf->buf + base_offset + offset + len);
1680+
jsonxzDecompressTo(cjb, base_offset + offset + len);
16701681
jsonxFillValue(container, index, base_addr, offset, result);
16711682
}
16721683

@@ -1702,7 +1713,7 @@ findValueInCompressedJsonbObject(CompressedJsonb *cjb, const char *keystr, int k
17021713
key.val.string.val = keystr;
17031714
key.val.string.len = keylen;
17041715

1705-
PG_DETOAST_ITERATE(cjb->iter, cjb->iter->buf->buf + base_offset);
1716+
jsonxzDecompressTo(cjb, base_offset);
17061717

17071718
/* Binary search on object/pair keys *only* */
17081719
while (stopLow < stopHigh)
@@ -1717,7 +1728,7 @@ findValueInCompressedJsonbObject(CompressedJsonb *cjb, const char *keystr, int k
17171728
offset = getJsonbOffset(container, stopMiddle);
17181729
len = getJsonbLength(container, stopMiddle);
17191730

1720-
PG_DETOAST_ITERATE(cjb->iter, cjb->iter->buf->buf + base_offset + offset + len);
1731+
jsonxzDecompressTo(cjb, base_offset + offset + len);
17211732

17221733
difference = lengthCompareJsonbString(base_addr + offset, len,
17231734
key.val.string.val,
@@ -1767,7 +1778,7 @@ jsonxzFindKeyInObject(JsonContainer *jc, const char *key, int len, JsonValue *re
17671778

17681779
CompressedDatumDecompress(cjb->datum, cjb->offset + offsetof(JsonbContainerHeader, header));
17691780
#else
1770-
PG_DETOAST_ITERATE(cjb->iter, cjb->iter->buf->buf + cjb->offset + offsetof(JsonbContainerHeader, header));
1781+
jsonxzDecompressTo(cjb, cjb->offset + offsetof(JsonbContainerHeader, children));
17711782
#endif
17721783

17731784
return findValueInCompressedJsonbObject(cjb, key, len, res);
@@ -1789,11 +1800,11 @@ JsonbzArrayIteratorInit(JsonbzArrayIterator *it, CompressedJsonb *cjb)
17891800
JsonbDatum *jb = (JsonbDatum *) cjb->iter->buf->buf;
17901801
const JsonbContainerHeader *jbc = (const JsonbContainerHeader *)((char *) jb + cjb->offset);
17911802

1792-
PG_DETOAST_ITERATE(cjb->iter, (const char *) &jbc->children);
1803+
jsonxzDecompressTo(cjb, cjb->offset + ((char *) &jbc->children - (char *) jbc));
17931804

17941805
it->count = (cjb->header & JBC_CMASK);
17951806

1796-
PG_DETOAST_ITERATE(cjb->iter, (const char *) &jbc->children[it->count]);
1807+
jsonxzDecompressTo(cjb, cjb->offset + ((char *) &jbc->children[it->count] - (char *) jbc));
17971808

17981809
it->cjb = cjb;
17991810
it->container = jbc;
@@ -1871,7 +1882,7 @@ jsonxzIteratorInit(JsonContainer *jc)
18711882
JsonbContainerHeader *jbc = (JsonbContainerHeader *)((char *) jb + cjb->offset);
18721883

18731884
if (!jsonb_partial_decompression)
1874-
PG_DETOAST_ITERATE(cjb->iter, cjb->iter->buf->capacity);
1885+
jsonxzDecompressAll(cjb);
18751886

18761887
return JsonxIteratorInit(jc, jbc, cjb);
18771888
}
@@ -1880,15 +1891,17 @@ static void
18801891
jsonxzInitFromDetoastIterator(JsonContainerData *jc, DetoastIterator iter, JsonbContainerHdr *header)
18811892
{
18821893
CompressedJsonb *cjb = palloc(sizeof(*cjb));
1894+
int len = VARSIZE_ANY_EXHDR(iter->buf->buf);
1895+
18831896
cjb->iter = iter;
18841897
cjb->offset = offsetof(JsonbDatum, root);
18851898

18861899
if (!jsonb_partial_decompression)
1887-
PG_DETOAST_ITERATE(iter, iter->buf->capacity);
1900+
jsonxzDecompressAll(cjb);
18881901
else if (!header)
1889-
PG_DETOAST_ITERATE(iter, Min(iter->buf->buf + offsetof(JsonbDatum, root.children), iter->buf->capacity));
1902+
jsonxzDecompressTo(cjb, Min(offsetof(JsonbDatum, root.children), iter->buf->capacity - iter->buf->buf));
18901903

1891-
jsonxzInitContainer(jc, cjb, header, VARSIZE_ANY_EXHDR(iter->buf->buf)); // cd->total_len - VARHDRSZ
1904+
jsonxzInitContainer(jc, cjb, header, len);
18921905
}
18931906

18941907
static void

0 commit comments

Comments
 (0)