mirror of https://github.com/postgres/postgres
An extensible node is always tagged T_Extensible, but the extnodename field identifies it more specifically; it may also include arbitrary private data. Extensible nodes can be copied, tested for equality, serialized, and deserialized, but the core system doesn't know anything about them otherwise. Some extensions may find it useful to include these nodes in fdw_private or custom_private lists in lieu of arm-wrestling their data into a format that the core code can understand. Along the way, so as not to burden the authors of such extensible node types too much, expose the functions for writing serialized tokens, and for serializing and deserializing bitmapsets. KaiGai Kohei, per a design suggested by me. Reviewed by Andres Freund and by me, and further edited by me.pull/10/merge
parent
63461a63f9
commit
bcac23de73
@ -0,0 +1,92 @@ |
||||
/*-------------------------------------------------------------------------
|
||||
* |
||||
* extensible.c |
||||
* Support for extensible node types |
||||
* |
||||
* Loadable modules can define what are in effect new types of nodes using |
||||
* the routines in this file. All such nodes are flagged T_ExtensibleNode, |
||||
* with the extnodename field distinguishing the specific type. Use |
||||
* RegisterExtensibleNodeMethods to register a new type of extensible node, |
||||
* and GetExtensibleNodeMethods to get information about a previously |
||||
* registered type of extensible node. |
||||
* |
||||
* Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group |
||||
* Portions Copyright (c) 1994, Regents of the University of California |
||||
* |
||||
* IDENTIFICATION |
||||
* src/backend/nodes/extensible.c |
||||
* |
||||
*------------------------------------------------------------------------- |
||||
*/ |
||||
#include "postgres.h" |
||||
|
||||
#include "nodes/extensible.h" |
||||
#include "utils/hsearch.h" |
||||
|
||||
static HTAB *extensible_node_methods = NULL; |
||||
|
||||
typedef struct |
||||
{ |
||||
char extnodename[EXTNODENAME_MAX_LEN]; |
||||
const ExtensibleNodeMethods *methods; |
||||
} ExtensibleNodeEntry; |
||||
|
||||
/*
|
||||
* Register a new type of extensible node. |
||||
*/ |
||||
void |
||||
RegisterExtensibleNodeMethods(const ExtensibleNodeMethods *methods) |
||||
{ |
||||
ExtensibleNodeEntry *entry; |
||||
bool found; |
||||
|
||||
if (extensible_node_methods == NULL) |
||||
{ |
||||
HASHCTL ctl; |
||||
|
||||
memset(&ctl, 0, sizeof(HASHCTL)); |
||||
ctl.keysize = NAMEDATALEN; |
||||
ctl.entrysize = sizeof(ExtensibleNodeEntry); |
||||
extensible_node_methods = hash_create("Extensible Node Methods", |
||||
100, &ctl, HASH_ELEM); |
||||
} |
||||
|
||||
Assert(strlen(methods->extnodename) <= EXTNODENAME_MAX_LEN); |
||||
|
||||
entry = (ExtensibleNodeEntry *) hash_search(extensible_node_methods, |
||||
methods->extnodename, |
||||
HASH_ENTER, &found); |
||||
if (found) |
||||
ereport(ERROR, |
||||
(errcode(ERRCODE_DUPLICATE_OBJECT), |
||||
errmsg("extensible node type \"%s\" already exists", |
||||
methods->extnodename))); |
||||
|
||||
entry->methods = methods; |
||||
} |
||||
|
||||
/*
|
||||
* Get the methods for a given type of extensible node. |
||||
*/ |
||||
const ExtensibleNodeMethods * |
||||
GetExtensibleNodeMethods(const char *extnodename, bool missing_ok) |
||||
{ |
||||
ExtensibleNodeEntry *entry = NULL; |
||||
|
||||
if (extensible_node_methods != NULL) |
||||
entry = (ExtensibleNodeEntry *) hash_search(extensible_node_methods, |
||||
extnodename, |
||||
HASH_FIND, NULL); |
||||
|
||||
if (!entry) |
||||
{ |
||||
if (missing_ok) |
||||
return NULL; |
||||
ereport(ERROR, |
||||
(errcode(ERRCODE_UNDEFINED_OBJECT), |
||||
errmsg("ExtensibleNodeMethods \"%s\" was not registered", |
||||
extnodename))); |
||||
} |
||||
|
||||
return entry->methods; |
||||
} |
@ -0,0 +1,72 @@ |
||||
/*-------------------------------------------------------------------------
|
||||
* |
||||
* extensible.h |
||||
* Definitions for extensible node type |
||||
* |
||||
* |
||||
* Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group |
||||
* Portions Copyright (c) 1994, Regents of the University of California |
||||
* |
||||
* src/include/nodes/extensible.h |
||||
* |
||||
*------------------------------------------------------------------------- |
||||
*/ |
||||
#ifndef EXTENSIBLE_H |
||||
#define EXTENSIBLE_H |
||||
|
||||
#include "nodes/nodes.h" |
||||
|
||||
#define EXTNODENAME_MAX_LEN 64 |
||||
|
||||
/*
|
||||
* An extensible node is a new type of node defined by an extension. The |
||||
* type is always T_ExtensibleNode, while the extnodename identifies the |
||||
* specific type of node. extnodename can be looked up to find the |
||||
* ExtensibleNodeMethods for this node type. |
||||
*/ |
||||
typedef struct ExtensibleNode |
||||
{ |
||||
NodeTag type; |
||||
const char *extnodename; /* identifier of ExtensibleNodeMethods */ |
||||
} ExtensibleNode; |
||||
|
||||
/*
|
||||
* node_size is the size of an extensible node of this type in bytes. |
||||
* |
||||
* nodeCopy is a function which performs a deep copy from oldnode to newnode. |
||||
* It does not need to copy type or extnodename, which are copied by the |
||||
* core system. |
||||
* |
||||
* nodeEqual is a function which performs a deep equality comparison between |
||||
* a and b and returns true or false accordingly. It does not need to compare |
||||
* type or extnodename, which are compared by the core system. |
||||
* |
||||
* nodeOut is a serialization function for the node type. It should use the |
||||
* output conventions typical for outfuncs.c. It does not need to output |
||||
* type or extnodename; the core system handles those. |
||||
* |
||||
* nodeRead is a deserialization function for the node type. It does not need |
||||
* to read type or extnodename; the core system handles those. It should fetch |
||||
* the next token using pg_strtok() from the current input stream, and then |
||||
* reconstruct the private fields according to the manner in readfuncs.c. |
||||
* |
||||
* All callbacks are mandatory. |
||||
*/ |
||||
typedef struct ExtensibleNodeMethods |
||||
{ |
||||
const char *extnodename; |
||||
Size node_size; |
||||
void (*nodeCopy)(struct ExtensibleNode *newnode, |
||||
const struct ExtensibleNode *oldnode); |
||||
bool (*nodeEqual)(const struct ExtensibleNode *a, |
||||
const struct ExtensibleNode *b); |
||||
void (*nodeOut)(struct StringInfoData *str, |
||||
const struct ExtensibleNode *node); |
||||
void (*nodeRead)(struct ExtensibleNode *node); |
||||
} ExtensibleNodeMethods; |
||||
|
||||
extern void RegisterExtensibleNodeMethods(const ExtensibleNodeMethods *method); |
||||
extern const ExtensibleNodeMethods *GetExtensibleNodeMethods(const char *name, |
||||
bool missing_ok); |
||||
|
||||
#endif /* EXTENSIBLE_H */ |
Loading…
Reference in new issue