-
Notifications
You must be signed in to change notification settings - Fork 1
/
extenddb.h
194 lines (172 loc) · 5.32 KB
/
extenddb.h
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
Copyright (C) 2013 Sridhar Valaguru <sridharnitt@gmail.com>
This file is part of eXTendDB.
eXTendDB is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
eXTendDB 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with eXTendDB. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __XTDB_H__
#define __XTDB_H__
#include <stdio.h>
#include <stdlib.h>
//#include <sys/statfs.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <stddef.h>
#include <stdarg.h>
#include "query.h"
#include "bson.h"
#include "stringutil.h"
#include "dbbe.h"
#include "extenddberror.h"
struct XTDBHandle;
typedef struct XTDBHandle* XTDBHandlePtr;
/*!
* Initializes a handle for XTDB to be used in all operations.
*
* @param dbName -- Database name to be opened/created.
* @param dataDir-- Directory to store the database files.
* @param out -- Output parameter holds the pointer to XTDBHandle
*/
XTDBError XTDBInitHandle(const char* dbName,const char* dataDir,XTDBHandlePtr* out);
/**
* Frees XTDBHandle.
* @param handle -- Handle to XTDB .
*/
void XTDBFreeHandle(XTDBHandlePtr handle);
/**
* Creates index of a field.
*
* @param handle -- Handle to XTDB .
* @param fieldName -- Name of the field to be indexed.
* \returns True if successful or error is set.
* \retval error is set to XTDB_INDEX_EXISTS if index exists.
*/
uint8_t XTDBCreateIndex(XTDBHandlePtr handle,char* fieldName);
/**
* Removes an index.
*
* @param handle -- Handle to XTDB .
* @param fieldName -- Name of the field to be indexed.
* \returns True if successful or error is set.
*/
uint8_t XTDBDropIndex(XTDBHandlePtr handle,char* fieldName);
/**
* Removes entire XTDB database and files.
*
* @param handle -- Handle to XTDB .
* \returns True if successful or error is set.
*/
XTDBError XTDBDrop(XTDBHandlePtr handle);
/**
* Syncs cached data to files/disk.
*
* @param handle -- Handle to XTDB .
* \param diskSync -- if True all data is synced to disk.
if False all data cached in memory is synced to file.
* \returns True if successful or error is set.
*/
uint8_t XTDBSync(XTDBHandlePtr handle,uint8_t diskSync);
typedef enum XTDBCursorIndexStatus {
NO_INDEX=0,
MAIN_INDEX=1,
OTHER_INDEX=2,
}XTDBCursorIndexStatus;
typedef struct XTDBCursor {
void* dbIter;
BinaryStr query;
XTDBHandlePtr mdbHandle;
uint8_t usingIndex;
int idx;
uint8_t first;
BinaryStr lastKey;
uint8_t nextKeyStatus;
uint64_t gen;
DataBaseBE* curDB;
XTDBError error;
}XTDBCursor;
/**
* Inserts a bson object into the db.
*
* @param handle -- Handle to XTDB .
* \param newVal -- Complete bson object.
* \returns True if successful or error is set.
*/
uint8_t XTDBInsert(XTDBHandlePtr handle,bson* newVal);
/**
* Finds objects matching query bson object and returns cursor.
*
* @param handle -- Handle to XTDB .
* \param query -- Bson query object
* \returns cursor object.
*/
XTDBCursor* XTDBFind(XTDBHandlePtr handle,bson* query);
/**
* Finds objects matching query bson object and returns cursor.
*
* @param cursor -- Cursor object returned by XTDBFind.
* \param[out] outKey -- Returns _id of the next BSON object.
* \param[out] outVal -- Returns next BSON object in the cursor.
* \returns True if successful or error is set.
*/
uint8_t XTDBCursorNext(XTDBCursor* cursor,
BinaryStr* outKey,
bson* outVal);
/**
* Frees cursor returned by XTDBFind.
*
* @param cursor -- Cursor object returned by XTDBFind.
*/
void XTDBCursorFree(XTDBCursor* cursor);
/**
* Updates all bson objects matched by query.
*
* @param handle -- Handle to XTDB .
* \param query -- Query bson object.
* \param newVal -- Value of the updated bson object.
* \param upsert -- If True if the query returns no object new object is returned.
* \returns True if successful or error is set.
*/
uint8_t XTDBUpdate(XTDBHandlePtr handle,bson* query,bson* newVal,uint8_t upsert);
/**
* Removes all bson objects matched by query.
*
* @param handle -- Handle to XTDB .
* \param query -- Query bson object.
* \returns True if successful or error is set.
*/
uint8_t XTDBRemove(XTDBHandlePtr handle,bson* query);
/**
* Counts the number of objects matching the query.
*
* @param handle -- Handle to XTDB .
* \param query -- Query bson object.
* \returns Number of documents matching the query.
*/
int64_t XTDBCount(XTDBHandlePtr handle, bson* query);
/**
* Counts the number of objects matching the query.
*
* @param handle -- Handle to XTDB .
* \returns Last occured error.
*/
XTDBError XTDBGetLastError(XTDBHandlePtr handle);
/**
* Counts the number of objects matching the query.
*
* @param handle -- Handle to XTDB .
* \param[out] error -- Output bson object to hold error information.
*/
void XTDBGetLastErrorBson(XTDBHandlePtr handle,bson* error);
#endif