/ Check-in [4590e433]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Enable the use of shared cache for an in-memory database, so that separate database connections can share the same in-memory database.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | shared-cache-memdb
Files: files | file ages | folders
SHA1: 4590e433f2a595bb80fb061024b0a3d2ca25b7b2
User & Date: drh 2012-05-26 18:06:38
Context
2012-05-26
20:08
Only allow :memory: databases to share cache if there are created using a URI filename. This minimizes the risk of breakages in legacy applications that have shared-cache enabled but also use :memory: databases which they expect to keep separate. check-in: e3ad61e0 user: drh tags: shared-cache-memdb
18:06
Enable the use of shared cache for an in-memory database, so that separate database connections can share the same in-memory database. check-in: 4590e433 user: drh tags: shared-cache-memdb
2012-05-22
02:45
Version 3.7.12.1 check-in: 6d326d44 user: drh tags: trunk, release, version-3.7.12.1
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/btree.c.

  1753   1753   #endif
  1754   1754   
  1755   1755   #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
  1756   1756     /*
  1757   1757     ** If this Btree is a candidate for shared cache, try to find an
  1758   1758     ** existing BtShared object that we can share with
  1759   1759     */
  1760         -  if( isMemdb==0 && isTempDb==0 ){
         1760  +  if( isTempDb==0 ){
  1761   1761       if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){
  1762   1762         int nFullPathname = pVfs->mxPathname+1;
  1763   1763         char *zFullPathname = sqlite3Malloc(nFullPathname);
  1764   1764         MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
  1765   1765         p->sharable = 1;
  1766   1766         if( !zFullPathname ){
  1767   1767           sqlite3_free(p);
  1768   1768           return SQLITE_NOMEM;
  1769   1769         }
  1770         -      rc = sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname);
  1771         -      if( rc ){
  1772         -        sqlite3_free(zFullPathname);
  1773         -        sqlite3_free(p);
  1774         -        return rc;
         1770  +      if( isMemdb ){
         1771  +        memcpy(zFullPathname, zFilename, sqlite3Strlen30(zFilename)+1);
         1772  +      }else{
         1773  +        rc = sqlite3OsFullPathname(pVfs, zFilename,
         1774  +                                   nFullPathname, zFullPathname);
         1775  +        if( rc ){
         1776  +          sqlite3_free(zFullPathname);
         1777  +          sqlite3_free(p);
         1778  +          return rc;
         1779  +        }
  1775   1780         }
  1776   1781   #if SQLITE_THREADSAFE
  1777   1782         mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN);
  1778   1783         sqlite3_mutex_enter(mutexOpen);
  1779   1784         mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
  1780   1785         sqlite3_mutex_enter(mutexShared);
  1781   1786   #endif

Changes to src/pager.c.

  4356   4356   
  4357   4357     /* Set the output variable to NULL in case an error occurs. */
  4358   4358     *ppPager = 0;
  4359   4359   
  4360   4360   #ifndef SQLITE_OMIT_MEMORYDB
  4361   4361     if( flags & PAGER_MEMORY ){
  4362   4362       memDb = 1;
         4363  +    zPathname = sqlite3DbStrDup(0, zFilename);
         4364  +    nPathname = sqlite3Strlen30(zPathname);
  4363   4365       zFilename = 0;
  4364   4366     }
  4365   4367   #endif
  4366   4368   
  4367   4369     /* Compute and store the full pathname in an allocated buffer pointed
  4368   4370     ** to by zPathname, length nPathname. Or, if this is a temporary file,
  4369   4371     ** leave both nPathname and zPathname set to 0.
................................................................................
  6739   6741   
  6740   6742   /*
  6741   6743   ** Return true if the underlying VFS for the given pager supports the
  6742   6744   ** primitives necessary for write-ahead logging.
  6743   6745   */
  6744   6746   int sqlite3PagerWalSupported(Pager *pPager){
  6745   6747     const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
  6746         -  return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
         6748  +  return pPager->memDb==0 && 
         6749  +        (pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap));
  6747   6750   }
  6748   6751   
  6749   6752   /*
  6750   6753   ** Attempt to take an exclusive lock on the database file. If a PENDING lock
  6751   6754   ** is obtained instead, immediately release it.
  6752   6755   */
  6753   6756   static int pagerExclusiveLock(Pager *pPager){

Changes to test/attach.test.

   853    853       SELECT name FROM inmem.sqlite_master;
   854    854     }
   855    855   } {noname inmem}
   856    856   do_test attach-10.2 {
   857    857     lrange [execsql {
   858    858       PRAGMA database_list;
   859    859     }] 9 end
   860         -} {4 noname {} 5 inmem {}}
          860  +} {4 noname {} 5 inmem :memory:}
   861    861   
   862    862   finish_test

Changes to test/shared.test.

  1052   1052   } {1 2 6 8 9 12 1 2 5 11 12 14 1 2 4}
  1053   1053   do_test shared-$av-15.2 {
  1054   1054     execsql { DROP TABLE t1 } db2
  1055   1055   } {}
  1056   1056   db close
  1057   1057   db2 close
  1058   1058   
  1059         -}
         1059  +# Shared cache on a :memory: database.
         1060  +#
         1061  +do_test shared-$av-16.1 {
         1062  +  sqlite3 db1 :memory:
         1063  +  sqlite3 db2 :memory:
         1064  +  db1 eval {
         1065  +    CREATE TABLE t1(x); INSERT INTO t1 VALUES(1),(2),(3);
         1066  +  }
         1067  +  db2 eval {
         1068  +    SELECT x FROM t1 ORDER BY x;
         1069  +  }
         1070  +} {1 2 3}
         1071  +do_test shared-$av-16.2 {
         1072  +  db2 eval {
         1073  +    INSERT INTO t1 VALUES(99);
         1074  +    DELETE FROM t1 WHERE x=2;
         1075  +  }
         1076  +  db1 eval {
         1077  +    SELECT x FROM t1 ORDER BY x;
         1078  +  }
         1079  +} {1 3 99}
         1080  +
         1081  +}  ;# end of autovacuum on/off loop
  1060   1082   
  1061   1083   sqlite3_enable_shared_cache $::enable_shared_cache
  1062   1084   finish_test