summaryrefslogtreecommitdiff
path: root/src/libtidbit/tidbit-database-fork.c
blob: 4c681846810f7e878c6f9842a64df4c86c178e07 (plain)
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
/* 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-fork.h"

#define GET_FORK_BACKEND(_database) ((PtrTidbitDatabaseFork)(_database->backend))

static void tidbit_database_fork_free (PtrTidbitDatabase database);
static unsigned int tidbit_database_fork_insert (PtrTidbitDatabase database, PtrTidbitRecord record);
static PtrTidbitRecord tidbit_database_fork_fetch (PtrTidbitDatabase database, PtrTidbitGuid guid);
static PtrTidbitGuidSet tidbit_database_fork_query (PtrTidbitDatabase database, PtrTidbitQuery query);

static TidbitDatabaseFunctions backend_functions = {
    &tidbit_database_fork_free,
    &tidbit_database_fork_insert,
    &tidbit_database_fork_fetch,
    &tidbit_database_fork_query,
    };


PtrTidbitDatabase tidbit_database_fork_new (PtrTidbitDatabase sub_a, PtrTidbitDatabase sub_b)
{
 PtrTidbitDatabaseFork backend = g_new(TidbitDatabaseFork, 1);
 backend->sub_a = sub_a;
 tidbit_database_ref (backend->sub_a);
 backend->sub_b = sub_b;
 tidbit_database_ref (backend->sub_b);
 
 PtrTidbitDatabase database = tidbit_database_new(backend, &backend_functions);
 return database;
}

static void tidbit_database_fork_free (PtrTidbitDatabase database)
{
 PtrTidbitDatabaseFork backend = GET_FORK_BACKEND(database);
 tidbit_database_unref (backend->sub_a);
 tidbit_database_unref (backend->sub_b);
 g_free(backend);
}

static unsigned int tidbit_database_fork_insert (PtrTidbitDatabase database, PtrTidbitRecord record)
{
 PtrTidbitDatabaseFork backend = GET_FORK_BACKEND(database);
 tidbit_database_insert_record (backend->sub_a, record);
 return tidbit_database_insert_record (backend->sub_b, record);     //FIXME combine success
}

static PtrTidbitRecord tidbit_database_fork_fetch (PtrTidbitDatabase database, PtrTidbitGuid guid)
{
 PtrTidbitDatabaseFork backend = GET_FORK_BACKEND(database);
 PtrTidbitRecord record = tidbit_database_fetch_record (backend->sub_a, guid);
 if (!record)
    record = tidbit_database_fetch_record (backend->sub_b, guid);
 return record;
}

static PtrTidbitGuidSet tidbit_database_fork_query (PtrTidbitDatabase database, PtrTidbitQuery query)
{
 PtrTidbitDatabaseFork backend = GET_FORK_BACKEND(database);
 PtrTidbitGuidSet guid_set_a = tidbit_database_query (backend->sub_a, query);
 PtrTidbitGuidSet guid_set_b = tidbit_database_query (backend->sub_b, query);
 PtrTidbitGuidSet guid_set = tidbit_guid_set_merge (guid_set_a, guid_set_b);
 tidbit_guid_set_unref (guid_set_a);
 tidbit_guid_set_unref (guid_set_b);
 return guid_set;
}