2
* Copyright 2011 Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU General Public License version 3, as published
6
* by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranties of
10
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11
* PURPOSE. See the GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License along
14
* with this program. If not, see <http://www.gnu.org/licenses/>.
20
typedef struct _u1database u1database;
23
#define U1DB_INVALID_PARAMETER -1
24
// put_doc() was called but the doc_rev stored in the database doesn't match
26
#define U1DB_INVALID_DOC_REV -2
27
#define U1DB_INVALID_DOC_ID -3
30
* The basic constructor for a new connection.
32
u1database *u1db_open(const char *fname);
35
* Close an existing connection, freeing memory, etc.
36
* This is generally used as u1db_free(&db);
37
* After freeing the memory, we will set the pointer to NULL.
39
void u1db_free(u1database **db);
42
* Set the machine_id defined for this database.
44
int u1db_set_machine_id(u1database *db, const char *machine_id);
47
* Get the machine_id defined for this database.
49
int u1db_get_machine_id(u1database *db, char **machine_id);
52
* Create a new document.
54
* @param doc: The JSON string representing the document.
55
* @param n: The number of bytes in doc
56
* @param doc_id: A string identifying the document. If the value supplied is
57
* NULL, then a new doc_id will be generated. Callers are responsible for
58
* then freeing the returned string.
59
* @param doc_rev: The document revision. Callers are responsible for freeing
62
int u1db_create_doc(u1database *db, const char *doc, int n, char **doc_id,
66
* Put new document content for the given document identifier.
68
* @param doc_id: A string identifying the document. If the value supplied is
69
* NULL, then a new doc_id will be generated. Callers are responsible for
70
* then freeing the returned string.
71
* @param doc_rev: The document revision. This should contain the revision that
72
* is being replaced, and it will be filled in with the new document revision.
73
* The new revision will be malloced(), callers are responsible for
75
* @param doc: The JSON string representing the document.
76
* @param n: The number of bytes in doc
78
int u1db_put_doc(u1database *db, const char *doc_id, char **doc_rev,
79
const char *doc, int n);
82
* Get the document defined by the given document id.
84
* @param doc_id (IN) The document we are looking for
85
* @param doc_rev (OUT) The final document revision. Callers must free the memory
86
* @param doc (OUT) Callers are responsible for freeing the memory
87
* @param n (OUT) Number of bytes for doc
88
* @param has_conflicts (OUT) Are there conflicts present for this document?
90
int u1db_get_doc(u1database *db, const char *doc_id, char **doc_rev,
91
char **doc, int *n, int *has_conflicts);
94
* Mark a document as deleted.
96
* @param doc_id (IN) The document we want to delete.
97
* @param doc_rev (IN/OUT) The rev of the document we are deleting, must match
98
* the stored value, or the delete will fail. Will be updated
99
* to point at the new document revision (for the delete),
100
* callers must free the memory.
102
int u1db_delete_doc(u1database *db, const char *doc_id, char **doc_rev);
105
* Get the document defined by the given document id.
107
* @param db_rev The global database revision to start at. You can pass '0' to
108
* get all changes in the database. The integer will be updated
109
* to point at the current db_rev.
110
* @param cb A callback function. This will be called passing in 'context',
111
* and a document identifier for each document that has been modified.
112
* The doc_id string is transient, so callers must copy it to
113
* their own memory if they want to keep it.
114
* If a document is changed more than once, it is currently
115
* undefined whether this will call cb() once per change, or just
117
* @param context Opaque context, passed back to the caller.
119
int u1db_whats_changed(u1database *db, int *db_rev,
120
int (*cb)(void *, char *doc_id), void *context);
124
* Internal API, Get the global database rev.
126
int u1db__get_db_rev(u1database *db, int *db_rev);
129
* Internal API, Allocate a new document id, for cases when callers do not
130
* supply their own. Callers of this API are expected to free the result.
132
char *u1db__allocate_doc_id(u1database *db);
135
* Internal api, close the underlying sql instance.
137
int u1db__sql_close(u1database *db);
140
* Internal api, check to see if the underlying SQLite handle has been closed.
142
int u1db__sql_is_open(u1database *db);
145
* Internal api, run an SQL query directly.
147
typedef struct _u1db_row {
148
struct _u1db_row *next;
151
unsigned char **columns;
154
typedef struct _u1db_table {
159
u1db_table *u1db__sql_run(u1database *db, const char *sql, size_t n);
160
void u1db__free_table(u1db_table **table);
164
* Internal sync api, get the stored information about another machine.
166
int u1db__sync_get_machine_info(u1database *db, const char *other_machine_id,
167
int *other_db_rev, char **my_machine_id,
171
* Internal sync api, store information about another machine.
173
int u1db__sync_record_machine_info(u1database *db, const char *machine_id,
176
typedef struct _u1db_record {
177
struct _u1db_record *next;
184
* Internal sync api, exchange sync records.
186
int u1db__sync_exchange(u1database *db, const char *from_machine_id,
187
int from_db_rev, int last_known_rev,
188
u1db_record *from_records, u1db_record **new_records,
189
u1db_record **conflict_records);
192
* Allocate a new u1db_record, and copy all records over.
194
u1db_record *u1db__create_record(const char *doc_id, const char *doc_rev,
197
u1db_record *u1db__copy_record(u1db_record *src);
200
* Free a linked list of records. All linked records will be freed, including
201
* all memory referenced from them.
203
void u1db__free_records(u1db_record **record);
205
typedef struct _u1db_vectorclock_item {
208
} u1db_vectorclock_item;
210
typedef struct _u1db_vectorclock {
212
u1db_vectorclock_item *items;
215
u1db_vectorclock *u1db__vectorclock_from_str(const char *s);
217
void u1db__free_vectorclock(u1db_vectorclock **clock);
218
int u1db__vectorclock_increment(u1db_vectorclock *clock,
219
const char *machine_id);
221
int u1db__vectorclock_maximize(u1db_vectorclock *clock,
222
u1db_vectorclock *other);
224
* Return a null-terminated string representation for this vector clock.
225
* Callers must take care to free() the result.
227
int u1db__vectorclock_as_str(u1db_vectorclock *clock, char **result);
228
int u1db__vectorclock_is_newer(u1db_vectorclock *maybe_newer,
229
u1db_vectorclock *older);