1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
/* Tidbit
* Copyright (C) 2010 Charlie Brej tidbit@brej.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <glib.h>
#include "tidbit.h"
#include "tidbit-database.h"
#include "tidbit-database-mem.h"
#include "tidbit-record.h"
#define GET_MEM_BACKEND(_database) ((PtrTidbitDatabaseMem)(_database->backend))
static void tidbit_database_mem_free (PtrTidbitDatabase database);
static unsigned int tidbit_database_mem_insert (PtrTidbitDatabase database, PtrTidbitRecord record);
static PtrTidbitRecord tidbit_database_mem_fetch (PtrTidbitDatabase database, PtrTidbitGuid guid);
PtrTidbitGuidGroup tidbit_database_mem_query (PtrTidbitDatabase database, PtrTidbitQuery query);
static TidbitDatabaseFunctions backend_functions = {
&tidbit_database_mem_free,
&tidbit_database_mem_insert,
&tidbit_database_mem_fetch,
&tidbit_database_mem_query
};
PtrTidbitDatabase tidbit_database_mem_new (void)
{
PtrTidbitDatabaseMem backend = g_new(TidbitDatabaseMem, 1);
backend->records = NULL;
PtrTidbitDatabase database = tidbit_database_new(backend, &backend_functions);
return database;
}
static int tidbit_database_mem_record_has_guid_cmp (gconstpointer a, gconstpointer b)
{
PtrTidbitRecord record = (PtrTidbitRecord) a;
PtrTidbitGuid guid = (PtrTidbitGuid) b;
return tidbit_guid_cmp(record->guid, guid);
}
static void tidbit_database_mem_free (PtrTidbitDatabase database)
{
PtrTidbitDatabaseMem backend = GET_MEM_BACKEND(database);
g_free(backend);
}
static unsigned int tidbit_database_mem_insert (PtrTidbitDatabase database, PtrTidbitRecord record)
{
PtrTidbitDatabaseMem backend = GET_MEM_BACKEND(database);
GList* list_element = g_list_find_custom (backend->records, record->guid, tidbit_database_mem_record_has_guid_cmp);
if (list_element)
return 1;
PtrTidbitQuery query = tidbit_query_record_replaces (record);
GList* records = backend->records;
while (records){
GList* next_records = g_list_next(records);
PtrTidbitRecord record = records->data;
if (tidbit_query_test_record (query, record)){
tidbit_record_unref (record);
backend->records = g_list_delete_link (backend->records, records);
}
records = next_records;
}
tidbit_query_unref (query);
tidbit_record_ref (record);
backend->records = g_list_append(backend->records, record);
return 0; //FIXME use propper numbers
}
static PtrTidbitRecord tidbit_database_mem_fetch (PtrTidbitDatabase database, PtrTidbitGuid guid)
{
PtrTidbitDatabaseMem backend = GET_MEM_BACKEND(database);
GList* list_element = g_list_find_custom (backend->records, guid, tidbit_database_mem_record_has_guid_cmp);
if (!list_element)
return NULL;
PtrTidbitRecord record = (PtrTidbitRecord) list_element->data;
tidbit_record_ref (record);
return record;
}
PtrTidbitGuidGroup tidbit_database_mem_query (PtrTidbitDatabase database, PtrTidbitQuery query)
{
PtrTidbitDatabaseMem backend = GET_MEM_BACKEND(database);
PtrTidbitGuidGroup guid_group = tidbit_guid_group_new ();
for (GList* records = backend->records; records; records = g_list_next(records)){
PtrTidbitRecord record = records->data;
if (tidbit_query_test_record (query, record)){
tidbit_guid_group_append (guid_group, record->guid);
}
}
return guid_group;
}
|