📄 capi3ref.html
字号:
part of system initialization by the sqlite3_initialize() function. The xMutexInit routine shall be called by SQLite once for eacheffective call to <a href="#sqlite3_initialize">sqlite3_initialize()</a>.</p><p>The xMutexEnd method defined by this structure is invoked aspart of system shutdown by the sqlite3_shutdown() function. Theimplementation of this method is expected to release all outstandingresources obtained by the mutex methods implementation, especiallythose obtained by the xMutexInit method. The xMutexEnd()interface shall be invoked once for each call to <a href="#sqlite3_initialize">sqlite3_shutdown()</a>.</p><p>The remaining seven methods defined by this structure (xMutexAlloc,xMutexFree, xMutexEnter, xMutexTry, xMutexLeave, xMutexHeld andxMutexNotheld) implement the following interfaces (respectively):</p><p><ul><li> <a href="#sqlite3_mutex_alloc">sqlite3_mutex_alloc()</a> </li><li> <a href="#sqlite3_mutex_alloc">sqlite3_mutex_free()</a> </li><li> <a href="#sqlite3_mutex_alloc">sqlite3_mutex_enter()</a> </li><li> <a href="#sqlite3_mutex_alloc">sqlite3_mutex_try()</a> </li><li> <a href="#sqlite3_mutex_alloc">sqlite3_mutex_leave()</a> </li><li> <a href="#sqlite3_mutex_held">sqlite3_mutex_held()</a> </li><li> <a href="#sqlite3_mutex_held">sqlite3_mutex_notheld()</a> </li></ul></p><p>The only difference is that the public sqlite3_XXX functions enumeratedabove silently ignore any invocations that pass a NULL pointer insteadof a valid mutex handle. The implementations of the methods definedby this structure are not required to handle this case, the resultsof passing a NULL pointer instead of a valid mutex handle are undefined(i.e. it is acceptable to provide an implementation that segfaults ifit is passed a NULL pointer).</p><hr><a name="sqlite3_pcache"></a><h2>Custom Page Cache Object</h2><blockquote><pre>typedef struct sqlite3_pcache sqlite3_pcache;</pre></blockquote><p><b>Important:</b> This interface is <a href="capi3ref.html">experimental</a> and is subject to change without notice.</p><p>The sqlite3_pcache type is opaque. It is implemented bythe pluggable module. The SQLite core has no knowledge ofits size or internal structure and never deals with thesqlite3_pcache object except by holding and passing pointersto the object.</p><p>See <a href="#sqlite3_pcache_methods">sqlite3_pcache_methods</a> for additional information.</p><hr><a name="sqlite3_pcache_methods"></a><h2>Application Defined Page Cache.</h2><blockquote><pre>typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;struct sqlite3_pcache_methods { void *pArg; int (*xInit)(void*); void (*xShutdown)(void*); sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable); void (*xCachesize)(sqlite3_pcache*, int nCachesize); int (*xPagecount)(sqlite3_pcache*); void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag); void (*xUnpin)(sqlite3_pcache*, void*, int discard); void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey); void (*xTruncate)(sqlite3_pcache*, unsigned iLimit); void (*xDestroy)(sqlite3_pcache*);};</pre></blockquote><p><b>Important:</b> This interface is <a href="capi3ref.html">experimental</a> and is subject to change without notice.</p><p>The <a href="#sqlite3_config">sqlite3_config</a>(<a href="#SQLITE_CONFIG_GETMALLOC">SQLITE_CONFIG_PCACHE</a>, ...) interface canregister an alternative page cache implementation by passing in aninstance of the sqlite3_pcache_methods structure. The majority of theheap memory used by sqlite is used by the page cache to cache data readfrom, or ready to be written to, the database file. By implementing acustom page cache using this API, an application can control moreprecisely the amount of memory consumed by sqlite, the way in whichsaid memory is allocated and released, and the policies used todetermine exactly which parts of a database file are cached and forhow long.</p><p>The contents of the structure are copied to an internal buffer by sqlitewithin the call to <a href="#sqlite3_config">sqlite3_config</a>.</p><p>The xInit() method is called once for each call to <a href="#sqlite3_initialize">sqlite3_initialize()</a>(usually only once during the lifetime of the process). It is passeda copy of the sqlite3_pcache_methods.pArg value. It can be used to setup global structures and mutexes required by the custom page cacheimplementation. The xShutdown() method is called from within<a href="#sqlite3_initialize">sqlite3_shutdown()</a>, if the application invokes this API. It can be usedto clean up any outstanding resources before process shutdown, if required.</p><p>The xCreate() method is used to construct a new cache instance. Thefirst parameter, szPage, is the size in bytes of the pages that mustbe allocated by the cache. szPage will not be a power of two. Thesecond argument, bPurgeable, is true if the cache being created willbe used to cache database pages read from a file stored on disk, orfalse if it is used for an in-memory database. The cache implementationdoes not have to do anything special based on the value of bPurgeable,it is purely advisory.</p><p>The xCachesize() method may be called at any time by SQLite to set thesuggested maximum cache-size (number of pages stored by) the cacheinstance passed as the first argument. This is the value configured usingthe SQLite "<a href="pragma.html#pragma_cache_size">PRAGMA cache_size</a>" command. As with the bPurgeable parameter,the implementation is not required to do anything special with thisvalue, it is advisory only.</p><p>The xPagecount() method should return the number of pages currentlystored in the cache supplied as an argument.</p><p>The xFetch() method is used to fetch a page and return a pointer to it.A 'page', in this context, is a buffer of szPage bytes aligned at an8-byte boundary. The page to be fetched is determined by the key. Themimimum key value is 1. After it has been retrieved using xFetch, the pageis considered to be pinned.</p><p>If the requested page is already in the page cache, then a pointer tothe cached buffer should be returned with its contents intact. If thepage is not already in the cache, then the expected behaviour of thecache is determined by the value of the createFlag parameter passedto xFetch, according to the following table:</p><p><table border=1 width=85% align=center><tr><th>createFlag<th>Expected Behaviour<tr><td>0<td>NULL should be returned. No new cache entry is created.<tr><td>1<td>If createFlag is set to 1, this indicates thatSQLite is holding pinned pages that can be unpinnedby writing their contents to the database file (arelatively expensive operation). In this situation thecache implementation has two choices: it can return NULL,in which case SQLite will attempt to unpin one or morepages before re-requesting the same page, or it canallocate a new page and return a pointer to it. If a newpage is allocated, then it must be completely zeroed beforeit is returned.<tr><td>2<td>If createFlag is set to 2, then SQLite is not holding anypinned pages associated with the specific cache passedas the first argument to xFetch() that can be unpinned. Thecache implementation should attempt to allocate a newcache entry and return a pointer to it. Again, the newpage should be zeroed before it is returned. If the xFetch()method returns NULL when createFlag==2, SQLite assumes thata memory allocation failed and returns SQLITE_NOMEM to theuser.</table></p><p>xUnpin() is called by SQLite with a pointer to a currently pinned pageas its second argument. If the third parameter, discard, is non-zero,then the page should be evicted from the cache. In this case SQLiteassumes that the next time the page is retrieved from the cache usingthe xFetch() method, it will be zeroed. If the discard parameter iszero, then the page is considered to be unpinned. The cache implementationmay choose to reclaim (free or recycle) unpinned pages at any time.SQLite assumes that next time the page is retrieved from the cacheit will either be zeroed, or contain the same data that it did when itwas unpinned.</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -