summaryrefslogtreecommitdiffstats
path: root/common/unqlite/unqliteInt.h
diff options
context:
space:
mode:
authorAaron Seigo <aseigo@kde.org>2014-12-14 12:00:05 +0100
committerAaron Seigo <aseigo@kde.org>2014-12-14 12:00:05 +0100
commit7cc25005b8c46d1fa783d33def2c6923e8ef8469 (patch)
tree64fa59d17af29838396cf37b912b3babd885e5dd /common/unqlite/unqliteInt.h
parentbfc32f265e8ad72823db960fed371d72596003b7 (diff)
parenta6ed70495f9f3ecb21c26860dda16aadcdc91c3a (diff)
downloadsink-7cc25005b8c46d1fa783d33def2c6923e8ef8469.tar.gz
sink-7cc25005b8c46d1fa783d33def2c6923e8ef8469.zip
Merge branch 'unqlite'
Diffstat (limited to 'common/unqlite/unqliteInt.h')
-rw-r--r--common/unqlite/unqliteInt.h319
1 files changed, 319 insertions, 0 deletions
diff --git a/common/unqlite/unqliteInt.h b/common/unqlite/unqliteInt.h
new file mode 100644
index 0000000..4c93a68
--- /dev/null
+++ b/common/unqlite/unqliteInt.h
@@ -0,0 +1,319 @@
1/*
2 * Symisc unQLite: An Embeddable NoSQL (Post Modern) Database Engine.
3 * Copyright (C) 2012-2013, Symisc Systems http://unqlite.org/
4 * Version 1.1.6
5 * For information on licensing, redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES
6 * please contact Symisc Systems via:
7 * legal@symisc.net
8 * licensing@symisc.net
9 * contact@symisc.net
10 * or visit:
11 * http://unqlite.org/licensing.html
12 */
13 /* $SymiscID: unqliteInt.h v1.7 FreeBSD 2012-11-02 11:25 devel <chm@symisc.net> $ */
14#ifndef __UNQLITEINT_H__
15#define __UNQLITEINT_H__
16/* Internal interface definitions for UnQLite. */
17#ifdef UNQLITE_AMALGAMATION
18/* Marker for routines not intended for external use */
19#define UNQLITE_PRIVATE static
20#define JX9_AMALGAMATION
21#else
22#define UNQLITE_PRIVATE
23#include "unqlite.h"
24#include "jx9Int.h"
25#endif
26/* forward declaration */
27typedef struct unqlite_db unqlite_db;
28/*
29** The following values may be passed as the second argument to
30** UnqliteOsLock(). The various locks exhibit the following semantics:
31**
32** SHARED: Any number of processes may hold a SHARED lock simultaneously.
33** RESERVED: A single process may hold a RESERVED lock on a file at
34** any time. Other processes may hold and obtain new SHARED locks.
35** PENDING: A single process may hold a PENDING lock on a file at
36** any one time. Existing SHARED locks may persist, but no new
37** SHARED locks may be obtained by other processes.
38** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
39**
40** PENDING_LOCK may not be passed directly to UnqliteOsLock(). Instead, a
41** process that requests an EXCLUSIVE lock may actually obtain a PENDING
42** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
43** UnqliteOsLock().
44*/
45#define NO_LOCK 0
46#define SHARED_LOCK 1
47#define RESERVED_LOCK 2
48#define PENDING_LOCK 3
49#define EXCLUSIVE_LOCK 4
50/*
51 * UnQLite Locking Strategy (Same as SQLite3)
52 *
53 * The following #defines specify the range of bytes used for locking.
54 * SHARED_SIZE is the number of bytes available in the pool from which
55 * a random byte is selected for a shared lock. The pool of bytes for
56 * shared locks begins at SHARED_FIRST.
57 *
58 * The same locking strategy and byte ranges are used for Unix and Windows.
59 * This leaves open the possiblity of having clients on winNT, and
60 * unix all talking to the same shared file and all locking correctly.
61 * To do so would require that samba (or whatever
62 * tool is being used for file sharing) implements locks correctly between
63 * windows and unix. I'm guessing that isn't likely to happen, but by
64 * using the same locking range we are at least open to the possibility.
65 *
66 * Locking in windows is mandatory. For this reason, we cannot store
67 * actual data in the bytes used for locking. The pager never allocates
68 * the pages involved in locking therefore. SHARED_SIZE is selected so
69 * that all locks will fit on a single page even at the minimum page size.
70 * PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE
71 * is set high so that we don't have to allocate an unused page except
72 * for very large databases. But one should test the page skipping logic
73 * by setting PENDING_BYTE low and running the entire regression suite.
74 *
75 * Changing the value of PENDING_BYTE results in a subtly incompatible
76 * file format. Depending on how it is changed, you might not notice
77 * the incompatibility right away, even running a full regression test.
78 * The default location of PENDING_BYTE is the first byte past the
79 * 1GB boundary.
80 */
81#define PENDING_BYTE (0x40000000)
82#define RESERVED_BYTE (PENDING_BYTE+1)
83#define SHARED_FIRST (PENDING_BYTE+2)
84#define SHARED_SIZE 510
85/*
86 * The default size of a disk sector in bytes.
87 */
88#ifndef UNQLITE_DEFAULT_SECTOR_SIZE
89#define UNQLITE_DEFAULT_SECTOR_SIZE 512
90#endif
91/*
92 * Each open database file is managed by a separate instance
93 * of the "Pager" structure.
94 */
95typedef struct Pager Pager;
96/*
97 * Each database file to be accessed by the system is an instance
98 * of the following structure.
99 */
100struct unqlite_db
101{
102 Pager *pPager; /* Pager and Transaction manager */
103 jx9 *pJx9; /* Jx9 Engine handle */
104 unqlite_kv_cursor *pCursor; /* Database cursor for common usage */
105};
106/*
107 * Each database connection is an instance of the following structure.
108 */
109struct unqlite
110{
111 SyMemBackend sMem; /* Memory allocator subsystem */
112 SyBlob sErr; /* Error log */
113 unqlite_db sDB; /* Storage backend */
114#if defined(UNQLITE_ENABLE_THREADS)
115 const SyMutexMethods *pMethods; /* Mutex methods */
116 SyMutex *pMutex; /* Per-handle mutex */
117#endif
118 unqlite_vm *pVms; /* List of active VM */
119 sxi32 iVm; /* Total number of active VM */
120 sxi32 iFlags; /* Control flags (See below) */
121 unqlite *pNext,*pPrev; /* List of active DB handles */
122 sxu32 nMagic; /* Sanity check against misuse */
123};
124#define UNQLITE_FL_DISABLE_AUTO_COMMIT 0x001 /* Disable auto-commit on close */
125/*
126 * VM control flags (Mostly related to collection handling).
127 */
128#define UNQLITE_VM_COLLECTION_CREATE 0x001 /* Create a new collection */
129#define UNQLITE_VM_COLLECTION_OVERWRITE 0x002 /* Overwrite old collection */
130#define UNQLITE_VM_AUTO_LOAD 0x004 /* Auto load a collection from the vfs */
131/* Forward declaration */
132typedef struct unqlite_col_record unqlite_col_record;
133typedef struct unqlite_col unqlite_col;
134/*
135 * Each an in-memory collection record is stored in an instance
136 * of the following structure.
137 */
138struct unqlite_col_record
139{
140 unqlite_col *pCol; /* Collecion this record belong */
141 jx9_int64 nId; /* Unique record ID */
142 jx9_value sValue; /* In-memory value of the record */
143 unqlite_col_record *pNextCol,*pPrevCol; /* Collision chain */
144 unqlite_col_record *pNext,*pPrev; /* Linked list of records */
145};
146/*
147 * Magic number to identify a valid collection on disk.
148 */
149#define UNQLITE_COLLECTION_MAGIC 0x611E /* sizeof(unsigned short) 2 bytes */
150/*
151 * A loaded collection is identified by an instance of the following structure.
152 */
153struct unqlite_col
154{
155 unqlite_vm *pVm; /* VM that own this instance */
156 SyString sName; /* ID of the collection */
157 sxu32 nHash; /* sName hash */
158 jx9_value sSchema; /* Collection schema */
159 sxu32 nSchemaOfft; /* Shema offset in sHeader */
160 SyBlob sWorker; /* General purpose working buffer */
161 SyBlob sHeader; /* Collection binary header */
162 jx9_int64 nLastid; /* Last collection record ID */
163 jx9_int64 nCurid; /* Current record ID */
164 jx9_int64 nTotRec; /* Total number of records in the collection */
165 int iFlags; /* Control flags (see below) */
166 unqlite_col_record **apRecord; /* Hashtable of loaded records */
167 unqlite_col_record *pList; /* Linked list of records */
168 sxu32 nRec; /* Total number of records in apRecord[] */
169 sxu32 nRecSize; /* apRecord[] size */
170 Sytm sCreation; /* Colleation creation time */
171 unqlite_kv_cursor *pCursor; /* Cursor pointing to the raw binary data */
172 unqlite_col *pNext,*pPrev; /* Next and previous collection in the chain */
173 unqlite_col *pNextCol,*pPrevCol; /* Collision chain */
174};
175/*
176 * Each unQLite Virtual Machine resulting from successful compilation of
177 * a Jx9 script is represented by an instance of the following structure.
178 */
179struct unqlite_vm
180{
181 unqlite *pDb; /* Database handle that own this instance */
182 SyMemBackend sAlloc; /* Private memory allocator */
183#if defined(UNQLITE_ENABLE_THREADS)
184 SyMutex *pMutex; /* Recursive mutex associated with this VM. */
185#endif
186 unqlite_col **apCol; /* Table of loaded collections */
187 unqlite_col *pCol; /* List of loaded collections */
188 sxu32 iCol; /* Total number of loaded collections */
189 sxu32 iColSize; /* apCol[] size */
190 jx9_vm *pJx9Vm; /* Compiled Jx9 script*/
191 unqlite_vm *pNext,*pPrev; /* Linked list of active unQLite VM */
192 sxu32 nMagic; /* Magic number to avoid misuse */
193};
194/*
195 * Database signature to identify a valid database image.
196 */
197#define UNQLITE_DB_SIG "unqlite"
198/*
199 * Database magic number (4 bytes).
200 */
201#define UNQLITE_DB_MAGIC 0xDB7C2712
202/*
203 * Maximum page size in bytes.
204 */
205#ifdef UNQLITE_MAX_PAGE_SIZE
206# undef UNQLITE_MAX_PAGE_SIZE
207#endif
208#define UNQLITE_MAX_PAGE_SIZE 65536 /* 65K */
209/*
210 * Minimum page size in bytes.
211 */
212#ifdef UNQLITE_MIN_PAGE_SIZE
213# undef UNQLITE_MIN_PAGE_SIZE
214#endif
215#define UNQLITE_MIN_PAGE_SIZE 512
216/*
217 * The default size of a database page.
218 */
219#ifndef UNQLITE_DEFAULT_PAGE_SIZE
220# undef UNQLITE_DEFAULT_PAGE_SIZE
221#endif
222# define UNQLITE_DEFAULT_PAGE_SIZE 4096 /* 4K */
223/* Forward declaration */
224typedef struct Bitvec Bitvec;
225/* Private library functions */
226/* api.c */
227UNQLITE_PRIVATE const SyMemBackend * unqliteExportMemBackend(void);
228UNQLITE_PRIVATE int unqliteDataConsumer(
229 const void *pOut, /* Data to consume */
230 unsigned int nLen, /* Data length */
231 void *pUserData /* User private data */
232 );
233UNQLITE_PRIVATE unqlite_kv_methods * unqliteFindKVStore(
234 const char *zName, /* Storage engine name [i.e. Hash, B+tree, LSM, etc.] */
235 sxu32 nByte /* zName length */
236 );
237UNQLITE_PRIVATE int unqliteGetPageSize(void);
238UNQLITE_PRIVATE int unqliteGenError(unqlite *pDb,const char *zErr);
239UNQLITE_PRIVATE int unqliteGenErrorFormat(unqlite *pDb,const char *zFmt,...);
240UNQLITE_PRIVATE int unqliteGenOutofMem(unqlite *pDb);
241/* unql_vm.c */
242UNQLITE_PRIVATE int unqliteCreateCollection(unqlite_vm *pVm,SyString *pName);
243UNQLITE_PRIVATE jx9_int64 unqliteCollectionLastRecordId(unqlite_col *pCol);
244UNQLITE_PRIVATE jx9_int64 unqliteCollectionCurrentRecordId(unqlite_col *pCol);
245UNQLITE_PRIVATE int unqliteCollectionCacheRemoveRecord(unqlite_col *pCol,jx9_int64 nId);
246UNQLITE_PRIVATE jx9_int64 unqliteCollectionTotalRecords(unqlite_col *pCol);
247UNQLITE_PRIVATE void unqliteCollectionResetRecordCursor(unqlite_col *pCol);
248UNQLITE_PRIVATE int unqliteCollectionFetchNextRecord(unqlite_col *pCol,jx9_value *pValue);
249UNQLITE_PRIVATE int unqliteCollectionFetchRecordById(unqlite_col *pCol,jx9_int64 nId,jx9_value *pValue);
250UNQLITE_PRIVATE unqlite_col * unqliteCollectionFetch(unqlite_vm *pVm,SyString *pCol,int iFlag);
251UNQLITE_PRIVATE int unqliteCollectionSetSchema(unqlite_col *pCol,jx9_value *pValue);
252UNQLITE_PRIVATE int unqliteCollectionPut(unqlite_col *pCol,jx9_value *pValue,int iFlag);
253UNQLITE_PRIVATE int unqliteCollectionDropRecord(unqlite_col *pCol,jx9_int64 nId,int wr_header,int log_err);
254UNQLITE_PRIVATE int unqliteDropCollection(unqlite_col *pCol);
255/* unql_jx9.c */
256UNQLITE_PRIVATE int unqliteRegisterJx9Functions(unqlite_vm *pVm);
257/* fastjson.c */
258UNQLITE_PRIVATE sxi32 FastJsonEncode(
259 jx9_value *pValue, /* Value to encode */
260 SyBlob *pOut, /* Store encoded value here */
261 int iNest /* Nesting limit */
262 );
263UNQLITE_PRIVATE sxi32 FastJsonDecode(
264 const void *pIn, /* Binary JSON */
265 sxu32 nByte, /* Chunk delimiter */
266 jx9_value *pOut, /* Decoded value */
267 const unsigned char **pzPtr,
268 int iNest /* Nesting limit */
269 );
270/* vfs.c [io_win.c, io_unix.c ] */
271UNQLITE_PRIVATE const unqlite_vfs * unqliteExportBuiltinVfs(void);
272/* mem_kv.c */
273UNQLITE_PRIVATE const unqlite_kv_methods * unqliteExportMemKvStorage(void);
274/* lhash_kv.c */
275UNQLITE_PRIVATE const unqlite_kv_methods * unqliteExportDiskKvStorage(void);
276/* os.c */
277UNQLITE_PRIVATE int unqliteOsRead(unqlite_file *id, void *pBuf, unqlite_int64 amt, unqlite_int64 offset);
278UNQLITE_PRIVATE int unqliteOsWrite(unqlite_file *id, const void *pBuf, unqlite_int64 amt, unqlite_int64 offset);
279UNQLITE_PRIVATE int unqliteOsTruncate(unqlite_file *id, unqlite_int64 size);
280UNQLITE_PRIVATE int unqliteOsSync(unqlite_file *id, int flags);
281UNQLITE_PRIVATE int unqliteOsFileSize(unqlite_file *id, unqlite_int64 *pSize);
282UNQLITE_PRIVATE int unqliteOsLock(unqlite_file *id, int lockType);
283UNQLITE_PRIVATE int unqliteOsUnlock(unqlite_file *id, int lockType);
284UNQLITE_PRIVATE int unqliteOsCheckReservedLock(unqlite_file *id, int *pResOut);
285UNQLITE_PRIVATE int unqliteOsSectorSize(unqlite_file *id);
286UNQLITE_PRIVATE int unqliteOsOpen(
287 unqlite_vfs *pVfs,
288 SyMemBackend *pAlloc,
289 const char *zPath,
290 unqlite_file **ppOut,
291 unsigned int flags
292);
293UNQLITE_PRIVATE int unqliteOsCloseFree(SyMemBackend *pAlloc,unqlite_file *pId);
294UNQLITE_PRIVATE int unqliteOsDelete(unqlite_vfs *pVfs, const char *zPath, int dirSync);
295UNQLITE_PRIVATE int unqliteOsAccess(unqlite_vfs *pVfs,const char *zPath,int flags,int *pResOut);
296/* bitmap.c */
297UNQLITE_PRIVATE Bitvec *unqliteBitvecCreate(SyMemBackend *pAlloc,pgno iSize);
298UNQLITE_PRIVATE int unqliteBitvecTest(Bitvec *p,pgno i);
299UNQLITE_PRIVATE int unqliteBitvecSet(Bitvec *p,pgno i);
300UNQLITE_PRIVATE void unqliteBitvecDestroy(Bitvec *p);
301/* pager.c */
302UNQLITE_PRIVATE int unqliteInitCursor(unqlite *pDb,unqlite_kv_cursor **ppOut);
303UNQLITE_PRIVATE int unqliteReleaseCursor(unqlite *pDb,unqlite_kv_cursor *pCur);
304UNQLITE_PRIVATE int unqlitePagerSetCachesize(Pager *pPager,int mxPage);
305UNQLITE_PRIVATE int unqlitePagerClose(Pager *pPager);
306UNQLITE_PRIVATE int unqlitePagerOpen(
307 unqlite_vfs *pVfs, /* The virtual file system to use */
308 unqlite *pDb, /* Database handle */
309 const char *zFilename, /* Name of the database file to open */
310 unsigned int iFlags /* flags controlling this file */
311 );
312UNQLITE_PRIVATE int unqlitePagerRegisterKvEngine(Pager *pPager,unqlite_kv_methods *pMethods);
313UNQLITE_PRIVATE unqlite_kv_engine * unqlitePagerGetKvEngine(unqlite *pDb);
314UNQLITE_PRIVATE int unqlitePagerBegin(Pager *pPager);
315UNQLITE_PRIVATE int unqlitePagerCommit(Pager *pPager);
316UNQLITE_PRIVATE int unqlitePagerRollback(Pager *pPager,int bResetKvEngine);
317UNQLITE_PRIVATE void unqlitePagerRandomString(Pager *pPager,char *zBuf,sxu32 nLen);
318UNQLITE_PRIVATE sxu32 unqlitePagerRandomNum(Pager *pPager);
319#endif /* __UNQLITEINT_H__ */