+#define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
+
+typedef struct {
+ const char *name;
+ const char *prefix;
+} prefix_t;
+
+/* Here's the current schema for our database:
+ *
+ * We currently have two different types of documents: mail and timestamps.
+ *
+ * Mail document
+ * -------------
+ * A mail document is associated with a particular email message file
+ * on disk. It is indexed with the following prefixed terms:
+ *
+ * Single terms of given prefix:
+ *
+ * type: mail
+ *
+ * id: Unique ID of mail, (from Message-ID header or generated
+ * as "notmuch-sha1-<sha1_sum_of_entire_file>.
+ *
+ * thread: The ID of the thread to which the mail belongs
+ *
+ * Multiple terms of given prefix:
+ *
+ * ref: All unresolved message IDs from In-Reply-To and
+ * References headers in the message. (Once a referenced
+ * message is added to the database and the thread IDs
+ * are linked the corresponding "ref" term is dropped
+ * from the message document.)
+ *
+ * tag: Any tags associated with this message by the user.
+ *
+ * A mail document also has two values:
+ *
+ * TIMESTAMP: The time_t value corresponding to the message's
+ * Date header.
+ *
+ * MESSAGE_ID: The unique ID of the mail mess (see "id" above)
+ *
+ * Timestamp document
+ * ------------------
+ * A timestamp document is used by a client of the notmuch library to
+ * maintain data necessary to allow for efficient polling of mail
+ * directories. The notmuch library does no interpretation of
+ * timestamps, but merely allows the user to store and retrieve
+ * timestamps as name/value pairs.
+ *
+ * The timestamp document is indexed with a single prefixed term:
+ *
+ * timestamp: The user's key value (likely a directory name)
+ *
+ * and has a single value:
+ *
+ * TIMETAMPS: The time_t value from the user.
+ */
+
+/* With these prefix values we follow the conventions published here:
+ *
+ * http://xapian.org/docs/omega/termprefixes.html
+ *
+ * as much as makes sense. Note that I took some liberty in matching
+ * the reserved prefix values to notmuch concepts, (for example, 'G'
+ * is documented as "newsGroup (or similar entity - e.g. a web forum
+ * name)", for which I think the thread is the closest analogue in
+ * notmuch. This in spite of the fact that we will eventually be
+ * storing mailing-list messages where 'G' for "mailing list name"
+ * might be even a closer analogue. I'm treating the single-character
+ * prefixes preferentially for core notmuch concepts (which will be
+ * nearly universal to all mail messages).
+ */
+
+prefix_t BOOLEAN_PREFIX_INTERNAL[] = {
+ { "type", "T" },
+ { "ref", "XREFERENCE" },
+ { "timestamp", "XTIMESTAMP" },
+ { "contact", "XCONTACT" }
+};
+
+prefix_t BOOLEAN_PREFIX_EXTERNAL[] = {
+ { "thread", "G" },
+ { "tag", "K" },
+ { "id", "Q" }
+};
+
+prefix_t PROBABILISTIC_PREFIX[]= {
+ { "from", "XFROM" },
+ { "to", "XTO" },
+ { "attachment", "XATTACHMENT" },
+ { "subject", "XSUBJECT"}
+};
+
+int
+_internal_error (const char *format, ...)
+{
+ va_list va_args;
+
+ va_start (va_args, format);
+
+ fprintf (stderr, "Internal error: ");
+ vfprintf (stderr, format, va_args);
+
+ exit (1);
+
+ return 1;
+}
+
+const char *
+_find_prefix (const char *name)
+{
+ unsigned int i;
+
+ for (i = 0; i < ARRAY_SIZE (BOOLEAN_PREFIX_INTERNAL); i++)
+ if (strcmp (name, BOOLEAN_PREFIX_INTERNAL[i].name) == 0)
+ return BOOLEAN_PREFIX_INTERNAL[i].prefix;
+
+ for (i = 0; i < ARRAY_SIZE (BOOLEAN_PREFIX_EXTERNAL); i++)
+ if (strcmp (name, BOOLEAN_PREFIX_EXTERNAL[i].name) == 0)
+ return BOOLEAN_PREFIX_EXTERNAL[i].prefix;
+
+ for (i = 0; i < ARRAY_SIZE (PROBABILISTIC_PREFIX); i++)
+ if (strcmp (name, PROBABILISTIC_PREFIX[i].name) == 0)
+ return PROBABILISTIC_PREFIX[i].prefix;
+
+ INTERNAL_ERROR ("No prefix exists for '%s'\n", name);
+
+ return "";
+}
+