Code

703696fb38dde4ce1ac1c72109f9c19d15917f16
[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/memstore.h"
35 #include "core/store.h"
36 #include "utils/error.h"
38 SDB_PLUGIN_MAGIC;
40 /*
41  * plugin API
42  */
44 static int
45 mem_init(sdb_object_t *user_data)
46 {
47         sdb_memstore_t *store = SDB_MEMSTORE(user_data);
49         if (! store) {
50                 sdb_log(SDB_LOG_ERR, "store: Failed to allocate store");
51                 return -1;
52         }
53         if (sdb_plugin_register_writer("memstore",
54                                 &sdb_memstore_writer, SDB_OBJ(store))) {
55                 sdb_object_deref(SDB_OBJ(store));
56                 return -1;
57         }
58         if (sdb_plugin_register_reader("memstore",
59                                 &sdb_memstore_reader, SDB_OBJ(store))) {
60                 sdb_object_deref(SDB_OBJ(store));
61                 return -1;
62         }
63         return 0;
64 } /* mem_init */
66 static int
67 mem_shutdown(sdb_object_t *user_data)
68 {
69         sdb_object_deref(user_data);
70         return 0;
71 } /* mem_shutdown */
73 int
74 sdb_module_init(sdb_plugin_info_t *info)
75 {
76         /* store singleton */
77         static sdb_memstore_t *store;
79         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC, "in-memory object store");
80         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
81                         "Copyright (C) 2015 Sebastian 'tokkee' Harl <sh@tokkee.org>");
82         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
83         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
84         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
86         if (! store) {
87                 if (! (store = sdb_memstore_create())) {
88                         sdb_log(SDB_LOG_ERR, "store::memory plugin: "
89                                         "Failed to create store object");
90                         return -1;
91                 }
92         }
94         sdb_plugin_register_init("main", mem_init, SDB_OBJ(store));
95         sdb_plugin_register_shutdown("main", mem_shutdown, SDB_OBJ(store));
96         return 0;
97 } /* sdb_module_init */
99 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */