tizen 2.3 release
[external/buxton.git] / src / shared / backend.h
1 /*
2  * This file is part of buxton.
3  *
4  * Copyright (C) 2013 Intel Corporation
5  *
6  * buxton is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as
8  * published by the Free Software Foundation; either version 2.1
9  * of the License, or (at your option) any later version.
10  */
11
12 /**
13  * \file backend.h Internal header
14  * This file is used internally by buxton to provide functionality
15  * used by and for the backend
16  */
17 #pragma once
18
19 #ifdef HAVE_CONFIG_H
20         #include "config.h"
21 #endif
22
23 #include <gdbm.h>
24
25 #include "buxtonarray.h"
26 #include "buxtondata.h"
27 #include "buxtonstring.h"
28 #include "protocol.h"
29 #include "hashmap.h"
30
31 /**
32  * Possible backends for Buxton
33  */
34 typedef enum BuxtonBackendType {
35         BACKEND_UNSET = 0, /**<No backend set */
36         BACKEND_GDBM, /**<GDBM backend */
37         BACKEND_MEMORY, /**<Memory backend */
38         BACKEND_MAXTYPES
39 } BuxtonBackendType;
40
41 /**
42  * Buxton layer type
43  */
44 typedef enum BuxtonLayerType {
45         LAYER_SYSTEM, /**<A system layer*/
46         LAYER_USER, /**<A user layer */
47         LAYER_MAXTYPES
48 } BuxtonLayerType;
49
50 /**
51  * Represents a layer within Buxton
52  *
53  * Keys can be stored in various layers within Buxton, using a variety
54  * of backend and configurations. This is all handled transparently and
55  * through a consistent API
56  */
57 typedef struct BuxtonLayer {
58         BuxtonString name; /**<Name of the layer*/
59         BuxtonLayerType type; /**<Type of layer */
60         BuxtonBackendType backend; /**<Backend for this layer */
61         uid_t uid; /**<User ID for layers of type LAYER_USER */
62         int priority; /**<Priority of this layer */
63         char *description; /**<Description of this layer */
64         bool readonly; /**<Layer is readonly or not */
65 } BuxtonLayer;
66
67 /**
68  * Backend manipulation function
69  * @param layer The layer to manipulate or query
70  * @param key The key to manipulate or query
71  * @param data Set or get data, dependant on operation
72  * @param label The key's label
73  * @return a int value, indicating success of the operation or errno
74  */
75 typedef int (*module_value_func) (BuxtonLayer *layer, _BuxtonKey *key,
76                                   BuxtonData *data, BuxtonString *label);
77
78 /**
79  * Backend key list function
80  * @param layer The layer to query
81  * @param data Pointer to store BuxtonArray in
82  * @return a boolean value, indicating success of the operation
83  */
84 typedef bool (*module_list_func) (BuxtonLayer *layer, BuxtonArray **data);
85
86 /**
87  * Backend database creation function
88  * @param layer The layer matching the db to create
89  * @return A Database (not intended to be used from direct functions)
90  */
91 typedef void *(*module_db_init_func) (BuxtonLayer *layer);
92
93 /**
94  * Destroy (or shutdown) a backend module
95  */
96 typedef void (*module_destroy_func) (void);
97
98 /**
99  * A data-backend for Buxton
100  *
101  * Backends are controlled by Buxton for storing and retrieving data
102  */
103 typedef struct BuxtonBackend {
104         void *module; /**<Private handle to the module */
105         module_destroy_func destroy; /**<Destroy method */
106         module_value_func set_value; /**<Set value function */
107         module_value_func get_value; /**<Get value function */
108         module_list_func list_keys; /**<List keys function */
109         module_value_func unset_value; /**<Unset value function */
110         module_db_init_func create_db; /**<DB file creation function */
111 } BuxtonBackend;
112
113 /**
114  * Stores internal configuration of Buxton
115  */
116 typedef struct BuxtonConfig {
117         Hashmap *databases; /**<Database mapping */
118         Hashmap *layers; /**<Global layer configuration */
119         Hashmap *backends; /**<Backend mapping */
120 } BuxtonConfig;
121
122 /**
123  * Internal controller for Buxton
124  */
125 typedef struct BuxtonControl {
126         _BuxtonClient client; /**<Valid client connection */
127         BuxtonConfig config; /**<Valid configuration (unused) */
128 } BuxtonControl;
129
130 /**
131  * Module initialisation function
132  * @param backend The backend to initialise
133  * @return A boolean value, representing the success of the operation
134  */
135 typedef bool (*module_init_func) (BuxtonBackend *backend)
136         __attribute__((warn_unused_result));
137
138 /**
139  * Return a valid backend for the given configuration and layer
140  * @param config A BuxtonControl's configuration
141  * @param layer The layer to query
142  * @return an initialised backend, or NULL if the layer is not found
143  */
144 BuxtonBackend *backend_for_layer(BuxtonConfig *config,
145                                  BuxtonLayer *layer)
146         __attribute__((warn_unused_result));
147
148 /**
149  * Initialize layers using the configuration file
150  * @param config A BuxtonControl's configuration
151  */
152 void buxton_init_layers(BuxtonConfig *config);
153
154 /**
155  * Remove association with backend (free and dlclose)
156  * @param backend A BuxtonBackend to be removed
157  */
158 void destroy_backend(BuxtonBackend *backend);
159
160 /*
161  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
162  *
163  * Local variables:
164  * c-basic-offset: 8
165  * tab-width: 8
166  * indent-tabs-mode: t
167  * End:
168  *
169  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
170  * :indentSize=8:tabSize=8:noTabs=false:
171  */