Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 79c3b71

Browse files
Jan WieckJan Wieck
authored andcommitted
The new LZ compression and an lztext data type based on it.
Jan
1 parent ddc3352 commit 79c3b71

File tree

8 files changed

+1123
-5
lines changed

8 files changed

+1123
-5
lines changed

src/backend/utils/adt/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Makefile for utils/adt
55
#
66
# IDENTIFICATION
7-
# $Header: /cvsroot/pgsql/src/backend/utils/adt/Makefile,v 1.26 1999/09/30 14:54:22 wieck Exp $
7+
# $Header: /cvsroot/pgsql/src/backend/utils/adt/Makefile,v 1.27 1999/11/17 21:21:50 wieck Exp $
88
#
99
#-------------------------------------------------------------------------
1010

@@ -35,7 +35,7 @@ OBJS = acl.o arrayfuncs.o arrayutils.o bool.o cash.o char.o chunk.o \
3535
regexp.o regproc.o ruleutils.o selfuncs.o sets.o \
3636
tid.o timestamp.o varchar.o varlena.o version.o \
3737
network.o mac.o inet_net_ntop.o inet_net_pton.o \
38-
ri_triggers.o
38+
ri_triggers.o pg_lzcompress.o lztext.o
3939

4040
all: SUBSYS.o
4141

src/backend/utils/adt/lztext.c

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
/* ----------
2+
* lztext.c -
3+
*
4+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/lztext.c,v 1.1 1999/11/17 21:21:50 wieck Exp $
5+
*
6+
* Text type with internal LZ compressed representation. Uses the
7+
* standard PostgreSQL compression method.
8+
* ----------
9+
*/
10+
11+
#include <stdio.h>
12+
#include <stdlib.h>
13+
#include <string.h>
14+
#include <math.h>
15+
#include <errno.h>
16+
17+
#include "postgres.h"
18+
#include "utils/builtins.h"
19+
#include "utils/palloc.h"
20+
#include "utils/pg_lzcompress.h"
21+
22+
23+
/* ----------
24+
* lztextin -
25+
*
26+
* Input function for datatype lztext
27+
* ----------
28+
*/
29+
lztext *
30+
lztextin(char *str)
31+
{
32+
lztext *result;
33+
int32 rawsize;
34+
lztext *tmp;
35+
int tmp_size;
36+
37+
/* ----------
38+
* Handle NULL
39+
* ----------
40+
*/
41+
if (str == NULL)
42+
return NULL;
43+
44+
/* ----------
45+
* Determine input size and eventually tuple size
46+
* ----------
47+
*/
48+
rawsize = strlen(str);
49+
tmp_size = PGLZ_MAX_OUTPUT(rawsize);
50+
51+
/* ----------
52+
* Allocate a temporary result and compress into it
53+
* ----------
54+
*/
55+
tmp = (lztext *) palloc(tmp_size);
56+
pglz_compress(str, rawsize, tmp, NULL);
57+
58+
/* ----------
59+
* If we miss less than x% bytes at the end of the temp value,
60+
* so be it. Therefore we save a memcpy().
61+
* ----------
62+
*/
63+
if (tmp_size - tmp->varsize < 256 ||
64+
tmp_size - tmp->varsize < tmp_size / 4)
65+
{
66+
result = tmp;
67+
} else {
68+
result = (lztext *) palloc(tmp->varsize);
69+
memcpy(result, tmp, tmp->varsize);
70+
pfree(tmp);
71+
}
72+
73+
return result;
74+
}
75+
76+
77+
/* ----------
78+
* lztextout -
79+
*
80+
* Output function for data type lztext
81+
* ----------
82+
*/
83+
char *
84+
lztextout(lztext *lz)
85+
{
86+
char *result;
87+
88+
/* ----------
89+
* Handle NULL
90+
* ----------
91+
*/
92+
if (lz == NULL)
93+
{
94+
result = (char *) palloc(2);
95+
result[0] = '-';
96+
result[1] = '\0';
97+
return result;
98+
}
99+
100+
/* ----------
101+
* Allocate the result string - the required size is remembered
102+
* in the lztext header so we don't need a temporary buffer or
103+
* have to diddle with realloc's.
104+
* ----------
105+
*/
106+
result = (char *) palloc(PGLZ_RAW_SIZE(lz) + 1);
107+
108+
/* ----------
109+
* Decompress and add terminating ZERO
110+
* ----------
111+
*/
112+
pglz_decompress(lz, result);
113+
result[lz->rawsize] = '\0';
114+
115+
/* ----------
116+
* Return the result
117+
* ----------
118+
*/
119+
return result;
120+
}
121+
122+
123+
/* ----------
124+
* lztextlen -
125+
*
126+
* Logical length of lztext field (it's the uncompressed size
127+
* of the original data).
128+
* ----------
129+
*/
130+
int32
131+
lztextlen(lztext *lz)
132+
{
133+
/* ----------
134+
* Handle NULL
135+
* ----------
136+
*/
137+
if (lz == NULL)
138+
return 0;
139+
140+
/* ----------
141+
* without multibyte support, it's the remembered rawsize
142+
* ----------
143+
*/
144+
return lz->rawsize;
145+
}
146+
147+
148+
/* ----------
149+
* lztextoctetlen -
150+
*
151+
* Physical length of lztext field (it's the compressed size
152+
* plus the rawsize field).
153+
* ----------
154+
*/
155+
int32
156+
lztextoctetlen(lztext *lz)
157+
{
158+
/* ----------
159+
* Handle NULL
160+
* ----------
161+
*/
162+
if (lz == NULL)
163+
return 0;
164+
165+
/* ----------
166+
* Return the varsize minus the VARSIZE field itself.
167+
* ----------
168+
*/
169+
return lz->varsize - sizeof(int32);
170+
}
171+
172+
173+
/* ----------
174+
* text_lztext -
175+
*
176+
* Convert text to lztext
177+
* ----------
178+
*/
179+
lztext *
180+
text_lztext(text *txt)
181+
{
182+
lztext *result;
183+
int32 rawsize;
184+
lztext *tmp;
185+
int tmp_size;
186+
char *str;
187+
188+
/* ----------
189+
* Handle NULL
190+
* ----------
191+
*/
192+
if (txt == NULL)
193+
return NULL;
194+
195+
/* ----------
196+
* Determine input size and eventually tuple size
197+
* ----------
198+
*/
199+
rawsize = VARSIZE(txt) - VARHDRSZ;
200+
str = VARDATA(txt);
201+
tmp_size = PGLZ_MAX_OUTPUT(rawsize);
202+
203+
/* ----------
204+
* Allocate a temporary result and compress into it
205+
* ----------
206+
*/
207+
tmp = (lztext *) palloc(tmp_size);
208+
pglz_compress(str, rawsize, tmp, NULL);
209+
210+
/* ----------
211+
* If we miss less than x% bytes at the end of the temp value,
212+
* so be it. Therefore we save a memcpy().
213+
* ----------
214+
*/
215+
if (tmp_size - tmp->varsize < 256 ||
216+
tmp_size - tmp->varsize < tmp_size / 4)
217+
{
218+
result = tmp;
219+
} else {
220+
result = (lztext *) palloc(tmp->varsize);
221+
memcpy(result, tmp, tmp->varsize);
222+
pfree(tmp);
223+
}
224+
225+
return result;
226+
227+
228+
}
229+
230+
231+
/* ----------
232+
* lztext_text -
233+
*
234+
* Convert lztext to text
235+
* ----------
236+
*/
237+
text *
238+
lztext_text(lztext *lz)
239+
{
240+
text *result;
241+
242+
/* ----------
243+
* Handle NULL
244+
* ----------
245+
*/
246+
if (lz == NULL)
247+
return NULL;
248+
249+
/* ----------
250+
* Allocate and initialize the text result
251+
* ----------
252+
*/
253+
result = (text *) palloc(lz->rawsize + VARHDRSZ + 1);
254+
VARSIZE(result) = lz->rawsize + VARHDRSZ;
255+
256+
/* ----------
257+
* Decompress directly into the text data area.
258+
* ----------
259+
*/
260+
pglz_decompress(lz, VARDATA(result));
261+
VARDATA(result)[lz->rawsize] = 0;
262+
263+
return result;
264+
}
265+
266+

0 commit comments

Comments
 (0)