Code

store::memory: Add a plugin providing an in-memory store.
[sysdb.git] / src / plugins / store / memory.c
1 /*
2  * SysDB - src/plugins/backend/store/memory.c
3  * Copyright (C) 2015 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #if HAVE_CONFIG_H
29 #       include "config.h"
30 #endif
32 #include "sysdb.h"
33 #include "core/plugin.h"
34 #include "core/store.h"
35 #include "utils/error.h"
37 SDB_PLUGIN_MAGIC;
39 /*
40  * plugin API
41  */
43 static int
44 mem_init(sdb_object_t *user_data)
45 {
46         sdb_store_t *store = SDB_STORE(user_data);
48         if (! store) {
49                 sdb_log(SDB_LOG_ERR, "store: Failed to allocate store");
50                 return -1;
51         }
52         if (sdb_plugin_register_writer("memstore",
53                                 &sdb_store_writer, SDB_OBJ(store))) {
54                 sdb_object_deref(SDB_OBJ(store));
55                 return -1;
56         }
57         if (sdb_plugin_register_reader("memstore",
58                                 &sdb_store_reader, SDB_OBJ(store))) {
59                 sdb_object_deref(SDB_OBJ(store));
60                 return -1;
61         }
62         return 0;
63 } /* mem_init */
65 static int
66 mem_shutdown(sdb_object_t *user_data)
67 {
68         sdb_object_deref(user_data);
69         return 0;
70 } /* mem_shutdown */
72 int
73 sdb_module_init(sdb_plugin_info_t *info)
74 {
75         /* store singleton */
76         static sdb_store_t *store;
78         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC, "in-memory object store");
79         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
80                         "Copyright (C) 2015 Sebastian 'tokkee' Harl <sh@tokkee.org>");
81         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
82         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
83         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
85         if (! store) {
86                 if (! (store = sdb_store_create())) {
87                         sdb_log(SDB_LOG_ERR, "store::memory plugin: "
88                                         "Failed to create store object");
89                         return -1;
90                 }
91         }
93         sdb_plugin_register_init("main", mem_init, SDB_OBJ(store));
94         sdb_plugin_register_shutdown("main", mem_shutdown, SDB_OBJ(store));
95         return 0;
96 } /* sdb_module_init */
98 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */