|
| 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 | +int32rawsize; |
| 34 | +lztext*tmp; |
| 35 | +inttmp_size; |
| 36 | + |
| 37 | +/* ---------- |
| 38 | + * Handle NULL |
| 39 | + * ---------- |
| 40 | + */ |
| 41 | +if (str==NULL) |
| 42 | +returnNULL; |
| 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 | +returnresult; |
| 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 | +returnresult; |
| 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 | +returnresult; |
| 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 | +return0; |
| 139 | + |
| 140 | +/* ---------- |
| 141 | + * without multibyte support, it's the remembered rawsize |
| 142 | + * ---------- |
| 143 | + */ |
| 144 | +returnlz->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 | +return0; |
| 164 | + |
| 165 | +/* ---------- |
| 166 | + * Return the varsize minus the VARSIZE field itself. |
| 167 | + * ---------- |
| 168 | + */ |
| 169 | +returnlz->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 | +int32rawsize; |
| 184 | +lztext*tmp; |
| 185 | +inttmp_size; |
| 186 | +char*str; |
| 187 | + |
| 188 | +/* ---------- |
| 189 | + * Handle NULL |
| 190 | + * ---------- |
| 191 | + */ |
| 192 | +if (txt==NULL) |
| 193 | +returnNULL; |
| 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 | +returnresult; |
| 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 | +returnNULL; |
| 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 | +returnresult; |
| 264 | +} |
| 265 | + |
| 266 | + |