The interface implemented by applications to provide custom ordering of records. More...
Public Attributes | |
int(* | compare )(WT_COLLATOR *collator, WT_SESSION *session, const WT_ITEM *key1, const WT_ITEM *key2, int *cmp) |
Callback to compare keys. |
The interface implemented by applications to provide custom ordering of records.
Applications register their implementation with WiredTiger by calling WT_CONNECTION::add_collator.
ret = conn->add_collator(conn, "nocase", &nocasecoll, NULL);
ret = conn->add_collator(conn, "prefix10", &pcoll10.iface, NULL); /* Open a session for the current thread's work. */ if ((ret = conn->open_session(conn, NULL, NULL, &session)) != 0) fprintf(stderr, "Error opening a session on %s: %s\n", home, wiredtiger_strerror(ret)); /* XXX Do some work... */ /* Note: closing the connection implicitly closes open session(s). */ if ((ret = conn->close(conn, NULL)) != 0)
int(* WT_COLLATOR::compare)(WT_COLLATOR *collator, WT_SESSION *session, const WT_ITEM *key1, const WT_ITEM *key2, int *cmp) |
Callback to compare keys.
[out] | cmp | set to -1 if key1 < key2 , 0 if key1 == key2 , 1 if key1 > key2 . |
/* * A simple example of the collator API: compare the keys as strings. */ static int my_compare(WT_COLLATOR *collator, WT_SESSION *session, const WT_ITEM *value1, const WT_ITEM *value2, int *cmp) { const char *p1, *p2; /* Unused parameters */ (void)collator; (void)session; p1 = (const char *)value1->data; p2 = (const char *)value2->data; while (*p1 != '\0' && *p1 == *p2) p1++, p2++; *cmp = (int)*p2 - (int)*p1; return (0); }
/* A simple case insensitive comparator. */ static int __compare_nocase(WT_COLLATOR *collator, WT_SESSION *session, const WT_ITEM *v1, const WT_ITEM *v2, int *cmp) { const char *s1 = (const char *)v1->data; const char *s2 = (const char *)v2->data; (void)session; /* unused */ (void)collator; /* unused */ *cmp = strcasecmp(s1, s2); return (0); } static WT_COLLATOR nocasecoll = { __compare_nocase };
/* * Comparator that only compares the first N prefix characters of the string. * This has associated data, so we need to extend WT_COLLATOR. */ typedef struct { WT_COLLATOR iface; uint32_t maxlen; } PREFIX_COLLATOR; static int __compare_prefixes(WT_COLLATOR *collator, WT_SESSION *session, const WT_ITEM *v1, const WT_ITEM *v2, int *cmp) { PREFIX_COLLATOR *pcoll = (PREFIX_COLLATOR *)collator; const char *s1 = (const char *)v1->data; const char *s2 = (const char *)v2->data; (void)session; /* unused */ *cmp = strncmp(s1, s2, pcoll->maxlen); return (0); } static PREFIX_COLLATOR pcoll10 = { {__compare_prefixes}, 10 };