OLD | NEW |
1 /* | 1 /* |
2 ** 2010 October 28 | 2 ** 2010 October 28 |
3 ** | 3 ** |
4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
6 ** | 6 ** |
7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
10 ** | 10 ** |
11 ************************************************************************* | 11 ************************************************************************* |
12 ** | 12 ** |
13 ** This file contains a VFS "shim" - a layer that sits in between the | 13 ** This file contains a VFS "shim" - a layer that sits in between the |
14 ** pager and the real VFS. | 14 ** pager and the real VFS - that breaks up a very large database file |
| 15 ** into two or more smaller files on disk. This is useful, for example, |
| 16 ** in order to support large, multi-gigabyte databases on older filesystems |
| 17 ** that limit the maximum file size to 2 GiB. |
15 ** | 18 ** |
16 ** This particular shim enforces a multiplex system on DB files. | 19 ** USAGE: |
17 ** This shim shards/partitions a single DB file into smaller | |
18 ** "chunks" such that the total DB file size may exceed the maximum | |
19 ** file size of the underlying file system. | |
20 ** | 20 ** |
| 21 ** Compile this source file and link it with your application. Then |
| 22 ** at start-time, invoke the following procedure: |
| 23 ** |
| 24 ** int sqlite3_multiplex_initialize( |
| 25 ** const char *zOrigVfsName, // The underlying real VFS |
| 26 ** int makeDefault // True to make multiplex the default VFS |
| 27 ** ); |
| 28 ** |
| 29 ** The procedure call above will create and register a new VFS shim named |
| 30 ** "multiplex". The multiplex VFS will use the VFS named by zOrigVfsName to |
| 31 ** do the actual disk I/O. (The zOrigVfsName parameter may be NULL, in |
| 32 ** which case the default VFS at the moment sqlite3_multiplex_initialize() |
| 33 ** is called will be used as the underlying real VFS.) |
| 34 ** |
| 35 ** If the makeDefault parameter is TRUE then multiplex becomes the new |
| 36 ** default VFS. Otherwise, you can use the multiplex VFS by specifying |
| 37 ** "multiplex" as the 4th parameter to sqlite3_open_v2() or by employing |
| 38 ** URI filenames and adding "vfs=multiplex" as a parameter to the filename |
| 39 ** URI. |
| 40 ** |
| 41 ** The multiplex VFS allows databases up to 32 GiB in size. But it splits |
| 42 ** the files up into smaller pieces, so that they will work even on |
| 43 ** filesystems that do not support large files. The default chunk size |
| 44 ** is 2147418112 bytes (which is 64KiB less than 2GiB) but this can be |
| 45 ** changed at compile-time by defining the SQLITE_MULTIPLEX_CHUNK_SIZE |
| 46 ** macro. Use the "chunksize=NNNN" query parameter with a URI filename |
| 47 ** in order to select an alternative chunk size for individual connections |
| 48 ** at run-time. |
21 */ | 49 */ |
22 #include "sqlite3.h" | 50 #include "sqlite3.h" |
23 #include <string.h> | 51 #include <string.h> |
24 #include <assert.h> | 52 #include <assert.h> |
| 53 #include <stdlib.h> |
25 #include "test_multiplex.h" | 54 #include "test_multiplex.h" |
26 | 55 |
27 #ifndef SQLITE_CORE | 56 #ifndef SQLITE_CORE |
28 #define SQLITE_CORE 1 /* Disable the API redefinition in sqlite3ext.h */ | 57 #define SQLITE_CORE 1 /* Disable the API redefinition in sqlite3ext.h */ |
29 #endif | 58 #endif |
30 #include "sqlite3ext.h" | 59 #include "sqlite3ext.h" |
31 | 60 |
32 /* | 61 /* |
33 ** These should be defined to be the same as the values in | 62 ** These should be defined to be the same as the values in |
34 ** sqliteInt.h. They are defined seperately here so that | 63 ** sqliteInt.h. They are defined separately here so that |
35 ** the multiplex VFS shim can be built as a loadable | 64 ** the multiplex VFS shim can be built as a loadable |
36 ** module. | 65 ** module. |
37 */ | 66 */ |
38 #define UNUSED_PARAMETER(x) (void)(x) | 67 #define UNUSED_PARAMETER(x) (void)(x) |
39 #define MAX_PAGE_SIZE 0x10000 | 68 #define MAX_PAGE_SIZE 0x10000 |
40 #define DEFAULT_SECTOR_SIZE 0x1000 | 69 #define DEFAULT_SECTOR_SIZE 0x1000 |
41 | 70 |
42 /* | 71 /* |
43 ** For a build without mutexes, no-op the mutex calls. | 72 ** For a build without mutexes, no-op the mutex calls. |
44 */ | 73 */ |
45 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE==0 | 74 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE==0 |
46 #define sqlite3_mutex_alloc(X) ((sqlite3_mutex*)8) | 75 #define sqlite3_mutex_alloc(X) ((sqlite3_mutex*)8) |
47 #define sqlite3_mutex_free(X) | 76 #define sqlite3_mutex_free(X) |
48 #define sqlite3_mutex_enter(X) | 77 #define sqlite3_mutex_enter(X) |
49 #define sqlite3_mutex_try(X) SQLITE_OK | 78 #define sqlite3_mutex_try(X) SQLITE_OK |
50 #define sqlite3_mutex_leave(X) | 79 #define sqlite3_mutex_leave(X) |
51 #define sqlite3_mutex_held(X) ((void)(X),1) | 80 #define sqlite3_mutex_held(X) ((void)(X),1) |
52 #define sqlite3_mutex_notheld(X) ((void)(X),1) | 81 #define sqlite3_mutex_notheld(X) ((void)(X),1) |
53 #endif /* SQLITE_THREADSAFE==0 */ | 82 #endif /* SQLITE_THREADSAFE==0 */ |
54 | 83 |
| 84 /* Maximum chunk number */ |
| 85 #define MX_CHUNK_NUMBER 299 |
| 86 |
| 87 /* First chunk for rollback journal files */ |
| 88 #define SQLITE_MULTIPLEX_JOURNAL_8_3_OFFSET 400 |
| 89 #define SQLITE_MULTIPLEX_WAL_8_3_OFFSET 700 |
| 90 |
55 | 91 |
56 /************************ Shim Definitions ******************************/ | 92 /************************ Shim Definitions ******************************/ |
57 | 93 |
58 #define SQLITE_MULTIPLEX_VFS_NAME "multiplex" | 94 #ifndef SQLITE_MULTIPLEX_VFS_NAME |
| 95 # define SQLITE_MULTIPLEX_VFS_NAME "multiplex" |
| 96 #endif |
59 | 97 |
60 /* This is the limit on the chunk size. It may be changed by calling | 98 /* This is the limit on the chunk size. It may be changed by calling |
61 ** the xFileControl() interface. It will be rounded up to a | 99 ** the xFileControl() interface. It will be rounded up to a |
62 ** multiple of MAX_PAGE_SIZE. We default it here to 1GB. | 100 ** multiple of MAX_PAGE_SIZE. We default it here to 2GiB less 64KiB. |
63 */ | 101 */ |
64 #define SQLITE_MULTIPLEX_CHUNK_SIZE (MAX_PAGE_SIZE*16384) | 102 #ifndef SQLITE_MULTIPLEX_CHUNK_SIZE |
| 103 # define SQLITE_MULTIPLEX_CHUNK_SIZE 2147418112 |
| 104 #endif |
65 | 105 |
66 /* Default limit on number of chunks. Care should be taken | 106 /* This used to be the default limit on number of chunks, but |
67 ** so that values for chunks numbers fit in the SQLITE_MULTIPLEX_EXT_FMT | 107 ** it is no longer enforced. There is currently no limit to the |
68 ** format specifier. It may be changed by calling | 108 ** number of chunks. |
69 ** the xFileControl() interface. | 109 ** |
| 110 ** May be changed by calling the xFileControl() interface. |
70 */ | 111 */ |
71 #define SQLITE_MULTIPLEX_MAX_CHUNKS 32 | 112 #ifndef SQLITE_MULTIPLEX_MAX_CHUNKS |
72 | 113 # define SQLITE_MULTIPLEX_MAX_CHUNKS 12 |
73 /* If SQLITE_MULTIPLEX_EXT_OVWR is defined, the | 114 #endif |
74 ** last SQLITE_MULTIPLEX_EXT_SZ characters of the | |
75 ** filename will be overwritten, otherwise, the | |
76 ** multiplex extension is simply appended to the filename. | |
77 ** Ex. (undefined) test.db -> test.db01 | |
78 ** (defined) test.db -> test.01 | |
79 ** Chunk 0 does not have a modified extension. | |
80 */ | |
81 #define SQLITE_MULTIPLEX_EXT_FMT "%02d" | |
82 #define SQLITE_MULTIPLEX_EXT_SZ 2 | |
83 | 115 |
84 /************************ Object Definitions ******************************/ | 116 /************************ Object Definitions ******************************/ |
85 | 117 |
86 /* Forward declaration of all object types */ | 118 /* Forward declaration of all object types */ |
87 typedef struct multiplexGroup multiplexGroup; | 119 typedef struct multiplexGroup multiplexGroup; |
88 typedef struct multiplexConn multiplexConn; | 120 typedef struct multiplexConn multiplexConn; |
89 | 121 |
90 /* | 122 /* |
91 ** A "multiplex group" is a collection of files that collectively | 123 ** A "multiplex group" is a collection of files that collectively |
92 ** makeup a single SQLite DB file. This allows the size of the DB | 124 ** makeup a single SQLite DB file. This allows the size of the DB |
93 ** to exceed the limits imposed by the file system. | 125 ** to exceed the limits imposed by the file system. |
94 ** | 126 ** |
95 ** There is an instance of the following object for each defined multiplex | 127 ** There is an instance of the following object for each defined multiplex |
96 ** group. | 128 ** group. |
97 */ | 129 */ |
98 struct multiplexGroup { | 130 struct multiplexGroup { |
99 sqlite3_file **pReal; /* Handles to each chunk */ | 131 struct multiplexReal { /* For each chunk */ |
100 char *bOpen; /* array of bools - 0 if chunk not opened */ | 132 sqlite3_file *p; /* Handle for the chunk */ |
| 133 char *z; /* Name of this chunk */ |
| 134 } *aReal; /* list of all chunks */ |
| 135 int nReal; /* Number of chunks */ |
101 char *zName; /* Base filename of this group */ | 136 char *zName; /* Base filename of this group */ |
102 int nName; /* Length of base filename */ | 137 int nName; /* Length of base filename */ |
103 int flags; /* Flags used for original opening */ | 138 int flags; /* Flags used for original opening */ |
104 int nChunkSize; /* Chunk size used for this group */ | 139 unsigned int szChunk; /* Chunk size used for this group */ |
105 int nMaxChunks; /* Max number of chunks for this group */ | 140 unsigned char bEnabled; /* TRUE to use Multiplex VFS for this file */ |
106 int bEnabled; /* TRUE to use Multiplex VFS for this file */ | 141 unsigned char bTruncate; /* TRUE to enable truncation of databases */ |
107 multiplexGroup *pNext, *pPrev; /* Doubly linked list of all group objects */ | 142 multiplexGroup *pNext, *pPrev; /* Doubly linked list of all group objects */ |
108 }; | 143 }; |
109 | 144 |
110 /* | 145 /* |
111 ** An instance of the following object represents each open connection | 146 ** An instance of the following object represents each open connection |
112 ** to a file that is multiplex'ed. This object is a | 147 ** to a file that is multiplex'ed. This object is a |
113 ** subclass of sqlite3_file. The sqlite3_file object for the underlying | 148 ** subclass of sqlite3_file. The sqlite3_file object for the underlying |
114 ** VFS is appended to this structure. | 149 ** VFS is appended to this structure. |
115 */ | 150 */ |
116 struct multiplexConn { | 151 struct multiplexConn { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 int isInitialized; | 189 int isInitialized; |
155 | 190 |
156 /* For run-time access any of the other global data structures in this | 191 /* For run-time access any of the other global data structures in this |
157 ** shim, the following mutex must be held. | 192 ** shim, the following mutex must be held. |
158 */ | 193 */ |
159 sqlite3_mutex *pMutex; | 194 sqlite3_mutex *pMutex; |
160 | 195 |
161 /* List of multiplexGroup objects. | 196 /* List of multiplexGroup objects. |
162 */ | 197 */ |
163 multiplexGroup *pGroups; | 198 multiplexGroup *pGroups; |
164 | |
165 /* Storage for temp file names. Allocated during | |
166 ** initialization to the max pathname of the underlying VFS. | |
167 */ | |
168 char *zName; | |
169 | |
170 } gMultiplex; | 199 } gMultiplex; |
171 | 200 |
172 /************************* Utility Routines *********************************/ | 201 /************************* Utility Routines *********************************/ |
173 /* | 202 /* |
174 ** Acquire and release the mutex used to serialize access to the | 203 ** Acquire and release the mutex used to serialize access to the |
175 ** list of multiplexGroups. | 204 ** list of multiplexGroups. |
176 */ | 205 */ |
177 static void multiplexEnter(void){ sqlite3_mutex_enter(gMultiplex.pMutex); } | 206 static void multiplexEnter(void){ sqlite3_mutex_enter(gMultiplex.pMutex); } |
178 static void multiplexLeave(void){ sqlite3_mutex_leave(gMultiplex.pMutex); } | 207 static void multiplexLeave(void){ sqlite3_mutex_leave(gMultiplex.pMutex); } |
179 | 208 |
180 /* | 209 /* |
181 ** Compute a string length that is limited to what can be stored in | 210 ** Compute a string length that is limited to what can be stored in |
182 ** lower 30 bits of a 32-bit signed integer. | 211 ** lower 30 bits of a 32-bit signed integer. |
183 ** | 212 ** |
184 ** The value returned will never be negative. Nor will it ever be greater | 213 ** The value returned will never be negative. Nor will it ever be greater |
185 ** than the actual length of the string. For very long strings (greater | 214 ** than the actual length of the string. For very long strings (greater |
186 ** than 1GiB) the value returned might be less than the true string length. | 215 ** than 1GiB) the value returned might be less than the true string length. |
187 */ | 216 */ |
188 int multiplexStrlen30(const char *z){ | 217 static int multiplexStrlen30(const char *z){ |
189 const char *z2 = z; | 218 const char *z2 = z; |
190 if( z==0 ) return 0; | 219 if( z==0 ) return 0; |
191 while( *z2 ){ z2++; } | 220 while( *z2 ){ z2++; } |
192 return 0x3fffffff & (int)(z2 - z); | 221 return 0x3fffffff & (int)(z2 - z); |
193 } | 222 } |
194 | 223 |
| 224 /* |
| 225 ** Generate the file-name for chunk iChunk of the group with base name |
| 226 ** zBase. The file-name is written to buffer zOut before returning. Buffer |
| 227 ** zOut must be allocated by the caller so that it is at least (nBase+5) |
| 228 ** bytes in size, where nBase is the length of zBase, not including the |
| 229 ** nul-terminator. |
| 230 ** |
| 231 ** If iChunk is 0 (or 400 - the number for the first journal file chunk), |
| 232 ** the output is a copy of the input string. Otherwise, if |
| 233 ** SQLITE_ENABLE_8_3_NAMES is not defined or the input buffer does not contain |
| 234 ** a "." character, then the output is a copy of the input string with the |
| 235 ** three-digit zero-padded decimal representation if iChunk appended to it. |
| 236 ** For example: |
| 237 ** |
| 238 ** zBase="test.db", iChunk=4 -> zOut="test.db004" |
| 239 ** |
| 240 ** Or, if SQLITE_ENABLE_8_3_NAMES is defined and the input buffer contains |
| 241 ** a "." character, then everything after the "." is replaced by the |
| 242 ** three-digit representation of iChunk. |
| 243 ** |
| 244 ** zBase="test.db", iChunk=4 -> zOut="test.004" |
| 245 ** |
| 246 ** The output buffer string is terminated by 2 0x00 bytes. This makes it safe |
| 247 ** to pass to sqlite3_uri_parameter() and similar. |
| 248 */ |
| 249 static void multiplexFilename( |
| 250 const char *zBase, /* Filename for chunk 0 */ |
| 251 int nBase, /* Size of zBase in bytes (without \0) */ |
| 252 int flags, /* Flags used to open file */ |
| 253 int iChunk, /* Chunk to generate filename for */ |
| 254 char *zOut /* Buffer to write generated name to */ |
| 255 ){ |
| 256 int n = nBase; |
| 257 memcpy(zOut, zBase, n+1); |
| 258 if( iChunk!=0 && iChunk<=MX_CHUNK_NUMBER ){ |
| 259 #ifdef SQLITE_ENABLE_8_3_NAMES |
| 260 int i; |
| 261 for(i=n-1; i>0 && i>=n-4 && zOut[i]!='.'; i--){} |
| 262 if( i>=n-4 ) n = i+1; |
| 263 if( flags & SQLITE_OPEN_MAIN_JOURNAL ){ |
| 264 /* The extensions on overflow files for main databases are 001, 002, |
| 265 ** 003 and so forth. To avoid name collisions, add 400 to the |
| 266 ** extensions of journal files so that they are 401, 402, 403, .... |
| 267 */ |
| 268 iChunk += SQLITE_MULTIPLEX_JOURNAL_8_3_OFFSET; |
| 269 }else if( flags & SQLITE_OPEN_WAL ){ |
| 270 /* To avoid name collisions, add 700 to the |
| 271 ** extensions of WAL files so that they are 701, 702, 703, .... |
| 272 */ |
| 273 iChunk += SQLITE_MULTIPLEX_WAL_8_3_OFFSET; |
| 274 } |
| 275 #endif |
| 276 sqlite3_snprintf(4,&zOut[n],"%03d",iChunk); |
| 277 n += 3; |
| 278 } |
| 279 |
| 280 assert( zOut[n]=='\0' ); |
| 281 zOut[n+1] = '\0'; |
| 282 } |
| 283 |
| 284 /* Compute the filename for the iChunk-th chunk |
| 285 */ |
| 286 static int multiplexSubFilename(multiplexGroup *pGroup, int iChunk){ |
| 287 if( iChunk>=pGroup->nReal ){ |
| 288 struct multiplexReal *p; |
| 289 p = sqlite3_realloc(pGroup->aReal, (iChunk+1)*sizeof(*p)); |
| 290 if( p==0 ){ |
| 291 return SQLITE_NOMEM; |
| 292 } |
| 293 memset(&p[pGroup->nReal], 0, sizeof(p[0])*(iChunk+1-pGroup->nReal)); |
| 294 pGroup->aReal = p; |
| 295 pGroup->nReal = iChunk+1; |
| 296 } |
| 297 if( pGroup->zName && pGroup->aReal[iChunk].z==0 ){ |
| 298 char *z; |
| 299 int n = pGroup->nName; |
| 300 pGroup->aReal[iChunk].z = z = sqlite3_malloc( n+5 ); |
| 301 if( z==0 ){ |
| 302 return SQLITE_NOMEM; |
| 303 } |
| 304 multiplexFilename(pGroup->zName, pGroup->nName, pGroup->flags, iChunk, z); |
| 305 } |
| 306 return SQLITE_OK; |
| 307 } |
| 308 |
195 /* Translate an sqlite3_file* that is really a multiplexGroup* into | 309 /* Translate an sqlite3_file* that is really a multiplexGroup* into |
196 ** the sqlite3_file* for the underlying original VFS. | 310 ** the sqlite3_file* for the underlying original VFS. |
| 311 ** |
| 312 ** For chunk 0, the pGroup->flags determines whether or not a new file |
| 313 ** is created if it does not already exist. For chunks 1 and higher, the |
| 314 ** file is created only if createFlag is 1. |
197 */ | 315 */ |
198 static sqlite3_file *multiplexSubOpen(multiplexConn *pConn, int iChunk, int *rc,
int *pOutFlags){ | 316 static sqlite3_file *multiplexSubOpen( |
199 multiplexGroup *pGroup = pConn->pGroup; | 317 multiplexGroup *pGroup, /* The multiplexor group */ |
| 318 int iChunk, /* Which chunk to open. 0==original file */ |
| 319 int *rc, /* Result code in and out */ |
| 320 int *pOutFlags, /* Output flags */ |
| 321 int createFlag /* True to create if iChunk>0 */ |
| 322 ){ |
| 323 sqlite3_file *pSubOpen = 0; |
200 sqlite3_vfs *pOrigVfs = gMultiplex.pOrigVfs; /* Real VFS */ | 324 sqlite3_vfs *pOrigVfs = gMultiplex.pOrigVfs; /* Real VFS */ |
201 if( iChunk<pGroup->nMaxChunks ){ | 325 |
202 sqlite3_file *pSubOpen = pGroup->pReal[iChunk]; /* Real file descriptor *
/ | 326 #ifdef SQLITE_ENABLE_8_3_NAMES |
203 if( !pGroup->bOpen[iChunk] ){ | 327 /* If JOURNAL_8_3_OFFSET is set to (say) 400, then any overflow files are |
204 memcpy(gMultiplex.zName, pGroup->zName, pGroup->nName+1); | 328 ** part of a database journal are named db.401, db.402, and so on. A |
205 if( iChunk ){ | 329 ** database may therefore not grow to larger than 400 chunks. Attempting |
206 #ifdef SQLITE_MULTIPLEX_EXT_OVWR | 330 ** to open chunk 401 indicates the database is full. */ |
207 sqlite3_snprintf(SQLITE_MULTIPLEX_EXT_SZ+1, gMultiplex.zName+pGroup->nNa
me-SQLITE_MULTIPLEX_EXT_SZ, SQLITE_MULTIPLEX_EXT_FMT, iChunk); | 331 if( iChunk>=SQLITE_MULTIPLEX_JOURNAL_8_3_OFFSET ){ |
208 #else | 332 sqlite3_log(SQLITE_FULL, "multiplexed chunk overflow: %s", pGroup->zName); |
209 sqlite3_snprintf(SQLITE_MULTIPLEX_EXT_SZ+1, gMultiplex.zName+pGroup->nNa
me, SQLITE_MULTIPLEX_EXT_FMT, iChunk); | 333 *rc = SQLITE_FULL; |
| 334 return 0; |
| 335 } |
210 #endif | 336 #endif |
| 337 |
| 338 *rc = multiplexSubFilename(pGroup, iChunk); |
| 339 if( (*rc)==SQLITE_OK && (pSubOpen = pGroup->aReal[iChunk].p)==0 ){ |
| 340 int flags, bExists; |
| 341 flags = pGroup->flags; |
| 342 if( createFlag ){ |
| 343 flags |= SQLITE_OPEN_CREATE; |
| 344 }else if( iChunk==0 ){ |
| 345 /* Fall through */ |
| 346 }else if( pGroup->aReal[iChunk].z==0 ){ |
| 347 return 0; |
| 348 }else{ |
| 349 *rc = pOrigVfs->xAccess(pOrigVfs, pGroup->aReal[iChunk].z, |
| 350 SQLITE_ACCESS_EXISTS, &bExists); |
| 351 if( *rc || !bExists ){ |
| 352 if( *rc ){ |
| 353 sqlite3_log(*rc, "multiplexor.xAccess failure on %s", |
| 354 pGroup->aReal[iChunk].z); |
| 355 } |
| 356 return 0; |
211 } | 357 } |
212 *rc = pOrigVfs->xOpen(pOrigVfs, gMultiplex.zName, pSubOpen, pGroup->flags,
pOutFlags); | 358 flags &= ~SQLITE_OPEN_CREATE; |
213 if( *rc==SQLITE_OK ){ | |
214 pGroup->bOpen[iChunk] = -1; | |
215 return pSubOpen; | |
216 } | |
217 return NULL; | |
218 } | 359 } |
219 *rc = SQLITE_OK; | 360 pSubOpen = sqlite3_malloc( pOrigVfs->szOsFile ); |
220 return pSubOpen; | 361 if( pSubOpen==0 ){ |
| 362 *rc = SQLITE_IOERR_NOMEM; |
| 363 return 0; |
| 364 } |
| 365 pGroup->aReal[iChunk].p = pSubOpen; |
| 366 *rc = pOrigVfs->xOpen(pOrigVfs, pGroup->aReal[iChunk].z, pSubOpen, |
| 367 flags, pOutFlags); |
| 368 if( (*rc)!=SQLITE_OK ){ |
| 369 sqlite3_log(*rc, "multiplexor.xOpen failure on %s", |
| 370 pGroup->aReal[iChunk].z); |
| 371 sqlite3_free(pSubOpen); |
| 372 pGroup->aReal[iChunk].p = 0; |
| 373 return 0; |
| 374 } |
221 } | 375 } |
222 *rc = SQLITE_FULL; | 376 return pSubOpen; |
223 return NULL; | |
224 } | 377 } |
225 | 378 |
226 /* | 379 /* |
| 380 ** Return the size, in bytes, of chunk number iChunk. If that chunk |
| 381 ** does not exist, then return 0. This function does not distingish between |
| 382 ** non-existant files and zero-length files. |
| 383 */ |
| 384 static sqlite3_int64 multiplexSubSize( |
| 385 multiplexGroup *pGroup, /* The multiplexor group */ |
| 386 int iChunk, /* Which chunk to open. 0==original file */ |
| 387 int *rc /* Result code in and out */ |
| 388 ){ |
| 389 sqlite3_file *pSub; |
| 390 sqlite3_int64 sz = 0; |
| 391 |
| 392 if( *rc ) return 0; |
| 393 pSub = multiplexSubOpen(pGroup, iChunk, rc, NULL, 0); |
| 394 if( pSub==0 ) return 0; |
| 395 *rc = pSub->pMethods->xFileSize(pSub, &sz); |
| 396 return sz; |
| 397 } |
| 398 |
| 399 /* |
227 ** This is the implementation of the multiplex_control() SQL function. | 400 ** This is the implementation of the multiplex_control() SQL function. |
228 */ | 401 */ |
229 static void multiplexControlFunc( | 402 static void multiplexControlFunc( |
230 sqlite3_context *context, | 403 sqlite3_context *context, |
231 int argc, | 404 int argc, |
232 sqlite3_value **argv | 405 sqlite3_value **argv |
233 ){ | 406 ){ |
234 int rc = SQLITE_OK; | 407 int rc = SQLITE_OK; |
235 sqlite3 *db = sqlite3_context_db_handle(context); | 408 sqlite3 *db = sqlite3_context_db_handle(context); |
236 int op; | 409 int op; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 sqlite3 *db, | 445 sqlite3 *db, |
273 char **pzErrMsg, | 446 char **pzErrMsg, |
274 const sqlite3_api_routines *pApi | 447 const sqlite3_api_routines *pApi |
275 ){ | 448 ){ |
276 int rc; | 449 int rc; |
277 rc = sqlite3_create_function(db, "multiplex_control", 2, SQLITE_ANY, | 450 rc = sqlite3_create_function(db, "multiplex_control", 2, SQLITE_ANY, |
278 0, multiplexControlFunc, 0, 0); | 451 0, multiplexControlFunc, 0, 0); |
279 return rc; | 452 return rc; |
280 } | 453 } |
281 | 454 |
| 455 /* |
| 456 ** Close a single sub-file in the connection group. |
| 457 */ |
| 458 static void multiplexSubClose( |
| 459 multiplexGroup *pGroup, |
| 460 int iChunk, |
| 461 sqlite3_vfs *pOrigVfs |
| 462 ){ |
| 463 sqlite3_file *pSubOpen = pGroup->aReal[iChunk].p; |
| 464 if( pSubOpen ){ |
| 465 pSubOpen->pMethods->xClose(pSubOpen); |
| 466 if( pOrigVfs && pGroup->aReal[iChunk].z ){ |
| 467 pOrigVfs->xDelete(pOrigVfs, pGroup->aReal[iChunk].z, 0); |
| 468 } |
| 469 sqlite3_free(pGroup->aReal[iChunk].p); |
| 470 } |
| 471 sqlite3_free(pGroup->aReal[iChunk].z); |
| 472 memset(&pGroup->aReal[iChunk], 0, sizeof(pGroup->aReal[iChunk])); |
| 473 } |
| 474 |
| 475 /* |
| 476 ** Deallocate memory held by a multiplexGroup |
| 477 */ |
| 478 static void multiplexFreeComponents(multiplexGroup *pGroup){ |
| 479 int i; |
| 480 for(i=0; i<pGroup->nReal; i++){ multiplexSubClose(pGroup, i, 0); } |
| 481 sqlite3_free(pGroup->aReal); |
| 482 pGroup->aReal = 0; |
| 483 pGroup->nReal = 0; |
| 484 } |
| 485 |
| 486 |
282 /************************* VFS Method Wrappers *****************************/ | 487 /************************* VFS Method Wrappers *****************************/ |
283 | 488 |
284 /* | 489 /* |
285 ** This is the xOpen method used for the "multiplex" VFS. | 490 ** This is the xOpen method used for the "multiplex" VFS. |
286 ** | 491 ** |
287 ** Most of the work is done by the underlying original VFS. This method | 492 ** Most of the work is done by the underlying original VFS. This method |
288 ** simply links the new file into the appropriate multiplex group if it is a | 493 ** simply links the new file into the appropriate multiplex group if it is a |
289 ** file that needs to be tracked. | 494 ** file that needs to be tracked. |
290 */ | 495 */ |
291 static int multiplexOpen( | 496 static int multiplexOpen( |
292 sqlite3_vfs *pVfs, /* The multiplex VFS */ | 497 sqlite3_vfs *pVfs, /* The multiplex VFS */ |
293 const char *zName, /* Name of file to be opened */ | 498 const char *zName, /* Name of file to be opened */ |
294 sqlite3_file *pConn, /* Fill in this file descriptor */ | 499 sqlite3_file *pConn, /* Fill in this file descriptor */ |
295 int flags, /* Flags to control the opening */ | 500 int flags, /* Flags to control the opening */ |
296 int *pOutFlags /* Flags showing results of opening */ | 501 int *pOutFlags /* Flags showing results of opening */ |
297 ){ | 502 ){ |
298 int rc; /* Result code */ | 503 int rc = SQLITE_OK; /* Result code */ |
299 multiplexConn *pMultiplexOpen; /* The new multiplex file descr
iptor */ | 504 multiplexConn *pMultiplexOpen; /* The new multiplex file descriptor */ |
300 multiplexGroup *pGroup; /* Corresponding multiplexGroup
object */ | 505 multiplexGroup *pGroup = 0; /* Corresponding multiplexGroup object */ |
301 sqlite3_file *pSubOpen; /* Real file descriptor */ | 506 sqlite3_file *pSubOpen = 0; /* Real file descriptor */ |
302 sqlite3_vfs *pOrigVfs = gMultiplex.pOrigVfs; /* Real VFS */ | 507 sqlite3_vfs *pOrigVfs = gMultiplex.pOrigVfs; /* Real VFS */ |
303 int nName = multiplexStrlen30(zName); | 508 int nName = 0; |
304 int i; | 509 int sz = 0; |
305 int sz; | 510 char *zToFree = 0; |
306 | 511 |
307 UNUSED_PARAMETER(pVfs); | 512 UNUSED_PARAMETER(pVfs); |
| 513 memset(pConn, 0, pVfs->szOsFile); |
| 514 assert( zName || (flags & SQLITE_OPEN_DELETEONCLOSE) ); |
308 | 515 |
309 /* We need to create a group structure and manage | 516 /* We need to create a group structure and manage |
310 ** access to this group of files. | 517 ** access to this group of files. |
311 */ | 518 */ |
312 multiplexEnter(); | 519 multiplexEnter(); |
313 pMultiplexOpen = (multiplexConn*)pConn; | 520 pMultiplexOpen = (multiplexConn*)pConn; |
314 /* allocate space for group */ | 521 |
315 sz = sizeof(multiplexGroup) /* multiplexGroup *
/ | 522 if( rc==SQLITE_OK ){ |
316 + (sizeof(sqlite3_file *)*SQLITE_MULTIPLEX_MAX_CHUNKS) /* pReal[] */ | 523 /* allocate space for group */ |
317 + (pOrigVfs->szOsFile*SQLITE_MULTIPLEX_MAX_CHUNKS) /* *pReal */ | 524 nName = zName ? multiplexStrlen30(zName) : 0; |
318 + SQLITE_MULTIPLEX_MAX_CHUNKS /* bOpen[] */ | 525 sz = sizeof(multiplexGroup) /* multiplexGroup */ |
319 + nName + 1; /* zName */ | 526 + nName + 1; /* zName */ |
320 #ifndef SQLITE_MULTIPLEX_EXT_OVWR | 527 pGroup = sqlite3_malloc( sz ); |
321 sz += SQLITE_MULTIPLEX_EXT_SZ; | 528 if( pGroup==0 ){ |
322 assert(nName+SQLITE_MULTIPLEX_EXT_SZ < pOrigVfs->mxPathname); | 529 rc = SQLITE_NOMEM; |
| 530 } |
| 531 } |
| 532 |
| 533 if( rc==SQLITE_OK ){ |
| 534 const char *zUri = (flags & SQLITE_OPEN_URI) ? zName : 0; |
| 535 /* assign pointers to extra space allocated */ |
| 536 memset(pGroup, 0, sz); |
| 537 pMultiplexOpen->pGroup = pGroup; |
| 538 pGroup->bEnabled = -1; |
| 539 pGroup->bTruncate = sqlite3_uri_boolean(zUri, "truncate", |
| 540 (flags & SQLITE_OPEN_MAIN_DB)==0); |
| 541 pGroup->szChunk = (int)sqlite3_uri_int64(zUri, "chunksize", |
| 542 SQLITE_MULTIPLEX_CHUNK_SIZE); |
| 543 pGroup->szChunk = (pGroup->szChunk+0xffff)&~0xffff; |
| 544 if( zName ){ |
| 545 char *p = (char *)&pGroup[1]; |
| 546 pGroup->zName = p; |
| 547 memcpy(pGroup->zName, zName, nName+1); |
| 548 pGroup->nName = nName; |
| 549 } |
| 550 if( pGroup->bEnabled ){ |
| 551 /* Make sure that the chunksize is such that the pending byte does not |
| 552 ** falls at the end of a chunk. A region of up to 64K following |
| 553 ** the pending byte is never written, so if the pending byte occurs |
| 554 ** near the end of a chunk, that chunk will be too small. */ |
| 555 #ifndef SQLITE_OMIT_WSD |
| 556 extern int sqlite3PendingByte; |
323 #else | 557 #else |
324 assert(nName >= SQLITE_MULTIPLEX_EXT_SZ); | 558 int sqlite3PendingByte = 0x40000000; |
325 assert(nName < pOrigVfs->mxPathname); | |
326 #endif | 559 #endif |
327 pGroup = sqlite3_malloc( sz ); | 560 while( (sqlite3PendingByte % pGroup->szChunk)>=(pGroup->szChunk-65536) ){ |
328 if( pGroup==0 ){ | 561 pGroup->szChunk += 65536; |
329 rc=SQLITE_NOMEM; | 562 } |
330 }else{ | |
331 /* assign pointers to extra space allocated */ | |
332 char *p = (char *)&pGroup[1]; | |
333 pMultiplexOpen->pGroup = pGroup; | |
334 memset(pGroup, 0, sz); | |
335 pGroup->bEnabled = -1; | |
336 pGroup->nChunkSize = SQLITE_MULTIPLEX_CHUNK_SIZE; | |
337 pGroup->nMaxChunks = SQLITE_MULTIPLEX_MAX_CHUNKS; | |
338 pGroup->pReal = (sqlite3_file **)p; | |
339 p += (sizeof(sqlite3_file *)*pGroup->nMaxChunks); | |
340 for(i=0; i<pGroup->nMaxChunks; i++){ | |
341 pGroup->pReal[i] = (sqlite3_file *)p; | |
342 p += pOrigVfs->szOsFile; | |
343 } | 563 } |
344 /* bOpen[] vals should all be zero from memset above */ | |
345 pGroup->bOpen = p; | |
346 p += pGroup->nMaxChunks; | |
347 pGroup->zName = p; | |
348 /* save off base filename, name length, and original open flags */ | |
349 memcpy(pGroup->zName, zName, nName+1); | |
350 pGroup->nName = nName; | |
351 pGroup->flags = flags; | 564 pGroup->flags = flags; |
352 pSubOpen = multiplexSubOpen(pMultiplexOpen, 0, &rc, pOutFlags); | 565 rc = multiplexSubFilename(pGroup, 1); |
353 if( pSubOpen ){ | 566 if( rc==SQLITE_OK ){ |
354 /* if this file is already larger than chunk size, disable | 567 pSubOpen = multiplexSubOpen(pGroup, 0, &rc, pOutFlags, 0); |
355 ** the multiplex feature. | 568 if( pSubOpen==0 && rc==SQLITE_OK ) rc = SQLITE_CANTOPEN; |
356 */ | 569 } |
| 570 if( rc==SQLITE_OK ){ |
357 sqlite3_int64 sz; | 571 sqlite3_int64 sz; |
358 int rc2 = pSubOpen->pMethods->xFileSize(pSubOpen, &sz); | 572 |
359 if( (rc2==SQLITE_OK) && (sz>pGroup->nChunkSize) ){ | 573 rc = pSubOpen->pMethods->xFileSize(pSubOpen, &sz); |
360 pGroup->bEnabled = 0; | 574 if( rc==SQLITE_OK && zName ){ |
| 575 int bExists; |
| 576 if( sz==0 ){ |
| 577 if( flags & SQLITE_OPEN_MAIN_JOURNAL ){ |
| 578 /* If opening a main journal file and the first chunk is zero |
| 579 ** bytes in size, delete any subsequent chunks from the |
| 580 ** file-system. */ |
| 581 int iChunk = 1; |
| 582 do { |
| 583 rc = pOrigVfs->xAccess(pOrigVfs, |
| 584 pGroup->aReal[iChunk].z, SQLITE_ACCESS_EXISTS, &bExists |
| 585 ); |
| 586 if( rc==SQLITE_OK && bExists ){ |
| 587 rc = pOrigVfs->xDelete(pOrigVfs, pGroup->aReal[iChunk].z, 0); |
| 588 if( rc==SQLITE_OK ){ |
| 589 rc = multiplexSubFilename(pGroup, ++iChunk); |
| 590 } |
| 591 } |
| 592 }while( rc==SQLITE_OK && bExists ); |
| 593 } |
| 594 }else{ |
| 595 /* If the first overflow file exists and if the size of the main file |
| 596 ** is different from the chunk size, that means the chunk size is set |
| 597 ** set incorrectly. So fix it. |
| 598 ** |
| 599 ** Or, if the first overflow file does not exist and the main file is |
| 600 ** larger than the chunk size, that means the chunk size is too small. |
| 601 ** But we have no way of determining the intended chunk size, so |
| 602 ** just disable the multiplexor all togethre. |
| 603 */ |
| 604 rc = pOrigVfs->xAccess(pOrigVfs, pGroup->aReal[1].z, |
| 605 SQLITE_ACCESS_EXISTS, &bExists); |
| 606 bExists = multiplexSubSize(pGroup, 1, &rc)>0; |
| 607 if( rc==SQLITE_OK && bExists && sz==(sz&0xffff0000) && sz>0 |
| 608 && sz!=pGroup->szChunk ){ |
| 609 pGroup->szChunk = (int)sz; |
| 610 }else if( rc==SQLITE_OK && !bExists && sz>pGroup->szChunk ){ |
| 611 pGroup->bEnabled = 0; |
| 612 } |
| 613 } |
361 } | 614 } |
| 615 } |
| 616 |
| 617 if( rc==SQLITE_OK ){ |
362 if( pSubOpen->pMethods->iVersion==1 ){ | 618 if( pSubOpen->pMethods->iVersion==1 ){ |
363 pMultiplexOpen->base.pMethods = &gMultiplex.sIoMethodsV1; | 619 pMultiplexOpen->base.pMethods = &gMultiplex.sIoMethodsV1; |
364 }else{ | 620 }else{ |
365 pMultiplexOpen->base.pMethods = &gMultiplex.sIoMethodsV2; | 621 pMultiplexOpen->base.pMethods = &gMultiplex.sIoMethodsV2; |
366 } | 622 } |
367 /* place this group at the head of our list */ | 623 /* place this group at the head of our list */ |
368 pGroup->pNext = gMultiplex.pGroups; | 624 pGroup->pNext = gMultiplex.pGroups; |
369 if( gMultiplex.pGroups ) gMultiplex.pGroups->pPrev = pGroup; | 625 if( gMultiplex.pGroups ) gMultiplex.pGroups->pPrev = pGroup; |
370 gMultiplex.pGroups = pGroup; | 626 gMultiplex.pGroups = pGroup; |
371 }else{ | 627 }else{ |
| 628 multiplexFreeComponents(pGroup); |
372 sqlite3_free(pGroup); | 629 sqlite3_free(pGroup); |
373 } | 630 } |
374 } | 631 } |
375 multiplexLeave(); | 632 multiplexLeave(); |
| 633 sqlite3_free(zToFree); |
376 return rc; | 634 return rc; |
377 } | 635 } |
378 | 636 |
379 /* | 637 /* |
380 ** This is the xDelete method used for the "multiplex" VFS. | 638 ** This is the xDelete method used for the "multiplex" VFS. |
381 ** It attempts to delete the filename specified, as well | 639 ** It attempts to delete the filename specified. |
382 ** as additional files with the SQLITE_MULTIPLEX_EXT_FMT extension. | |
383 */ | 640 */ |
384 static int multiplexDelete( | 641 static int multiplexDelete( |
385 sqlite3_vfs *pVfs, /* The multiplex VFS */ | 642 sqlite3_vfs *pVfs, /* The multiplex VFS */ |
386 const char *zName, /* Name of file to delete */ | 643 const char *zName, /* Name of file to delete */ |
387 int syncDir | 644 int syncDir |
388 ){ | 645 ){ |
| 646 int rc; |
389 sqlite3_vfs *pOrigVfs = gMultiplex.pOrigVfs; /* Real VFS */ | 647 sqlite3_vfs *pOrigVfs = gMultiplex.pOrigVfs; /* Real VFS */ |
390 int rc = SQLITE_OK; | 648 rc = pOrigVfs->xDelete(pOrigVfs, zName, syncDir); |
391 int nName = multiplexStrlen30(zName); | 649 if( rc==SQLITE_OK ){ |
392 int i; | 650 /* If the main chunk was deleted successfully, also delete any subsequent |
393 | 651 ** chunks - starting with the last (highest numbered). |
394 UNUSED_PARAMETER(pVfs); | 652 */ |
395 | 653 int nName = (int)strlen(zName); |
396 multiplexEnter(); | 654 char *z; |
397 memcpy(gMultiplex.zName, zName, nName+1); | 655 z = sqlite3_malloc(nName + 5); |
398 for(i=0; i<SQLITE_MULTIPLEX_MAX_CHUNKS; i++){ | 656 if( z==0 ){ |
399 int rc2; | 657 rc = SQLITE_IOERR_NOMEM; |
400 int exists = 0; | 658 }else{ |
401 if( i ){ | 659 int iChunk = 0; |
402 #ifdef SQLITE_MULTIPLEX_EXT_OVWR | 660 int bExists; |
403 sqlite3_snprintf(SQLITE_MULTIPLEX_EXT_SZ+1, | 661 do{ |
404 gMultiplex.zName+nName-SQLITE_MULTIPLEX_EXT_SZ, | 662 multiplexFilename(zName, nName, SQLITE_OPEN_MAIN_JOURNAL, ++iChunk, z); |
405 SQLITE_MULTIPLEX_EXT_FMT, i); | 663 rc = pOrigVfs->xAccess(pOrigVfs, z, SQLITE_ACCESS_EXISTS, &bExists); |
406 #else | 664 }while( rc==SQLITE_OK && bExists ); |
407 sqlite3_snprintf(SQLITE_MULTIPLEX_EXT_SZ+1, | 665 while( rc==SQLITE_OK && iChunk>1 ){ |
408 gMultiplex.zName+nName, | 666 multiplexFilename(zName, nName, SQLITE_OPEN_MAIN_JOURNAL, --iChunk, z); |
409 SQLITE_MULTIPLEX_EXT_FMT, i); | 667 rc = pOrigVfs->xDelete(pOrigVfs, z, syncDir); |
410 #endif | 668 } |
| 669 if( rc==SQLITE_OK ){ |
| 670 iChunk = 0; |
| 671 do{ |
| 672 multiplexFilename(zName, nName, SQLITE_OPEN_WAL, ++iChunk, z); |
| 673 rc = pOrigVfs->xAccess(pOrigVfs, z, SQLITE_ACCESS_EXISTS, &bExists); |
| 674 }while( rc==SQLITE_OK && bExists ); |
| 675 while( rc==SQLITE_OK && iChunk>1 ){ |
| 676 multiplexFilename(zName, nName, SQLITE_OPEN_WAL, --iChunk, z); |
| 677 rc = pOrigVfs->xDelete(pOrigVfs, z, syncDir); |
| 678 } |
| 679 } |
411 } | 680 } |
412 rc2 = pOrigVfs->xAccess(pOrigVfs, gMultiplex.zName, | 681 sqlite3_free(z); |
413 SQLITE_ACCESS_EXISTS, &exists); | |
414 if( rc2==SQLITE_OK && exists){ | |
415 /* if it exists, delete it */ | |
416 rc2 = pOrigVfs->xDelete(pOrigVfs, gMultiplex.zName, syncDir); | |
417 if( rc2!=SQLITE_OK ) rc = rc2; | |
418 }else{ | |
419 /* stop at first "gap" */ | |
420 break; | |
421 } | |
422 } | 682 } |
423 multiplexLeave(); | |
424 return rc; | 683 return rc; |
425 } | 684 } |
426 | 685 |
427 static int multiplexAccess(sqlite3_vfs *a, const char *b, int c, int *d){ | 686 static int multiplexAccess(sqlite3_vfs *a, const char *b, int c, int *d){ |
428 return gMultiplex.pOrigVfs->xAccess(gMultiplex.pOrigVfs, b, c, d); | 687 return gMultiplex.pOrigVfs->xAccess(gMultiplex.pOrigVfs, b, c, d); |
429 } | 688 } |
430 static int multiplexFullPathname(sqlite3_vfs *a, const char *b, int c, char *d){ | 689 static int multiplexFullPathname(sqlite3_vfs *a, const char *b, int c, char *d){ |
431 return gMultiplex.pOrigVfs->xFullPathname(gMultiplex.pOrigVfs, b, c, d); | 690 return gMultiplex.pOrigVfs->xFullPathname(gMultiplex.pOrigVfs, b, c, d); |
432 } | 691 } |
433 static void *multiplexDlOpen(sqlite3_vfs *a, const char *b){ | 692 static void *multiplexDlOpen(sqlite3_vfs *a, const char *b){ |
(...skipping 28 matching lines...) Expand all Loading... |
462 | 721 |
463 /* xClose requests get passed through to the original VFS. | 722 /* xClose requests get passed through to the original VFS. |
464 ** We loop over all open chunk handles and close them. | 723 ** We loop over all open chunk handles and close them. |
465 ** The group structure for this file is unlinked from | 724 ** The group structure for this file is unlinked from |
466 ** our list of groups and freed. | 725 ** our list of groups and freed. |
467 */ | 726 */ |
468 static int multiplexClose(sqlite3_file *pConn){ | 727 static int multiplexClose(sqlite3_file *pConn){ |
469 multiplexConn *p = (multiplexConn*)pConn; | 728 multiplexConn *p = (multiplexConn*)pConn; |
470 multiplexGroup *pGroup = p->pGroup; | 729 multiplexGroup *pGroup = p->pGroup; |
471 int rc = SQLITE_OK; | 730 int rc = SQLITE_OK; |
472 int i; | |
473 multiplexEnter(); | 731 multiplexEnter(); |
474 /* close any open handles */ | 732 multiplexFreeComponents(pGroup); |
475 for(i=0; i<pGroup->nMaxChunks; i++){ | |
476 if( pGroup->bOpen[i] ){ | |
477 sqlite3_file *pSubOpen = pGroup->pReal[i]; | |
478 int rc2 = pSubOpen->pMethods->xClose(pSubOpen); | |
479 if( rc2!=SQLITE_OK ) rc = rc2; | |
480 pGroup->bOpen[i] = 0; | |
481 } | |
482 } | |
483 /* remove from linked list */ | 733 /* remove from linked list */ |
484 if( pGroup->pNext ) pGroup->pNext->pPrev = pGroup->pPrev; | 734 if( pGroup->pNext ) pGroup->pNext->pPrev = pGroup->pPrev; |
485 if( pGroup->pPrev ){ | 735 if( pGroup->pPrev ){ |
486 pGroup->pPrev->pNext = pGroup->pNext; | 736 pGroup->pPrev->pNext = pGroup->pNext; |
487 }else{ | 737 }else{ |
488 gMultiplex.pGroups = pGroup->pNext; | 738 gMultiplex.pGroups = pGroup->pNext; |
489 } | 739 } |
490 sqlite3_free(pGroup); | 740 sqlite3_free(pGroup); |
491 multiplexLeave(); | 741 multiplexLeave(); |
492 return rc; | 742 return rc; |
493 } | 743 } |
494 | 744 |
495 /* Pass xRead requests thru to the original VFS after | 745 /* Pass xRead requests thru to the original VFS after |
496 ** determining the correct chunk to operate on. | 746 ** determining the correct chunk to operate on. |
497 ** Break up reads across chunk boundaries. | 747 ** Break up reads across chunk boundaries. |
498 */ | 748 */ |
499 static int multiplexRead( | 749 static int multiplexRead( |
500 sqlite3_file *pConn, | 750 sqlite3_file *pConn, |
501 void *pBuf, | 751 void *pBuf, |
502 int iAmt, | 752 int iAmt, |
503 sqlite3_int64 iOfst | 753 sqlite3_int64 iOfst |
504 ){ | 754 ){ |
505 multiplexConn *p = (multiplexConn*)pConn; | 755 multiplexConn *p = (multiplexConn*)pConn; |
506 multiplexGroup *pGroup = p->pGroup; | 756 multiplexGroup *pGroup = p->pGroup; |
507 int rc = SQLITE_OK; | 757 int rc = SQLITE_OK; |
508 multiplexEnter(); | 758 int nMutex = 0; |
| 759 multiplexEnter(); nMutex++; |
509 if( !pGroup->bEnabled ){ | 760 if( !pGroup->bEnabled ){ |
510 sqlite3_file *pSubOpen = multiplexSubOpen(p, 0, &rc, NULL); | 761 sqlite3_file *pSubOpen = multiplexSubOpen(pGroup, 0, &rc, NULL, 0); |
511 rc = ( !pSubOpen ) ? SQLITE_IOERR_READ : pSubOpen->pMethods->xRead(pSubOpen,
pBuf, iAmt, iOfst); | 762 multiplexLeave(); nMutex--; |
| 763 if( pSubOpen==0 ){ |
| 764 rc = SQLITE_IOERR_READ; |
| 765 }else{ |
| 766 rc = pSubOpen->pMethods->xRead(pSubOpen, pBuf, iAmt, iOfst); |
| 767 } |
512 }else{ | 768 }else{ |
513 while( iAmt > 0 ){ | 769 while( iAmt > 0 ){ |
514 int i = (int)(iOfst / pGroup->nChunkSize); | 770 int i = (int)(iOfst / pGroup->szChunk); |
515 sqlite3_file *pSubOpen = multiplexSubOpen(p, i, &rc, NULL); | 771 sqlite3_file *pSubOpen; |
| 772 if( nMutex==0 ){ multiplexEnter(); nMutex++; } |
| 773 pSubOpen = multiplexSubOpen(pGroup, i, &rc, NULL, 1); |
| 774 multiplexLeave(); nMutex--; |
516 if( pSubOpen ){ | 775 if( pSubOpen ){ |
517 int extra = ((int)(iOfst % pGroup->nChunkSize) + iAmt) - pGroup->nChunkS
ize; | 776 int extra = ((int)(iOfst % pGroup->szChunk) + iAmt) - pGroup->szChunk; |
518 if( extra<0 ) extra = 0; | 777 if( extra<0 ) extra = 0; |
519 iAmt -= extra; | 778 iAmt -= extra; |
520 rc = pSubOpen->pMethods->xRead(pSubOpen, pBuf, iAmt, iOfst % pGroup->nCh
unkSize); | 779 rc = pSubOpen->pMethods->xRead(pSubOpen, pBuf, iAmt, |
| 780 iOfst % pGroup->szChunk); |
521 if( rc!=SQLITE_OK ) break; | 781 if( rc!=SQLITE_OK ) break; |
522 pBuf = (char *)pBuf + iAmt; | 782 pBuf = (char *)pBuf + iAmt; |
523 iOfst += iAmt; | 783 iOfst += iAmt; |
524 iAmt = extra; | 784 iAmt = extra; |
525 }else{ | 785 }else{ |
526 rc = SQLITE_IOERR_READ; | 786 rc = SQLITE_IOERR_READ; |
527 break; | 787 break; |
528 } | 788 } |
529 } | 789 } |
530 } | 790 } |
531 multiplexLeave(); | 791 assert( nMutex==0 || nMutex==1 ); |
| 792 if( nMutex ) multiplexLeave(); |
532 return rc; | 793 return rc; |
533 } | 794 } |
534 | 795 |
535 /* Pass xWrite requests thru to the original VFS after | 796 /* Pass xWrite requests thru to the original VFS after |
536 ** determining the correct chunk to operate on. | 797 ** determining the correct chunk to operate on. |
537 ** Break up writes across chunk boundaries. | 798 ** Break up writes across chunk boundaries. |
538 */ | 799 */ |
539 static int multiplexWrite( | 800 static int multiplexWrite( |
540 sqlite3_file *pConn, | 801 sqlite3_file *pConn, |
541 const void *pBuf, | 802 const void *pBuf, |
542 int iAmt, | 803 int iAmt, |
543 sqlite3_int64 iOfst | 804 sqlite3_int64 iOfst |
544 ){ | 805 ){ |
545 multiplexConn *p = (multiplexConn*)pConn; | 806 multiplexConn *p = (multiplexConn*)pConn; |
546 multiplexGroup *pGroup = p->pGroup; | 807 multiplexGroup *pGroup = p->pGroup; |
547 int rc = SQLITE_OK; | 808 int rc = SQLITE_OK; |
548 multiplexEnter(); | 809 multiplexEnter(); |
549 if( !pGroup->bEnabled ){ | 810 if( !pGroup->bEnabled ){ |
550 sqlite3_file *pSubOpen = multiplexSubOpen(p, 0, &rc, NULL); | 811 sqlite3_file *pSubOpen = multiplexSubOpen(pGroup, 0, &rc, NULL, 0); |
551 rc = ( !pSubOpen ) ? SQLITE_IOERR_WRITE : pSubOpen->pMethods->xWrite(pSubOpe
n, pBuf, iAmt, iOfst); | 812 if( pSubOpen==0 ){ |
| 813 rc = SQLITE_IOERR_WRITE; |
| 814 }else{ |
| 815 rc = pSubOpen->pMethods->xWrite(pSubOpen, pBuf, iAmt, iOfst); |
| 816 } |
552 }else{ | 817 }else{ |
553 while( iAmt > 0 ){ | 818 while( rc==SQLITE_OK && iAmt>0 ){ |
554 int i = (int)(iOfst / pGroup->nChunkSize); | 819 int i = (int)(iOfst / pGroup->szChunk); |
555 sqlite3_file *pSubOpen = multiplexSubOpen(p, i, &rc, NULL); | 820 sqlite3_file *pSubOpen = multiplexSubOpen(pGroup, i, &rc, NULL, 1); |
556 if( pSubOpen ){ | 821 if( pSubOpen ){ |
557 int extra = ((int)(iOfst % pGroup->nChunkSize) + iAmt) - pGroup->nChunkS
ize; | 822 int extra = ((int)(iOfst % pGroup->szChunk) + iAmt) - |
| 823 pGroup->szChunk; |
558 if( extra<0 ) extra = 0; | 824 if( extra<0 ) extra = 0; |
559 iAmt -= extra; | 825 iAmt -= extra; |
560 rc = pSubOpen->pMethods->xWrite(pSubOpen, pBuf, iAmt, iOfst % pGroup->nC
hunkSize); | 826 rc = pSubOpen->pMethods->xWrite(pSubOpen, pBuf, iAmt, |
561 if( rc!=SQLITE_OK ) break; | 827 iOfst % pGroup->szChunk); |
562 pBuf = (char *)pBuf + iAmt; | 828 pBuf = (char *)pBuf + iAmt; |
563 iOfst += iAmt; | 829 iOfst += iAmt; |
564 iAmt = extra; | 830 iAmt = extra; |
565 }else{ | |
566 rc = SQLITE_IOERR_WRITE; | |
567 break; | |
568 } | 831 } |
569 } | 832 } |
570 } | 833 } |
571 multiplexLeave(); | 834 multiplexLeave(); |
572 return rc; | 835 return rc; |
573 } | 836 } |
574 | 837 |
575 /* Pass xTruncate requests thru to the original VFS after | 838 /* Pass xTruncate requests thru to the original VFS after |
576 ** determining the correct chunk to operate on. Delete any | 839 ** determining the correct chunk to operate on. Delete any |
577 ** chunks above the truncate mark. | 840 ** chunks above the truncate mark. |
578 */ | 841 */ |
579 static int multiplexTruncate(sqlite3_file *pConn, sqlite3_int64 size){ | 842 static int multiplexTruncate(sqlite3_file *pConn, sqlite3_int64 size){ |
580 multiplexConn *p = (multiplexConn*)pConn; | 843 multiplexConn *p = (multiplexConn*)pConn; |
581 multiplexGroup *pGroup = p->pGroup; | 844 multiplexGroup *pGroup = p->pGroup; |
582 int rc = SQLITE_OK; | 845 int rc = SQLITE_OK; |
583 multiplexEnter(); | 846 multiplexEnter(); |
584 if( !pGroup->bEnabled ){ | 847 if( !pGroup->bEnabled ){ |
585 sqlite3_file *pSubOpen = multiplexSubOpen(p, 0, &rc, NULL); | 848 sqlite3_file *pSubOpen = multiplexSubOpen(pGroup, 0, &rc, NULL, 0); |
586 rc = ( !pSubOpen ) ? SQLITE_IOERR_TRUNCATE : pSubOpen->pMethods->xTruncate(p
SubOpen, size); | 849 if( pSubOpen==0 ){ |
| 850 rc = SQLITE_IOERR_TRUNCATE; |
| 851 }else{ |
| 852 rc = pSubOpen->pMethods->xTruncate(pSubOpen, size); |
| 853 } |
587 }else{ | 854 }else{ |
588 int rc2; | |
589 int i; | 855 int i; |
| 856 int iBaseGroup = (int)(size / pGroup->szChunk); |
590 sqlite3_file *pSubOpen; | 857 sqlite3_file *pSubOpen; |
591 sqlite3_vfs *pOrigVfs = gMultiplex.pOrigVfs; /* Real VFS */ | 858 sqlite3_vfs *pOrigVfs = gMultiplex.pOrigVfs; /* Real VFS */ |
592 memcpy(gMultiplex.zName, pGroup->zName, pGroup->nName+1); | |
593 /* delete the chunks above the truncate limit */ | 859 /* delete the chunks above the truncate limit */ |
594 for(i=(int)(size / pGroup->nChunkSize)+1; i<pGroup->nMaxChunks; i++){ | 860 for(i = pGroup->nReal-1; i>iBaseGroup && rc==SQLITE_OK; i--){ |
595 /* close any open chunks before deleting them */ | 861 if( pGroup->bTruncate ){ |
596 if( pGroup->bOpen[i] ){ | 862 multiplexSubClose(pGroup, i, pOrigVfs); |
597 pSubOpen = pGroup->pReal[i]; | 863 }else{ |
598 rc2 = pSubOpen->pMethods->xClose(pSubOpen); | 864 pSubOpen = multiplexSubOpen(pGroup, i, &rc, 0, 0); |
599 if( rc2!=SQLITE_OK ) rc = SQLITE_IOERR_TRUNCATE; | 865 if( pSubOpen ){ |
600 pGroup->bOpen[i] = 0; | 866 rc = pSubOpen->pMethods->xTruncate(pSubOpen, 0); |
| 867 } |
601 } | 868 } |
602 #ifdef SQLITE_MULTIPLEX_EXT_OVWR | |
603 sqlite3_snprintf(SQLITE_MULTIPLEX_EXT_SZ+1, | |
604 gMultiplex.zName+pGroup->nName-SQLITE_MULTIPLEX_EXT_SZ, | |
605 SQLITE_MULTIPLEX_EXT_FMT, i); | |
606 #else | |
607 sqlite3_snprintf(SQLITE_MULTIPLEX_EXT_SZ+1, | |
608 gMultiplex.zName+pGroup->nName, | |
609 SQLITE_MULTIPLEX_EXT_FMT, i); | |
610 #endif | |
611 rc2 = pOrigVfs->xDelete(pOrigVfs, gMultiplex.zName, 0); | |
612 if( rc2!=SQLITE_OK ) rc = SQLITE_IOERR_TRUNCATE; | |
613 } | 869 } |
614 pSubOpen = multiplexSubOpen(p, (int)(size / pGroup->nChunkSize), &rc2, NULL)
; | 870 if( rc==SQLITE_OK ){ |
615 if( pSubOpen ){ | 871 pSubOpen = multiplexSubOpen(pGroup, iBaseGroup, &rc, 0, 0); |
616 rc2 = pSubOpen->pMethods->xTruncate(pSubOpen, size % pGroup->nChunkSize); | 872 if( pSubOpen ){ |
617 if( rc2!=SQLITE_OK ) rc = rc2; | 873 rc = pSubOpen->pMethods->xTruncate(pSubOpen, size % pGroup->szChunk); |
618 }else{ | 874 } |
619 rc = SQLITE_IOERR_TRUNCATE; | |
620 } | 875 } |
| 876 if( rc ) rc = SQLITE_IOERR_TRUNCATE; |
621 } | 877 } |
622 multiplexLeave(); | 878 multiplexLeave(); |
623 return rc; | 879 return rc; |
624 } | 880 } |
625 | 881 |
626 /* Pass xSync requests through to the original VFS without change | 882 /* Pass xSync requests through to the original VFS without change |
627 */ | 883 */ |
628 static int multiplexSync(sqlite3_file *pConn, int flags){ | 884 static int multiplexSync(sqlite3_file *pConn, int flags){ |
629 multiplexConn *p = (multiplexConn*)pConn; | 885 multiplexConn *p = (multiplexConn*)pConn; |
630 multiplexGroup *pGroup = p->pGroup; | 886 multiplexGroup *pGroup = p->pGroup; |
631 int rc = SQLITE_OK; | 887 int rc = SQLITE_OK; |
632 int i; | 888 int i; |
633 multiplexEnter(); | 889 multiplexEnter(); |
634 for(i=0; i<pGroup->nMaxChunks; i++){ | 890 for(i=0; i<pGroup->nReal; i++){ |
635 /* if we don't have it open, we don't need to sync it */ | 891 sqlite3_file *pSubOpen = pGroup->aReal[i].p; |
636 if( pGroup->bOpen[i] ){ | 892 if( pSubOpen ){ |
637 sqlite3_file *pSubOpen = pGroup->pReal[i]; | |
638 int rc2 = pSubOpen->pMethods->xSync(pSubOpen, flags); | 893 int rc2 = pSubOpen->pMethods->xSync(pSubOpen, flags); |
639 if( rc2!=SQLITE_OK ) rc = rc2; | 894 if( rc2!=SQLITE_OK ) rc = rc2; |
640 } | 895 } |
641 } | 896 } |
642 multiplexLeave(); | 897 multiplexLeave(); |
643 return rc; | 898 return rc; |
644 } | 899 } |
645 | 900 |
646 /* Pass xFileSize requests through to the original VFS. | 901 /* Pass xFileSize requests through to the original VFS. |
647 ** Aggregate the size of all the chunks before returning. | 902 ** Aggregate the size of all the chunks before returning. |
648 */ | 903 */ |
649 static int multiplexFileSize(sqlite3_file *pConn, sqlite3_int64 *pSize){ | 904 static int multiplexFileSize(sqlite3_file *pConn, sqlite3_int64 *pSize){ |
650 multiplexConn *p = (multiplexConn*)pConn; | 905 multiplexConn *p = (multiplexConn*)pConn; |
651 multiplexGroup *pGroup = p->pGroup; | 906 multiplexGroup *pGroup = p->pGroup; |
652 int rc = SQLITE_OK; | 907 int rc = SQLITE_OK; |
653 int rc2; | |
654 int i; | 908 int i; |
655 multiplexEnter(); | 909 multiplexEnter(); |
656 if( !pGroup->bEnabled ){ | 910 if( !pGroup->bEnabled ){ |
657 sqlite3_file *pSubOpen = multiplexSubOpen(p, 0, &rc, NULL); | 911 sqlite3_file *pSubOpen = multiplexSubOpen(pGroup, 0, &rc, NULL, 0); |
658 rc = ( !pSubOpen ) ? SQLITE_IOERR_FSTAT : pSubOpen->pMethods->xFileSize(pSub
Open, pSize); | 912 if( pSubOpen==0 ){ |
| 913 rc = SQLITE_IOERR_FSTAT; |
| 914 }else{ |
| 915 rc = pSubOpen->pMethods->xFileSize(pSubOpen, pSize); |
| 916 } |
659 }else{ | 917 }else{ |
660 *pSize = 0; | 918 *pSize = 0; |
661 for(i=0; i<pGroup->nMaxChunks; i++){ | 919 for(i=0; rc==SQLITE_OK; i++){ |
662 sqlite3_file *pSubOpen = NULL; | 920 sqlite3_int64 sz = multiplexSubSize(pGroup, i, &rc); |
663 /* if not opened already, check to see if the chunk exists */ | 921 if( sz==0 ) break; |
664 if( pGroup->bOpen[i] ){ | 922 *pSize = i*(sqlite3_int64)pGroup->szChunk + sz; |
665 pSubOpen = pGroup->pReal[i]; | |
666 }else{ | |
667 sqlite3_vfs *pOrigVfs = gMultiplex.pOrigVfs; /* Real VFS */ | |
668 int exists = 0; | |
669 memcpy(gMultiplex.zName, pGroup->zName, pGroup->nName+1); | |
670 if( i ){ | |
671 #ifdef SQLITE_MULTIPLEX_EXT_OVWR | |
672 sqlite3_snprintf(SQLITE_MULTIPLEX_EXT_SZ+1, | |
673 gMultiplex.zName+pGroup->nName-SQLITE_MULTIPLEX_EXT_SZ, | |
674 SQLITE_MULTIPLEX_EXT_FMT, i); | |
675 #else | |
676 sqlite3_snprintf(SQLITE_MULTIPLEX_EXT_SZ+1, | |
677 gMultiplex.zName+pGroup->nName, | |
678 SQLITE_MULTIPLEX_EXT_FMT, i); | |
679 #endif | |
680 } | |
681 rc2 = pOrigVfs->xAccess(pOrigVfs, gMultiplex.zName, | |
682 SQLITE_ACCESS_EXISTS, &exists); | |
683 if( rc2==SQLITE_OK && exists){ | |
684 /* if it exists, open it */ | |
685 pSubOpen = multiplexSubOpen(p, i, &rc, NULL); | |
686 }else{ | |
687 /* stop at first "gap" */ | |
688 break; | |
689 } | |
690 } | |
691 if( pSubOpen ){ | |
692 sqlite3_int64 sz; | |
693 rc2 = pSubOpen->pMethods->xFileSize(pSubOpen, &sz); | |
694 if( rc2!=SQLITE_OK ){ | |
695 rc = rc2; | |
696 }else{ | |
697 if( sz>pGroup->nChunkSize ){ | |
698 rc = SQLITE_IOERR_FSTAT; | |
699 } | |
700 *pSize += sz; | |
701 } | |
702 }else{ | |
703 break; | |
704 } | |
705 } | 923 } |
706 } | 924 } |
707 multiplexLeave(); | 925 multiplexLeave(); |
708 return rc; | 926 return rc; |
709 } | 927 } |
710 | 928 |
711 /* Pass xLock requests through to the original VFS unchanged. | 929 /* Pass xLock requests through to the original VFS unchanged. |
712 */ | 930 */ |
713 static int multiplexLock(sqlite3_file *pConn, int lock){ | 931 static int multiplexLock(sqlite3_file *pConn, int lock){ |
714 multiplexConn *p = (multiplexConn*)pConn; | 932 multiplexConn *p = (multiplexConn*)pConn; |
715 int rc; | 933 int rc; |
716 sqlite3_file *pSubOpen = multiplexSubOpen(p, 0, &rc, NULL); | 934 sqlite3_file *pSubOpen = multiplexSubOpen(p->pGroup, 0, &rc, NULL, 0); |
717 if( pSubOpen ){ | 935 if( pSubOpen ){ |
718 return pSubOpen->pMethods->xLock(pSubOpen, lock); | 936 return pSubOpen->pMethods->xLock(pSubOpen, lock); |
719 } | 937 } |
720 return SQLITE_BUSY; | 938 return SQLITE_BUSY; |
721 } | 939 } |
722 | 940 |
723 /* Pass xUnlock requests through to the original VFS unchanged. | 941 /* Pass xUnlock requests through to the original VFS unchanged. |
724 */ | 942 */ |
725 static int multiplexUnlock(sqlite3_file *pConn, int lock){ | 943 static int multiplexUnlock(sqlite3_file *pConn, int lock){ |
726 multiplexConn *p = (multiplexConn*)pConn; | 944 multiplexConn *p = (multiplexConn*)pConn; |
727 int rc; | 945 int rc; |
728 sqlite3_file *pSubOpen = multiplexSubOpen(p, 0, &rc, NULL); | 946 sqlite3_file *pSubOpen = multiplexSubOpen(p->pGroup, 0, &rc, NULL, 0); |
729 if( pSubOpen ){ | 947 if( pSubOpen ){ |
730 return pSubOpen->pMethods->xUnlock(pSubOpen, lock); | 948 return pSubOpen->pMethods->xUnlock(pSubOpen, lock); |
731 } | 949 } |
732 return SQLITE_IOERR_UNLOCK; | 950 return SQLITE_IOERR_UNLOCK; |
733 } | 951 } |
734 | 952 |
735 /* Pass xCheckReservedLock requests through to the original VFS unchanged. | 953 /* Pass xCheckReservedLock requests through to the original VFS unchanged. |
736 */ | 954 */ |
737 static int multiplexCheckReservedLock(sqlite3_file *pConn, int *pResOut){ | 955 static int multiplexCheckReservedLock(sqlite3_file *pConn, int *pResOut){ |
738 multiplexConn *p = (multiplexConn*)pConn; | 956 multiplexConn *p = (multiplexConn*)pConn; |
739 int rc; | 957 int rc; |
740 sqlite3_file *pSubOpen = multiplexSubOpen(p, 0, &rc, NULL); | 958 sqlite3_file *pSubOpen = multiplexSubOpen(p->pGroup, 0, &rc, NULL, 0); |
741 if( pSubOpen ){ | 959 if( pSubOpen ){ |
742 return pSubOpen->pMethods->xCheckReservedLock(pSubOpen, pResOut); | 960 return pSubOpen->pMethods->xCheckReservedLock(pSubOpen, pResOut); |
743 } | 961 } |
744 return SQLITE_IOERR_CHECKRESERVEDLOCK; | 962 return SQLITE_IOERR_CHECKRESERVEDLOCK; |
745 } | 963 } |
746 | 964 |
747 /* Pass xFileControl requests through to the original VFS unchanged, | 965 /* Pass xFileControl requests through to the original VFS unchanged, |
748 ** except for any MULTIPLEX_CTRL_* requests here. | 966 ** except for any MULTIPLEX_CTRL_* requests here. |
749 */ | 967 */ |
750 static int multiplexFileControl(sqlite3_file *pConn, int op, void *pArg){ | 968 static int multiplexFileControl(sqlite3_file *pConn, int op, void *pArg){ |
751 multiplexConn *p = (multiplexConn*)pConn; | 969 multiplexConn *p = (multiplexConn*)pConn; |
752 multiplexGroup *pGroup = p->pGroup; | 970 multiplexGroup *pGroup = p->pGroup; |
753 int rc = SQLITE_ERROR; | 971 int rc = SQLITE_ERROR; |
754 sqlite3_file *pSubOpen; | 972 sqlite3_file *pSubOpen; |
755 | 973 |
756 if( !gMultiplex.isInitialized ) return SQLITE_MISUSE; | 974 if( !gMultiplex.isInitialized ) return SQLITE_MISUSE; |
757 switch( op ){ | 975 switch( op ){ |
758 case MULTIPLEX_CTRL_ENABLE: | 976 case MULTIPLEX_CTRL_ENABLE: |
759 if( pArg ) { | 977 if( pArg ) { |
760 int bEnabled = *(int *)pArg; | 978 int bEnabled = *(int *)pArg; |
761 pGroup->bEnabled = bEnabled; | 979 pGroup->bEnabled = bEnabled; |
762 rc = SQLITE_OK; | 980 rc = SQLITE_OK; |
763 } | 981 } |
764 break; | 982 break; |
765 case MULTIPLEX_CTRL_SET_CHUNK_SIZE: | 983 case MULTIPLEX_CTRL_SET_CHUNK_SIZE: |
766 if( pArg ) { | 984 if( pArg ) { |
767 int nChunkSize = *(int *)pArg; | 985 unsigned int szChunk = *(unsigned*)pArg; |
768 if( nChunkSize<1 ){ | 986 if( szChunk<1 ){ |
769 rc = SQLITE_MISUSE; | 987 rc = SQLITE_MISUSE; |
770 }else{ | 988 }else{ |
771 /* Round up to nearest multiple of MAX_PAGE_SIZE. */ | 989 /* Round up to nearest multiple of MAX_PAGE_SIZE. */ |
772 nChunkSize = (nChunkSize + (MAX_PAGE_SIZE-1)); | 990 szChunk = (szChunk + (MAX_PAGE_SIZE-1)); |
773 nChunkSize &= ~(MAX_PAGE_SIZE-1); | 991 szChunk &= ~(MAX_PAGE_SIZE-1); |
774 pGroup->nChunkSize = nChunkSize; | 992 pGroup->szChunk = szChunk; |
775 rc = SQLITE_OK; | 993 rc = SQLITE_OK; |
776 } | 994 } |
777 } | 995 } |
778 break; | 996 break; |
779 case MULTIPLEX_CTRL_SET_MAX_CHUNKS: | 997 case MULTIPLEX_CTRL_SET_MAX_CHUNKS: |
780 if( pArg ) { | 998 rc = SQLITE_OK; |
781 int nMaxChunks = *(int *)pArg; | |
782 if(( nMaxChunks<1 ) || ( nMaxChunks>SQLITE_MULTIPLEX_MAX_CHUNKS )){ | |
783 rc = SQLITE_MISUSE; | |
784 }else{ | |
785 pGroup->nMaxChunks = nMaxChunks; | |
786 rc = SQLITE_OK; | |
787 } | |
788 } | |
789 break; | 999 break; |
790 case SQLITE_FCNTL_SIZE_HINT: | 1000 case SQLITE_FCNTL_SIZE_HINT: |
791 case SQLITE_FCNTL_CHUNK_SIZE: | 1001 case SQLITE_FCNTL_CHUNK_SIZE: |
792 /* no-op these */ | 1002 /* no-op these */ |
793 rc = SQLITE_OK; | 1003 rc = SQLITE_OK; |
794 break; | 1004 break; |
| 1005 case SQLITE_FCNTL_PRAGMA: { |
| 1006 char **aFcntl = (char**)pArg; |
| 1007 if( aFcntl[1] && sqlite3_stricmp(aFcntl[1],"multiplex_truncate")==0 ){ |
| 1008 if( aFcntl[2] && aFcntl[2][0] ){ |
| 1009 if( sqlite3_stricmp(aFcntl[2], "on")==0 |
| 1010 || sqlite3_stricmp(aFcntl[2], "1")==0 ){ |
| 1011 pGroup->bTruncate = 1; |
| 1012 }else |
| 1013 if( sqlite3_stricmp(aFcntl[2], "off")==0 |
| 1014 || sqlite3_stricmp(aFcntl[2], "0")==0 ){ |
| 1015 pGroup->bTruncate = 0; |
| 1016 } |
| 1017 } |
| 1018 aFcntl[0] = sqlite3_mprintf(pGroup->bTruncate ? "on" : "off"); |
| 1019 rc = SQLITE_OK; |
| 1020 break; |
| 1021 } |
| 1022 /* If the multiplexor does not handle the pragma, pass it through |
| 1023 ** into the default case. */ |
| 1024 } |
795 default: | 1025 default: |
796 pSubOpen = multiplexSubOpen(p, 0, &rc, NULL); | 1026 pSubOpen = multiplexSubOpen(pGroup, 0, &rc, NULL, 0); |
797 if( pSubOpen ){ | 1027 if( pSubOpen ){ |
798 rc = pSubOpen->pMethods->xFileControl(pSubOpen, op, pArg); | 1028 rc = pSubOpen->pMethods->xFileControl(pSubOpen, op, pArg); |
| 1029 if( op==SQLITE_FCNTL_VFSNAME && rc==SQLITE_OK ){ |
| 1030 *(char**)pArg = sqlite3_mprintf("multiplex/%z", *(char**)pArg); |
| 1031 } |
799 } | 1032 } |
800 break; | 1033 break; |
801 } | 1034 } |
802 return rc; | 1035 return rc; |
803 } | 1036 } |
804 | 1037 |
805 /* Pass xSectorSize requests through to the original VFS unchanged. | 1038 /* Pass xSectorSize requests through to the original VFS unchanged. |
806 */ | 1039 */ |
807 static int multiplexSectorSize(sqlite3_file *pConn){ | 1040 static int multiplexSectorSize(sqlite3_file *pConn){ |
808 multiplexConn *p = (multiplexConn*)pConn; | 1041 multiplexConn *p = (multiplexConn*)pConn; |
809 int rc; | 1042 int rc; |
810 sqlite3_file *pSubOpen = multiplexSubOpen(p, 0, &rc, NULL); | 1043 sqlite3_file *pSubOpen = multiplexSubOpen(p->pGroup, 0, &rc, NULL, 0); |
811 if( pSubOpen ){ | 1044 if( pSubOpen && pSubOpen->pMethods->xSectorSize ){ |
812 return pSubOpen->pMethods->xSectorSize(pSubOpen); | 1045 return pSubOpen->pMethods->xSectorSize(pSubOpen); |
813 } | 1046 } |
814 return DEFAULT_SECTOR_SIZE; | 1047 return DEFAULT_SECTOR_SIZE; |
815 } | 1048 } |
816 | 1049 |
817 /* Pass xDeviceCharacteristics requests through to the original VFS unchanged. | 1050 /* Pass xDeviceCharacteristics requests through to the original VFS unchanged. |
818 */ | 1051 */ |
819 static int multiplexDeviceCharacteristics(sqlite3_file *pConn){ | 1052 static int multiplexDeviceCharacteristics(sqlite3_file *pConn){ |
820 multiplexConn *p = (multiplexConn*)pConn; | 1053 multiplexConn *p = (multiplexConn*)pConn; |
821 int rc; | 1054 int rc; |
822 sqlite3_file *pSubOpen = multiplexSubOpen(p, 0, &rc, NULL); | 1055 sqlite3_file *pSubOpen = multiplexSubOpen(p->pGroup, 0, &rc, NULL, 0); |
823 if( pSubOpen ){ | 1056 if( pSubOpen ){ |
824 return pSubOpen->pMethods->xDeviceCharacteristics(pSubOpen); | 1057 return pSubOpen->pMethods->xDeviceCharacteristics(pSubOpen); |
825 } | 1058 } |
826 return 0; | 1059 return 0; |
827 } | 1060 } |
828 | 1061 |
829 /* Pass xShmMap requests through to the original VFS unchanged. | 1062 /* Pass xShmMap requests through to the original VFS unchanged. |
830 */ | 1063 */ |
831 static int multiplexShmMap( | 1064 static int multiplexShmMap( |
832 sqlite3_file *pConn, /* Handle open on database file */ | 1065 sqlite3_file *pConn, /* Handle open on database file */ |
833 int iRegion, /* Region to retrieve */ | 1066 int iRegion, /* Region to retrieve */ |
834 int szRegion, /* Size of regions */ | 1067 int szRegion, /* Size of regions */ |
835 int bExtend, /* True to extend file if necessary */ | 1068 int bExtend, /* True to extend file if necessary */ |
836 void volatile **pp /* OUT: Mapped memory */ | 1069 void volatile **pp /* OUT: Mapped memory */ |
837 ){ | 1070 ){ |
838 multiplexConn *p = (multiplexConn*)pConn; | 1071 multiplexConn *p = (multiplexConn*)pConn; |
839 int rc; | 1072 int rc; |
840 sqlite3_file *pSubOpen = multiplexSubOpen(p, 0, &rc, NULL); | 1073 sqlite3_file *pSubOpen = multiplexSubOpen(p->pGroup, 0, &rc, NULL, 0); |
841 if( pSubOpen ){ | 1074 if( pSubOpen ){ |
842 return pSubOpen->pMethods->xShmMap(pSubOpen, iRegion, szRegion, bExtend, pp)
; | 1075 return pSubOpen->pMethods->xShmMap(pSubOpen, iRegion, szRegion, bExtend,pp); |
843 } | 1076 } |
844 return SQLITE_IOERR; | 1077 return SQLITE_IOERR; |
845 } | 1078 } |
846 | 1079 |
847 /* Pass xShmLock requests through to the original VFS unchanged. | 1080 /* Pass xShmLock requests through to the original VFS unchanged. |
848 */ | 1081 */ |
849 static int multiplexShmLock( | 1082 static int multiplexShmLock( |
850 sqlite3_file *pConn, /* Database file holding the shared memory */ | 1083 sqlite3_file *pConn, /* Database file holding the shared memory */ |
851 int ofst, /* First lock to acquire or release */ | 1084 int ofst, /* First lock to acquire or release */ |
852 int n, /* Number of locks to acquire or release */ | 1085 int n, /* Number of locks to acquire or release */ |
853 int flags /* What to do with the lock */ | 1086 int flags /* What to do with the lock */ |
854 ){ | 1087 ){ |
855 multiplexConn *p = (multiplexConn*)pConn; | 1088 multiplexConn *p = (multiplexConn*)pConn; |
856 int rc; | 1089 int rc; |
857 sqlite3_file *pSubOpen = multiplexSubOpen(p, 0, &rc, NULL); | 1090 sqlite3_file *pSubOpen = multiplexSubOpen(p->pGroup, 0, &rc, NULL, 0); |
858 if( pSubOpen ){ | 1091 if( pSubOpen ){ |
859 return pSubOpen->pMethods->xShmLock(pSubOpen, ofst, n, flags); | 1092 return pSubOpen->pMethods->xShmLock(pSubOpen, ofst, n, flags); |
860 } | 1093 } |
861 return SQLITE_BUSY; | 1094 return SQLITE_BUSY; |
862 } | 1095 } |
863 | 1096 |
864 /* Pass xShmBarrier requests through to the original VFS unchanged. | 1097 /* Pass xShmBarrier requests through to the original VFS unchanged. |
865 */ | 1098 */ |
866 static void multiplexShmBarrier(sqlite3_file *pConn){ | 1099 static void multiplexShmBarrier(sqlite3_file *pConn){ |
867 multiplexConn *p = (multiplexConn*)pConn; | 1100 multiplexConn *p = (multiplexConn*)pConn; |
868 int rc; | 1101 int rc; |
869 sqlite3_file *pSubOpen = multiplexSubOpen(p, 0, &rc, NULL); | 1102 sqlite3_file *pSubOpen = multiplexSubOpen(p->pGroup, 0, &rc, NULL, 0); |
870 if( pSubOpen ){ | 1103 if( pSubOpen ){ |
871 pSubOpen->pMethods->xShmBarrier(pSubOpen); | 1104 pSubOpen->pMethods->xShmBarrier(pSubOpen); |
872 } | 1105 } |
873 } | 1106 } |
874 | 1107 |
875 /* Pass xShmUnmap requests through to the original VFS unchanged. | 1108 /* Pass xShmUnmap requests through to the original VFS unchanged. |
876 */ | 1109 */ |
877 static int multiplexShmUnmap(sqlite3_file *pConn, int deleteFlag){ | 1110 static int multiplexShmUnmap(sqlite3_file *pConn, int deleteFlag){ |
878 multiplexConn *p = (multiplexConn*)pConn; | 1111 multiplexConn *p = (multiplexConn*)pConn; |
879 int rc; | 1112 int rc; |
880 sqlite3_file *pSubOpen = multiplexSubOpen(p, 0, &rc, NULL); | 1113 sqlite3_file *pSubOpen = multiplexSubOpen(p->pGroup, 0, &rc, NULL, 0); |
881 if( pSubOpen ){ | 1114 if( pSubOpen ){ |
882 return pSubOpen->pMethods->xShmUnmap(pSubOpen, deleteFlag); | 1115 return pSubOpen->pMethods->xShmUnmap(pSubOpen, deleteFlag); |
883 } | 1116 } |
884 return SQLITE_OK; | 1117 return SQLITE_OK; |
885 } | 1118 } |
886 | 1119 |
887 /************************** Public Interfaces *****************************/ | 1120 /************************** Public Interfaces *****************************/ |
888 /* | 1121 /* |
889 ** CAPI: Initialize the multiplex VFS shim - sqlite3_multiplex_initialize() | 1122 ** CAPI: Initialize the multiplex VFS shim - sqlite3_multiplex_initialize() |
890 ** | 1123 ** |
891 ** Use the VFS named zOrigVfsName as the VFS that does the actual work. | 1124 ** Use the VFS named zOrigVfsName as the VFS that does the actual work. |
892 ** Use the default if zOrigVfsName==NULL. | 1125 ** Use the default if zOrigVfsName==NULL. |
893 ** | 1126 ** |
894 ** The multiplex VFS shim is named "multiplex". It will become the default | 1127 ** The multiplex VFS shim is named "multiplex". It will become the default |
895 ** VFS if makeDefault is non-zero. | 1128 ** VFS if makeDefault is non-zero. |
896 ** | 1129 ** |
897 ** THIS ROUTINE IS NOT THREADSAFE. Call this routine exactly once | 1130 ** THIS ROUTINE IS NOT THREADSAFE. Call this routine exactly once |
898 ** during start-up. | 1131 ** during start-up. |
899 */ | 1132 */ |
900 int sqlite3_multiplex_initialize(const char *zOrigVfsName, int makeDefault){ | 1133 int sqlite3_multiplex_initialize(const char *zOrigVfsName, int makeDefault){ |
901 sqlite3_vfs *pOrigVfs; | 1134 sqlite3_vfs *pOrigVfs; |
902 if( gMultiplex.isInitialized ) return SQLITE_MISUSE; | 1135 if( gMultiplex.isInitialized ) return SQLITE_MISUSE; |
903 pOrigVfs = sqlite3_vfs_find(zOrigVfsName); | 1136 pOrigVfs = sqlite3_vfs_find(zOrigVfsName); |
904 if( pOrigVfs==0 ) return SQLITE_ERROR; | 1137 if( pOrigVfs==0 ) return SQLITE_ERROR; |
905 assert( pOrigVfs!=&gMultiplex.sThisVfs ); | 1138 assert( pOrigVfs!=&gMultiplex.sThisVfs ); |
906 gMultiplex.pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); | 1139 gMultiplex.pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
907 if( !gMultiplex.pMutex ){ | 1140 if( !gMultiplex.pMutex ){ |
908 return SQLITE_NOMEM; | 1141 return SQLITE_NOMEM; |
909 } | 1142 } |
910 gMultiplex.zName = sqlite3_malloc(pOrigVfs->mxPathname); | |
911 if( !gMultiplex.zName ){ | |
912 sqlite3_mutex_free(gMultiplex.pMutex); | |
913 return SQLITE_NOMEM; | |
914 } | |
915 gMultiplex.pGroups = NULL; | 1143 gMultiplex.pGroups = NULL; |
916 gMultiplex.isInitialized = 1; | 1144 gMultiplex.isInitialized = 1; |
917 gMultiplex.pOrigVfs = pOrigVfs; | 1145 gMultiplex.pOrigVfs = pOrigVfs; |
918 gMultiplex.sThisVfs = *pOrigVfs; | 1146 gMultiplex.sThisVfs = *pOrigVfs; |
919 gMultiplex.sThisVfs.szOsFile += sizeof(multiplexConn); | 1147 gMultiplex.sThisVfs.szOsFile += sizeof(multiplexConn); |
920 gMultiplex.sThisVfs.zName = SQLITE_MULTIPLEX_VFS_NAME; | 1148 gMultiplex.sThisVfs.zName = SQLITE_MULTIPLEX_VFS_NAME; |
921 gMultiplex.sThisVfs.xOpen = multiplexOpen; | 1149 gMultiplex.sThisVfs.xOpen = multiplexOpen; |
922 gMultiplex.sThisVfs.xDelete = multiplexDelete; | 1150 gMultiplex.sThisVfs.xDelete = multiplexDelete; |
923 gMultiplex.sThisVfs.xAccess = multiplexAccess; | 1151 gMultiplex.sThisVfs.xAccess = multiplexAccess; |
924 gMultiplex.sThisVfs.xFullPathname = multiplexFullPathname; | 1152 gMultiplex.sThisVfs.xFullPathname = multiplexFullPathname; |
(...skipping 12 matching lines...) Expand all Loading... |
937 gMultiplex.sIoMethodsV1.xRead = multiplexRead; | 1165 gMultiplex.sIoMethodsV1.xRead = multiplexRead; |
938 gMultiplex.sIoMethodsV1.xWrite = multiplexWrite; | 1166 gMultiplex.sIoMethodsV1.xWrite = multiplexWrite; |
939 gMultiplex.sIoMethodsV1.xTruncate = multiplexTruncate; | 1167 gMultiplex.sIoMethodsV1.xTruncate = multiplexTruncate; |
940 gMultiplex.sIoMethodsV1.xSync = multiplexSync; | 1168 gMultiplex.sIoMethodsV1.xSync = multiplexSync; |
941 gMultiplex.sIoMethodsV1.xFileSize = multiplexFileSize; | 1169 gMultiplex.sIoMethodsV1.xFileSize = multiplexFileSize; |
942 gMultiplex.sIoMethodsV1.xLock = multiplexLock; | 1170 gMultiplex.sIoMethodsV1.xLock = multiplexLock; |
943 gMultiplex.sIoMethodsV1.xUnlock = multiplexUnlock; | 1171 gMultiplex.sIoMethodsV1.xUnlock = multiplexUnlock; |
944 gMultiplex.sIoMethodsV1.xCheckReservedLock = multiplexCheckReservedLock; | 1172 gMultiplex.sIoMethodsV1.xCheckReservedLock = multiplexCheckReservedLock; |
945 gMultiplex.sIoMethodsV1.xFileControl = multiplexFileControl; | 1173 gMultiplex.sIoMethodsV1.xFileControl = multiplexFileControl; |
946 gMultiplex.sIoMethodsV1.xSectorSize = multiplexSectorSize; | 1174 gMultiplex.sIoMethodsV1.xSectorSize = multiplexSectorSize; |
947 gMultiplex.sIoMethodsV1.xDeviceCharacteristics = multiplexDeviceCharacteristic
s; | 1175 gMultiplex.sIoMethodsV1.xDeviceCharacteristics = |
| 1176 multiplexDeviceCharacteristics; |
948 gMultiplex.sIoMethodsV2 = gMultiplex.sIoMethodsV1; | 1177 gMultiplex.sIoMethodsV2 = gMultiplex.sIoMethodsV1; |
949 gMultiplex.sIoMethodsV2.iVersion = 2; | 1178 gMultiplex.sIoMethodsV2.iVersion = 2; |
950 gMultiplex.sIoMethodsV2.xShmMap = multiplexShmMap; | 1179 gMultiplex.sIoMethodsV2.xShmMap = multiplexShmMap; |
951 gMultiplex.sIoMethodsV2.xShmLock = multiplexShmLock; | 1180 gMultiplex.sIoMethodsV2.xShmLock = multiplexShmLock; |
952 gMultiplex.sIoMethodsV2.xShmBarrier = multiplexShmBarrier; | 1181 gMultiplex.sIoMethodsV2.xShmBarrier = multiplexShmBarrier; |
953 gMultiplex.sIoMethodsV2.xShmUnmap = multiplexShmUnmap; | 1182 gMultiplex.sIoMethodsV2.xShmUnmap = multiplexShmUnmap; |
954 sqlite3_vfs_register(&gMultiplex.sThisVfs, makeDefault); | 1183 sqlite3_vfs_register(&gMultiplex.sThisVfs, makeDefault); |
955 | 1184 |
956 sqlite3_auto_extension((void*)multiplexFuncInit); | 1185 sqlite3_auto_extension((void*)multiplexFuncInit); |
957 | 1186 |
958 return SQLITE_OK; | 1187 return SQLITE_OK; |
959 } | 1188 } |
960 | 1189 |
961 /* | 1190 /* |
962 ** CAPI: Shutdown the multiplex system - sqlite3_multiplex_shutdown() | 1191 ** CAPI: Shutdown the multiplex system - sqlite3_multiplex_shutdown() |
963 ** | 1192 ** |
964 ** All SQLite database connections must be closed before calling this | 1193 ** All SQLite database connections must be closed before calling this |
965 ** routine. | 1194 ** routine. |
966 ** | 1195 ** |
967 ** THIS ROUTINE IS NOT THREADSAFE. Call this routine exactly once while | 1196 ** THIS ROUTINE IS NOT THREADSAFE. Call this routine exactly once while |
968 ** shutting down in order to free all remaining multiplex groups. | 1197 ** shutting down in order to free all remaining multiplex groups. |
969 */ | 1198 */ |
970 int sqlite3_multiplex_shutdown(void){ | 1199 int sqlite3_multiplex_shutdown(int eForce){ |
| 1200 int rc = SQLITE_OK; |
971 if( gMultiplex.isInitialized==0 ) return SQLITE_MISUSE; | 1201 if( gMultiplex.isInitialized==0 ) return SQLITE_MISUSE; |
972 if( gMultiplex.pGroups ) return SQLITE_MISUSE; | 1202 if( gMultiplex.pGroups ){ |
| 1203 sqlite3_log(SQLITE_MISUSE, "sqlite3_multiplex_shutdown() called " |
| 1204 "while database connections are still open"); |
| 1205 if( !eForce ) return SQLITE_MISUSE; |
| 1206 rc = SQLITE_MISUSE; |
| 1207 } |
973 gMultiplex.isInitialized = 0; | 1208 gMultiplex.isInitialized = 0; |
974 sqlite3_free(gMultiplex.zName); | |
975 sqlite3_mutex_free(gMultiplex.pMutex); | 1209 sqlite3_mutex_free(gMultiplex.pMutex); |
976 sqlite3_vfs_unregister(&gMultiplex.sThisVfs); | 1210 sqlite3_vfs_unregister(&gMultiplex.sThisVfs); |
977 memset(&gMultiplex, 0, sizeof(gMultiplex)); | 1211 memset(&gMultiplex, 0, sizeof(gMultiplex)); |
978 return SQLITE_OK; | 1212 return rc; |
979 } | 1213 } |
980 | 1214 |
981 /***************************** Test Code ***********************************/ | 1215 /***************************** Test Code ***********************************/ |
982 #ifdef SQLITE_TEST | 1216 #ifdef SQLITE_TEST |
983 #include <tcl.h> | 1217 #include <tcl.h> |
984 extern const char *sqlite3TestErrorName(int); | 1218 extern const char *sqlite3ErrName(int); |
985 | 1219 |
986 | 1220 |
987 /* | 1221 /* |
988 ** tclcmd: sqlite3_multiplex_initialize NAME MAKEDEFAULT | 1222 ** tclcmd: sqlite3_multiplex_initialize NAME MAKEDEFAULT |
989 */ | 1223 */ |
990 static int test_multiplex_initialize( | 1224 static int test_multiplex_initialize( |
991 void * clientData, | 1225 void * clientData, |
992 Tcl_Interp *interp, | 1226 Tcl_Interp *interp, |
993 int objc, | 1227 int objc, |
994 Tcl_Obj *CONST objv[] | 1228 Tcl_Obj *CONST objv[] |
995 ){ | 1229 ){ |
996 const char *zName; /* Name of new multiplex VFS */ | 1230 const char *zName; /* Name of new multiplex VFS */ |
997 int makeDefault; /* True to make the new VFS the default */ | 1231 int makeDefault; /* True to make the new VFS the default */ |
998 int rc; /* Value returned by multiplex_initialize() */ | 1232 int rc; /* Value returned by multiplex_initialize() */ |
999 | 1233 |
1000 UNUSED_PARAMETER(clientData); | 1234 UNUSED_PARAMETER(clientData); |
1001 | 1235 |
1002 /* Process arguments */ | 1236 /* Process arguments */ |
1003 if( objc!=3 ){ | 1237 if( objc!=3 ){ |
1004 Tcl_WrongNumArgs(interp, 1, objv, "NAME MAKEDEFAULT"); | 1238 Tcl_WrongNumArgs(interp, 1, objv, "NAME MAKEDEFAULT"); |
1005 return TCL_ERROR; | 1239 return TCL_ERROR; |
1006 } | 1240 } |
1007 zName = Tcl_GetString(objv[1]); | 1241 zName = Tcl_GetString(objv[1]); |
1008 if( Tcl_GetBooleanFromObj(interp, objv[2], &makeDefault) ) return TCL_ERROR; | 1242 if( Tcl_GetBooleanFromObj(interp, objv[2], &makeDefault) ) return TCL_ERROR; |
1009 if( zName[0]=='\0' ) zName = 0; | 1243 if( zName[0]=='\0' ) zName = 0; |
1010 | 1244 |
1011 /* Call sqlite3_multiplex_initialize() */ | 1245 /* Call sqlite3_multiplex_initialize() */ |
1012 rc = sqlite3_multiplex_initialize(zName, makeDefault); | 1246 rc = sqlite3_multiplex_initialize(zName, makeDefault); |
1013 Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC); | 1247 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC); |
1014 | 1248 |
1015 return TCL_OK; | 1249 return TCL_OK; |
1016 } | 1250 } |
1017 | 1251 |
1018 /* | 1252 /* |
1019 ** tclcmd: sqlite3_multiplex_shutdown | 1253 ** tclcmd: sqlite3_multiplex_shutdown |
1020 */ | 1254 */ |
1021 static int test_multiplex_shutdown( | 1255 static int test_multiplex_shutdown( |
1022 void * clientData, | 1256 void * clientData, |
1023 Tcl_Interp *interp, | 1257 Tcl_Interp *interp, |
1024 int objc, | 1258 int objc, |
1025 Tcl_Obj *CONST objv[] | 1259 Tcl_Obj *CONST objv[] |
1026 ){ | 1260 ){ |
1027 int rc; /* Value returned by multiplex_shutdown() */ | 1261 int rc; /* Value returned by multiplex_shutdown() */ |
1028 | 1262 |
1029 UNUSED_PARAMETER(clientData); | 1263 UNUSED_PARAMETER(clientData); |
1030 | 1264 |
1031 if( objc!=1 ){ | 1265 if( objc==2 && strcmp(Tcl_GetString(objv[1]),"-force")!=0 ){ |
1032 Tcl_WrongNumArgs(interp, 1, objv, ""); | 1266 objc = 3; |
| 1267 } |
| 1268 if( (objc!=1 && objc!=2) ){ |
| 1269 Tcl_WrongNumArgs(interp, 1, objv, "?-force?"); |
1033 return TCL_ERROR; | 1270 return TCL_ERROR; |
1034 } | 1271 } |
1035 | 1272 |
1036 /* Call sqlite3_multiplex_shutdown() */ | 1273 /* Call sqlite3_multiplex_shutdown() */ |
1037 rc = sqlite3_multiplex_shutdown(); | 1274 rc = sqlite3_multiplex_shutdown(objc==2); |
1038 Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC); | 1275 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC); |
1039 | 1276 |
1040 return TCL_OK; | 1277 return TCL_OK; |
1041 } | 1278 } |
1042 | 1279 |
1043 /* | 1280 /* |
1044 ** tclcmd: sqlite3_multiplex_dump | 1281 ** tclcmd: sqlite3_multiplex_dump |
1045 */ | 1282 */ |
1046 static int test_multiplex_dump( | 1283 static int test_multiplex_dump( |
1047 void * clientData, | 1284 void * clientData, |
1048 Tcl_Interp *interp, | 1285 Tcl_Interp *interp, |
1049 int objc, | 1286 int objc, |
1050 Tcl_Obj *CONST objv[] | 1287 Tcl_Obj *CONST objv[] |
1051 ){ | 1288 ){ |
1052 Tcl_Obj *pResult; | 1289 Tcl_Obj *pResult; |
1053 Tcl_Obj *pGroupTerm; | 1290 Tcl_Obj *pGroupTerm; |
1054 multiplexGroup *pGroup; | 1291 multiplexGroup *pGroup; |
1055 int i; | 1292 int i; |
1056 int nChunks = 0; | 1293 int nChunks = 0; |
1057 | 1294 |
1058 UNUSED_PARAMETER(clientData); | 1295 UNUSED_PARAMETER(clientData); |
1059 UNUSED_PARAMETER(objc); | 1296 UNUSED_PARAMETER(objc); |
1060 UNUSED_PARAMETER(objv); | 1297 UNUSED_PARAMETER(objv); |
1061 | 1298 |
1062 pResult = Tcl_NewObj(); | 1299 pResult = Tcl_NewObj(); |
1063 multiplexEnter(); | 1300 multiplexEnter(); |
1064 for(pGroup=gMultiplex.pGroups; pGroup; pGroup=pGroup->pNext){ | 1301 for(pGroup=gMultiplex.pGroups; pGroup; pGroup=pGroup->pNext){ |
1065 pGroupTerm = Tcl_NewObj(); | 1302 pGroupTerm = Tcl_NewObj(); |
1066 | 1303 |
1067 pGroup->zName[pGroup->nName] = '\0'; | 1304 if( pGroup->zName ){ |
1068 Tcl_ListObjAppendElement(interp, pGroupTerm, | 1305 pGroup->zName[pGroup->nName] = '\0'; |
| 1306 Tcl_ListObjAppendElement(interp, pGroupTerm, |
1069 Tcl_NewStringObj(pGroup->zName, -1)); | 1307 Tcl_NewStringObj(pGroup->zName, -1)); |
| 1308 }else{ |
| 1309 Tcl_ListObjAppendElement(interp, pGroupTerm, Tcl_NewObj()); |
| 1310 } |
1070 Tcl_ListObjAppendElement(interp, pGroupTerm, | 1311 Tcl_ListObjAppendElement(interp, pGroupTerm, |
1071 Tcl_NewIntObj(pGroup->nName)); | 1312 Tcl_NewIntObj(pGroup->nName)); |
1072 Tcl_ListObjAppendElement(interp, pGroupTerm, | 1313 Tcl_ListObjAppendElement(interp, pGroupTerm, |
1073 Tcl_NewIntObj(pGroup->flags)); | 1314 Tcl_NewIntObj(pGroup->flags)); |
1074 | 1315 |
1075 /* count number of chunks with open handles */ | 1316 /* count number of chunks with open handles */ |
1076 for(i=0; i<pGroup->nMaxChunks; i++){ | 1317 for(i=0; i<pGroup->nReal; i++){ |
1077 if( pGroup->bOpen[i] ) nChunks++; | 1318 if( pGroup->aReal[i].p!=0 ) nChunks++; |
1078 } | 1319 } |
1079 Tcl_ListObjAppendElement(interp, pGroupTerm, | 1320 Tcl_ListObjAppendElement(interp, pGroupTerm, |
1080 Tcl_NewIntObj(nChunks)); | 1321 Tcl_NewIntObj(nChunks)); |
1081 | 1322 |
1082 Tcl_ListObjAppendElement(interp, pGroupTerm, | 1323 Tcl_ListObjAppendElement(interp, pGroupTerm, |
1083 Tcl_NewIntObj(pGroup->nChunkSize)); | 1324 Tcl_NewIntObj(pGroup->szChunk)); |
1084 Tcl_ListObjAppendElement(interp, pGroupTerm, | 1325 Tcl_ListObjAppendElement(interp, pGroupTerm, |
1085 Tcl_NewIntObj(pGroup->nMaxChunks)); | 1326 Tcl_NewIntObj(pGroup->nReal)); |
1086 | 1327 |
1087 Tcl_ListObjAppendElement(interp, pResult, pGroupTerm); | 1328 Tcl_ListObjAppendElement(interp, pResult, pGroupTerm); |
1088 } | 1329 } |
1089 multiplexLeave(); | 1330 multiplexLeave(); |
1090 Tcl_SetObjResult(interp, pResult); | 1331 Tcl_SetObjResult(interp, pResult); |
1091 return TCL_OK; | 1332 return TCL_OK; |
1092 } | 1333 } |
1093 | 1334 |
1094 /* | 1335 /* |
1095 ** Tclcmd: test_multiplex_control HANDLE DBNAME SUB-COMMAND ?INT-VALUE? | 1336 ** Tclcmd: test_multiplex_control HANDLE DBNAME SUB-COMMAND ?INT-VALUE? |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1142 return TCL_ERROR; | 1383 return TCL_ERROR; |
1143 } | 1384 } |
1144 pArg = (void *)&iValue; | 1385 pArg = (void *)&iValue; |
1145 break; | 1386 break; |
1146 default: | 1387 default: |
1147 Tcl_WrongNumArgs(interp, 4, objv, "SUB-COMMAND"); | 1388 Tcl_WrongNumArgs(interp, 4, objv, "SUB-COMMAND"); |
1148 return TCL_ERROR; | 1389 return TCL_ERROR; |
1149 } | 1390 } |
1150 | 1391 |
1151 rc = sqlite3_file_control(db, Tcl_GetString(objv[2]), aSub[idx].op, pArg); | 1392 rc = sqlite3_file_control(db, Tcl_GetString(objv[2]), aSub[idx].op, pArg); |
1152 Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC); | 1393 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC); |
1153 return (rc==SQLITE_OK) ? TCL_OK : TCL_ERROR; | 1394 return (rc==SQLITE_OK) ? TCL_OK : TCL_ERROR; |
1154 } | 1395 } |
1155 | 1396 |
1156 /* | 1397 /* |
1157 ** This routine registers the custom TCL commands defined in this | 1398 ** This routine registers the custom TCL commands defined in this |
1158 ** module. This should be the only procedure visible from outside | 1399 ** module. This should be the only procedure visible from outside |
1159 ** of this module. | 1400 ** of this module. |
1160 */ | 1401 */ |
1161 int Sqlitemultiplex_Init(Tcl_Interp *interp){ | 1402 int Sqlitemultiplex_Init(Tcl_Interp *interp){ |
1162 static struct { | 1403 static struct { |
1163 char *zName; | 1404 char *zName; |
1164 Tcl_ObjCmdProc *xProc; | 1405 Tcl_ObjCmdProc *xProc; |
1165 } aCmd[] = { | 1406 } aCmd[] = { |
1166 { "sqlite3_multiplex_initialize", test_multiplex_initialize }, | 1407 { "sqlite3_multiplex_initialize", test_multiplex_initialize }, |
1167 { "sqlite3_multiplex_shutdown", test_multiplex_shutdown }, | 1408 { "sqlite3_multiplex_shutdown", test_multiplex_shutdown }, |
1168 { "sqlite3_multiplex_dump", test_multiplex_dump }, | 1409 { "sqlite3_multiplex_dump", test_multiplex_dump }, |
1169 { "sqlite3_multiplex_control", test_multiplex_control }, | 1410 { "sqlite3_multiplex_control", test_multiplex_control }, |
1170 }; | 1411 }; |
1171 int i; | 1412 int i; |
1172 | 1413 |
1173 for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){ | 1414 for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){ |
1174 Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0); | 1415 Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0); |
1175 } | 1416 } |
1176 | 1417 |
1177 return TCL_OK; | 1418 return TCL_OK; |
1178 } | 1419 } |
1179 #endif | 1420 #endif |
OLD | NEW |