@@ -12,6 +12,7 @@ module Glean.Database.Storage.LMDB
1212 ) where
1313
1414import Control.Exception
15+ import Control.Monad
1516import Data.Int
1617import qualified Data.Text as Text
1718import Foreign.C.String
@@ -60,15 +61,25 @@ newStorage root ServerConfig.Config{..} = do
6061 Nothing -> Nothing
6162 }
6263
63- newtype instance Database LMDB = Database DB
64- deriving (CanLookup )
64+ data instance Database LMDB = Database DB LMDB
65+
66+ instance CanLookup (Database LMDB ) where
67+ withLookup (Database db _) = withLookup db
68+ lookupName (Database db _) = lookupName db
6569
6670instance Storage LMDB where
6771 describe db = " lmdb:" <> lmdbRoot db
6872
6973 open lmdb repo mode (DBVersion version) = do
7074 (cmode, start, ownership) <- case mode of
71- ReadOnly -> return (0 , invalidFid, Nothing )
75+ ReadOnly -> do
76+ exists <- doesDirectoryExist (path </> " data.mdb" )
77+ when (not exists) $ do
78+ haveSquash <- doesFileExist squash
79+ when haveSquash $ do
80+ createDirectoryIfMissing True path
81+ callProcess " squashfuse_ll" [squash, path]
82+ return (0 , invalidFid, Nothing )
7283 ReadWrite -> return (1 , invalidFid, Nothing )
7384 Create start ownership _ -> do
7485 createDirectoryIfMissing True path
@@ -82,9 +93,10 @@ instance Storage LMDB where
8293 glean_lmdb_container_open_database container start
8394 first_unit_id version
8495 newForeignPtr glean_rocksdb_database_free p
85- return (Database (DB (castForeignPtr fp) repo))
96+ return (Database (DB (castForeignPtr fp) repo) lmdb )
8697 where
8798 path = containerPath lmdb repo
99+ squash = path <.> " squashfs"
88100
89101 delete lmdb = safeRemovePathForcibly . containerPath lmdb
90102
@@ -116,39 +128,49 @@ instance Storage LMDB where
116128
117129 withScratchRoot rocks f = f $ lmdbRoot rocks </> " .scratch"
118130
119- restore lmdb repo scratch scratch_file = do
131+ restore lmdb cfg repo scratch scratch_file = do
120132 withTempDirectory scratch " restore" $ \ scratch_restore -> do
121- let db = scratch_restore </> " db"
122- createDirectoryIfMissing True db
123- callProcess " unsquashfs" [" -d" , db, scratch_file ]
124- -- to avoid retaining an extra copy of the DB during restore,
125- -- delete the input file now.
126133 let target = containerPath lmdb repo
127134 createDirectoryIfMissing True $ takeDirectory target
128- renameDirectory db target
135+ if ServerConfig. config_db_lmdb_restore_unpack cfg
136+ then do
137+ let db = scratch_restore </> " db"
138+ createDirectoryIfMissing True db
139+ callProcess " unsquashfs" [" -d" , db, scratch_file ]
140+ -- to avoid retaining an extra copy of the DB during restore,
141+ -- delete the input file now.
142+ renameDirectory db target
143+ else
144+ renameFile scratch_file (target <.> " squashfs" )
129145
130146containerPath :: LMDB -> Repo -> FilePath
131147containerPath LMDB {.. } repo = databasePath lmdbRoot repo </> " db"
132148
133149instance DatabaseOps (Database LMDB ) where
134- close (Database db) = close db
135- predicateStats (Database db) = predicateStats db
136- store (Database db) = store db
137- retrieve (Database db) = retrieve db
138- commit (Database db) = commit db
139- addOwnership (Database db) = addOwnership db
140- optimize (Database db) = optimize db
141- computeOwnership (Database db) = computeOwnership db
142- storeOwnership (Database db) = storeOwnership db
143- getOwnership (Database db) = getOwnership db
144- getUnitId (Database db) = getUnitId db
145- getUnit (Database db) = getUnit db
146- addDefineOwnership (Database db) = addDefineOwnership db
147- computeDerivedOwnership (Database db) = computeDerivedOwnership db
148- cacheOwnership (Database db) = cacheOwnership db
149- prepareFactOwnerCache (Database db) = prepareFactOwnerCache db
150-
151- backup (Database db) cfg scratch process =
150+ close (Database db@ (DB _ repo) lmdb) = do
151+ close db
152+ let path = containerPath lmdb repo; squash = path <.> " squashfs"
153+ haveSquash <- doesFileExist squash
154+ when haveSquash $ callProcess " umount" [path]
155+ `catch` \ (_ :: IOException ) -> return ()
156+
157+ predicateStats (Database db _) = predicateStats db
158+ store (Database db _) = store db
159+ retrieve (Database db _) = retrieve db
160+ commit (Database db _) = commit db
161+ addOwnership (Database db _) = addOwnership db
162+ optimize (Database db _) = optimize db
163+ computeOwnership (Database db _) = computeOwnership db
164+ storeOwnership (Database db _) = storeOwnership db
165+ getOwnership (Database db _) = getOwnership db
166+ getUnitId (Database db _) = getUnitId db
167+ getUnit (Database db _) = getUnit db
168+ addDefineOwnership (Database db _) = addDefineOwnership db
169+ computeDerivedOwnership (Database db _) = computeDerivedOwnership db
170+ cacheOwnership (Database db _) = cacheOwnership db
171+ prepareFactOwnerCache (Database db _) = prepareFactOwnerCache db
172+
173+ backup (Database db _) cfg scratch process =
152174 backup db cfg scratch $ \ path _ -> do
153175 withTempDirectory scratch " out" $ \ tmpdir -> do
154176 let out = tmpdir </> " db.squashfs"
0 commit comments