| | @@ -1,8 +1,8 @@ |
| 1 | 1 | /****************************************************************************** |
| 2 | 2 | ** This file is an amalgamation of many separate C source files from SQLite |
| 3 | | -** version 3.6.23.1. By combining all the individual C code files into this |
| 3 | +** version 3.6.23. By combining all the individual C code files into this |
| 4 | 4 | ** single large file, the entire code can be compiled as a one translation |
| 5 | 5 | ** unit. This allows many compilers to do optimizations that would not be |
| 6 | 6 | ** possible if the files were compiled separately. Performance improvements |
| 7 | 7 | ** of 5% are more are commonly seen when SQLite is compiled as a single |
| 8 | 8 | ** translation unit. |
| | @@ -321,11 +321,11 @@ |
| 321 | 321 | ** to generate appropriate macros for a wide range of compilers. |
| 322 | 322 | ** |
| 323 | 323 | ** The correct "ANSI" way to do this is to use the intptr_t type. |
| 324 | 324 | ** Unfortunately, that typedef is not available on all compilers, or |
| 325 | 325 | ** if it is available, it requires an #include of specific headers |
| 326 | | -** that very from one machine to the next. |
| 326 | +** that vary from one machine to the next. |
| 327 | 327 | ** |
| 328 | 328 | ** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on |
| 329 | 329 | ** the ((void*)&((char*)0)[X]) construct. But MSVC chokes on ((void*)(X)). |
| 330 | 330 | ** So we have to define the macros in different ways depending on the |
| 331 | 331 | ** compiler. |
| | @@ -626,13 +626,13 @@ |
| 626 | 626 | ** |
| 627 | 627 | ** See also: [sqlite3_libversion()], |
| 628 | 628 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 629 | 629 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 630 | 630 | */ |
| 631 | | -#define SQLITE_VERSION "3.6.23.1" |
| 631 | +#define SQLITE_VERSION "3.6.23" |
| 632 | 632 | #define SQLITE_VERSION_NUMBER 3006023 |
| 633 | | -#define SQLITE_SOURCE_ID "2010-03-26 22:28:06 ex-b078b588d617e07886ad156e9f54ade6d823568e" |
| 633 | +#define SQLITE_SOURCE_ID "2010-04-15 23:24:29 f96782b389b5b97b488dc5814f7082e0393f64cd" |
| 634 | 634 | |
| 635 | 635 | /* |
| 636 | 636 | ** CAPI3REF: Run-Time Library Version Numbers |
| 637 | 637 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 638 | 638 | ** |
| | @@ -665,11 +665,10 @@ |
| 665 | 665 | SQLITE_API const char sqlite3_version[] = SQLITE_VERSION; |
| 666 | 666 | SQLITE_API const char *sqlite3_libversion(void); |
| 667 | 667 | SQLITE_API const char *sqlite3_sourceid(void); |
| 668 | 668 | SQLITE_API int sqlite3_libversion_number(void); |
| 669 | 669 | |
| 670 | | -#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS |
| 671 | 670 | /* |
| 672 | 671 | ** CAPI3REF: Run-Time Library Compilation Options Diagnostics |
| 673 | 672 | ** |
| 674 | 673 | ** ^The sqlite3_compileoption_used() function returns 0 or 1 |
| 675 | 674 | ** indicating whether the specified option was defined at |
| | @@ -688,13 +687,14 @@ |
| 688 | 687 | ** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time. |
| 689 | 688 | ** |
| 690 | 689 | ** See also: SQL functions [sqlite_compileoption_used()] and |
| 691 | 690 | ** [sqlite_compileoption_get()] and the [compile_options pragma]. |
| 692 | 691 | */ |
| 692 | +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS |
| 693 | 693 | SQLITE_API int sqlite3_compileoption_used(const char *zOptName); |
| 694 | 694 | SQLITE_API const char *sqlite3_compileoption_get(int N); |
| 695 | | -#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ |
| 695 | +#endif |
| 696 | 696 | |
| 697 | 697 | /* |
| 698 | 698 | ** CAPI3REF: Test To See If The Library Is Threadsafe |
| 699 | 699 | ** |
| 700 | 700 | ** ^The sqlite3_threadsafe() function returns zero if and only if |
| | @@ -1492,15 +1492,14 @@ |
| 1492 | 1492 | ** |
| 1493 | 1493 | ** ^When a configuration option is set, sqlite3_config() returns [SQLITE_OK]. |
| 1494 | 1494 | ** ^If the option is unknown or SQLite is unable to set the option |
| 1495 | 1495 | ** then this routine returns a non-zero [error code]. |
| 1496 | 1496 | */ |
| 1497 | | -SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_config(int, ...); |
| 1497 | +SQLITE_API int sqlite3_config(int, ...); |
| 1498 | 1498 | |
| 1499 | 1499 | /* |
| 1500 | 1500 | ** CAPI3REF: Configure database connections |
| 1501 | | -** EXPERIMENTAL |
| 1502 | 1501 | ** |
| 1503 | 1502 | ** The sqlite3_db_config() interface is used to make configuration |
| 1504 | 1503 | ** changes to a [database connection]. The interface is similar to |
| 1505 | 1504 | ** [sqlite3_config()] except that the changes apply to a single |
| 1506 | 1505 | ** [database connection] (specified in the first argument). The |
| | @@ -1516,15 +1515,14 @@ |
| 1516 | 1515 | ** Additional arguments depend on the verb. |
| 1517 | 1516 | ** |
| 1518 | 1517 | ** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if |
| 1519 | 1518 | ** the call is considered successful. |
| 1520 | 1519 | */ |
| 1521 | | -SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_db_config(sqlite3*, int op, ...); |
| 1520 | +SQLITE_API int sqlite3_db_config(sqlite3*, int op, ...); |
| 1522 | 1521 | |
| 1523 | 1522 | /* |
| 1524 | 1523 | ** CAPI3REF: Memory Allocation Routines |
| 1525 | | -** EXPERIMENTAL |
| 1526 | 1524 | ** |
| 1527 | 1525 | ** An instance of this object defines the interface between SQLite |
| 1528 | 1526 | ** and low-level memory allocation routines. |
| 1529 | 1527 | ** |
| 1530 | 1528 | ** This object is used in only one place in the SQLite interface. |
| | @@ -1602,11 +1600,10 @@ |
| 1602 | 1600 | void *pAppData; /* Argument to xInit() and xShutdown() */ |
| 1603 | 1601 | }; |
| 1604 | 1602 | |
| 1605 | 1603 | /* |
| 1606 | 1604 | ** CAPI3REF: Configuration Options |
| 1607 | | -** EXPERIMENTAL |
| 1608 | 1605 | ** |
| 1609 | 1606 | ** These constants are the available integer configuration options that |
| 1610 | 1607 | ** can be passed as the first argument to the [sqlite3_config()] interface. |
| 1611 | 1608 | ** |
| 1612 | 1609 | ** New configuration options may be added in future releases of SQLite. |
| | @@ -1788,10 +1785,28 @@ |
| 1788 | 1785 | ** <dt>SQLITE_CONFIG_GETPCACHE</dt> |
| 1789 | 1786 | ** <dd> ^(This option takes a single argument which is a pointer to an |
| 1790 | 1787 | ** [sqlite3_pcache_methods] object. SQLite copies of the current |
| 1791 | 1788 | ** page cache implementation into that object.)^ </dd> |
| 1792 | 1789 | ** |
| 1790 | +** <dt>SQLITE_CONFIG_LOG</dt> |
| 1791 | +** <dd> ^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a |
| 1792 | +** function with a call signature of void(*)(void*,int,const char*), |
| 1793 | +** and a pointer to void. ^If the function pointer is not NULL, it is |
| 1794 | +** invoked by [sqlite3_log()] to process each logging event. ^If the |
| 1795 | +** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op. |
| 1796 | +** ^The void pointer that is the second argument to SQLITE_CONFIG_LOG is |
| 1797 | +** passed through as the first parameter to the application-defined logger |
| 1798 | +** function whenever that function is invoked. ^The second parameter to |
| 1799 | +** the logger function is a copy of the first parameter to the corresponding |
| 1800 | +** [sqlite3_log()] call and is intended to be a [result code] or an |
| 1801 | +** [extended result code]. ^The third parameter passed to the logger is |
| 1802 | +** log message after formatting via [sqlite3_snprintf()]. |
| 1803 | +** The SQLite logging interface is not reentrant; the logger function |
| 1804 | +** supplied by the application must not invoke any SQLite interface. |
| 1805 | +** In a multi-threaded application, the application-defined logger |
| 1806 | +** function must be threadsafe. </dd> |
| 1807 | +** |
| 1793 | 1808 | ** </dl> |
| 1794 | 1809 | */ |
| 1795 | 1810 | #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */ |
| 1796 | 1811 | #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */ |
| 1797 | 1812 | #define SQLITE_CONFIG_SERIALIZED 3 /* nil */ |
| | @@ -1808,12 +1823,11 @@ |
| 1808 | 1823 | #define SQLITE_CONFIG_PCACHE 14 /* sqlite3_pcache_methods* */ |
| 1809 | 1824 | #define SQLITE_CONFIG_GETPCACHE 15 /* sqlite3_pcache_methods* */ |
| 1810 | 1825 | #define SQLITE_CONFIG_LOG 16 /* xFunc, void* */ |
| 1811 | 1826 | |
| 1812 | 1827 | /* |
| 1813 | | -** CAPI3REF: Configuration Options |
| 1814 | | -** EXPERIMENTAL |
| 1828 | +** CAPI3REF: Database Connection Configuration Options |
| 1815 | 1829 | ** |
| 1816 | 1830 | ** These constants are the available integer configuration options that |
| 1817 | 1831 | ** can be passed as the second argument to the [sqlite3_db_config()] interface. |
| 1818 | 1832 | ** |
| 1819 | 1833 | ** New configuration options may be added in future releases of SQLite. |
| | @@ -2585,11 +2599,10 @@ |
| 2585 | 2599 | #define SQLITE_SAVEPOINT 32 /* Operation Savepoint Name */ |
| 2586 | 2600 | #define SQLITE_COPY 0 /* No longer used */ |
| 2587 | 2601 | |
| 2588 | 2602 | /* |
| 2589 | 2603 | ** CAPI3REF: Tracing And Profiling Functions |
| 2590 | | -** EXPERIMENTAL |
| 2591 | 2604 | ** |
| 2592 | 2605 | ** These routines register callback functions that can be used for |
| 2593 | 2606 | ** tracing and profiling the execution of SQL statements. |
| 2594 | 2607 | ** |
| 2595 | 2608 | ** ^The callback function registered by sqlite3_trace() is invoked at |
| | @@ -2603,11 +2616,11 @@ |
| 2603 | 2616 | ** ^The callback function registered by sqlite3_profile() is invoked |
| 2604 | 2617 | ** as each SQL statement finishes. ^The profile callback contains |
| 2605 | 2618 | ** the original statement text and an estimate of wall-clock time |
| 2606 | 2619 | ** of how long that statement took to run. |
| 2607 | 2620 | */ |
| 2608 | | -SQLITE_API SQLITE_EXPERIMENTAL void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*); |
| 2621 | +SQLITE_API void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*); |
| 2609 | 2622 | SQLITE_API SQLITE_EXPERIMENTAL void *sqlite3_profile(sqlite3*, |
| 2610 | 2623 | void(*xProfile)(void*,const char*,sqlite3_uint64), void*); |
| 2611 | 2624 | |
| 2612 | 2625 | /* |
| 2613 | 2626 | ** CAPI3REF: Query Progress Callbacks |
| | @@ -4208,11 +4221,11 @@ |
| 4208 | 4221 | sqlite3*, |
| 4209 | 4222 | void*, |
| 4210 | 4223 | void(*)(void*,sqlite3*,int eTextRep,const void*) |
| 4211 | 4224 | ); |
| 4212 | 4225 | |
| 4213 | | -#if SQLITE_HAS_CODEC |
| 4226 | +#ifdef SQLITE_HAS_CODEC |
| 4214 | 4227 | /* |
| 4215 | 4228 | ** Specify the key for an encrypted database. This routine should be |
| 4216 | 4229 | ** called right after sqlite3_open(). |
| 4217 | 4230 | ** |
| 4218 | 4231 | ** The code to implement this API is not available in the public release |
| | @@ -4391,12 +4404,10 @@ |
| 4391 | 4404 | ** ^For the purposes of this API, a transaction is said to have been |
| 4392 | 4405 | ** rolled back if an explicit "ROLLBACK" statement is executed, or |
| 4393 | 4406 | ** an error or constraint causes an implicit rollback to occur. |
| 4394 | 4407 | ** ^The rollback callback is not invoked if a transaction is |
| 4395 | 4408 | ** automatically rolled back because the database connection is closed. |
| 4396 | | -** ^The rollback callback is not invoked if a transaction is |
| 4397 | | -** rolled back because a commit callback returned non-zero. |
| 4398 | 4409 | ** |
| 4399 | 4410 | ** See also the [sqlite3_update_hook()] interface. |
| 4400 | 4411 | */ |
| 4401 | 4412 | SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*); |
| 4402 | 4413 | SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*); |
| | @@ -4678,12 +4689,10 @@ |
| 4678 | 4689 | ** ^This function disables automatic extensions in all threads. |
| 4679 | 4690 | */ |
| 4680 | 4691 | SQLITE_API void sqlite3_reset_auto_extension(void); |
| 4681 | 4692 | |
| 4682 | 4693 | /* |
| 4683 | | -****** EXPERIMENTAL - subject to change without notice ************** |
| 4684 | | -** |
| 4685 | 4694 | ** The interface to the virtual-table mechanism is currently considered |
| 4686 | 4695 | ** to be experimental. The interface might change in incompatible ways. |
| 4687 | 4696 | ** If this is a problem for you, do not use the interface at this time. |
| 4688 | 4697 | ** |
| 4689 | 4698 | ** When the virtual-table mechanism stabilizes, we will declare the |
| | @@ -4699,11 +4708,10 @@ |
| 4699 | 4708 | typedef struct sqlite3_module sqlite3_module; |
| 4700 | 4709 | |
| 4701 | 4710 | /* |
| 4702 | 4711 | ** CAPI3REF: Virtual Table Object |
| 4703 | 4712 | ** KEYWORDS: sqlite3_module {virtual table module} |
| 4704 | | -** EXPERIMENTAL |
| 4705 | 4713 | ** |
| 4706 | 4714 | ** This structure, sometimes called a a "virtual table module", |
| 4707 | 4715 | ** defines the implementation of a [virtual tables]. |
| 4708 | 4716 | ** This structure consists mostly of methods for the module. |
| 4709 | 4717 | ** |
| | @@ -4746,11 +4754,10 @@ |
| 4746 | 4754 | }; |
| 4747 | 4755 | |
| 4748 | 4756 | /* |
| 4749 | 4757 | ** CAPI3REF: Virtual Table Indexing Information |
| 4750 | 4758 | ** KEYWORDS: sqlite3_index_info |
| 4751 | | -** EXPERIMENTAL |
| 4752 | 4759 | ** |
| 4753 | 4760 | ** The sqlite3_index_info structure and its substructures is used to |
| 4754 | 4761 | ** pass information into and receive the reply from the [xBestIndex] |
| 4755 | 4762 | ** method of a [virtual table module]. The fields under **Inputs** are the |
| 4756 | 4763 | ** inputs to xBestIndex and are read-only. xBestIndex inserts its |
| | @@ -4828,11 +4835,10 @@ |
| 4828 | 4835 | #define SQLITE_INDEX_CONSTRAINT_GE 32 |
| 4829 | 4836 | #define SQLITE_INDEX_CONSTRAINT_MATCH 64 |
| 4830 | 4837 | |
| 4831 | 4838 | /* |
| 4832 | 4839 | ** CAPI3REF: Register A Virtual Table Implementation |
| 4833 | | -** EXPERIMENTAL |
| 4834 | 4840 | ** |
| 4835 | 4841 | ** ^These routines are used to register a new [virtual table module] name. |
| 4836 | 4842 | ** ^Module names must be registered before |
| 4837 | 4843 | ** creating a new [virtual table] using the module and before using a |
| 4838 | 4844 | ** preexisting [virtual table] for the module. |
| | @@ -4850,17 +4856,17 @@ |
| 4850 | 4856 | ** invoke the destructor function (if it is not NULL) when SQLite |
| 4851 | 4857 | ** no longer needs the pClientData pointer. ^The sqlite3_create_module() |
| 4852 | 4858 | ** interface is equivalent to sqlite3_create_module_v2() with a NULL |
| 4853 | 4859 | ** destructor. |
| 4854 | 4860 | */ |
| 4855 | | -SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_create_module( |
| 4861 | +SQLITE_API int sqlite3_create_module( |
| 4856 | 4862 | sqlite3 *db, /* SQLite connection to register module with */ |
| 4857 | 4863 | const char *zName, /* Name of the module */ |
| 4858 | 4864 | const sqlite3_module *p, /* Methods for the module */ |
| 4859 | 4865 | void *pClientData /* Client data for xCreate/xConnect */ |
| 4860 | 4866 | ); |
| 4861 | | -SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_create_module_v2( |
| 4867 | +SQLITE_API int sqlite3_create_module_v2( |
| 4862 | 4868 | sqlite3 *db, /* SQLite connection to register module with */ |
| 4863 | 4869 | const char *zName, /* Name of the module */ |
| 4864 | 4870 | const sqlite3_module *p, /* Methods for the module */ |
| 4865 | 4871 | void *pClientData, /* Client data for xCreate/xConnect */ |
| 4866 | 4872 | void(*xDestroy)(void*) /* Module destructor function */ |
| | @@ -4867,11 +4873,10 @@ |
| 4867 | 4873 | ); |
| 4868 | 4874 | |
| 4869 | 4875 | /* |
| 4870 | 4876 | ** CAPI3REF: Virtual Table Instance Object |
| 4871 | 4877 | ** KEYWORDS: sqlite3_vtab |
| 4872 | | -** EXPERIMENTAL |
| 4873 | 4878 | ** |
| 4874 | 4879 | ** Every [virtual table module] implementation uses a subclass |
| 4875 | 4880 | ** of this object to describe a particular instance |
| 4876 | 4881 | ** of the [virtual table]. Each subclass will |
| 4877 | 4882 | ** be tailored to the specific needs of the module implementation. |
| | @@ -4893,11 +4898,10 @@ |
| 4893 | 4898 | }; |
| 4894 | 4899 | |
| 4895 | 4900 | /* |
| 4896 | 4901 | ** CAPI3REF: Virtual Table Cursor Object |
| 4897 | 4902 | ** KEYWORDS: sqlite3_vtab_cursor {virtual table cursor} |
| 4898 | | -** EXPERIMENTAL |
| 4899 | 4903 | ** |
| 4900 | 4904 | ** Every [virtual table module] implementation uses a subclass of the |
| 4901 | 4905 | ** following structure to describe cursors that point into the |
| 4902 | 4906 | ** [virtual table] and are used |
| 4903 | 4907 | ** to loop through the virtual table. Cursors are created using the |
| | @@ -4915,22 +4919,20 @@ |
| 4915 | 4919 | /* Virtual table implementations will typically add additional fields */ |
| 4916 | 4920 | }; |
| 4917 | 4921 | |
| 4918 | 4922 | /* |
| 4919 | 4923 | ** CAPI3REF: Declare The Schema Of A Virtual Table |
| 4920 | | -** EXPERIMENTAL |
| 4921 | 4924 | ** |
| 4922 | 4925 | ** ^The [xCreate] and [xConnect] methods of a |
| 4923 | 4926 | ** [virtual table module] call this interface |
| 4924 | 4927 | ** to declare the format (the names and datatypes of the columns) of |
| 4925 | 4928 | ** the virtual tables they implement. |
| 4926 | 4929 | */ |
| 4927 | | -SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_declare_vtab(sqlite3*, const char *zSQL); |
| 4930 | +SQLITE_API int sqlite3_declare_vtab(sqlite3*, const char *zSQL); |
| 4928 | 4931 | |
| 4929 | 4932 | /* |
| 4930 | 4933 | ** CAPI3REF: Overload A Function For A Virtual Table |
| 4931 | | -** EXPERIMENTAL |
| 4932 | 4934 | ** |
| 4933 | 4935 | ** ^(Virtual tables can provide alternative implementations of functions |
| 4934 | 4936 | ** using the [xFindFunction] method of the [virtual table module]. |
| 4935 | 4937 | ** But global versions of those functions |
| 4936 | 4938 | ** must exist in order to be overloaded.)^ |
| | @@ -4941,22 +4943,20 @@ |
| 4941 | 4943 | ** of the new function always causes an exception to be thrown. So |
| 4942 | 4944 | ** the new function is not good for anything by itself. Its only |
| 4943 | 4945 | ** purpose is to be a placeholder function that can be overloaded |
| 4944 | 4946 | ** by a [virtual table]. |
| 4945 | 4947 | */ |
| 4946 | | -SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg); |
| 4948 | +SQLITE_API int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg); |
| 4947 | 4949 | |
| 4948 | 4950 | /* |
| 4949 | 4951 | ** The interface to the virtual-table mechanism defined above (back up |
| 4950 | 4952 | ** to a comment remarkably similar to this one) is currently considered |
| 4951 | 4953 | ** to be experimental. The interface might change in incompatible ways. |
| 4952 | 4954 | ** If this is a problem for you, do not use the interface at this time. |
| 4953 | 4955 | ** |
| 4954 | 4956 | ** When the virtual-table mechanism stabilizes, we will declare the |
| 4955 | 4957 | ** interface fixed, support it indefinitely, and remove this comment. |
| 4956 | | -** |
| 4957 | | -****** EXPERIMENTAL - subject to change without notice ************** |
| 4958 | 4958 | */ |
| 4959 | 4959 | |
| 4960 | 4960 | /* |
| 4961 | 4961 | ** CAPI3REF: A Handle To An Open BLOB |
| 4962 | 4962 | ** KEYWORDS: {BLOB handle} {BLOB handles} |
| | @@ -5295,11 +5295,10 @@ |
| 5295 | 5295 | SQLITE_API int sqlite3_mutex_try(sqlite3_mutex*); |
| 5296 | 5296 | SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex*); |
| 5297 | 5297 | |
| 5298 | 5298 | /* |
| 5299 | 5299 | ** CAPI3REF: Mutex Methods Object |
| 5300 | | -** EXPERIMENTAL |
| 5301 | 5300 | ** |
| 5302 | 5301 | ** An instance of this structure defines the low-level routines |
| 5303 | 5302 | ** used to allocate and use mutexes. |
| 5304 | 5303 | ** |
| 5305 | 5304 | ** Usually, the default mutex implementations provided by SQLite are |
| | @@ -5512,11 +5511,10 @@ |
| 5512 | 5511 | #define SQLITE_TESTCTRL_ISKEYWORD 16 |
| 5513 | 5512 | #define SQLITE_TESTCTRL_LAST 16 |
| 5514 | 5513 | |
| 5515 | 5514 | /* |
| 5516 | 5515 | ** CAPI3REF: SQLite Runtime Status |
| 5517 | | -** EXPERIMENTAL |
| 5518 | 5516 | ** |
| 5519 | 5517 | ** ^This interface is used to retrieve runtime status information |
| 5520 | 5518 | ** about the preformance of SQLite, and optionally to reset various |
| 5521 | 5519 | ** highwater marks. ^The first argument is an integer code for |
| 5522 | 5520 | ** the specific parameter to measure. ^(Recognized integer codes |
| | @@ -5540,16 +5538,15 @@ |
| 5540 | 5538 | ** and it is possible that another thread might change the parameter |
| 5541 | 5539 | ** in between the times when *pCurrent and *pHighwater are written. |
| 5542 | 5540 | ** |
| 5543 | 5541 | ** See also: [sqlite3_db_status()] |
| 5544 | 5542 | */ |
| 5545 | | -SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag); |
| 5543 | +SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag); |
| 5546 | 5544 | |
| 5547 | 5545 | |
| 5548 | 5546 | /* |
| 5549 | 5547 | ** CAPI3REF: Status Parameters |
| 5550 | | -** EXPERIMENTAL |
| 5551 | 5548 | ** |
| 5552 | 5549 | ** These integer constants designate various run-time status parameters |
| 5553 | 5550 | ** that can be returned by [sqlite3_status()]. |
| 5554 | 5551 | ** |
| 5555 | 5552 | ** <dl> |
| | @@ -5632,31 +5629,31 @@ |
| 5632 | 5629 | #define SQLITE_STATUS_PAGECACHE_SIZE 7 |
| 5633 | 5630 | #define SQLITE_STATUS_SCRATCH_SIZE 8 |
| 5634 | 5631 | |
| 5635 | 5632 | /* |
| 5636 | 5633 | ** CAPI3REF: Database Connection Status |
| 5637 | | -** EXPERIMENTAL |
| 5638 | 5634 | ** |
| 5639 | 5635 | ** ^This interface is used to retrieve runtime status information |
| 5640 | 5636 | ** about a single [database connection]. ^The first argument is the |
| 5641 | 5637 | ** database connection object to be interrogated. ^The second argument |
| 5642 | | -** is the parameter to interrogate. ^Currently, the only allowed value |
| 5643 | | -** for the second parameter is [SQLITE_DBSTATUS_LOOKASIDE_USED]. |
| 5644 | | -** Additional options will likely appear in future releases of SQLite. |
| 5638 | +** is an integer constant, taken from the set of |
| 5639 | +** [SQLITE_DBSTATUS_LOOKASIDE_USED | SQLITE_DBSTATUS_*] macros, that |
| 5640 | +** determiness the parameter to interrogate. The set of |
| 5641 | +** [SQLITE_DBSTATUS_LOOKASIDE_USED | SQLITE_DBSTATUS_*] macros is likely |
| 5642 | +** to grow in future releases of SQLite. |
| 5645 | 5643 | ** |
| 5646 | 5644 | ** ^The current value of the requested parameter is written into *pCur |
| 5647 | 5645 | ** and the highest instantaneous value is written into *pHiwtr. ^If |
| 5648 | 5646 | ** the resetFlg is true, then the highest instantaneous value is |
| 5649 | 5647 | ** reset back down to the current value. |
| 5650 | 5648 | ** |
| 5651 | 5649 | ** See also: [sqlite3_status()] and [sqlite3_stmt_status()]. |
| 5652 | 5650 | */ |
| 5653 | | -SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg); |
| 5651 | +SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg); |
| 5654 | 5652 | |
| 5655 | 5653 | /* |
| 5656 | 5654 | ** CAPI3REF: Status Parameters for database connections |
| 5657 | | -** EXPERIMENTAL |
| 5658 | 5655 | ** |
| 5659 | 5656 | ** These constants are the available integer "verbs" that can be passed as |
| 5660 | 5657 | ** the second argument to the [sqlite3_db_status()] interface. |
| 5661 | 5658 | ** |
| 5662 | 5659 | ** New verbs may be added in future releases of SQLite. Existing verbs |
| | @@ -5667,18 +5664,25 @@ |
| 5667 | 5664 | ** |
| 5668 | 5665 | ** <dl> |
| 5669 | 5666 | ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_USED</dt> |
| 5670 | 5667 | ** <dd>This parameter returns the number of lookaside memory slots currently |
| 5671 | 5668 | ** checked out.</dd>)^ |
| 5669 | +** |
| 5670 | +** <dt>SQLITE_DBSTATUS_CACHE_USED</dt> |
| 5671 | +** <dd>^This parameter returns the approximate number of of bytes of heap |
| 5672 | +** memory used by all pager caches associated with the database connection. |
| 5673 | +** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0. |
| 5674 | +** checked out.</dd>)^ |
| 5672 | 5675 | ** </dl> |
| 5673 | 5676 | */ |
| 5674 | 5677 | #define SQLITE_DBSTATUS_LOOKASIDE_USED 0 |
| 5678 | +#define SQLITE_DBSTATUS_CACHE_USED 1 |
| 5679 | +#define SQLITE_DBSTATUS_MAX 1 /* Largest defined DBSTATUS */ |
| 5675 | 5680 | |
| 5676 | 5681 | |
| 5677 | 5682 | /* |
| 5678 | 5683 | ** CAPI3REF: Prepared Statement Status |
| 5679 | | -** EXPERIMENTAL |
| 5680 | 5684 | ** |
| 5681 | 5685 | ** ^(Each prepared statement maintains various |
| 5682 | 5686 | ** [SQLITE_STMTSTATUS_SORT | counters] that measure the number |
| 5683 | 5687 | ** of times it has performed specific operations.)^ These counters can |
| 5684 | 5688 | ** be used to monitor the performance characteristics of the prepared |
| | @@ -5696,15 +5700,14 @@ |
| 5696 | 5700 | ** ^If the resetFlg is true, then the counter is reset to zero after this |
| 5697 | 5701 | ** interface call returns. |
| 5698 | 5702 | ** |
| 5699 | 5703 | ** See also: [sqlite3_status()] and [sqlite3_db_status()]. |
| 5700 | 5704 | */ |
| 5701 | | -SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg); |
| 5705 | +SQLITE_API int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg); |
| 5702 | 5706 | |
| 5703 | 5707 | /* |
| 5704 | 5708 | ** CAPI3REF: Status Parameters for prepared statements |
| 5705 | | -** EXPERIMENTAL |
| 5706 | 5709 | ** |
| 5707 | 5710 | ** These preprocessor macros define integer codes that name counter |
| 5708 | 5711 | ** values associated with the [sqlite3_stmt_status()] interface. |
| 5709 | 5712 | ** The meanings of the various counters are as follows: |
| 5710 | 5713 | ** |
| | @@ -5718,18 +5721,25 @@ |
| 5718 | 5721 | ** <dt>SQLITE_STMTSTATUS_SORT</dt> |
| 5719 | 5722 | ** <dd>^This is the number of sort operations that have occurred. |
| 5720 | 5723 | ** A non-zero value in this counter may indicate an opportunity to |
| 5721 | 5724 | ** improvement performance through careful use of indices.</dd> |
| 5722 | 5725 | ** |
| 5726 | +** <dt>SQLITE_STMTSTATUS_AUTOINDEX</dt> |
| 5727 | +** <dd>^This is the number of rows inserted into transient indices that |
| 5728 | +** were created automatically in order to help joins run faster. |
| 5729 | +** A non-zero value in this counter may indicate an opportunity to |
| 5730 | +** improvement performance by adding permanent indices that do not |
| 5731 | +** need to be reinitialized each time the statement is run.</dd> |
| 5732 | +** |
| 5723 | 5733 | ** </dl> |
| 5724 | 5734 | */ |
| 5725 | 5735 | #define SQLITE_STMTSTATUS_FULLSCAN_STEP 1 |
| 5726 | 5736 | #define SQLITE_STMTSTATUS_SORT 2 |
| 5737 | +#define SQLITE_STMTSTATUS_AUTOINDEX 3 |
| 5727 | 5738 | |
| 5728 | 5739 | /* |
| 5729 | 5740 | ** CAPI3REF: Custom Page Cache Object |
| 5730 | | -** EXPERIMENTAL |
| 5731 | 5741 | ** |
| 5732 | 5742 | ** The sqlite3_pcache type is opaque. It is implemented by |
| 5733 | 5743 | ** the pluggable module. The SQLite core has no knowledge of |
| 5734 | 5744 | ** its size or internal structure and never deals with the |
| 5735 | 5745 | ** sqlite3_pcache object except by holding and passing pointers |
| | @@ -5740,11 +5750,10 @@ |
| 5740 | 5750 | typedef struct sqlite3_pcache sqlite3_pcache; |
| 5741 | 5751 | |
| 5742 | 5752 | /* |
| 5743 | 5753 | ** CAPI3REF: Application Defined Page Cache. |
| 5744 | 5754 | ** KEYWORDS: {page cache} |
| 5745 | | -** EXPERIMENTAL |
| 5746 | 5755 | ** |
| 5747 | 5756 | ** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE], ...) interface can |
| 5748 | 5757 | ** register an alternative page cache implementation by passing in an |
| 5749 | 5758 | ** instance of the sqlite3_pcache_methods structure.)^ The majority of the |
| 5750 | 5759 | ** heap memory used by SQLite is used by the page cache to cache data read |
| | @@ -5882,11 +5891,10 @@ |
| 5882 | 5891 | void (*xDestroy)(sqlite3_pcache*); |
| 5883 | 5892 | }; |
| 5884 | 5893 | |
| 5885 | 5894 | /* |
| 5886 | 5895 | ** CAPI3REF: Online Backup Object |
| 5887 | | -** EXPERIMENTAL |
| 5888 | 5896 | ** |
| 5889 | 5897 | ** The sqlite3_backup object records state information about an ongoing |
| 5890 | 5898 | ** online backup operation. ^The sqlite3_backup object is created by |
| 5891 | 5899 | ** a call to [sqlite3_backup_init()] and is destroyed by a call to |
| 5892 | 5900 | ** [sqlite3_backup_finish()]. |
| | @@ -5895,11 +5903,10 @@ |
| 5895 | 5903 | */ |
| 5896 | 5904 | typedef struct sqlite3_backup sqlite3_backup; |
| 5897 | 5905 | |
| 5898 | 5906 | /* |
| 5899 | 5907 | ** CAPI3REF: Online Backup API. |
| 5900 | | -** EXPERIMENTAL |
| 5901 | 5908 | ** |
| 5902 | 5909 | ** The backup API copies the content of one database into another. |
| 5903 | 5910 | ** It is useful either for creating backups of databases or |
| 5904 | 5911 | ** for copying in-memory databases to or from persistent files. |
| 5905 | 5912 | ** |
| | @@ -6083,11 +6090,10 @@ |
| 6083 | 6090 | SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p); |
| 6084 | 6091 | SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p); |
| 6085 | 6092 | |
| 6086 | 6093 | /* |
| 6087 | 6094 | ** CAPI3REF: Unlock Notification |
| 6088 | | -** EXPERIMENTAL |
| 6089 | 6095 | ** |
| 6090 | 6096 | ** ^When running in shared-cache mode, a database operation may fail with |
| 6091 | 6097 | ** an [SQLITE_LOCKED] error if the required locks on the shared-cache or |
| 6092 | 6098 | ** individual tables within the shared-cache cannot be obtained. See |
| 6093 | 6099 | ** [SQLite Shared-Cache Mode] for a description of shared-cache locking. |
| | @@ -6205,11 +6211,10 @@ |
| 6205 | 6211 | ); |
| 6206 | 6212 | |
| 6207 | 6213 | |
| 6208 | 6214 | /* |
| 6209 | 6215 | ** CAPI3REF: String Comparison |
| 6210 | | -** EXPERIMENTAL |
| 6211 | 6216 | ** |
| 6212 | 6217 | ** ^The [sqlite3_strnicmp()] API allows applications and extensions to |
| 6213 | 6218 | ** compare the contents of two buffers containing UTF-8 strings in a |
| 6214 | 6219 | ** case-indendent fashion, using the same definition of case independence |
| 6215 | 6220 | ** that SQLite uses internally when comparing identifiers. |
| | @@ -6216,16 +6221,15 @@ |
| 6216 | 6221 | */ |
| 6217 | 6222 | SQLITE_API int sqlite3_strnicmp(const char *, const char *, int); |
| 6218 | 6223 | |
| 6219 | 6224 | /* |
| 6220 | 6225 | ** CAPI3REF: Error Logging Interface |
| 6221 | | -** EXPERIMENTAL |
| 6222 | 6226 | ** |
| 6223 | 6227 | ** ^The [sqlite3_log()] interface writes a message into the error log |
| 6224 | 6228 | ** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()]. |
| 6225 | 6229 | ** ^If logging is enabled, the zFormat string and subsequent arguments are |
| 6226 | | -** passed through to [sqlite3_vmprintf()] to generate the final output string. |
| 6230 | +** used with [sqlite3_snprintf()] to generate the final output string. |
| 6227 | 6231 | ** |
| 6228 | 6232 | ** The sqlite3_log() interface is intended for use by extensions such as |
| 6229 | 6233 | ** virtual tables, collating functions, and SQL functions. While there is |
| 6230 | 6234 | ** nothing to prevent an application from calling sqlite3_log(), doing so |
| 6231 | 6235 | ** is considered bad form. |
| | @@ -6529,10 +6533,11 @@ |
| 6529 | 6533 | ** If compiling for a processor that lacks floating point support, |
| 6530 | 6534 | ** substitute integer for floating-point |
| 6531 | 6535 | */ |
| 6532 | 6536 | #ifdef SQLITE_OMIT_FLOATING_POINT |
| 6533 | 6537 | # define double sqlite_int64 |
| 6538 | +# define float sqlite_int64 |
| 6534 | 6539 | # define LONGDOUBLE_TYPE sqlite_int64 |
| 6535 | 6540 | # ifndef SQLITE_BIG_DBL |
| 6536 | 6541 | # define SQLITE_BIG_DBL (((sqlite3_int64)1)<<50) |
| 6537 | 6542 | # endif |
| 6538 | 6543 | # define SQLITE_OMIT_DATETIME_FUNCS 1 |
| | @@ -6940,10 +6945,11 @@ |
| 6940 | 6945 | SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(Btree*,int,int); |
| 6941 | 6946 | SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*); |
| 6942 | 6947 | SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix); |
| 6943 | 6948 | SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*); |
| 6944 | 6949 | SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int); |
| 6950 | +SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree*); |
| 6945 | 6951 | SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree*,int); |
| 6946 | 6952 | SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree*); |
| 6947 | 6953 | SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int); |
| 6948 | 6954 | SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *); |
| 6949 | 6955 | SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int); |
| | @@ -7334,85 +7340,85 @@ |
| 7334 | 7340 | #define OP_ReadCookie 35 |
| 7335 | 7341 | #define OP_SetCookie 36 |
| 7336 | 7342 | #define OP_VerifyCookie 37 |
| 7337 | 7343 | #define OP_OpenRead 38 |
| 7338 | 7344 | #define OP_OpenWrite 39 |
| 7339 | | -#define OP_OpenEphemeral 40 |
| 7340 | | -#define OP_OpenPseudo 41 |
| 7341 | | -#define OP_Close 42 |
| 7342 | | -#define OP_SeekLt 43 |
| 7343 | | -#define OP_SeekLe 44 |
| 7344 | | -#define OP_SeekGe 45 |
| 7345 | | -#define OP_SeekGt 46 |
| 7346 | | -#define OP_Seek 47 |
| 7347 | | -#define OP_NotFound 48 |
| 7348 | | -#define OP_Found 49 |
| 7349 | | -#define OP_IsUnique 50 |
| 7350 | | -#define OP_NotExists 51 |
| 7351 | | -#define OP_Sequence 52 |
| 7352 | | -#define OP_NewRowid 53 |
| 7353 | | -#define OP_Insert 54 |
| 7354 | | -#define OP_InsertInt 55 |
| 7355 | | -#define OP_Delete 56 |
| 7356 | | -#define OP_ResetCount 57 |
| 7357 | | -#define OP_RowKey 58 |
| 7358 | | -#define OP_RowData 59 |
| 7359 | | -#define OP_Rowid 60 |
| 7360 | | -#define OP_NullRow 61 |
| 7361 | | -#define OP_Last 62 |
| 7362 | | -#define OP_Sort 63 |
| 7363 | | -#define OP_Rewind 64 |
| 7364 | | -#define OP_Prev 65 |
| 7365 | | -#define OP_Next 66 |
| 7366 | | -#define OP_IdxInsert 67 |
| 7367 | | -#define OP_IdxDelete 70 |
| 7368 | | -#define OP_IdxRowid 71 |
| 7369 | | -#define OP_IdxLT 72 |
| 7370 | | -#define OP_IdxGE 81 |
| 7371 | | -#define OP_Destroy 92 |
| 7372 | | -#define OP_Clear 95 |
| 7373 | | -#define OP_CreateIndex 96 |
| 7374 | | -#define OP_CreateTable 97 |
| 7375 | | -#define OP_ParseSchema 98 |
| 7376 | | -#define OP_LoadAnalysis 99 |
| 7377 | | -#define OP_DropTable 100 |
| 7378 | | -#define OP_DropIndex 101 |
| 7379 | | -#define OP_DropTrigger 102 |
| 7380 | | -#define OP_IntegrityCk 103 |
| 7381 | | -#define OP_RowSetAdd 104 |
| 7382 | | -#define OP_RowSetRead 105 |
| 7383 | | -#define OP_RowSetTest 106 |
| 7384 | | -#define OP_Program 107 |
| 7385 | | -#define OP_Param 108 |
| 7386 | | -#define OP_FkCounter 109 |
| 7387 | | -#define OP_FkIfZero 110 |
| 7388 | | -#define OP_MemMax 111 |
| 7389 | | -#define OP_IfPos 112 |
| 7390 | | -#define OP_IfNeg 113 |
| 7391 | | -#define OP_IfZero 114 |
| 7392 | | -#define OP_AggStep 115 |
| 7393 | | -#define OP_AggFinal 116 |
| 7394 | | -#define OP_Vacuum 117 |
| 7395 | | -#define OP_IncrVacuum 118 |
| 7396 | | -#define OP_Expire 119 |
| 7397 | | -#define OP_TableLock 120 |
| 7398 | | -#define OP_VBegin 121 |
| 7399 | | -#define OP_VCreate 122 |
| 7400 | | -#define OP_VDestroy 123 |
| 7401 | | -#define OP_VOpen 124 |
| 7402 | | -#define OP_VFilter 125 |
| 7403 | | -#define OP_VColumn 126 |
| 7404 | | -#define OP_VNext 127 |
| 7405 | | -#define OP_VRename 128 |
| 7406 | | -#define OP_VUpdate 129 |
| 7407 | | -#define OP_Pagecount 131 |
| 7408 | | -#define OP_Trace 132 |
| 7409 | | -#define OP_Noop 133 |
| 7410 | | -#define OP_Explain 134 |
| 7411 | | - |
| 7412 | | -/* The following opcode values are never used */ |
| 7413 | | -#define OP_NotUsed_135 135 |
| 7345 | +#define OP_OpenAutoindex 40 |
| 7346 | +#define OP_OpenEphemeral 41 |
| 7347 | +#define OP_OpenPseudo 42 |
| 7348 | +#define OP_Close 43 |
| 7349 | +#define OP_SeekLt 44 |
| 7350 | +#define OP_SeekLe 45 |
| 7351 | +#define OP_SeekGe 46 |
| 7352 | +#define OP_SeekGt 47 |
| 7353 | +#define OP_Seek 48 |
| 7354 | +#define OP_NotFound 49 |
| 7355 | +#define OP_Found 50 |
| 7356 | +#define OP_IsUnique 51 |
| 7357 | +#define OP_NotExists 52 |
| 7358 | +#define OP_Sequence 53 |
| 7359 | +#define OP_NewRowid 54 |
| 7360 | +#define OP_Insert 55 |
| 7361 | +#define OP_InsertInt 56 |
| 7362 | +#define OP_Delete 57 |
| 7363 | +#define OP_ResetCount 58 |
| 7364 | +#define OP_RowKey 59 |
| 7365 | +#define OP_RowData 60 |
| 7366 | +#define OP_Rowid 61 |
| 7367 | +#define OP_NullRow 62 |
| 7368 | +#define OP_Last 63 |
| 7369 | +#define OP_Sort 64 |
| 7370 | +#define OP_Rewind 65 |
| 7371 | +#define OP_Prev 66 |
| 7372 | +#define OP_Next 67 |
| 7373 | +#define OP_IdxInsert 70 |
| 7374 | +#define OP_IdxDelete 71 |
| 7375 | +#define OP_IdxRowid 72 |
| 7376 | +#define OP_IdxLT 81 |
| 7377 | +#define OP_IdxGE 92 |
| 7378 | +#define OP_Destroy 95 |
| 7379 | +#define OP_Clear 96 |
| 7380 | +#define OP_CreateIndex 97 |
| 7381 | +#define OP_CreateTable 98 |
| 7382 | +#define OP_ParseSchema 99 |
| 7383 | +#define OP_LoadAnalysis 100 |
| 7384 | +#define OP_DropTable 101 |
| 7385 | +#define OP_DropIndex 102 |
| 7386 | +#define OP_DropTrigger 103 |
| 7387 | +#define OP_IntegrityCk 104 |
| 7388 | +#define OP_RowSetAdd 105 |
| 7389 | +#define OP_RowSetRead 106 |
| 7390 | +#define OP_RowSetTest 107 |
| 7391 | +#define OP_Program 108 |
| 7392 | +#define OP_Param 109 |
| 7393 | +#define OP_FkCounter 110 |
| 7394 | +#define OP_FkIfZero 111 |
| 7395 | +#define OP_MemMax 112 |
| 7396 | +#define OP_IfPos 113 |
| 7397 | +#define OP_IfNeg 114 |
| 7398 | +#define OP_IfZero 115 |
| 7399 | +#define OP_AggStep 116 |
| 7400 | +#define OP_AggFinal 117 |
| 7401 | +#define OP_Vacuum 118 |
| 7402 | +#define OP_IncrVacuum 119 |
| 7403 | +#define OP_Expire 120 |
| 7404 | +#define OP_TableLock 121 |
| 7405 | +#define OP_VBegin 122 |
| 7406 | +#define OP_VCreate 123 |
| 7407 | +#define OP_VDestroy 124 |
| 7408 | +#define OP_VOpen 125 |
| 7409 | +#define OP_VFilter 126 |
| 7410 | +#define OP_VColumn 127 |
| 7411 | +#define OP_VNext 128 |
| 7412 | +#define OP_VRename 129 |
| 7413 | +#define OP_VUpdate 131 |
| 7414 | +#define OP_Pagecount 132 |
| 7415 | +#define OP_Trace 133 |
| 7416 | +#define OP_Noop 134 |
| 7417 | +#define OP_Explain 135 |
| 7418 | + |
| 7419 | +/* The following opcode values are never used */ |
| 7414 | 7420 | #define OP_NotUsed_136 136 |
| 7415 | 7421 | #define OP_NotUsed_137 137 |
| 7416 | 7422 | #define OP_NotUsed_138 138 |
| 7417 | 7423 | #define OP_NotUsed_139 139 |
| 7418 | 7424 | #define OP_NotUsed_140 140 |
| | @@ -7433,22 +7439,22 @@ |
| 7433 | 7439 | /* 0 */ 0x00, 0x01, 0x05, 0x04, 0x04, 0x10, 0x00, 0x02,\ |
| 7434 | 7440 | /* 8 */ 0x02, 0x02, 0x02, 0x02, 0x00, 0x00, 0x24, 0x24,\ |
| 7435 | 7441 | /* 16 */ 0x00, 0x00, 0x00, 0x24, 0x04, 0x05, 0x04, 0x00,\ |
| 7436 | 7442 | /* 24 */ 0x00, 0x01, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02,\ |
| 7437 | 7443 | /* 32 */ 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00, 0x00,\ |
| 7438 | | -/* 40 */ 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x08,\ |
| 7439 | | -/* 48 */ 0x11, 0x11, 0x11, 0x11, 0x02, 0x02, 0x00, 0x00,\ |
| 7440 | | -/* 56 */ 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01, 0x01,\ |
| 7441 | | -/* 64 */ 0x01, 0x01, 0x01, 0x08, 0x4c, 0x4c, 0x00, 0x02,\ |
| 7442 | | -/* 72 */ 0x01, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15, 0x15,\ |
| 7444 | +/* 40 */ 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11,\ |
| 7445 | +/* 48 */ 0x08, 0x11, 0x11, 0x11, 0x11, 0x02, 0x02, 0x00,\ |
| 7446 | +/* 56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01,\ |
| 7447 | +/* 64 */ 0x01, 0x01, 0x01, 0x01, 0x4c, 0x4c, 0x08, 0x00,\ |
| 7448 | +/* 72 */ 0x02, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15, 0x15,\ |
| 7443 | 7449 | /* 80 */ 0x15, 0x01, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c,\ |
| 7444 | | -/* 88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x02, 0x24, 0x02, 0x00,\ |
| 7445 | | -/* 96 */ 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\ |
| 7446 | | -/* 104 */ 0x0c, 0x45, 0x15, 0x01, 0x02, 0x00, 0x01, 0x08,\ |
| 7447 | | -/* 112 */ 0x05, 0x05, 0x05, 0x00, 0x00, 0x00, 0x01, 0x00,\ |
| 7448 | | -/* 120 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,\ |
| 7449 | | -/* 128 */ 0x00, 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00,\ |
| 7450 | +/* 88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x01, 0x24, 0x02, 0x02,\ |
| 7451 | +/* 96 */ 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,\ |
| 7452 | +/* 104 */ 0x00, 0x0c, 0x45, 0x15, 0x01, 0x02, 0x00, 0x01,\ |
| 7453 | +/* 112 */ 0x08, 0x05, 0x05, 0x05, 0x00, 0x00, 0x00, 0x01,\ |
| 7454 | +/* 120 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,\ |
| 7455 | +/* 128 */ 0x01, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00,\ |
| 7450 | 7456 | /* 136 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x04,\ |
| 7451 | 7457 | /* 144 */ 0x04, 0x04,} |
| 7452 | 7458 | |
| 7453 | 7459 | /************** End of opcodes.h *********************************************/ |
| 7454 | 7460 | /************** Continuing where we left off in vdbe.h ***********************/ |
| | @@ -7658,10 +7664,11 @@ |
| 7658 | 7664 | SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager); |
| 7659 | 7665 | |
| 7660 | 7666 | /* Functions used to query pager state and configuration. */ |
| 7661 | 7667 | SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*); |
| 7662 | 7668 | SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*); |
| 7669 | +SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager*); |
| 7663 | 7670 | SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager*); |
| 7664 | 7671 | SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager*); |
| 7665 | 7672 | SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*); |
| 7666 | 7673 | SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*); |
| 7667 | 7674 | SQLITE_PRIVATE int sqlite3PagerNosync(Pager*); |
| | @@ -8478,10 +8485,11 @@ |
| 8478 | 8485 | #define SQLITE_LoadExtension 0x00400000 /* Enable load_extension */ |
| 8479 | 8486 | #define SQLITE_RecoveryMode 0x00800000 /* Ignore schema errors */ |
| 8480 | 8487 | #define SQLITE_ReverseOrder 0x01000000 /* Reverse unordered SELECTs */ |
| 8481 | 8488 | #define SQLITE_RecTriggers 0x02000000 /* Enable recursive triggers */ |
| 8482 | 8489 | #define SQLITE_ForeignKeys 0x04000000 /* Enforce foreign key constraints */ |
| 8490 | +#define SQLITE_AutoIndex 0x08000000 /* Enable automatic indexes */ |
| 8483 | 8491 | |
| 8484 | 8492 | /* |
| 8485 | 8493 | ** Bits of the sqlite3.flags field that are used by the |
| 8486 | 8494 | ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface. |
| 8487 | 8495 | ** These must be the low-order bits of the flags field. |
| | @@ -9340,10 +9348,13 @@ |
| 9340 | 9348 | ** |
| 9341 | 9349 | ** The jointype starts out showing the join type between the current table |
| 9342 | 9350 | ** and the next table on the list. The parser builds the list this way. |
| 9343 | 9351 | ** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each |
| 9344 | 9352 | ** jointype expresses the join between the table and the previous table. |
| 9353 | +** |
| 9354 | +** In the colUsed field, the high-order bit (bit 63) is set if the table |
| 9355 | +** contains more than 63 columns and the 64-th or later column is used. |
| 9345 | 9356 | */ |
| 9346 | 9357 | struct SrcList { |
| 9347 | 9358 | i16 nSrc; /* Number of tables or subqueries in the FROM clause */ |
| 9348 | 9359 | i16 nAlloc; /* Number of entries allocated in a[] below */ |
| 9349 | 9360 | struct SrcList_item { |
| | @@ -9451,11 +9462,11 @@ |
| 9451 | 9462 | #define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */ |
| 9452 | 9463 | #define WHERE_ORDERBY_MIN 0x0001 /* ORDER BY processing for min() func */ |
| 9453 | 9464 | #define WHERE_ORDERBY_MAX 0x0002 /* ORDER BY processing for max() func */ |
| 9454 | 9465 | #define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */ |
| 9455 | 9466 | #define WHERE_DUPLICATES_OK 0x0008 /* Ok to return a row more than once */ |
| 9456 | | -#define WHERE_OMIT_OPEN 0x0010 /* Table cursor are already open */ |
| 9467 | +#define WHERE_OMIT_OPEN 0x0010 /* Table cursors are already open */ |
| 9457 | 9468 | #define WHERE_OMIT_CLOSE 0x0020 /* Omit close of table & index cursors */ |
| 9458 | 9469 | #define WHERE_FORCE_TABLE 0x0040 /* Do not use an index-only search */ |
| 9459 | 9470 | #define WHERE_ONETABLE_ONLY 0x0080 /* Only code the 1st table in pTabList */ |
| 9460 | 9471 | |
| 9461 | 9472 | /* |
| | @@ -9474,10 +9485,11 @@ |
| 9474 | 9485 | int iTop; /* The very beginning of the WHERE loop */ |
| 9475 | 9486 | int iContinue; /* Jump here to continue with next record */ |
| 9476 | 9487 | int iBreak; /* Jump here to break out of the loop */ |
| 9477 | 9488 | int nLevel; /* Number of nested loop */ |
| 9478 | 9489 | struct WhereClause *pWC; /* Decomposition of the WHERE clause */ |
| 9490 | + double savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */ |
| 9479 | 9491 | WhereLevel a[1]; /* Information about each nest loop in WHERE */ |
| 9480 | 9492 | }; |
| 9481 | 9493 | |
| 9482 | 9494 | /* |
| 9483 | 9495 | ** A NameContext defines a context in which to resolve table and column |
| | @@ -9715,10 +9727,11 @@ |
| 9715 | 9727 | u32 oldmask; /* Mask of old.* columns referenced */ |
| 9716 | 9728 | u32 newmask; /* Mask of new.* columns referenced */ |
| 9717 | 9729 | u8 eTriggerOp; /* TK_UPDATE, TK_INSERT or TK_DELETE */ |
| 9718 | 9730 | u8 eOrconf; /* Default ON CONFLICT policy for trigger steps */ |
| 9719 | 9731 | u8 disableTriggers; /* True to disable triggers */ |
| 9732 | + double nQueryLoop; /* Estimated number of iterations of a query */ |
| 9720 | 9733 | |
| 9721 | 9734 | /* Above is constant between recursions. Below is reset before and after |
| 9722 | 9735 | ** each recursion */ |
| 9723 | 9736 | |
| 9724 | 9737 | int nVar; /* Number of '?' variables seen in the SQL so far */ |
| | @@ -10656,11 +10669,50 @@ |
| 10656 | 10669 | #else |
| 10657 | 10670 | # define IOTRACE(A) |
| 10658 | 10671 | # define sqlite3VdbeIOTraceSql(X) |
| 10659 | 10672 | #endif |
| 10660 | 10673 | |
| 10674 | +/* |
| 10675 | +** These routines are available for the mem2.c debugging memory allocator |
| 10676 | +** only. They are used to verify that different "types" of memory |
| 10677 | +** allocations are properly tracked by the system. |
| 10678 | +** |
| 10679 | +** sqlite3MemdebugSetType() sets the "type" of an allocation to one of |
| 10680 | +** the MEMTYPE_* macros defined below. The type must be a bitmask with |
| 10681 | +** a single bit set. |
| 10682 | +** |
| 10683 | +** sqlite3MemdebugHasType() returns true if any of the bits in its second |
| 10684 | +** argument match the type set by the previous sqlite3MemdebugSetType(). |
| 10685 | +** sqlite3MemdebugHasType() is intended for use inside assert() statements. |
| 10686 | +** For example: |
| 10687 | +** |
| 10688 | +** assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 10689 | +** |
| 10690 | +** Perhaps the most important point is the difference between MEMTYPE_HEAP |
| 10691 | +** and MEMTYPE_DB. If an allocation is MEMTYPE_DB, that means it might have |
| 10692 | +** been allocated by lookaside, except the allocation was too large or |
| 10693 | +** lookaside was already full. It is important to verify that allocations |
| 10694 | +** that might have been satisfied by lookaside are not passed back to |
| 10695 | +** non-lookaside free() routines. Asserts such as the example above are |
| 10696 | +** placed on the non-lookaside free() routines to verify this constraint. |
| 10697 | +** |
| 10698 | +** All of this is no-op for a production build. It only comes into |
| 10699 | +** play when the SQLITE_MEMDEBUG compile-time option is used. |
| 10700 | +*/ |
| 10701 | +#ifdef SQLITE_MEMDEBUG |
| 10702 | +SQLITE_PRIVATE void sqlite3MemdebugSetType(void*,u8); |
| 10703 | +SQLITE_PRIVATE int sqlite3MemdebugHasType(void*,u8); |
| 10704 | +#else |
| 10705 | +# define sqlite3MemdebugSetType(X,Y) /* no-op */ |
| 10706 | +# define sqlite3MemdebugHasType(X,Y) 1 |
| 10661 | 10707 | #endif |
| 10708 | +#define MEMTYPE_HEAP 0x01 /* General heap allocations */ |
| 10709 | +#define MEMTYPE_DB 0x02 /* Associated with a database connection */ |
| 10710 | +#define MEMTYPE_SCRATCH 0x04 /* Scratch allocations */ |
| 10711 | +#define MEMTYPE_PCACHE 0x08 /* Page cache allocations */ |
| 10712 | + |
| 10713 | +#endif /* _SQLITEINT_H_ */ |
| 10662 | 10714 | |
| 10663 | 10715 | /************** End of sqliteInt.h *******************************************/ |
| 10664 | 10716 | /************** Begin file global.c ******************************************/ |
| 10665 | 10717 | /* |
| 10666 | 10718 | ** 2008 June 13 |
| | @@ -11038,10 +11090,13 @@ |
| 11038 | 11090 | #ifdef SQLITE_OMIT_AUTOINCREMENT |
| 11039 | 11091 | "OMIT_AUTOINCREMENT", |
| 11040 | 11092 | #endif |
| 11041 | 11093 | #ifdef SQLITE_OMIT_AUTOINIT |
| 11042 | 11094 | "OMIT_AUTOINIT", |
| 11095 | +#endif |
| 11096 | +#ifdef SQLITE_OMIT_AUTOMATIC_INDEX |
| 11097 | + "OMIT_AUTOMATIC_INDEX", |
| 11043 | 11098 | #endif |
| 11044 | 11099 | #ifdef SQLITE_OMIT_AUTOVACUUM |
| 11045 | 11100 | "OMIT_AUTOVACUUM", |
| 11046 | 11101 | #endif |
| 11047 | 11102 | #ifdef SQLITE_OMIT_BETWEEN_OPTIMIZATION |
| | @@ -11367,10 +11422,30 @@ |
| 11367 | 11422 | if( resetFlag ){ |
| 11368 | 11423 | db->lookaside.mxOut = db->lookaside.nOut; |
| 11369 | 11424 | } |
| 11370 | 11425 | break; |
| 11371 | 11426 | } |
| 11427 | + |
| 11428 | + /* |
| 11429 | + ** Return an approximation for the amount of memory currently used |
| 11430 | + ** by all pagers associated with the given database connection. The |
| 11431 | + ** highwater mark is meaningless and is returned as zero. |
| 11432 | + */ |
| 11433 | + case SQLITE_DBSTATUS_CACHE_USED: { |
| 11434 | + int totalUsed = 0; |
| 11435 | + int i; |
| 11436 | + for(i=0; i<db->nDb; i++){ |
| 11437 | + Btree *pBt = db->aDb[i].pBt; |
| 11438 | + if( pBt ){ |
| 11439 | + Pager *pPager = sqlite3BtreePager(pBt); |
| 11440 | + totalUsed += sqlite3PagerMemUsed(pPager); |
| 11441 | + } |
| 11442 | + } |
| 11443 | + *pCurrent = totalUsed; |
| 11444 | + *pHighwater = 0; |
| 11445 | + break; |
| 11446 | + } |
| 11372 | 11447 | default: { |
| 11373 | 11448 | return SQLITE_ERROR; |
| 11374 | 11449 | } |
| 11375 | 11450 | } |
| 11376 | 11451 | return SQLITE_OK; |
| | @@ -13140,11 +13215,12 @@ |
| 13140 | 13215 | struct MemBlockHdr { |
| 13141 | 13216 | i64 iSize; /* Size of this allocation */ |
| 13142 | 13217 | struct MemBlockHdr *pNext, *pPrev; /* Linked list of all unfreed memory */ |
| 13143 | 13218 | char nBacktrace; /* Number of backtraces on this alloc */ |
| 13144 | 13219 | char nBacktraceSlots; /* Available backtrace slots */ |
| 13145 | | - short nTitle; /* Bytes of title; includes '\0' */ |
| 13220 | + u8 nTitle; /* Bytes of title; includes '\0' */ |
| 13221 | + u8 eType; /* Allocation type code */ |
| 13146 | 13222 | int iForeGuard; /* Guard word for sanity */ |
| 13147 | 13223 | }; |
| 13148 | 13224 | |
| 13149 | 13225 | /* |
| 13150 | 13226 | ** Guard words |
| | @@ -13348,10 +13424,11 @@ |
| 13348 | 13424 | }else{ |
| 13349 | 13425 | mem.pFirst = pHdr; |
| 13350 | 13426 | } |
| 13351 | 13427 | mem.pLast = pHdr; |
| 13352 | 13428 | pHdr->iForeGuard = FOREGUARD; |
| 13429 | + pHdr->eType = MEMTYPE_HEAP; |
| 13353 | 13430 | pHdr->nBacktraceSlots = mem.nBacktrace; |
| 13354 | 13431 | pHdr->nTitle = mem.nTitle; |
| 13355 | 13432 | if( mem.nBacktrace ){ |
| 13356 | 13433 | void *aAddr[40]; |
| 13357 | 13434 | pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1; |
| | @@ -13454,10 +13531,51 @@ |
| 13454 | 13531 | sqlite3MemShutdown, |
| 13455 | 13532 | 0 |
| 13456 | 13533 | }; |
| 13457 | 13534 | sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods); |
| 13458 | 13535 | } |
| 13536 | + |
| 13537 | +/* |
| 13538 | +** Set the "type" of an allocation. |
| 13539 | +*/ |
| 13540 | +SQLITE_PRIVATE void sqlite3MemdebugSetType(void *p, u8 eType){ |
| 13541 | + if( p ){ |
| 13542 | + struct MemBlockHdr *pHdr; |
| 13543 | + pHdr = sqlite3MemsysGetHeader(p); |
| 13544 | + assert( pHdr->iForeGuard==FOREGUARD ); |
| 13545 | + pHdr->eType = eType; |
| 13546 | + } |
| 13547 | +} |
| 13548 | + |
| 13549 | +/* |
| 13550 | +** Return TRUE if the mask of type in eType matches the type of the |
| 13551 | +** allocation p. Also return true if p==NULL. |
| 13552 | +** |
| 13553 | +** This routine is designed for use within an assert() statement, to |
| 13554 | +** verify the type of an allocation. For example: |
| 13555 | +** |
| 13556 | +** assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) ); |
| 13557 | +*/ |
| 13558 | +SQLITE_PRIVATE int sqlite3MemdebugHasType(void *p, u8 eType){ |
| 13559 | + int rc = 1; |
| 13560 | + if( p ){ |
| 13561 | + struct MemBlockHdr *pHdr; |
| 13562 | + pHdr = sqlite3MemsysGetHeader(p); |
| 13563 | + assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */ |
| 13564 | + assert( (pHdr->eType & (pHdr->eType-1))==0 ); /* Only one type bit set */ |
| 13565 | + if( (pHdr->eType&eType)==0 ){ |
| 13566 | + void **pBt; |
| 13567 | + pBt = (void**)pHdr; |
| 13568 | + pBt -= pHdr->nBacktraceSlots; |
| 13569 | + backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(stderr)); |
| 13570 | + fprintf(stderr, "\n"); |
| 13571 | + rc = 0; |
| 13572 | + } |
| 13573 | + } |
| 13574 | + return rc; |
| 13575 | +} |
| 13576 | + |
| 13459 | 13577 | |
| 13460 | 13578 | /* |
| 13461 | 13579 | ** Set the number of backtrace levels kept for each allocation. |
| 13462 | 13580 | ** A value of zero turns off backtracing. The number is always rounded |
| 13463 | 13581 | ** up to a multiple of 2. |
| | @@ -16446,10 +16564,11 @@ |
| 16446 | 16564 | if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n); |
| 16447 | 16565 | sqlite3_mutex_leave(mem0.mutex); |
| 16448 | 16566 | }else{ |
| 16449 | 16567 | p = sqlite3GlobalConfig.m.xMalloc(n); |
| 16450 | 16568 | } |
| 16569 | + sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH); |
| 16451 | 16570 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
| 16452 | 16571 | scratchAllocOut = p!=0; |
| 16453 | 16572 | #endif |
| 16454 | 16573 | return p; |
| 16455 | 16574 | } |
| | @@ -16466,10 +16585,12 @@ |
| 16466 | 16585 | #endif |
| 16467 | 16586 | |
| 16468 | 16587 | if( sqlite3GlobalConfig.pScratch==0 |
| 16469 | 16588 | || p<sqlite3GlobalConfig.pScratch |
| 16470 | 16589 | || p>=(void*)mem0.aScratchFree ){ |
| 16590 | + assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) ); |
| 16591 | + sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 16471 | 16592 | if( sqlite3GlobalConfig.bMemstat ){ |
| 16472 | 16593 | int iSize = sqlite3MallocSize(p); |
| 16473 | 16594 | sqlite3_mutex_enter(mem0.mutex); |
| 16474 | 16595 | sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize); |
| 16475 | 16596 | sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize); |
| | @@ -16506,26 +16627,30 @@ |
| 16506 | 16627 | /* |
| 16507 | 16628 | ** Return the size of a memory allocation previously obtained from |
| 16508 | 16629 | ** sqlite3Malloc() or sqlite3_malloc(). |
| 16509 | 16630 | */ |
| 16510 | 16631 | SQLITE_PRIVATE int sqlite3MallocSize(void *p){ |
| 16632 | + assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 16511 | 16633 | return sqlite3GlobalConfig.m.xSize(p); |
| 16512 | 16634 | } |
| 16513 | 16635 | SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){ |
| 16514 | 16636 | assert( db==0 || sqlite3_mutex_held(db->mutex) ); |
| 16515 | 16637 | if( isLookaside(db, p) ){ |
| 16516 | 16638 | return db->lookaside.sz; |
| 16517 | 16639 | }else{ |
| 16640 | + assert( sqlite3MemdebugHasType(p, |
| 16641 | + db ? (MEMTYPE_DB|MEMTYPE_HEAP) : MEMTYPE_HEAP) ); |
| 16518 | 16642 | return sqlite3GlobalConfig.m.xSize(p); |
| 16519 | 16643 | } |
| 16520 | 16644 | } |
| 16521 | 16645 | |
| 16522 | 16646 | /* |
| 16523 | 16647 | ** Free memory previously obtained from sqlite3Malloc(). |
| 16524 | 16648 | */ |
| 16525 | 16649 | SQLITE_API void sqlite3_free(void *p){ |
| 16526 | 16650 | if( p==0 ) return; |
| 16651 | + assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 16527 | 16652 | if( sqlite3GlobalConfig.bMemstat ){ |
| 16528 | 16653 | sqlite3_mutex_enter(mem0.mutex); |
| 16529 | 16654 | sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p)); |
| 16530 | 16655 | sqlite3GlobalConfig.m.xFree(p); |
| 16531 | 16656 | sqlite3_mutex_leave(mem0.mutex); |
| | @@ -16544,10 +16669,12 @@ |
| 16544 | 16669 | LookasideSlot *pBuf = (LookasideSlot*)p; |
| 16545 | 16670 | pBuf->pNext = db->lookaside.pFree; |
| 16546 | 16671 | db->lookaside.pFree = pBuf; |
| 16547 | 16672 | db->lookaside.nOut--; |
| 16548 | 16673 | }else{ |
| 16674 | + assert( sqlite3MemdebugHasType(p, MEMTYPE_DB|MEMTYPE_HEAP) ); |
| 16675 | + sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 16549 | 16676 | sqlite3_free(p); |
| 16550 | 16677 | } |
| 16551 | 16678 | } |
| 16552 | 16679 | |
| 16553 | 16680 | /* |
| | @@ -16576,10 +16703,11 @@ |
| 16576 | 16703 | sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes); |
| 16577 | 16704 | if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >= |
| 16578 | 16705 | mem0.alarmThreshold ){ |
| 16579 | 16706 | sqlite3MallocAlarm(nNew-nOld); |
| 16580 | 16707 | } |
| 16708 | + assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) ); |
| 16581 | 16709 | pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
| 16582 | 16710 | if( pNew==0 && mem0.alarmCallback ){ |
| 16583 | 16711 | sqlite3MallocAlarm(nBytes); |
| 16584 | 16712 | pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
| 16585 | 16713 | } |
| | @@ -16673,10 +16801,12 @@ |
| 16673 | 16801 | #endif |
| 16674 | 16802 | p = sqlite3Malloc(n); |
| 16675 | 16803 | if( !p && db ){ |
| 16676 | 16804 | db->mallocFailed = 1; |
| 16677 | 16805 | } |
| 16806 | + sqlite3MemdebugSetType(p, |
| 16807 | + (db && db->lookaside.bEnabled) ? MEMTYPE_DB : MEMTYPE_HEAP); |
| 16678 | 16808 | return p; |
| 16679 | 16809 | } |
| 16680 | 16810 | |
| 16681 | 16811 | /* |
| 16682 | 16812 | ** Resize the block of memory pointed to by p to n bytes. If the |
| | @@ -16698,14 +16828,18 @@ |
| 16698 | 16828 | if( pNew ){ |
| 16699 | 16829 | memcpy(pNew, p, db->lookaside.sz); |
| 16700 | 16830 | sqlite3DbFree(db, p); |
| 16701 | 16831 | } |
| 16702 | 16832 | }else{ |
| 16833 | + assert( sqlite3MemdebugHasType(p, MEMTYPE_DB|MEMTYPE_HEAP) ); |
| 16834 | + sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 16703 | 16835 | pNew = sqlite3_realloc(p, n); |
| 16704 | 16836 | if( !pNew ){ |
| 16705 | 16837 | db->mallocFailed = 1; |
| 16706 | 16838 | } |
| 16839 | + sqlite3MemdebugSetType(pNew, |
| 16840 | + db->lookaside.bEnabled ? MEMTYPE_DB : MEMTYPE_HEAP); |
| 16707 | 16841 | } |
| 16708 | 16842 | } |
| 16709 | 16843 | return pNew; |
| 16710 | 16844 | } |
| 16711 | 16845 | |
| | @@ -18305,11 +18439,11 @@ |
| 18305 | 18439 | u8 isPrepareV2; /* True if prepared with prepare_v2() */ |
| 18306 | 18440 | int nChange; /* Number of db changes made since last reset */ |
| 18307 | 18441 | int btreeMask; /* Bitmask of db->aDb[] entries referenced */ |
| 18308 | 18442 | i64 startTime; /* Time when query started - used for profiling */ |
| 18309 | 18443 | BtreeMutexArray aMutex; /* An array of Btree used here and needing locks */ |
| 18310 | | - int aCounter[2]; /* Counters used by sqlite3_stmt_status() */ |
| 18444 | + int aCounter[3]; /* Counters used by sqlite3_stmt_status() */ |
| 18311 | 18445 | char *zSql; /* Text of the SQL statement that generated this */ |
| 18312 | 18446 | void *pFree; /* Free this when deleting the vdbe */ |
| 18313 | 18447 | i64 nFkConstraint; /* Number of imm. FK constraints this VM */ |
| 18314 | 18448 | i64 nStmtDefCons; /* Number of def. constraints when stmt started */ |
| 18315 | 18449 | int iStatement; /* Statement number (or 0 if has not opened stmt) */ |
| | @@ -20345,52 +20479,52 @@ |
| 20345 | 20479 | /* 35 */ "ReadCookie", |
| 20346 | 20480 | /* 36 */ "SetCookie", |
| 20347 | 20481 | /* 37 */ "VerifyCookie", |
| 20348 | 20482 | /* 38 */ "OpenRead", |
| 20349 | 20483 | /* 39 */ "OpenWrite", |
| 20350 | | - /* 40 */ "OpenEphemeral", |
| 20351 | | - /* 41 */ "OpenPseudo", |
| 20352 | | - /* 42 */ "Close", |
| 20353 | | - /* 43 */ "SeekLt", |
| 20354 | | - /* 44 */ "SeekLe", |
| 20355 | | - /* 45 */ "SeekGe", |
| 20356 | | - /* 46 */ "SeekGt", |
| 20357 | | - /* 47 */ "Seek", |
| 20358 | | - /* 48 */ "NotFound", |
| 20359 | | - /* 49 */ "Found", |
| 20360 | | - /* 50 */ "IsUnique", |
| 20361 | | - /* 51 */ "NotExists", |
| 20362 | | - /* 52 */ "Sequence", |
| 20363 | | - /* 53 */ "NewRowid", |
| 20364 | | - /* 54 */ "Insert", |
| 20365 | | - /* 55 */ "InsertInt", |
| 20366 | | - /* 56 */ "Delete", |
| 20367 | | - /* 57 */ "ResetCount", |
| 20368 | | - /* 58 */ "RowKey", |
| 20369 | | - /* 59 */ "RowData", |
| 20370 | | - /* 60 */ "Rowid", |
| 20371 | | - /* 61 */ "NullRow", |
| 20372 | | - /* 62 */ "Last", |
| 20373 | | - /* 63 */ "Sort", |
| 20374 | | - /* 64 */ "Rewind", |
| 20375 | | - /* 65 */ "Prev", |
| 20376 | | - /* 66 */ "Next", |
| 20377 | | - /* 67 */ "IdxInsert", |
| 20484 | + /* 40 */ "OpenAutoindex", |
| 20485 | + /* 41 */ "OpenEphemeral", |
| 20486 | + /* 42 */ "OpenPseudo", |
| 20487 | + /* 43 */ "Close", |
| 20488 | + /* 44 */ "SeekLt", |
| 20489 | + /* 45 */ "SeekLe", |
| 20490 | + /* 46 */ "SeekGe", |
| 20491 | + /* 47 */ "SeekGt", |
| 20492 | + /* 48 */ "Seek", |
| 20493 | + /* 49 */ "NotFound", |
| 20494 | + /* 50 */ "Found", |
| 20495 | + /* 51 */ "IsUnique", |
| 20496 | + /* 52 */ "NotExists", |
| 20497 | + /* 53 */ "Sequence", |
| 20498 | + /* 54 */ "NewRowid", |
| 20499 | + /* 55 */ "Insert", |
| 20500 | + /* 56 */ "InsertInt", |
| 20501 | + /* 57 */ "Delete", |
| 20502 | + /* 58 */ "ResetCount", |
| 20503 | + /* 59 */ "RowKey", |
| 20504 | + /* 60 */ "RowData", |
| 20505 | + /* 61 */ "Rowid", |
| 20506 | + /* 62 */ "NullRow", |
| 20507 | + /* 63 */ "Last", |
| 20508 | + /* 64 */ "Sort", |
| 20509 | + /* 65 */ "Rewind", |
| 20510 | + /* 66 */ "Prev", |
| 20511 | + /* 67 */ "Next", |
| 20378 | 20512 | /* 68 */ "Or", |
| 20379 | 20513 | /* 69 */ "And", |
| 20380 | | - /* 70 */ "IdxDelete", |
| 20381 | | - /* 71 */ "IdxRowid", |
| 20382 | | - /* 72 */ "IdxLT", |
| 20514 | + /* 70 */ "IdxInsert", |
| 20515 | + /* 71 */ "IdxDelete", |
| 20516 | + /* 72 */ "IdxRowid", |
| 20383 | 20517 | /* 73 */ "IsNull", |
| 20384 | 20518 | /* 74 */ "NotNull", |
| 20385 | 20519 | /* 75 */ "Ne", |
| 20386 | 20520 | /* 76 */ "Eq", |
| 20387 | 20521 | /* 77 */ "Gt", |
| 20388 | 20522 | /* 78 */ "Le", |
| 20389 | 20523 | /* 79 */ "Lt", |
| 20390 | 20524 | /* 80 */ "Ge", |
| 20391 | | - /* 81 */ "IdxGE", |
| 20525 | + /* 81 */ "IdxLT", |
| 20392 | 20526 | /* 82 */ "BitAnd", |
| 20393 | 20527 | /* 83 */ "BitOr", |
| 20394 | 20528 | /* 84 */ "ShiftLeft", |
| 20395 | 20529 | /* 85 */ "ShiftRight", |
| 20396 | 20530 | /* 86 */ "Add", |
| | @@ -20397,54 +20531,54 @@ |
| 20397 | 20531 | /* 87 */ "Subtract", |
| 20398 | 20532 | /* 88 */ "Multiply", |
| 20399 | 20533 | /* 89 */ "Divide", |
| 20400 | 20534 | /* 90 */ "Remainder", |
| 20401 | 20535 | /* 91 */ "Concat", |
| 20402 | | - /* 92 */ "Destroy", |
| 20536 | + /* 92 */ "IdxGE", |
| 20403 | 20537 | /* 93 */ "BitNot", |
| 20404 | 20538 | /* 94 */ "String8", |
| 20405 | | - /* 95 */ "Clear", |
| 20406 | | - /* 96 */ "CreateIndex", |
| 20407 | | - /* 97 */ "CreateTable", |
| 20408 | | - /* 98 */ "ParseSchema", |
| 20409 | | - /* 99 */ "LoadAnalysis", |
| 20410 | | - /* 100 */ "DropTable", |
| 20411 | | - /* 101 */ "DropIndex", |
| 20412 | | - /* 102 */ "DropTrigger", |
| 20413 | | - /* 103 */ "IntegrityCk", |
| 20414 | | - /* 104 */ "RowSetAdd", |
| 20415 | | - /* 105 */ "RowSetRead", |
| 20416 | | - /* 106 */ "RowSetTest", |
| 20417 | | - /* 107 */ "Program", |
| 20418 | | - /* 108 */ "Param", |
| 20419 | | - /* 109 */ "FkCounter", |
| 20420 | | - /* 110 */ "FkIfZero", |
| 20421 | | - /* 111 */ "MemMax", |
| 20422 | | - /* 112 */ "IfPos", |
| 20423 | | - /* 113 */ "IfNeg", |
| 20424 | | - /* 114 */ "IfZero", |
| 20425 | | - /* 115 */ "AggStep", |
| 20426 | | - /* 116 */ "AggFinal", |
| 20427 | | - /* 117 */ "Vacuum", |
| 20428 | | - /* 118 */ "IncrVacuum", |
| 20429 | | - /* 119 */ "Expire", |
| 20430 | | - /* 120 */ "TableLock", |
| 20431 | | - /* 121 */ "VBegin", |
| 20432 | | - /* 122 */ "VCreate", |
| 20433 | | - /* 123 */ "VDestroy", |
| 20434 | | - /* 124 */ "VOpen", |
| 20435 | | - /* 125 */ "VFilter", |
| 20436 | | - /* 126 */ "VColumn", |
| 20437 | | - /* 127 */ "VNext", |
| 20438 | | - /* 128 */ "VRename", |
| 20439 | | - /* 129 */ "VUpdate", |
| 20539 | + /* 95 */ "Destroy", |
| 20540 | + /* 96 */ "Clear", |
| 20541 | + /* 97 */ "CreateIndex", |
| 20542 | + /* 98 */ "CreateTable", |
| 20543 | + /* 99 */ "ParseSchema", |
| 20544 | + /* 100 */ "LoadAnalysis", |
| 20545 | + /* 101 */ "DropTable", |
| 20546 | + /* 102 */ "DropIndex", |
| 20547 | + /* 103 */ "DropTrigger", |
| 20548 | + /* 104 */ "IntegrityCk", |
| 20549 | + /* 105 */ "RowSetAdd", |
| 20550 | + /* 106 */ "RowSetRead", |
| 20551 | + /* 107 */ "RowSetTest", |
| 20552 | + /* 108 */ "Program", |
| 20553 | + /* 109 */ "Param", |
| 20554 | + /* 110 */ "FkCounter", |
| 20555 | + /* 111 */ "FkIfZero", |
| 20556 | + /* 112 */ "MemMax", |
| 20557 | + /* 113 */ "IfPos", |
| 20558 | + /* 114 */ "IfNeg", |
| 20559 | + /* 115 */ "IfZero", |
| 20560 | + /* 116 */ "AggStep", |
| 20561 | + /* 117 */ "AggFinal", |
| 20562 | + /* 118 */ "Vacuum", |
| 20563 | + /* 119 */ "IncrVacuum", |
| 20564 | + /* 120 */ "Expire", |
| 20565 | + /* 121 */ "TableLock", |
| 20566 | + /* 122 */ "VBegin", |
| 20567 | + /* 123 */ "VCreate", |
| 20568 | + /* 124 */ "VDestroy", |
| 20569 | + /* 125 */ "VOpen", |
| 20570 | + /* 126 */ "VFilter", |
| 20571 | + /* 127 */ "VColumn", |
| 20572 | + /* 128 */ "VNext", |
| 20573 | + /* 129 */ "VRename", |
| 20440 | 20574 | /* 130 */ "Real", |
| 20441 | | - /* 131 */ "Pagecount", |
| 20442 | | - /* 132 */ "Trace", |
| 20443 | | - /* 133 */ "Noop", |
| 20444 | | - /* 134 */ "Explain", |
| 20445 | | - /* 135 */ "NotUsed_135", |
| 20575 | + /* 131 */ "VUpdate", |
| 20576 | + /* 132 */ "Pagecount", |
| 20577 | + /* 133 */ "Trace", |
| 20578 | + /* 134 */ "Noop", |
| 20579 | + /* 135 */ "Explain", |
| 20446 | 20580 | /* 136 */ "NotUsed_136", |
| 20447 | 20581 | /* 137 */ "NotUsed_137", |
| 20448 | 20582 | /* 138 */ "NotUsed_138", |
| 20449 | 20583 | /* 139 */ "NotUsed_139", |
| 20450 | 20584 | /* 140 */ "NotUsed_140", |
| | @@ -26155,13 +26289,11 @@ |
| 26155 | 26289 | flags |= SQLITE_OPEN_READONLY; |
| 26156 | 26290 | openFlags |= O_RDONLY; |
| 26157 | 26291 | fd = open(zName, openFlags, openMode); |
| 26158 | 26292 | } |
| 26159 | 26293 | if( fd<0 ){ |
| 26160 | | - sqlite3_log(SQLITE_CANTOPEN, "cannot open file [%s]: %s", zName, |
| 26161 | | - strerror(errno)); |
| 26162 | | - rc = SQLITE_CANTOPEN; |
| 26294 | + rc = SQLITE_CANTOPEN_BKPT; |
| 26163 | 26295 | goto open_finished; |
| 26164 | 26296 | } |
| 26165 | 26297 | } |
| 26166 | 26298 | assert( fd>=0 ); |
| 26167 | 26299 | if( pOutFlags ){ |
| | @@ -30816,11 +30948,16 @@ |
| 30816 | 30948 | if( pCache->pCache ){ |
| 30817 | 30949 | PgHdr *p; |
| 30818 | 30950 | PgHdr *pNext; |
| 30819 | 30951 | for(p=pCache->pDirty; p; p=pNext){ |
| 30820 | 30952 | pNext = p->pDirtyNext; |
| 30821 | | - if( p->pgno>pgno ){ |
| 30953 | + /* This routine never gets call with a positive pgno except right |
| 30954 | + ** after sqlite3PcacheCleanAll(). So if there are dirty pages, |
| 30955 | + ** it must be that pgno==0. |
| 30956 | + */ |
| 30957 | + assert( p->pgno>0 ); |
| 30958 | + if( ALWAYS(p->pgno>pgno) ){ |
| 30822 | 30959 | assert( p->flags&PGHDR_DIRTY ); |
| 30823 | 30960 | sqlite3PcacheMakeClean(p); |
| 30824 | 30961 | } |
| 30825 | 30962 | } |
| 30826 | 30963 | if( pgno==0 && pCache->pPage1 ){ |
| | @@ -31160,10 +31297,11 @@ |
| 31160 | 31297 | pcache1EnterMutex(); |
| 31161 | 31298 | if( p ){ |
| 31162 | 31299 | int sz = sqlite3MallocSize(p); |
| 31163 | 31300 | sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz); |
| 31164 | 31301 | } |
| 31302 | + sqlite3MemdebugSetType(p, MEMTYPE_PCACHE); |
| 31165 | 31303 | } |
| 31166 | 31304 | return p; |
| 31167 | 31305 | } |
| 31168 | 31306 | |
| 31169 | 31307 | /* |
| | @@ -31177,11 +31315,14 @@ |
| 31177 | 31315 | sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1); |
| 31178 | 31316 | pSlot = (PgFreeslot*)p; |
| 31179 | 31317 | pSlot->pNext = pcache1.pFree; |
| 31180 | 31318 | pcache1.pFree = pSlot; |
| 31181 | 31319 | }else{ |
| 31182 | | - int iSize = sqlite3MallocSize(p); |
| 31320 | + int iSize; |
| 31321 | + assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) ); |
| 31322 | + sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 31323 | + iSize = sqlite3MallocSize(p); |
| 31183 | 31324 | sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize); |
| 31184 | 31325 | sqlite3_free(p); |
| 31185 | 31326 | } |
| 31186 | 31327 | } |
| 31187 | 31328 | |
| | @@ -32213,10 +32354,91 @@ |
| 32213 | 32354 | ** file simultaneously, or one process from reading the database while |
| 32214 | 32355 | ** another is writing. |
| 32215 | 32356 | */ |
| 32216 | 32357 | #ifndef SQLITE_OMIT_DISKIO |
| 32217 | 32358 | |
| 32359 | +/* |
| 32360 | +******************** NOTES ON THE DESIGN OF THE PAGER ************************ |
| 32361 | +** |
| 32362 | +** Within this comment block, a page is deemed to have been synced |
| 32363 | +** automatically as soon as it is written when PRAGMA synchronous=OFF. |
| 32364 | +** Otherwise, the page is not synced until the xSync method of the VFS |
| 32365 | +** is called successfully on the file containing the page. |
| 32366 | +** |
| 32367 | +** Definition: A page of the database file is said to be "overwriteable" if |
| 32368 | +** one or more of the following are true about the page: |
| 32369 | +** |
| 32370 | +** (a) The original content of the page as it was at the beginning of |
| 32371 | +** the transaction has been written into the rollback journal and |
| 32372 | +** synced. |
| 32373 | +** |
| 32374 | +** (b) The page was a freelist leaf page at the start of the transaction. |
| 32375 | +** |
| 32376 | +** (c) The page number is greater than the largest page that existed in |
| 32377 | +** the database file at the start of the transaction. |
| 32378 | +** |
| 32379 | +** (1) A page of the database file is never overwritten unless one of the |
| 32380 | +** following are true: |
| 32381 | +** |
| 32382 | +** (a) The page and all other pages on the same sector are overwriteable. |
| 32383 | +** |
| 32384 | +** (b) The atomic page write optimization is enabled, and the entire |
| 32385 | +** transaction other than the update of the transaction sequence |
| 32386 | +** number consists of a single page change. |
| 32387 | +** |
| 32388 | +** (2) The content of a page written into the rollback journal exactly matches |
| 32389 | +** both the content in the database when the rollback journal was written |
| 32390 | +** and the content in the database at the beginning of the current |
| 32391 | +** transaction. |
| 32392 | +** |
| 32393 | +** (3) Writes to the database file are an integer multiple of the page size |
| 32394 | +** in length and are aligned to a page boundary. |
| 32395 | +** |
| 32396 | +** (4) Reads from the database file are either aligned on a page boundary and |
| 32397 | +** an integer multiple of the page size in length or are taken from the |
| 32398 | +** first 100 bytes of the database file. |
| 32399 | +** |
| 32400 | +** (5) All writes to the database file are synced prior to the rollback journal |
| 32401 | +** being deleted, truncated, or zeroed. |
| 32402 | +** |
| 32403 | +** (6) If a master journal file is used, then all writes to the database file |
| 32404 | +** are synced prior to the master journal being deleted. |
| 32405 | +** |
| 32406 | +** Definition: Two databases (or the same database at two points it time) |
| 32407 | +** are said to be "logically equivalent" if they give the same answer to |
| 32408 | +** all queries. Note in particular the the content of freelist leaf |
| 32409 | +** pages can be changed arbitarily without effecting the logical equivalence |
| 32410 | +** of the database. |
| 32411 | +** |
| 32412 | +** (7) At any time, if any subset, including the empty set and the total set, |
| 32413 | +** of the unsynced changes to a rollback journal are removed and the |
| 32414 | +** journal is rolled back, the resulting database file will be logical |
| 32415 | +** equivalent to the database file at the beginning of the transaction. |
| 32416 | +** |
| 32417 | +** (8) When a transaction is rolled back, the xTruncate method of the VFS |
| 32418 | +** is called to restore the database file to the same size it was at |
| 32419 | +** the beginning of the transaction. (In some VFSes, the xTruncate |
| 32420 | +** method is a no-op, but that does not change the fact the SQLite will |
| 32421 | +** invoke it.) |
| 32422 | +** |
| 32423 | +** (9) Whenever the database file is modified, at least one bit in the range |
| 32424 | +** of bytes from 24 through 39 inclusive will be changed prior to releasing |
| 32425 | +** the EXCLUSIVE lock. |
| 32426 | +** |
| 32427 | +** (10) The pattern of bits in bytes 24 through 39 shall not repeat in less |
| 32428 | +** than one billion transactions. |
| 32429 | +** |
| 32430 | +** (11) A database file is well-formed at the beginning and at the conclusion |
| 32431 | +** of every transaction. |
| 32432 | +** |
| 32433 | +** (12) An EXCLUSIVE lock is held on the database file when writing to |
| 32434 | +** the database file. |
| 32435 | +** |
| 32436 | +** (13) A SHARED lock is held on the database file while reading any |
| 32437 | +** content out of the database file. |
| 32438 | +*/ |
| 32439 | + |
| 32218 | 32440 | /* |
| 32219 | 32441 | ** Macros for troubleshooting. Normally turned off |
| 32220 | 32442 | */ |
| 32221 | 32443 | #if 0 |
| 32222 | 32444 | int sqlite3PagerTrace=1; /* True to enable tracing */ |
| | @@ -32389,11 +32611,12 @@ |
| 32389 | 32611 | ** It is used when committing or otherwise ending a transaction. If |
| 32390 | 32612 | ** the dbModified flag is clear then less work has to be done. |
| 32391 | 32613 | ** |
| 32392 | 32614 | ** journalStarted |
| 32393 | 32615 | ** |
| 32394 | | -** This flag is set whenever the the main journal is synced. |
| 32616 | +** This flag is set whenever the the main journal is opened and |
| 32617 | +** initialized |
| 32395 | 32618 | ** |
| 32396 | 32619 | ** The point of this flag is that it must be set after the |
| 32397 | 32620 | ** first journal header in a journal file has been synced to disk. |
| 32398 | 32621 | ** After this has happened, new pages appended to the database |
| 32399 | 32622 | ** do not need the PGHDR_NEED_SYNC flag set, as they do not need |
| | @@ -32415,11 +32638,14 @@ |
| 32415 | 32638 | ** master journal name is only written to the journal file the first |
| 32416 | 32639 | ** time CommitPhaseOne() is called. |
| 32417 | 32640 | ** |
| 32418 | 32641 | ** doNotSync |
| 32419 | 32642 | ** |
| 32420 | | -** This variable is set and cleared by sqlite3PagerWrite(). |
| 32643 | +** When enabled, cache spills are prohibited and the journal file cannot |
| 32644 | +** be synced. This variable is set and cleared by sqlite3PagerWrite() |
| 32645 | +** in order to prevent a journal sync from happening in between the |
| 32646 | +** journalling of two pages on the same sector. |
| 32421 | 32647 | ** |
| 32422 | 32648 | ** needSync |
| 32423 | 32649 | ** |
| 32424 | 32650 | ** TODO: It might be easier to set this variable in writeJournalHdr() |
| 32425 | 32651 | ** and writeMasterJournal() only. Change its meaning to "unsynced data |
| | @@ -32475,10 +32701,11 @@ |
| 32475 | 32701 | sqlite3_file *fd; /* File descriptor for database */ |
| 32476 | 32702 | sqlite3_file *jfd; /* File descriptor for main journal */ |
| 32477 | 32703 | sqlite3_file *sjfd; /* File descriptor for sub-journal */ |
| 32478 | 32704 | i64 journalOff; /* Current write offset in the journal file */ |
| 32479 | 32705 | i64 journalHdr; /* Byte offset to previous journal header */ |
| 32706 | + i64 journalSizeLimit; /* Size limit for persistent journal files */ |
| 32480 | 32707 | PagerSavepoint *aSavepoint; /* Array of active savepoints */ |
| 32481 | 32708 | int nSavepoint; /* Number of elements in aSavepoint[] */ |
| 32482 | 32709 | char dbFileVers[16]; /* Changes whenever database file changes */ |
| 32483 | 32710 | u32 sectorSize; /* Assumed sector size during rollback */ |
| 32484 | 32711 | |
| | @@ -32501,11 +32728,10 @@ |
| 32501 | 32728 | void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */ |
| 32502 | 32729 | void (*xCodecFree)(void*); /* Destructor for the codec */ |
| 32503 | 32730 | void *pCodec; /* First argument to xCodec... methods */ |
| 32504 | 32731 | #endif |
| 32505 | 32732 | char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */ |
| 32506 | | - i64 journalSizeLimit; /* Size limit for persistent journal files */ |
| 32507 | 32733 | PCache *pPCache; /* Pointer to page cache object */ |
| 32508 | 32734 | sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */ |
| 32509 | 32735 | }; |
| 32510 | 32736 | |
| 32511 | 32737 | /* |
| | @@ -33017,10 +33243,11 @@ |
| 33017 | 33243 | ** to populate the entire journal header sector. |
| 33018 | 33244 | */ |
| 33019 | 33245 | for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){ |
| 33020 | 33246 | IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader)) |
| 33021 | 33247 | rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff); |
| 33248 | + assert( pPager->journalHdr <= pPager->journalOff ); |
| 33022 | 33249 | pPager->journalOff += nHeader; |
| 33023 | 33250 | } |
| 33024 | 33251 | |
| 33025 | 33252 | return rc; |
| 33026 | 33253 | } |
| | @@ -33175,10 +33402,11 @@ |
| 33175 | 33402 | ){ |
| 33176 | 33403 | return SQLITE_OK; |
| 33177 | 33404 | } |
| 33178 | 33405 | pPager->setMaster = 1; |
| 33179 | 33406 | assert( isOpen(pPager->jfd) ); |
| 33407 | + assert( pPager->journalHdr <= pPager->journalOff ); |
| 33180 | 33408 | |
| 33181 | 33409 | /* Calculate the length in bytes and the checksum of zMaster */ |
| 33182 | 33410 | for(nMaster=0; zMaster[nMaster]; nMaster++){ |
| 33183 | 33411 | cksum += zMaster[nMaster]; |
| 33184 | 33412 | } |
| | @@ -33315,12 +33543,13 @@ |
| 33315 | 33543 | pPager->pInJournal = 0; |
| 33316 | 33544 | releaseAllSavepoints(pPager); |
| 33317 | 33545 | |
| 33318 | 33546 | /* If the file is unlocked, somebody else might change it. The |
| 33319 | 33547 | ** values stored in Pager.dbSize etc. might become invalid if |
| 33320 | | - ** this happens. TODO: Really, this doesn't need to be cleared |
| 33548 | + ** this happens. One can argue that this doesn't need to be cleared |
| 33321 | 33549 | ** until the change-counter check fails in PagerSharedLock(). |
| 33550 | + ** Clearing the page size cache here is being conservative. |
| 33322 | 33551 | */ |
| 33323 | 33552 | pPager->dbSizeValid = 0; |
| 33324 | 33553 | |
| 33325 | 33554 | rc = osUnlock(pPager->fd, NO_LOCK); |
| 33326 | 33555 | if( rc ){ |
| | @@ -33506,16 +33735,15 @@ |
| 33506 | 33735 | } |
| 33507 | 33736 | |
| 33508 | 33737 | #ifdef SQLITE_CHECK_PAGES |
| 33509 | 33738 | sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash); |
| 33510 | 33739 | #endif |
| 33511 | | - |
| 33512 | | - sqlite3PcacheCleanAll(pPager->pPCache); |
| 33513 | | - sqlite3BitvecDestroy(pPager->pInJournal); |
| 33514 | | - pPager->pInJournal = 0; |
| 33515 | | - pPager->nRec = 0; |
| 33516 | 33740 | } |
| 33741 | + sqlite3BitvecDestroy(pPager->pInJournal); |
| 33742 | + pPager->pInJournal = 0; |
| 33743 | + pPager->nRec = 0; |
| 33744 | + sqlite3PcacheCleanAll(pPager->pPCache); |
| 33517 | 33745 | |
| 33518 | 33746 | if( !pPager->exclusiveMode ){ |
| 33519 | 33747 | rc2 = osUnlock(pPager->fd, SHARED_LOCK); |
| 33520 | 33748 | pPager->state = PAGER_SHARED; |
| 33521 | 33749 | pPager->changeCountDone = 0; |
| | @@ -33604,22 +33832,22 @@ |
| 33604 | 33832 | ** allocated by this function. If this is the case and an allocation fails, |
| 33605 | 33833 | ** SQLITE_NOMEM is returned. |
| 33606 | 33834 | */ |
| 33607 | 33835 | static int pager_playback_one_page( |
| 33608 | 33836 | Pager *pPager, /* The pager being played back */ |
| 33609 | | - int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */ |
| 33610 | | - int isUnsync, /* True if reading from unsynced main journal */ |
| 33611 | 33837 | i64 *pOffset, /* Offset of record to playback */ |
| 33612 | | - int isSavepnt, /* True for a savepoint rollback */ |
| 33613 | | - Bitvec *pDone /* Bitvec of pages already played back */ |
| 33838 | + Bitvec *pDone, /* Bitvec of pages already played back */ |
| 33839 | + int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */ |
| 33840 | + int isSavepnt /* True for a savepoint rollback */ |
| 33614 | 33841 | ){ |
| 33615 | 33842 | int rc; |
| 33616 | 33843 | PgHdr *pPg; /* An existing page in the cache */ |
| 33617 | 33844 | Pgno pgno; /* The page number of a page in journal */ |
| 33618 | 33845 | u32 cksum; /* Checksum used for sanity checking */ |
| 33619 | 33846 | char *aData; /* Temporary storage for the page */ |
| 33620 | 33847 | sqlite3_file *jfd; /* The file descriptor for the journal file */ |
| 33848 | + int isSynced; /* True if journal page is synced */ |
| 33621 | 33849 | |
| 33622 | 33850 | assert( (isMainJrnl&~1)==0 ); /* isMainJrnl is 0 or 1 */ |
| 33623 | 33851 | assert( (isSavepnt&~1)==0 ); /* isSavepnt is 0 or 1 */ |
| 33624 | 33852 | assert( isMainJrnl || pDone ); /* pDone always used on sub-journals */ |
| 33625 | 33853 | assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */ |
| | @@ -33699,16 +33927,21 @@ |
| 33699 | 33927 | assert( pPg || !MEMDB ); |
| 33700 | 33928 | PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n", |
| 33701 | 33929 | PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData), |
| 33702 | 33930 | (isMainJrnl?"main-journal":"sub-journal") |
| 33703 | 33931 | )); |
| 33932 | + if( isMainJrnl ){ |
| 33933 | + isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr); |
| 33934 | + }else{ |
| 33935 | + isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC)); |
| 33936 | + } |
| 33704 | 33937 | if( (pPager->state>=PAGER_EXCLUSIVE) |
| 33705 | | - && (pPg==0 || 0==(pPg->flags&PGHDR_NEED_SYNC)) |
| 33706 | 33938 | && isOpen(pPager->fd) |
| 33707 | | - && !isUnsync |
| 33939 | + && isSynced |
| 33708 | 33940 | ){ |
| 33709 | 33941 | i64 ofst = (pgno-1)*(i64)pPager->pageSize; |
| 33942 | + testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 ); |
| 33710 | 33943 | rc = sqlite3OsWrite(pPager->fd, (u8*)aData, pPager->pageSize, ofst); |
| 33711 | 33944 | if( pgno>pPager->dbFileSize ){ |
| 33712 | 33945 | pPager->dbFileSize = pgno; |
| 33713 | 33946 | } |
| 33714 | 33947 | if( pPager->pBackup ){ |
| | @@ -33753,11 +33986,12 @@ |
| 33753 | 33986 | pPager->xReiniter(pPg); |
| 33754 | 33987 | if( isMainJrnl && (!isSavepnt || *pOffset<=pPager->journalHdr) ){ |
| 33755 | 33988 | /* If the contents of this page were just restored from the main |
| 33756 | 33989 | ** journal file, then its content must be as they were when the |
| 33757 | 33990 | ** transaction was first opened. In this case we can mark the page |
| 33758 | | - ** as clean, since there will be no need to write it out to the. |
| 33991 | + ** as clean, since there will be no need to write it out to the |
| 33992 | + ** database. |
| 33759 | 33993 | ** |
| 33760 | 33994 | ** There is one exception to this rule. If the page is being rolled |
| 33761 | 33995 | ** back as part of a savepoint (or statement) rollback from an |
| 33762 | 33996 | ** unsynced portion of the main journal file, then it is not safe |
| 33763 | 33997 | ** to mark the page as clean. This is because marking the page as |
| | @@ -34100,12 +34334,10 @@ |
| 34100 | 34334 | /* This loop terminates either when a readJournalHdr() or |
| 34101 | 34335 | ** pager_playback_one_page() call returns SQLITE_DONE or an IO error |
| 34102 | 34336 | ** occurs. |
| 34103 | 34337 | */ |
| 34104 | 34338 | while( 1 ){ |
| 34105 | | - int isUnsync = 0; |
| 34106 | | - |
| 34107 | 34339 | /* Read the next journal header from the journal file. If there are |
| 34108 | 34340 | ** not enough bytes left in the journal file for a complete header, or |
| 34109 | 34341 | ** it is corrupted, then a process must of failed while writing it. |
| 34110 | 34342 | ** This indicates nothing more needs to be rolled back. |
| 34111 | 34343 | */ |
| | @@ -34142,11 +34374,10 @@ |
| 34142 | 34374 | ** should be computed based on the journal file size. |
| 34143 | 34375 | */ |
| 34144 | 34376 | if( nRec==0 && !isHot && |
| 34145 | 34377 | pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){ |
| 34146 | 34378 | nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager)); |
| 34147 | | - isUnsync = 1; |
| 34148 | 34379 | } |
| 34149 | 34380 | |
| 34150 | 34381 | /* If this is the first header read from the journal, truncate the |
| 34151 | 34382 | ** database file back to its original size. |
| 34152 | 34383 | */ |
| | @@ -34164,16 +34395,24 @@ |
| 34164 | 34395 | for(u=0; u<nRec; u++){ |
| 34165 | 34396 | if( needPagerReset ){ |
| 34166 | 34397 | pager_reset(pPager); |
| 34167 | 34398 | needPagerReset = 0; |
| 34168 | 34399 | } |
| 34169 | | - rc = pager_playback_one_page(pPager,1,isUnsync,&pPager->journalOff,0,0); |
| 34400 | + rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0); |
| 34170 | 34401 | if( rc!=SQLITE_OK ){ |
| 34171 | 34402 | if( rc==SQLITE_DONE ){ |
| 34172 | 34403 | rc = SQLITE_OK; |
| 34173 | 34404 | pPager->journalOff = szJ; |
| 34174 | 34405 | break; |
| 34406 | + }else if( rc==SQLITE_IOERR_SHORT_READ ){ |
| 34407 | + /* If the journal has been truncated, simply stop reading and |
| 34408 | + ** processing the journal. This might happen if the journal was |
| 34409 | + ** not completely written and synced prior to a crash. In that |
| 34410 | + ** case, the database should have never been written in the |
| 34411 | + ** first place so it is OK to simply abandon the rollback. */ |
| 34412 | + rc = SQLITE_OK; |
| 34413 | + goto end_playback; |
| 34175 | 34414 | }else{ |
| 34176 | 34415 | /* If we are unable to rollback, quit and return the error |
| 34177 | 34416 | ** code. This will cause the pager to enter the error state |
| 34178 | 34417 | ** so that no further harm will be done. Perhaps the next |
| 34179 | 34418 | ** process to come along will be able to rollback the database. |
| | @@ -34309,11 +34548,11 @@ |
| 34309 | 34548 | */ |
| 34310 | 34549 | if( pSavepoint ){ |
| 34311 | 34550 | iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ; |
| 34312 | 34551 | pPager->journalOff = pSavepoint->iOffset; |
| 34313 | 34552 | while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){ |
| 34314 | | - rc = pager_playback_one_page(pPager, 1, 0, &pPager->journalOff, 1, pDone); |
| 34553 | + rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1); |
| 34315 | 34554 | } |
| 34316 | 34555 | assert( rc!=SQLITE_DONE ); |
| 34317 | 34556 | }else{ |
| 34318 | 34557 | pPager->journalOff = 0; |
| 34319 | 34558 | } |
| | @@ -34339,11 +34578,11 @@ |
| 34339 | 34578 | && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff |
| 34340 | 34579 | ){ |
| 34341 | 34580 | nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager)); |
| 34342 | 34581 | } |
| 34343 | 34582 | for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){ |
| 34344 | | - rc = pager_playback_one_page(pPager, 1, 0, &pPager->journalOff, 1, pDone); |
| 34583 | + rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1); |
| 34345 | 34584 | } |
| 34346 | 34585 | assert( rc!=SQLITE_DONE ); |
| 34347 | 34586 | } |
| 34348 | 34587 | assert( rc!=SQLITE_OK || pPager->journalOff==szJ ); |
| 34349 | 34588 | |
| | @@ -34354,11 +34593,11 @@ |
| 34354 | 34593 | if( pSavepoint ){ |
| 34355 | 34594 | u32 ii; /* Loop counter */ |
| 34356 | 34595 | i64 offset = pSavepoint->iSubRec*(4+pPager->pageSize); |
| 34357 | 34596 | for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){ |
| 34358 | 34597 | assert( offset==ii*(4+pPager->pageSize) ); |
| 34359 | | - rc = pager_playback_one_page(pPager, 0, 0, &offset, 1, pDone); |
| 34598 | + rc = pager_playback_one_page(pPager, &offset, pDone, 0, 1); |
| 34360 | 34599 | } |
| 34361 | 34600 | assert( rc!=SQLITE_DONE ); |
| 34362 | 34601 | } |
| 34363 | 34602 | |
| 34364 | 34603 | sqlite3BitvecDestroy(pDone); |
| | @@ -34575,14 +34814,16 @@ |
| 34575 | 34814 | ** maximum page count below the current size of the database. |
| 34576 | 34815 | ** |
| 34577 | 34816 | ** Regardless of mxPage, return the current maximum page count. |
| 34578 | 34817 | */ |
| 34579 | 34818 | SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){ |
| 34819 | + int nPage; |
| 34580 | 34820 | if( mxPage>0 ){ |
| 34581 | 34821 | pPager->mxPgno = mxPage; |
| 34582 | 34822 | } |
| 34583 | | - sqlite3PagerPagecount(pPager, 0); |
| 34823 | + sqlite3PagerPagecount(pPager, &nPage); |
| 34824 | + assert( pPager->mxPgno>=nPage ); |
| 34584 | 34825 | return pPager->mxPgno; |
| 34585 | 34826 | } |
| 34586 | 34827 | |
| 34587 | 34828 | /* |
| 34588 | 34829 | ** The following set of routines are used to disable the simulated |
| | @@ -34652,15 +34893,10 @@ |
| 34652 | 34893 | ** and *pnPage is set to the number of pages in the database. |
| 34653 | 34894 | */ |
| 34654 | 34895 | SQLITE_PRIVATE int sqlite3PagerPagecount(Pager *pPager, int *pnPage){ |
| 34655 | 34896 | Pgno nPage; /* Value to return via *pnPage */ |
| 34656 | 34897 | |
| 34657 | | - /* If the pager is already in the error state, return the error code. */ |
| 34658 | | - if( pPager->errCode ){ |
| 34659 | | - return pPager->errCode; |
| 34660 | | - } |
| 34661 | | - |
| 34662 | 34898 | /* Determine the number of pages in the file. Store this in nPage. */ |
| 34663 | 34899 | if( pPager->dbSizeValid ){ |
| 34664 | 34900 | nPage = pPager->dbSize; |
| 34665 | 34901 | }else{ |
| 34666 | 34902 | int rc; /* Error returned by OsFileSize() */ |
| | @@ -34690,13 +34926,11 @@ |
| 34690 | 34926 | if( nPage>pPager->mxPgno ){ |
| 34691 | 34927 | pPager->mxPgno = (Pgno)nPage; |
| 34692 | 34928 | } |
| 34693 | 34929 | |
| 34694 | 34930 | /* Set the output variable and return SQLITE_OK */ |
| 34695 | | - if( pnPage ){ |
| 34696 | | - *pnPage = nPage; |
| 34697 | | - } |
| 34931 | + *pnPage = nPage; |
| 34698 | 34932 | return SQLITE_OK; |
| 34699 | 34933 | } |
| 34700 | 34934 | |
| 34701 | 34935 | |
| 34702 | 34936 | /* |
| | @@ -34796,10 +35030,35 @@ |
| 34796 | 35030 | assert( pPager->dbSize>=nPage ); |
| 34797 | 35031 | assert( pPager->state>=PAGER_RESERVED ); |
| 34798 | 35032 | pPager->dbSize = nPage; |
| 34799 | 35033 | assertTruncateConstraint(pPager); |
| 34800 | 35034 | } |
| 35035 | + |
| 35036 | +/* |
| 35037 | +** This function is called before attempting a hot-journal rollback. It |
| 35038 | +** syncs the journal file to disk, then sets pPager->journalHdr to the |
| 35039 | +** size of the journal file so that the pager_playback() routine knows |
| 35040 | +** that the entire journal file has been synced. |
| 35041 | +** |
| 35042 | +** Syncing a hot-journal to disk before attempting to roll it back ensures |
| 35043 | +** that if a power-failure occurs during the rollback, the process that |
| 35044 | +** attempts rollback following system recovery sees the same journal |
| 35045 | +** content as this process. |
| 35046 | +** |
| 35047 | +** If everything goes as planned, SQLITE_OK is returned. Otherwise, |
| 35048 | +** an SQLite error code. |
| 35049 | +*/ |
| 35050 | +static int pagerSyncHotJournal(Pager *pPager){ |
| 35051 | + int rc = SQLITE_OK; |
| 35052 | + if( !pPager->noSync ){ |
| 35053 | + rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL); |
| 35054 | + } |
| 35055 | + if( rc==SQLITE_OK ){ |
| 35056 | + rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr); |
| 35057 | + } |
| 35058 | + return rc; |
| 35059 | +} |
| 34801 | 35060 | |
| 34802 | 35061 | /* |
| 34803 | 35062 | ** Shutdown the page cache. Free all memory and close all files. |
| 34804 | 35063 | ** |
| 34805 | 35064 | ** If a transaction was in progress when this routine is called, that |
| | @@ -34826,11 +35085,13 @@ |
| 34826 | 35085 | ** call which may be made from within pagerUnlockAndRollback(). If it |
| 34827 | 35086 | ** is not -1, then the unsynced portion of an open journal file may |
| 34828 | 35087 | ** be played back into the database. If a power failure occurs while |
| 34829 | 35088 | ** this is happening, the database may become corrupt. |
| 34830 | 35089 | */ |
| 34831 | | - pPager->journalHdr = -1; |
| 35090 | + if( isOpen(pPager->jfd) ){ |
| 35091 | + pPager->errCode = pagerSyncHotJournal(pPager); |
| 35092 | + } |
| 34832 | 35093 | pagerUnlockAndRollback(pPager); |
| 34833 | 35094 | } |
| 34834 | 35095 | sqlite3EndBenignMalloc(); |
| 34835 | 35096 | enable_simulated_io_errors(); |
| 34836 | 35097 | PAGERTRACE(("CLOSE %d\n", PAGERID(pPager))); |
| | @@ -34916,11 +35177,11 @@ |
| 34916 | 35177 | /* This block deals with an obscure problem. If the last connection |
| 34917 | 35178 | ** that wrote to this database was operating in persistent-journal |
| 34918 | 35179 | ** mode, then the journal file may at this point actually be larger |
| 34919 | 35180 | ** than Pager.journalOff bytes. If the next thing in the journal |
| 34920 | 35181 | ** file happens to be a journal-header (written as part of the |
| 34921 | | - ** previous connections transaction), and a crash or power-failure |
| 35182 | + ** previous connection's transaction), and a crash or power-failure |
| 34922 | 35183 | ** occurs after nRec is updated but before this connection writes |
| 34923 | 35184 | ** anything else to the journal file (or commits/rolls back its |
| 34924 | 35185 | ** transaction), then SQLite may become confused when doing the |
| 34925 | 35186 | ** hot-journal rollback following recovery. It may roll back all |
| 34926 | 35187 | ** of this connections data, then proceed to rolling back the old, |
| | @@ -34988,10 +35249,11 @@ |
| 34988 | 35249 | /* The journal file was just successfully synced. Set Pager.needSync |
| 34989 | 35250 | ** to zero and clear the PGHDR_NEED_SYNC flag on all pagess. |
| 34990 | 35251 | */ |
| 34991 | 35252 | pPager->needSync = 0; |
| 34992 | 35253 | pPager->journalStarted = 1; |
| 35254 | + pPager->journalHdr = pPager->journalOff; |
| 34993 | 35255 | sqlite3PcacheClearSyncFlags(pPager->pPCache); |
| 34994 | 35256 | } |
| 34995 | 35257 | |
| 34996 | 35258 | return SQLITE_OK; |
| 34997 | 35259 | } |
| | @@ -35850,23 +36112,32 @@ |
| 35850 | 36112 | } |
| 35851 | 36113 | if( rc!=SQLITE_OK ){ |
| 35852 | 36114 | goto failed; |
| 35853 | 36115 | } |
| 35854 | 36116 | |
| 35855 | | - /* TODO: Why are these cleared here? Is it necessary? */ |
| 36117 | + /* Reset the journal status fields to indicates that we have no |
| 36118 | + ** rollback journal at this time. */ |
| 35856 | 36119 | pPager->journalStarted = 0; |
| 35857 | 36120 | pPager->journalOff = 0; |
| 35858 | 36121 | pPager->setMaster = 0; |
| 35859 | 36122 | pPager->journalHdr = 0; |
| 36123 | + |
| 36124 | + /* Make sure the journal file has been synced to disk. */ |
| 35860 | 36125 | |
| 35861 | 36126 | /* Playback and delete the journal. Drop the database write |
| 35862 | 36127 | ** lock and reacquire the read lock. Purge the cache before |
| 35863 | 36128 | ** playing back the hot-journal so that we don't end up with |
| 35864 | | - ** an inconsistent cache. |
| 36129 | + ** an inconsistent cache. Sync the hot journal before playing |
| 36130 | + ** it back since the process that crashed and left the hot journal |
| 36131 | + ** probably did not sync it and we are required to always sync |
| 36132 | + ** the journal before playing it back. |
| 35865 | 36133 | */ |
| 35866 | 36134 | if( isOpen(pPager->jfd) ){ |
| 35867 | | - rc = pager_playback(pPager, 1); |
| 36135 | + rc = pagerSyncHotJournal(pPager); |
| 36136 | + if( rc==SQLITE_OK ){ |
| 36137 | + rc = pager_playback(pPager, 1); |
| 36138 | + } |
| 35868 | 36139 | if( rc!=SQLITE_OK ){ |
| 35869 | 36140 | rc = pager_error(pPager, rc); |
| 35870 | 36141 | goto failed; |
| 35871 | 36142 | } |
| 35872 | 36143 | } |
| | @@ -35890,20 +36161,20 @@ |
| 35890 | 36161 | ** |
| 35891 | 36162 | ** There is a vanishingly small chance that a change will not be |
| 35892 | 36163 | ** detected. The chance of an undetected change is so small that |
| 35893 | 36164 | ** it can be neglected. |
| 35894 | 36165 | */ |
| 36166 | + int nPage; |
| 35895 | 36167 | char dbFileVers[sizeof(pPager->dbFileVers)]; |
| 35896 | | - sqlite3PagerPagecount(pPager, 0); |
| 36168 | + sqlite3PagerPagecount(pPager, &nPage); |
| 35897 | 36169 | |
| 35898 | 36170 | if( pPager->errCode ){ |
| 35899 | 36171 | rc = pPager->errCode; |
| 35900 | 36172 | goto failed; |
| 35901 | 36173 | } |
| 35902 | 36174 | |
| 35903 | | - assert( pPager->dbSizeValid ); |
| 35904 | | - if( pPager->dbSize>0 ){ |
| 36175 | + if( nPage>0 ){ |
| 35905 | 36176 | IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers))); |
| 35906 | 36177 | rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24); |
| 35907 | 36178 | if( rc!=SQLITE_OK ){ |
| 35908 | 36179 | goto failed; |
| 35909 | 36180 | } |
| | @@ -35968,11 +36239,11 @@ |
| 35968 | 36239 | ** of the page. This occurs in two seperate scenarios: |
| 35969 | 36240 | ** |
| 35970 | 36241 | ** a) When reading a free-list leaf page from the database, and |
| 35971 | 36242 | ** |
| 35972 | 36243 | ** b) When a savepoint is being rolled back and we need to load |
| 35973 | | -** a new page into the cache to populate with the data read |
| 36244 | +** a new page into the cache to be filled with the data read |
| 35974 | 36245 | ** from the savepoint journal. |
| 35975 | 36246 | ** |
| 35976 | 36247 | ** If noContent is true, then the data returned is zeroed instead of |
| 35977 | 36248 | ** being read from the database. Additionally, the bits corresponding |
| 35978 | 36249 | ** to pgno in Pager.pInJournal (bitvec of pages already written to the |
| | @@ -36024,11 +36295,11 @@ |
| 36024 | 36295 | goto pager_acquire_err; |
| 36025 | 36296 | } |
| 36026 | 36297 | assert( (*ppPage)->pgno==pgno ); |
| 36027 | 36298 | assert( (*ppPage)->pPager==pPager || (*ppPage)->pPager==0 ); |
| 36028 | 36299 | |
| 36029 | | - if( (*ppPage)->pPager ){ |
| 36300 | + if( (*ppPage)->pPager && !noContent ){ |
| 36030 | 36301 | /* In this case the pcache already contains an initialized copy of |
| 36031 | 36302 | ** the page. Return without further ado. */ |
| 36032 | 36303 | assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) ); |
| 36033 | 36304 | PAGER_INCR(pPager->nHit); |
| 36034 | 36305 | return SQLITE_OK; |
| | @@ -36184,10 +36455,11 @@ |
| 36184 | 36455 | ** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or |
| 36185 | 36456 | ** an IO error code if opening or writing the journal file fails. |
| 36186 | 36457 | */ |
| 36187 | 36458 | static int pager_open_journal(Pager *pPager){ |
| 36188 | 36459 | int rc = SQLITE_OK; /* Return code */ |
| 36460 | + int nPage; /* Size of database file */ |
| 36189 | 36461 | sqlite3_vfs * const pVfs = pPager->pVfs; /* Local cache of vfs pointer */ |
| 36190 | 36462 | |
| 36191 | 36463 | assert( pPager->state>=PAGER_RESERVED ); |
| 36192 | 36464 | assert( pPager->useJournal ); |
| 36193 | 36465 | assert( pPager->journalMode!=PAGER_JOURNALMODE_OFF ); |
| | @@ -36196,17 +36468,14 @@ |
| 36196 | 36468 | /* If already in the error state, this function is a no-op. But on |
| 36197 | 36469 | ** the other hand, this routine is never called if we are already in |
| 36198 | 36470 | ** an error state. */ |
| 36199 | 36471 | if( NEVER(pPager->errCode) ) return pPager->errCode; |
| 36200 | 36472 | |
| 36201 | | - /* TODO: Is it really possible to get here with dbSizeValid==0? If not, |
| 36202 | | - ** the call to PagerPagecount() can be removed. |
| 36203 | | - */ |
| 36204 | 36473 | testcase( pPager->dbSizeValid==0 ); |
| 36205 | | - sqlite3PagerPagecount(pPager, 0); |
| 36206 | | - |
| 36207 | | - pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize); |
| 36474 | + rc = sqlite3PagerPagecount(pPager, &nPage); |
| 36475 | + if( rc ) return rc; |
| 36476 | + pPager->pInJournal = sqlite3BitvecCreate(nPage); |
| 36208 | 36477 | if( pPager->pInJournal==0 ){ |
| 36209 | 36478 | return SQLITE_NOMEM; |
| 36210 | 36479 | } |
| 36211 | 36480 | |
| 36212 | 36481 | /* Open the journal file if it is not already open. */ |
| | @@ -36301,16 +36570,15 @@ |
| 36301 | 36570 | if( exFlag ){ |
| 36302 | 36571 | rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK); |
| 36303 | 36572 | } |
| 36304 | 36573 | } |
| 36305 | 36574 | |
| 36306 | | - /* If the required locks were successfully obtained, open the journal |
| 36307 | | - ** file and write the first journal-header to it. |
| 36575 | + /* No need to open the journal file at this time. It will be |
| 36576 | + ** opened before it is written to. If we defer opening the journal, |
| 36577 | + ** we might save the work of creating a file if the transaction |
| 36578 | + ** ends up being a no-op. |
| 36308 | 36579 | */ |
| 36309 | | - if( rc==SQLITE_OK && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){ |
| 36310 | | - rc = pager_open_journal(pPager); |
| 36311 | | - } |
| 36312 | 36580 | }else if( isOpen(pPager->jfd) && pPager->journalOff==0 ){ |
| 36313 | 36581 | /* This happens when the pager was in exclusive-access mode the last |
| 36314 | 36582 | ** time a (read or write) transaction was successfully concluded |
| 36315 | 36583 | ** by this connection. Instead of deleting the journal file it was |
| 36316 | 36584 | ** kept open and either was truncated to 0 bytes or its header was |
| | @@ -36321,11 +36589,10 @@ |
| 36321 | 36589 | assert( pPager->pInJournal==0 ); |
| 36322 | 36590 | rc = pager_open_journal(pPager); |
| 36323 | 36591 | } |
| 36324 | 36592 | |
| 36325 | 36593 | PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager))); |
| 36326 | | - assert( !isOpen(pPager->jfd) || pPager->journalOff>0 || rc!=SQLITE_OK ); |
| 36327 | 36594 | if( rc!=SQLITE_OK ){ |
| 36328 | 36595 | assert( !pPager->dbModified ); |
| 36329 | 36596 | /* Ignore any IO error that occurs within pager_end_transaction(). The |
| 36330 | 36597 | ** purpose of this call is to reset the internal state of the pager |
| 36331 | 36598 | ** sub-system. It doesn't matter if the journal-file is not properly |
| | @@ -36351,12 +36618,12 @@ |
| 36351 | 36618 | /* This routine is not called unless a transaction has already been |
| 36352 | 36619 | ** started. |
| 36353 | 36620 | */ |
| 36354 | 36621 | assert( pPager->state>=PAGER_RESERVED ); |
| 36355 | 36622 | |
| 36356 | | - /* If an error has been previously detected, we should not be |
| 36357 | | - ** calling this routine. Repeat the error for robustness. |
| 36623 | + /* If an error has been previously detected, report the same error |
| 36624 | + ** again. |
| 36358 | 36625 | */ |
| 36359 | 36626 | if( NEVER(pPager->errCode) ) return pPager->errCode; |
| 36360 | 36627 | |
| 36361 | 36628 | /* Higher-level routines never call this function if database is not |
| 36362 | 36629 | ** writable. But check anyway, just for robustness. */ |
| | @@ -36377,15 +36644,15 @@ |
| 36377 | 36644 | /* If we get this far, it means that the page needs to be |
| 36378 | 36645 | ** written to the transaction journal or the ckeckpoint journal |
| 36379 | 36646 | ** or both. |
| 36380 | 36647 | ** |
| 36381 | 36648 | ** Higher level routines should have already started a transaction, |
| 36382 | | - ** which means they have acquired the necessary locks and opened |
| 36383 | | - ** a rollback journal. Double-check to makes sure this is the case. |
| 36649 | + ** which means they have acquired the necessary locks but the rollback |
| 36650 | + ** journal might not yet be open. |
| 36384 | 36651 | */ |
| 36385 | 36652 | rc = sqlite3PagerBegin(pPager, 0, pPager->subjInMemory); |
| 36386 | | - if( NEVER(rc!=SQLITE_OK) ){ |
| 36653 | + if( rc!=SQLITE_OK ){ |
| 36387 | 36654 | return rc; |
| 36388 | 36655 | } |
| 36389 | 36656 | if( !isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){ |
| 36390 | 36657 | assert( pPager->useJournal ); |
| 36391 | 36658 | rc = pager_open_journal(pPager); |
| | @@ -36404,10 +36671,12 @@ |
| 36404 | 36671 | |
| 36405 | 36672 | /* We should never write to the journal file the page that |
| 36406 | 36673 | ** contains the database locks. The following assert verifies |
| 36407 | 36674 | ** that we do not. */ |
| 36408 | 36675 | assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) ); |
| 36676 | + |
| 36677 | + assert( pPager->journalHdr <= pPager->journalOff ); |
| 36409 | 36678 | CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2); |
| 36410 | 36679 | cksum = pager_cksum(pPager, (u8*)pData2); |
| 36411 | 36680 | rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno); |
| 36412 | 36681 | if( rc==SQLITE_OK ){ |
| 36413 | 36682 | rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, |
| | @@ -36523,11 +36792,12 @@ |
| 36523 | 36792 | ** an integer power of 2. It sets variable pg1 to the identifier |
| 36524 | 36793 | ** of the first page of the sector pPg is located on. |
| 36525 | 36794 | */ |
| 36526 | 36795 | pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1; |
| 36527 | 36796 | |
| 36528 | | - sqlite3PagerPagecount(pPager, (int *)&nPageCount); |
| 36797 | + rc = sqlite3PagerPagecount(pPager, (int *)&nPageCount); |
| 36798 | + if( rc ) return rc; |
| 36529 | 36799 | if( pPg->pgno>nPageCount ){ |
| 36530 | 36800 | nPage = (pPg->pgno - pg1)+1; |
| 36531 | 36801 | }else if( (pg1+nPagePerSector-1)>nPageCount ){ |
| 36532 | 36802 | nPage = nPageCount+1-pg1; |
| 36533 | 36803 | }else{ |
| | @@ -36684,10 +36954,13 @@ |
| 36684 | 36954 | /* Increment the value just read and write it back to byte 24. */ |
| 36685 | 36955 | change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers); |
| 36686 | 36956 | change_counter++; |
| 36687 | 36957 | put32bits(((char*)pPgHdr->pData)+24, change_counter); |
| 36688 | 36958 | |
| 36959 | + /* Also store the SQLite version number in bytes 96..99 */ |
| 36960 | + put32bits(((char*)pPgHdr->pData)+96, SQLITE_VERSION_NUMBER); |
| 36961 | + |
| 36689 | 36962 | /* If running in direct mode, write the contents of page 1 to the file. */ |
| 36690 | 36963 | if( DIRECT_MODE ){ |
| 36691 | 36964 | const void *zBuf = pPgHdr->pData; |
| 36692 | 36965 | assert( pPager->dbFileSize>0 ); |
| 36693 | 36966 | rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0); |
| | @@ -36757,13 +37030,11 @@ |
| 36757 | 37030 | int rc = SQLITE_OK; /* Return code */ |
| 36758 | 37031 | |
| 36759 | 37032 | /* The dbOrigSize is never set if journal_mode=OFF */ |
| 36760 | 37033 | assert( pPager->journalMode!=PAGER_JOURNALMODE_OFF || pPager->dbOrigSize==0 ); |
| 36761 | 37034 | |
| 36762 | | - /* If a prior error occurred, this routine should not be called. ROLLBACK |
| 36763 | | - ** is the appropriate response to an error, not COMMIT. Guard against |
| 36764 | | - ** coding errors by repeating the prior error. */ |
| 37035 | + /* If a prior error occurred, report that error again. */ |
| 36765 | 37036 | if( NEVER(pPager->errCode) ) return pPager->errCode; |
| 36766 | 37037 | |
| 36767 | 37038 | PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n", |
| 36768 | 37039 | pPager->zFilename, zMaster, pPager->dbSize)); |
| 36769 | 37040 | |
| | @@ -37048,10 +37319,20 @@ |
| 37048 | 37319 | ** Return the number of references to the pager. |
| 37049 | 37320 | */ |
| 37050 | 37321 | SQLITE_PRIVATE int sqlite3PagerRefcount(Pager *pPager){ |
| 37051 | 37322 | return sqlite3PcacheRefCount(pPager->pPCache); |
| 37052 | 37323 | } |
| 37324 | + |
| 37325 | +/* |
| 37326 | +** Return the approximate number of bytes of memory currently |
| 37327 | +** used by the pager and its associated cache. |
| 37328 | +*/ |
| 37329 | +SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager *pPager){ |
| 37330 | + int perPageSize = pPager->pageSize + pPager->nExtra + 20; |
| 37331 | + return perPageSize*sqlite3PcachePagecount(pPager->pPCache) |
| 37332 | + + sqlite3MallocSize(pPager); |
| 37333 | +} |
| 37053 | 37334 | |
| 37054 | 37335 | /* |
| 37055 | 37336 | ** Return the number of references to the specified page. |
| 37056 | 37337 | */ |
| 37057 | 37338 | SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage *pPage){ |
| | @@ -37101,15 +37382,14 @@ |
| 37101 | 37382 | int nCurrent = pPager->nSavepoint; /* Current number of savepoints */ |
| 37102 | 37383 | |
| 37103 | 37384 | if( nSavepoint>nCurrent && pPager->useJournal ){ |
| 37104 | 37385 | int ii; /* Iterator variable */ |
| 37105 | 37386 | PagerSavepoint *aNew; /* New Pager.aSavepoint array */ |
| 37387 | + int nPage; /* Size of database file */ |
| 37106 | 37388 | |
| 37107 | | - /* Either there is no active journal or the sub-journal is open or |
| 37108 | | - ** the journal is always stored in memory */ |
| 37109 | | - assert( pPager->nSavepoint==0 || isOpen(pPager->sjfd) || |
| 37110 | | - pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); |
| 37389 | + rc = sqlite3PagerPagecount(pPager, &nPage); |
| 37390 | + if( rc ) return rc; |
| 37111 | 37391 | |
| 37112 | 37392 | /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM |
| 37113 | 37393 | ** if the allocation fails. Otherwise, zero the new portion in case a |
| 37114 | 37394 | ** malloc failure occurs while populating it in the for(...) loop below. |
| 37115 | 37395 | */ |
| | @@ -37123,19 +37403,18 @@ |
| 37123 | 37403 | pPager->aSavepoint = aNew; |
| 37124 | 37404 | pPager->nSavepoint = nSavepoint; |
| 37125 | 37405 | |
| 37126 | 37406 | /* Populate the PagerSavepoint structures just allocated. */ |
| 37127 | 37407 | for(ii=nCurrent; ii<nSavepoint; ii++){ |
| 37128 | | - assert( pPager->dbSizeValid ); |
| 37129 | | - aNew[ii].nOrig = pPager->dbSize; |
| 37130 | | - if( isOpen(pPager->jfd) && ALWAYS(pPager->journalOff>0) ){ |
| 37408 | + aNew[ii].nOrig = nPage; |
| 37409 | + if( isOpen(pPager->jfd) && pPager->journalOff>0 ){ |
| 37131 | 37410 | aNew[ii].iOffset = pPager->journalOff; |
| 37132 | 37411 | }else{ |
| 37133 | 37412 | aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager); |
| 37134 | 37413 | } |
| 37135 | 37414 | aNew[ii].iSubRec = pPager->nSubRec; |
| 37136 | | - aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize); |
| 37415 | + aNew[ii].pInSavepoint = sqlite3BitvecCreate(nPage); |
| 37137 | 37416 | if( !aNew[ii].pInSavepoint ){ |
| 37138 | 37417 | return SQLITE_NOMEM; |
| 37139 | 37418 | } |
| 37140 | 37419 | } |
| 37141 | 37420 | |
| | @@ -37518,10 +37797,19 @@ |
| 37518 | 37797 | && (!isOpen(pPager->jfd) || 0==pPager->journalOff) |
| 37519 | 37798 | ){ |
| 37520 | 37799 | if( isOpen(pPager->jfd) ){ |
| 37521 | 37800 | sqlite3OsClose(pPager->jfd); |
| 37522 | 37801 | } |
| 37802 | + assert( (PAGER_JOURNALMODE_TRUNCATE & 1)==1 ); |
| 37803 | + assert( (PAGER_JOURNALMODE_PERSIST & 1)==1 ); |
| 37804 | + assert( (PAGER_JOURNALMODE_DELETE & 1)==0 ); |
| 37805 | + assert( (PAGER_JOURNALMODE_MEMORY & 1)==0 ); |
| 37806 | + assert( (PAGER_JOURNALMODE_OFF & 1)==0 ); |
| 37807 | + if( (pPager->journalMode & 1)==1 && (eMode & 1)==0 |
| 37808 | + && !pPager->exclusiveMode ){ |
| 37809 | + sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0); |
| 37810 | + } |
| 37523 | 37811 | pPager->journalMode = (u8)eMode; |
| 37524 | 37812 | } |
| 37525 | 37813 | return (int)pPager->journalMode; |
| 37526 | 37814 | } |
| 37527 | 37815 | |
| | @@ -37978,10 +38266,11 @@ |
| 37978 | 38266 | BtCursor *pCursor; /* A list of all open cursors */ |
| 37979 | 38267 | MemPage *pPage1; /* First page of the database */ |
| 37980 | 38268 | u8 readOnly; /* True if the underlying file is readonly */ |
| 37981 | 38269 | u8 pageSizeFixed; /* True if the page size can no longer be changed */ |
| 37982 | 38270 | u8 secureDelete; /* True if secure_delete is enabled */ |
| 38271 | + u8 initiallyEmpty; /* Database is empty at start of transaction */ |
| 37983 | 38272 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 37984 | 38273 | u8 autoVacuum; /* True if auto-vacuum is enabled */ |
| 37985 | 38274 | u8 incrVacuum; /* True if incr-vacuum is enabled */ |
| 37986 | 38275 | #endif |
| 37987 | 38276 | u16 pageSize; /* Total number of bytes on a page */ |
| | @@ -37990,10 +38279,11 @@ |
| 37990 | 38279 | u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */ |
| 37991 | 38280 | u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */ |
| 37992 | 38281 | u16 minLeaf; /* Minimum local payload in a LEAFDATA table */ |
| 37993 | 38282 | u8 inTransaction; /* Transaction state */ |
| 37994 | 38283 | int nTransaction; /* Number of open transactions (read + write) */ |
| 38284 | + u32 nPage; /* Number of pages in the database */ |
| 37995 | 38285 | void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */ |
| 37996 | 38286 | void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */ |
| 37997 | 38287 | sqlite3_mutex *mutex; /* Non-recursive mutex required to access this struct */ |
| 37998 | 38288 | Bitvec *pHasContent; /* Set of pages moved to free-list this transaction */ |
| 37999 | 38289 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| | @@ -39070,15 +39360,12 @@ |
| 39070 | 39360 | ** at the end of every transaction. |
| 39071 | 39361 | */ |
| 39072 | 39362 | static int btreeSetHasContent(BtShared *pBt, Pgno pgno){ |
| 39073 | 39363 | int rc = SQLITE_OK; |
| 39074 | 39364 | if( !pBt->pHasContent ){ |
| 39075 | | - int nPage = 100; |
| 39076 | | - sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 39077 | | - /* If sqlite3PagerPagecount() fails there is no harm because the |
| 39078 | | - ** nPage variable is unchanged from its default value of 100 */ |
| 39079 | | - pBt->pHasContent = sqlite3BitvecCreate((u32)nPage); |
| 39365 | + assert( pgno<=pBt->nPage ); |
| 39366 | + pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage); |
| 39080 | 39367 | if( !pBt->pHasContent ){ |
| 39081 | 39368 | rc = SQLITE_NOMEM; |
| 39082 | 39369 | } |
| 39083 | 39370 | } |
| 39084 | 39371 | if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){ |
| | @@ -40117,17 +40404,17 @@ |
| 40117 | 40404 | |
| 40118 | 40405 | /* |
| 40119 | 40406 | ** Return the size of the database file in pages. If there is any kind of |
| 40120 | 40407 | ** error, return ((unsigned int)-1). |
| 40121 | 40408 | */ |
| 40122 | | -static Pgno pagerPagecount(BtShared *pBt){ |
| 40123 | | - int nPage = -1; |
| 40124 | | - int rc; |
| 40125 | | - assert( pBt->pPage1 ); |
| 40126 | | - rc = sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 40127 | | - assert( rc==SQLITE_OK || nPage==-1 ); |
| 40128 | | - return (Pgno)nPage; |
| 40409 | +static Pgno btreePagecount(BtShared *pBt){ |
| 40410 | + return pBt->nPage; |
| 40411 | +} |
| 40412 | +SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree *p){ |
| 40413 | + assert( sqlite3BtreeHoldsMutex(p) ); |
| 40414 | + assert( ((p->pBt->nPage)&0x8000000)==0 ); |
| 40415 | + return (int)btreePagecount(p->pBt); |
| 40129 | 40416 | } |
| 40130 | 40417 | |
| 40131 | 40418 | /* |
| 40132 | 40419 | ** Get a page from the pager and initialize it. This routine is just a |
| 40133 | 40420 | ** convenience wrapper around separate calls to btreeGetPage() and |
| | @@ -40140,29 +40427,22 @@ |
| 40140 | 40427 | BtShared *pBt, /* The database file */ |
| 40141 | 40428 | Pgno pgno, /* Number of the page to get */ |
| 40142 | 40429 | MemPage **ppPage /* Write the page pointer here */ |
| 40143 | 40430 | ){ |
| 40144 | 40431 | int rc; |
| 40145 | | - TESTONLY( Pgno iLastPg = pagerPagecount(pBt); ) |
| 40146 | 40432 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 40147 | 40433 | |
| 40434 | + if( pgno<=0 || pgno>btreePagecount(pBt) ){ |
| 40435 | + return SQLITE_CORRUPT_BKPT; |
| 40436 | + } |
| 40148 | 40437 | rc = btreeGetPage(pBt, pgno, ppPage, 0); |
| 40149 | 40438 | if( rc==SQLITE_OK ){ |
| 40150 | 40439 | rc = btreeInitPage(*ppPage); |
| 40151 | 40440 | if( rc!=SQLITE_OK ){ |
| 40152 | 40441 | releasePage(*ppPage); |
| 40153 | 40442 | } |
| 40154 | 40443 | } |
| 40155 | | - |
| 40156 | | - /* If the requested page number was either 0 or greater than the page |
| 40157 | | - ** number of the last page in the database, this function should return |
| 40158 | | - ** SQLITE_CORRUPT or some other error (i.e. SQLITE_FULL). Check that this |
| 40159 | | - ** is the case. */ |
| 40160 | | - assert( (pgno>0 && pgno<=iLastPg) || rc!=SQLITE_OK ); |
| 40161 | | - testcase( pgno==0 ); |
| 40162 | | - testcase( pgno==iLastPg ); |
| 40163 | | - |
| 40164 | 40444 | return rc; |
| 40165 | 40445 | } |
| 40166 | 40446 | |
| 40167 | 40447 | /* |
| 40168 | 40448 | ** Release a MemPage. This should be called once for each prior |
| | @@ -40794,13 +41074,15 @@ |
| 40794 | 41074 | ** well-formed database file, then SQLITE_CORRUPT is returned. |
| 40795 | 41075 | ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM |
| 40796 | 41076 | ** is returned if we run out of memory. |
| 40797 | 41077 | */ |
| 40798 | 41078 | static int lockBtree(BtShared *pBt){ |
| 40799 | | - int rc; |
| 40800 | | - MemPage *pPage1; |
| 40801 | | - int nPage; |
| 41079 | + int rc; /* Result code from subfunctions */ |
| 41080 | + MemPage *pPage1; /* Page 1 of the database file */ |
| 41081 | + int nPage; /* Number of pages in the database */ |
| 41082 | + int nPageFile = 0; /* Number of pages in the database file */ |
| 41083 | + int nPageHeader; /* Number of pages in the database according to hdr */ |
| 40802 | 41084 | |
| 40803 | 41085 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 40804 | 41086 | assert( pBt->pPage1==0 ); |
| 40805 | 41087 | rc = sqlite3PagerSharedLock(pBt->pPager); |
| 40806 | 41088 | if( rc!=SQLITE_OK ) return rc; |
| | @@ -40808,14 +41090,18 @@ |
| 40808 | 41090 | if( rc!=SQLITE_OK ) return rc; |
| 40809 | 41091 | |
| 40810 | 41092 | /* Do some checking to help insure the file we opened really is |
| 40811 | 41093 | ** a valid database file. |
| 40812 | 41094 | */ |
| 40813 | | - rc = sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 40814 | | - if( rc!=SQLITE_OK ){ |
| 41095 | + nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData); |
| 41096 | + if( (rc = sqlite3PagerPagecount(pBt->pPager, &nPageFile))!=SQLITE_OK ){; |
| 40815 | 41097 | goto page1_init_failed; |
| 40816 | | - }else if( nPage>0 ){ |
| 41098 | + } |
| 41099 | + if( nPage==0 ){ |
| 41100 | + nPage = nPageFile; |
| 41101 | + } |
| 41102 | + if( nPage>0 ){ |
| 40817 | 41103 | int pageSize; |
| 40818 | 41104 | int usableSize; |
| 40819 | 41105 | u8 *page1 = pPage1->aData; |
| 40820 | 41106 | rc = SQLITE_NOTADB; |
| 40821 | 41107 | if( memcmp(page1, zMagicHeader, 16)!=0 ){ |
| | @@ -40857,10 +41143,14 @@ |
| 40857 | 41143 | freeTempSpace(pBt); |
| 40858 | 41144 | rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, |
| 40859 | 41145 | pageSize-usableSize); |
| 40860 | 41146 | return rc; |
| 40861 | 41147 | } |
| 41148 | + if( nPageHeader>nPageFile ){ |
| 41149 | + rc = SQLITE_CORRUPT_BKPT; |
| 41150 | + goto page1_init_failed; |
| 41151 | + } |
| 40862 | 41152 | if( usableSize<480 ){ |
| 40863 | 41153 | goto page1_init_failed; |
| 40864 | 41154 | } |
| 40865 | 41155 | pBt->pageSize = (u16)pageSize; |
| 40866 | 41156 | pBt->usableSize = (u16)usableSize; |
| | @@ -40887,10 +41177,11 @@ |
| 40887 | 41177 | pBt->minLocal = (pBt->usableSize-12)*32/255 - 23; |
| 40888 | 41178 | pBt->maxLeaf = pBt->usableSize - 35; |
| 40889 | 41179 | pBt->minLeaf = (pBt->usableSize-12)*32/255 - 23; |
| 40890 | 41180 | assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) ); |
| 40891 | 41181 | pBt->pPage1 = pPage1; |
| 41182 | + pBt->nPage = nPage; |
| 40892 | 41183 | return SQLITE_OK; |
| 40893 | 41184 | |
| 40894 | 41185 | page1_init_failed: |
| 40895 | 41186 | releasePage(pPage1); |
| 40896 | 41187 | pBt->pPage1 = 0; |
| | @@ -40924,16 +41215,14 @@ |
| 40924 | 41215 | */ |
| 40925 | 41216 | static int newDatabase(BtShared *pBt){ |
| 40926 | 41217 | MemPage *pP1; |
| 40927 | 41218 | unsigned char *data; |
| 40928 | 41219 | int rc; |
| 40929 | | - int nPage; |
| 40930 | 41220 | |
| 40931 | 41221 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 40932 | | - rc = sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 40933 | | - if( rc!=SQLITE_OK || nPage>0 ){ |
| 40934 | | - return rc; |
| 41222 | + if( pBt->nPage>0 ){ |
| 41223 | + return SQLITE_OK; |
| 40935 | 41224 | } |
| 40936 | 41225 | pP1 = pBt->pPage1; |
| 40937 | 41226 | assert( pP1!=0 ); |
| 40938 | 41227 | data = pP1->aData; |
| 40939 | 41228 | rc = sqlite3PagerWrite(pP1->pDbPage); |
| | @@ -40955,10 +41244,12 @@ |
| 40955 | 41244 | assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 ); |
| 40956 | 41245 | assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 ); |
| 40957 | 41246 | put4byte(&data[36 + 4*4], pBt->autoVacuum); |
| 40958 | 41247 | put4byte(&data[36 + 7*4], pBt->incrVacuum); |
| 40959 | 41248 | #endif |
| 41249 | + pBt->nPage = 1; |
| 41250 | + data[31] = 1; |
| 40960 | 41251 | return SQLITE_OK; |
| 40961 | 41252 | } |
| 40962 | 41253 | |
| 40963 | 41254 | /* |
| 40964 | 41255 | ** Attempt to start a new transaction. A write-transaction |
| | @@ -41044,10 +41335,11 @@ |
| 41044 | 41335 | ** page 1. So if some other shared-cache client already has a write-lock |
| 41045 | 41336 | ** on page 1, the transaction cannot be opened. */ |
| 41046 | 41337 | rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK); |
| 41047 | 41338 | if( SQLITE_OK!=rc ) goto trans_begun; |
| 41048 | 41339 | |
| 41340 | + pBt->initiallyEmpty = pBt->nPage==0; |
| 41049 | 41341 | do { |
| 41050 | 41342 | /* Call lockBtree() until either pBt->pPage1 is populated or |
| 41051 | 41343 | ** lockBtree() returns something other than SQLITE_OK. lockBtree() |
| 41052 | 41344 | ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after |
| 41053 | 41345 | ** reading page 1 it discovers that the page-size of the database |
| | @@ -41323,16 +41615,16 @@ |
| 41323 | 41615 | ** which may or may not empty the freelist. A full autovacuum |
| 41324 | 41616 | ** has nFin>0. A "PRAGMA incremental_vacuum" has nFin==0. |
| 41325 | 41617 | */ |
| 41326 | 41618 | static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg){ |
| 41327 | 41619 | Pgno nFreeList; /* Number of pages still on the free-list */ |
| 41620 | + int rc; |
| 41328 | 41621 | |
| 41329 | 41622 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 41330 | 41623 | assert( iLastPg>nFin ); |
| 41331 | 41624 | |
| 41332 | 41625 | if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){ |
| 41333 | | - int rc; |
| 41334 | 41626 | u8 eType; |
| 41335 | 41627 | Pgno iPtrPage; |
| 41336 | 41628 | |
| 41337 | 41629 | nFreeList = get4byte(&pBt->pPage1->aData[36]); |
| 41338 | 41630 | if( nFreeList==0 ){ |
| | @@ -41404,11 +41696,11 @@ |
| 41404 | 41696 | if( nFin==0 ){ |
| 41405 | 41697 | iLastPg--; |
| 41406 | 41698 | while( iLastPg==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, iLastPg) ){ |
| 41407 | 41699 | if( PTRMAP_ISPAGE(pBt, iLastPg) ){ |
| 41408 | 41700 | MemPage *pPg; |
| 41409 | | - int rc = btreeGetPage(pBt, iLastPg, &pPg, 0); |
| 41701 | + rc = btreeGetPage(pBt, iLastPg, &pPg, 0); |
| 41410 | 41702 | if( rc!=SQLITE_OK ){ |
| 41411 | 41703 | return rc; |
| 41412 | 41704 | } |
| 41413 | 41705 | rc = sqlite3PagerWrite(pPg->pDbPage); |
| 41414 | 41706 | releasePage(pPg); |
| | @@ -41417,10 +41709,11 @@ |
| 41417 | 41709 | } |
| 41418 | 41710 | } |
| 41419 | 41711 | iLastPg--; |
| 41420 | 41712 | } |
| 41421 | 41713 | sqlite3PagerTruncateImage(pBt->pPager, iLastPg); |
| 41714 | + pBt->nPage = iLastPg; |
| 41422 | 41715 | } |
| 41423 | 41716 | return SQLITE_OK; |
| 41424 | 41717 | } |
| 41425 | 41718 | |
| 41426 | 41719 | /* |
| | @@ -41439,11 +41732,15 @@ |
| 41439 | 41732 | assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE ); |
| 41440 | 41733 | if( !pBt->autoVacuum ){ |
| 41441 | 41734 | rc = SQLITE_DONE; |
| 41442 | 41735 | }else{ |
| 41443 | 41736 | invalidateAllOverflowCache(pBt); |
| 41444 | | - rc = incrVacuumStep(pBt, 0, pagerPagecount(pBt)); |
| 41737 | + rc = incrVacuumStep(pBt, 0, btreePagecount(pBt)); |
| 41738 | + if( rc==SQLITE_OK ){ |
| 41739 | + rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 41740 | + put4byte(&pBt->pPage1->aData[28], pBt->nPage); |
| 41741 | + } |
| 41445 | 41742 | } |
| 41446 | 41743 | sqlite3BtreeLeave(p); |
| 41447 | 41744 | return rc; |
| 41448 | 41745 | } |
| 41449 | 41746 | |
| | @@ -41470,11 +41767,11 @@ |
| 41470 | 41767 | Pgno nPtrmap; /* Number of PtrMap pages to be freed */ |
| 41471 | 41768 | Pgno iFree; /* The next page to be freed */ |
| 41472 | 41769 | int nEntry; /* Number of entries on one ptrmap page */ |
| 41473 | 41770 | Pgno nOrig; /* Database size before freeing */ |
| 41474 | 41771 | |
| 41475 | | - nOrig = pagerPagecount(pBt); |
| 41772 | + nOrig = btreePagecount(pBt); |
| 41476 | 41773 | if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){ |
| 41477 | 41774 | /* It is not possible to create a database for which the final page |
| 41478 | 41775 | ** is either a pointer-map page or the pending-byte page. If one |
| 41479 | 41776 | ** is encountered, this indicates corruption. |
| 41480 | 41777 | */ |
| | @@ -41495,15 +41792,16 @@ |
| 41495 | 41792 | |
| 41496 | 41793 | for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){ |
| 41497 | 41794 | rc = incrVacuumStep(pBt, nFin, iFree); |
| 41498 | 41795 | } |
| 41499 | 41796 | if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){ |
| 41500 | | - rc = SQLITE_OK; |
| 41501 | 41797 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 41502 | 41798 | put4byte(&pBt->pPage1->aData[32], 0); |
| 41503 | 41799 | put4byte(&pBt->pPage1->aData[36], 0); |
| 41800 | + put4byte(&pBt->pPage1->aData[28], nFin); |
| 41504 | 41801 | sqlite3PagerTruncateImage(pBt->pPager, nFin); |
| 41802 | + pBt->nPage = nFin; |
| 41505 | 41803 | } |
| 41506 | 41804 | if( rc!=SQLITE_OK ){ |
| 41507 | 41805 | sqlite3PagerRollback(pPager); |
| 41508 | 41806 | } |
| 41509 | 41807 | } |
| | @@ -41749,10 +42047,15 @@ |
| 41749 | 42047 | |
| 41750 | 42048 | /* The rollback may have destroyed the pPage1->aData value. So |
| 41751 | 42049 | ** call btreeGetPage() on page 1 again to make |
| 41752 | 42050 | ** sure pPage1->aData is set correctly. */ |
| 41753 | 42051 | if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){ |
| 42052 | + int nPage = get4byte(28+(u8*)pPage1->aData); |
| 42053 | + testcase( nPage==0 ); |
| 42054 | + if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 42055 | + testcase( pBt->nPage!=nPage ); |
| 42056 | + pBt->nPage = nPage; |
| 41754 | 42057 | releasePage(pPage1); |
| 41755 | 42058 | } |
| 41756 | 42059 | assert( countWriteCursors(pBt)==0 ); |
| 41757 | 42060 | pBt->inTransaction = TRANS_READ; |
| 41758 | 42061 | } |
| | @@ -41786,21 +42089,17 @@ |
| 41786 | 42089 | sqlite3BtreeEnter(p); |
| 41787 | 42090 | assert( p->inTrans==TRANS_WRITE ); |
| 41788 | 42091 | assert( pBt->readOnly==0 ); |
| 41789 | 42092 | assert( iStatement>0 ); |
| 41790 | 42093 | assert( iStatement>p->db->nSavepoint ); |
| 41791 | | - if( NEVER(p->inTrans!=TRANS_WRITE || pBt->readOnly) ){ |
| 41792 | | - rc = SQLITE_INTERNAL; |
| 41793 | | - }else{ |
| 41794 | | - assert( pBt->inTransaction==TRANS_WRITE ); |
| 41795 | | - /* At the pager level, a statement transaction is a savepoint with |
| 41796 | | - ** an index greater than all savepoints created explicitly using |
| 41797 | | - ** SQL statements. It is illegal to open, release or rollback any |
| 41798 | | - ** such savepoints while the statement transaction savepoint is active. |
| 41799 | | - */ |
| 41800 | | - rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement); |
| 41801 | | - } |
| 42094 | + assert( pBt->inTransaction==TRANS_WRITE ); |
| 42095 | + /* At the pager level, a statement transaction is a savepoint with |
| 42096 | + ** an index greater than all savepoints created explicitly using |
| 42097 | + ** SQL statements. It is illegal to open, release or rollback any |
| 42098 | + ** such savepoints while the statement transaction savepoint is active. |
| 42099 | + */ |
| 42100 | + rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement); |
| 41802 | 42101 | sqlite3BtreeLeave(p); |
| 41803 | 42102 | return rc; |
| 41804 | 42103 | } |
| 41805 | 42104 | |
| 41806 | 42105 | /* |
| | @@ -41822,11 +42121,16 @@ |
| 41822 | 42121 | assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK ); |
| 41823 | 42122 | assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) ); |
| 41824 | 42123 | sqlite3BtreeEnter(p); |
| 41825 | 42124 | rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint); |
| 41826 | 42125 | if( rc==SQLITE_OK ){ |
| 42126 | + if( iSavepoint<0 && pBt->initiallyEmpty ) pBt->nPage = 0; |
| 41827 | 42127 | rc = newDatabase(pBt); |
| 42128 | + pBt->nPage = get4byte(28 + pBt->pPage1->aData); |
| 42129 | + if( pBt->nPage==0 ){ |
| 42130 | + sqlite3PagerPagecount(pBt->pPager, (int*)&pBt->nPage); |
| 42131 | + } |
| 41828 | 42132 | } |
| 41829 | 42133 | sqlite3BtreeLeave(p); |
| 41830 | 42134 | } |
| 41831 | 42135 | return rc; |
| 41832 | 42136 | } |
| | @@ -41888,11 +42192,11 @@ |
| 41888 | 42192 | assert( pBt->pPage1 && pBt->pPage1->aData ); |
| 41889 | 42193 | |
| 41890 | 42194 | if( NEVER(wrFlag && pBt->readOnly) ){ |
| 41891 | 42195 | return SQLITE_READONLY; |
| 41892 | 42196 | } |
| 41893 | | - if( iTable==1 && pagerPagecount(pBt)==0 ){ |
| 42197 | + if( iTable==1 && btreePagecount(pBt)==0 ){ |
| 41894 | 42198 | return SQLITE_EMPTY; |
| 41895 | 42199 | } |
| 41896 | 42200 | |
| 41897 | 42201 | /* Now that no other errors can occur, finish filling in the BtCursor |
| 41898 | 42202 | ** variables and link the cursor into the BtShared list. */ |
| | @@ -42159,11 +42463,11 @@ |
| 42159 | 42463 | |
| 42160 | 42464 | while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){ |
| 42161 | 42465 | iGuess++; |
| 42162 | 42466 | } |
| 42163 | 42467 | |
| 42164 | | - if( iGuess<=pagerPagecount(pBt) ){ |
| 42468 | + if( iGuess<=btreePagecount(pBt) ){ |
| 42165 | 42469 | rc = ptrmapGet(pBt, iGuess, &eType, &pgno); |
| 42166 | 42470 | if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){ |
| 42167 | 42471 | next = iGuess; |
| 42168 | 42472 | rc = SQLITE_DONE; |
| 42169 | 42473 | } |
| | @@ -43191,11 +43495,11 @@ |
| 43191 | 43495 | MemPage *pPrevTrunk = 0; |
| 43192 | 43496 | Pgno mxPage; /* Total size of the database file */ |
| 43193 | 43497 | |
| 43194 | 43498 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 43195 | 43499 | pPage1 = pBt->pPage1; |
| 43196 | | - mxPage = pagerPagecount(pBt); |
| 43500 | + mxPage = btreePagecount(pBt); |
| 43197 | 43501 | n = get4byte(&pPage1->aData[36]); |
| 43198 | 43502 | testcase( n==mxPage-1 ); |
| 43199 | 43503 | if( n>=mxPage ){ |
| 43200 | 43504 | return SQLITE_CORRUPT_BKPT; |
| 43201 | 43505 | } |
| | @@ -43387,39 +43691,39 @@ |
| 43387 | 43691 | pPrevTrunk = 0; |
| 43388 | 43692 | }while( searchList ); |
| 43389 | 43693 | }else{ |
| 43390 | 43694 | /* There are no pages on the freelist, so create a new page at the |
| 43391 | 43695 | ** end of the file */ |
| 43392 | | - int nPage = pagerPagecount(pBt); |
| 43393 | | - *pPgno = nPage + 1; |
| 43394 | | - |
| 43395 | | - if( *pPgno==PENDING_BYTE_PAGE(pBt) ){ |
| 43396 | | - (*pPgno)++; |
| 43397 | | - } |
| 43696 | + rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 43697 | + if( rc ) return rc; |
| 43698 | + pBt->nPage++; |
| 43699 | + if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++; |
| 43398 | 43700 | |
| 43399 | 43701 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 43400 | | - if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, *pPgno) ){ |
| 43702 | + if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){ |
| 43401 | 43703 | /* If *pPgno refers to a pointer-map page, allocate two new pages |
| 43402 | 43704 | ** at the end of the file instead of one. The first allocated page |
| 43403 | 43705 | ** becomes a new pointer-map page, the second is used by the caller. |
| 43404 | 43706 | */ |
| 43405 | 43707 | MemPage *pPg = 0; |
| 43406 | | - TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno)); |
| 43407 | | - assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
| 43408 | | - rc = btreeGetPage(pBt, *pPgno, &pPg, 0); |
| 43708 | + TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage)); |
| 43709 | + assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) ); |
| 43710 | + rc = btreeGetPage(pBt, pBt->nPage, &pPg, 1); |
| 43409 | 43711 | if( rc==SQLITE_OK ){ |
| 43410 | 43712 | rc = sqlite3PagerWrite(pPg->pDbPage); |
| 43411 | 43713 | releasePage(pPg); |
| 43412 | 43714 | } |
| 43413 | 43715 | if( rc ) return rc; |
| 43414 | | - (*pPgno)++; |
| 43415 | | - if( *pPgno==PENDING_BYTE_PAGE(pBt) ){ (*pPgno)++; } |
| 43716 | + pBt->nPage++; |
| 43717 | + if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; } |
| 43416 | 43718 | } |
| 43417 | 43719 | #endif |
| 43720 | + put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage); |
| 43721 | + *pPgno = pBt->nPage; |
| 43418 | 43722 | |
| 43419 | 43723 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
| 43420 | | - rc = btreeGetPage(pBt, *pPgno, ppPage, 0); |
| 43724 | + rc = btreeGetPage(pBt, *pPgno, ppPage, 1); |
| 43421 | 43725 | if( rc ) return rc; |
| 43422 | 43726 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
| 43423 | 43727 | if( rc!=SQLITE_OK ){ |
| 43424 | 43728 | releasePage(*ppPage); |
| 43425 | 43729 | } |
| | @@ -43605,11 +43909,11 @@ |
| 43605 | 43909 | nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize; |
| 43606 | 43910 | assert( ovflPgno==0 || nOvfl>0 ); |
| 43607 | 43911 | while( nOvfl-- ){ |
| 43608 | 43912 | Pgno iNext = 0; |
| 43609 | 43913 | MemPage *pOvfl = 0; |
| 43610 | | - if( ovflPgno<2 || ovflPgno>pagerPagecount(pBt) ){ |
| 43914 | + if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){ |
| 43611 | 43915 | /* 0 is not a legal page number and page 1 cannot be an |
| 43612 | 43916 | ** overflow page. Therefore if ovflPgno<2 or past the end of the |
| 43613 | 43917 | ** file the database must be corrupt. */ |
| 43614 | 43918 | return SQLITE_CORRUPT_BKPT; |
| 43615 | 43919 | } |
| | @@ -45437,12 +45741,18 @@ |
| 45437 | 45741 | ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0, &rc); |
| 45438 | 45742 | if( rc ){ |
| 45439 | 45743 | releasePage(pRoot); |
| 45440 | 45744 | return rc; |
| 45441 | 45745 | } |
| 45746 | + |
| 45747 | + /* When the new root page was allocated, page 1 was made writable in |
| 45748 | + ** order either to increase the database filesize, or to decrement the |
| 45749 | + ** freelist count. Hence, the sqlite3BtreeUpdateMeta() call cannot fail. |
| 45750 | + */ |
| 45751 | + assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) ); |
| 45442 | 45752 | rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot); |
| 45443 | | - if( rc ){ |
| 45753 | + if( NEVER(rc) ){ |
| 45444 | 45754 | releasePage(pRoot); |
| 45445 | 45755 | return rc; |
| 45446 | 45756 | } |
| 45447 | 45757 | |
| 45448 | 45758 | }else{ |
| | @@ -45478,11 +45788,11 @@ |
| 45478 | 45788 | int rc; |
| 45479 | 45789 | unsigned char *pCell; |
| 45480 | 45790 | int i; |
| 45481 | 45791 | |
| 45482 | 45792 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 45483 | | - if( pgno>pagerPagecount(pBt) ){ |
| 45793 | + if( pgno>btreePagecount(pBt) ){ |
| 45484 | 45794 | return SQLITE_CORRUPT_BKPT; |
| 45485 | 45795 | } |
| 45486 | 45796 | |
| 45487 | 45797 | rc = getAndInitPage(pBt, pgno, &pPage); |
| 45488 | 45798 | if( rc ) return rc; |
| | @@ -46229,11 +46539,11 @@ |
| 46229 | 46539 | sqlite3BtreeEnter(p); |
| 46230 | 46540 | assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE ); |
| 46231 | 46541 | nRef = sqlite3PagerRefcount(pBt->pPager); |
| 46232 | 46542 | sCheck.pBt = pBt; |
| 46233 | 46543 | sCheck.pPager = pBt->pPager; |
| 46234 | | - sCheck.nPage = pagerPagecount(sCheck.pBt); |
| 46544 | + sCheck.nPage = btreePagecount(sCheck.pBt); |
| 46235 | 46545 | sCheck.mxErr = mxErr; |
| 46236 | 46546 | sCheck.nErr = 0; |
| 46237 | 46547 | sCheck.mallocFailed = 0; |
| 46238 | 46548 | *pnErr = 0; |
| 46239 | 46549 | if( sCheck.nPage==0 ){ |
| | @@ -46831,13 +47141,12 @@ |
| 46831 | 47141 | } |
| 46832 | 47142 | |
| 46833 | 47143 | /* Now that there is a read-lock on the source database, query the |
| 46834 | 47144 | ** source pager for the number of pages in the database. |
| 46835 | 47145 | */ |
| 46836 | | - if( rc==SQLITE_OK ){ |
| 46837 | | - rc = sqlite3PagerPagecount(pSrcPager, &nSrcPage); |
| 46838 | | - } |
| 47146 | + nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc); |
| 47147 | + assert( nSrcPage>=0 ); |
| 46839 | 47148 | for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){ |
| 46840 | 47149 | const Pgno iSrcPg = p->iNext; /* Source page number */ |
| 46841 | 47150 | if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){ |
| 46842 | 47151 | DbPage *pSrcPg; /* Source page object */ |
| 46843 | 47152 | rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg); |
| | @@ -48986,11 +49295,11 @@ |
| 48986 | 49295 | nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField; |
| 48987 | 49296 | pKeyInfo = sqlite3Malloc( nByte ); |
| 48988 | 49297 | pOp->p4.pKeyInfo = pKeyInfo; |
| 48989 | 49298 | if( pKeyInfo ){ |
| 48990 | 49299 | u8 *aSortOrder; |
| 48991 | | - memcpy(pKeyInfo, zP4, nByte); |
| 49300 | + memcpy((char*)pKeyInfo, zP4, nByte - nField); |
| 48992 | 49301 | aSortOrder = pKeyInfo->aSortOrder; |
| 48993 | 49302 | if( aSortOrder ){ |
| 48994 | 49303 | pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField]; |
| 48995 | 49304 | memcpy(pKeyInfo->aSortOrder, aSortOrder, nField); |
| 48996 | 49305 | } |
| | @@ -53812,18 +54121,13 @@ |
| 53812 | 54121 | int i; |
| 53813 | 54122 | sqlite_int64 rowid; |
| 53814 | 54123 | Mem **apArg; |
| 53815 | 54124 | Mem *pX; |
| 53816 | 54125 | } ck; |
| 53817 | | - struct OP_Pagecount_stack_vars { |
| 53818 | | - int p1; |
| 53819 | | - int nPage; |
| 53820 | | - Pager *pPager; |
| 53821 | | - } cl; |
| 53822 | 54126 | struct OP_Trace_stack_vars { |
| 53823 | 54127 | char *zTrace; |
| 53824 | | - } cm; |
| 54128 | + } cl; |
| 53825 | 54129 | } u; |
| 53826 | 54130 | /* End automatically generated code |
| 53827 | 54131 | ********************************************************************/ |
| 53828 | 54132 | |
| 53829 | 54133 | assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */ |
| | @@ -54646,11 +54950,11 @@ |
| 54646 | 54950 | assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+u.ag.n ); |
| 54647 | 54951 | u.ag.pArg = &aMem[pOp->p2]; |
| 54648 | 54952 | for(u.ag.i=0; u.ag.i<u.ag.n; u.ag.i++, u.ag.pArg++){ |
| 54649 | 54953 | u.ag.apVal[u.ag.i] = u.ag.pArg; |
| 54650 | 54954 | sqlite3VdbeMemStoreType(u.ag.pArg); |
| 54651 | | - REGISTER_TRACE(pOp->p2, u.ag.pArg); |
| 54955 | + REGISTER_TRACE(pOp->p2+u.ag.i, u.ag.pArg); |
| 54652 | 54956 | } |
| 54653 | 54957 | |
| 54654 | 54958 | assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC ); |
| 54655 | 54959 | if( pOp->p4type==P4_FUNCDEF ){ |
| 54656 | 54960 | u.ag.ctx.pFunc = pOp->p4.pFunc; |
| | @@ -56363,14 +56667,14 @@ |
| 56363 | 56667 | |
| 56364 | 56668 | /* Opcode: OpenEphemeral P1 P2 * P4 * |
| 56365 | 56669 | ** |
| 56366 | 56670 | ** Open a new cursor P1 to a transient table. |
| 56367 | 56671 | ** The cursor is always opened read/write even if |
| 56368 | | -** the main database is read-only. The transient or virtual |
| 56672 | +** the main database is read-only. The ephemeral |
| 56369 | 56673 | ** table is deleted automatically when the cursor is closed. |
| 56370 | 56674 | ** |
| 56371 | | -** P2 is the number of columns in the virtual table. |
| 56675 | +** P2 is the number of columns in the ephemeral table. |
| 56372 | 56676 | ** The cursor points to a BTree table if P4==0 and to a BTree index |
| 56373 | 56677 | ** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure |
| 56374 | 56678 | ** that defines the format of keys in the index. |
| 56375 | 56679 | ** |
| 56376 | 56680 | ** This opcode was once called OpenTemp. But that created |
| | @@ -56377,10 +56681,18 @@ |
| 56377 | 56681 | ** confusion because the term "temp table", might refer either |
| 56378 | 56682 | ** to a TEMP table at the SQL level, or to a table opened by |
| 56379 | 56683 | ** this opcode. Then this opcode was call OpenVirtual. But |
| 56380 | 56684 | ** that created confusion with the whole virtual-table idea. |
| 56381 | 56685 | */ |
| 56686 | +/* Opcode: OpenAutoindex P1 P2 * P4 * |
| 56687 | +** |
| 56688 | +** This opcode works the same as OP_OpenEphemeral. It has a |
| 56689 | +** different name to distinguish its use. Tables created using |
| 56690 | +** by this opcode will be used for automatically created transient |
| 56691 | +** indices in joins. |
| 56692 | +*/ |
| 56693 | +case OP_OpenAutoindex: |
| 56382 | 56694 | case OP_OpenEphemeral: { |
| 56383 | 56695 | #if 0 /* local variables moved into u.ax */ |
| 56384 | 56696 | VdbeCursor *pCx; |
| 56385 | 56697 | #endif /* local variables moved into u.ax */ |
| 56386 | 56698 | static const int openFlags = |
| | @@ -57515,29 +57827,35 @@ |
| 57515 | 57827 | pc = pOp->p2 - 1; |
| 57516 | 57828 | } |
| 57517 | 57829 | break; |
| 57518 | 57830 | } |
| 57519 | 57831 | |
| 57520 | | -/* Opcode: Next P1 P2 * * * |
| 57832 | +/* Opcode: Next P1 P2 * * P5 |
| 57521 | 57833 | ** |
| 57522 | 57834 | ** Advance cursor P1 so that it points to the next key/data pair in its |
| 57523 | 57835 | ** table or index. If there are no more key/value pairs then fall through |
| 57524 | 57836 | ** to the following instruction. But if the cursor advance was successful, |
| 57525 | 57837 | ** jump immediately to P2. |
| 57526 | 57838 | ** |
| 57527 | 57839 | ** The P1 cursor must be for a real table, not a pseudo-table. |
| 57528 | 57840 | ** |
| 57841 | +** If P5 is positive and the jump is taken, then event counter |
| 57842 | +** number P5-1 in the prepared statement is incremented. |
| 57843 | +** |
| 57529 | 57844 | ** See also: Prev |
| 57530 | 57845 | */ |
| 57531 | | -/* Opcode: Prev P1 P2 * * * |
| 57846 | +/* Opcode: Prev P1 P2 * * P5 |
| 57532 | 57847 | ** |
| 57533 | 57848 | ** Back up cursor P1 so that it points to the previous key/data pair in its |
| 57534 | 57849 | ** table or index. If there is no previous key/value pairs then fall through |
| 57535 | 57850 | ** to the following instruction. But if the cursor backup was successful, |
| 57536 | 57851 | ** jump immediately to P2. |
| 57537 | 57852 | ** |
| 57538 | 57853 | ** The P1 cursor must be for a real table, not a pseudo-table. |
| 57854 | +** |
| 57855 | +** If P5 is positive and the jump is taken, then event counter |
| 57856 | +** number P5-1 in the prepared statement is incremented. |
| 57539 | 57857 | */ |
| 57540 | 57858 | case OP_Prev: /* jump */ |
| 57541 | 57859 | case OP_Next: { /* jump */ |
| 57542 | 57860 | #if 0 /* local variables moved into u.bm */ |
| 57543 | 57861 | VdbeCursor *pC; |
| | @@ -57545,10 +57863,11 @@ |
| 57545 | 57863 | int res; |
| 57546 | 57864 | #endif /* local variables moved into u.bm */ |
| 57547 | 57865 | |
| 57548 | 57866 | CHECK_FOR_INTERRUPT; |
| 57549 | 57867 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 57868 | + assert( pOp->p5<=ArraySize(p->aCounter) ); |
| 57550 | 57869 | u.bm.pC = p->apCsr[pOp->p1]; |
| 57551 | 57870 | if( u.bm.pC==0 ){ |
| 57552 | 57871 | break; /* See ticket #2273 */ |
| 57553 | 57872 | } |
| 57554 | 57873 | u.bm.pCrsr = u.bm.pC->pCursor; |
| | @@ -58991,25 +59310,11 @@ |
| 58991 | 59310 | /* Opcode: Pagecount P1 P2 * * * |
| 58992 | 59311 | ** |
| 58993 | 59312 | ** Write the current number of pages in database P1 to memory cell P2. |
| 58994 | 59313 | */ |
| 58995 | 59314 | case OP_Pagecount: { /* out2-prerelease */ |
| 58996 | | -#if 0 /* local variables moved into u.cl */ |
| 58997 | | - int p1; |
| 58998 | | - int nPage; |
| 58999 | | - Pager *pPager; |
| 59000 | | -#endif /* local variables moved into u.cl */ |
| 59001 | | - |
| 59002 | | - u.cl.p1 = pOp->p1; |
| 59003 | | - u.cl.pPager = sqlite3BtreePager(db->aDb[u.cl.p1].pBt); |
| 59004 | | - rc = sqlite3PagerPagecount(u.cl.pPager, &u.cl.nPage); |
| 59005 | | - /* OP_Pagecount is always called from within a read transaction. The |
| 59006 | | - ** page count has already been successfully read and cached. So the |
| 59007 | | - ** sqlite3PagerPagecount() call above cannot fail. */ |
| 59008 | | - if( ALWAYS(rc==SQLITE_OK) ){ |
| 59009 | | - pOut->u.i = u.cl.nPage; |
| 59010 | | - } |
| 59315 | + pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt); |
| 59011 | 59316 | break; |
| 59012 | 59317 | } |
| 59013 | 59318 | #endif |
| 59014 | 59319 | |
| 59015 | 59320 | #ifndef SQLITE_OMIT_TRACE |
| | @@ -59017,24 +59322,24 @@ |
| 59017 | 59322 | ** |
| 59018 | 59323 | ** If tracing is enabled (by the sqlite3_trace()) interface, then |
| 59019 | 59324 | ** the UTF-8 string contained in P4 is emitted on the trace callback. |
| 59020 | 59325 | */ |
| 59021 | 59326 | case OP_Trace: { |
| 59022 | | -#if 0 /* local variables moved into u.cm */ |
| 59327 | +#if 0 /* local variables moved into u.cl */ |
| 59023 | 59328 | char *zTrace; |
| 59024 | | -#endif /* local variables moved into u.cm */ |
| 59329 | +#endif /* local variables moved into u.cl */ |
| 59025 | 59330 | |
| 59026 | | - u.cm.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql); |
| 59027 | | - if( u.cm.zTrace ){ |
| 59331 | + u.cl.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql); |
| 59332 | + if( u.cl.zTrace ){ |
| 59028 | 59333 | if( db->xTrace ){ |
| 59029 | | - char *z = sqlite3VdbeExpandSql(p, u.cm.zTrace); |
| 59334 | + char *z = sqlite3VdbeExpandSql(p, u.cl.zTrace); |
| 59030 | 59335 | db->xTrace(db->pTraceArg, z); |
| 59031 | 59336 | sqlite3DbFree(db, z); |
| 59032 | 59337 | } |
| 59033 | 59338 | #ifdef SQLITE_DEBUG |
| 59034 | 59339 | if( (db->flags & SQLITE_SqlTrace)!=0 ){ |
| 59035 | | - sqlite3DebugPrintf("SQL-trace: %s\n", u.cm.zTrace); |
| 59340 | + sqlite3DebugPrintf("SQL-trace: %s\n", u.cl.zTrace); |
| 59036 | 59341 | } |
| 59037 | 59342 | #endif /* SQLITE_DEBUG */ |
| 59038 | 59343 | } |
| 59039 | 59344 | break; |
| 59040 | 59345 | } |
| | @@ -59973,15 +60278,14 @@ |
| 59973 | 60278 | ** Syncing an in-memory journal is a no-op. And, in fact, this routine |
| 59974 | 60279 | ** is never called in a working implementation. This implementation |
| 59975 | 60280 | ** exists purely as a contingency, in case some malfunction in some other |
| 59976 | 60281 | ** part of SQLite causes Sync to be called by mistake. |
| 59977 | 60282 | */ |
| 59978 | | -static int memjrnlSync(sqlite3_file *NotUsed, int NotUsed2){ /*NO_TEST*/ |
| 59979 | | - UNUSED_PARAMETER2(NotUsed, NotUsed2); /*NO_TEST*/ |
| 59980 | | - assert( 0 ); /*NO_TEST*/ |
| 59981 | | - return SQLITE_OK; /*NO_TEST*/ |
| 59982 | | -} /*NO_TEST*/ |
| 60283 | +static int memjrnlSync(sqlite3_file *NotUsed, int NotUsed2){ |
| 60284 | + UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 60285 | + return SQLITE_OK; |
| 60286 | +} |
| 59983 | 60287 | |
| 59984 | 60288 | /* |
| 59985 | 60289 | ** Query the size of the file in bytes. |
| 59986 | 60290 | */ |
| 59987 | 60291 | static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){ |
| | @@ -60573,11 +60877,11 @@ |
| 60573 | 60877 | } |
| 60574 | 60878 | } |
| 60575 | 60879 | |
| 60576 | 60880 | /* |
| 60577 | 60881 | ** Allocate and return a pointer to an expression to load the column iCol |
| 60578 | | -** from datasource iSrc datasource in SrcList pSrc. |
| 60882 | +** from datasource iSrc in SrcList pSrc. |
| 60579 | 60883 | */ |
| 60580 | 60884 | SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){ |
| 60581 | 60885 | Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0); |
| 60582 | 60886 | if( p ){ |
| 60583 | 60887 | struct SrcList_item *pItem = &pSrc->a[iSrc]; |
| | @@ -60585,10 +60889,12 @@ |
| 60585 | 60889 | p->iTable = pItem->iCursor; |
| 60586 | 60890 | if( p->pTab->iPKey==iCol ){ |
| 60587 | 60891 | p->iColumn = -1; |
| 60588 | 60892 | }else{ |
| 60589 | 60893 | p->iColumn = (ynVar)iCol; |
| 60894 | + testcase( iCol==BMS ); |
| 60895 | + testcase( iCol==BMS-1 ); |
| 60590 | 60896 | pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol); |
| 60591 | 60897 | } |
| 60592 | 60898 | ExprSetProperty(p, EP_Resolved); |
| 60593 | 60899 | } |
| 60594 | 60900 | return p; |
| | @@ -66537,16 +66843,20 @@ |
| 66537 | 66843 | int n = sqlite3_column_bytes(pStmt, 2); |
| 66538 | 66844 | if( n>24 ){ |
| 66539 | 66845 | n = 24; |
| 66540 | 66846 | } |
| 66541 | 66847 | pSample->nByte = (u8)n; |
| 66542 | | - pSample->u.z = sqlite3DbMallocRaw(dbMem, n); |
| 66543 | | - if( pSample->u.z ){ |
| 66544 | | - memcpy(pSample->u.z, z, n); |
| 66848 | + if( n < 1){ |
| 66849 | + pSample->u.z = 0; |
| 66545 | 66850 | }else{ |
| 66546 | | - db->mallocFailed = 1; |
| 66547 | | - break; |
| 66851 | + pSample->u.z = sqlite3DbMallocRaw(dbMem, n); |
| 66852 | + if( pSample->u.z ){ |
| 66853 | + memcpy(pSample->u.z, z, n); |
| 66854 | + }else{ |
| 66855 | + db->mallocFailed = 1; |
| 66856 | + break; |
| 66857 | + } |
| 66548 | 66858 | } |
| 66549 | 66859 | } |
| 66550 | 66860 | } |
| 66551 | 66861 | } |
| 66552 | 66862 | } |
| | @@ -75790,11 +76100,11 @@ |
| 75790 | 76100 | }else{ |
| 75791 | 76101 | for(j=0; j<pColumn->nId; j++){ |
| 75792 | 76102 | if( pColumn->a[j].idx==i ) break; |
| 75793 | 76103 | } |
| 75794 | 76104 | } |
| 75795 | | - if( pColumn && j>=pColumn->nId ){ |
| 76105 | + if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId) ){ |
| 75796 | 76106 | sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1); |
| 75797 | 76107 | }else if( useTempTable ){ |
| 75798 | 76108 | sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1); |
| 75799 | 76109 | }else{ |
| 75800 | 76110 | assert( pSelect==0 ); /* Otherwise useTempTable is true */ |
| | @@ -78086,10 +78396,13 @@ |
| 78086 | 78396 | { "count_changes", SQLITE_CountRows }, |
| 78087 | 78397 | { "empty_result_callbacks", SQLITE_NullCallback }, |
| 78088 | 78398 | { "legacy_file_format", SQLITE_LegacyFileFmt }, |
| 78089 | 78399 | { "fullfsync", SQLITE_FullFSync }, |
| 78090 | 78400 | { "reverse_unordered_selects", SQLITE_ReverseOrder }, |
| 78401 | +#ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 78402 | + { "automatic_index", SQLITE_AutoIndex }, |
| 78403 | +#endif |
| 78091 | 78404 | #ifdef SQLITE_DEBUG |
| 78092 | 78405 | { "sql_trace", SQLITE_SqlTrace }, |
| 78093 | 78406 | { "vdbe_listing", SQLITE_VdbeListing }, |
| 78094 | 78407 | { "vdbe_trace", SQLITE_VdbeTrace }, |
| 78095 | 78408 | #endif |
| | @@ -79461,32 +79774,32 @@ |
| 79461 | 79774 | /* Call the parser to process a CREATE TABLE, INDEX or VIEW. |
| 79462 | 79775 | ** But because db->init.busy is set to 1, no VDBE code is generated |
| 79463 | 79776 | ** or executed. All the parser does is build the internal data |
| 79464 | 79777 | ** structures that describe the table, index, or view. |
| 79465 | 79778 | */ |
| 79466 | | - char *zErr; |
| 79467 | 79779 | int rc; |
| 79780 | + sqlite3_stmt *pStmt; |
| 79781 | + |
| 79468 | 79782 | assert( db->init.busy ); |
| 79469 | 79783 | db->init.iDb = iDb; |
| 79470 | 79784 | db->init.newTnum = atoi(argv[1]); |
| 79471 | 79785 | db->init.orphanTrigger = 0; |
| 79472 | | - rc = sqlite3_exec(db, argv[2], 0, 0, &zErr); |
| 79786 | + rc = sqlite3_prepare(db, argv[2], -1, &pStmt, 0); |
| 79473 | 79787 | db->init.iDb = 0; |
| 79474 | | - assert( rc!=SQLITE_OK || zErr==0 ); |
| 79475 | 79788 | if( SQLITE_OK!=rc ){ |
| 79476 | 79789 | if( db->init.orphanTrigger ){ |
| 79477 | 79790 | assert( iDb==1 ); |
| 79478 | 79791 | }else{ |
| 79479 | 79792 | pData->rc = rc; |
| 79480 | 79793 | if( rc==SQLITE_NOMEM ){ |
| 79481 | 79794 | db->mallocFailed = 1; |
| 79482 | 79795 | }else if( rc!=SQLITE_INTERRUPT && rc!=SQLITE_LOCKED ){ |
| 79483 | | - corruptSchema(pData, argv[0], zErr); |
| 79796 | + corruptSchema(pData, argv[0], sqlite3_errmsg(db)); |
| 79484 | 79797 | } |
| 79485 | 79798 | } |
| 79486 | | - sqlite3DbFree(db, zErr); |
| 79487 | 79799 | } |
| 79800 | + sqlite3_finalize(pStmt); |
| 79488 | 79801 | }else if( argv[0]==0 ){ |
| 79489 | 79802 | corruptSchema(pData, 0, 0); |
| 79490 | 79803 | }else{ |
| 79491 | 79804 | /* If the SQL column is blank it means this is an index that |
| 79492 | 79805 | ** was created to be the PRIMARY KEY or to fulfill a UNIQUE |
| | @@ -79967,10 +80280,11 @@ |
| 79967 | 80280 | } |
| 79968 | 80281 | |
| 79969 | 80282 | sqlite3VtabUnlockList(db); |
| 79970 | 80283 | |
| 79971 | 80284 | pParse->db = db; |
| 80285 | + pParse->nQueryLoop = (double)1; |
| 79972 | 80286 | if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){ |
| 79973 | 80287 | char *zSqlCopy; |
| 79974 | 80288 | int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH]; |
| 79975 | 80289 | testcase( nBytes==mxLen ); |
| 79976 | 80290 | testcase( nBytes==mxLen+1 ); |
| | @@ -79988,10 +80302,11 @@ |
| 79988 | 80302 | pParse->zTail = &zSql[nBytes]; |
| 79989 | 80303 | } |
| 79990 | 80304 | }else{ |
| 79991 | 80305 | sqlite3RunParser(pParse, zSql, &zErrMsg); |
| 79992 | 80306 | } |
| 80307 | + assert( 1==(int)pParse->nQueryLoop ); |
| 79993 | 80308 | |
| 79994 | 80309 | if( db->mallocFailed ){ |
| 79995 | 80310 | pParse->rc = SQLITE_NOMEM; |
| 79996 | 80311 | } |
| 79997 | 80312 | if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK; |
| | @@ -82772,12 +83087,12 @@ |
| 82772 | 83087 | ** (13) The subquery and outer query do not both use LIMIT |
| 82773 | 83088 | ** |
| 82774 | 83089 | ** (14) The subquery does not use OFFSET |
| 82775 | 83090 | ** |
| 82776 | 83091 | ** (15) The outer query is not part of a compound select or the |
| 82777 | | -** subquery does not have both an ORDER BY and a LIMIT clause. |
| 82778 | | -** (See ticket #2339) |
| 83092 | +** subquery does not have a LIMIT clause. |
| 83093 | +** (See ticket #2339 and ticket [02a8e81d44]). |
| 82779 | 83094 | ** |
| 82780 | 83095 | ** (16) The outer query is not an aggregate or the subquery does |
| 82781 | 83096 | ** not contain ORDER BY. (Ticket #2942) This used to not matter |
| 82782 | 83097 | ** until we introduced the group_concat() function. |
| 82783 | 83098 | ** |
| | @@ -82856,11 +83171,11 @@ |
| 82856 | 83171 | ** because they could be computed at compile-time. But when LIMIT and OFFSET |
| 82857 | 83172 | ** became arbitrary expressions, we were forced to add restrictions (13) |
| 82858 | 83173 | ** and (14). */ |
| 82859 | 83174 | if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */ |
| 82860 | 83175 | if( pSub->pOffset ) return 0; /* Restriction (14) */ |
| 82861 | | - if( p->pRightmost && pSub->pLimit && pSub->pOrderBy ){ |
| 83176 | + if( p->pRightmost && pSub->pLimit ){ |
| 82862 | 83177 | return 0; /* Restriction (15) */ |
| 82863 | 83178 | } |
| 82864 | 83179 | if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */ |
| 82865 | 83180 | if( ((pSub->selFlags & SF_Distinct)!=0 || pSub->pLimit) |
| 82866 | 83181 | && (pSrc->nSrc>1 || isAgg) ){ /* Restrictions (4)(5)(8)(9) */ |
| | @@ -83744,10 +84059,22 @@ |
| 83744 | 84059 | if( addrNext ){ |
| 83745 | 84060 | sqlite3VdbeResolveLabel(v, addrNext); |
| 83746 | 84061 | sqlite3ExprCacheClear(pParse); |
| 83747 | 84062 | } |
| 83748 | 84063 | } |
| 84064 | + |
| 84065 | + /* Before populating the accumulator registers, clear the column cache. |
| 84066 | + ** Otherwise, if any of the required column values are already present |
| 84067 | + ** in registers, sqlite3ExprCode() may use OP_SCopy to copy the value |
| 84068 | + ** to pC->iMem. But by the time the value is used, the original register |
| 84069 | + ** may have been used, invalidating the underlying buffer holding the |
| 84070 | + ** text or blob value. See ticket [883034dcb5]. |
| 84071 | + ** |
| 84072 | + ** Another solution would be to change the OP_SCopy used to copy cached |
| 84073 | + ** values to an OP_Copy. |
| 84074 | + */ |
| 84075 | + sqlite3ExprCacheClear(pParse); |
| 83749 | 84076 | for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){ |
| 83750 | 84077 | sqlite3ExprCode(pParse, pC->pExpr, pC->iMem); |
| 83751 | 84078 | } |
| 83752 | 84079 | pAggInfo->directMode = 0; |
| 83753 | 84080 | sqlite3ExprCacheClear(pParse); |
| | @@ -85557,10 +85884,11 @@ |
| 85557 | 85884 | pSubParse->db = db; |
| 85558 | 85885 | pSubParse->pTriggerTab = pTab; |
| 85559 | 85886 | pSubParse->pToplevel = pTop; |
| 85560 | 85887 | pSubParse->zAuthContext = pTrigger->zName; |
| 85561 | 85888 | pSubParse->eTriggerOp = pTrigger->op; |
| 85889 | + pSubParse->nQueryLoop = pParse->nQueryLoop; |
| 85562 | 85890 | |
| 85563 | 85891 | v = sqlite3GetVdbe(pSubParse); |
| 85564 | 85892 | if( v ){ |
| 85565 | 85893 | VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)", |
| 85566 | 85894 | pTrigger->zName, onErrorText(orconf), |
| | @@ -87477,10 +87805,11 @@ |
| 87477 | 87805 | if( pParse==0 ){ |
| 87478 | 87806 | rc = SQLITE_NOMEM; |
| 87479 | 87807 | }else{ |
| 87480 | 87808 | pParse->declareVtab = 1; |
| 87481 | 87809 | pParse->db = db; |
| 87810 | + pParse->nQueryLoop = 1; |
| 87482 | 87811 | |
| 87483 | 87812 | if( SQLITE_OK==sqlite3RunParser(pParse, zCreateTable, &zErr) |
| 87484 | 87813 | && pParse->pNewTable |
| 87485 | 87814 | && !db->mallocFailed |
| 87486 | 87815 | && !pParse->pNewTable->pSelect |
| | @@ -87997,19 +88326,21 @@ |
| 87997 | 88326 | #define WHERE_COLUMN_EQ 0x00010000 /* x=EXPR or x IN (...) or x IS NULL */ |
| 87998 | 88327 | #define WHERE_COLUMN_RANGE 0x00020000 /* x<EXPR and/or x>EXPR */ |
| 87999 | 88328 | #define WHERE_COLUMN_IN 0x00040000 /* x IN (...) */ |
| 88000 | 88329 | #define WHERE_COLUMN_NULL 0x00080000 /* x IS NULL */ |
| 88001 | 88330 | #define WHERE_INDEXED 0x000f0000 /* Anything that uses an index */ |
| 88331 | +#define WHERE_NOT_FULLSCAN 0x000f3000 /* Does not do a full table scan */ |
| 88002 | 88332 | #define WHERE_IN_ABLE 0x000f1000 /* Able to support an IN operator */ |
| 88003 | 88333 | #define WHERE_TOP_LIMIT 0x00100000 /* x<EXPR or x<=EXPR constraint */ |
| 88004 | 88334 | #define WHERE_BTM_LIMIT 0x00200000 /* x>EXPR or x>=EXPR constraint */ |
| 88005 | 88335 | #define WHERE_IDX_ONLY 0x00800000 /* Use index only - omit table */ |
| 88006 | 88336 | #define WHERE_ORDERBY 0x01000000 /* Output will appear in correct order */ |
| 88007 | 88337 | #define WHERE_REVERSE 0x02000000 /* Scan in reverse order */ |
| 88008 | 88338 | #define WHERE_UNIQUE 0x04000000 /* Selects no more than one row */ |
| 88009 | 88339 | #define WHERE_VIRTUALTABLE 0x08000000 /* Use virtual-table processing */ |
| 88010 | 88340 | #define WHERE_MULTI_OR 0x10000000 /* OR using multiple indices */ |
| 88341 | +#define WHERE_TEMP_INDEX 0x20000000 /* Uses an ephemeral index */ |
| 88011 | 88342 | |
| 88012 | 88343 | /* |
| 88013 | 88344 | ** Initialize a preallocated WhereClause structure. |
| 88014 | 88345 | */ |
| 88015 | 88346 | static void whereClauseInit( |
| | @@ -89334,10 +89665,15 @@ |
| 89334 | 89665 | #ifndef SQLITE_OMIT_OR_OPTIMIZATION |
| 89335 | 89666 | const int iCur = pSrc->iCursor; /* The cursor of the table to be accessed */ |
| 89336 | 89667 | const Bitmask maskSrc = getMask(pWC->pMaskSet, iCur); /* Bitmask for pSrc */ |
| 89337 | 89668 | WhereTerm * const pWCEnd = &pWC->a[pWC->nTerm]; /* End of pWC->a[] */ |
| 89338 | 89669 | WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 89670 | + |
| 89671 | + /* No OR-clause optimization allowed if the NOT INDEXED clause is used */ |
| 89672 | + if( pSrc->notIndexed ){ |
| 89673 | + return; |
| 89674 | + } |
| 89339 | 89675 | |
| 89340 | 89676 | /* Search the WHERE clause terms for a usable WO_OR term. */ |
| 89341 | 89677 | for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
| 89342 | 89678 | if( pTerm->eOperator==WO_OR |
| 89343 | 89679 | && ((pTerm->prereqAll & ~maskSrc) & notReady)==0 |
| | @@ -89377,12 +89713,13 @@ |
| 89377 | 89713 | } |
| 89378 | 89714 | |
| 89379 | 89715 | /* If there is an ORDER BY clause, increase the scan cost to account |
| 89380 | 89716 | ** for the cost of the sort. */ |
| 89381 | 89717 | if( pOrderBy!=0 ){ |
| 89718 | + WHERETRACE(("... sorting increases OR cost %.9g to %.9g\n", |
| 89719 | + rTotal, rTotal+nRow*estLog(nRow))); |
| 89382 | 89720 | rTotal += nRow*estLog(nRow); |
| 89383 | | - WHERETRACE(("... sorting increases OR cost to %.9g\n", rTotal)); |
| 89384 | 89721 | } |
| 89385 | 89722 | |
| 89386 | 89723 | /* If the cost of scanning using this OR term for optimization is |
| 89387 | 89724 | ** less than the current cost stored in pCost, replace the contents |
| 89388 | 89725 | ** of pCost. */ |
| | @@ -89397,10 +89734,251 @@ |
| 89397 | 89734 | } |
| 89398 | 89735 | } |
| 89399 | 89736 | #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
| 89400 | 89737 | } |
| 89401 | 89738 | |
| 89739 | +#ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 89740 | +/* |
| 89741 | +** Return TRUE if the WHERE clause term pTerm is of a form where it |
| 89742 | +** could be used with an index to access pSrc, assuming an appropriate |
| 89743 | +** index existed. |
| 89744 | +*/ |
| 89745 | +static int termCanDriveIndex( |
| 89746 | + WhereTerm *pTerm, /* WHERE clause term to check */ |
| 89747 | + struct SrcList_item *pSrc, /* Table we are trying to access */ |
| 89748 | + Bitmask notReady /* Tables in outer loops of the join */ |
| 89749 | +){ |
| 89750 | + char aff; |
| 89751 | + if( pTerm->leftCursor!=pSrc->iCursor ) return 0; |
| 89752 | + if( pTerm->eOperator!=WO_EQ ) return 0; |
| 89753 | + if( (pTerm->prereqRight & notReady)!=0 ) return 0; |
| 89754 | + aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity; |
| 89755 | + if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0; |
| 89756 | + return 1; |
| 89757 | +} |
| 89758 | +#endif |
| 89759 | + |
| 89760 | +#ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 89761 | +/* |
| 89762 | +** If the query plan for pSrc specified in pCost is a full table scan |
| 89763 | +** and indexing is allows (if there is no NOT INDEXED clause) and it |
| 89764 | +** possible to construct a transient index that would perform better |
| 89765 | +** than a full table scan even when the cost of constructing the index |
| 89766 | +** is taken into account, then alter the query plan to use the |
| 89767 | +** transient index. |
| 89768 | +*/ |
| 89769 | +static void bestAutomaticIndex( |
| 89770 | + Parse *pParse, /* The parsing context */ |
| 89771 | + WhereClause *pWC, /* The WHERE clause */ |
| 89772 | + struct SrcList_item *pSrc, /* The FROM clause term to search */ |
| 89773 | + Bitmask notReady, /* Mask of cursors that are not available */ |
| 89774 | + WhereCost *pCost /* Lowest cost query plan */ |
| 89775 | +){ |
| 89776 | + double nTableRow; /* Rows in the input table */ |
| 89777 | + double logN; /* log(nTableRow) */ |
| 89778 | + double costTempIdx; /* per-query cost of the transient index */ |
| 89779 | + WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 89780 | + WhereTerm *pWCEnd; /* End of pWC->a[] */ |
| 89781 | + Table *pTable; /* Table tht might be indexed */ |
| 89782 | + |
| 89783 | + if( (pParse->db->flags & SQLITE_AutoIndex)==0 ){ |
| 89784 | + /* Automatic indices are disabled at run-time */ |
| 89785 | + return; |
| 89786 | + } |
| 89787 | + if( (pCost->plan.wsFlags & WHERE_NOT_FULLSCAN)!=0 ){ |
| 89788 | + /* We already have some kind of index in use for this query. */ |
| 89789 | + return; |
| 89790 | + } |
| 89791 | + if( pSrc->notIndexed ){ |
| 89792 | + /* The NOT INDEXED clause appears in the SQL. */ |
| 89793 | + return; |
| 89794 | + } |
| 89795 | + |
| 89796 | + assert( pParse->nQueryLoop >= (double)1 ); |
| 89797 | + pTable = pSrc->pTab; |
| 89798 | + nTableRow = pTable->pIndex ? pTable->pIndex->aiRowEst[0] : 1000000; |
| 89799 | + logN = estLog(nTableRow); |
| 89800 | + costTempIdx = 2*logN*(nTableRow/pParse->nQueryLoop + 1); |
| 89801 | + if( costTempIdx>=pCost->rCost ){ |
| 89802 | + /* The cost of creating the transient table would be greater than |
| 89803 | + ** doing the full table scan */ |
| 89804 | + return; |
| 89805 | + } |
| 89806 | + |
| 89807 | + /* Search for any equality comparison term */ |
| 89808 | + pWCEnd = &pWC->a[pWC->nTerm]; |
| 89809 | + for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
| 89810 | + if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
| 89811 | + WHERETRACE(("auto-index reduces cost from %.2f to %.2f\n", |
| 89812 | + pCost->rCost, costTempIdx)); |
| 89813 | + pCost->rCost = costTempIdx; |
| 89814 | + pCost->nRow = logN + 1; |
| 89815 | + pCost->plan.wsFlags = WHERE_TEMP_INDEX; |
| 89816 | + pCost->used = pTerm->prereqRight; |
| 89817 | + break; |
| 89818 | + } |
| 89819 | + } |
| 89820 | +} |
| 89821 | +#else |
| 89822 | +# define bestAutomaticIndex(A,B,C,D,E) /* no-op */ |
| 89823 | +#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ |
| 89824 | + |
| 89825 | + |
| 89826 | +#ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 89827 | +/* |
| 89828 | +** Generate code to construct the Index object for an automatic index |
| 89829 | +** and to set up the WhereLevel object pLevel so that the code generator |
| 89830 | +** makes use of the automatic index. |
| 89831 | +*/ |
| 89832 | +static void constructAutomaticIndex( |
| 89833 | + Parse *pParse, /* The parsing context */ |
| 89834 | + WhereClause *pWC, /* The WHERE clause */ |
| 89835 | + struct SrcList_item *pSrc, /* The FROM clause term to get the next index */ |
| 89836 | + Bitmask notReady, /* Mask of cursors that are not available */ |
| 89837 | + WhereLevel *pLevel /* Write new index here */ |
| 89838 | +){ |
| 89839 | + int nColumn; /* Number of columns in the constructed index */ |
| 89840 | + WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 89841 | + WhereTerm *pWCEnd; /* End of pWC->a[] */ |
| 89842 | + int nByte; /* Byte of memory needed for pIdx */ |
| 89843 | + Index *pIdx; /* Object describing the transient index */ |
| 89844 | + Vdbe *v; /* Prepared statement under construction */ |
| 89845 | + int regIsInit; /* Register set by initialization */ |
| 89846 | + int addrInit; /* Address of the initialization bypass jump */ |
| 89847 | + Table *pTable; /* The table being indexed */ |
| 89848 | + KeyInfo *pKeyinfo; /* Key information for the index */ |
| 89849 | + int addrTop; /* Top of the index fill loop */ |
| 89850 | + int regRecord; /* Register holding an index record */ |
| 89851 | + int n; /* Column counter */ |
| 89852 | + int i; /* Loop counter */ |
| 89853 | + int mxBitCol; /* Maximum column in pSrc->colUsed */ |
| 89854 | + CollSeq *pColl; /* Collating sequence to on a column */ |
| 89855 | + Bitmask idxCols; /* Bitmap of columns used for indexing */ |
| 89856 | + Bitmask extraCols; /* Bitmap of additional columns */ |
| 89857 | + |
| 89858 | + /* Generate code to skip over the creation and initialization of the |
| 89859 | + ** transient index on 2nd and subsequent iterations of the loop. */ |
| 89860 | + v = pParse->pVdbe; |
| 89861 | + assert( v!=0 ); |
| 89862 | + regIsInit = ++pParse->nMem; |
| 89863 | + addrInit = sqlite3VdbeAddOp1(v, OP_If, regIsInit); |
| 89864 | + sqlite3VdbeAddOp2(v, OP_Integer, 1, regIsInit); |
| 89865 | + |
| 89866 | + /* Count the number of columns that will be added to the index |
| 89867 | + ** and used to match WHERE clause constraints */ |
| 89868 | + nColumn = 0; |
| 89869 | + pTable = pSrc->pTab; |
| 89870 | + pWCEnd = &pWC->a[pWC->nTerm]; |
| 89871 | + idxCols = 0; |
| 89872 | + for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
| 89873 | + if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
| 89874 | + int iCol = pTerm->u.leftColumn; |
| 89875 | + Bitmask cMask = iCol>=BMS ? ((Bitmask)1)<<(BMS-1) : ((Bitmask)1)<<iCol; |
| 89876 | + testcase( iCol==BMS ); |
| 89877 | + testcase( iCol==BMS-1 ); |
| 89878 | + if( (idxCols & cMask)==0 ){ |
| 89879 | + nColumn++; |
| 89880 | + idxCols |= cMask; |
| 89881 | + } |
| 89882 | + } |
| 89883 | + } |
| 89884 | + assert( nColumn>0 ); |
| 89885 | + pLevel->plan.nEq = nColumn; |
| 89886 | + |
| 89887 | + /* Count the number of additional columns needed to create a |
| 89888 | + ** covering index. A "covering index" is an index that contains all |
| 89889 | + ** columns that are needed by the query. With a covering index, the |
| 89890 | + ** original table never needs to be accessed. Automatic indices must |
| 89891 | + ** be a covering index because the index will not be updated if the |
| 89892 | + ** original table changes and the index and table cannot both be used |
| 89893 | + ** if they go out of sync. |
| 89894 | + */ |
| 89895 | + extraCols = pSrc->colUsed & (~idxCols | (((Bitmask)1)<<(BMS-1))); |
| 89896 | + mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol; |
| 89897 | + testcase( pTable->nCol==BMS-1 ); |
| 89898 | + testcase( pTable->nCol==BMS-2 ); |
| 89899 | + for(i=0; i<mxBitCol; i++){ |
| 89900 | + if( extraCols & (((Bitmask)1)<<i) ) nColumn++; |
| 89901 | + } |
| 89902 | + if( pSrc->colUsed & (((Bitmask)1)<<(BMS-1)) ){ |
| 89903 | + nColumn += pTable->nCol - BMS + 1; |
| 89904 | + } |
| 89905 | + pLevel->plan.wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WO_EQ; |
| 89906 | + |
| 89907 | + /* Construct the Index object to describe this index */ |
| 89908 | + nByte = sizeof(Index); |
| 89909 | + nByte += nColumn*sizeof(int); /* Index.aiColumn */ |
| 89910 | + nByte += nColumn*sizeof(char*); /* Index.azColl */ |
| 89911 | + nByte += nColumn; /* Index.aSortOrder */ |
| 89912 | + pIdx = sqlite3DbMallocZero(pParse->db, nByte); |
| 89913 | + if( pIdx==0 ) return; |
| 89914 | + pLevel->plan.u.pIdx = pIdx; |
| 89915 | + pIdx->azColl = (char**)&pIdx[1]; |
| 89916 | + pIdx->aiColumn = (int*)&pIdx->azColl[nColumn]; |
| 89917 | + pIdx->aSortOrder = (u8*)&pIdx->aiColumn[nColumn]; |
| 89918 | + pIdx->zName = "auto-index"; |
| 89919 | + pIdx->nColumn = nColumn; |
| 89920 | + pIdx->pTable = pTable; |
| 89921 | + n = 0; |
| 89922 | + idxCols = 0; |
| 89923 | + for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
| 89924 | + if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
| 89925 | + int iCol = pTerm->u.leftColumn; |
| 89926 | + Bitmask cMask = iCol>=BMS ? ((Bitmask)1)<<(BMS-1) : ((Bitmask)1)<<iCol; |
| 89927 | + if( (idxCols & cMask)==0 ){ |
| 89928 | + Expr *pX = pTerm->pExpr; |
| 89929 | + idxCols |= cMask; |
| 89930 | + pIdx->aiColumn[n] = pTerm->u.leftColumn; |
| 89931 | + pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight); |
| 89932 | + pIdx->azColl[n] = pColl->zName; |
| 89933 | + n++; |
| 89934 | + } |
| 89935 | + } |
| 89936 | + } |
| 89937 | + assert( n==pLevel->plan.nEq ); |
| 89938 | + |
| 89939 | + /* Add additional columns needed to make the automatic index into |
| 89940 | + ** a covering index */ |
| 89941 | + for(i=0; i<mxBitCol; i++){ |
| 89942 | + if( extraCols & (((Bitmask)1)<<i) ){ |
| 89943 | + pIdx->aiColumn[n] = i; |
| 89944 | + pIdx->azColl[n] = "BINARY"; |
| 89945 | + n++; |
| 89946 | + } |
| 89947 | + } |
| 89948 | + if( pSrc->colUsed & (((Bitmask)1)<<(BMS-1)) ){ |
| 89949 | + for(i=BMS-1; i<pTable->nCol; i++){ |
| 89950 | + pIdx->aiColumn[n] = i; |
| 89951 | + pIdx->azColl[n] = "BINARY"; |
| 89952 | + n++; |
| 89953 | + } |
| 89954 | + } |
| 89955 | + assert( n==nColumn ); |
| 89956 | + |
| 89957 | + /* Create the automatic index */ |
| 89958 | + pKeyinfo = sqlite3IndexKeyinfo(pParse, pIdx); |
| 89959 | + assert( pLevel->iIdxCur>=0 ); |
| 89960 | + sqlite3VdbeAddOp4(v, OP_OpenAutoindex, pLevel->iIdxCur, nColumn+1, 0, |
| 89961 | + (char*)pKeyinfo, P4_KEYINFO_HANDOFF); |
| 89962 | + VdbeComment((v, "for %s", pTable->zName)); |
| 89963 | + |
| 89964 | + /* Fill the automatic index with content */ |
| 89965 | + addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); |
| 89966 | + regRecord = sqlite3GetTempReg(pParse); |
| 89967 | + sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 1); |
| 89968 | + sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord); |
| 89969 | + sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 89970 | + sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); |
| 89971 | + sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX); |
| 89972 | + sqlite3VdbeJumpHere(v, addrTop); |
| 89973 | + sqlite3ReleaseTempReg(pParse, regRecord); |
| 89974 | + |
| 89975 | + /* Jump here when skipping the initialization */ |
| 89976 | + sqlite3VdbeJumpHere(v, addrInit); |
| 89977 | +} |
| 89978 | +#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ |
| 89979 | + |
| 89402 | 89980 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 89403 | 89981 | /* |
| 89404 | 89982 | ** Allocate and populate an sqlite3_index_info structure. It is the |
| 89405 | 89983 | ** responsibility of the caller to eventually release the structure |
| 89406 | 89984 | ** by passing the pointer returned by this function to sqlite3_free(). |
| | @@ -89581,10 +90159,11 @@ |
| 89581 | 90159 | struct sqlite3_index_constraint *pIdxCons; |
| 89582 | 90160 | struct sqlite3_index_constraint_usage *pUsage; |
| 89583 | 90161 | WhereTerm *pTerm; |
| 89584 | 90162 | int i, j; |
| 89585 | 90163 | int nOrderBy; |
| 90164 | + double rCost; |
| 89586 | 90165 | |
| 89587 | 90166 | /* Make sure wsFlags is initialized to some sane value. Otherwise, if the |
| 89588 | 90167 | ** malloc in allocateIndexInfo() fails and this function returns leaving |
| 89589 | 90168 | ** wsFlags in an uninitialized state, the caller may behave unpredictably. |
| 89590 | 90169 | */ |
| | @@ -89666,22 +90245,31 @@ |
| 89666 | 90245 | for(i=0; i<pIdxInfo->nConstraint; i++){ |
| 89667 | 90246 | if( pUsage[i].argvIndex>0 ){ |
| 89668 | 90247 | pCost->used |= pWC->a[pIdxCons[i].iTermOffset].prereqRight; |
| 89669 | 90248 | } |
| 89670 | 90249 | } |
| 90250 | + |
| 90251 | + /* If there is an ORDER BY clause, and the selected virtual table index |
| 90252 | + ** does not satisfy it, increase the cost of the scan accordingly. This |
| 90253 | + ** matches the processing for non-virtual tables in bestBtreeIndex(). |
| 90254 | + */ |
| 90255 | + rCost = pIdxInfo->estimatedCost; |
| 90256 | + if( pOrderBy && pIdxInfo->orderByConsumed==0 ){ |
| 90257 | + rCost += estLog(rCost)*rCost; |
| 90258 | + } |
| 89671 | 90259 | |
| 89672 | 90260 | /* The cost is not allowed to be larger than SQLITE_BIG_DBL (the |
| 89673 | 90261 | ** inital value of lowestCost in this loop. If it is, then the |
| 89674 | 90262 | ** (cost<lowestCost) test below will never be true. |
| 89675 | 90263 | ** |
| 89676 | 90264 | ** Use "(double)2" instead of "2.0" in case OMIT_FLOATING_POINT |
| 89677 | 90265 | ** is defined. |
| 89678 | 90266 | */ |
| 89679 | | - if( (SQLITE_BIG_DBL/((double)2))<pIdxInfo->estimatedCost ){ |
| 90267 | + if( (SQLITE_BIG_DBL/((double)2))<rCost ){ |
| 89680 | 90268 | pCost->rCost = (SQLITE_BIG_DBL/((double)2)); |
| 89681 | 90269 | }else{ |
| 89682 | | - pCost->rCost = pIdxInfo->estimatedCost; |
| 90270 | + pCost->rCost = rCost; |
| 89683 | 90271 | } |
| 89684 | 90272 | pCost->plan.u.pVtabIdx = pIdxInfo; |
| 89685 | 90273 | if( pIdxInfo->orderByConsumed ){ |
| 89686 | 90274 | pCost->plan.wsFlags |= WHERE_ORDERBY; |
| 89687 | 90275 | } |
| | @@ -90078,18 +90666,18 @@ |
| 90078 | 90666 | ** |
| 90079 | 90667 | ** bInEst: |
| 90080 | 90668 | ** Set to true if there was at least one "x IN (SELECT ...)" term used |
| 90081 | 90669 | ** in determining the value of nInMul. |
| 90082 | 90670 | ** |
| 90083 | | - ** nBound: |
| 90671 | + ** estBound: |
| 90084 | 90672 | ** An estimate on the amount of the table that must be searched. A |
| 90085 | 90673 | ** value of 100 means the entire table is searched. Range constraints |
| 90086 | 90674 | ** might reduce this to a value less than 100 to indicate that only |
| 90087 | 90675 | ** a fraction of the table needs searching. In the absence of |
| 90088 | 90676 | ** sqlite_stat2 ANALYZE data, a single inequality reduces the search |
| 90089 | 90677 | ** space to 1/3rd its original size. So an x>? constraint reduces |
| 90090 | | - ** nBound to 33. Two constraints (x>? AND x<?) reduce nBound to 11. |
| 90678 | + ** estBound to 33. Two constraints (x>? AND x<?) reduce estBound to 11. |
| 90091 | 90679 | ** |
| 90092 | 90680 | ** bSort: |
| 90093 | 90681 | ** Boolean. True if there is an ORDER BY clause that will require an |
| 90094 | 90682 | ** external sort (i.e. scanning the index being evaluated will not |
| 90095 | 90683 | ** correctly order records). |
| | @@ -90107,17 +90695,18 @@ |
| 90107 | 90695 | ** SELECT a, b, c FROM tbl WHERE a = 1; |
| 90108 | 90696 | */ |
| 90109 | 90697 | int nEq; |
| 90110 | 90698 | int bInEst = 0; |
| 90111 | 90699 | int nInMul = 1; |
| 90112 | | - int nBound = 100; |
| 90700 | + int estBound = 100; |
| 90701 | + int nBound = 0; /* Number of range constraints seen */ |
| 90113 | 90702 | int bSort = 0; |
| 90114 | 90703 | int bLookup = 0; |
| 90704 | + WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 90115 | 90705 | |
| 90116 | 90706 | /* Determine the values of nEq and nInMul */ |
| 90117 | 90707 | for(nEq=0; nEq<pProbe->nColumn; nEq++){ |
| 90118 | | - WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 90119 | 90708 | int j = pProbe->aiColumn[nEq]; |
| 90120 | 90709 | pTerm = findTerm(pWC, iCur, j, notReady, eqTermMask, pIdx); |
| 90121 | 90710 | if( pTerm==0 ) break; |
| 90122 | 90711 | wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ); |
| 90123 | 90712 | if( pTerm->eOperator & WO_IN ){ |
| | @@ -90133,22 +90722,24 @@ |
| 90133 | 90722 | wsFlags |= WHERE_COLUMN_NULL; |
| 90134 | 90723 | } |
| 90135 | 90724 | used |= pTerm->prereqRight; |
| 90136 | 90725 | } |
| 90137 | 90726 | |
| 90138 | | - /* Determine the value of nBound. */ |
| 90727 | + /* Determine the value of estBound. */ |
| 90139 | 90728 | if( nEq<pProbe->nColumn ){ |
| 90140 | 90729 | int j = pProbe->aiColumn[nEq]; |
| 90141 | 90730 | if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){ |
| 90142 | 90731 | WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx); |
| 90143 | 90732 | WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx); |
| 90144 | | - whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &nBound); |
| 90733 | + whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &estBound); |
| 90145 | 90734 | if( pTop ){ |
| 90735 | + nBound = 1; |
| 90146 | 90736 | wsFlags |= WHERE_TOP_LIMIT; |
| 90147 | 90737 | used |= pTop->prereqRight; |
| 90148 | 90738 | } |
| 90149 | 90739 | if( pBtm ){ |
| 90740 | + nBound++; |
| 90150 | 90741 | wsFlags |= WHERE_BTM_LIMIT; |
| 90151 | 90742 | used |= pBtm->prereqRight; |
| 90152 | 90743 | } |
| 90153 | 90744 | wsFlags |= (WHERE_COLUMN_RANGE|WHERE_ROWID_RANGE); |
| 90154 | 90745 | } |
| | @@ -90175,11 +90766,11 @@ |
| 90175 | 90766 | } |
| 90176 | 90767 | } |
| 90177 | 90768 | |
| 90178 | 90769 | /* If currently calculating the cost of using an index (not the IPK |
| 90179 | 90770 | ** index), determine if all required column data may be obtained without |
| 90180 | | - ** seeking to entries in the main table (i.e. if the index is a covering |
| 90771 | + ** using the main table (i.e. if the index is a covering |
| 90181 | 90772 | ** index for this query). If it is, set the WHERE_IDX_ONLY flag in |
| 90182 | 90773 | ** wsFlags. Otherwise, set the bLookup variable to true. */ |
| 90183 | 90774 | if( pIdx && wsFlags ){ |
| 90184 | 90775 | Bitmask m = pSrc->colUsed; |
| 90185 | 90776 | int j; |
| | @@ -90194,12 +90785,11 @@ |
| 90194 | 90785 | }else{ |
| 90195 | 90786 | bLookup = 1; |
| 90196 | 90787 | } |
| 90197 | 90788 | } |
| 90198 | 90789 | |
| 90199 | | - /**** Begin adding up the cost of using this index (Needs improvements) |
| 90200 | | - ** |
| 90790 | + /* |
| 90201 | 90791 | ** Estimate the number of rows of output. For an IN operator, |
| 90202 | 90792 | ** do not let the estimate exceed half the rows in the table. |
| 90203 | 90793 | */ |
| 90204 | 90794 | nRow = (double)(aiRowEst[nEq] * nInMul); |
| 90205 | 90795 | if( bInEst && nRow*2>aiRowEst[0] ){ |
| | @@ -90214,12 +90804,12 @@ |
| 90214 | 90804 | cost = nRow + nInMul*estLog(aiRowEst[0]); |
| 90215 | 90805 | |
| 90216 | 90806 | /* Adjust the number of rows and the cost downward to reflect rows |
| 90217 | 90807 | ** that are excluded by range constraints. |
| 90218 | 90808 | */ |
| 90219 | | - nRow = (nRow * (double)nBound) / (double)100; |
| 90220 | | - cost = (cost * (double)nBound) / (double)100; |
| 90809 | + nRow = (nRow * (double)estBound) / (double)100; |
| 90810 | + cost = (cost * (double)estBound) / (double)100; |
| 90221 | 90811 | |
| 90222 | 90812 | /* Add in the estimated cost of sorting the result |
| 90223 | 90813 | */ |
| 90224 | 90814 | if( bSort ){ |
| 90225 | 90815 | cost += cost*estLog(cost); |
| | @@ -90231,22 +90821,80 @@ |
| 90231 | 90821 | */ |
| 90232 | 90822 | if( pIdx && bLookup==0 ){ |
| 90233 | 90823 | cost /= (double)2; |
| 90234 | 90824 | } |
| 90235 | 90825 | /**** Cost of using this index has now been computed ****/ |
| 90826 | + |
| 90827 | + /* If there are additional constraints on this table that cannot |
| 90828 | + ** be used with the current index, but which might lower the number |
| 90829 | + ** of output rows, adjust the nRow value accordingly. This only |
| 90830 | + ** matters if the current index is the least costly, so do not bother |
| 90831 | + ** with this step if we already know this index will not be chosen. |
| 90832 | + ** Also, never reduce the output row count below 2 using this step. |
| 90833 | + ** |
| 90834 | + ** Do not reduce the output row count if pSrc is the only table that |
| 90835 | + ** is notReady; if notReady is a power of two. This will be the case |
| 90836 | + ** when the main sqlite3WhereBegin() loop is scanning for a table with |
| 90837 | + ** and "optimal" index, and on such a scan the output row count |
| 90838 | + ** reduction is not valid because it does not update the "pCost->used" |
| 90839 | + ** bitmap. The notReady bitmap will also be a power of two when we |
| 90840 | + ** are scanning for the last table in a 64-way join. We are willing |
| 90841 | + ** to bypass this optimization in that corner case. |
| 90842 | + */ |
| 90843 | + if( nRow>2 && cost<=pCost->rCost && (notReady & (notReady-1))!=0 ){ |
| 90844 | + int k; /* Loop counter */ |
| 90845 | + int nSkipEq = nEq; /* Number of == constraints to skip */ |
| 90846 | + int nSkipRange = nBound; /* Number of < constraints to skip */ |
| 90847 | + Bitmask thisTab; /* Bitmap for pSrc */ |
| 90848 | + |
| 90849 | + thisTab = getMask(pWC->pMaskSet, iCur); |
| 90850 | + for(pTerm=pWC->a, k=pWC->nTerm; nRow>2 && k; k--, pTerm++){ |
| 90851 | + if( pTerm->wtFlags & TERM_VIRTUAL ) continue; |
| 90852 | + if( (pTerm->prereqAll & notReady)!=thisTab ) continue; |
| 90853 | + if( pTerm->eOperator & (WO_EQ|WO_IN|WO_ISNULL) ){ |
| 90854 | + if( nSkipEq ){ |
| 90855 | + /* Ignore the first nEq equality matches since the index |
| 90856 | + ** has already accounted for these */ |
| 90857 | + nSkipEq--; |
| 90858 | + }else{ |
| 90859 | + /* Assume each additional equality match reduces the result |
| 90860 | + ** set size by a factor of 10 */ |
| 90861 | + nRow /= 10; |
| 90862 | + } |
| 90863 | + }else if( pTerm->eOperator & (WO_LT|WO_LE|WO_GT|WO_GE) ){ |
| 90864 | + if( nSkipRange ){ |
| 90865 | + /* Ignore the first nBound range constraints since the index |
| 90866 | + ** has already accounted for these */ |
| 90867 | + nSkipRange--; |
| 90868 | + }else{ |
| 90869 | + /* Assume each additional range constraint reduces the result |
| 90870 | + ** set size by a factor of 3 */ |
| 90871 | + nRow /= 3; |
| 90872 | + } |
| 90873 | + }else{ |
| 90874 | + /* Any other expression lowers the output row count by half */ |
| 90875 | + nRow /= 2; |
| 90876 | + } |
| 90877 | + } |
| 90878 | + if( nRow<2 ) nRow = 2; |
| 90879 | + } |
| 90880 | + |
| 90236 | 90881 | |
| 90237 | 90882 | WHERETRACE(( |
| 90238 | | - "tbl=%s idx=%s nEq=%d nInMul=%d nBound=%d bSort=%d bLookup=%d" |
| 90239 | | - " wsFlags=%d (nRow=%.2f cost=%.2f)\n", |
| 90883 | + "%s(%s): nEq=%d nInMul=%d estBound=%d bSort=%d bLookup=%d wsFlags=0x%x\n" |
| 90884 | + " notReady=0x%llx nRow=%.2f cost=%.2f used=0x%llx\n", |
| 90240 | 90885 | pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"), |
| 90241 | | - nEq, nInMul, nBound, bSort, bLookup, wsFlags, nRow, cost |
| 90886 | + nEq, nInMul, estBound, bSort, bLookup, wsFlags, |
| 90887 | + notReady, nRow, cost, used |
| 90242 | 90888 | )); |
| 90243 | 90889 | |
| 90244 | 90890 | /* If this index is the best we have seen so far, then record this |
| 90245 | 90891 | ** index and its cost in the pCost structure. |
| 90246 | 90892 | */ |
| 90247 | | - if( (!pIdx || wsFlags) && cost<pCost->rCost ){ |
| 90893 | + if( (!pIdx || wsFlags) |
| 90894 | + && (cost<pCost->rCost || (cost<=pCost->rCost && nRow<pCost->nRow)) |
| 90895 | + ){ |
| 90248 | 90896 | pCost->rCost = cost; |
| 90249 | 90897 | pCost->nRow = nRow; |
| 90250 | 90898 | pCost->used = used; |
| 90251 | 90899 | pCost->plan.wsFlags = (wsFlags&wsFlagMask); |
| 90252 | 90900 | pCost->plan.nEq = nEq; |
| | @@ -90277,14 +90925,16 @@ |
| 90277 | 90925 | || pCost->plan.u.pIdx==0 |
| 90278 | 90926 | || pCost->plan.u.pIdx==pSrc->pIndex |
| 90279 | 90927 | ); |
| 90280 | 90928 | |
| 90281 | 90929 | WHERETRACE(("best index is: %s\n", |
| 90282 | | - (pCost->plan.u.pIdx ? pCost->plan.u.pIdx->zName : "ipk") |
| 90930 | + ((pCost->plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ? "none" : |
| 90931 | + pCost->plan.u.pIdx ? pCost->plan.u.pIdx->zName : "ipk") |
| 90283 | 90932 | )); |
| 90284 | 90933 | |
| 90285 | 90934 | bestOrClauseIndex(pParse, pWC, pSrc, notReady, pOrderBy, pCost); |
| 90935 | + bestAutomaticIndex(pParse, pWC, pSrc, notReady, pCost); |
| 90286 | 90936 | pCost->plan.wsFlags |= eqTermMask; |
| 90287 | 90937 | } |
| 90288 | 90938 | |
| 90289 | 90939 | /* |
| 90290 | 90940 | ** Find the query plan for accessing table pSrc->pTab. Write the |
| | @@ -90750,11 +91400,15 @@ |
| 90750 | 91400 | } |
| 90751 | 91401 | start = sqlite3VdbeCurrentAddr(v); |
| 90752 | 91402 | pLevel->op = bRev ? OP_Prev : OP_Next; |
| 90753 | 91403 | pLevel->p1 = iCur; |
| 90754 | 91404 | pLevel->p2 = start; |
| 90755 | | - pLevel->p5 = (pStart==0 && pEnd==0) ?1:0; |
| 91405 | + if( pStart==0 && pEnd==0 ){ |
| 91406 | + pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 91407 | + }else{ |
| 91408 | + assert( pLevel->p5==0 ); |
| 91409 | + } |
| 90756 | 91410 | if( testOp!=OP_Noop ){ |
| 90757 | 91411 | iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse); |
| 90758 | 91412 | sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg); |
| 90759 | 91413 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
| 90760 | 91414 | sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg); |
| | @@ -91208,20 +91862,27 @@ |
| 91208 | 91862 | |
| 91209 | 91863 | /* |
| 91210 | 91864 | ** Free a WhereInfo structure |
| 91211 | 91865 | */ |
| 91212 | 91866 | static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){ |
| 91213 | | - if( pWInfo ){ |
| 91867 | + if( ALWAYS(pWInfo) ){ |
| 91214 | 91868 | int i; |
| 91215 | 91869 | for(i=0; i<pWInfo->nLevel; i++){ |
| 91216 | 91870 | sqlite3_index_info *pInfo = pWInfo->a[i].pIdxInfo; |
| 91217 | 91871 | if( pInfo ){ |
| 91218 | 91872 | /* assert( pInfo->needToFreeIdxStr==0 || db->mallocFailed ); */ |
| 91219 | 91873 | if( pInfo->needToFreeIdxStr ){ |
| 91220 | 91874 | sqlite3_free(pInfo->idxStr); |
| 91221 | 91875 | } |
| 91222 | 91876 | sqlite3DbFree(db, pInfo); |
| 91877 | + } |
| 91878 | + if( pWInfo->a[i].plan.wsFlags & WHERE_TEMP_INDEX ){ |
| 91879 | + Index *pIdx = pWInfo->a[i].plan.u.pIdx; |
| 91880 | + if( pIdx ){ |
| 91881 | + sqlite3DbFree(db, pIdx->zColAff); |
| 91882 | + sqlite3DbFree(db, pIdx); |
| 91883 | + } |
| 91223 | 91884 | } |
| 91224 | 91885 | } |
| 91225 | 91886 | whereClauseClear(pWInfo->pWC); |
| 91226 | 91887 | sqlite3DbFree(db, pWInfo); |
| 91227 | 91888 | } |
| | @@ -91338,10 +91999,11 @@ |
| 91338 | 91999 | sqlite3 *db; /* Database connection */ |
| 91339 | 92000 | |
| 91340 | 92001 | /* The number of tables in the FROM clause is limited by the number of |
| 91341 | 92002 | ** bits in a Bitmask |
| 91342 | 92003 | */ |
| 92004 | + testcase( pTabList->nSrc==BMS ); |
| 91343 | 92005 | if( pTabList->nSrc>BMS ){ |
| 91344 | 92006 | sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS); |
| 91345 | 92007 | return 0; |
| 91346 | 92008 | } |
| 91347 | 92009 | |
| | @@ -91365,18 +92027,21 @@ |
| 91365 | 92027 | nByteWInfo + |
| 91366 | 92028 | sizeof(WhereClause) + |
| 91367 | 92029 | sizeof(WhereMaskSet) |
| 91368 | 92030 | ); |
| 91369 | 92031 | if( db->mallocFailed ){ |
| 92032 | + sqlite3DbFree(db, pWInfo); |
| 92033 | + pWInfo = 0; |
| 91370 | 92034 | goto whereBeginError; |
| 91371 | 92035 | } |
| 91372 | 92036 | pWInfo->nLevel = nTabList; |
| 91373 | 92037 | pWInfo->pParse = pParse; |
| 91374 | 92038 | pWInfo->pTabList = pTabList; |
| 91375 | 92039 | pWInfo->iBreak = sqlite3VdbeMakeLabel(v); |
| 91376 | 92040 | pWInfo->pWC = pWC = (WhereClause *)&((u8 *)pWInfo)[nByteWInfo]; |
| 91377 | 92041 | pWInfo->wctrlFlags = wctrlFlags; |
| 92042 | + pWInfo->savedNQueryLoop = pParse->nQueryLoop; |
| 91378 | 92043 | pMaskSet = (WhereMaskSet*)&pWC[1]; |
| 91379 | 92044 | |
| 91380 | 92045 | /* Split the WHERE clause into separate subexpressions where each |
| 91381 | 92046 | ** subexpression is separated by an AND operator. |
| 91382 | 92047 | */ |
| | @@ -91474,24 +92139,29 @@ |
| 91474 | 92139 | |
| 91475 | 92140 | memset(&bestPlan, 0, sizeof(bestPlan)); |
| 91476 | 92141 | bestPlan.rCost = SQLITE_BIG_DBL; |
| 91477 | 92142 | |
| 91478 | 92143 | /* Loop through the remaining entries in the FROM clause to find the |
| 91479 | | - ** next nested loop. The FROM clause entries may be iterated through |
| 92144 | + ** next nested loop. The loop tests all FROM clause entries |
| 91480 | 92145 | ** either once or twice. |
| 91481 | 92146 | ** |
| 91482 | | - ** The first iteration, which is always performed, searches for the |
| 91483 | | - ** FROM clause entry that permits the lowest-cost, "optimal" scan. In |
| 92147 | + ** The first test is always performed if there are two or more entries |
| 92148 | + ** remaining and never performed if there is only one FROM clause entry |
| 92149 | + ** to choose from. The first test looks for an "optimal" scan. In |
| 91484 | 92150 | ** this context an optimal scan is one that uses the same strategy |
| 91485 | 92151 | ** for the given FROM clause entry as would be selected if the entry |
| 91486 | 92152 | ** were used as the innermost nested loop. In other words, a table |
| 91487 | 92153 | ** is chosen such that the cost of running that table cannot be reduced |
| 91488 | | - ** by waiting for other tables to run first. |
| 92154 | + ** by waiting for other tables to run first. This "optimal" test works |
| 92155 | + ** by first assuming that the FROM clause is on the inner loop and finding |
| 92156 | + ** its query plan, then checking to see if that query plan uses any |
| 92157 | + ** other FROM clause terms that are notReady. If no notReady terms are |
| 92158 | + ** used then the "optimal" query plan works. |
| 91489 | 92159 | ** |
| 91490 | | - ** The second iteration is only performed if no optimal scan strategies |
| 91491 | | - ** were found by the first. This iteration is used to search for the |
| 91492 | | - ** lowest cost scan overall. |
| 92160 | + ** The second loop iteration is only performed if no optimal scan |
| 92161 | + ** strategies were found by the first loop. This 2nd iteration is used to |
| 92162 | + ** search for the lowest cost scan overall. |
| 91493 | 92163 | ** |
| 91494 | 92164 | ** Previous versions of SQLite performed only the second iteration - |
| 91495 | 92165 | ** the next outermost loop was always that with the lowest overall |
| 91496 | 92166 | ** cost. However, this meant that SQLite could select the wrong plan |
| 91497 | 92167 | ** for scripts such as the following: |
| | @@ -91505,13 +92175,12 @@ |
| 91505 | 92175 | ** However, since the cost of a linear scan through table t2 is the same |
| 91506 | 92176 | ** as the cost of a linear scan through table t1, a simple greedy |
| 91507 | 92177 | ** algorithm may choose to use t2 for the outer loop, which is a much |
| 91508 | 92178 | ** costlier approach. |
| 91509 | 92179 | */ |
| 91510 | | - for(isOptimal=1; isOptimal>=0 && bestJ<0; isOptimal--){ |
| 91511 | | - Bitmask mask = (isOptimal ? 0 : notReady); |
| 91512 | | - assert( (nTabList-iFrom)>1 || isOptimal ); |
| 92180 | + for(isOptimal=(iFrom<nTabList-1); isOptimal>=0; isOptimal--){ |
| 92181 | + Bitmask mask; /* Mask of tables not yet ready */ |
| 91513 | 92182 | for(j=iFrom, pTabItem=&pTabList->a[j]; j<nTabList; j++, pTabItem++){ |
| 91514 | 92183 | int doNotReorder; /* True if this table should not be reordered */ |
| 91515 | 92184 | WhereCost sCost; /* Cost information from best[Virtual]Index() */ |
| 91516 | 92185 | ExprList *pOrderBy; /* ORDER BY clause for index to optimize */ |
| 91517 | 92186 | |
| | @@ -91520,10 +92189,11 @@ |
| 91520 | 92189 | m = getMask(pMaskSet, pTabItem->iCursor); |
| 91521 | 92190 | if( (m & notReady)==0 ){ |
| 91522 | 92191 | if( j==iFrom ) iFrom++; |
| 91523 | 92192 | continue; |
| 91524 | 92193 | } |
| 92194 | + mask = (isOptimal ? m : notReady); |
| 91525 | 92195 | pOrderBy = ((i==0 && ppOrderBy )?*ppOrderBy:0); |
| 91526 | 92196 | |
| 91527 | 92197 | assert( pTabItem->pTab ); |
| 91528 | 92198 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 91529 | 92199 | if( IsVirtual(pTabItem->pTab) ){ |
| | @@ -91535,12 +92205,15 @@ |
| 91535 | 92205 | bestBtreeIndex(pParse, pWC, pTabItem, mask, pOrderBy, &sCost); |
| 91536 | 92206 | } |
| 91537 | 92207 | assert( isOptimal || (sCost.used¬Ready)==0 ); |
| 91538 | 92208 | |
| 91539 | 92209 | if( (sCost.used¬Ready)==0 |
| 91540 | | - && (j==iFrom || sCost.rCost<bestPlan.rCost) |
| 92210 | + && (bestJ<0 || sCost.rCost<bestPlan.rCost |
| 92211 | + || (sCost.rCost<=bestPlan.rCost && sCost.nRow<bestPlan.nRow)) |
| 91541 | 92212 | ){ |
| 92213 | + WHERETRACE(("... best so far with cost=%g and nRow=%g\n", |
| 92214 | + sCost.rCost, sCost.nRow)); |
| 91542 | 92215 | bestPlan = sCost; |
| 91543 | 92216 | bestJ = j; |
| 91544 | 92217 | } |
| 91545 | 92218 | if( doNotReorder ) break; |
| 91546 | 92219 | } |
| | @@ -91552,17 +92225,20 @@ |
| 91552 | 92225 | if( (bestPlan.plan.wsFlags & WHERE_ORDERBY)!=0 ){ |
| 91553 | 92226 | *ppOrderBy = 0; |
| 91554 | 92227 | } |
| 91555 | 92228 | andFlags &= bestPlan.plan.wsFlags; |
| 91556 | 92229 | pLevel->plan = bestPlan.plan; |
| 91557 | | - if( bestPlan.plan.wsFlags & WHERE_INDEXED ){ |
| 92230 | + testcase( bestPlan.plan.wsFlags & WHERE_INDEXED ); |
| 92231 | + testcase( bestPlan.plan.wsFlags & WHERE_TEMP_INDEX ); |
| 92232 | + if( bestPlan.plan.wsFlags & (WHERE_INDEXED|WHERE_TEMP_INDEX) ){ |
| 91558 | 92233 | pLevel->iIdxCur = pParse->nTab++; |
| 91559 | 92234 | }else{ |
| 91560 | 92235 | pLevel->iIdxCur = -1; |
| 91561 | 92236 | } |
| 91562 | 92237 | notReady &= ~getMask(pMaskSet, pTabList->a[bestJ].iCursor); |
| 91563 | 92238 | pLevel->iFrom = (u8)bestJ; |
| 92239 | + if( bestPlan.nRow>=(double)1 ) pParse->nQueryLoop *= bestPlan.nRow; |
| 91564 | 92240 | |
| 91565 | 92241 | /* Check that if the table scanned by this loop iteration had an |
| 91566 | 92242 | ** INDEXED BY clause attached to it, that the named index is being |
| 91567 | 92243 | ** used for the scan. If not, then query compilation has failed. |
| 91568 | 92244 | ** Return an error. |
| | @@ -91605,10 +92281,11 @@ |
| 91605 | 92281 | |
| 91606 | 92282 | /* Open all tables in the pTabList and any indices selected for |
| 91607 | 92283 | ** searching those tables. |
| 91608 | 92284 | */ |
| 91609 | 92285 | sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */ |
| 92286 | + notReady = ~(Bitmask)0; |
| 91610 | 92287 | for(i=0, pLevel=pWInfo->a; i<nTabList; i++, pLevel++){ |
| 91611 | 92288 | Table *pTab; /* Table to open */ |
| 91612 | 92289 | int iDb; /* Index of database containing table/index */ |
| 91613 | 92290 | |
| 91614 | 92291 | #ifndef SQLITE_OMIT_EXPLAIN |
| | @@ -91617,11 +92294,13 @@ |
| 91617 | 92294 | struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom]; |
| 91618 | 92295 | zMsg = sqlite3MPrintf(db, "TABLE %s", pItem->zName); |
| 91619 | 92296 | if( pItem->zAlias ){ |
| 91620 | 92297 | zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias); |
| 91621 | 92298 | } |
| 91622 | | - if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){ |
| 92299 | + if( (pLevel->plan.wsFlags & WHERE_TEMP_INDEX)!=0 ){ |
| 92300 | + zMsg = sqlite3MAppendf(db, zMsg, "%s WITH AUTOMATIC INDEX", zMsg); |
| 92301 | + }else if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){ |
| 91623 | 92302 | zMsg = sqlite3MAppendf(db, zMsg, "%s WITH INDEX %s", |
| 91624 | 92303 | zMsg, pLevel->plan.u.pIdx->zName); |
| 91625 | 92304 | }else if( pLevel->plan.wsFlags & WHERE_MULTI_OR ){ |
| 91626 | 92305 | zMsg = sqlite3MAppendf(db, zMsg, "%s VIA MULTI-INDEX UNION", zMsg); |
| 91627 | 92306 | }else if( pLevel->plan.wsFlags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){ |
| | @@ -91640,12 +92319,15 @@ |
| 91640 | 92319 | sqlite3VdbeAddOp4(v, OP_Explain, i, pLevel->iFrom, 0, zMsg, P4_DYNAMIC); |
| 91641 | 92320 | } |
| 91642 | 92321 | #endif /* SQLITE_OMIT_EXPLAIN */ |
| 91643 | 92322 | pTabItem = &pTabList->a[pLevel->iFrom]; |
| 91644 | 92323 | pTab = pTabItem->pTab; |
| 92324 | + pLevel->iTabCur = pTabItem->iCursor; |
| 91645 | 92325 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 91646 | | - if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ) continue; |
| 92326 | + if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){ |
| 92327 | + /* Do nothing */ |
| 92328 | + }else |
| 91647 | 92329 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 91648 | 92330 | if( (pLevel->plan.wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
| 91649 | 92331 | const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); |
| 91650 | 92332 | int iCur = pTabItem->iCursor; |
| 91651 | 92333 | sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB); |
| | @@ -91653,10 +92335,12 @@ |
| 91653 | 92335 | #endif |
| 91654 | 92336 | if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0 |
| 91655 | 92337 | && (wctrlFlags & WHERE_OMIT_OPEN)==0 ){ |
| 91656 | 92338 | int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead; |
| 91657 | 92339 | sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op); |
| 92340 | + testcase( pTab->nCol==BMS-1 ); |
| 92341 | + testcase( pTab->nCol==BMS ); |
| 91658 | 92342 | if( !pWInfo->okOnePass && pTab->nCol<BMS ){ |
| 91659 | 92343 | Bitmask b = pTabItem->colUsed; |
| 91660 | 92344 | int n = 0; |
| 91661 | 92345 | for(; b; b=b>>1, n++){} |
| 91662 | 92346 | sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1, |
| | @@ -91664,11 +92348,15 @@ |
| 91664 | 92348 | assert( n<=pTab->nCol ); |
| 91665 | 92349 | } |
| 91666 | 92350 | }else{ |
| 91667 | 92351 | sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
| 91668 | 92352 | } |
| 91669 | | - pLevel->iTabCur = pTabItem->iCursor; |
| 92353 | +#ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 92354 | + if( (pLevel->plan.wsFlags & WHERE_TEMP_INDEX)!=0 ){ |
| 92355 | + constructAutomaticIndex(pParse, pWC, pTabItem, notReady, pLevel); |
| 92356 | + }else |
| 92357 | +#endif |
| 91670 | 92358 | if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){ |
| 91671 | 92359 | Index *pIx = pLevel->plan.u.pIdx; |
| 91672 | 92360 | KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx); |
| 91673 | 92361 | int iIdxCur = pLevel->iIdxCur; |
| 91674 | 92362 | assert( pIx->pSchema==pTab->pSchema ); |
| | @@ -91676,12 +92364,14 @@ |
| 91676 | 92364 | sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIx->tnum, iDb, |
| 91677 | 92365 | (char*)pKey, P4_KEYINFO_HANDOFF); |
| 91678 | 92366 | VdbeComment((v, "%s", pIx->zName)); |
| 91679 | 92367 | } |
| 91680 | 92368 | sqlite3CodeVerifySchema(pParse, iDb); |
| 92369 | + notReady &= ~getMask(pWC->pMaskSet, pTabItem->iCursor); |
| 91681 | 92370 | } |
| 91682 | 92371 | pWInfo->iTop = sqlite3VdbeCurrentAddr(v); |
| 92372 | + if( db->mallocFailed ) goto whereBeginError; |
| 91683 | 92373 | |
| 91684 | 92374 | /* Generate the code to do the search. Each iteration of the for |
| 91685 | 92375 | ** loop below generates code for a single nested loop of the VM |
| 91686 | 92376 | ** program. |
| 91687 | 92377 | */ |
| | @@ -91745,11 +92435,14 @@ |
| 91745 | 92435 | */ |
| 91746 | 92436 | return pWInfo; |
| 91747 | 92437 | |
| 91748 | 92438 | /* Jump here if malloc fails */ |
| 91749 | 92439 | whereBeginError: |
| 91750 | | - whereInfoFree(db, pWInfo); |
| 92440 | + if( pWInfo ){ |
| 92441 | + pParse->nQueryLoop = pWInfo->savedNQueryLoop; |
| 92442 | + whereInfoFree(db, pWInfo); |
| 92443 | + } |
| 91751 | 92444 | return 0; |
| 91752 | 92445 | } |
| 91753 | 92446 | |
| 91754 | 92447 | /* |
| 91755 | 92448 | ** Generate the end of the WHERE loop. See comments on |
| | @@ -91815,16 +92508,19 @@ |
| 91815 | 92508 | assert( pWInfo->nLevel==1 || pWInfo->nLevel==pTabList->nSrc ); |
| 91816 | 92509 | for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){ |
| 91817 | 92510 | struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom]; |
| 91818 | 92511 | Table *pTab = pTabItem->pTab; |
| 91819 | 92512 | assert( pTab!=0 ); |
| 91820 | | - if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ) continue; |
| 91821 | | - if( (pWInfo->wctrlFlags & WHERE_OMIT_CLOSE)==0 ){ |
| 91822 | | - if( !pWInfo->okOnePass && (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0 ){ |
| 92513 | + if( (pTab->tabFlags & TF_Ephemeral)==0 |
| 92514 | + && pTab->pSelect==0 |
| 92515 | + && (pWInfo->wctrlFlags & WHERE_OMIT_CLOSE)==0 |
| 92516 | + ){ |
| 92517 | + int ws = pLevel->plan.wsFlags; |
| 92518 | + if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){ |
| 91823 | 92519 | sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor); |
| 91824 | 92520 | } |
| 91825 | | - if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){ |
| 92521 | + if( (ws & WHERE_INDEXED)!=0 && (ws & WHERE_TEMP_INDEX)==0 ){ |
| 91826 | 92522 | sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur); |
| 91827 | 92523 | } |
| 91828 | 92524 | } |
| 91829 | 92525 | |
| 91830 | 92526 | /* If this scan uses an index, make code substitutions to read data |
| | @@ -91868,10 +92564,11 @@ |
| 91868 | 92564 | } |
| 91869 | 92565 | } |
| 91870 | 92566 | |
| 91871 | 92567 | /* Final cleanup |
| 91872 | 92568 | */ |
| 92569 | + pParse->nQueryLoop = pWInfo->savedNQueryLoop; |
| 91873 | 92570 | whereInfoFree(db, pWInfo); |
| 91874 | 92571 | return; |
| 91875 | 92572 | } |
| 91876 | 92573 | |
| 91877 | 92574 | /************** End of where.c ***********************************************/ |
| | @@ -98071,11 +98768,11 @@ |
| 98071 | 98768 | assert( sizeof(db->aLimit)==sizeof(aHardLimit) ); |
| 98072 | 98769 | memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit)); |
| 98073 | 98770 | db->autoCommit = 1; |
| 98074 | 98771 | db->nextAutovac = -1; |
| 98075 | 98772 | db->nextPagesize = 0; |
| 98076 | | - db->flags |= SQLITE_ShortColNames |
| 98773 | + db->flags |= SQLITE_ShortColNames | SQLITE_AutoIndex |
| 98077 | 98774 | #if SQLITE_DEFAULT_FILE_FORMAT<4 |
| 98078 | 98775 | | SQLITE_LegacyFileFmt |
| 98079 | 98776 | #endif |
| 98080 | 98777 | #ifdef SQLITE_ENABLE_LOAD_EXTENSION |
| 98081 | 98778 | | SQLITE_LoadExtension |
| | @@ -99199,11 +99896,11 @@ |
| 99199 | 99896 | ** and so on. |
| 99200 | 99897 | ** |
| 99201 | 99898 | ** This is similar in concept to how sqlite encodes "varints" but |
| 99202 | 99899 | ** the encoding is not the same. SQLite varints are big-endian |
| 99203 | 99900 | ** are are limited to 9 bytes in length whereas FTS3 varints are |
| 99204 | | -** little-endian and can be upt to 10 bytes in length (in theory). |
| 99901 | +** little-endian and can be up to 10 bytes in length (in theory). |
| 99205 | 99902 | ** |
| 99206 | 99903 | ** Example encodings: |
| 99207 | 99904 | ** |
| 99208 | 99905 | ** 1: 0x01 |
| 99209 | 99906 | ** 127: 0x7f |
| | @@ -99210,30 +99907,30 @@ |
| 99210 | 99907 | ** 128: 0x81 0x00 |
| 99211 | 99908 | ** |
| 99212 | 99909 | ** |
| 99213 | 99910 | **** Document lists **** |
| 99214 | 99911 | ** A doclist (document list) holds a docid-sorted list of hits for a |
| 99215 | | -** given term. Doclists hold docids, and can optionally associate |
| 99216 | | -** token positions and offsets with docids. A position is the index |
| 99217 | | -** of a word within the document. The first word of the document has |
| 99218 | | -** a position of 0. |
| 99912 | +** given term. Doclists hold docids and associated token positions. |
| 99913 | +** A docid is the unique integer identifier for a single document. |
| 99914 | +** A position is the index of a word within the document. The first |
| 99915 | +** word of the document has a position of 0. |
| 99219 | 99916 | ** |
| 99220 | 99917 | ** FTS3 used to optionally store character offsets using a compile-time |
| 99221 | 99918 | ** option. But that functionality is no longer supported. |
| 99222 | 99919 | ** |
| 99223 | | -** A DL_POSITIONS_OFFSETS doclist is stored like this: |
| 99920 | +** A doclist is stored like this: |
| 99224 | 99921 | ** |
| 99225 | 99922 | ** array { |
| 99226 | 99923 | ** varint docid; |
| 99227 | 99924 | ** array { (position list for column 0) |
| 99228 | | -** varint position; (delta from previous position plus POS_BASE) |
| 99925 | +** varint position; (2 more than the delta from previous position) |
| 99229 | 99926 | ** } |
| 99230 | 99927 | ** array { |
| 99231 | 99928 | ** varint POS_COLUMN; (marks start of position list for new column) |
| 99232 | 99929 | ** varint column; (index of new column) |
| 99233 | 99930 | ** array { |
| 99234 | | -** varint position; (delta from previous position plus POS_BASE) |
| 99931 | +** varint position; (2 more than the delta from previous position) |
| 99235 | 99932 | ** } |
| 99236 | 99933 | ** } |
| 99237 | 99934 | ** varint POS_END; (marks end of positions for this document. |
| 99238 | 99935 | ** } |
| 99239 | 99936 | ** |
| | @@ -99241,11 +99938,11 @@ |
| 99241 | 99938 | ** memory. A "position" is an index of a token in the token stream |
| 99242 | 99939 | ** generated by the tokenizer. Note that POS_END and POS_COLUMN occur |
| 99243 | 99940 | ** in the same logical place as the position element, and act as sentinals |
| 99244 | 99941 | ** ending a position list array. POS_END is 0. POS_COLUMN is 1. |
| 99245 | 99942 | ** The positions numbers are not stored literally but rather as two more |
| 99246 | | -** the difference from the prior position, or the just the position plus |
| 99943 | +** than the difference from the prior position, or the just the position plus |
| 99247 | 99944 | ** 2 for the first position. Example: |
| 99248 | 99945 | ** |
| 99249 | 99946 | ** label: A B C D E F G H I J K |
| 99250 | 99947 | ** value: 123 5 9 1 1 14 35 0 234 72 0 |
| 99251 | 99948 | ** |
| | @@ -99255,18 +99952,18 @@ |
| 99255 | 99952 | ** new column is column number 1. There are two positions at 12 and 45 |
| 99256 | 99953 | ** (14-2 and 35-2+12). The 0 at H indicate the end-of-document. The |
| 99257 | 99954 | ** 234 at I is the next docid. It has one position 72 (72-2) and then |
| 99258 | 99955 | ** terminates with the 0 at K. |
| 99259 | 99956 | ** |
| 99260 | | -** A DL_POSITIONS doclist omits the startOffset and endOffset |
| 99261 | | -** information. A DL_DOCIDS doclist omits both the position and |
| 99262 | | -** offset information, becoming an array of varint-encoded docids. |
| 99263 | | -** |
| 99264 | | -** On-disk data is stored as type DL_DEFAULT, so we don't serialize |
| 99265 | | -** the type. Due to how deletion is implemented in the segmentation |
| 99266 | | -** system, on-disk doclists MUST store at least positions. |
| 99267 | | -** |
| 99957 | +** A "position-list" is the list of positions for multiple columns for |
| 99958 | +** a single docid. A "column-list" is the set of positions for a single |
| 99959 | +** column. Hence, a position-list consists of one or more column-lists, |
| 99960 | +** a document record consists of a docid followed by a position-list and |
| 99961 | +** a doclist consists of one or more document records. |
| 99962 | +** |
| 99963 | +** A bare doclist omits the position information, becoming an |
| 99964 | +** array of varint-encoded docids. |
| 99268 | 99965 | ** |
| 99269 | 99966 | **** Segment leaf nodes **** |
| 99270 | 99967 | ** Segment leaf nodes store terms and doclists, ordered by term. Leaf |
| 99271 | 99968 | ** nodes are written using LeafWriter, and read using LeafReader (to |
| 99272 | 99969 | ** iterate through a single leaf node's data) and LeavesReader (to |
| | @@ -99776,10 +100473,24 @@ |
| 99776 | 100473 | ** Maximum length of a varint encoded integer. The varint format is different |
| 99777 | 100474 | ** from that used by SQLite, so the maximum length is 10, not 9. |
| 99778 | 100475 | */ |
| 99779 | 100476 | #define FTS3_VARINT_MAX 10 |
| 99780 | 100477 | |
| 100478 | +/* |
| 100479 | +** The testcase() macro is only used by the amalgamation. If undefined, |
| 100480 | +** make it a no-op. |
| 100481 | +*/ |
| 100482 | +#ifndef testcase |
| 100483 | +# define testcase(X) |
| 100484 | +#endif |
| 100485 | + |
| 100486 | +/* |
| 100487 | +** Terminator values for position-lists and column-lists. |
| 100488 | +*/ |
| 100489 | +#define POS_COLUMN (1) /* Column-list terminator */ |
| 100490 | +#define POS_END (0) /* Position-list terminator */ |
| 100491 | + |
| 99781 | 100492 | /* |
| 99782 | 100493 | ** This section provides definitions to allow the |
| 99783 | 100494 | ** FTS3 extension to be compiled outside of the |
| 99784 | 100495 | ** amalgamation. |
| 99785 | 100496 | */ |
| | @@ -100087,12 +100798,11 @@ |
| 100087 | 100798 | *pi = (int) i; |
| 100088 | 100799 | return ret; |
| 100089 | 100800 | } |
| 100090 | 100801 | |
| 100091 | 100802 | /* |
| 100092 | | -** Return the number of bytes required to store the value passed as the |
| 100093 | | -** first argument in varint form. |
| 100803 | +** Return the number of bytes required to encode v as a varint |
| 100094 | 100804 | */ |
| 100095 | 100805 | SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64 v){ |
| 100096 | 100806 | int i = 0; |
| 100097 | 100807 | do{ |
| 100098 | 100808 | i++; |
| | @@ -100139,11 +100849,11 @@ |
| 100139 | 100849 | } |
| 100140 | 100850 | } |
| 100141 | 100851 | |
| 100142 | 100852 | /* |
| 100143 | 100853 | ** Read a single varint from the doclist at *pp and advance *pp to point |
| 100144 | | -** to the next element of the varlist. Add the value of the varint |
| 100854 | +** to the first byte past the end of the varint. Add the value of the varint |
| 100145 | 100855 | ** to *pVal. |
| 100146 | 100856 | */ |
| 100147 | 100857 | static void fts3GetDeltaVarint(char **pp, sqlite3_int64 *pVal){ |
| 100148 | 100858 | sqlite3_int64 iVal; |
| 100149 | 100859 | *pp += sqlite3Fts3GetVarint(*pp, &iVal); |
| | @@ -100195,11 +100905,11 @@ |
| 100195 | 100905 | ** and then evaluate those statements. The success code is writting |
| 100196 | 100906 | ** into *pRc. |
| 100197 | 100907 | ** |
| 100198 | 100908 | ** If *pRc is initially non-zero then this routine is a no-op. |
| 100199 | 100909 | */ |
| 100200 | | -void fts3DbExec( |
| 100910 | +static void fts3DbExec( |
| 100201 | 100911 | int *pRc, /* Success code */ |
| 100202 | 100912 | sqlite3 *db, /* Database in which to run SQL */ |
| 100203 | 100913 | const char *zFormat, /* Format string for SQL */ |
| 100204 | 100914 | ... /* Arguments to the format string */ |
| 100205 | 100915 | ){ |
| | @@ -100275,10 +100985,14 @@ |
| 100275 | 100985 | |
| 100276 | 100986 | /* |
| 100277 | 100987 | ** Create the backing store tables (%_content, %_segments and %_segdir) |
| 100278 | 100988 | ** required by the FTS3 table passed as the only argument. This is done |
| 100279 | 100989 | ** as part of the vtab xCreate() method. |
| 100990 | +** |
| 100991 | +** If the p->bHasDocsize boolean is true (indicating that this is an |
| 100992 | +** FTS4 table, not an FTS3 table) then also create the %_docsize and |
| 100993 | +** %_stat tables required by FTS4. |
| 100280 | 100994 | */ |
| 100281 | 100995 | static int fts3CreateTables(Fts3Table *p){ |
| 100282 | 100996 | int rc = SQLITE_OK; /* Return code */ |
| 100283 | 100997 | int i; /* Iterator variable */ |
| 100284 | 100998 | char *zContentCols; /* Columns of %_content table */ |
| | @@ -100367,11 +101081,11 @@ |
| 100367 | 101081 | ** This function is the implementation of both the xConnect and xCreate |
| 100368 | 101082 | ** methods of the FTS3 virtual table. |
| 100369 | 101083 | ** |
| 100370 | 101084 | ** The argv[] array contains the following: |
| 100371 | 101085 | ** |
| 100372 | | -** argv[0] -> module name |
| 101086 | +** argv[0] -> module name ("fts3" or "fts4") |
| 100373 | 101087 | ** argv[1] -> database name |
| 100374 | 101088 | ** argv[2] -> table name |
| 100375 | 101089 | ** argv[...] -> "column name" and other module argument fields. |
| 100376 | 101090 | */ |
| 100377 | 101091 | static int fts3InitVtab( |
| | @@ -100386,16 +101100,16 @@ |
| 100386 | 101100 | Fts3Hash *pHash = (Fts3Hash *)pAux; |
| 100387 | 101101 | Fts3Table *p; /* Pointer to allocated vtab */ |
| 100388 | 101102 | int rc; /* Return code */ |
| 100389 | 101103 | int i; /* Iterator variable */ |
| 100390 | 101104 | int nByte; /* Size of allocation used for *p */ |
| 100391 | | - int iCol; |
| 100392 | | - int nString = 0; |
| 100393 | | - int nCol = 0; |
| 100394 | | - char *zCsr; |
| 100395 | | - int nDb; |
| 100396 | | - int nName; |
| 101105 | + int iCol; /* Column index */ |
| 101106 | + int nString = 0; /* Bytes required to hold all column names */ |
| 101107 | + int nCol = 0; /* Number of columns in the FTS table */ |
| 101108 | + char *zCsr; /* Space for holding column names */ |
| 101109 | + int nDb; /* Bytes required to hold database name */ |
| 101110 | + int nName; /* Bytes required to hold table name */ |
| 100397 | 101111 | |
| 100398 | 101112 | const char *zTokenizer = 0; /* Name of tokenizer to use */ |
| 100399 | 101113 | sqlite3_tokenizer *pTokenizer = 0; /* Tokenizer for this table */ |
| 100400 | 101114 | |
| 100401 | 101115 | nDb = (int)strlen(argv[1]) + 1; |
| | @@ -100621,10 +101335,15 @@ |
| 100621 | 101335 | sqlite3_free(pCsr->aMatchinfo); |
| 100622 | 101336 | sqlite3_free(pCsr); |
| 100623 | 101337 | return SQLITE_OK; |
| 100624 | 101338 | } |
| 100625 | 101339 | |
| 101340 | +/* |
| 101341 | +** Position the pCsr->pStmt statement so that it is on the row |
| 101342 | +** of the %_content table that contains the last match. Return |
| 101343 | +** SQLITE_OK on success. |
| 101344 | +*/ |
| 100626 | 101345 | static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){ |
| 100627 | 101346 | if( pCsr->isRequireSeek ){ |
| 100628 | 101347 | pCsr->isRequireSeek = 0; |
| 100629 | 101348 | sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId); |
| 100630 | 101349 | if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){ |
| | @@ -100647,10 +101366,21 @@ |
| 100647 | 101366 | }else{ |
| 100648 | 101367 | return SQLITE_OK; |
| 100649 | 101368 | } |
| 100650 | 101369 | } |
| 100651 | 101370 | |
| 101371 | +/* |
| 101372 | +** Advance the cursor to the next row in the %_content table that |
| 101373 | +** matches the search criteria. For a MATCH search, this will be |
| 101374 | +** the next row that matches. For a full-table scan, this will be |
| 101375 | +** simply the next row in the %_content table. For a docid lookup, |
| 101376 | +** this routine simply sets the EOF flag. |
| 101377 | +** |
| 101378 | +** Return SQLITE_OK if nothing goes wrong. SQLITE_OK is returned |
| 101379 | +** even if we reach end-of-file. The fts3EofMethod() will be called |
| 101380 | +** subsequently to determine whether or not an EOF was hit. |
| 101381 | +*/ |
| 100652 | 101382 | static int fts3NextMethod(sqlite3_vtab_cursor *pCursor){ |
| 100653 | 101383 | int rc = SQLITE_OK; /* Return code */ |
| 100654 | 101384 | Fts3Cursor *pCsr = (Fts3Cursor *)pCursor; |
| 100655 | 101385 | |
| 100656 | 101386 | if( pCsr->aDoclist==0 ){ |
| | @@ -100782,10 +101512,15 @@ |
| 100782 | 101512 | |
| 100783 | 101513 | /* |
| 100784 | 101514 | ** When this function is called, *ppPoslist is assumed to point to the |
| 100785 | 101515 | ** start of a position-list. After it returns, *ppPoslist points to the |
| 100786 | 101516 | ** first byte after the position-list. |
| 101517 | +** |
| 101518 | +** A position list is list of positions (delta encoded) and columns for |
| 101519 | +** a single document record of a doclist. So, in other words, this |
| 101520 | +** routine advances *ppPoslist so that it points to the next docid in |
| 101521 | +** the doclist, or to the first byte past the end of the doclist. |
| 100787 | 101522 | ** |
| 100788 | 101523 | ** If pp is not NULL, then the contents of the position list are copied |
| 100789 | 101524 | ** to *pp. *pp is set to point to the first byte past the last byte copied |
| 100790 | 101525 | ** before this function returns. |
| 100791 | 101526 | */ |
| | @@ -100792,21 +101527,24 @@ |
| 100792 | 101527 | static void fts3PoslistCopy(char **pp, char **ppPoslist){ |
| 100793 | 101528 | char *pEnd = *ppPoslist; |
| 100794 | 101529 | char c = 0; |
| 100795 | 101530 | |
| 100796 | 101531 | /* The end of a position list is marked by a zero encoded as an FTS3 |
| 100797 | | - ** varint. A single 0x00 byte. Except, if the 0x00 byte is preceded by |
| 101532 | + ** varint. A single POS_END (0) byte. Except, if the 0 byte is preceded by |
| 100798 | 101533 | ** a byte with the 0x80 bit set, then it is not a varint 0, but the tail |
| 100799 | 101534 | ** of some other, multi-byte, value. |
| 100800 | 101535 | ** |
| 100801 | | - ** The following block moves pEnd to point to the first byte that is not |
| 101536 | + ** The following while-loop moves pEnd to point to the first byte that is not |
| 100802 | 101537 | ** immediately preceded by a byte with the 0x80 bit set. Then increments |
| 100803 | 101538 | ** pEnd once more so that it points to the byte immediately following the |
| 100804 | 101539 | ** last byte in the position-list. |
| 100805 | 101540 | */ |
| 100806 | | - while( *pEnd | c ) c = *pEnd++ & 0x80; |
| 100807 | | - pEnd++; |
| 101541 | + while( *pEnd | c ){ |
| 101542 | + c = *pEnd++ & 0x80; |
| 101543 | + testcase( c!=0 && (*pEnd)==0 ); |
| 101544 | + } |
| 101545 | + pEnd++; /* Advance past the POS_END terminator byte */ |
| 100808 | 101546 | |
| 100809 | 101547 | if( pp ){ |
| 100810 | 101548 | int n = (int)(pEnd - *ppPoslist); |
| 100811 | 101549 | char *p = *pp; |
| 100812 | 101550 | memcpy(p, *ppPoslist, n); |
| | @@ -100814,16 +101552,38 @@ |
| 100814 | 101552 | *pp = p; |
| 100815 | 101553 | } |
| 100816 | 101554 | *ppPoslist = pEnd; |
| 100817 | 101555 | } |
| 100818 | 101556 | |
| 101557 | +/* |
| 101558 | +** When this function is called, *ppPoslist is assumed to point to the |
| 101559 | +** start of a column-list. After it returns, *ppPoslist points to the |
| 101560 | +** to the terminator (POS_COLUMN or POS_END) byte of the column-list. |
| 101561 | +** |
| 101562 | +** A column-list is list of delta-encoded positions for a single column |
| 101563 | +** within a single document within a doclist. |
| 101564 | +** |
| 101565 | +** The column-list is terminated either by a POS_COLUMN varint (1) or |
| 101566 | +** a POS_END varint (0). This routine leaves *ppPoslist pointing to |
| 101567 | +** the POS_COLUMN or POS_END that terminates the column-list. |
| 101568 | +** |
| 101569 | +** If pp is not NULL, then the contents of the column-list are copied |
| 101570 | +** to *pp. *pp is set to point to the first byte past the last byte copied |
| 101571 | +** before this function returns. The POS_COLUMN or POS_END terminator |
| 101572 | +** is not copied into *pp. |
| 101573 | +*/ |
| 100819 | 101574 | static void fts3ColumnlistCopy(char **pp, char **ppPoslist){ |
| 100820 | 101575 | char *pEnd = *ppPoslist; |
| 100821 | 101576 | char c = 0; |
| 100822 | 101577 | |
| 100823 | | - /* A column-list is terminated by either a 0x01 or 0x00. */ |
| 100824 | | - while( 0xFE & (*pEnd | c) ) c = *pEnd++ & 0x80; |
| 101578 | + /* A column-list is terminated by either a 0x01 or 0x00 byte that is |
| 101579 | + ** not part of a multi-byte varint. |
| 101580 | + */ |
| 101581 | + while( 0xFE & (*pEnd | c) ){ |
| 101582 | + c = *pEnd++ & 0x80; |
| 101583 | + testcase( c!=0 && ((*pEnd)&0xfe)==0 ); |
| 101584 | + } |
| 100825 | 101585 | if( pp ){ |
| 100826 | 101586 | int n = (int)(pEnd - *ppPoslist); |
| 100827 | 101587 | char *p = *pp; |
| 100828 | 101588 | memcpy(p, *ppPoslist, n); |
| 100829 | 101589 | p += n; |
| | @@ -100831,41 +101591,49 @@ |
| 100831 | 101591 | } |
| 100832 | 101592 | *ppPoslist = pEnd; |
| 100833 | 101593 | } |
| 100834 | 101594 | |
| 100835 | 101595 | /* |
| 100836 | | -** Value used to signify the end of an offset-list. This is safe because |
| 101596 | +** Value used to signify the end of an position-list. This is safe because |
| 100837 | 101597 | ** it is not possible to have a document with 2^31 terms. |
| 100838 | 101598 | */ |
| 100839 | | -#define OFFSET_LIST_END 0x7fffffff |
| 101599 | +#define POSITION_LIST_END 0x7fffffff |
| 100840 | 101600 | |
| 100841 | 101601 | /* |
| 100842 | | -** This function is used to help parse offset-lists. When this function is |
| 100843 | | -** called, *pp may point to the start of the next varint in the offset-list |
| 100844 | | -** being parsed, or it may point to 1 byte past the end of the offset-list |
| 100845 | | -** (in which case **pp will be 0x00 or 0x01). |
| 101602 | +** This function is used to help parse position-lists. When this function is |
| 101603 | +** called, *pp may point to the start of the next varint in the position-list |
| 101604 | +** being parsed, or it may point to 1 byte past the end of the position-list |
| 101605 | +** (in which case **pp will be a terminator bytes POS_END (0) or |
| 101606 | +** (1)). |
| 100846 | 101607 | ** |
| 100847 | | -** If *pp points past the end of the current offset list, set *pi to |
| 100848 | | -** OFFSET_LIST_END and return. Otherwise, read the next varint from *pp, |
| 101608 | +** If *pp points past the end of the current position-list, set *pi to |
| 101609 | +** POSITION_LIST_END and return. Otherwise, read the next varint from *pp, |
| 100849 | 101610 | ** increment the current value of *pi by the value read, and set *pp to |
| 100850 | 101611 | ** point to the next value before returning. |
| 101612 | +** |
| 101613 | +** Before calling this routine *pi must be initialized to the value of |
| 101614 | +** the previous position, or zero if we are reading the first position |
| 101615 | +** in the position-list. Because positions are delta-encoded, the value |
| 101616 | +** of the previous position is needed in order to compute the value of |
| 101617 | +** the next position. |
| 100851 | 101618 | */ |
| 100852 | 101619 | static void fts3ReadNextPos( |
| 100853 | | - char **pp, /* IN/OUT: Pointer into offset-list buffer */ |
| 100854 | | - sqlite3_int64 *pi /* IN/OUT: Value read from offset-list */ |
| 101620 | + char **pp, /* IN/OUT: Pointer into position-list buffer */ |
| 101621 | + sqlite3_int64 *pi /* IN/OUT: Value read from position-list */ |
| 100855 | 101622 | ){ |
| 100856 | | - if( **pp&0xFE ){ |
| 101623 | + if( (**pp)&0xFE ){ |
| 100857 | 101624 | fts3GetDeltaVarint(pp, pi); |
| 100858 | 101625 | *pi -= 2; |
| 100859 | 101626 | }else{ |
| 100860 | | - *pi = OFFSET_LIST_END; |
| 101627 | + *pi = POSITION_LIST_END; |
| 100861 | 101628 | } |
| 100862 | 101629 | } |
| 100863 | 101630 | |
| 100864 | 101631 | /* |
| 100865 | | -** If parameter iCol is not 0, write an 0x01 byte followed by the value of |
| 100866 | | -** iCol encoded as a varint to *pp. |
| 101632 | +** If parameter iCol is not 0, write an POS_COLUMN (1) byte followed by |
| 101633 | +** the value of iCol encoded as a varint to *pp. This will start a new |
| 101634 | +** column list. |
| 100867 | 101635 | ** |
| 100868 | 101636 | ** Set *pp to point to the byte just after the last byte written before |
| 100869 | 101637 | ** returning (do not modify it if iCol==0). Return the total number of bytes |
| 100870 | 101638 | ** written (0 if iCol==0). |
| 100871 | 101639 | */ |
| | @@ -100879,11 +101647,15 @@ |
| 100879 | 101647 | } |
| 100880 | 101648 | return n; |
| 100881 | 101649 | } |
| 100882 | 101650 | |
| 100883 | 101651 | /* |
| 100884 | | -** |
| 101652 | +** Compute the union of two position lists. The output written |
| 101653 | +** into *pp contains all positions of both *pp1 and *pp2 in sorted |
| 101654 | +** order and with any duplicates removed. All pointers are |
| 101655 | +** updated appropriately. The caller is responsible for insuring |
| 101656 | +** that there is enough space in *pp to hold the complete output. |
| 100885 | 101657 | */ |
| 100886 | 101658 | static void fts3PoslistMerge( |
| 100887 | 101659 | char **pp, /* Output buffer */ |
| 100888 | 101660 | char **pp1, /* Left input list */ |
| 100889 | 101661 | char **pp2 /* Right input list */ |
| | @@ -100891,36 +101663,37 @@ |
| 100891 | 101663 | char *p = *pp; |
| 100892 | 101664 | char *p1 = *pp1; |
| 100893 | 101665 | char *p2 = *pp2; |
| 100894 | 101666 | |
| 100895 | 101667 | while( *p1 || *p2 ){ |
| 100896 | | - int iCol1; |
| 100897 | | - int iCol2; |
| 101668 | + int iCol1; /* The current column index in pp1 */ |
| 101669 | + int iCol2; /* The current column index in pp2 */ |
| 100898 | 101670 | |
| 100899 | | - if( *p1==0x01 ) sqlite3Fts3GetVarint32(&p1[1], &iCol1); |
| 100900 | | - else if( *p1==0x00 ) iCol1 = OFFSET_LIST_END; |
| 101671 | + if( *p1==POS_COLUMN ) sqlite3Fts3GetVarint32(&p1[1], &iCol1); |
| 101672 | + else if( *p1==POS_END ) iCol1 = POSITION_LIST_END; |
| 100901 | 101673 | else iCol1 = 0; |
| 100902 | 101674 | |
| 100903 | | - if( *p2==0x01 ) sqlite3Fts3GetVarint32(&p2[1], &iCol2); |
| 100904 | | - else if( *p2==0x00 ) iCol2 = OFFSET_LIST_END; |
| 101675 | + if( *p2==POS_COLUMN ) sqlite3Fts3GetVarint32(&p2[1], &iCol2); |
| 101676 | + else if( *p2==POS_END ) iCol2 = POSITION_LIST_END; |
| 100905 | 101677 | else iCol2 = 0; |
| 100906 | 101678 | |
| 100907 | 101679 | if( iCol1==iCol2 ){ |
| 100908 | | - sqlite3_int64 i1 = 0; |
| 100909 | | - sqlite3_int64 i2 = 0; |
| 101680 | + sqlite3_int64 i1 = 0; /* Last position from pp1 */ |
| 101681 | + sqlite3_int64 i2 = 0; /* Last position from pp2 */ |
| 100910 | 101682 | sqlite3_int64 iPrev = 0; |
| 100911 | 101683 | int n = fts3PutColNumber(&p, iCol1); |
| 100912 | 101684 | p1 += n; |
| 100913 | 101685 | p2 += n; |
| 100914 | 101686 | |
| 100915 | | - /* At this point, both p1 and p2 point to the start of offset-lists. |
| 100916 | | - ** An offset-list is a list of non-negative delta-encoded varints, each |
| 100917 | | - ** incremented by 2 before being stored. Each list is terminated by a 0 |
| 100918 | | - ** or 1 value (0x00 or 0x01). The following block merges the two lists |
| 101687 | + /* At this point, both p1 and p2 point to the start of column-lists |
| 101688 | + ** for the same column (the column with index iCol1 and iCol2). |
| 101689 | + ** A column-list is a list of non-negative delta-encoded varints, each |
| 101690 | + ** incremented by 2 before being stored. Each list is terminated by a |
| 101691 | + ** POS_END (0) or POS_COLUMN (1). The following block merges the two lists |
| 100919 | 101692 | ** and writes the results to buffer p. p is left pointing to the byte |
| 100920 | | - ** after the list written. No terminator (0x00 or 0x01) is written to |
| 100921 | | - ** the output. |
| 101693 | + ** after the list written. No terminator (POS_END or POS_COLUMN) is |
| 101694 | + ** written to the output. |
| 100922 | 101695 | */ |
| 100923 | 101696 | fts3GetDeltaVarint(&p1, &i1); |
| 100924 | 101697 | fts3GetDeltaVarint(&p2, &i2); |
| 100925 | 101698 | do { |
| 100926 | 101699 | fts3PutDeltaVarint(&p, &iPrev, (i1<i2) ? i1 : i2); |
| | @@ -100931,21 +101704,21 @@ |
| 100931 | 101704 | }else if( i1<i2 ){ |
| 100932 | 101705 | fts3ReadNextPos(&p1, &i1); |
| 100933 | 101706 | }else{ |
| 100934 | 101707 | fts3ReadNextPos(&p2, &i2); |
| 100935 | 101708 | } |
| 100936 | | - }while( i1!=OFFSET_LIST_END || i2!=OFFSET_LIST_END ); |
| 101709 | + }while( i1!=POSITION_LIST_END || i2!=POSITION_LIST_END ); |
| 100937 | 101710 | }else if( iCol1<iCol2 ){ |
| 100938 | 101711 | p1 += fts3PutColNumber(&p, iCol1); |
| 100939 | 101712 | fts3ColumnlistCopy(&p, &p1); |
| 100940 | 101713 | }else{ |
| 100941 | 101714 | p2 += fts3PutColNumber(&p, iCol2); |
| 100942 | 101715 | fts3ColumnlistCopy(&p, &p2); |
| 100943 | 101716 | } |
| 100944 | 101717 | } |
| 100945 | 101718 | |
| 100946 | | - *p++ = '\0'; |
| 101719 | + *p++ = POS_END; |
| 100947 | 101720 | *pp = p; |
| 100948 | 101721 | *pp1 = p1 + 1; |
| 100949 | 101722 | *pp2 = p2 + 1; |
| 100950 | 101723 | } |
| 100951 | 101724 | |
| | @@ -100964,15 +101737,15 @@ |
| 100964 | 101737 | char *p2 = *pp2; |
| 100965 | 101738 | |
| 100966 | 101739 | int iCol1 = 0; |
| 100967 | 101740 | int iCol2 = 0; |
| 100968 | 101741 | assert( *p1!=0 && *p2!=0 ); |
| 100969 | | - if( *p1==0x01 ){ |
| 101742 | + if( *p1==POS_COLUMN ){ |
| 100970 | 101743 | p1++; |
| 100971 | 101744 | p1 += sqlite3Fts3GetVarint32(p1, &iCol1); |
| 100972 | 101745 | } |
| 100973 | | - if( *p2==0x01 ){ |
| 101746 | + if( *p2==POS_COLUMN ){ |
| 100974 | 101747 | p2++; |
| 100975 | 101748 | p2 += sqlite3Fts3GetVarint32(p2, &iCol2); |
| 100976 | 101749 | } |
| 100977 | 101750 | |
| 100978 | 101751 | while( 1 ){ |
| | @@ -100981,15 +101754,16 @@ |
| 100981 | 101754 | sqlite3_int64 iPrev = 0; |
| 100982 | 101755 | sqlite3_int64 iPos1 = 0; |
| 100983 | 101756 | sqlite3_int64 iPos2 = 0; |
| 100984 | 101757 | |
| 100985 | 101758 | if( pp && iCol1 ){ |
| 100986 | | - *p++ = 0x01; |
| 101759 | + *p++ = POS_COLUMN; |
| 100987 | 101760 | p += sqlite3Fts3PutVarint(p, iCol1); |
| 100988 | 101761 | } |
| 100989 | 101762 | |
| 100990 | | - assert( *p1!=0x00 && *p2!=0x00 && *p1!=0x01 && *p2!=0x01 ); |
| 101763 | + assert( *p1!=POS_END && *p1!=POS_COLUMN ); |
| 101764 | + assert( *p2!=POS_END && *p2!=POS_COLUMN ); |
| 100991 | 101765 | fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2; |
| 100992 | 101766 | fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2; |
| 100993 | 101767 | |
| 100994 | 101768 | while( 1 ){ |
| 100995 | 101769 | if( iPos2>iPos1 && iPos2<=iPos1+nToken ){ |
| | @@ -101237,10 +102011,11 @@ |
| 101237 | 102011 | } |
| 101238 | 102012 | |
| 101239 | 102013 | default: assert( mergetype==MERGE_POS_NEAR || mergetype==MERGE_NEAR ); { |
| 101240 | 102014 | char *aTmp = 0; |
| 101241 | 102015 | char **ppPos = 0; |
| 102016 | + |
| 101242 | 102017 | if( mergetype==MERGE_POS_NEAR ){ |
| 101243 | 102018 | ppPos = &p; |
| 101244 | 102019 | aTmp = sqlite3_malloc(2*(n1+n2+1)); |
| 101245 | 102020 | if( !aTmp ){ |
| 101246 | 102021 | return SQLITE_NOMEM; |
| | @@ -101341,13 +102116,13 @@ |
| 101341 | 102116 | ** This function retreives the doclist for the specified term (or term |
| 101342 | 102117 | ** prefix) from the database. |
| 101343 | 102118 | ** |
| 101344 | 102119 | ** The returned doclist may be in one of two formats, depending on the |
| 101345 | 102120 | ** value of parameter isReqPos. If isReqPos is zero, then the doclist is |
| 101346 | | -** a sorted list of delta-compressed docids. If isReqPos is non-zero, |
| 101347 | | -** then the returned list is in the same format as is stored in the |
| 101348 | | -** database without the found length specifier at the start of on-disk |
| 102121 | +** a sorted list of delta-compressed docids (a bare doclist). If isReqPos |
| 102122 | +** is non-zero, then the returned list is in the same format as is stored |
| 102123 | +** in the database without the found length specifier at the start of on-disk |
| 101349 | 102124 | ** doclists. |
| 101350 | 102125 | */ |
| 101351 | 102126 | static int fts3TermSelect( |
| 101352 | 102127 | Fts3Table *p, /* Virtual table handle */ |
| 101353 | 102128 | int iColumn, /* Column to query (or -ve for all columns) */ |
| | @@ -101603,11 +102378,13 @@ |
| 101603 | 102378 | return rc; |
| 101604 | 102379 | } |
| 101605 | 102380 | |
| 101606 | 102381 | /* |
| 101607 | 102382 | ** Evaluate the full-text expression pExpr against fts3 table pTab. Store |
| 101608 | | -** the resulting doclist in *paOut and *pnOut. |
| 102383 | +** the resulting doclist in *paOut and *pnOut. This routine mallocs for |
| 102384 | +** the space needed to store the output. The caller is responsible for |
| 102385 | +** freeing the space when it has finished. |
| 101609 | 102386 | */ |
| 101610 | 102387 | static int evalFts3Expr( |
| 101611 | 102388 | Fts3Table *p, /* Virtual table handle */ |
| 101612 | 102389 | Fts3Expr *pExpr, /* Parsed fts3 expression */ |
| 101613 | 102390 | char **paOut, /* OUT: Pointer to malloc'd result buffer */ |
| | @@ -108163,11 +108940,11 @@ |
| 108163 | 108940 | ** This is done as part of extracting the snippet text, not when selecting |
| 108164 | 108941 | ** the snippet. Snippet selection is done based on doclists only, so there |
| 108165 | 108942 | ** is no way for fts3BestSnippet() to know whether or not the document |
| 108166 | 108943 | ** actually contains terms that follow the final highlighted term. |
| 108167 | 108944 | */ |
| 108168 | | -int fts3SnippetShift( |
| 108945 | +static int fts3SnippetShift( |
| 108169 | 108946 | Fts3Table *pTab, /* FTS3 table snippet comes from */ |
| 108170 | 108947 | int nSnippet, /* Number of tokens desired for snippet */ |
| 108171 | 108948 | const char *zDoc, /* Document text to extract snippet from */ |
| 108172 | 108949 | int nDoc, /* Size of buffer zDoc in bytes */ |
| 108173 | 108950 | int *piPos, /* IN/OUT: First token of snippet */ |
| | @@ -111280,10 +112057,11 @@ |
| 111280 | 112057 | rc = SQLITE_CONSTRAINT; |
| 111281 | 112058 | goto constraint; |
| 111282 | 112059 | } |
| 111283 | 112060 | rc = sqlite3_reset(pRtree->pReadRowid); |
| 111284 | 112061 | } |
| 112062 | + *pRowid = cell.iRowid; |
| 111285 | 112063 | |
| 111286 | 112064 | if( rc==SQLITE_OK ){ |
| 111287 | 112065 | rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf); |
| 111288 | 112066 | } |
| 111289 | 112067 | if( rc==SQLITE_OK ){ |
| 111290 | 112068 | |