1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-07-29 08:01:23 +03:00

Change RTREE so that the sqlite3_blob object is closed whenever the cursor

count drops to zero and there is not a pending write transaction.

FossilOrigin-Name: 9bb4eafe1a60176ed2e731bb7e3067c0b8a46615
This commit is contained in:
drh
2017-02-02 14:40:06 +00:00
parent 6d683c5c6e
commit 2033d1c8ca
4 changed files with 28 additions and 19 deletions

View File

@ -119,11 +119,13 @@ struct Rtree {
u8 nDim2; /* Twice the number of dimensions */ u8 nDim2; /* Twice the number of dimensions */
u8 eCoordType; /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */ u8 eCoordType; /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */
u8 nBytesPerCell; /* Bytes consumed per cell */ u8 nBytesPerCell; /* Bytes consumed per cell */
u8 inWrTrans; /* True if inside write transaction */
int iDepth; /* Current depth of the r-tree structure */ int iDepth; /* Current depth of the r-tree structure */
char *zDb; /* Name of database containing r-tree table */ char *zDb; /* Name of database containing r-tree table */
char *zName; /* Name of r-tree table */ char *zName; /* Name of r-tree table */
int nBusy; /* Current number of users of this structure */ u32 nBusy; /* Current number of users of this structure */
i64 nRowEst; /* Estimated number of rows in this table */ i64 nRowEst; /* Estimated number of rows in this table */
u32 nCursor; /* Number of open cursors */
/* List of nodes removed during a CondenseTree operation. List is /* List of nodes removed during a CondenseTree operation. List is
** linked together via the pointer normally used for hash chains - ** linked together via the pointer normally used for hash chains -
@ -622,7 +624,7 @@ static RtreeNode *nodeNew(Rtree *pRtree, RtreeNode *pParent){
** Clear the Rtree.pNodeBlob object ** Clear the Rtree.pNodeBlob object
*/ */
static void nodeBlobReset(Rtree *pRtree){ static void nodeBlobReset(Rtree *pRtree){
if( pRtree->pNodeBlob ){ if( pRtree->pNodeBlob && pRtree->inWrTrans==0 && pRtree->nCursor==0 ){
sqlite3_blob_close(pRtree->pNodeBlob); sqlite3_blob_close(pRtree->pNodeBlob);
pRtree->pNodeBlob = 0; pRtree->pNodeBlob = 0;
} }
@ -661,7 +663,7 @@ static int nodeAcquire(
pRtree->pNodeBlob = pBlob; pRtree->pNodeBlob = pBlob;
if( rc ){ if( rc ){
nodeBlobReset(pRtree); nodeBlobReset(pRtree);
if( rc==SQLITE_NOMEM ) return rc; if( rc==SQLITE_NOMEM ) return SQLITE_NOMEM;
} }
} }
if( pRtree->pNodeBlob==0 ){ if( pRtree->pNodeBlob==0 ){
@ -674,6 +676,9 @@ static int nodeAcquire(
if( rc ){ if( rc ){
nodeBlobReset(pRtree); nodeBlobReset(pRtree);
*ppNode = 0; *ppNode = 0;
/* If unable to open an sqlite3_blob on the desired row, that can only
** be because the shadow tables hold erroneous data. */
if( rc==SQLITE_ERROR ) rc = SQLITE_CORRUPT_VTAB;
}else if( pRtree->iNodeSize==sqlite3_blob_bytes(pRtree->pNodeBlob) ){ }else if( pRtree->iNodeSize==sqlite3_blob_bytes(pRtree->pNodeBlob) ){
pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize); pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize);
if( !pNode ){ if( !pNode ){
@ -935,6 +940,8 @@ static void rtreeReference(Rtree *pRtree){
static void rtreeRelease(Rtree *pRtree){ static void rtreeRelease(Rtree *pRtree){
pRtree->nBusy--; pRtree->nBusy--;
if( pRtree->nBusy==0 ){ if( pRtree->nBusy==0 ){
pRtree->inWrTrans = 0;
pRtree->nCursor = 0;
nodeBlobReset(pRtree); nodeBlobReset(pRtree);
sqlite3_finalize(pRtree->pReadNode); sqlite3_finalize(pRtree->pReadNode);
sqlite3_finalize(pRtree->pWriteNode); sqlite3_finalize(pRtree->pWriteNode);
@ -990,6 +997,7 @@ static int rtreeDestroy(sqlite3_vtab *pVtab){
*/ */
static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
int rc = SQLITE_NOMEM; int rc = SQLITE_NOMEM;
Rtree *pRtree = (Rtree *)pVTab;
RtreeCursor *pCsr; RtreeCursor *pCsr;
pCsr = (RtreeCursor *)sqlite3_malloc(sizeof(RtreeCursor)); pCsr = (RtreeCursor *)sqlite3_malloc(sizeof(RtreeCursor));
@ -997,6 +1005,7 @@ static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
memset(pCsr, 0, sizeof(RtreeCursor)); memset(pCsr, 0, sizeof(RtreeCursor));
pCsr->base.pVtab = pVTab; pCsr->base.pVtab = pVTab;
rc = SQLITE_OK; rc = SQLITE_OK;
pRtree->nCursor++;
} }
*ppCursor = (sqlite3_vtab_cursor *)pCsr; *ppCursor = (sqlite3_vtab_cursor *)pCsr;
@ -1029,10 +1038,13 @@ static int rtreeClose(sqlite3_vtab_cursor *cur){
Rtree *pRtree = (Rtree *)(cur->pVtab); Rtree *pRtree = (Rtree *)(cur->pVtab);
int ii; int ii;
RtreeCursor *pCsr = (RtreeCursor *)cur; RtreeCursor *pCsr = (RtreeCursor *)cur;
assert( pRtree->nCursor>0 );
freeCursorConstraints(pCsr); freeCursorConstraints(pCsr);
sqlite3_free(pCsr->aPoint); sqlite3_free(pCsr->aPoint);
for(ii=0; ii<RTREE_CACHE_SZ; ii++) nodeRelease(pRtree, pCsr->aNode[ii]); for(ii=0; ii<RTREE_CACHE_SZ; ii++) nodeRelease(pRtree, pCsr->aNode[ii]);
sqlite3_free(pCsr); sqlite3_free(pCsr);
pRtree->nCursor--;
nodeBlobReset(pRtree);
return SQLITE_OK; return SQLITE_OK;
} }
@ -3182,12 +3194,11 @@ constraint:
/* /*
** Called when a transaction starts. ** Called when a transaction starts.
** This is a no-op. But the Virtual Table mechanism needs a method
** here or else it will never call the xRollback and xCommit methods,
** and those methods are necessary for clearing the sqlite3_blob object.
*/ */
static int rtreeBeginTransaction(sqlite3_vtab *pVtab){ static int rtreeBeginTransaction(sqlite3_vtab *pVtab){
(void)pVtab; Rtree *pRtree = (Rtree *)pVtab;
assert( pRtree->inWrTrans==0 );
pRtree->inWrTrans++;
return SQLITE_OK; return SQLITE_OK;
} }
@ -3197,6 +3208,7 @@ static int rtreeBeginTransaction(sqlite3_vtab *pVtab){
*/ */
static int rtreeEndTransaction(sqlite3_vtab *pVtab){ static int rtreeEndTransaction(sqlite3_vtab *pVtab){
Rtree *pRtree = (Rtree *)pVtab; Rtree *pRtree = (Rtree *)pVtab;
pRtree->inWrTrans = 0;
nodeBlobReset(pRtree); nodeBlobReset(pRtree);
return SQLITE_OK; return SQLITE_OK;
} }

View File

@ -109,7 +109,7 @@ do_corruption_tests rtreeA-1.1 {
} }
do_execsql_test rtreeA-1.2.0 { DROP TABLE t1_node } {} do_execsql_test rtreeA-1.2.0 { DROP TABLE t1_node } {}
do_corruption_tests rtreeA-1.2 -error "SQL logic error or missing database" { do_corruption_tests rtreeA-1.2 -error "database disk image is malformed" {
1 "SELECT * FROM t1" 1 "SELECT * FROM t1"
2 "SELECT * FROM t1 WHERE rowid=5" 2 "SELECT * FROM t1 WHERE rowid=5"
3 "INSERT INTO t1 VALUES(1000, 1, 2, 3, 4)" 3 "INSERT INTO t1 VALUES(1000, 1, 2, 3, 4)"

View File

@ -1,5 +1,5 @@
C Use\sthe\ssqlite3_blob\sinterface\sfor\sreading\svalues\sfrom\sthe\s%_node\sshadow\ntable\sin\sRTREE.\s\sThis\sis\sa\swork\sin\sprogress.\s\sThere\sare\sstill\ssome\sminor\nproblems. C Change\sRTREE\sso\sthat\sthe\ssqlite3_blob\sobject\sis\sclosed\swhenever\sthe\scursor\ncount\sdrops\sto\szero\sand\sthere\sis\snot\sa\spending\swrite\stransaction.
D 2017-02-02T02:28:45.543 D 2017-02-02T14:40:06.876
F Makefile.in 5f415e7867296d678fed2e6779aea10c1318b4bc F Makefile.in 5f415e7867296d678fed2e6779aea10c1318b4bc
F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434
F Makefile.msc ba953c8921fc7e18333f61898007206de7e23964 F Makefile.msc ba953c8921fc7e18333f61898007206de7e23964
@ -264,7 +264,7 @@ F ext/rbu/sqlite3rbu.c bb0de6cdbdb14a7d55a097238a434b7e99caf318
F ext/rbu/sqlite3rbu.h 6fb6294c34a9ca93b5894a33bca530c6f08decba F ext/rbu/sqlite3rbu.h 6fb6294c34a9ca93b5894a33bca530c6f08decba
F ext/rbu/test_rbu.c 5aa22616afac6f71ebd3d9bc9bf1006cfabcca88 F ext/rbu/test_rbu.c 5aa22616afac6f71ebd3d9bc9bf1006cfabcca88
F ext/rtree/README 6315c0d73ebf0ec40dedb5aa0e942bc8b54e3761 F ext/rtree/README 6315c0d73ebf0ec40dedb5aa0e942bc8b54e3761
F ext/rtree/rtree.c 73c4308585c47a7500b9e98617e45a62f8564ddb F ext/rtree/rtree.c df33d86df608a16516a95c6d184a5ed9988d2bc9
F ext/rtree/rtree.h 834dbcb82dc85b2481cde6a07cdadfddc99e9b9e F ext/rtree/rtree.h 834dbcb82dc85b2481cde6a07cdadfddc99e9b9e
F ext/rtree/rtree1.test 42dadfc7b44a436cd74a1bebc0b9b689e4eaf7ec F ext/rtree/rtree1.test 42dadfc7b44a436cd74a1bebc0b9b689e4eaf7ec
F ext/rtree/rtree2.test acbb3a4ce0f4fbc2c304d2b4b784cfa161856bba F ext/rtree/rtree2.test acbb3a4ce0f4fbc2c304d2b4b784cfa161856bba
@ -275,7 +275,7 @@ F ext/rtree/rtree6.test 773a90db2dce6a8353dd0d5b64bca69b29761196
F ext/rtree/rtree7.test 1fa710b9e6bf997a0c1a537b81be7bb6fded1971 F ext/rtree/rtree7.test 1fa710b9e6bf997a0c1a537b81be7bb6fded1971
F ext/rtree/rtree8.test db79c812f9e4a11f9b1f3f9934007884610a713a F ext/rtree/rtree8.test db79c812f9e4a11f9b1f3f9934007884610a713a
F ext/rtree/rtree9.test b5eb13849545dfd271a54ff16784cb00d8792aea F ext/rtree/rtree9.test b5eb13849545dfd271a54ff16784cb00d8792aea
F ext/rtree/rtreeA.test ace05e729a36e342d40cf94e9efc7b4723d9dcdf F ext/rtree/rtreeA.test ac8b503931f2f397cc3c3303354e1e085dcadb86
F ext/rtree/rtreeB.test c85f9ce78766c4e68b8b89fbf2979ee9cfa82b4e F ext/rtree/rtreeB.test c85f9ce78766c4e68b8b89fbf2979ee9cfa82b4e
F ext/rtree/rtreeC.test c0a9c67f2efa98b6fae12acb8a28348d231a481d F ext/rtree/rtreeC.test c0a9c67f2efa98b6fae12acb8a28348d231a481d
F ext/rtree/rtreeD.test bdfaaf26df8b4eea7364039aca9150bc1e1f8825 F ext/rtree/rtreeD.test bdfaaf26df8b4eea7364039aca9150bc1e1f8825
@ -1552,10 +1552,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P 01d97e5b6502b1811b52a681f445e1aaae6c0ee6 P fc4917d730b29b0bf60fea5e0166728635783e9c
R 79c2c34c9473af6d4e0161547969b398 R 0b53bd94b2e2abfad755fb806046ae11
T *branch * rtree-sqlite3_blob
T *sym-rtree-sqlite3_blob *
T -sym-savepoint-rollback *
U drh U drh
Z fc394298cc4191cf41d9c752fa550c9c Z 9c59d9d407f6159f87ae8b113cdc33c2

View File

@ -1 +1 @@
fc4917d730b29b0bf60fea5e0166728635783e9c 9bb4eafe1a60176ed2e731bb7e3067c0b8a46615