sqlite3rbu.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. /*
  2. ** 2014 August 30
  3. **
  4. ** The author disclaims copyright to this source code. In place of
  5. ** a legal notice, here is a blessing:
  6. **
  7. ** May you do good and not evil.
  8. ** May you find forgiveness for yourself and forgive others.
  9. ** May you share freely, never taking more than you give.
  10. **
  11. *************************************************************************
  12. **
  13. ** This file contains the public interface for the RBU extension.
  14. */
  15. /*
  16. ** SUMMARY
  17. **
  18. ** Writing a transaction containing a large number of operations on
  19. ** b-tree indexes that are collectively larger than the available cache
  20. ** memory can be very inefficient.
  21. **
  22. ** The problem is that in order to update a b-tree, the leaf page (at least)
  23. ** containing the entry being inserted or deleted must be modified. If the
  24. ** working set of leaves is larger than the available cache memory, then a
  25. ** single leaf that is modified more than once as part of the transaction
  26. ** may be loaded from or written to the persistent media multiple times.
  27. ** Additionally, because the index updates are likely to be applied in
  28. ** random order, access to pages within the database is also likely to be in
  29. ** random order, which is itself quite inefficient.
  30. **
  31. ** One way to improve the situation is to sort the operations on each index
  32. ** by index key before applying them to the b-tree. This leads to an IO
  33. ** pattern that resembles a single linear scan through the index b-tree,
  34. ** and all but guarantees each modified leaf page is loaded and stored
  35. ** exactly once. SQLite uses this trick to improve the performance of
  36. ** CREATE INDEX commands. This extension allows it to be used to improve
  37. ** the performance of large transactions on existing databases.
  38. **
  39. ** Additionally, this extension allows the work involved in writing the
  40. ** large transaction to be broken down into sub-transactions performed
  41. ** sequentially by separate processes. This is useful if the system cannot
  42. ** guarantee that a single update process will run for long enough to apply
  43. ** the entire update, for example because the update is being applied on a
  44. ** mobile device that is frequently rebooted. Even after the writer process
  45. ** has committed one or more sub-transactions, other database clients continue
  46. ** to read from the original database snapshot. In other words, partially
  47. ** applied transactions are not visible to other clients.
  48. **
  49. ** "RBU" stands for "Resumable Bulk Update". As in a large database update
  50. ** transmitted via a wireless network to a mobile device. A transaction
  51. ** applied using this extension is hence refered to as an "RBU update".
  52. **
  53. **
  54. ** LIMITATIONS
  55. **
  56. ** An "RBU update" transaction is subject to the following limitations:
  57. **
  58. ** * The transaction must consist of INSERT, UPDATE and DELETE operations
  59. ** only.
  60. **
  61. ** * INSERT statements may not use any default values.
  62. **
  63. ** * UPDATE and DELETE statements must identify their target rows by
  64. ** non-NULL PRIMARY KEY values. Rows with NULL values stored in PRIMARY
  65. ** KEY fields may not be updated or deleted. If the table being written
  66. ** has no PRIMARY KEY, affected rows must be identified by rowid.
  67. **
  68. ** * UPDATE statements may not modify PRIMARY KEY columns.
  69. **
  70. ** * No triggers will be fired.
  71. **
  72. ** * No foreign key violations are detected or reported.
  73. **
  74. ** * CHECK constraints are not enforced.
  75. **
  76. ** * No constraint handling mode except for "OR ROLLBACK" is supported.
  77. **
  78. **
  79. ** PREPARATION
  80. **
  81. ** An "RBU update" is stored as a separate SQLite database. A database
  82. ** containing an RBU update is an "RBU database". For each table in the
  83. ** target database to be updated, the RBU database should contain a table
  84. ** named "data_<target name>" containing the same set of columns as the
  85. ** target table, and one more - "rbu_control". The data_% table should
  86. ** have no PRIMARY KEY or UNIQUE constraints, but each column should have
  87. ** the same type as the corresponding column in the target database.
  88. ** The "rbu_control" column should have no type at all. For example, if
  89. ** the target database contains:
  90. **
  91. ** CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c UNIQUE);
  92. **
  93. ** Then the RBU database should contain:
  94. **
  95. ** CREATE TABLE data_t1(a INTEGER, b TEXT, c, rbu_control);
  96. **
  97. ** The order of the columns in the data_% table does not matter.
  98. **
  99. ** Instead of a regular table, the RBU database may also contain virtual
  100. ** tables or views named using the data_<target> naming scheme.
  101. **
  102. ** Instead of the plain data_<target> naming scheme, RBU database tables
  103. ** may also be named data<integer>_<target>, where <integer> is any sequence
  104. ** of zero or more numeric characters (0-9). This can be significant because
  105. ** tables within the RBU database are always processed in order sorted by
  106. ** name. By judicious selection of the <integer> portion of the names
  107. ** of the RBU tables the user can therefore control the order in which they
  108. ** are processed. This can be useful, for example, to ensure that "external
  109. ** content" FTS4 tables are updated before their underlying content tables.
  110. **
  111. ** If the target database table is a virtual table or a table that has no
  112. ** PRIMARY KEY declaration, the data_% table must also contain a column
  113. ** named "rbu_rowid". This column is mapped to the table's implicit primary
  114. ** key column - "rowid". Virtual tables for which the "rowid" column does
  115. ** not function like a primary key value cannot be updated using RBU. For
  116. ** example, if the target db contains either of the following:
  117. **
  118. ** CREATE VIRTUAL TABLE x1 USING fts3(a, b);
  119. ** CREATE TABLE x1(a, b)
  120. **
  121. ** then the RBU database should contain:
  122. **
  123. ** CREATE TABLE data_x1(a, b, rbu_rowid, rbu_control);
  124. **
  125. ** All non-hidden columns (i.e. all columns matched by "SELECT *") of the
  126. ** target table must be present in the input table. For virtual tables,
  127. ** hidden columns are optional - they are updated by RBU if present in
  128. ** the input table, or not otherwise. For example, to write to an fts4
  129. ** table with a hidden languageid column such as:
  130. **
  131. ** CREATE VIRTUAL TABLE ft1 USING fts4(a, b, languageid='langid');
  132. **
  133. ** Either of the following input table schemas may be used:
  134. **
  135. ** CREATE TABLE data_ft1(a, b, langid, rbu_rowid, rbu_control);
  136. ** CREATE TABLE data_ft1(a, b, rbu_rowid, rbu_control);
  137. **
  138. ** For each row to INSERT into the target database as part of the RBU
  139. ** update, the corresponding data_% table should contain a single record
  140. ** with the "rbu_control" column set to contain integer value 0. The
  141. ** other columns should be set to the values that make up the new record
  142. ** to insert.
  143. **
  144. ** If the target database table has an INTEGER PRIMARY KEY, it is not
  145. ** possible to insert a NULL value into the IPK column. Attempting to
  146. ** do so results in an SQLITE_MISMATCH error.
  147. **
  148. ** For each row to DELETE from the target database as part of the RBU
  149. ** update, the corresponding data_% table should contain a single record
  150. ** with the "rbu_control" column set to contain integer value 1. The
  151. ** real primary key values of the row to delete should be stored in the
  152. ** corresponding columns of the data_% table. The values stored in the
  153. ** other columns are not used.
  154. **
  155. ** For each row to UPDATE from the target database as part of the RBU
  156. ** update, the corresponding data_% table should contain a single record
  157. ** with the "rbu_control" column set to contain a value of type text.
  158. ** The real primary key values identifying the row to update should be
  159. ** stored in the corresponding columns of the data_% table row, as should
  160. ** the new values of all columns being update. The text value in the
  161. ** "rbu_control" column must contain the same number of characters as
  162. ** there are columns in the target database table, and must consist entirely
  163. ** of 'x' and '.' characters (or in some special cases 'd' - see below). For
  164. ** each column that is being updated, the corresponding character is set to
  165. ** 'x'. For those that remain as they are, the corresponding character of the
  166. ** rbu_control value should be set to '.'. For example, given the tables
  167. ** above, the update statement:
  168. **
  169. ** UPDATE t1 SET c = 'usa' WHERE a = 4;
  170. **
  171. ** is represented by the data_t1 row created by:
  172. **
  173. ** INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..x');
  174. **
  175. ** Instead of an 'x' character, characters of the rbu_control value specified
  176. ** for UPDATEs may also be set to 'd'. In this case, instead of updating the
  177. ** target table with the value stored in the corresponding data_% column, the
  178. ** user-defined SQL function "rbu_delta()" is invoked and the result stored in
  179. ** the target table column. rbu_delta() is invoked with two arguments - the
  180. ** original value currently stored in the target table column and the
  181. ** value specified in the data_xxx table.
  182. **
  183. ** For example, this row:
  184. **
  185. ** INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..d');
  186. **
  187. ** is similar to an UPDATE statement such as:
  188. **
  189. ** UPDATE t1 SET c = rbu_delta(c, 'usa') WHERE a = 4;
  190. **
  191. ** Finally, if an 'f' character appears in place of a 'd' or 's' in an
  192. ** ota_control string, the contents of the data_xxx table column is assumed
  193. ** to be a "fossil delta" - a patch to be applied to a blob value in the
  194. ** format used by the fossil source-code management system. In this case
  195. ** the existing value within the target database table must be of type BLOB.
  196. ** It is replaced by the result of applying the specified fossil delta to
  197. ** itself.
  198. **
  199. ** If the target database table is a virtual table or a table with no PRIMARY
  200. ** KEY, the rbu_control value should not include a character corresponding
  201. ** to the rbu_rowid value. For example, this:
  202. **
  203. ** INSERT INTO data_ft1(a, b, rbu_rowid, rbu_control)
  204. ** VALUES(NULL, 'usa', 12, '.x');
  205. **
  206. ** causes a result similar to:
  207. **
  208. ** UPDATE ft1 SET b = 'usa' WHERE rowid = 12;
  209. **
  210. ** The data_xxx tables themselves should have no PRIMARY KEY declarations.
  211. ** However, RBU is more efficient if reading the rows in from each data_xxx
  212. ** table in "rowid" order is roughly the same as reading them sorted by
  213. ** the PRIMARY KEY of the corresponding target database table. In other
  214. ** words, rows should be sorted using the destination table PRIMARY KEY
  215. ** fields before they are inserted into the data_xxx tables.
  216. **
  217. ** USAGE
  218. **
  219. ** The API declared below allows an application to apply an RBU update
  220. ** stored on disk to an existing target database. Essentially, the
  221. ** application:
  222. **
  223. ** 1) Opens an RBU handle using the sqlite3rbu_open() function.
  224. **
  225. ** 2) Registers any required virtual table modules with the database
  226. ** handle returned by sqlite3rbu_db(). Also, if required, register
  227. ** the rbu_delta() implementation.
  228. **
  229. ** 3) Calls the sqlite3rbu_step() function one or more times on
  230. ** the new handle. Each call to sqlite3rbu_step() performs a single
  231. ** b-tree operation, so thousands of calls may be required to apply
  232. ** a complete update.
  233. **
  234. ** 4) Calls sqlite3rbu_close() to close the RBU update handle. If
  235. ** sqlite3rbu_step() has been called enough times to completely
  236. ** apply the update to the target database, then the RBU database
  237. ** is marked as fully applied. Otherwise, the state of the RBU
  238. ** update application is saved in the RBU database for later
  239. ** resumption.
  240. **
  241. ** See comments below for more detail on APIs.
  242. **
  243. ** If an update is only partially applied to the target database by the
  244. ** time sqlite3rbu_close() is called, various state information is saved
  245. ** within the RBU database. This allows subsequent processes to automatically
  246. ** resume the RBU update from where it left off.
  247. **
  248. ** To remove all RBU extension state information, returning an RBU database
  249. ** to its original contents, it is sufficient to drop all tables that begin
  250. ** with the prefix "rbu_"
  251. **
  252. ** DATABASE LOCKING
  253. **
  254. ** An RBU update may not be applied to a database in WAL mode. Attempting
  255. ** to do so is an error (SQLITE_ERROR).
  256. **
  257. ** While an RBU handle is open, a SHARED lock may be held on the target
  258. ** database file. This means it is possible for other clients to read the
  259. ** database, but not to write it.
  260. **
  261. ** If an RBU update is started and then suspended before it is completed,
  262. ** then an external client writes to the database, then attempting to resume
  263. ** the suspended RBU update is also an error (SQLITE_BUSY).
  264. */
  265. #ifndef _SQLITE3RBU_H
  266. #define _SQLITE3RBU_H
  267. #include "sqlite3.h" /* Required for error code definitions */
  268. #ifdef __cplusplus
  269. extern "C" {
  270. #endif
  271. typedef struct sqlite3rbu sqlite3rbu;
  272. /*
  273. ** Open an RBU handle.
  274. **
  275. ** Argument zTarget is the path to the target database. Argument zRbu is
  276. ** the path to the RBU database. Each call to this function must be matched
  277. ** by a call to sqlite3rbu_close(). When opening the databases, RBU passes
  278. ** the SQLITE_CONFIG_URI flag to sqlite3_open_v2(). So if either zTarget
  279. ** or zRbu begin with "file:", it will be interpreted as an SQLite
  280. ** database URI, not a regular file name.
  281. **
  282. ** If the zState argument is passed a NULL value, the RBU extension stores
  283. ** the current state of the update (how many rows have been updated, which
  284. ** indexes are yet to be updated etc.) within the RBU database itself. This
  285. ** can be convenient, as it means that the RBU application does not need to
  286. ** organize removing a separate state file after the update is concluded.
  287. ** Or, if zState is non-NULL, it must be a path to a database file in which
  288. ** the RBU extension can store the state of the update.
  289. **
  290. ** When resuming an RBU update, the zState argument must be passed the same
  291. ** value as when the RBU update was started.
  292. **
  293. ** Once the RBU update is finished, the RBU extension does not
  294. ** automatically remove any zState database file, even if it created it.
  295. **
  296. ** By default, RBU uses the default VFS to access the files on disk. To
  297. ** use a VFS other than the default, an SQLite "file:" URI containing a
  298. ** "vfs=..." option may be passed as the zTarget option.
  299. **
  300. ** IMPORTANT NOTE FOR ZIPVFS USERS: The RBU extension works with all of
  301. ** SQLite's built-in VFSs, including the multiplexor VFS. However it does
  302. ** not work out of the box with zipvfs. Refer to the comment describing
  303. ** the zipvfs_create_vfs() API below for details on using RBU with zipvfs.
  304. */
  305. SQLITE_API sqlite3rbu *sqlite3rbu_open(
  306. const char *zTarget,
  307. const char *zRbu,
  308. const char *zState
  309. );
  310. /*
  311. ** Open an RBU handle to perform an RBU vacuum on database file zTarget.
  312. ** An RBU vacuum is similar to SQLite's built-in VACUUM command, except
  313. ** that it can be suspended and resumed like an RBU update.
  314. **
  315. ** The second argument to this function identifies a database in which
  316. ** to store the state of the RBU vacuum operation if it is suspended. The
  317. ** first time sqlite3rbu_vacuum() is called, to start an RBU vacuum
  318. ** operation, the state database should either not exist or be empty
  319. ** (contain no tables). If an RBU vacuum is suspended by calling
  320. ** sqlite3rbu_close() on the RBU handle before sqlite3rbu_step() has
  321. ** returned SQLITE_DONE, the vacuum state is stored in the state database.
  322. ** The vacuum can be resumed by calling this function to open a new RBU
  323. ** handle specifying the same target and state databases.
  324. **
  325. ** If the second argument passed to this function is NULL, then the
  326. ** name of the state database is "<database>-vacuum", where <database>
  327. ** is the name of the target database file. In this case, on UNIX, if the
  328. ** state database is not already present in the file-system, it is created
  329. ** with the same permissions as the target db is made.
  330. **
  331. ** With an RBU vacuum, it is an SQLITE_MISUSE error if the name of the
  332. ** state database ends with "-vactmp". This name is reserved for internal
  333. ** use.
  334. **
  335. ** This function does not delete the state database after an RBU vacuum
  336. ** is completed, even if it created it. However, if the call to
  337. ** sqlite3rbu_close() returns any value other than SQLITE_OK, the contents
  338. ** of the state tables within the state database are zeroed. This way,
  339. ** the next call to sqlite3rbu_vacuum() opens a handle that starts a
  340. ** new RBU vacuum operation.
  341. **
  342. ** As with sqlite3rbu_open(), Zipvfs users should rever to the comment
  343. ** describing the sqlite3rbu_create_vfs() API function below for
  344. ** a description of the complications associated with using RBU with
  345. ** zipvfs databases.
  346. */
  347. SQLITE_API sqlite3rbu *sqlite3rbu_vacuum(
  348. const char *zTarget,
  349. const char *zState
  350. );
  351. /*
  352. ** Configure a limit for the amount of temp space that may be used by
  353. ** the RBU handle passed as the first argument. The new limit is specified
  354. ** in bytes by the second parameter. If it is positive, the limit is updated.
  355. ** If the second parameter to this function is passed zero, then the limit
  356. ** is removed entirely. If the second parameter is negative, the limit is
  357. ** not modified (this is useful for querying the current limit).
  358. **
  359. ** In all cases the returned value is the current limit in bytes (zero
  360. ** indicates unlimited).
  361. **
  362. ** If the temp space limit is exceeded during operation, an SQLITE_FULL
  363. ** error is returned.
  364. */
  365. SQLITE_API sqlite3_int64 sqlite3rbu_temp_size_limit(sqlite3rbu*, sqlite3_int64);
  366. /*
  367. ** Return the current amount of temp file space, in bytes, currently used by
  368. ** the RBU handle passed as the only argument.
  369. */
  370. SQLITE_API sqlite3_int64 sqlite3rbu_temp_size(sqlite3rbu*);
  371. /*
  372. ** Internally, each RBU connection uses a separate SQLite database
  373. ** connection to access the target and rbu update databases. This
  374. ** API allows the application direct access to these database handles.
  375. **
  376. ** The first argument passed to this function must be a valid, open, RBU
  377. ** handle. The second argument should be passed zero to access the target
  378. ** database handle, or non-zero to access the rbu update database handle.
  379. ** Accessing the underlying database handles may be useful in the
  380. ** following scenarios:
  381. **
  382. ** * If any target tables are virtual tables, it may be necessary to
  383. ** call sqlite3_create_module() on the target database handle to
  384. ** register the required virtual table implementations.
  385. **
  386. ** * If the data_xxx tables in the RBU source database are virtual
  387. ** tables, the application may need to call sqlite3_create_module() on
  388. ** the rbu update db handle to any required virtual table
  389. ** implementations.
  390. **
  391. ** * If the application uses the "rbu_delta()" feature described above,
  392. ** it must use sqlite3_create_function() or similar to register the
  393. ** rbu_delta() implementation with the target database handle.
  394. **
  395. ** If an error has occurred, either while opening or stepping the RBU object,
  396. ** this function may return NULL. The error code and message may be collected
  397. ** when sqlite3rbu_close() is called.
  398. **
  399. ** Database handles returned by this function remain valid until the next
  400. ** call to any sqlite3rbu_xxx() function other than sqlite3rbu_db().
  401. */
  402. SQLITE_API sqlite3 *sqlite3rbu_db(sqlite3rbu*, int bRbu);
  403. /*
  404. ** Do some work towards applying the RBU update to the target db.
  405. **
  406. ** Return SQLITE_DONE if the update has been completely applied, or
  407. ** SQLITE_OK if no error occurs but there remains work to do to apply
  408. ** the RBU update. If an error does occur, some other error code is
  409. ** returned.
  410. **
  411. ** Once a call to sqlite3rbu_step() has returned a value other than
  412. ** SQLITE_OK, all subsequent calls on the same RBU handle are no-ops
  413. ** that immediately return the same value.
  414. */
  415. SQLITE_API int sqlite3rbu_step(sqlite3rbu *pRbu);
  416. /*
  417. ** Force RBU to save its state to disk.
  418. **
  419. ** If a power failure or application crash occurs during an update, following
  420. ** system recovery RBU may resume the update from the point at which the state
  421. ** was last saved. In other words, from the most recent successful call to
  422. ** sqlite3rbu_close() or this function.
  423. **
  424. ** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
  425. */
  426. SQLITE_API int sqlite3rbu_savestate(sqlite3rbu *pRbu);
  427. /*
  428. ** Close an RBU handle.
  429. **
  430. ** If the RBU update has been completely applied, mark the RBU database
  431. ** as fully applied. Otherwise, assuming no error has occurred, save the
  432. ** current state of the RBU update appliation to the RBU database.
  433. **
  434. ** If an error has already occurred as part of an sqlite3rbu_step()
  435. ** or sqlite3rbu_open() call, or if one occurs within this function, an
  436. ** SQLite error code is returned. Additionally, if pzErrmsg is not NULL,
  437. ** *pzErrmsg may be set to point to a buffer containing a utf-8 formatted
  438. ** English language error message. It is the responsibility of the caller to
  439. ** eventually free any such buffer using sqlite3_free().
  440. **
  441. ** Otherwise, if no error occurs, this function returns SQLITE_OK if the
  442. ** update has been partially applied, or SQLITE_DONE if it has been
  443. ** completely applied.
  444. */
  445. SQLITE_API int sqlite3rbu_close(sqlite3rbu *pRbu, char **pzErrmsg);
  446. /*
  447. ** Return the total number of key-value operations (inserts, deletes or
  448. ** updates) that have been performed on the target database since the
  449. ** current RBU update was started.
  450. */
  451. SQLITE_API sqlite3_int64 sqlite3rbu_progress(sqlite3rbu *pRbu);
  452. /*
  453. ** Obtain permyriadage (permyriadage is to 10000 as percentage is to 100)
  454. ** progress indications for the two stages of an RBU update. This API may
  455. ** be useful for driving GUI progress indicators and similar.
  456. **
  457. ** An RBU update is divided into two stages:
  458. **
  459. ** * Stage 1, in which changes are accumulated in an oal/wal file, and
  460. ** * Stage 2, in which the contents of the wal file are copied into the
  461. ** main database.
  462. **
  463. ** The update is visible to non-RBU clients during stage 2. During stage 1
  464. ** non-RBU reader clients may see the original database.
  465. **
  466. ** If this API is called during stage 2 of the update, output variable
  467. ** (*pnOne) is set to 10000 to indicate that stage 1 has finished and (*pnTwo)
  468. ** to a value between 0 and 10000 to indicate the permyriadage progress of
  469. ** stage 2. A value of 5000 indicates that stage 2 is half finished,
  470. ** 9000 indicates that it is 90% finished, and so on.
  471. **
  472. ** If this API is called during stage 1 of the update, output variable
  473. ** (*pnTwo) is set to 0 to indicate that stage 2 has not yet started. The
  474. ** value to which (*pnOne) is set depends on whether or not the RBU
  475. ** database contains an "rbu_count" table. The rbu_count table, if it
  476. ** exists, must contain the same columns as the following:
  477. **
  478. ** CREATE TABLE rbu_count(tbl TEXT PRIMARY KEY, cnt INTEGER) WITHOUT ROWID;
  479. **
  480. ** There must be one row in the table for each source (data_xxx) table within
  481. ** the RBU database. The 'tbl' column should contain the name of the source
  482. ** table. The 'cnt' column should contain the number of rows within the
  483. ** source table.
  484. **
  485. ** If the rbu_count table is present and populated correctly and this
  486. ** API is called during stage 1, the *pnOne output variable is set to the
  487. ** permyriadage progress of the same stage. If the rbu_count table does
  488. ** not exist, then (*pnOne) is set to -1 during stage 1. If the rbu_count
  489. ** table exists but is not correctly populated, the value of the *pnOne
  490. ** output variable during stage 1 is undefined.
  491. */
  492. SQLITE_API void sqlite3rbu_bp_progress(sqlite3rbu *pRbu, int *pnOne, int*pnTwo);
  493. /*
  494. ** Obtain an indication as to the current stage of an RBU update or vacuum.
  495. ** This function always returns one of the SQLITE_RBU_STATE_XXX constants
  496. ** defined in this file. Return values should be interpreted as follows:
  497. **
  498. ** SQLITE_RBU_STATE_OAL:
  499. ** RBU is currently building a *-oal file. The next call to sqlite3rbu_step()
  500. ** may either add further data to the *-oal file, or compute data that will
  501. ** be added by a subsequent call.
  502. **
  503. ** SQLITE_RBU_STATE_MOVE:
  504. ** RBU has finished building the *-oal file. The next call to sqlite3rbu_step()
  505. ** will move the *-oal file to the equivalent *-wal path. If the current
  506. ** operation is an RBU update, then the updated version of the database
  507. ** file will become visible to ordinary SQLite clients following the next
  508. ** call to sqlite3rbu_step().
  509. **
  510. ** SQLITE_RBU_STATE_CHECKPOINT:
  511. ** RBU is currently performing an incremental checkpoint. The next call to
  512. ** sqlite3rbu_step() will copy a page of data from the *-wal file into
  513. ** the target database file.
  514. **
  515. ** SQLITE_RBU_STATE_DONE:
  516. ** The RBU operation has finished. Any subsequent calls to sqlite3rbu_step()
  517. ** will immediately return SQLITE_DONE.
  518. **
  519. ** SQLITE_RBU_STATE_ERROR:
  520. ** An error has occurred. Any subsequent calls to sqlite3rbu_step() will
  521. ** immediately return the SQLite error code associated with the error.
  522. */
  523. #define SQLITE_RBU_STATE_OAL 1
  524. #define SQLITE_RBU_STATE_MOVE 2
  525. #define SQLITE_RBU_STATE_CHECKPOINT 3
  526. #define SQLITE_RBU_STATE_DONE 4
  527. #define SQLITE_RBU_STATE_ERROR 5
  528. SQLITE_API int sqlite3rbu_state(sqlite3rbu *pRbu);
  529. /*
  530. ** As part of applying an RBU update or performing an RBU vacuum operation,
  531. ** the system must at one point move the *-oal file to the equivalent *-wal
  532. ** path. Normally, it does this by invoking POSIX function rename(2) directly.
  533. ** Except on WINCE platforms, where it uses win32 API MoveFileW(). This
  534. ** function may be used to register a callback that the RBU module will invoke
  535. ** instead of one of these APIs.
  536. **
  537. ** If a callback is registered with an RBU handle, it invokes it instead
  538. ** of rename(2) when it needs to move a file within the file-system. The
  539. ** first argument passed to the xRename() callback is a copy of the second
  540. ** argument (pArg) passed to this function. The second is the full path
  541. ** to the file to move and the third the full path to which it should be
  542. ** moved. The callback function should return SQLITE_OK to indicate
  543. ** success. If an error occurs, it should return an SQLite error code.
  544. ** In this case the RBU operation will be abandoned and the error returned
  545. ** to the RBU user.
  546. **
  547. ** Passing a NULL pointer in place of the xRename argument to this function
  548. ** restores the default behaviour.
  549. */
  550. SQLITE_API void sqlite3rbu_rename_handler(
  551. sqlite3rbu *pRbu,
  552. void *pArg,
  553. int (*xRename)(void *pArg, const char *zOld, const char *zNew)
  554. );
  555. /*
  556. ** Create an RBU VFS named zName that accesses the underlying file-system
  557. ** via existing VFS zParent. Or, if the zParent parameter is passed NULL,
  558. ** then the new RBU VFS uses the default system VFS to access the file-system.
  559. ** The new object is registered as a non-default VFS with SQLite before
  560. ** returning.
  561. **
  562. ** Part of the RBU implementation uses a custom VFS object. Usually, this
  563. ** object is created and deleted automatically by RBU.
  564. **
  565. ** The exception is for applications that also use zipvfs. In this case,
  566. ** the custom VFS must be explicitly created by the user before the RBU
  567. ** handle is opened. The RBU VFS should be installed so that the zipvfs
  568. ** VFS uses the RBU VFS, which in turn uses any other VFS layers in use
  569. ** (for example multiplexor) to access the file-system. For example,
  570. ** to assemble an RBU enabled VFS stack that uses both zipvfs and
  571. ** multiplexor (error checking omitted):
  572. **
  573. ** // Create a VFS named "multiplex" (not the default).
  574. ** sqlite3_multiplex_initialize(0, 0);
  575. **
  576. ** // Create an rbu VFS named "rbu" that uses multiplexor. If the
  577. ** // second argument were replaced with NULL, the "rbu" VFS would
  578. ** // access the file-system via the system default VFS, bypassing the
  579. ** // multiplexor.
  580. ** sqlite3rbu_create_vfs("rbu", "multiplex");
  581. **
  582. ** // Create a zipvfs VFS named "zipvfs" that uses rbu.
  583. ** zipvfs_create_vfs_v3("zipvfs", "rbu", 0, xCompressorAlgorithmDetector);
  584. **
  585. ** // Make zipvfs the default VFS.
  586. ** sqlite3_vfs_register(sqlite3_vfs_find("zipvfs"), 1);
  587. **
  588. ** Because the default VFS created above includes a RBU functionality, it
  589. ** may be used by RBU clients. Attempting to use RBU with a zipvfs VFS stack
  590. ** that does not include the RBU layer results in an error.
  591. **
  592. ** The overhead of adding the "rbu" VFS to the system is negligible for
  593. ** non-RBU users. There is no harm in an application accessing the
  594. ** file-system via "rbu" all the time, even if it only uses RBU functionality
  595. ** occasionally.
  596. */
  597. SQLITE_API int sqlite3rbu_create_vfs(const char *zName, const char *zParent);
  598. /*
  599. ** Deregister and destroy an RBU vfs created by an earlier call to
  600. ** sqlite3rbu_create_vfs().
  601. **
  602. ** VFS objects are not reference counted. If a VFS object is destroyed
  603. ** before all database handles that use it have been closed, the results
  604. ** are undefined.
  605. */
  606. SQLITE_API void sqlite3rbu_destroy_vfs(const char *zName);
  607. #ifdef __cplusplus
  608. } /* end of the 'extern "C"' block */
  609. #endif
  610. #endif /* _SQLITE3RBU_H */