PostgreSQL Source Code  git master
compression.c File Reference
#include "postgres.h"
#include "common/compression.h"
Include dependency graph for compression.c:

Go to the source code of this file.

Functions

static int expect_integer_value (char *keyword, char *value, pg_compress_specification *result)
 
bool parse_compress_algorithm (char *name, pg_compress_algorithm *algorithm)
 
const char * get_compress_algorithm_name (pg_compress_algorithm algorithm)
 
void parse_compress_specification (pg_compress_algorithm algorithm, char *specification, pg_compress_specification *result)
 
char * validate_compress_specification (pg_compress_specification *spec)
 

Function Documentation

◆ expect_integer_value()

static int expect_integer_value ( char *  keyword,
char *  value,
pg_compress_specification result 
)
static

Definition at line 218 of file compression.c.

219 {
220  int ivalue;
221  char *ivalue_endp;
222 
223  if (value == NULL)
224  {
225  result->parse_error =
226  psprintf(_("compression option \"%s\" requires a value"),
227  keyword);
228  return -1;
229  }
230 
231  ivalue = strtol(value, &ivalue_endp, 10);
232  if (ivalue_endp == value || *ivalue_endp != '\0')
233  {
234  result->parse_error =
235  psprintf(_("value for compression option \"%s\" must be an integer"),
236  keyword);
237  return -1;
238  }
239  return ivalue;
240 }
#define _(x)
Definition: elog.c:89
static struct @151 value
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46

References _, pg_compress_specification::parse_error, psprintf(), and value.

Referenced by parse_compress_specification().

◆ get_compress_algorithm_name()

const char* get_compress_algorithm_name ( pg_compress_algorithm  algorithm)

Definition at line 60 of file compression.c.

61 {
62  switch (algorithm)
63  {
65  return "none";
67  return "gzip";
68  case PG_COMPRESSION_LZ4:
69  return "lz4";
71  return "zstd";
72  /* no default, to provoke compiler warnings if values are added */
73  }
74  Assert(false);
75  return "???"; /* placate compiler */
76 }
@ PG_COMPRESSION_GZIP
Definition: compression.h:20
@ PG_COMPRESSION_LZ4
Definition: compression.h:21
@ PG_COMPRESSION_NONE
Definition: compression.h:19
@ PG_COMPRESSION_ZSTD
Definition: compression.h:22
Assert(fmt[strlen(fmt) - 1] !='\n')

References Assert(), PG_COMPRESSION_GZIP, PG_COMPRESSION_LZ4, PG_COMPRESSION_NONE, and PG_COMPRESSION_ZSTD.

Referenced by validate_compress_specification().

◆ parse_compress_algorithm()

bool parse_compress_algorithm ( char *  name,
pg_compress_algorithm algorithm 
)

Definition at line 40 of file compression.c.

41 {
42  if (strcmp(name, "none") == 0)
43  *algorithm = PG_COMPRESSION_NONE;
44  else if (strcmp(name, "gzip") == 0)
45  *algorithm = PG_COMPRESSION_GZIP;
46  else if (strcmp(name, "lz4") == 0)
47  *algorithm = PG_COMPRESSION_LZ4;
48  else if (strcmp(name, "zstd") == 0)
49  *algorithm = PG_COMPRESSION_ZSTD;
50  else
51  return false;
52  return true;
53 }
const char * name
Definition: encode.c:561

References name, PG_COMPRESSION_GZIP, PG_COMPRESSION_LZ4, PG_COMPRESSION_NONE, and PG_COMPRESSION_ZSTD.

Referenced by main(), and parse_basebackup_options().

◆ parse_compress_specification()

void parse_compress_specification ( pg_compress_algorithm  algorithm,
char *  specification,
pg_compress_specification result 
)

Definition at line 95 of file compression.c.

97 {
98  int bare_level;
99  char *bare_level_endp;
100 
101  /* Initial setup of result object. */
102  result->algorithm = algorithm;
103  result->options = 0;
104  result->level = -1;
105  result->parse_error = NULL;
106 
107  /* If there is no specification, we're done already. */
108  if (specification == NULL)
109  return;
110 
111  /* As a special case, the specification can be a bare integer. */
112  bare_level = strtol(specification, &bare_level_endp, 10);
113  if (specification != bare_level_endp && *bare_level_endp == '\0')
114  {
115  result->level = bare_level;
117  return;
118  }
119 
120  /* Look for comma-separated keyword or keyword=value entries. */
121  while (1)
122  {
123  char *kwstart;
124  char *kwend;
125  char *vstart;
126  char *vend;
127  int kwlen;
128  int vlen;
129  bool has_value;
130  char *keyword;
131  char *value;
132 
133  /* Figure start, end, and length of next keyword and any value. */
134  kwstart = kwend = specification;
135  while (*kwend != '\0' && *kwend != ',' && *kwend != '=')
136  ++kwend;
137  kwlen = kwend - kwstart;
138  if (*kwend != '=')
139  {
140  vstart = vend = NULL;
141  vlen = 0;
142  has_value = false;
143  }
144  else
145  {
146  vstart = vend = kwend + 1;
147  while (*vend != '\0' && *vend != ',')
148  ++vend;
149  vlen = vend - vstart;
150  has_value = true;
151  }
152 
153  /* Reject empty keyword. */
154  if (kwlen == 0)
155  {
156  result->parse_error =
157  pstrdup(_("found empty string where a compression option was expected"));
158  break;
159  }
160 
161  /* Extract keyword and value as separate C strings. */
162  keyword = palloc(kwlen + 1);
163  memcpy(keyword, kwstart, kwlen);
164  keyword[kwlen] = '\0';
165  if (!has_value)
166  value = NULL;
167  else
168  {
169  value = palloc(vlen + 1);
170  memcpy(value, vstart, vlen);
171  value[vlen] = '\0';
172  }
173 
174  /* Handle whatever keyword we found. */
175  if (strcmp(keyword, "level") == 0)
176  {
177  result->level = expect_integer_value(keyword, value, result);
179  }
180  else if (strcmp(keyword, "workers") == 0)
181  {
182  result->workers = expect_integer_value(keyword, value, result);
184  }
185  else
186  result->parse_error =
187  psprintf(_("unknown compression option \"%s\""), keyword);
188 
189  /* Release memory, just to be tidy. */
190  pfree(keyword);
191  if (value != NULL)
192  pfree(value);
193 
194  /*
195  * If we got an error or have reached the end of the string, stop.
196  *
197  * If there is no value, then the end of the keyword might have been
198  * the end of the string. If there is a value, then the end of the
199  * keyword cannot have been the end of the string, but the end of the
200  * value might have been.
201  */
202  if (result->parse_error != NULL ||
203  (vend == NULL ? *kwend == '\0' : *vend == '\0'))
204  break;
205 
206  /* Advance to next entry and loop around. */
207  specification = vend == NULL ? kwend + 1 : vend + 1;
208  }
209 }
static int expect_integer_value(char *keyword, char *value, pg_compress_specification *result)
Definition: compression.c:218
#define PG_COMPRESSION_OPTION_LEVEL
Definition: compression.h:25
#define PG_COMPRESSION_OPTION_WORKERS
Definition: compression.h:26
char * pstrdup(const char *in)
Definition: mcxt.c:1305
void pfree(void *pointer)
Definition: mcxt.c:1175
void * palloc(Size size)
Definition: mcxt.c:1068
pg_compress_algorithm algorithm
Definition: compression.h:30

References _, pg_compress_specification::algorithm, expect_integer_value(), pg_compress_specification::level, pg_compress_specification::options, palloc(), pg_compress_specification::parse_error, pfree(), PG_COMPRESSION_OPTION_LEVEL, PG_COMPRESSION_OPTION_WORKERS, psprintf(), pstrdup(), value, and pg_compress_specification::workers.

Referenced by main(), and parse_basebackup_options().

◆ validate_compress_specification()

char* validate_compress_specification ( pg_compress_specification spec)

Definition at line 250 of file compression.c.

251 {
252  /* If it didn't even parse OK, it's definitely no good. */
253  if (spec->parse_error != NULL)
254  return spec->parse_error;
255 
256  /*
257  * If a compression level was specified, check that the algorithm expects
258  * a compression level and that the level is within the legal range for
259  * the algorithm.
260  */
261  if ((spec->options & PG_COMPRESSION_OPTION_LEVEL) != 0)
262  {
263  int min_level = 1;
264  int max_level;
265 
266  if (spec->algorithm == PG_COMPRESSION_GZIP)
267  max_level = 9;
268  else if (spec->algorithm == PG_COMPRESSION_LZ4)
269  max_level = 12;
270  else if (spec->algorithm == PG_COMPRESSION_ZSTD)
271  max_level = 22;
272  else
273  return psprintf(_("compression algorithm \"%s\" does not accept a compression level"),
275 
276  if (spec->level < min_level || spec->level > max_level)
277  return psprintf(_("compression algorithm \"%s\" expects a compression level between %d and %d"),
279  min_level, max_level);
280  }
281 
282  /*
283  * Of the compression algorithms that we currently support, only zstd
284  * allows parallel workers.
285  */
286  if ((spec->options & PG_COMPRESSION_OPTION_WORKERS) != 0 &&
287  (spec->algorithm != PG_COMPRESSION_ZSTD))
288  {
289  return psprintf(_("compression algorithm \"%s\" does not accept a worker count"),
291  }
292 
293  return NULL;
294 }
const char * get_compress_algorithm_name(pg_compress_algorithm algorithm)
Definition: compression.c:60

References _, pg_compress_specification::algorithm, get_compress_algorithm_name(), pg_compress_specification::level, pg_compress_specification::options, pg_compress_specification::parse_error, PG_COMPRESSION_GZIP, PG_COMPRESSION_LZ4, PG_COMPRESSION_OPTION_LEVEL, PG_COMPRESSION_OPTION_WORKERS, PG_COMPRESSION_ZSTD, and psprintf().

Referenced by main(), and parse_basebackup_options().