Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Importing prefix operator and opclass files

  • Loading branch information...
commit 0fc1320e8b7fafc7b061c0c74112df7b39141333 0 parents
dim authored
11 Makefile
@@ -0,0 +1,11 @@
+
+MODULES = prefix
+DATA_built = prefix.sql
+DOCS = README.txt
+
+PGXS = $(shell pg_config --pgxs)
+include $(PGXS)
+
+html: $(DOCS)
+ asciidoc -a toc $<
+
445 README.html
@@ -0,0 +1,445 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
+ "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+<meta name="generator" content="AsciiDoc 8.2.2" />
+<style type="text/css">
+/* Debug borders */
+p, li, dt, dd, div, pre, h1, h2, h3, h4, h5, h6 {
+/*
+ border: 1px solid red;
+*/
+}
+
+body {
+ margin: 1em 5% 1em 5%;
+}
+
+a {
+ color: blue;
+ text-decoration: underline;
+}
+a:visited {
+ color: fuchsia;
+}
+
+em {
+ font-style: italic;
+}
+
+strong {
+ font-weight: bold;
+}
+
+tt {
+ color: navy;
+}
+
+h1, h2, h3, h4, h5, h6 {
+ color: #527bbd;
+ font-family: sans-serif;
+ margin-top: 1.2em;
+ margin-bottom: 0.5em;
+ line-height: 1.3;
+}
+
+h1 {
+ border-bottom: 2px solid silver;
+}
+h2 {
+ border-bottom: 2px solid silver;
+ padding-top: 0.5em;
+}
+
+div.sectionbody {
+ font-family: serif;
+ margin-left: 0;
+}
+
+hr {
+ border: 1px solid silver;
+}
+
+p {
+ margin-top: 0.5em;
+ margin-bottom: 0.5em;
+}
+
+pre {
+ padding: 0;
+ margin: 0;
+}
+
+span#author {
+ color: #527bbd;
+ font-family: sans-serif;
+ font-weight: bold;
+ font-size: 1.1em;
+}
+span#email {
+}
+span#revision {
+ font-family: sans-serif;
+}
+
+div#footer {
+ font-family: sans-serif;
+ font-size: small;
+ border-top: 2px solid silver;
+ padding-top: 0.5em;
+ margin-top: 4.0em;
+}
+div#footer-text {
+ float: left;
+ padding-bottom: 0.5em;
+}
+div#footer-badges {
+ float: right;
+ padding-bottom: 0.5em;
+}
+
+div#preamble,
+div.tableblock, div.imageblock, div.exampleblock, div.verseblock,
+div.quoteblock, div.literalblock, div.listingblock, div.sidebarblock,
+div.admonitionblock {
+ margin-right: 10%;
+ margin-top: 1.5em;
+ margin-bottom: 1.5em;
+}
+div.admonitionblock {
+ margin-top: 2.5em;
+ margin-bottom: 2.5em;
+}
+
+div.content { /* Block element content. */
+ padding: 0;
+}
+
+/* Block element titles. */
+div.title, caption.title {
+ font-family: sans-serif;
+ font-weight: bold;
+ text-align: left;
+ margin-top: 1.0em;
+ margin-bottom: 0.5em;
+}
+div.title + * {
+ margin-top: 0;
+}
+
+td div.title:first-child {
+ margin-top: 0.0em;
+}
+div.content div.title:first-child {
+ margin-top: 0.0em;
+}
+div.content + div.title {
+ margin-top: 0.0em;
+}
+
+div.sidebarblock > div.content {
+ background: #ffffee;
+ border: 1px solid silver;
+ padding: 0.5em;
+}
+
+div.listingblock {
+ margin-right: 0%;
+}
+div.listingblock > div.content {
+ border: 1px solid silver;
+ background: #f4f4f4;
+ padding: 0.5em;
+}
+
+div.quoteblock > div.content {
+ padding-left: 2.0em;
+}
+
+div.attribution {
+ text-align: right;
+}
+div.verseblock + div.attribution {
+ text-align: left;
+}
+
+div.admonitionblock .icon {
+ vertical-align: top;
+ font-size: 1.1em;
+ font-weight: bold;
+ text-decoration: underline;
+ color: #527bbd;
+ padding-right: 0.5em;
+}
+div.admonitionblock td.content {
+ padding-left: 0.5em;
+ border-left: 2px solid silver;
+}
+
+div.exampleblock > div.content {
+ border-left: 2px solid silver;
+ padding: 0.5em;
+}
+
+div.verseblock div.content {
+ white-space: pre;
+}
+
+div.imageblock div.content { padding-left: 0; }
+div.imageblock img { border: 1px solid silver; }
+span.image img { border-style: none; }
+
+dl {
+ margin-top: 0.8em;
+ margin-bottom: 0.8em;
+}
+dt {
+ margin-top: 0.5em;
+ margin-bottom: 0;
+ font-style: italic;
+}
+dd > *:first-child {
+ margin-top: 0;
+}
+
+ul, ol {
+ list-style-position: outside;
+}
+ol.olist2 {
+ list-style-type: lower-alpha;
+}
+
+div.tableblock > table {
+ border: 3px solid #527bbd;
+}
+thead {
+ font-family: sans-serif;
+ font-weight: bold;
+}
+tfoot {
+ font-weight: bold;
+}
+
+div.hlist {
+ margin-top: 0.8em;
+ margin-bottom: 0.8em;
+}
+div.hlist td {
+ padding-bottom: 5px;
+}
+td.hlist1 {
+ vertical-align: top;
+ font-style: italic;
+ padding-right: 0.8em;
+}
+td.hlist2 {
+ vertical-align: top;
+}
+
+@media print {
+ div#footer-badges { display: none; }
+}
+
+div#toctitle {
+ color: #527bbd;
+ font-family: sans-serif;
+ font-size: 1.1em;
+ font-weight: bold;
+ margin-top: 1.0em;
+ margin-bottom: 0.1em;
+}
+
+div.toclevel1, div.toclevel2, div.toclevel3, div.toclevel4 {
+ margin-top: 0;
+ margin-bottom: 0;
+}
+div.toclevel2 {
+ margin-left: 2em;
+ font-size: 0.9em;
+}
+div.toclevel3 {
+ margin-left: 4em;
+ font-size: 0.9em;
+}
+div.toclevel4 {
+ margin-left: 6em;
+ font-size: 0.9em;
+}
+/* Workarounds for IE6's broken and incomplete CSS2. */
+
+div.sidebar-content {
+ background: #ffffee;
+ border: 1px solid silver;
+ padding: 0.5em;
+}
+div.sidebar-title, div.image-title {
+ font-family: sans-serif;
+ font-weight: bold;
+ margin-top: 0.0em;
+ margin-bottom: 0.5em;
+}
+
+div.listingblock div.content {
+ border: 1px solid silver;
+ background: #f4f4f4;
+ padding: 0.5em;
+}
+
+div.quoteblock-content {
+ padding-left: 2.0em;
+}
+
+div.exampleblock-content {
+ border-left: 2px solid silver;
+ padding-left: 0.5em;
+}
+
+/* IE6 sets dynamically generated links as visited. */
+div#toc a:visited { color: blue; }
+</style>
+<script type="text/javascript">
+/*<![CDATA[*/
+window.onload = function(){generateToc(2)}
+/* Author: Mihai Bazon, September 2002
+ * http://students.infoiasi.ro/~mishoo
+ *
+ * Table Of Content generator
+ * Version: 0.4
+ *
+ * Feel free to use this script under the terms of the GNU General Public
+ * License, as long as you do not remove or alter this notice.
+ */
+
+ /* modified by Troy D. Hanson, September 2006. License: GPL */
+ /* modified by Stuart Rackham, October 2006. License: GPL */
+
+function getText(el) {
+ var text = "";
+ for (var i = el.firstChild; i != null; i = i.nextSibling) {
+ if (i.nodeType == 3 /* Node.TEXT_NODE */) // IE doesn't speak constants.
+ text += i.data;
+ else if (i.firstChild != null)
+ text += getText(i);
+ }
+ return text;
+}
+
+function TocEntry(el, text, toclevel) {
+ this.element = el;
+ this.text = text;
+ this.toclevel = toclevel;
+}
+
+function tocEntries(el, toclevels) {
+ var result = new Array;
+ var re = new RegExp('[hH]([2-'+(toclevels+1)+'])');
+ // Function that scans the DOM tree for header elements (the DOM2
+ // nodeIterator API would be a better technique but not supported by all
+ // browsers).
+ var iterate = function (el) {
+ for (var i = el.firstChild; i != null; i = i.nextSibling) {
+ if (i.nodeType == 1 /* Node.ELEMENT_NODE */) {
+ var mo = re.exec(i.tagName)
+ if (mo)
+ result[result.length] = new TocEntry(i, getText(i), mo[1]-1);
+ iterate(i);
+ }
+ }
+ }
+ iterate(el);
+ return result;
+}
+
+// This function does the work. toclevels = 1..4.
+function generateToc(toclevels) {
+ var toc = document.getElementById("toc");
+ var entries = tocEntries(document.getElementsByTagName("body")[0], toclevels);
+ for (var i = 0; i < entries.length; ++i) {
+ var entry = entries[i];
+ if (entry.element.id == "")
+ entry.element.id = "toc" + i;
+ var a = document.createElement("a");
+ a.href = "#" + entry.element.id;
+ a.appendChild(document.createTextNode(entry.text));
+ var div = document.createElement("div");
+ div.appendChild(a);
+ div.className = "toclevel" + entry.toclevel;
+ toc.appendChild(div);
+ }
+}
+/*]]>*/
+</script>
+<title>Prefix Opclass</title>
+</head>
+<body>
+<div id="header">
+<h1>Prefix Opclass</h1>
+<div id="toc">
+ <div id="toctitle">Table of Contents</div>
+ <noscript><p><b>JavaScript must be enabled in your browser to display the table of contents.</b></p></noscript>
+</div>
+</div>
+<h2>Presentation</h2>
+<div class="sectionbody">
+</div>
+<h2>Installation</h2>
+<div class="sectionbody">
+<p>Check $PATH, then</p>
+<div class="literalblock">
+<div class="content">
+<pre><tt>make
+make install
+psql &lt;connection string&gt; -f prefix.sql &lt;database&gt;</tt></pre>
+</div></div>
+<p>The <tt>make install</tt> step might have to be done as <tt>root</tt>, and the
+psql one has to be done as a PostgreSQL <em>superuser</em>.</p>
+</div>
+<h2>Tests</h2>
+<div class="sectionbody">
+<p>The <tt>prefixes.sql</tt> creates a table which fits <tt>prefixes.fr.csv</tt>
+content. This file contains all the official French Telephony prefixes
+used as of early 2008, as found on the
+<a href="http://www.art-telecom.fr/">French telecom regulation authority</a>
+website (see <a href="http://www.art-telecom.fr/fileadmin/wopnum.rtf">http://www.art-telecom.fr/fileadmin/wopnum.rtf</a>).</p>
+<div class="literalblock">
+<div class="content">
+<pre><tt>create table prefixes (
+ prefix text primary key,
+ name text not null,
+ shortname text,
+ state char default 'S',</tt></pre>
+</div></div>
+<div class="literalblock">
+<div class="content">
+<pre><tt> check( state in ('S', 'R') )
+);
+comment on column prefixes.state is 'S: - R: reserved';</tt></pre>
+</div></div>
+<div class="literalblock">
+<div class="content">
+<pre><tt>\copy prefixes from 'prefixes.fr.csv' with delimiter ; csv quote '"'</tt></pre>
+</div></div>
+</div>
+<h2>Usage</h2>
+<div class="sectionbody">
+<div class="literalblock">
+<div class="content">
+<pre><tt>postgres=# select '123' @&gt; '123456';
+ ?column?
+----------
+ t
+(1 row)</tt></pre>
+</div></div>
+<div class="literalblock">
+<div class="content">
+<pre><tt>CREATE INDEX idx_prefix ON prefixes USING GIST(prefix gist_prefix_ops);</tt></pre>
+</div></div>
+</div>
+<div id="footer">
+<div id="footer-text">
+Last updated 19-Jan-2008 16:28:59 CEST
+</div>
+</div>
+</body>
+</html>
45 README.txt
@@ -0,0 +1,45 @@
+= Prefix Opclass
+
+== Presentation
+
+== Installation
+
+Check $PATH, then
+
+ make
+ make install
+ psql <connection string> -f prefix.sql <database>
+
+The +make install+ step might have to be done as +root+, and the
+psql one has to be done as a PostgreSQL 'superuser'.
+
+== Tests
+
+The +prefixes.sql+ creates a table which fits +prefixes.fr.csv+
+content. This file contains all the official French Telephony prefixes
+used as of early 2008, as found on the
+http://www.art-telecom.fr/[French telecom regulation authority]
+website (see http://www.art-telecom.fr/fileadmin/wopnum.rtf[]).
+
+ create table prefixes (
+ prefix text primary key,
+ name text not null,
+ shortname text,
+ state char default 'S',
+
+ check( state in ('S', 'R') )
+ );
+ comment on column prefixes.state is 'S: - R: reserved';
+
+ \copy prefixes from 'prefixes.fr.csv' with delimiter ; csv quote '"'
+
+== Usage
+
+ postgres=# select '123' @> '123456';
+ ?column?
+ ----------
+ t
+ (1 row)
+
+
+ CREATE INDEX idx_prefix ON prefixes USING GIST(prefix gist_prefix_ops);
360 prefix.c
@@ -0,0 +1,360 @@
+/**
+ * Prefix opclass allows to efficiently index a prefix table with
+ * GiST.
+ *
+ * More common use case is telephony prefix searching for cost or
+ * routing.
+ *
+ * Many thanks to AndrewSN, who provided great amount of help in the
+ * writting of this opclass, on the PostgreSQL internals, GiST inner
+ * working and prefix search analyses.
+ *
+ * $Id: prefix.c,v 1.1 2008/01/19 15:30:08 dim Exp $
+ */
+
+#include <stdio.h>
+#include "postgres.h"
+
+#include "access/gist.h"
+#include "access/skey.h"
+#include "utils/elog.h"
+#include "utils/palloc.h"
+#include "utils/builtins.h"
+#include <math.h>
+
+#define DEBUG
+
+PG_MODULE_MAGIC;
+
+/**
+ * Operator prefix @> query and query <@ prefix
+ */
+Datum prefix_contains(PG_FUNCTION_ARGS);
+Datum prefix_contained_by(PG_FUNCTION_ARGS);
+Datum greater_prefix(PG_FUNCTION_ARGS);
+
+/**
+ * GiST support methods
+ */
+Datum gprefix_consistent(PG_FUNCTION_ARGS);
+Datum gprefix_compress(PG_FUNCTION_ARGS);
+Datum gprefix_decompress(PG_FUNCTION_ARGS);
+Datum gprefix_penalty(PG_FUNCTION_ARGS);
+Datum gprefix_picksplit(PG_FUNCTION_ARGS);
+Datum gprefix_union(PG_FUNCTION_ARGS);
+Datum gprefix_same(PG_FUNCTION_ARGS);
+
+
+/**
+ * prefix opclass only provides 1 operator, @>
+ */
+static inline
+bool prefix_contains_internal(text *prefix, text *query, bool eqval)
+{
+ int plen, qlen = 0;
+
+ if( DirectFunctionCall2(texteq,
+ PointerGetDatum(prefix),
+ PointerGetDatum(query)) )
+ return eqval;
+
+ plen = VARSIZE(prefix) - VARHDRSZ;
+ qlen = VARSIZE(query) - VARHDRSZ;
+
+ if(qlen < plen )
+ return false;
+
+ return memcmp(VARDATA(prefix), VARDATA(query), plen) == 0;
+}
+
+/**
+ * The operator @> code
+ */
+PG_FUNCTION_INFO_V1(prefix_contains);
+Datum
+prefix_contains(PG_FUNCTION_ARGS)
+{
+ PG_RETURN_BOOL( prefix_contains_internal(PG_GETARG_TEXT_P(0),
+ PG_GETARG_TEXT_P(1),
+ TRUE) );
+}
+
+/**
+ * The commutator, <@, using the same internal code
+ */
+PG_FUNCTION_INFO_V1(prefix_contained_by);
+Datum
+prefix_contained_by(PG_FUNCTION_ARGS)
+{
+ PG_RETURN_BOOL( prefix_contains_internal(PG_GETARG_TEXT_P(1),
+ PG_GETARG_TEXT_P(0),
+ TRUE) );
+}
+
+/**
+ * greater_prefix returns the greater prefix of any 2 given texts
+ */
+static inline
+text *greater_prefix_internal(text *a, text *b)
+{
+ int i = 0;
+ int la = VARSIZE(a) - VARHDRSZ;
+ int lb = VARSIZE(b) - VARHDRSZ;
+ char *ca = VARDATA(a);
+ char *cb = VARDATA(b);
+
+ for(i=0; i<la && i<lb && ca[i] == cb[i]; i++);
+
+ /* i is the last common char position in a, or 0 */
+ if( i == 0 )
+ return DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum("")));
+ else
+ return DatumGetTextPSlice(PointerGetDatum(a), 0, i);
+}
+
+PG_FUNCTION_INFO_V1(greater_prefix);
+Datum
+greater_prefix(PG_FUNCTION_ARGS)
+{
+
+ PG_RETURN_POINTER( greater_prefix_internal(PG_GETARG_TEXT_P(0),
+ PG_GETARG_TEXT_P(1)) );
+}
+
+
+/**
+ * GiST opclass methods
+ */
+
+PG_FUNCTION_INFO_V1(gprefix_consistent);
+Datum
+gprefix_consistent(PG_FUNCTION_ARGS)
+{
+ GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
+ text *query = (text *) PG_GETARG_POINTER(1);
+ StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
+ text *key = (text *) DatumGetPointer(entry->key);
+ bool retval;
+
+ /**
+ * We only have 1 Strategy (operator @>)
+ */
+ Assert(strategy == 1);
+ retval = prefix_contains_internal(query, key, true);
+
+ PG_RETURN_BOOL(retval);
+}
+
+/**
+ * Prefix penalty: we want the penalty to be lower for closer
+ * prefixes, taking into account length difference and content
+ * distance.
+ *
+ * For examples we want new prefix 125 to be inserted by preference in
+ * the 124 branch, not in a 128 or a 256 branch.
+ *
+ */
+PG_FUNCTION_INFO_V1(gprefix_penalty);
+Datum
+gprefix_penalty(PG_FUNCTION_ARGS)
+{
+ GISTENTRY *origentry = (GISTENTRY *) PG_GETARG_POINTER(0);
+ GISTENTRY *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
+ float *penalty = (float *) PG_GETARG_POINTER(2);
+
+ text *orig = (text *) DatumGetPointer(origentry->key);
+ text *new = (text *) DatumGetPointer(newentry->key);
+ text *gp;
+
+ int nlen, olen, gplen, dist, lastcd = 0;
+
+ if( DirectFunctionCall2(texteq,
+ PointerGetDatum(new),
+ PointerGetDatum(orig)) ) {
+
+#ifdef DEBUG
+ elog(NOTICE, "gprefix_penalty(%s, %s) = 0",
+ DatumGetCString(DirectFunctionCall1(textout,PointerGetDatum(orig))),
+ DatumGetCString(DirectFunctionCall1(textout,PointerGetDatum(new))));
+#endif
+ *penalty = 0;
+ PG_RETURN_POINTER(penalty);
+ }
+ else {
+ /**
+ * Consider greater common prefix length, the greater the better,
+ * then for a distance of 1 (only last prefix char is different),
+ * consider char code distance.
+ */
+ nlen = VARSIZE(new) - VARHDRSZ;
+ olen = VARSIZE(orig) - VARHDRSZ;
+ gp = greater_prefix_internal(orig, new);
+ gplen = VARSIZE(gp) - VARHDRSZ;
+
+ dist = nlen - gplen;
+ lastcd = 0;
+
+ if( dist == 1 ) {
+ char *o = VARDATA(orig);
+ char *n = VARDATA(new);
+ lastcd = abs((int)o[olen] - (int)n[nlen]);
+ }
+ *penalty = (dist + lastcd - gplen);
+ }
+
+#ifdef DEBUG
+ elog(NOTICE, "gprefix_penalty(%s, %s) [%d %d %d] = %f",
+ DatumGetCString(DirectFunctionCall1(textout,PointerGetDatum(orig))),
+ DatumGetCString(DirectFunctionCall1(textout,PointerGetDatum(new))),
+ gplen, dist, lastcd,
+ *penalty);
+#endif
+
+ PG_RETURN_POINTER(penalty);
+}
+
+/**
+ * prefix picksplit implementation
+ *
+ * Let's try a very naive first implementation
+ *
+ */
+PG_FUNCTION_INFO_V1(gprefix_picksplit);
+Datum
+gprefix_picksplit(PG_FUNCTION_ARGS)
+{
+ GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
+ OffsetNumber maxoff = entryvec->n - 2;
+ GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
+ GISTENTRY *ent = entryvec->vector;
+
+ int nbytes;
+ OffsetNumber i;
+ OffsetNumber split_at;
+ OffsetNumber *listL;
+ OffsetNumber *listR;
+ text *cur;
+ text *unionL;
+ text *unionR;
+
+#ifdef DEBUG
+ elog(NOTICE, "gprefix_picksplit()");
+#endif
+
+ nbytes = (maxoff + 2) * sizeof(OffsetNumber);
+ listL = (OffsetNumber *) palloc(nbytes);
+ listR = (OffsetNumber *) palloc(nbytes);
+
+ split_at = FirstOffsetNumber + (maxoff - FirstOffsetNumber + 1)/2;
+ v->spl_nleft = v->spl_nright = 0;
+
+ v->spl_left = listL;
+ v->spl_right = listR;
+
+ unionL = (text *) DatumGetPointer(ent[OffsetNumberNext(FirstOffsetNumber)].key);
+
+ for (i = FirstOffsetNumber; i < split_at; i = OffsetNumberNext(i)) {
+ cur = (text *) DatumGetPointer(ent[i].key);
+ v->spl_left[v->spl_nleft++] = i;
+ unionL = greater_prefix_internal(unionL, cur);
+ }
+
+ unionR = (text *)DatumGetPointer(ent[i].key);
+ for (; i <= maxoff; i = OffsetNumberNext(i)) {
+ cur = (text *) DatumGetPointer(ent[i].key);
+ v->spl_right[v->spl_nright++] = i;
+ unionR = greater_prefix_internal(unionR, cur);
+ }
+
+ v->spl_ldatum = PointerGetDatum(unionL);
+ v->spl_rdatum = PointerGetDatum(unionR);
+
+ PG_RETURN_POINTER(v);
+}
+
+/**
+ * Prefix union should return the greatest common prefix.
+ */
+PG_FUNCTION_INFO_V1(gprefix_union);
+Datum
+gprefix_union(PG_FUNCTION_ARGS)
+{
+ GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
+ GISTENTRY *ent = entryvec->vector;
+
+ text *out, *tmp, *gp;
+ int numranges, i = 0;
+
+ numranges = entryvec->n;
+ tmp = (text *) DatumGetPointer(ent[0].key);
+ out = tmp;
+
+ if( numranges == 1 ) {
+ /**
+ * We need to return a palloc()ed copy of ent[0].key (==tmp)
+ */
+ out = DatumGetTextPCopy(PointerGetDatum(tmp));
+#ifdef DEBUG
+ elog(NOTICE, "gprefix_union(%s) == %s",
+ DatumGetCString(DirectFunctionCall1(textout,PointerGetDatum(tmp))),
+ DatumGetCString(DirectFunctionCall1(textout,PointerGetDatum(out))));
+#endif
+ PG_RETURN_POINTER(out);
+ }
+
+ for (i = 1; i < numranges; i++) {
+ tmp = (text *) DatumGetPointer(ent[i].key);
+ gp = greater_prefix_internal(out, tmp);
+#ifdef DEBUG
+ elog(NOTICE, "gprefix_union: gp(%s, %s) == %s",
+ DatumGetCString(DirectFunctionCall1(textout,PointerGetDatum(out))),
+ DatumGetCString(DirectFunctionCall1(textout,PointerGetDatum(tmp))),
+ DatumGetCString(DirectFunctionCall1(textout,PointerGetDatum(gp))));
+#endif
+ out = gp;
+ }
+
+#ifdef DEBUG
+ elog(NOTICE, "gprefix_union: %s",
+ DatumGetCString(DirectFunctionCall1(textout,PointerGetDatum(out))));
+#endif
+
+ PG_RETURN_POINTER(out);
+}
+
+/**
+ * GiST Compress and Decompress methods for prefix
+ * do not do anything.
+ */
+PG_FUNCTION_INFO_V1(gprefix_compress);
+Datum
+gprefix_compress(PG_FUNCTION_ARGS)
+{
+ PG_RETURN_POINTER(PG_GETARG_POINTER(0));
+}
+
+PG_FUNCTION_INFO_V1(gprefix_decompress);
+Datum
+gprefix_decompress(PG_FUNCTION_ARGS)
+{
+ PG_RETURN_POINTER(PG_GETARG_POINTER(0));
+}
+
+/**
+ * Equality methods
+ */
+PG_FUNCTION_INFO_V1(gprefix_same);
+Datum
+gprefix_same(PG_FUNCTION_ARGS)
+{
+ text *v1 = (text *) PG_GETARG_POINTER(0);
+ text *v2 = (text *) PG_GETARG_POINTER(1);
+ bool *result = (bool *) PG_GETARG_POINTER(2);
+
+ *result = DirectFunctionCall2(texteq,
+ PointerGetDatum(v1),
+ PointerGetDatum(v2));
+
+ PG_RETURN_POINTER(result);
+}
+
BIN  prefix.so
Binary file not shown
100 prefix.sql
@@ -0,0 +1,100 @@
+---
+--- prefix opclass installation
+---
+BEGIN;
+
+CREATE OR REPLACE FUNCTION prefix_contains(text, text)
+RETURNS bool
+AS '$libdir/prefix'
+LANGUAGE 'C' IMMUTABLE STRICT;
+
+CREATE OR REPLACE FUNCTION prefix_contained_by(text, text)
+RETURNS bool
+AS '$libdir/prefix'
+LANGUAGE 'C' IMMUTABLE STRICT;
+
+DROP OPERATOR IF EXISTS @>(text, text) CASCADE;
+CREATE OPERATOR @> (
+ LEFTARG = text,
+ RIGHTARG = text,
+ PROCEDURE = prefix_contains,
+ COMMUTATOR = '<@'
+);
+COMMENT ON OPERATOR @>(text, text) IS 'prefix contains query';
+
+DROP OPERATOR IF EXISTS <@(text, text) CASCADE;
+CREATE OPERATOR <@ (
+ LEFTARG = text,
+ RIGHTARG = text,
+ PROCEDURE = prefix_contained_by,
+ COMMUTATOR = '@>'
+);
+COMMENT ON OPERATOR <@(text, text) IS 'query is contained by prefix';
+
+--
+-- greatest prefix aggregate
+--
+CREATE OR REPLACE FUNCTION greater_prefix(text, text)
+RETURNS text
+AS '$libdir/prefix'
+LANGUAGE 'C' IMMUTABLE STRICT;
+
+DROP AGGREGATE IF EXISTS greater_prefix(text);
+CREATE AGGREGATE greater_prefix(text) (
+ SFUNC = greater_prefix,
+ STYPE = text
+);
+COMMENT ON AGGREGATE greater_prefix(text) IS 'greater prefix aggregate';
+
+--
+-- define the GiST support methods
+--
+
+CREATE OR REPLACE FUNCTION gprefix_consistent(internal, text, text)
+RETURNS bool
+AS '$libdir/prefix'
+LANGUAGE 'C';
+
+CREATE OR REPLACE FUNCTION gprefix_compress(internal)
+RETURNS internal
+AS '$libdir/prefix'
+LANGUAGE 'C';
+
+CREATE OR REPLACE FUNCTION gprefix_decompress(internal)
+RETURNS internal
+AS '$libdir/prefix'
+LANGUAGE 'C';
+
+CREATE OR REPLACE FUNCTION gprefix_penalty(internal, internal, internal)
+RETURNS internal
+AS '$libdir/prefix'
+LANGUAGE 'C' STRICT;
+
+CREATE OR REPLACE FUNCTION gprefix_picksplit(internal, internal)
+RETURNS internal
+AS '$libdir/prefix'
+LANGUAGE 'C';
+
+CREATE OR REPLACE FUNCTION gprefix_union(internal, internal)
+RETURNS text
+AS '$libdir/prefix'
+LANGUAGE 'C';
+
+CREATE OR REPLACE FUNCTION gprefix_same(text, text, internal)
+RETURNS internal
+AS '$libdir/prefix'
+LANGUAGE 'C';
+
+CREATE OPERATOR CLASS gist_prefix_ops
+FOR TYPE text USING gist
+AS
+ OPERATOR 1 @>,
+ FUNCTION 1 gprefix_consistent (internal, text, text),
+ FUNCTION 2 gprefix_union (internal, internal),
+ FUNCTION 3 gprefix_compress (internal),
+ FUNCTION 4 gprefix_decompress (internal),
+ FUNCTION 5 gprefix_penalty (internal, internal, internal),
+ FUNCTION 6 gprefix_picksplit (internal, internal),
+ FUNCTION 7 gprefix_same (text, text, internal);
+
+COMMIT;
100 prefix.sql.in
@@ -0,0 +1,100 @@
+---
+--- prefix opclass installation
+---
+BEGIN;
+
+CREATE OR REPLACE FUNCTION prefix_contains(text, text)
+RETURNS bool
+AS 'MODULE_PATHNAME'
+LANGUAGE 'C' IMMUTABLE STRICT;
+
+CREATE OR REPLACE FUNCTION prefix_contained_by(text, text)
+RETURNS bool
+AS 'MODULE_PATHNAME'
+LANGUAGE 'C' IMMUTABLE STRICT;
+
+DROP OPERATOR IF EXISTS @>(text, text) CASCADE;
+CREATE OPERATOR @> (
+ LEFTARG = text,
+ RIGHTARG = text,
+ PROCEDURE = prefix_contains,
+ COMMUTATOR = '<@'
+);
+COMMENT ON OPERATOR @>(text, text) IS 'prefix contains query';
+
+DROP OPERATOR IF EXISTS <@(text, text) CASCADE;
+CREATE OPERATOR <@ (
+ LEFTARG = text,
+ RIGHTARG = text,
+ PROCEDURE = prefix_contained_by,
+ COMMUTATOR = '@>'
+);
+COMMENT ON OPERATOR <@(text, text) IS 'query is contained by prefix';
+
+--
+-- greatest prefix aggregate
+--
+CREATE OR REPLACE FUNCTION greater_prefix(text, text)
+RETURNS text
+AS 'MODULE_PATHNAME'
+LANGUAGE 'C' IMMUTABLE STRICT;
+
+DROP AGGREGATE IF EXISTS greater_prefix(text);
+CREATE AGGREGATE greater_prefix(text) (
+ SFUNC = greater_prefix,
+ STYPE = text
+);
+COMMENT ON AGGREGATE greater_prefix(text) IS 'greater prefix aggregate';
+
+--
+-- define the GiST support methods
+--
+
+CREATE OR REPLACE FUNCTION gprefix_consistent(internal, text, text)
+RETURNS bool
+AS 'MODULE_PATHNAME'
+LANGUAGE 'C';
+
+CREATE OR REPLACE FUNCTION gprefix_compress(internal)
+RETURNS internal
+AS 'MODULE_PATHNAME'
+LANGUAGE 'C';
+
+CREATE OR REPLACE FUNCTION gprefix_decompress(internal)
+RETURNS internal
+AS 'MODULE_PATHNAME'
+LANGUAGE 'C';
+
+CREATE OR REPLACE FUNCTION gprefix_penalty(internal, internal, internal)
+RETURNS internal
+AS 'MODULE_PATHNAME'
+LANGUAGE 'C' STRICT;
+
+CREATE OR REPLACE FUNCTION gprefix_picksplit(internal, internal)
+RETURNS internal
+AS 'MODULE_PATHNAME'
+LANGUAGE 'C';
+
+CREATE OR REPLACE FUNCTION gprefix_union(internal, internal)
+RETURNS text
+AS 'MODULE_PATHNAME'
+LANGUAGE 'C';
+
+CREATE OR REPLACE FUNCTION gprefix_same(text, text, internal)
+RETURNS internal
+AS 'MODULE_PATHNAME'
+LANGUAGE 'C';
+
+CREATE OPERATOR CLASS gist_prefix_ops
+FOR TYPE text USING gist
+AS
+ OPERATOR 1 @>,
+ FUNCTION 1 gprefix_consistent (internal, text, text),
+ FUNCTION 2 gprefix_union (internal, internal),
+ FUNCTION 3 gprefix_compress (internal),
+ FUNCTION 4 gprefix_decompress (internal),
+ FUNCTION 5 gprefix_penalty (internal, internal, internal),
+ FUNCTION 6 gprefix_picksplit (internal, internal),
+ FUNCTION 7 gprefix_same (text, text, internal);
+
+COMMIT;
11,966 prefixes.fr.csv
11,966 additions, 0 deletions not shown
10 prefixes.sql
@@ -0,0 +1,10 @@
+create table prefixes (
+ prefix text primary key,
+ name text not null,
+ shortname text,
+ state char default 'S',
+
+ check( state in ('S', 'R') )
+);
+
+comment on column prefixes.state is 'S: attribué - R: réservé';
Please sign in to comment.
Something went wrong with that request. Please try again.