]> git.jsancho.org Git - bongodb.git/blob - src/bongodb.c
Initial commit
[bongodb.git] / src / bongodb.c
1 /*
2  * BongoDB, an embedded document-based engine
3  * Copyright (C) 2015 by Javier Sancho Fernandez <jsf at jsancho dot org>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "bongodb.h"
20
21 #include <stdlib.h>
22 #include <string.h>
23
24
25 /* BongoDB Client */
26
27 bongo_client_t *bongo_client_new(void)
28 {
29   bongo_client_t *bongo_client;
30
31   bongo_client = malloc(sizeof(bongo_client_t));
32   bongo_client->n_databases = 0;
33   bongo_client->databases = NULL;
34   
35   return bongo_client;
36 }
37
38 bongo_database_node_t *bongo_database_node_new(bongo_client_t *bongo_client, const char *db_name)
39 {
40   bongo_database_node_t *bongo_database_node;
41
42   if (bongo_client == NULL)
43     return NULL;
44
45   bongo_database_node = malloc(sizeof(bongo_database_node_t));
46   bongo_database_node->db_name = db_name;
47   bongo_database_node->n_collections = 0;
48   bongo_database_node->collections = NULL;
49   bongo_database_node->next = (struct bongo_database_node_t *)bongo_client->databases;
50   
51   bongo_client->databases = (bongo_database_node_t *)bongo_database_node;
52   bongo_client->n_databases++;
53
54   return bongo_database_node;
55 }
56
57 bongo_collection_node_t *bongo_collection_node_new(bongo_database_node_t *bongo_database_node, const char *col_name)
58 {
59   bongo_collection_node_t *bongo_collection_node;
60
61   if (bongo_database_node == NULL)
62     return NULL;
63
64   bongo_collection_node = malloc(sizeof(bongo_collection_node_t));
65   bongo_collection_node->col_name = col_name;
66   bongo_collection_node->next = (struct bongo_collection_node_t *)bongo_database_node->collections;
67
68   bongo_database_node->collections = (bongo_collection_node_t *)bongo_collection_node;
69   bongo_database_node->n_collections++;
70
71   return bongo_collection_node;
72 }
73
74 bongo_database_node_t *bongo_database_node_search(bongo_client_t *bongo_client, const char *db_name)
75 {
76   bongo_database_node_t *bongo_database_node;
77
78   if (bongo_client == NULL)
79     return NULL;
80
81   bongo_database_node = bongo_client->databases;
82   while (bongo_database_node != NULL && strcmp(bongo_database_node->db_name, db_name) != 0)
83     bongo_database_node = (bongo_database_node_t *)bongo_database_node->next;
84
85   return bongo_database_node;
86 }
87
88 bongo_collection_node_t *bongo_collection_node_search(bongo_database_node_t *bongo_database_node, const char *col_name)
89 {
90   bongo_collection_node_t *bongo_collection_node;
91
92   if (bongo_database_node == NULL)
93     return NULL;
94
95   bongo_collection_node = bongo_database_node->collections;
96   while (bongo_collection_node != NULL && strcmp(bongo_collection_node->col_name, col_name) != 0)
97     bongo_collection_node = (bongo_collection_node_t *)bongo_collection_node->next;
98
99   return bongo_collection_node;
100 }
101   
102 void bongo_collection_node_destroy(bongo_collection_node_t *bongo_collection_node)
103 {
104   free(bongo_collection_node);
105 }
106
107 void bongo_database_node_destroy(bongo_database_node_t *bongo_database_node)
108 {
109   bongo_collection_node_t *bongo_collection_node;
110
111   bongo_collection_node = bongo_database_node->collections;
112   while (bongo_collection_node != NULL)
113     {
114       bongo_database_node->collections = (bongo_collection_node_t *)bongo_collection_node->next;
115       bongo_collection_node_destroy(bongo_collection_node);
116       bongo_collection_node = bongo_database_node->collections;
117     }
118   
119   free(bongo_database_node);
120 }
121
122 void bongo_client_destroy(bongo_client_t *bongo_client)
123 {
124   bongo_database_node_t *bongo_database_node;
125
126   bongo_database_node = bongo_client->databases;
127   while (bongo_database_node != NULL)
128     {
129       bongo_client->databases = (bongo_database_node_t *)bongo_database_node->next;
130       bongo_database_node_destroy(bongo_database_node);
131       bongo_database_node = bongo_client->databases;
132     }
133
134   free(bongo_client);
135 }
136
137
138 /* BongoDB Database */
139
140 bongo_database_t *bongo_client_get_database(bongo_client_t *bongo_client, const char *db_name)
141 {
142   bongo_database_node_t *bongo_database_node;
143   bongo_database_t *bongo_database;
144
145   if (bongo_client == NULL)
146     return NULL;
147
148   bongo_database_node = bongo_database_node_search(bongo_client, db_name);
149   if (bongo_database_node == NULL)
150     bongo_database_node = bongo_database_node_new(bongo_client, db_name);
151
152   bongo_database = malloc(sizeof(bongo_database_t));
153   bongo_database->database_node = bongo_database_node;
154   
155   return bongo_database;
156 }
157
158 void bongo_database_destroy(bongo_database_t *bongo_database)
159 {
160   free(bongo_database);
161 }
162
163
164 /* BongoDB Collection */
165
166 bongo_collection_t *bongo_client_get_collection(bongo_client_t *bongo_client, const char *db_name, const char *col_name)
167 {
168   bongo_database_t *bongo_database;
169   bongo_collection_node_t *bongo_collection_node;
170   bongo_collection_t *bongo_collection;
171
172   if (bongo_client == NULL)
173     return NULL;
174
175   bongo_database = bongo_client_get_database(bongo_client, db_name);
176
177   bongo_collection_node = bongo_collection_node_search(bongo_database->database_node, col_name);
178   if (bongo_collection_node == NULL)
179     bongo_collection_node = bongo_collection_node_new(bongo_database->database_node, col_name);
180
181   bongo_database_destroy(bongo_database);
182
183   bongo_collection = malloc(sizeof(bongo_collection_t));
184   bongo_collection->collection_node = bongo_collection_node;
185
186   return bongo_collection;
187 }
188
189 void bongo_collection_destroy(bongo_collection_t *bongo_collection)
190 {
191   free(bongo_collection);
192 }