2 * This file is part of buxton.
4 * Copyright (C) 2013 Intel Corporation
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.
26 * Memory Database Module
28 * Used for quick testing and debugging of Buxton, to ensure protocol
29 * and direct access are working as intended.
30 * Note this is not persistent.
34 static Hashmap *_resources;
36 /* Return existing hashmap or create new hashmap on the fly */
37 static Hashmap *_db_for_resource(BuxtonLayer *layer)
46 if (layer->type == LAYER_USER) {
47 r = asprintf(&name, "%s-%d", layer->name.value, layer->uid);
49 r = asprintf(&name, "%s", layer->name.value);
55 db = hashmap_get(_resources, name);
57 db = hashmap_new(string_hash_func, string_compare_func);
58 hashmap_put(_resources, name, db);
66 static int set_value(BuxtonLayer *layer, _BuxtonKey *key, BuxtonData *data,
70 BuxtonArray *array = NULL;
72 BuxtonData *data_copy = NULL;
74 BuxtonString *label_copy = NULL;
76 char *full_key = NULL;
84 db = _db_for_resource(layer);
90 if (key->name.value) {
91 if (asprintf(&full_key, "%s%s", key->group.value, key->name.value) == -1) {
95 full_key = strdup(key->group.value);
102 stored = (BuxtonArray *)hashmap_get(db, full_key);
108 data = buxton_array_get(stored, 0);
111 array = buxton_array_new();
115 data_copy = malloc0(sizeof(BuxtonData));
119 label_copy = malloc0(sizeof(BuxtonString));
124 if (!buxton_data_copy(data, data_copy)) {
127 if (!buxton_string_copy(label, label_copy)) {
130 if (!buxton_array_add(array, data_copy)) {
133 if (!buxton_array_add(array, label_copy)) {
136 if (!buxton_array_add(array, full_key)) {
140 ret = hashmap_put(db, full_key, array);
142 if (ret == -ENOMEM) {
145 /* remove the old value */
146 stored = (BuxtonArray *)hashmap_remove(db, full_key);
150 d = buxton_array_get(stored, 0);
152 l = buxton_array_get(stored, 1);
154 k = buxton_array_get(stored, 2);
156 buxton_array_free(&stored, NULL);
157 ret = hashmap_put(db, full_key, array);
169 static int get_value(BuxtonLayer *layer, _BuxtonKey *key, BuxtonData *data,
176 char *full_key = NULL;
184 db = _db_for_resource(layer);
187 * Set negative here to indicate layer not found
188 * rather than key not found, optimization for
195 if (key->name.value) {
196 if (asprintf(&full_key, "%s%s", key->group.value, key->name.value) == -1) {
200 full_key = strdup(key->group.value);
206 stored = (BuxtonArray *)hashmap_get(db, full_key);
211 d = buxton_array_get(stored, 0);
212 if (d->type != key->type) {
217 if (!buxton_data_copy(d, data)) {
221 l = buxton_array_get(stored, 1);
222 if (!buxton_string_copy(l, label)) {
233 static int unset_value(BuxtonLayer *layer,
235 __attribute__((unused)) BuxtonData *data,
236 __attribute__((unused)) BuxtonString *label)
242 char *full_key = NULL;
249 db = _db_for_resource(layer);
255 if (key->name.value) {
256 if (asprintf(&full_key, "%s%s", key->group.value, key->name.value) == -1) {
260 full_key = strdup(key->group.value);
266 /* test if the value exists */
267 stored = (BuxtonArray *)hashmap_remove(db, full_key);
274 d = buxton_array_get(stored, 0);
276 l = buxton_array_get(stored, 1);
278 k = buxton_array_get(stored, 2);
280 buxton_array_free(&stored, NULL);
289 _bx_export_ void buxton_module_destroy(void)
291 const char *key1, *key2;
292 Iterator iteratori, iteratoro;
298 /* free all hashmaps */
299 HASHMAP_FOREACH_KEY(map, key1, _resources, iteratoro) {
300 HASHMAP_FOREACH_KEY(array, key2, map, iteratori) {
301 hashmap_remove(map, key2);
302 d = buxton_array_get(array, 0);
304 l = buxton_array_get(array, 1);
306 buxton_array_free(&array, NULL);
308 hashmap_remove(_resources, key1);
313 hashmap_free(_resources);
317 _bx_export_ bool buxton_module_init(BuxtonBackend *backend)
322 /* Point the struct methods back to our own */
323 backend->set_value = &set_value;
324 backend->get_value = &get_value;
325 backend->unset_value = &unset_value;
326 backend->list_keys = NULL;
327 backend->create_db = NULL;
329 _resources = hashmap_new(string_hash_func, string_compare_func);
337 * Editor modelines - http://www.wireshark.org/tools/modelines.html
342 * indent-tabs-mode: t
345 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
346 * :indentSize=8:tabSize=8:noTabs=false: