소스 검색

Use X macros to handle all DB labels and indices.

Stefano Cossu 3 년 전
부모
커밋
5a209a1bf1
1개의 변경된 파일68개의 추가작업 그리고 91개의 파일을 삭제
  1. 68 91
      src/store_mdb.c

+ 68 - 91
src/store_mdb.c

@@ -39,76 +39,48 @@ static char *env_path = NULL;
 static bool env_init = false;
 static bool db_init = false;
 
+
 /**
- * Main DB labels
+ * Main DBs. These are the master information containers.
+ *
+ * Data columns are: identifier prefix, DB label, flags.
  */
-// Term key to serialized term
-static const DbLabel DB_T_ST = "t:st";
-// Joined triple keys to context key
-static const DbLabel DB_SPO_C = "spo:c";
-// This has empty values and is used to keep track of empty contexts.
-static const DbLabel DB_C_ = "c:";
-// Prefix to namespace
-static const DbLabel DB_PFX_NS = "pfx:ns";
+#define MAIN_TABLE \
+    ENTRY(  T_ST,    "t:st",    INT_KEY_MASK    )   /* Key to ser. term */  \
+    ENTRY(  SPO_C,   "spo:c",   INT_DUP_MASK    )   /* Triple to context */ \
+    ENTRY(  C_,      "c:",      INT_KEY_MASK    )   /* Track empty contexts */\
+    ENTRY(  PFX_NS,  "pfx:ns",  0               )   /* Prefix to NS */      \
 
 /**
- * Indices
+ * Lookup DBs. These are indices and may be destroyed and rebuilt.
  */
-// Namespace to prefix
-static const DbLabel DB_NS_PFX = "ns:pfx";
-// Term hash to term key
-static const DbLabel DB_TH_T = "th:t";
-// 1-bound lookups
-static const DbLabel DB_S_PO = "s:po";
-static const DbLabel DB_P_SO = "p:so";
-static const DbLabel DB_O_SP = "o:sp";
-// 2-bound lookups
-static const DbLabel DB_PO_S = "po:s";
-static const DbLabel DB_SO_P = "so:p";
-static const DbLabel DB_SP_O = "sp:o";
-// Context lookup
-static const DbLabel DB_C_SPO = "c:spo";
+#define LOOKUP_TABLE \
+    ENTRY(  S_PO,    "s:po",    INT_DUP_KEY_MASK)   /* 1-bound lookup */    \
+    ENTRY(  P_SO,    "p:so",    INT_DUP_KEY_MASK)   /* 1-bound lookup */    \
+    ENTRY(  O_SP,    "o:sp",    INT_DUP_KEY_MASK)   /* 1-bound lookup */    \
+    ENTRY(  PO_S,    "po:s",    INT_DUP_MASK    )   /* 2-bound lookup */    \
+    ENTRY(  SO_P,    "so:p",    INT_DUP_MASK    )   /* 2-bound lookup */    \
+    ENTRY(  SP_O,    "sp:o",    INT_DUP_MASK    )   /* 2-bound lookup */    \
+    ENTRY(  C_SPO,   "c:spo",   INT_DUP_KEY_MASK)   /* Context lookup */    \
+    ENTRY(  TH_T,    "th:t",    0               )   /* Term hash to key */  \
+    ENTRY(  NS_PFX,  "ns:pfx",  0               )   /* NS to prefix */      \
 
 /**
- * Order in which keys are looked up if two terms are bound.
- * The indices with the smallest average number of values per key should be
- * looked up first.
- *
- * 0 = s:po
- * 1 = p:so
- * 2 = o:sp
+ * DB labels. They are prefixed with DB_
  */
-static const uint8_t lookup_rank[3] = {0, 2, 1};
-
-static const uint8_t lookup_ordering_1bound[3][3] = {
-    {0, 1, 2}, // spo
-    {1, 0, 2}, // pso
-    {2, 0, 1}, // osp
-};
-
-static const uint8_t lookup_ordering_2bound[3][3] = {
-    {1, 2, 0}, // po:s
-    {0, 2, 1}, // so:p
-    {0, 1, 2}, // sp:o
-};
+#define ENTRY(a, b, c) static const DbLabel DB_##a = b;
+MAIN_TABLE
+LOOKUP_TABLE
+#undef ENTRY
 
 /**
- * Index of each DB in the following constants.
+ * Numveric index of each DB in the following constants. Prefixed with IDX_
  */
 typedef enum {
-    IDX_T_ST,
-    IDX_SPO_C,
-    IDX_C_,
-    IDX_PFX_NS,
-    IDX_NS_PFX,
-    IDX_TH_T,
-    IDX_S_PO,
-    IDX_P_SO,
-    IDX_O_SP,
-    IDX_PO_S,
-    IDX_SO_P,
-    IDX_SP_O,
-    IDX_C_SPO,
+#define ENTRY(a, b, c) IDX_##a,
+    MAIN_TABLE
+    LOOKUP_TABLE
+#undef ENTRY
 } DBIdx;
 
 /**
@@ -120,50 +92,55 @@ static MDB_dbi dbis[N_DB];
  * DB labels.
  */
 static const char *db_labels[N_DB] = {
-    DB_T_ST,
-    DB_SPO_C,
-    DB_C_,
-    DB_PFX_NS,
-    DB_NS_PFX,
-    DB_TH_T,
-    DB_S_PO,
-    DB_P_SO,
-    DB_O_SP,
-    DB_PO_S,
-    DB_SO_P,
-    DB_SP_O,
-    DB_C_SPO,
+#define ENTRY(a, b, c) DB_##a,
+    MAIN_TABLE
+    LOOKUP_TABLE
+#undef ENTRY
 };
 
 /**
  * DB flags. These are aligned with the dbi_labels index.
  */
 static const unsigned int db_flags[N_DB] = {
-    INT_KEY_MASK,
-    INT_DUP_KEY_MASK,
-    0,
-    INT_DUP_KEY_MASK,
-    0,
-    INT_DUP_MASK,
-    INT_DUP_KEY_MASK,
-    INT_DUP_MASK,
-    INT_DUP_MASK,
-    INT_DUP_MASK,
-    INT_DUP_KEY_MASK,
-    0,
-    INT_KEY_MASK,
+#define ENTRY(a, b, c) c,
+    MAIN_TABLE
+    LOOKUP_TABLE
+#undef ENTRY
 };
 
 /**
  * 1-bound and 2-bound lookup indices.
+ *
+ * N.B. Only the first 6 (1-bound and 2-bound term lookup) are used.
+ * The others are added just because they belong logically to the lookup table.
  */
-static DBIdx lookup_indices[6] = {
-    IDX_S_PO,
-    IDX_P_SO,
-    IDX_O_SP,
-    IDX_PO_S,
-    IDX_SO_P,
-    IDX_SP_O,
+static DBIdx lookup_indices[9] = {
+#define ENTRY(a, b, c) IDX_##a,
+    LOOKUP_TABLE
+#undef ENTRY
+};
+
+/**
+ * Order in which keys are looked up if two terms are bound.
+ * The indices with the smallest average number of values per key should be
+ * looked up first.
+ *
+ * 0 = s:po
+ * 1 = p:so
+ * 2 = o:sp
+ */
+static const uint8_t lookup_rank[3] = {0, 2, 1};
+
+static const uint8_t lookup_ordering_1bound[3][3] = {
+    {0, 1, 2}, // spo
+    {1, 0, 2}, // pso
+    {2, 0, 1}, // osp
+};
+
+static const uint8_t lookup_ordering_2bound[3][3] = {
+    {1, 2, 0}, // po:s
+    {0, 2, 1}, // so:p
+    {0, 1, 2}, // sp:o
 };