aboutsummaryrefslogtreecommitdiffstats
path: root/src/anydb.h
diff options
context:
space:
mode:
authorJose Bollo <jose.bollo@iot.bzh>2019-05-02 17:10:08 +0200
committerJose Bollo <jose.bollo@iot.bzh>2019-05-09 15:12:03 +0200
commit97e91b39fceed0309b6863f508f92db2bcc3b114 (patch)
tree722d36869dbee8a7e6e5a96514fefb51fd5de119 /src/anydb.h
parent4dbd0881526d93198db2c5d0a2e15605bab9aaae (diff)
Introduce Any DB abstraction and memdb on its top
Signed-off-by: Jose Bollo <jose.bollo@iot.bzh>
Diffstat (limited to 'src/anydb.h')
-rw-r--r--src/anydb.h142
1 files changed, 142 insertions, 0 deletions
diff --git a/src/anydb.h b/src/anydb.h
new file mode 100644
index 0000000..33f0447
--- /dev/null
+++ b/src/anydb.h
@@ -0,0 +1,142 @@
+/*
+ * Copyright (C) 2018 "IoT.bzh"
+ * Author José Bollo <jose.bollo@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+/**
+ * An index is an integer
+ */
+typedef uint32_t anydb_idx_t;
+
+#define AnyIdx_Invalid ((anydb_idx_t)0xffffffffu)
+#define AnyIdx_Any ((anydb_idx_t)0xfffffffeu)
+#define AnyIdx_Wide ((anydb_idx_t)0xfffffffdu)
+#define AnyIdx_None ((anydb_idx_t)0xfffffffcu)
+#define AnyIdx_Max ((anydb_idx_t)0xfffffff7u)
+
+/**
+ * A key is a set of index
+ */
+struct anydb_key {
+ /** client string id */
+ anydb_idx_t client;
+
+ /** session string id */
+ anydb_idx_t session;
+
+ /** user string id */
+ anydb_idx_t user;
+
+ /** permission string id */
+ anydb_idx_t permission;
+};
+typedef struct anydb_key anydb_key_t;
+
+/**
+ * A rule is a set of 32 bits integers
+ */
+struct anydb_value
+{
+ /** value string id */
+ anydb_idx_t value;
+
+ /** expiration */
+ time_t expire;
+};
+typedef struct anydb_value anydb_value_t;
+
+/**
+ */
+enum anydb_action
+{
+ Anydb_Action_Continue = 0,
+ Anydb_Action_Update_And_Stop = 1,
+ Anydb_Action_Remove_And_Continue = 2
+};
+typedef enum anydb_action anydb_action_t;
+
+struct anydb_itf
+{
+ int (*index)(void *clodb, anydb_idx_t *idx, const char *name, bool create);
+ const char *(*string)(void *clodb, anydb_idx_t idx);
+ void (*apply)(void *clodb, anydb_action_t (*oper)(void *closure, const anydb_key_t *key, anydb_value_t *value), void *closure);
+ int (*add)(void *clodb, const anydb_key_t *key, const anydb_value_t *value);
+ void (*gc)(void *clodb);
+ void (*destroy)(void *clodb);
+};
+typedef struct anydb_itf anydb_itf_t;
+
+struct anydb
+{
+ void *clodb;
+ anydb_itf_t itf;
+};
+typedef struct anydb anydb_t;
+
+
+/** enumerate */
+extern
+void
+anydb_for_all(
+ anydb_t *db,
+ void *closure,
+ void (*callback)(
+ void *closure,
+ const data_key_t *key,
+ const data_value_t *value),
+ const data_key_t *key
+);
+
+/** drop rules */
+extern
+int
+anydb_drop(
+ anydb_t *db,
+ const data_key_t *key
+);
+
+/** set a rules */
+extern
+int
+anydb_set(
+ anydb_t *db,
+ const data_key_t *key,
+ const data_value_t *value
+);
+
+/** test a rule, returns 0 or the score: count of exact keys */
+extern
+int
+anydb_test(
+ anydb_t *db,
+ const data_key_t *key,
+ data_value_t *value
+);
+
+/** drop rules */
+extern
+void
+anydb_cleanup(
+ anydb_t *db
+);
+
+/** destroy the database */
+extern
+void
+anydb_destroy(
+ anydb_t *db
+);