PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
mac.c File Reference
#include "postgres.h"
#include "access/hash.h"
#include "lib/hyperloglog.h"
#include "libpq/pqformat.h"
#include "port/pg_bswap.h"
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/inet.h"
#include "utils/sortsupport.h"
Include dependency graph for mac.c:

Go to the source code of this file.

Data Structures

struct  macaddr_sortsupport_state
 

Macros

#define hibits(addr)   ((unsigned long)(((addr)->a<<16)|((addr)->b<<8)|((addr)->c)))
 
#define lobits(addr)   ((unsigned long)(((addr)->d<<16)|((addr)->e<<8)|((addr)->f)))
 

Functions

static int macaddr_cmp_internal (macaddr *a1, macaddr *a2)
 
static int macaddr_fast_cmp (Datum x, Datum y, SortSupport ssup)
 
static int macaddr_cmp_abbrev (Datum x, Datum y, SortSupport ssup)
 
static bool macaddr_abbrev_abort (int memtupcount, SortSupport ssup)
 
static Datum macaddr_abbrev_convert (Datum original, SortSupport ssup)
 
Datum macaddr_in (PG_FUNCTION_ARGS)
 
Datum macaddr_out (PG_FUNCTION_ARGS)
 
Datum macaddr_recv (PG_FUNCTION_ARGS)
 
Datum macaddr_send (PG_FUNCTION_ARGS)
 
Datum macaddr_cmp (PG_FUNCTION_ARGS)
 
Datum macaddr_lt (PG_FUNCTION_ARGS)
 
Datum macaddr_le (PG_FUNCTION_ARGS)
 
Datum macaddr_eq (PG_FUNCTION_ARGS)
 
Datum macaddr_ge (PG_FUNCTION_ARGS)
 
Datum macaddr_gt (PG_FUNCTION_ARGS)
 
Datum macaddr_ne (PG_FUNCTION_ARGS)
 
Datum hashmacaddr (PG_FUNCTION_ARGS)
 
Datum macaddr_not (PG_FUNCTION_ARGS)
 
Datum macaddr_and (PG_FUNCTION_ARGS)
 
Datum macaddr_or (PG_FUNCTION_ARGS)
 
Datum macaddr_trunc (PG_FUNCTION_ARGS)
 
Datum macaddr_sortsupport (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

#define hibits (   addr)    ((unsigned long)(((addr)->a<<16)|((addr)->b<<8)|((addr)->c)))

Definition at line 30 of file mac.c.

Referenced by macaddr_cmp_internal().

#define lobits (   addr)    ((unsigned long)(((addr)->d<<16)|((addr)->e<<8)|((addr)->f)))

Definition at line 33 of file mac.c.

Referenced by macaddr_cmp_internal().

Function Documentation

Datum hashmacaddr ( PG_FUNCTION_ARGS  )

Definition at line 267 of file mac.c.

References hash_any(), and PG_GETARG_MACADDR_P.

268 {
269  macaddr *key = PG_GETARG_MACADDR_P(0);
270 
271  return hash_any((unsigned char *) key, sizeof(macaddr));
272 }
#define PG_GETARG_MACADDR_P(n)
Definition: inet.h:133
Datum hash_any(register const unsigned char *k, register int keylen)
Definition: hashfunc.c:307
Definition: inet.h:94
static bool macaddr_abbrev_abort ( int  memtupcount,
SortSupport  ssup 
)
static

Definition at line 422 of file mac.c.

References macaddr_sortsupport_state::abbr_card, elog, estimateHyperLogLog(), macaddr_sortsupport_state::estimating, macaddr_sortsupport_state::input_count, INT64_FORMAT, LOG, SortSupportData::ssup_extra, and trace_sort.

Referenced by macaddr_sortsupport().

423 {
425  double abbr_card;
426 
427  if (memtupcount < 10000 || uss->input_count < 10000 || !uss->estimating)
428  return false;
429 
430  abbr_card = estimateHyperLogLog(&uss->abbr_card);
431 
432  /*
433  * If we have >100k distinct values, then even if we were sorting many
434  * billion rows we'd likely still break even, and the penalty of undoing
435  * that many rows of abbrevs would probably not be worth it. At this point
436  * we stop counting because we know that we're now fully committed.
437  */
438  if (abbr_card > 100000.0)
439  {
440 #ifdef TRACE_SORT
441  if (trace_sort)
442  elog(LOG,
443  "macaddr_abbrev: estimation ends at cardinality %f"
444  " after " INT64_FORMAT " values (%d rows)",
445  abbr_card, uss->input_count, memtupcount);
446 #endif
447  uss->estimating = false;
448  return false;
449  }
450 
451  /*
452  * Target minimum cardinality is 1 per ~2k of non-null inputs. 0.5 row
453  * fudge factor allows us to abort earlier on genuinely pathological data
454  * where we've had exactly one abbreviated value in the first 2k
455  * (non-null) rows.
456  */
457  if (abbr_card < uss->input_count / 2000.0 + 0.5)
458  {
459 #ifdef TRACE_SORT
460  if (trace_sort)
461  elog(LOG,
462  "macaddr_abbrev: aborting abbreviation at cardinality %f"
463  " below threshold %f after " INT64_FORMAT " values (%d rows)",
464  abbr_card, uss->input_count / 2000.0 + 0.5, uss->input_count,
465  memtupcount);
466 #endif
467  return true;
468  }
469 
470 #ifdef TRACE_SORT
471  if (trace_sort)
472  elog(LOG,
473  "macaddr_abbrev: cardinality %f after " INT64_FORMAT
474  " values (%d rows)", abbr_card, uss->input_count, memtupcount);
475 #endif
476 
477  return false;
478 }
#define LOG
Definition: elog.h:26
bool trace_sort
Definition: tuplesort.c:155
void * ssup_extra
Definition: sortsupport.h:87
hyperLogLogState abbr_card
Definition: mac.c:42
double estimateHyperLogLog(hyperLogLogState *cState)
Definition: hyperloglog.c:185
#define INT64_FORMAT
Definition: c.h:315
#define elog
Definition: elog.h:219
static Datum macaddr_abbrev_convert ( Datum  original,
SortSupport  ssup 
)
static

Definition at line 490 of file mac.c.

References macaddr_sortsupport_state::abbr_card, addHyperLogLog(), DatumBigEndianToNative, DatumGetMacaddrP, DatumGetUInt32, macaddr_sortsupport_state::estimating, hash_uint32(), macaddr_sortsupport_state::input_count, SIZEOF_DATUM, and SortSupportData::ssup_extra.

Referenced by macaddr_sortsupport().

491 {
493  macaddr *authoritative = DatumGetMacaddrP(original);
494  Datum res;
495 
496  /*
497  * On a 64-bit machine, zero out the 8-byte datum and copy the 6 bytes of
498  * the MAC address in. There will be two bytes of zero padding on the end
499  * of the least significant bits.
500  */
501 #if SIZEOF_DATUM == 8
502  memset(&res, 0, SIZEOF_DATUM);
503  memcpy(&res, authoritative, sizeof(macaddr));
504 #else /* SIZEOF_DATUM != 8 */
505  memcpy(&res, authoritative, SIZEOF_DATUM);
506 #endif
507  uss->input_count += 1;
508 
509  /*
510  * Cardinality estimation. The estimate uses uint32, so on a 64-bit
511  * architecture, XOR the two 32-bit halves together to produce slightly
512  * more entropy. The two zeroed bytes won't have any practical impact on
513  * this operation.
514  */
515  if (uss->estimating)
516  {
517  uint32 tmp;
518 
519 #if SIZEOF_DATUM == 8
520  tmp = (uint32) res ^ (uint32) ((uint64) res >> 32);
521 #else /* SIZEOF_DATUM != 8 */
522  tmp = (uint32) res;
523 #endif
524 
526  }
527 
528  /*
529  * Byteswap on little-endian machines.
530  *
531  * This is needed so that macaddr_cmp_abbrev() (an unsigned integer 3-way
532  * comparator) works correctly on all platforms. Without this, the
533  * comparator would have to call memcmp() with a pair of pointers to the
534  * first byte of each abbreviated key, which is slower.
535  */
536  res = DatumBigEndianToNative(res);
537 
538  return res;
539 }
#define DatumGetUInt32(X)
Definition: postgres.h:492
#define DatumGetMacaddrP(X)
Definition: inet.h:131
#define DatumBigEndianToNative(x)
Definition: pg_bswap.h:65
unsigned int uint32
Definition: c.h:268
void * ssup_extra
Definition: sortsupport.h:87
Datum hash_uint32(uint32 k)
Definition: hashfunc.c:512
hyperLogLogState abbr_card
Definition: mac.c:42
uintptr_t Datum
Definition: postgres.h:372
void addHyperLogLog(hyperLogLogState *cState, uint32 hash)
Definition: hyperloglog.c:166
Definition: inet.h:94
#define SIZEOF_DATUM
Definition: postgres.h:374
Datum macaddr_and ( PG_FUNCTION_ARGS  )

Definition at line 294 of file mac.c.

References macaddr::a, macaddr::b, macaddr::c, macaddr::d, macaddr::e, macaddr::f, palloc(), PG_GETARG_MACADDR_P, PG_RETURN_MACADDR_P, and result.

295 {
296  macaddr *addr1 = PG_GETARG_MACADDR_P(0);
297  macaddr *addr2 = PG_GETARG_MACADDR_P(1);
298  macaddr *result;
299 
300  result = (macaddr *) palloc(sizeof(macaddr));
301  result->a = addr1->a & addr2->a;
302  result->b = addr1->b & addr2->b;
303  result->c = addr1->c & addr2->c;
304  result->d = addr1->d & addr2->d;
305  result->e = addr1->e & addr2->e;
306  result->f = addr1->f & addr2->f;
307  PG_RETURN_MACADDR_P(result);
308 }
unsigned char f
Definition: inet.h:101
return result
Definition: formatting.c:1618
#define PG_RETURN_MACADDR_P(x)
Definition: inet.h:134
unsigned char c
Definition: inet.h:98
unsigned char a
Definition: inet.h:96
#define PG_GETARG_MACADDR_P(n)
Definition: inet.h:133
unsigned char d
Definition: inet.h:99
unsigned char b
Definition: inet.h:97
unsigned char e
Definition: inet.h:100
Definition: inet.h:94
void * palloc(Size size)
Definition: mcxt.c:849
Datum macaddr_cmp ( PG_FUNCTION_ARGS  )

Definition at line 197 of file mac.c.

References a1, a2, macaddr_cmp_internal(), PG_GETARG_MACADDR_P, and PG_RETURN_INT32.

Referenced by gbt_macadkey_cmp().

198 {
201 
203 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
#define PG_GETARG_MACADDR_P(n)
Definition: inet.h:133
static FormData_pg_attribute a1
Definition: heap.c:145
Definition: inet.h:94
static FormData_pg_attribute a2
Definition: heap.c:151
static int macaddr_cmp_internal(macaddr *a1, macaddr *a2)
Definition: mac.c:182
static int macaddr_cmp_abbrev ( Datum  x,
Datum  y,
SortSupport  ssup 
)
static

Definition at line 405 of file mac.c.

Referenced by macaddr_sortsupport().

406 {
407  if (x > y)
408  return 1;
409  else if (x == y)
410  return 0;
411  else
412  return -1;
413 }
static int macaddr_cmp_internal ( macaddr a1,
macaddr a2 
)
static

Definition at line 182 of file mac.c.

References hibits, and lobits.

Referenced by macaddr_cmp(), macaddr_eq(), macaddr_fast_cmp(), macaddr_ge(), macaddr_gt(), macaddr_le(), macaddr_lt(), and macaddr_ne().

183 {
184  if (hibits(a1) < hibits(a2))
185  return -1;
186  else if (hibits(a1) > hibits(a2))
187  return 1;
188  else if (lobits(a1) < lobits(a2))
189  return -1;
190  else if (lobits(a1) > lobits(a2))
191  return 1;
192  else
193  return 0;
194 }
#define hibits(addr)
Definition: mac.c:30
#define lobits(addr)
Definition: mac.c:33
Datum macaddr_eq ( PG_FUNCTION_ARGS  )

Definition at line 228 of file mac.c.

References a1, a2, macaddr_cmp_internal(), PG_GETARG_MACADDR_P, and PG_RETURN_BOOL.

Referenced by gbt_macadeq().

229 {
232 
233  PG_RETURN_BOOL(macaddr_cmp_internal(a1, a2) == 0);
234 }
#define PG_GETARG_MACADDR_P(n)
Definition: inet.h:133
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static FormData_pg_attribute a1
Definition: heap.c:145
Definition: inet.h:94
static FormData_pg_attribute a2
Definition: heap.c:151
static int macaddr_cmp_internal(macaddr *a1, macaddr *a2)
Definition: mac.c:182
static int macaddr_fast_cmp ( Datum  x,
Datum  y,
SortSupport  ssup 
)
static

Definition at line 391 of file mac.c.

References DatumGetMacaddrP, and macaddr_cmp_internal().

Referenced by macaddr_sortsupport().

392 {
393  macaddr *arg1 = DatumGetMacaddrP(x);
394  macaddr *arg2 = DatumGetMacaddrP(y);
395 
396  return macaddr_cmp_internal(arg1, arg2);
397 }
#define DatumGetMacaddrP(X)
Definition: inet.h:131
Definition: inet.h:94
static int macaddr_cmp_internal(macaddr *a1, macaddr *a2)
Definition: mac.c:182
Datum macaddr_ge ( PG_FUNCTION_ARGS  )

Definition at line 237 of file mac.c.

References a1, a2, macaddr_cmp_internal(), PG_GETARG_MACADDR_P, and PG_RETURN_BOOL.

Referenced by gbt_macadge().

238 {
241 
242  PG_RETURN_BOOL(macaddr_cmp_internal(a1, a2) >= 0);
243 }
#define PG_GETARG_MACADDR_P(n)
Definition: inet.h:133
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static FormData_pg_attribute a1
Definition: heap.c:145
Definition: inet.h:94
static FormData_pg_attribute a2
Definition: heap.c:151
static int macaddr_cmp_internal(macaddr *a1, macaddr *a2)
Definition: mac.c:182
Datum macaddr_gt ( PG_FUNCTION_ARGS  )

Definition at line 246 of file mac.c.

References a1, a2, macaddr_cmp_internal(), PG_GETARG_MACADDR_P, and PG_RETURN_BOOL.

Referenced by gbt_macadgt().

247 {
250 
252 }
#define PG_GETARG_MACADDR_P(n)
Definition: inet.h:133
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static FormData_pg_attribute a1
Definition: heap.c:145
Definition: inet.h:94
static FormData_pg_attribute a2
Definition: heap.c:151
static int macaddr_cmp_internal(macaddr *a1, macaddr *a2)
Definition: mac.c:182
Datum macaddr_in ( PG_FUNCTION_ARGS  )

Definition at line 56 of file mac.c.

References macaddr::a, macaddr::b, macaddr::c, macaddr::d, macaddr::e, ereport, errcode(), errmsg(), ERROR, macaddr::f, palloc(), PG_GETARG_CSTRING, PG_RETURN_MACADDR_P, and result.

57 {
58  char *str = PG_GETARG_CSTRING(0);
59  macaddr *result;
60  int a,
61  b,
62  c,
63  d,
64  e,
65  f;
66  char junk[2];
67  int count;
68 
69  /* %1s matches iff there is trailing non-whitespace garbage */
70 
71  count = sscanf(str, "%x:%x:%x:%x:%x:%x%1s",
72  &a, &b, &c, &d, &e, &f, junk);
73  if (count != 6)
74  count = sscanf(str, "%x-%x-%x-%x-%x-%x%1s",
75  &a, &b, &c, &d, &e, &f, junk);
76  if (count != 6)
77  count = sscanf(str, "%2x%2x%2x:%2x%2x%2x%1s",
78  &a, &b, &c, &d, &e, &f, junk);
79  if (count != 6)
80  count = sscanf(str, "%2x%2x%2x-%2x%2x%2x%1s",
81  &a, &b, &c, &d, &e, &f, junk);
82  if (count != 6)
83  count = sscanf(str, "%2x%2x.%2x%2x.%2x%2x%1s",
84  &a, &b, &c, &d, &e, &f, junk);
85  if (count != 6)
86  count = sscanf(str, "%2x%2x-%2x%2x-%2x%2x%1s",
87  &a, &b, &c, &d, &e, &f, junk);
88  if (count != 6)
89  count = sscanf(str, "%2x%2x%2x%2x%2x%2x%1s",
90  &a, &b, &c, &d, &e, &f, junk);
91  if (count != 6)
92  ereport(ERROR,
93  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
94  errmsg("invalid input syntax for type %s: \"%s\"", "macaddr",
95  str)));
96 
97  if ((a < 0) || (a > 255) || (b < 0) || (b > 255) ||
98  (c < 0) || (c > 255) || (d < 0) || (d > 255) ||
99  (e < 0) || (e > 255) || (f < 0) || (f > 255))
100  ereport(ERROR,
101  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
102  errmsg("invalid octet value in \"macaddr\" value: \"%s\"", str)));
103 
104  result = (macaddr *) palloc(sizeof(macaddr));
105 
106  result->a = a;
107  result->b = b;
108  result->c = c;
109  result->d = d;
110  result->e = e;
111  result->f = f;
112 
113  PG_RETURN_MACADDR_P(result);
114 }
unsigned char f
Definition: inet.h:101
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
#define PG_RETURN_MACADDR_P(x)
Definition: inet.h:134
unsigned char c
Definition: inet.h:98
#define ERROR
Definition: elog.h:43
unsigned char a
Definition: inet.h:96
unsigned char d
Definition: inet.h:99
char * c
#define ereport(elevel, rest)
Definition: elog.h:122
unsigned char b
Definition: inet.h:97
unsigned char e
Definition: inet.h:100
Definition: inet.h:94
e
Definition: preproc-init.c:82
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:242
Datum macaddr_le ( PG_FUNCTION_ARGS  )

Definition at line 219 of file mac.c.

References a1, a2, macaddr_cmp_internal(), PG_GETARG_MACADDR_P, and PG_RETURN_BOOL.

Referenced by gbt_macadle().

220 {
223 
224  PG_RETURN_BOOL(macaddr_cmp_internal(a1, a2) <= 0);
225 }
#define PG_GETARG_MACADDR_P(n)
Definition: inet.h:133
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static FormData_pg_attribute a1
Definition: heap.c:145
Definition: inet.h:94
static FormData_pg_attribute a2
Definition: heap.c:151
static int macaddr_cmp_internal(macaddr *a1, macaddr *a2)
Definition: mac.c:182
Datum macaddr_lt ( PG_FUNCTION_ARGS  )

Definition at line 210 of file mac.c.

References a1, a2, macaddr_cmp_internal(), PG_GETARG_MACADDR_P, and PG_RETURN_BOOL.

Referenced by gbt_macadlt().

211 {
214 
216 }
#define PG_GETARG_MACADDR_P(n)
Definition: inet.h:133
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static FormData_pg_attribute a1
Definition: heap.c:145
Definition: inet.h:94
static FormData_pg_attribute a2
Definition: heap.c:151
static int macaddr_cmp_internal(macaddr *a1, macaddr *a2)
Definition: mac.c:182
Datum macaddr_ne ( PG_FUNCTION_ARGS  )

Definition at line 255 of file mac.c.

References a1, a2, macaddr_cmp_internal(), PG_GETARG_MACADDR_P, and PG_RETURN_BOOL.

256 {
259 
260  PG_RETURN_BOOL(macaddr_cmp_internal(a1, a2) != 0);
261 }
#define PG_GETARG_MACADDR_P(n)
Definition: inet.h:133
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static FormData_pg_attribute a1
Definition: heap.c:145
Definition: inet.h:94
static FormData_pg_attribute a2
Definition: heap.c:151
static int macaddr_cmp_internal(macaddr *a1, macaddr *a2)
Definition: mac.c:182
Datum macaddr_not ( PG_FUNCTION_ARGS  )

Definition at line 278 of file mac.c.

References macaddr::a, macaddr::b, macaddr::c, macaddr::d, macaddr::e, macaddr::f, palloc(), PG_GETARG_MACADDR_P, PG_RETURN_MACADDR_P, and result.

279 {
280  macaddr *addr = PG_GETARG_MACADDR_P(0);
281  macaddr *result;
282 
283  result = (macaddr *) palloc(sizeof(macaddr));
284  result->a = ~addr->a;
285  result->b = ~addr->b;
286  result->c = ~addr->c;
287  result->d = ~addr->d;
288  result->e = ~addr->e;
289  result->f = ~addr->f;
290  PG_RETURN_MACADDR_P(result);
291 }
unsigned char f
Definition: inet.h:101
return result
Definition: formatting.c:1618
#define PG_RETURN_MACADDR_P(x)
Definition: inet.h:134
unsigned char c
Definition: inet.h:98
unsigned char a
Definition: inet.h:96
#define PG_GETARG_MACADDR_P(n)
Definition: inet.h:133
unsigned char d
Definition: inet.h:99
unsigned char b
Definition: inet.h:97
unsigned char e
Definition: inet.h:100
Definition: inet.h:94
void * palloc(Size size)
Definition: mcxt.c:849
Datum macaddr_or ( PG_FUNCTION_ARGS  )

Definition at line 311 of file mac.c.

References macaddr::a, macaddr::b, macaddr::c, macaddr::d, macaddr::e, macaddr::f, palloc(), PG_GETARG_MACADDR_P, PG_RETURN_MACADDR_P, and result.

312 {
313  macaddr *addr1 = PG_GETARG_MACADDR_P(0);
314  macaddr *addr2 = PG_GETARG_MACADDR_P(1);
315  macaddr *result;
316 
317  result = (macaddr *) palloc(sizeof(macaddr));
318  result->a = addr1->a | addr2->a;
319  result->b = addr1->b | addr2->b;
320  result->c = addr1->c | addr2->c;
321  result->d = addr1->d | addr2->d;
322  result->e = addr1->e | addr2->e;
323  result->f = addr1->f | addr2->f;
324  PG_RETURN_MACADDR_P(result);
325 }
unsigned char f
Definition: inet.h:101
return result
Definition: formatting.c:1618
#define PG_RETURN_MACADDR_P(x)
Definition: inet.h:134
unsigned char c
Definition: inet.h:98
unsigned char a
Definition: inet.h:96
#define PG_GETARG_MACADDR_P(n)
Definition: inet.h:133
unsigned char d
Definition: inet.h:99
unsigned char b
Definition: inet.h:97
unsigned char e
Definition: inet.h:100
Definition: inet.h:94
void * palloc(Size size)
Definition: mcxt.c:849
Datum macaddr_out ( PG_FUNCTION_ARGS  )

Definition at line 121 of file mac.c.

References macaddr::a, macaddr::b, macaddr::c, macaddr::d, macaddr::e, macaddr::f, palloc(), PG_GETARG_MACADDR_P, PG_RETURN_CSTRING, result, and snprintf().

122 {
123  macaddr *addr = PG_GETARG_MACADDR_P(0);
124  char *result;
125 
126  result = (char *) palloc(32);
127 
128  snprintf(result, 32, "%02x:%02x:%02x:%02x:%02x:%02x",
129  addr->a, addr->b, addr->c, addr->d, addr->e, addr->f);
130 
131  PG_RETURN_CSTRING(result);
132 }
unsigned char f
Definition: inet.h:101
return result
Definition: formatting.c:1618
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
unsigned char c
Definition: inet.h:98
unsigned char a
Definition: inet.h:96
#define PG_GETARG_MACADDR_P(n)
Definition: inet.h:133
unsigned char d
Definition: inet.h:99
unsigned char b
Definition: inet.h:97
unsigned char e
Definition: inet.h:100
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:322
Definition: inet.h:94
void * palloc(Size size)
Definition: mcxt.c:849
Datum macaddr_recv ( PG_FUNCTION_ARGS  )

Definition at line 140 of file mac.c.

References macaddr::a, macaddr::b, buf, macaddr::c, macaddr::d, macaddr::e, macaddr::f, palloc(), PG_GETARG_POINTER, PG_RETURN_MACADDR_P, and pq_getmsgbyte().

141 {
143  macaddr *addr;
144 
145  addr = (macaddr *) palloc(sizeof(macaddr));
146 
147  addr->a = pq_getmsgbyte(buf);
148  addr->b = pq_getmsgbyte(buf);
149  addr->c = pq_getmsgbyte(buf);
150  addr->d = pq_getmsgbyte(buf);
151  addr->e = pq_getmsgbyte(buf);
152  addr->f = pq_getmsgbyte(buf);
153 
154  PG_RETURN_MACADDR_P(addr);
155 }
unsigned char f
Definition: inet.h:101
StringInfoData * StringInfo
Definition: stringinfo.h:46
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
#define PG_RETURN_MACADDR_P(x)
Definition: inet.h:134
unsigned char c
Definition: inet.h:98
unsigned char a
Definition: inet.h:96
unsigned char d
Definition: inet.h:99
static char * buf
Definition: pg_test_fsync.c:66
unsigned char b
Definition: inet.h:97
unsigned char e
Definition: inet.h:100
int pq_getmsgbyte(StringInfo msg)
Definition: pqformat.c:432
Definition: inet.h:94
void * palloc(Size size)
Definition: mcxt.c:849
Datum macaddr_send ( PG_FUNCTION_ARGS  )

Definition at line 161 of file mac.c.

References macaddr::a, macaddr::b, buf, macaddr::c, macaddr::d, macaddr::e, macaddr::f, PG_GETARG_MACADDR_P, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), and pq_sendbyte().

162 {
163  macaddr *addr = PG_GETARG_MACADDR_P(0);
165 
166  pq_begintypsend(&buf);
167  pq_sendbyte(&buf, addr->a);
168  pq_sendbyte(&buf, addr->b);
169  pq_sendbyte(&buf, addr->c);
170  pq_sendbyte(&buf, addr->d);
171  pq_sendbyte(&buf, addr->e);
172  pq_sendbyte(&buf, addr->f);
174 }
void pq_sendbyte(StringInfo buf, int byt)
Definition: pqformat.c:105
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:359
unsigned char f
Definition: inet.h:101
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:329
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:379
unsigned char c
Definition: inet.h:98
unsigned char a
Definition: inet.h:96
#define PG_GETARG_MACADDR_P(n)
Definition: inet.h:133
unsigned char d
Definition: inet.h:99
static char * buf
Definition: pg_test_fsync.c:66
unsigned char b
Definition: inet.h:97
unsigned char e
Definition: inet.h:100
Definition: inet.h:94
Datum macaddr_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 354 of file mac.c.

References macaddr_sortsupport_state::abbr_card, SortSupportData::abbrev_abort, SortSupportData::abbrev_converter, SortSupportData::abbrev_full_comparator, SortSupportData::abbreviate, SortSupportData::comparator, macaddr_sortsupport_state::estimating, initHyperLogLog(), macaddr_sortsupport_state::input_count, macaddr_abbrev_abort(), macaddr_abbrev_convert(), macaddr_cmp_abbrev(), macaddr_fast_cmp(), MemoryContextSwitchTo(), NULL, palloc(), PG_GETARG_POINTER, PG_RETURN_VOID, SortSupportData::ssup_cxt, and SortSupportData::ssup_extra.

355 {
357 
359  ssup->ssup_extra = NULL;
360 
361  if (ssup->abbreviate)
362  {
364  MemoryContext oldcontext;
365 
366  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
367 
368  uss = palloc(sizeof(macaddr_sortsupport_state));
369  uss->input_count = 0;
370  uss->estimating = true;
371  initHyperLogLog(&uss->abbr_card, 10);
372 
373  ssup->ssup_extra = uss;
374 
379 
380  MemoryContextSwitchTo(oldcontext);
381  }
382 
383  PG_RETURN_VOID();
384 }
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:107
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
void initHyperLogLog(hyperLogLogState *cState, uint8 bwidth)
Definition: hyperloglog.c:65
MemoryContext ssup_cxt
Definition: sortsupport.h:66
int(* abbrev_full_comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:192
void * ssup_extra
Definition: sortsupport.h:87
hyperLogLogState abbr_card
Definition: mac.c:42
static int macaddr_cmp_abbrev(Datum x, Datum y, SortSupport ssup)
Definition: mac.c:405
#define PG_RETURN_VOID()
Definition: fmgr.h:309
#define NULL
Definition: c.h:229
static int macaddr_fast_cmp(Datum x, Datum y, SortSupport ssup)
Definition: mac.c:391
void * palloc(Size size)
Definition: mcxt.c:849
bool(* abbrev_abort)(int memtupcount, SortSupport ssup)
Definition: sortsupport.h:183
Datum(* abbrev_converter)(Datum original, SortSupport ssup)
Definition: sortsupport.h:173
static bool macaddr_abbrev_abort(int memtupcount, SortSupport ssup)
Definition: mac.c:422
static Datum macaddr_abbrev_convert(Datum original, SortSupport ssup)
Definition: mac.c:490
Datum macaddr_trunc ( PG_FUNCTION_ARGS  )

Definition at line 332 of file mac.c.

References macaddr::a, macaddr::b, macaddr::c, macaddr::d, macaddr::e, macaddr::f, palloc(), PG_GETARG_MACADDR_P, PG_RETURN_MACADDR_P, and result.

333 {
334  macaddr *addr = PG_GETARG_MACADDR_P(0);
335  macaddr *result;
336 
337  result = (macaddr *) palloc(sizeof(macaddr));
338 
339  result->a = addr->a;
340  result->b = addr->b;
341  result->c = addr->c;
342  result->d = 0;
343  result->e = 0;
344  result->f = 0;
345 
346  PG_RETURN_MACADDR_P(result);
347 }
unsigned char f
Definition: inet.h:101
return result
Definition: formatting.c:1618
#define PG_RETURN_MACADDR_P(x)
Definition: inet.h:134
unsigned char c
Definition: inet.h:98
unsigned char a
Definition: inet.h:96
#define PG_GETARG_MACADDR_P(n)
Definition: inet.h:133
unsigned char d
Definition: inet.h:99
unsigned char b
Definition: inet.h:97
unsigned char e
Definition: inet.h:100
Definition: inet.h:94
void * palloc(Size size)
Definition: mcxt.c:849