-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.c
2789 lines (2788 loc) · 88.5 KB
/
api.c
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Symisc unQLite: An Embeddable NoSQL (Post Modern) Database Engine.
* Copyright (C) 2012-2013, Symisc Systems http://unqlite.org/
* Version 1.1.6
* For information on licensing, redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES
* please contact Symisc Systems via:
* or visit:
* http://unqlite.org/licensing.html
*/
/* $SymiscID: api.c v2.0 FreeBSD 2012-11-08 23:07 stable <[email protected]> $ */
#ifndef UNQLITE_AMALGAMATION
#include "unqliteInt.h"
#endif
/* This file implement the public interfaces presented to host-applications.
* Routines in other files are for internal use by UnQLite and should not be
* accessed by users of the library.
*/
#define UNQLITE_DB_MISUSE(DB) (DB == 0 || DB->nMagic != UNQLITE_DB_MAGIC)
#define UNQLITE_VM_MISUSE(VM) (VM == 0 || VM->nMagic == JX9_VM_STALE)
/* If another thread have released a working instance, the following macros
* evaluates to true. These macros are only used when the library
* is built with threading support enabled.
*/
#define UNQLITE_THRD_DB_RELEASE(DB) (DB->nMagic != UNQLITE_DB_MAGIC)
#define UNQLITE_THRD_VM_RELEASE(VM) (VM->nMagic == JX9_VM_STALE)
/* IMPLEMENTATION: unqlite@embedded@symisc 118-09-4785 */
/*
* All global variables are collected in the structure named "sUnqlMPGlobal".
* That way it is clear in the code when we are using static variable because
* its name start with sUnqlMPGlobal.
*/
static struct unqlGlobal_Data
{
SyMemBackend sAllocator; /* Global low level memory allocator */
#if defined(UNQLITE_ENABLE_THREADS)
const SyMutexMethods *pMutexMethods; /* Mutex methods */
SyMutex *pMutex; /* Global mutex */
sxu32 nThreadingLevel; /* Threading level: 0 == Single threaded/1 == Multi-Threaded
* The threading level can be set using the [unqlite_lib_config()]
* interface with a configuration verb set to
* UNQLITE_LIB_CONFIG_THREAD_LEVEL_SINGLE or
* UNQLITE_LIB_CONFIG_THREAD_LEVEL_MULTI
*/
#endif
SySet kv_storage; /* Installed KV storage engines */
int iPageSize; /* Default Page size */
unqlite_vfs *pVfs; /* Underlying virtual file system (Vfs) */
sxi32 nDB; /* Total number of active DB handles */
unqlite *pDB; /* List of active DB handles */
sxu32 nMagic; /* Sanity check against library misuse */
}sUnqlMPGlobal = {
{0, 0, 0, 0, 0, 0, 0, 0, {0}},
#if defined(UNQLITE_ENABLE_THREADS)
0,
0,
0,
#endif
{0, 0, 0, 0, 0, 0, 0 },
UNQLITE_DEFAULT_PAGE_SIZE,
0,
0,
0,
0
};
#define UNQLITE_LIB_MAGIC 0xEA1495BA
#define UNQLITE_LIB_MISUSE (sUnqlMPGlobal.nMagic != UNQLITE_LIB_MAGIC)
/*
* Supported threading level.
* These options have meaning only when the library is compiled with multi-threading
* support. That is, the UNQLITE_ENABLE_THREADS compile time directive must be defined
* when UnQLite is built.
* UNQLITE_THREAD_LEVEL_SINGLE:
* In this mode, mutexing is disabled and the library can only be used by a single thread.
* UNQLITE_THREAD_LEVEL_MULTI
* In this mode, all mutexes including the recursive mutexes on [unqlite] objects
* are enabled so that the application is free to share the same database handle
* between different threads at the same time.
*/
#define UNQLITE_THREAD_LEVEL_SINGLE 1
#define UNQLITE_THREAD_LEVEL_MULTI 2
/*
* Find a Key Value storage engine from the set of installed engines.
* Return a pointer to the storage engine methods on success. NULL on failure.
*/
UNQLITE_PRIVATE unqlite_kv_methods * unqliteFindKVStore(
const char *zName, /* Storage engine name [i.e. Hash, B+tree, LSM, etc.] */
sxu32 nByte /* zName length */
)
{
unqlite_kv_methods **apStore,*pEntry;
sxu32 n,nMax;
/* Point to the set of installed engines */
apStore = (unqlite_kv_methods **)SySetBasePtr(&sUnqlMPGlobal.kv_storage);
nMax = SySetUsed(&sUnqlMPGlobal.kv_storage);
for( n = 0 ; n < nMax; ++n ){
pEntry = apStore[n];
if( nByte == SyStrlen(pEntry->zName) && SyStrnicmp(pEntry->zName,zName,nByte) == 0 ){
/* Storage engine found */
return pEntry;
}
}
/* No such entry, return NULL */
return 0;
}
/*
* Configure the UnQLite library.
* Return UNQLITE_OK on success. Any other return value indicates failure.
* Refer to [unqlite_lib_config()].
*/
static sxi32 unqliteCoreConfigure(sxi32 nOp, va_list ap)
{
int rc = UNQLITE_OK;
switch(nOp){
case UNQLITE_LIB_CONFIG_PAGE_SIZE: {
/* Default page size: Must be a power of two */
int iPage = va_arg(ap,int);
if( iPage >= UNQLITE_MIN_PAGE_SIZE && iPage <= UNQLITE_MAX_PAGE_SIZE ){
if( !(iPage & (iPage - 1)) ){
sUnqlMPGlobal.iPageSize = iPage;
}else{
/* Invalid page size */
rc = UNQLITE_INVALID;
}
}else{
/* Invalid page size */
rc = UNQLITE_INVALID;
}
break;
}
case UNQLITE_LIB_CONFIG_STORAGE_ENGINE: {
/* Install a key value storage engine */
unqlite_kv_methods *pMethods = va_arg(ap,unqlite_kv_methods *);
/* Make sure we are delaing with a valid methods */
if( pMethods == 0 || SX_EMPTY_STR(pMethods->zName) || pMethods->xSeek == 0 || pMethods->xData == 0
|| pMethods->xKey == 0 || pMethods->xDataLength == 0 || pMethods->xKeyLength == 0
|| pMethods->szKv < (int)sizeof(unqlite_kv_engine) ){
rc = UNQLITE_INVALID;
break;
}
/* Install it */
rc = SySetPut(&sUnqlMPGlobal.kv_storage,(const void *)&pMethods);
break;
}
case UNQLITE_LIB_CONFIG_VFS:{
/* Install a virtual file system */
unqlite_vfs *pVfs = va_arg(ap,unqlite_vfs *);
if( pVfs ){
sUnqlMPGlobal.pVfs = pVfs;
}
break;
}
case UNQLITE_LIB_CONFIG_USER_MALLOC: {
/* Use an alternative low-level memory allocation routines */
const SyMemMethods *pMethods = va_arg(ap, const SyMemMethods *);
/* Save the memory failure callback (if available) */
ProcMemError xMemErr = sUnqlMPGlobal.sAllocator.xMemError;
void *pMemErr = sUnqlMPGlobal.sAllocator.pUserData;
if( pMethods == 0 ){
/* Use the built-in memory allocation subsystem */
rc = SyMemBackendInit(&sUnqlMPGlobal.sAllocator, xMemErr, pMemErr);
}else{
rc = SyMemBackendInitFromOthers(&sUnqlMPGlobal.sAllocator, pMethods, xMemErr, pMemErr);
}
break;
}
case UNQLITE_LIB_CONFIG_MEM_ERR_CALLBACK: {
/* Memory failure callback */
ProcMemError xMemErr = va_arg(ap, ProcMemError);
void *pUserData = va_arg(ap, void *);
sUnqlMPGlobal.sAllocator.xMemError = xMemErr;
sUnqlMPGlobal.sAllocator.pUserData = pUserData;
break;
}
case UNQLITE_LIB_CONFIG_USER_MUTEX: {
#if defined(UNQLITE_ENABLE_THREADS)
/* Use an alternative low-level mutex subsystem */
const SyMutexMethods *pMethods = va_arg(ap, const SyMutexMethods *);
#if defined (UNTRUST)
if( pMethods == 0 ){
rc = UNQLITE_CORRUPT;
}
#endif
/* Sanity check */
if( pMethods->xEnter == 0 || pMethods->xLeave == 0 || pMethods->xNew == 0){
/* At least three criticial callbacks xEnter(), xLeave() and xNew() must be supplied */
rc = UNQLITE_CORRUPT;
break;
}
if( sUnqlMPGlobal.pMutexMethods ){
/* Overwrite the previous mutex subsystem */
SyMutexRelease(sUnqlMPGlobal.pMutexMethods, sUnqlMPGlobal.pMutex);
if( sUnqlMPGlobal.pMutexMethods->xGlobalRelease ){
sUnqlMPGlobal.pMutexMethods->xGlobalRelease();
}
sUnqlMPGlobal.pMutex = 0;
}
/* Initialize and install the new mutex subsystem */
if( pMethods->xGlobalInit ){
rc = pMethods->xGlobalInit();
if ( rc != UNQLITE_OK ){
break;
}
}
/* Create the global mutex */
sUnqlMPGlobal.pMutex = pMethods->xNew(SXMUTEX_TYPE_FAST);
if( sUnqlMPGlobal.pMutex == 0 ){
/*
* If the supplied mutex subsystem is so sick that we are unable to
* create a single mutex, there is no much we can do here.
*/
if( pMethods->xGlobalRelease ){
pMethods->xGlobalRelease();
}
rc = UNQLITE_CORRUPT;
break;
}
sUnqlMPGlobal.pMutexMethods = pMethods;
if( sUnqlMPGlobal.nThreadingLevel == 0 ){
/* Set a default threading level */
sUnqlMPGlobal.nThreadingLevel = UNQLITE_THREAD_LEVEL_MULTI;
}
#endif
break;
}
case UNQLITE_LIB_CONFIG_THREAD_LEVEL_SINGLE:
#if defined(UNQLITE_ENABLE_THREADS)
/* Single thread mode (Only one thread is allowed to play with the library) */
sUnqlMPGlobal.nThreadingLevel = UNQLITE_THREAD_LEVEL_SINGLE;
jx9_lib_config(JX9_LIB_CONFIG_THREAD_LEVEL_SINGLE);
#endif
break;
case UNQLITE_LIB_CONFIG_THREAD_LEVEL_MULTI:
#if defined(UNQLITE_ENABLE_THREADS)
/* Multi-threading mode (library is thread safe and database handles and virtual machines
* may be shared between multiple threads).
*/
sUnqlMPGlobal.nThreadingLevel = UNQLITE_THREAD_LEVEL_MULTI;
jx9_lib_config(JX9_LIB_CONFIG_THREAD_LEVEL_MULTI);
#endif
break;
default:
/* Unknown configuration option */
rc = UNQLITE_CORRUPT;
break;
}
return rc;
}
/*
* [CAPIREF: unqlite_lib_config()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
int unqlite_lib_config(int nConfigOp,...)
{
va_list ap;
int rc;
if( sUnqlMPGlobal.nMagic == UNQLITE_LIB_MAGIC ){
/* Library is already initialized, this operation is forbidden */
return UNQLITE_LOCKED;
}
va_start(ap,nConfigOp);
rc = unqliteCoreConfigure(nConfigOp,ap);
va_end(ap);
return rc;
}
/*
* Global library initialization
* Refer to [unqlite_lib_init()]
* This routine must be called to initialize the memory allocation subsystem, the mutex
* subsystem prior to doing any serious work with the library. The first thread to call
* this routine does the initialization process and set the magic number so no body later
* can re-initialize the library. If subsequent threads call this routine before the first
* thread have finished the initialization process, then the subsequent threads must block
* until the initialization process is done.
*/
static sxi32 unqliteCoreInitialize(void)
{
const unqlite_kv_methods *pMethods;
const unqlite_vfs *pVfs; /* Built-in vfs */
#if defined(UNQLITE_ENABLE_THREADS)
const SyMutexMethods *pMutexMethods = 0;
SyMutex *pMaster = 0;
#endif
int rc;
/*
* If the library is already initialized, then a call to this routine
* is a no-op.
*/
if( sUnqlMPGlobal.nMagic == UNQLITE_LIB_MAGIC ){
return UNQLITE_OK; /* Already initialized */
}
/* Point to the built-in vfs */
pVfs = unqliteExportBuiltinVfs();
/* Install it */
unqlite_lib_config(UNQLITE_LIB_CONFIG_VFS, pVfs);
#if defined(UNQLITE_ENABLE_THREADS)
if( sUnqlMPGlobal.nThreadingLevel != UNQLITE_THREAD_LEVEL_SINGLE ){
pMutexMethods = sUnqlMPGlobal.pMutexMethods;
if( pMutexMethods == 0 ){
/* Use the built-in mutex subsystem */
pMutexMethods = SyMutexExportMethods();
if( pMutexMethods == 0 ){
return UNQLITE_CORRUPT; /* Can't happen */
}
/* Install the mutex subsystem */
rc = unqlite_lib_config(UNQLITE_LIB_CONFIG_USER_MUTEX, pMutexMethods);
if( rc != UNQLITE_OK ){
return rc;
}
}
/* Obtain a static mutex so we can initialize the library without calling malloc() */
pMaster = SyMutexNew(pMutexMethods, SXMUTEX_TYPE_STATIC_1);
if( pMaster == 0 ){
return UNQLITE_CORRUPT; /* Can't happen */
}
}
/* Lock the master mutex */
rc = UNQLITE_OK;
SyMutexEnter(pMutexMethods, pMaster); /* NO-OP if sUnqlMPGlobal.nThreadingLevel == UNQLITE_THREAD_LEVEL_SINGLE */
if( sUnqlMPGlobal.nMagic != UNQLITE_LIB_MAGIC ){
#endif
if( sUnqlMPGlobal.sAllocator.pMethods == 0 ){
/* Install a memory subsystem */
rc = unqlite_lib_config(UNQLITE_LIB_CONFIG_USER_MALLOC, 0); /* zero mean use the built-in memory backend */
if( rc != UNQLITE_OK ){
/* If we are unable to initialize the memory backend, there is no much we can do here.*/
goto End;
}
}
#if defined(UNQLITE_ENABLE_THREADS)
if( sUnqlMPGlobal.nThreadingLevel > UNQLITE_THREAD_LEVEL_SINGLE ){
/* Protect the memory allocation subsystem */
rc = SyMemBackendMakeThreadSafe(&sUnqlMPGlobal.sAllocator, sUnqlMPGlobal.pMutexMethods);
if( rc != UNQLITE_OK ){
goto End;
}
}
#endif
SySetInit(&sUnqlMPGlobal.kv_storage,&sUnqlMPGlobal.sAllocator,sizeof(unqlite_kv_methods *));
/* Install the built-in Key Value storage engines */
pMethods = unqliteExportMemKvStorage(); /* In-memory storage */
unqlite_lib_config(UNQLITE_LIB_CONFIG_STORAGE_ENGINE,pMethods);
/* Default disk key/value storage engine */
pMethods = unqliteExportDiskKvStorage(); /* Disk storage */
unqlite_lib_config(UNQLITE_LIB_CONFIG_STORAGE_ENGINE,pMethods);
/* Default page size */
if( sUnqlMPGlobal.iPageSize < UNQLITE_MIN_PAGE_SIZE ){
unqlite_lib_config(UNQLITE_LIB_CONFIG_PAGE_SIZE,UNQLITE_DEFAULT_PAGE_SIZE);
}
/* Our library is initialized, set the magic number */
sUnqlMPGlobal.nMagic = UNQLITE_LIB_MAGIC;
rc = UNQLITE_OK;
#if defined(UNQLITE_ENABLE_THREADS)
} /* sUnqlMPGlobal.nMagic != UNQLITE_LIB_MAGIC */
#endif
End:
#if defined(UNQLITE_ENABLE_THREADS)
/* Unlock the master mutex */
SyMutexLeave(pMutexMethods, pMaster); /* NO-OP if sUnqlMPGlobal.nThreadingLevel == UNQLITE_THREAD_LEVEL_SINGLE */
#endif
return rc;
}
/* Forward declaration */
static int unqliteVmRelease(unqlite_vm *pVm);
/*
* Release a single instance of an unqlite database handle.
*/
static int unqliteDbRelease(unqlite *pDb)
{
unqlite_db *pStore = &pDb->sDB;
unqlite_vm *pVm,*pNext;
int rc = UNQLITE_OK;
if( (pDb->iFlags & UNQLITE_FL_DISABLE_AUTO_COMMIT) == 0 ){
/* Commit any outstanding transaction */
rc = unqlitePagerCommit(pStore->pPager);
if( rc != UNQLITE_OK ){
/* Rollback the transaction */
rc = unqlitePagerRollback(pStore->pPager,FALSE);
}
}else{
/* Rollback any outstanding transaction */
rc = unqlitePagerRollback(pStore->pPager,FALSE);
}
/* Close the pager */
unqlitePagerClose(pStore->pPager);
/* Release any active VM's */
pVm = pDb->pVms;
for(;;){
if( pDb->iVm < 1 ){
break;
}
/* Point to the next entry */
pNext = pVm->pNext;
unqliteVmRelease(pVm);
pVm = pNext;
pDb->iVm--;
}
/* Release the Jx9 handle */
jx9_release(pStore->pJx9);
/* Set a dummy magic number */
pDb->nMagic = 0x7250;
/* Release the whole memory subsystem */
SyMemBackendRelease(&pDb->sMem);
/* Commit or rollback result */
return rc;
}
/*
* Release all resources consumed by the library.
* Note: This call is not thread safe. Refer to [unqlite_lib_shutdown()].
*/
static void unqliteCoreShutdown(void)
{
unqlite *pDb, *pNext;
/* Release all active databases handles */
pDb = sUnqlMPGlobal.pDB;
for(;;){
if( sUnqlMPGlobal.nDB < 1 ){
break;
}
pNext = pDb->pNext;
unqliteDbRelease(pDb);
pDb = pNext;
sUnqlMPGlobal.nDB--;
}
/* Release the storage methods container */
SySetRelease(&sUnqlMPGlobal.kv_storage);
#if defined(UNQLITE_ENABLE_THREADS)
/* Release the mutex subsystem */
if( sUnqlMPGlobal.pMutexMethods ){
if( sUnqlMPGlobal.pMutex ){
SyMutexRelease(sUnqlMPGlobal.pMutexMethods, sUnqlMPGlobal.pMutex);
sUnqlMPGlobal.pMutex = 0;
}
if( sUnqlMPGlobal.pMutexMethods->xGlobalRelease ){
sUnqlMPGlobal.pMutexMethods->xGlobalRelease();
}
sUnqlMPGlobal.pMutexMethods = 0;
}
sUnqlMPGlobal.nThreadingLevel = 0;
#endif
if( sUnqlMPGlobal.sAllocator.pMethods ){
/* Release the memory backend */
SyMemBackendRelease(&sUnqlMPGlobal.sAllocator);
}
sUnqlMPGlobal.nMagic = 0x1764;
/* Finally, shutdown the Jx9 library */
jx9_lib_shutdown();
}
/*
* [CAPIREF: unqlite_lib_init()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
int unqlite_lib_init(void)
{
int rc;
rc = unqliteCoreInitialize();
return rc;
}
/*
* [CAPIREF: unqlite_lib_shutdown()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
int unqlite_lib_shutdown(void)
{
if( sUnqlMPGlobal.nMagic != UNQLITE_LIB_MAGIC ){
/* Already shut */
return UNQLITE_OK;
}
unqliteCoreShutdown();
return UNQLITE_OK;
}
/*
* [CAPIREF: unqlite_lib_is_threadsafe()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
int unqlite_lib_is_threadsafe(void)
{
if( sUnqlMPGlobal.nMagic != UNQLITE_LIB_MAGIC ){
return 0;
}
#if defined(UNQLITE_ENABLE_THREADS)
if( sUnqlMPGlobal.nThreadingLevel > UNQLITE_THREAD_LEVEL_SINGLE ){
/* Muli-threading support is enabled */
return 1;
}else{
/* Single-threading */
return 0;
}
#else
return 0;
#endif
}
/*
*
* [CAPIREF: unqlite_lib_version()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
const char * unqlite_lib_version(void)
{
return UNQLITE_VERSION;
}
/*
*
* [CAPIREF: unqlite_lib_signature()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
const char * unqlite_lib_signature(void)
{
return UNQLITE_SIG;
}
/*
*
* [CAPIREF: unqlite_lib_ident()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
const char * unqlite_lib_ident(void)
{
return UNQLITE_IDENT;
}
/*
*
* [CAPIREF: unqlite_lib_copyright()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
const char * unqlite_lib_copyright(void)
{
return UNQLITE_COPYRIGHT;
}
/*
* Remove harmfull and/or stale flags passed to the [unqlite_open()] interface.
*/
static unsigned int unqliteSanityzeFlag(unsigned int iFlags)
{
iFlags &= ~UNQLITE_OPEN_EXCLUSIVE; /* Reserved flag */
if( iFlags & UNQLITE_OPEN_TEMP_DB ){
/* Omit journaling for temporary database */
iFlags |= UNQLITE_OPEN_OMIT_JOURNALING|UNQLITE_OPEN_CREATE;
}
if( (iFlags & (UNQLITE_OPEN_READONLY|UNQLITE_OPEN_READWRITE)) == 0 ){
/* Auto-append the R+W flag */
iFlags |= UNQLITE_OPEN_READWRITE;
}
if( iFlags & UNQLITE_OPEN_CREATE ){
iFlags &= ~(UNQLITE_OPEN_MMAP|UNQLITE_OPEN_READONLY);
/* Auto-append the R+W flag */
iFlags |= UNQLITE_OPEN_READWRITE;
}else{
if( iFlags & UNQLITE_OPEN_READONLY ){
iFlags &= ~UNQLITE_OPEN_READWRITE;
}else if( iFlags & UNQLITE_OPEN_READWRITE ){
iFlags &= ~UNQLITE_OPEN_MMAP;
}
}
return iFlags;
}
/*
* This routine does the work of initializing a database handle on behalf
* of [unqlite_open()].
*/
static int unqliteInitDatabase(
unqlite *pDB, /* Database handle */
SyMemBackend *pParent, /* Master memory backend */
const char *zFilename, /* Target database */
unsigned int iFlags /* Open flags */
)
{
unqlite_db *pStorage = &pDB->sDB;
int rc;
/* Initialiaze the memory subsystem */
SyMemBackendInitFromParent(&pDB->sMem,pParent);
#if defined(UNQLITE_ENABLE_THREADS)
/* No need for internal mutexes */
SyMemBackendDisbaleMutexing(&pDB->sMem);
#endif
SyBlobInit(&pDB->sErr,&pDB->sMem);
/* Sanityze flags */
iFlags = unqliteSanityzeFlag(iFlags);
/* Init the pager and the transaction manager */
rc = unqlitePagerOpen(sUnqlMPGlobal.pVfs,pDB,zFilename,iFlags);
if( rc != UNQLITE_OK ){
return rc;
}
/* Allocate a new Jx9 engine handle */
rc = jx9_init(&pStorage->pJx9);
if( rc != JX9_OK ){
return rc;
}
return UNQLITE_OK;
}
/*
* Allocate and initialize a new UnQLite Virtual Mahcine and attach it
* to the compiled Jx9 script.
*/
static int unqliteInitVm(unqlite *pDb,jx9_vm *pJx9Vm,unqlite_vm **ppOut)
{
unqlite_vm *pVm;
*ppOut = 0;
/* Allocate a new VM instance */
pVm = (unqlite_vm *)SyMemBackendPoolAlloc(&pDb->sMem,sizeof(unqlite_vm));
if( pVm == 0 ){
return UNQLITE_NOMEM;
}
/* Zero the structure */
SyZero(pVm,sizeof(unqlite_vm));
/* Initialize */
SyMemBackendInitFromParent(&pVm->sAlloc,&pDb->sMem);
/* Allocate a new collection table */
pVm->apCol = (unqlite_col **)SyMemBackendAlloc(&pVm->sAlloc,32 * sizeof(unqlite_col *));
if( pVm->apCol == 0 ){
goto fail;
}
pVm->iColSize = 32; /* Must be a power of two */
/* Zero the table */
SyZero((void *)pVm->apCol,pVm->iColSize * sizeof(unqlite_col *));
#if defined(UNQLITE_ENABLE_THREADS)
if( sUnqlMPGlobal.nThreadingLevel > UNQLITE_THREAD_LEVEL_SINGLE ){
/* Associate a recursive mutex with this instance */
pVm->pMutex = SyMutexNew(sUnqlMPGlobal.pMutexMethods, SXMUTEX_TYPE_RECURSIVE);
if( pVm->pMutex == 0 ){
goto fail;
}
}
#endif
/* Link the VM to the list of active virtual machines */
pVm->pJx9Vm = pJx9Vm;
pVm->pDb = pDb;
MACRO_LD_PUSH(pDb->pVms,pVm);
pDb->iVm++;
/* Register Jx9 functions */
unqliteRegisterJx9Functions(pVm);
/* Set the magic number */
pVm->nMagic = JX9_VM_INIT; /* Same magic number as Jx9 */
/* All done */
*ppOut = pVm;
return UNQLITE_OK;
fail:
SyMemBackendRelease(&pVm->sAlloc);
SyMemBackendPoolFree(&pDb->sMem,pVm);
return UNQLITE_NOMEM;
}
/*
* Release an active VM.
*/
static int unqliteVmRelease(unqlite_vm *pVm)
{
/* Release the Jx9 VM */
jx9_vm_release(pVm->pJx9Vm);
/* Release the private memory backend */
SyMemBackendRelease(&pVm->sAlloc);
/* Upper layer will discard this VM from the list
* of active VM.
*/
return UNQLITE_OK;
}
/*
* Return the default page size.
*/
UNQLITE_PRIVATE int unqliteGetPageSize(void)
{
int iSize = sUnqlMPGlobal.iPageSize;
if( iSize < UNQLITE_MIN_PAGE_SIZE || iSize > UNQLITE_MAX_PAGE_SIZE ){
iSize = UNQLITE_DEFAULT_PAGE_SIZE;
}
return iSize;
}
/*
* Generate an error message.
*/
UNQLITE_PRIVATE int unqliteGenError(unqlite *pDb,const char *zErr)
{
int rc;
/* Append the error message */
rc = SyBlobAppend(&pDb->sErr,(const void *)zErr,SyStrlen(zErr));
/* Append a new line */
SyBlobAppend(&pDb->sErr,(const void *)"\n",sizeof(char));
return rc;
}
/*
* Generate an error message (Printf like).
*/
UNQLITE_PRIVATE int unqliteGenErrorFormat(unqlite *pDb,const char *zFmt,...)
{
va_list ap;
int rc;
va_start(ap,zFmt);
rc = SyBlobFormatAp(&pDb->sErr,zFmt,ap);
va_end(ap);
/* Append a new line */
SyBlobAppend(&pDb->sErr,(const void *)"\n",sizeof(char));
return rc;
}
/*
* Generate an error message (Out of memory).
*/
UNQLITE_PRIVATE int unqliteGenOutofMem(unqlite *pDb)
{
int rc;
rc = unqliteGenError(pDb,"unQLite is running out of memory");
return rc;
}
/*
* Configure a working UnQLite database handle.
*/
static int unqliteConfigure(unqlite *pDb,int nOp,va_list ap)
{
int rc = UNQLITE_OK;
switch(nOp){
case UNQLITE_CONFIG_JX9_ERR_LOG:
/* Jx9 compile-time error log */
rc = jx9EngineConfig(pDb->sDB.pJx9,JX9_CONFIG_ERR_LOG,ap);
break;
case UNQLITE_CONFIG_MAX_PAGE_CACHE: {
int max_page = va_arg(ap,int);
/* Maximum number of page to cache (Simple hint). */
rc = unqlitePagerSetCachesize(pDb->sDB.pPager,max_page);
break;
}
case UNQLITE_CONFIG_ERR_LOG: {
/* Database error log if any */
const char **pzPtr = va_arg(ap, const char **);
int *pLen = va_arg(ap, int *);
if( pzPtr == 0 ){
rc = JX9_CORRUPT;
break;
}
/* NULL terminate the error-log buffer */
SyBlobNullAppend(&pDb->sErr);
/* Point to the error-log buffer */
*pzPtr = (const char *)SyBlobData(&pDb->sErr);
if( pLen ){
if( SyBlobLength(&pDb->sErr) > 1 /* NULL '\0' terminator */ ){
*pLen = (int)SyBlobLength(&pDb->sErr);
}else{
*pLen = 0;
}
}
break;
}
case UNQLITE_CONFIG_DISABLE_AUTO_COMMIT:{
/* Disable auto-commit */
pDb->iFlags |= UNQLITE_FL_DISABLE_AUTO_COMMIT;
break;
}
case UNQLITE_CONFIG_GET_KV_NAME: {
/* Name of the underlying KV storage engine */
const char **pzPtr = va_arg(ap,const char **);
if( pzPtr ){
unqlite_kv_engine *pEngine;
pEngine = unqlitePagerGetKvEngine(pDb);
/* Point to the name */
*pzPtr = pEngine->pIo->pMethods->zName;
}
break;
}
default:
/* Unknown configuration option */
rc = UNQLITE_UNKNOWN;
break;
}
return rc;
}
/*
* Export the global (master) memory allocator to submodules.
*/
UNQLITE_PRIVATE const SyMemBackend * unqliteExportMemBackend(void)
{
return &sUnqlMPGlobal.sAllocator;
}
/*
* [CAPIREF: unqlite_open()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
int unqlite_open(unqlite **ppDB,const char *zFilename,unsigned int iMode)
{
unqlite *pHandle;
int rc;
#if defined(UNTRUST)
if( ppDB == 0 ){
return UNQLITE_CORRUPT;
}
#endif
*ppDB = 0;
/* One-time automatic library initialization */
rc = unqliteCoreInitialize();
if( rc != UNQLITE_OK ){
return rc;
}
/* Allocate a new database handle */
pHandle = (unqlite *)SyMemBackendPoolAlloc(&sUnqlMPGlobal.sAllocator, sizeof(unqlite));
if( pHandle == 0 ){
return UNQLITE_NOMEM;
}
/* Zero the structure */
SyZero(pHandle,sizeof(unqlite));
if( iMode < 1 ){
/* Assume a read-only database */
iMode = UNQLITE_OPEN_READONLY|UNQLITE_OPEN_MMAP;
}
/* Init the database */
rc = unqliteInitDatabase(pHandle,&sUnqlMPGlobal.sAllocator,zFilename,iMode);
if( rc != UNQLITE_OK ){
goto Release;
}
#if defined(UNQLITE_ENABLE_THREADS)
if( !(iMode & UNQLITE_OPEN_NOMUTEX) && (sUnqlMPGlobal.nThreadingLevel > UNQLITE_THREAD_LEVEL_SINGLE) ){
/* Associate a recursive mutex with this instance */
pHandle->pMutex = SyMutexNew(sUnqlMPGlobal.pMutexMethods, SXMUTEX_TYPE_RECURSIVE);
if( pHandle->pMutex == 0 ){
rc = UNQLITE_NOMEM;
goto Release;
}
}
#endif
/* Link to the list of active DB handles */
#if defined(UNQLITE_ENABLE_THREADS)
/* Enter the global mutex */
SyMutexEnter(sUnqlMPGlobal.pMutexMethods, sUnqlMPGlobal.pMutex); /* NO-OP if sUnqlMPGlobal.nThreadingLevel == UNQLITE_THREAD_LEVEL_SINGLE */
#endif
MACRO_LD_PUSH(sUnqlMPGlobal.pDB,pHandle);
sUnqlMPGlobal.nDB++;
#if defined(UNQLITE_ENABLE_THREADS)
/* Leave the global mutex */
SyMutexLeave(sUnqlMPGlobal.pMutexMethods, sUnqlMPGlobal.pMutex); /* NO-OP if sUnqlMPGlobal.nThreadingLevel == UNQLITE_THREAD_LEVEL_SINGLE */
#endif
/* Set the magic number to identify a valid DB handle */
pHandle->nMagic = UNQLITE_DB_MAGIC;
/* Make the handle available to the caller */
*ppDB = pHandle;
return UNQLITE_OK;
Release:
SyMemBackendRelease(&pHandle->sMem);
SyMemBackendPoolFree(&sUnqlMPGlobal.sAllocator,pHandle);
return rc;
}
/*
* [CAPIREF: unqlite_config()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
int unqlite_config(unqlite *pDb,int nConfigOp,...)
{
va_list ap;
int rc;
if( UNQLITE_DB_MISUSE(pDb) ){
return UNQLITE_CORRUPT;
}
#if defined(UNQLITE_ENABLE_THREADS)
/* Acquire DB mutex */
SyMutexEnter(sUnqlMPGlobal.pMutexMethods, pDb->pMutex); /* NO-OP if sUnqlMPGlobal.nThreadingLevel != UNQLITE_THREAD_LEVEL_MULTI */
if( sUnqlMPGlobal.nThreadingLevel > UNQLITE_THREAD_LEVEL_SINGLE &&
UNQLITE_THRD_DB_RELEASE(pDb) ){
return UNQLITE_ABORT; /* Another thread have released this instance */
}
#endif
va_start(ap, nConfigOp);
rc = unqliteConfigure(&(*pDb),nConfigOp, ap);
va_end(ap);
#if defined(UNQLITE_ENABLE_THREADS)
/* Leave DB mutex */
SyMutexLeave(sUnqlMPGlobal.pMutexMethods,pDb->pMutex); /* NO-OP if sUnqlMPGlobal.nThreadingLevel != UNQLITE_THREAD_LEVEL_MULTI */
#endif
return rc;
}
/*
* [CAPIREF: unqlite_close()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
int unqlite_close(unqlite *pDb)
{
int rc;
if( UNQLITE_DB_MISUSE(pDb) ){
return UNQLITE_CORRUPT;
}
#if defined(UNQLITE_ENABLE_THREADS)
/* Acquire DB mutex */
SyMutexEnter(sUnqlMPGlobal.pMutexMethods, pDb->pMutex); /* NO-OP if sUnqlMPGlobal.nThreadingLevel != UNQLITE_THREAD_LEVEL_MULTI */
if( sUnqlMPGlobal.nThreadingLevel > UNQLITE_THREAD_LEVEL_SINGLE &&
UNQLITE_THRD_DB_RELEASE(pDb) ){
return UNQLITE_ABORT; /* Another thread have released this instance */
}
#endif
/* Release the database handle */
rc = unqliteDbRelease(pDb);
#if defined(UNQLITE_ENABLE_THREADS)
/* Leave DB mutex */
SyMutexLeave(sUnqlMPGlobal.pMutexMethods, pDb->pMutex); /* NO-OP if sUnqlMPGlobal.nThreadingLevel != UNQLITE_THREAD_LEVEL_MULTI */
/* Release DB mutex */
SyMutexRelease(sUnqlMPGlobal.pMutexMethods, pDb->pMutex) /* NO-OP if sUnqlMPGlobal.nThreadingLevel != UNQLITE_THREAD_LEVEL_MULTI */
#endif
#if defined(UNQLITE_ENABLE_THREADS)
/* Enter the global mutex */
SyMutexEnter(sUnqlMPGlobal.pMutexMethods, sUnqlMPGlobal.pMutex); /* NO-OP if sUnqlMPGlobal.nThreadingLevel == UNQLITE_THREAD_LEVEL_SINGLE */
#endif
/* Unlink from the list of active database handles */
MACRO_LD_REMOVE(sUnqlMPGlobal.pDB, pDb);
sUnqlMPGlobal.nDB--;
#if defined(UNQLITE_ENABLE_THREADS)
/* Leave the global mutex */
SyMutexLeave(sUnqlMPGlobal.pMutexMethods, sUnqlMPGlobal.pMutex); /* NO-OP if sUnqlMPGlobal.nThreadingLevel == UNQLITE_THREAD_LEVEL_SINGLE */
#endif
/* Release the memory chunk allocated to this handle */
SyMemBackendPoolFree(&sUnqlMPGlobal.sAllocator,pDb);
return rc;
}
/*
* [CAPIREF: unqlite_compile()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
int unqlite_compile(unqlite *pDb,const char *zJx9,int nByte,unqlite_vm **ppOut)
{
jx9_vm *pVm;
int rc;
if( UNQLITE_DB_MISUSE(pDb) || ppOut == 0){
return UNQLITE_CORRUPT;
}
#if defined(UNQLITE_ENABLE_THREADS)
/* Acquire DB mutex */
SyMutexEnter(sUnqlMPGlobal.pMutexMethods, pDb->pMutex); /* NO-OP if sUnqlMPGlobal.nThreadingLevel != UNQLITE_THREAD_LEVEL_MULTI */
if( sUnqlMPGlobal.nThreadingLevel > UNQLITE_THREAD_LEVEL_SINGLE &&
UNQLITE_THRD_DB_RELEASE(pDb) ){
return UNQLITE_ABORT;
}
#endif
/* Compile the Jx9 script first */
rc = jx9_compile(pDb->sDB.pJx9,zJx9,nByte,&pVm);
if( rc == JX9_OK ){
/* Allocate a new unqlite VM instance */
rc = unqliteInitVm(pDb,pVm,ppOut);
if( rc != UNQLITE_OK ){
/* Release the Jx9 VM */
jx9_vm_release(pVm);
}
}
#if defined(UNQLITE_ENABLE_THREADS)
/* Leave DB mutex */
SyMutexLeave(sUnqlMPGlobal.pMutexMethods,pDb->pMutex); /* NO-OP if sUnqlMPGlobal.nThreadingLevel != UNQLITE_THREAD_LEVEL_MULTI */
#endif
return rc;
}
/*
* [CAPIREF: unqlite_compile_file()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
int unqlite_compile_file(unqlite *pDb,const char *zPath,unqlite_vm **ppOut)
{
jx9_vm *pVm;
int rc;
if( UNQLITE_DB_MISUSE(pDb) || ppOut == 0){
return UNQLITE_CORRUPT;
}
#if defined(UNQLITE_ENABLE_THREADS)
/* Acquire DB mutex */
SyMutexEnter(sUnqlMPGlobal.pMutexMethods, pDb->pMutex); /* NO-OP if sUnqlMPGlobal.nThreadingLevel != UNQLITE_THREAD_LEVEL_MULTI */
if( sUnqlMPGlobal.nThreadingLevel > UNQLITE_THREAD_LEVEL_SINGLE &&
UNQLITE_THRD_DB_RELEASE(pDb) ){
return UNQLITE_ABORT;
}
#endif
/* Compile the Jx9 script first */
rc = jx9_compile_file(pDb->sDB.pJx9,zPath,&pVm);
if( rc == JX9_OK ){
/* Allocate a new unqlite VM instance */
rc = unqliteInitVm(pDb,pVm,ppOut);
if( rc != UNQLITE_OK ){
/* Release the Jx9 VM */
jx9_vm_release(pVm);
}
}
#if defined(UNQLITE_ENABLE_THREADS)
/* Leave DB mutex */
SyMutexLeave(sUnqlMPGlobal.pMutexMethods,pDb->pMutex); /* NO-OP if sUnqlMPGlobal.nThreadingLevel != UNQLITE_THREAD_LEVEL_MULTI */
#endif
return rc;
}
/*
* Configure an unqlite virtual machine (Mostly Jx9 VM) instance.
*/
static int unqliteVmConfig(unqlite_vm *pVm,sxi32 iOp,va_list ap)
{
int rc;
rc = jx9VmConfigure(pVm->pJx9Vm,iOp,ap);
return rc;
}
/*
* [CAPIREF: unqlite_vm_config()]
* Please refer to the official documentation for function purpose and expected parameters.
*/
int unqlite_vm_config(unqlite_vm *pVm,int iOp,...)
{
va_list ap;
int rc;
if( UNQLITE_VM_MISUSE(pVm) ){
return UNQLITE_CORRUPT;
}
#if defined(UNQLITE_ENABLE_THREADS)
/* Acquire VM mutex */
SyMutexEnter(sUnqlMPGlobal.pMutexMethods, pVm->pMutex); /* NO-OP if sUnqlMPGlobal.nThreadingLevel != UNQLITE_THREAD_LEVEL_MULTI */
if( sUnqlMPGlobal.nThreadingLevel > UNQLITE_THREAD_LEVEL_SINGLE &&