venum-poc.patch
text/x-patch
Patch
Same data as JSON:
GET /api/v1/attachments/:id/patch
the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes.
API reference →
Format: context
| File | + | − |
|---|---|---|
| enum.c | 123 | 7 |
Index: enum.c
===================================================================
RCS file: /cvsroot/pgsql/src/backend/utils/adt/enum.c,v
retrieving revision 1.11
diff -c -r1.11 enum.c
*** enum.c 26 Feb 2010 02:01:08 -0000 1.11
--- enum.c 22 Jun 2010 02:16:48 -0000
***************
*** 14,19 ****
--- 14,20 ----
#include "postgres.h"
#include "catalog/pg_enum.h"
+ #include "catalog/pg_type.h"
#include "fmgr.h"
#include "utils/array.h"
#include "utils/builtins.h"
***************
*** 22,27 ****
--- 23,52 ----
#include "libpq/pqformat.h"
#include "miscadmin.h"
+ typedef struct
+ {
+ Oid enum_oid;
+ uint32 sort_order;
+ } enum_sort;
+
+ typedef struct
+ {
+ bool oids_are_sorted;
+ int sort_list_length;
+ enum_sort sort_order_list[1024];
+ } enum_sort_cache;
+
+
+ static int
+ enum_sort_cmp(void * es1, void * es2)
+ {
+ enum_sort *p1, *p2;
+ p1 = (enum_sort *)es1;
+ p2 = (enum_sort *)es2;
+ return p1->enum_oid - p2->enum_oid;
+ }
+
+
static ArrayType *enum_range_internal(Oid enumtypoid, Oid lower, Oid upper);
static int enum_elem_cmp(const void *left, const void *right);
***************
*** 155,167 ****
/* Comparison functions and related */
Datum
enum_lt(PG_FUNCTION_ARGS)
{
Oid a = PG_GETARG_OID(0);
Oid b = PG_GETARG_OID(1);
! PG_RETURN_BOOL(a < b);
}
Datum
--- 180,283 ----
/* Comparison functions and related */
+ static inline int
+ enum_ccmp(Oid arg1, Oid arg2, FunctionCallInfo fcinfo)
+ {
+
+ enum_sort_cache * mycache;
+ enum_sort *es1, *es2;
+ int sort1, sort2;
+ bool added = false;
+ HeapTuple tup;
+ Form_pg_enum en;
+ Oid typeoid;
+ Form_pg_type typ;
+
+ if (arg1 == arg2)
+ return 0;
+
+ mycache = (enum_sort_cache *) fcinfo->flinfo->fn_extra;
+ if (mycache == NULL )
+ {
+ fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
+ sizeof(enum_sort_cache));
+ mycache = (enum_sort_cache *) fcinfo->flinfo->fn_extra;
+ mycache->sort_list_length = 1;
+ tup = SearchSysCache1(ENUMOID, ObjectIdGetDatum(arg1));
+ en = (Form_pg_enum) GETSTRUCT(tup);
+ mycache->sort_order_list[0].enum_oid = arg1;
+ mycache->sort_order_list[0].sort_order = arg1;
+ typeoid = en->enumtypid;
+ ReleaseSysCache(tup);
+ tup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typeoid));
+ typ = (Form_pg_type) GETSTRUCT(tup);
+ if (typ->typtype != 'e')
+ elog(ERROR,"wrong type for oid %u",typeoid);
+ /* XXX TODO fill in oids_are_sorted property from type tuple here */
+ mycache->oids_are_sorted = false;
+ ReleaseSysCache(tup);
+ }
+
+ if (mycache->oids_are_sorted)
+ return arg1 - arg2;
+
+ es1 = bsearch(&arg1,mycache->sort_order_list,mycache->sort_list_length,
+ sizeof(enum_sort),enum_sort_cmp);
+ es2 = bsearch(&arg2,mycache->sort_order_list,mycache->sort_list_length,
+ sizeof(enum_sort),enum_sort_cmp);
+
+ if (es1 == NULL)
+ {
+
+ tup = SearchSysCache1(ENUMOID, ObjectIdGetDatum(arg1));
+ en = (Form_pg_enum) GETSTRUCT(tup);
+ mycache->sort_order_list[mycache->sort_list_length].enum_oid = arg1;
+ sort1 = arg1; /* XXX should be en->enumsort */
+ mycache->sort_order_list[mycache->sort_list_length].sort_order =
+ sort1;
+ ReleaseSysCache(tup);
+ mycache->sort_list_length ++;
+ added = true;
+ }
+ else
+ {
+ /* already in cache */
+ sort1 = es1->sort_order;
+ }
+
+ if (es2 == NULL)
+ {
+
+ tup = SearchSysCache1(ENUMOID, ObjectIdGetDatum(arg2));
+ en = (Form_pg_enum) GETSTRUCT(tup);
+ sort2 = arg2; /* XXX should be en->enumsort */
+ mycache->sort_order_list[mycache->sort_list_length].enum_oid = arg2;
+ mycache->sort_order_list[mycache->sort_list_length].sort_order =
+ sort2;
+ ReleaseSysCache(tup);
+ mycache->sort_list_length ++;
+ added = true;
+ }
+ else
+ {
+ /* already in cache */
+ sort2 = es2->sort_order;
+ }
+
+ if (added)
+ qsort(mycache->sort_order_list,mycache->sort_list_length,
+ sizeof(enum_sort),enum_sort_cmp);
+
+ return sort1 - sort2;
+ }
+
Datum
enum_lt(PG_FUNCTION_ARGS)
{
Oid a = PG_GETARG_OID(0);
Oid b = PG_GETARG_OID(1);
! PG_RETURN_BOOL(enum_ccmp(a,b,fcinfo) < 0);
}
Datum
***************
*** 170,176 ****
Oid a = PG_GETARG_OID(0);
Oid b = PG_GETARG_OID(1);
! PG_RETURN_BOOL(a <= b);
}
Datum
--- 286,292 ----
Oid a = PG_GETARG_OID(0);
Oid b = PG_GETARG_OID(1);
! PG_RETURN_BOOL(enum_ccmp(a,b,fcinfo) <= 0);
}
Datum
***************
*** 197,203 ****
Oid a = PG_GETARG_OID(0);
Oid b = PG_GETARG_OID(1);
! PG_RETURN_BOOL(a >= b);
}
Datum
--- 313,319 ----
Oid a = PG_GETARG_OID(0);
Oid b = PG_GETARG_OID(1);
! PG_RETURN_BOOL(enum_ccmp(a,b,fcinfo) >= 0);
}
Datum
***************
*** 206,212 ****
Oid a = PG_GETARG_OID(0);
Oid b = PG_GETARG_OID(1);
! PG_RETURN_BOOL(a > b);
}
Datum
--- 322,328 ----
Oid a = PG_GETARG_OID(0);
Oid b = PG_GETARG_OID(1);
! PG_RETURN_BOOL(enum_ccmp(a,b,fcinfo) > 0);
}
Datum
***************
*** 233,242 ****
Oid a = PG_GETARG_OID(0);
Oid b = PG_GETARG_OID(1);
! if (a > b)
! PG_RETURN_INT32(1);
! else if (a == b)
PG_RETURN_INT32(0);
else
PG_RETURN_INT32(-1);
}
--- 349,358 ----
Oid a = PG_GETARG_OID(0);
Oid b = PG_GETARG_OID(1);
! if (a == b)
PG_RETURN_INT32(0);
+ else if (enum_ccmp(a,b,fcinfo) > 0)
+ PG_RETURN_INT32(1);
else
PG_RETURN_INT32(-1);
}