Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Have the ota extension perform an incremental checkpoint after generating the wal file. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | ota-update |
Files: | files | file ages | folders |
SHA1: |
0bf1301aacb3b717b4cc020fbda9fab0 |
User & Date: | dan 2014-10-20 16:24:23.616 |
Context
2014-10-20
| ||
16:34 | Merge version-3.8.7 changes with this branch. (check-in: d380a6482a user: dan tags: ota-update) | |
16:24 | Have the ota extension perform an incremental checkpoint after generating the wal file. (check-in: 0bf1301aac user: dan tags: ota-update) | |
2014-09-19
| ||
18:08 | Add further tests to ota5.test. Add "ota.test", for running all ota tests. (check-in: 95ffdaa542 user: dan tags: ota-update) | |
Changes
Changes to ext/ota/ota1.test.
︙ | |||
120 121 122 123 124 125 126 | 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | - + - + - + | CREATE INDEX i1 ON t1(b, c); CREATE INDEX i2 ON t1(c, b); CREATE INDEX i3 ON t1(a, b, c, a, b, c); } } { reset_db execsql $schema |
︙ |
Changes to ext/ota/sqlite3ota.c.
︙ | |||
18 19 20 21 22 23 24 | 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | - - + + + + + + + + + + + + - + + - - + + + + - + + + - - + + + + + + + + + + + + + + + + + + + + - + + + + | #include "sqlite3.h" #include "sqlite3ota.h" /* ** The ota_state table is used to save the state of a partially applied |
︙ | |||
84 85 86 87 88 89 90 91 92 93 94 95 96 | 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | + + - + + | sqlite3_stmt *pUpdate; /* Last update statement (or NULL) */ }; /* ** OTA handle. */ struct sqlite3ota { int eStage; /* Value of OTA_STATE_STAGE field */ sqlite3 *db; /* "main" -> target db, "ota" -> ota db */ char *zTarget; /* Path to target db */ char *zOta; /* Path to ota db */ int rc; /* Value returned by last ota_step() call */ char *zErrmsg; /* Error message if rc!=SQLITE_OK */ int nStep; /* Rows processed for current object */ int nProgress; /* Rows processed for all objects */ |
︙ | |||
737 738 739 740 741 742 743 744 745 746 747 748 749 750 | 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 | + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + | memcpy(pIter->zMask, zMask, pIter->nTblCol); } sqlite3_free(zWhere); sqlite3_free(zSet); } return p->rc; } static void otaOpenDatabase(sqlite3ota *p){ assert( p->rc==SQLITE_OK ); sqlite3_close(p->db); p->db = 0; p->rc = sqlite3_open(p->zTarget, &p->db); if( p->rc ){ p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(p->db)); } otaMPrintfExec(p, "ATTACH %Q AS ota", p->zOta); } /* ** This routine is a copy of the sqlite3FileSuffix3() routine from the core. ** It is a no-op unless SQLITE_ENABLE_8_3_NAMES is defined. ** ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database ** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and ** if filename in z[] has a suffix (a.k.a. "extension") that is longer than ** three characters, then shorten the suffix on z[] to be the last three ** characters of the original suffix. ** ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always ** do the suffix shortening regardless of URI parameter. ** ** Examples: ** ** test.db-journal => test.nal ** test.db-wal => test.wal ** test.db-shm => test.shm ** test.db-mj7f3319fa => test.9fa */ static void otaFileSuffix3(const char *zBase, char *z){ #ifdef SQLITE_ENABLE_8_3_NAMES #if SQLITE_ENABLE_8_3_NAMES<2 if( sqlite3_uri_boolean(zBase, "8_3_names", 0) ) #endif { int i, sz; sz = sqlite3Strlen30(z); for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){} if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4); } #endif } /* ** Move the "*-oal" file corresponding to the target database to the ** "*-wal" location. If an error occurs, leave an error code and error ** message in the ota handle. */ static void otaMoveOalFile(sqlite3ota *p){ const char *zBase = sqlite3_db_filename(p->db, "main"); char *zWal = sqlite3_mprintf("%s-wal", zBase); char *zOal = sqlite3_mprintf("%s-oal", zBase); assert( p->rc==SQLITE_OK && p->zErrmsg==0 ); if( zWal==0 || zOal==0 ){ p->rc = SQLITE_NOMEM; }else{ /* Move the *-oal file to *-wal. At this point connection p->db is ** holding a SHARED lock on the target database file (because it is ** in WAL mode). So no other connection may be writing the db. */ otaFileSuffix3(zBase, zWal); otaFileSuffix3(zBase, zOal); rename(zOal, zWal); /* Re-open the databases. */ otaObjIterFinalize(&p->objiter); otaOpenDatabase(p); p->eStage = OTA_STAGE_CKPT; } sqlite3_free(zWal); sqlite3_free(zOal); } /* ** The SELECT statement iterating through the keys for the current object ** (p->objiter.pSelect) currently points to a valid row. This function ** determines the type of operation requested by this row and returns ** one of the following values to indicate the result: ** |
︙ | |||
858 859 860 861 862 863 864 865 866 867 868 869 870 | 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 | + + + + + + + + + + + + + + + + + + + + + + + + - - + + - - - - - - - - - - - - + + + + + + + + + + + + - - - - - - - - + + + + + + + + - - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + + + - - - - - + + + + + + + - - + + + + + - - + - - - - - + + + + + + - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - | /* no-op */ assert( eType==OTA_DELETE && pIter->zIdx ); } } return p->rc; } /* ** Increment the schema cookie of the main database opened by p->db. */ static void otaIncrSchemaCookie(sqlite3ota *p){ int iCookie = 1000000; sqlite3_stmt *pStmt; assert( p->rc==SQLITE_OK && p->zErrmsg==0 ); p->rc = prepareAndCollectError(p->db, &pStmt, &p->zErrmsg, "PRAGMA schema_version" ); if( p->rc==SQLITE_OK ){ if( SQLITE_ROW==sqlite3_step(pStmt) ){ iCookie = sqlite3_column_int(pStmt, 0); } p->rc = sqlite3_finalize(pStmt); } if( p->rc==SQLITE_OK ){ otaMPrintfExec(p, "PRAGMA schema_version = %d", iCookie+1); } } /* ** Step the OTA object. */ int sqlite3ota_step(sqlite3ota *p){ if( p ){ switch( p->eStage ){ case OTA_STAGE_OAL: { |
︙ | |||
995 996 997 998 999 1000 1001 | 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - + - - + + - - - + + - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + - - - - + + + + - + - - - - - + + + + + + + + + - - + - + - - - + + + + + + - + + - - - - - - | rc = otaObjIterPrepareAll(p, &p->objiter, p->nStep); } p->rc = rc; } } |
︙ |
Changes to src/main.c.
︙ | |||
1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 | 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 | + + + + + + + + + + + + + + + + + + | sqlite3Error(db, rc); } rc = sqlite3ApiExit(db, rc); sqlite3_mutex_leave(db->mutex); return rc; #endif } int sqlite3_ckpt_open( sqlite3 *db, unsigned char *a, int n, sqlite3_ckpt **ppCkpt ){ Pager *pPager; Btree *pBt; int rc; *ppCkpt = 0; sqlite3_mutex_enter(db->mutex); pBt = db->aDb[0].pBt; pPager = sqlite3BtreePager(pBt); rc = sqlite3PagerWalCheckpointStart(db, pPager, a, n, ppCkpt); sqlite3_mutex_leave(db->mutex); return rc; } /* ** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points ** to contains a zero-length string, all attached databases are ** checkpointed. */ |
︙ |
Changes to src/pager.c.
︙ | |||
619 620 621 622 623 624 625 626 627 628 629 630 631 632 | 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 | + + | ** otaMode ** This variable is normally 0. It is set to 1 by the PagerSetOtaMode() ** function - as a result of a "PRAGMA pager_ota_mode=1" command. Once ** the *-oal file has been opened and it has been determined that the ** database file has not been modified since it was created, this variable ** is set to 2. ** ** noCkptOnClose ** ** */ struct Pager { sqlite3_vfs *pVfs; /* OS functions to use for IO */ u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */ u8 journalMode; /* One of the PAGER_JOURNALMODE_* values */ u8 useJournal; /* Use a rollback journal on this file */ |
︙ | |||
7284 7285 7286 7287 7288 7289 7290 | 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 | - - + + - + + + + + + + + + + + + + | return sqlite3WalFramesize(pPager->pWal); } #endif /* ** Set or clear the "OTA mode" flag. */ |
Changes to src/pager.h.
︙ | |||
205 206 207 208 209 210 211 212 213 | 205 206 207 208 209 210 211 212 213 214 | + | #else # define disable_simulated_io_errors() # define enable_simulated_io_errors() #endif int sqlite3PagerSetOtaMode(Pager *pPager, int bOta); void sqlite3PagerWalSalt(Pager *pPager, u32 *aSalt); int sqlite3PagerWalCheckpointStart(sqlite3*, Pager*, u8*, int, sqlite3_ckpt**); #endif /* _PAGER_H_ */ |
Changes to src/pragma.c.
︙ | |||
896 897 898 899 900 901 902 | 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 | - + | ** Other clients see a rollback-mode database on which the pager_ota_mode ** client is holding a SHARED lock. */ case PragTyp_PAGER_OTA_MODE: { Btree *pBt = pDb->pBt; assert( pBt!=0 ); if( zRight ){ |
︙ |
Changes to src/sqlite.h.in.
︙ | |||
7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 | 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 | + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + | int bDelete, /* Zero for insert, non-zero for delete */ const char *zIndex, /* Index to write to */ sqlite3_stmt**, /* OUT: New statement handle */ const char ***pazColl, /* OUT: Collation sequences for each column */ int **paiCol, int *pnCol /* OUT: See above */ ); /* ** Incremental checkpoint API. ** ** An incremental checkpoint handle is opened using the sqlite3_ckpt_open() ** API. To begin a new checkpoint, the second and third arguments should both ** be passed zero. To resume an earlier checkpoint, the second and third ** arguments should specify a buffer returned by an earlier call to ** sqlite3_ckpt_close(). When resuming a checkpoint, if the database or WAL ** file has been modified since the checkpoint was suspended, the ** sqlite3_ckpt_open() call fails with SQLITE_MISMATCH. ** ** Each time sqlite3_ckpt_step() is called on an open checkpoint handle, a ** single page is copied from the WAL file to the database. If no error ** occurs, but the checkpoint is not finished, SQLITE_OK is returned. If the ** checkpoint has been finished (and so sqlite3_ckpt_step() should not be ** called again), SQLITE_DONE is returned. Otherwise, if an error occurs, ** some other SQLite error code is returned. ** ** Calling sqlite3_ckpt_close() closes an open checkpoint handle. If the ** checkpoint has finished and no error has occurred, SQLITE_OK is returned ** and the two output parameters zeroed. Or, if an error has occurred, an ** error code is returned and the two output parameters are zeroed. Finally, ** if the checkpoint is not finished but no error has occurred, SQLITE_OK is ** returned and the first output variable set to point to a buffer allocated ** using sqlite3_malloc() containing the serialized state of the checkpoint. ** The contents of this buffer may be passed to a later call to ** sqlite3_ckpt_open() to restart the checkpoint. The second output variable ** is set to the size of the buffer in bytes. */ typedef struct sqlite3_ckpt sqlite3_ckpt; int sqlite3_ckpt_open(sqlite3*, unsigned char *a, int n, sqlite3_ckpt **ppCkpt); int sqlite3_ckpt_step(sqlite3_ckpt*); int sqlite3_ckpt_close(sqlite3_ckpt*, unsigned char **pa, int *pn); /* ** Undo the hack that converts floating point types to integer for ** builds on processors without floating point support. */ #ifdef SQLITE_OMIT_FLOATING_POINT # undef double #endif |
︙ |
Changes to src/wal.c.
︙ | |||
478 479 480 481 482 483 484 485 486 487 488 489 490 491 | 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 | + + + + + + + + + + + + + + + + + + | ht_slot *aIndex; /* i0, i1, i2... such that aPgno[iN] ascend */ u32 *aPgno; /* Array of page numbers. */ int nEntry; /* Nr. of entries in aPgno[] and aIndex[] */ int iZero; /* Frame number associated with aPgno[0] */ } aSegment[1]; /* One for every 32KB page in the wal-index */ }; /* ** walCheckpoint */ typedef struct WalCkpt WalCkpt; struct WalCkpt { sqlite3 *db; /* Database pointer (incremental only) */ int szPage; /* Database page-size */ int sync_flags; /* Flags for OsSync() (or 0) */ u32 mxSafeFrame; /* Max frame that can be backfilled */ u32 mxPage; /* Max database page to write */ volatile WalCkptInfo *pInfo; /* The checkpoint status information */ WalIterator *pIter; /* Wal iterator context */ Wal *pWal; /* Pointer to owner object */ u8 *aBuf; /* Temporary page-sized buffer to use */ int rc; /* Error code. SQLITE_DONE -> finished */ int nStep; /* Number of times pIter has been stepped */ }; /* ** Define the parameters of the hash tables in the wal-index file. There ** is a hash-table following every HASHTABLE_NPAGE page numbers in the ** wal-index. ** ** Changing any of these constants will alter the wal-index format and ** create incompatibilities. |
︙ | |||
1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 | 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 | + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + | /* ** The cache of the wal-index header must be valid to call this function. ** Return the page-size in bytes used by the database. */ static int walPagesize(Wal *pWal){ return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16); } static int walCheckpointStart( Wal *pWal, u8 *aBuf, /* Page-sized temporary buffer */ int nBuf, /* Size of aBuf[] in bytes */ int (*xBusy)(void*), /* Function to call when busy (or NULL) */ void *pBusyArg, /* Context argument for xBusyHandler */ int sync_flags, /* Flags for OsSync() (or 0) */ WalCkpt *p /* Allocated object to populate */ ){ int rc; /* Return code */ int i; /* Iterator variable */ memset(p, 0, sizeof(WalCkpt)); if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){ return SQLITE_CORRUPT_BKPT; } p->szPage = walPagesize(pWal); p->pWal = pWal; p->aBuf = aBuf; p->sync_flags = sync_flags; testcase( p->szPage<=32768 ); testcase( p->szPage>=65536 ); p->pInfo = walCkptInfo(pWal); if( p->pInfo->nBackfill>=pWal->hdr.mxFrame ) return SQLITE_OK; /* Allocate the iterator */ rc = walIteratorInit(pWal, &p->pIter); if( rc!=SQLITE_OK ) return rc; assert( p->pIter ); /* Compute in mxSafeFrame the index of the last frame of the WAL that is ** safe to write into the database. Frames beyond mxSafeFrame might ** overwrite database pages that are in use by active readers and thus ** cannot be backfilled from the WAL. */ p->mxSafeFrame = pWal->hdr.mxFrame; p->mxPage = pWal->hdr.nPage; for(i=1; i<WAL_NREADER; i++){ u32 y = p->pInfo->aReadMark[i]; if( p->mxSafeFrame>y ){ assert( y<=pWal->hdr.mxFrame ); rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), 1); if( rc==SQLITE_OK ){ p->pInfo->aReadMark[i] = (i==1 ? p->mxSafeFrame : READMARK_NOT_USED); walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1); }else if( rc==SQLITE_BUSY ){ p->mxSafeFrame = y; xBusy = 0; }else{ walIteratorFree(p->pIter); p->pIter = 0; return rc; } } } if( p->pInfo->nBackfill>=p->mxSafeFrame || (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0), 1))!=SQLITE_OK ){ walIteratorFree(p->pIter); p->pIter = 0; } if( rc==SQLITE_BUSY ) rc = SQLITE_OK; if( rc==SQLITE_OK && p->pIter ){ /* Sync the WAL to disk */ if( sync_flags ){ rc = sqlite3OsSync(pWal->pWalFd, sync_flags); } /* If the database may grow as a result of this checkpoint, hint ** about the eventual size of the db file to the VFS layer. */ if( rc==SQLITE_OK ){ i64 nSize; /* Current size of database file */ i64 nReq = ((i64)p->mxPage * p->szPage); rc = sqlite3OsFileSize(pWal->pDbFd, &nSize); if( rc==SQLITE_OK && nSize<nReq ){ sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq); } } } return rc; } static int walCheckpointStep(WalCkpt *p){ u32 iDbpage = 0; /* Next database page to write */ u32 iFrame = 0; /* Wal frame containing data for iDbpage */ int rc = SQLITE_DONE; assert( p->rc==SQLITE_OK ); while( p->pIter && 0==walIteratorNext(p->pIter, &iDbpage, &iFrame) ){ i64 iOffset; assert( walFramePgno(p->pWal, iFrame)==iDbpage ); p->nStep++; if( iFrame<=p->pInfo->nBackfill || iFrame>p->mxSafeFrame || iDbpage>p->mxPage ){ continue; } iOffset = walFrameOffset(iFrame, p->szPage) + WAL_FRAME_HDRSIZE; /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */ rc = sqlite3OsRead(p->pWal->pWalFd, p->aBuf, p->szPage, iOffset); if( rc!=SQLITE_OK ) break; iOffset = (iDbpage-1)*(i64)p->szPage; testcase( IS_BIG_INT(iOffset) ); rc = sqlite3OsWrite(p->pWal->pDbFd, p->aBuf, p->szPage, iOffset); break; } p->rc = rc; return rc; } static int walCheckpointFinalize(WalCkpt *p){ if( p->pIter ){ int rc = p->rc; Wal *pWal = p->pWal; /* If work was completed */ if( p->pIter && rc==SQLITE_DONE ){ rc = SQLITE_OK; if( p->mxSafeFrame==walIndexHdr(pWal)->mxFrame ){ i64 szDb = pWal->hdr.nPage*(i64)p->szPage; testcase( IS_BIG_INT(szDb) ); rc = sqlite3OsTruncate(pWal->pDbFd, szDb); if( rc==SQLITE_OK && p->sync_flags ){ rc = sqlite3OsSync(pWal->pDbFd, p->sync_flags); } } if( rc==SQLITE_OK ){ p->pInfo->nBackfill = p->mxSafeFrame; } p->rc = rc; } /* Release the reader lock held while backfilling */ walUnlockExclusive(pWal, WAL_READ_LOCK(0), 1); walIteratorFree(p->pIter); p->pIter = 0; } return p->rc; } /* ** Copy as much content as we can from the WAL back into the database file ** in response to an sqlite3_wal_checkpoint() request or the equivalent. ** ** The amount of information copies from WAL to database might be limited ** by active readers. This routine will never overwrite a database page |
︙ | |||
1656 1657 1658 1659 1660 1661 1662 | 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 | + - + - - - - - - - - - + - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - + - - - - - + - - + - - - - - - - - - - + - - - - - - + - - + - - - - - - - - - - - - - + - - - - - - - - - - + - + - + | */ static int walCheckpoint( Wal *pWal, /* Wal connection */ int eMode, /* One of PASSIVE, FULL or RESTART */ int (*xBusyCall)(void*), /* Function to call when busy */ void *pBusyArg, /* Context argument for xBusyHandler */ int sync_flags, /* Flags for OsSync() (or 0) */ u8 *zBuf, /* Temporary buffer to use */ |
︙ | |||
2967 2968 2969 2970 2971 2972 2973 | 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 | - - - - + - | if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){ sqlite3OsUnfetch(pWal->pDbFd, 0, 0); } } /* Copy data from the log to the database file. */ if( rc==SQLITE_OK ){ |
︙ | |||
2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 | 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 | + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + | /* Release the locks. */ sqlite3WalEndWriteTransaction(pWal); walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1); pWal->ckptLock = 0; WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok")); return (rc==SQLITE_OK && eMode!=eMode2 ? SQLITE_BUSY : rc); } int sqlite3_ckpt_step(sqlite3_ckpt *pCkpt){ int rc; WalCkpt *p = (WalCkpt*)pCkpt; sqlite3_mutex_enter(p->db->mutex); rc = walCheckpointStep(p); sqlite3_mutex_leave(p->db->mutex); return rc; } int sqlite3_ckpt_close(sqlite3_ckpt *pCkpt, u8 **paState, int *pnState){ int rc; WalCkpt *p = (WalCkpt*)pCkpt; sqlite3 *db = p->db; Wal *pWal = p->pWal; sqlite3_mutex_enter(db->mutex); if( paState ){ *paState = 0; *pnState = 0; if( p->rc==SQLITE_OK ){ u8 *aState = sqlite3_malloc(sizeof(u32) * 3); if( aState==0 ){ p->rc = SQLITE_NOMEM; }else{ *pnState = sizeof(u32)*3; sqlite3Put4byte(&aState[0], p->nStep); sqlite3Put4byte(&aState[4], p->pWal->hdr.aCksum[0]); sqlite3Put4byte(&aState[8], p->pWal->hdr.aCksum[1]); *paState = aState; } } } rc = walCheckpointFinalize(p); walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1); pWal->ckptLock = 0; sqlite3_free(p); memset(&pWal->hdr, 0, sizeof(WalIndexHdr)); sqlite3_mutex_leave(db->mutex); return rc; } int sqlite3WalCheckpointStart( sqlite3 *db, /* Database connection */ Wal *pWal, /* Wal connection */ u8 *aState, int nState, /* Checkpoint state to restore */ int (*xBusy)(void*), /* Function to call when busy */ void *pBusyArg, /* Context argument for xBusyHandler */ int sync_flags, /* Flags to sync db file with (or 0) */ sqlite3_ckpt **ppCkpt /* OUT: Incremental checkpoint object */ ){ WalCkpt *p = 0; int isChanged = 0; int rc; int pgsz; *ppCkpt = 0; if( pWal->readOnly ) return SQLITE_READONLY; WALTRACE(("WAL%p: checkpoint begins\n", pWal)); rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1); if( rc ){ /* Usually this is SQLITE_BUSY meaning that another thread or process ** is already running a checkpoint, or maybe a recovery. But it might ** also be SQLITE_IOERR. */ return rc; } pWal->ckptLock = 1; /* Read the wal-index header. */ rc = walIndexReadHdr(pWal, &isChanged); if( rc!=SQLITE_OK ) goto ckptstart_out; if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){ sqlite3OsUnfetch(pWal->pDbFd, 0, 0); } pgsz = walPagesize(pWal); p = sqlite3_malloc(sizeof(WalCkpt) + pgsz); if( p==0 ){ rc = SQLITE_NOMEM; goto ckptstart_out; } rc = walCheckpointStart( pWal, (u8*)&p[1], pgsz, xBusy, pBusyArg, sync_flags, p ); p->db = db; if( rc==SQLITE_OK && aState ){ if( nState!=sizeof(u32)*3 ){ rc = SQLITE_CORRUPT_BKPT; }else{ int i; if( pWal->hdr.aCksum[0]!=sqlite3Get4byte(&aState[4]) || pWal->hdr.aCksum[1]!=sqlite3Get4byte(&aState[8]) ){ rc = SQLITE_MISMATCH; }else{ p->nStep = (int)sqlite3Get4byte(aState); sqlite3Put4byte(&aState[4], pWal->hdr.aCksum[0]); sqlite3Put4byte(&aState[8], pWal->hdr.aCksum[1]); for(i=0; rc==SQLITE_OK && i<p->nStep; i++){ u32 dummy1, dummy2; rc = walIteratorNext(p->pIter, &dummy1, &dummy2); } } } } ckptstart_out: if( rc!=SQLITE_OK ){ if( p ) walIteratorFree(p->pIter); walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1); pWal->ckptLock = 0; sqlite3_free(p); p = 0; } *ppCkpt = (sqlite3_ckpt*)p; return rc; } /* Return the value to pass to a sqlite3_wal_hook callback, the ** number of frames in the WAL at the point of the last commit since ** sqlite3WalCallback() was called. If no commits have occurred since ** the last call, then return 0. */ int sqlite3WalCallback(Wal *pWal){ |
︙ |
Changes to src/wal.h.
︙ | |||
123 124 125 126 127 128 129 130 131 132 133 134 135 136 | 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | + + + + + + + + + | /* Return true if the argument is non-NULL and the WAL module is using ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the ** WAL module is using shared-memory, return false. */ int sqlite3WalHeapMemory(Wal *pWal); int sqlite3WalCheckSalt(Wal *pWal, sqlite3_file*); int sqlite3WalCheckpointStart(sqlite3 *, Wal *pWal, /* Wal connection */ u8 *aState, int nState, /* Checkpoint state to restore */ int (*xBusy)(void*), /* Function to call when busy */ void *pBusyArg, /* Context argument for xBusyHandler */ int sync_flags, /* Flags to sync db file with (or 0) */ sqlite3_ckpt **ppCkpt /* OUT: Incremental checkpoint object */ ); #ifdef SQLITE_ENABLE_ZIPVFS /* If the WAL file is not empty, return the number of bytes of content ** stored in each frame (i.e. the db page-size when the WAL was created). */ int sqlite3WalFramesize(Wal *pWal); #endif |
︙ |
Changes to test/wal.test.
︙ | |||
371 372 373 374 375 376 377 378 379 380 381 382 383 384 | 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 | + | list [file size test.db] [file size test.db-wal] } [list 2048 [wal_file_size 3 1024]] # Execute some transactions in auto-vacuum mode to test database file # truncation. # do_test wal-8.1 { breakpoint reopen_db catch { db close } forcedelete test.db test.db-wal sqlite3 db test.db db function blob blob execsql { |
︙ |