@@ -1,3 +1,4 @@
1+{-# LANGUAGE TypeApplications #-}
1module ProjectM36.Transaction.Persist where
2module ProjectM36.Transaction.Persist where
2import ProjectM36.Base
3import ProjectM36.Base
3import ProjectM36.Error
4import ProjectM36.Error
@@ -15,13 +16,17 @@ import qualified Data.Text as T
15import Control.Monad
16import Control.Monad
16import ProjectM36.ScriptSession
17import ProjectM36.ScriptSession
17import ProjectM36.AtomFunctions.Basic (precompiledAtomFunctions)
18import ProjectM36.AtomFunctions.Basic (precompiledAtomFunctions)
18−import Control.Exception
19+
21import Codec.Compression.GZip
20import Codec.Compression.GZip
22import qualified Data.ByteString as BS
21import qualified Data.ByteString as BS
23import qualified Data.ByteString.Lazy as BSL
22import qualified Data.ByteString.Lazy as BSL
24+#ifdef PM36_HASKELL_SCRIPTING
26+import Control.Exception
25getDirectoryNames :: FilePath -> IO [FilePath]
30getDirectoryNames :: FilePath -> IO [FilePath]
26getDirectoryNames path =
31getDirectoryNames path =
27 filter (\ n -> n `notElem` ["..", "."]) <$> getDirectoryContents path
32 filter (\ n -> n `notElem` ["..", "."]) <$> getDirectoryContents path
@@ -128,33 +133,40 @@ readAtomFuncs transDir mScriptSession = do
128 pure (HS.union precompiledAtomFunctions (HS.fromList funcs))
133 pure (HS.union precompiledAtomFunctions (HS.fromList funcs))
130loadAtomFunc :: AtomFunctions -> Maybe ScriptSession -> AtomFunctionName -> [AtomType] -> Maybe AtomFunctionBodyScript -> IO AtomFunction
135loadAtomFunc :: AtomFunctions -> Maybe ScriptSession -> AtomFunctionName -> [AtomType] -> Maybe AtomFunctionBodyScript -> IO AtomFunction
131−loadAtomFunc precompiledFuncs mScriptSession funcName funcType mFuncScript = case mFuncScript of
136+loadAtomFunc precompiledFuncs _mScriptSession funcName _funcType mFuncScript = case mFuncScript of
132 --handle pre-compiled case- pull it from the precompiled list
137 --handle pre-compiled case- pull it from the precompiled list
133 Nothing -> case atomFunctionForName funcName precompiledFuncs of
138 Nothing -> case atomFunctionForName funcName precompiledFuncs of
134 --WARNING: possible landmine here if we remove a precompiled atom function in the future, then the transaction cannot be restored
139 --WARNING: possible landmine here if we remove a precompiled atom function in the future, then the transaction cannot be restored
135 Left _ -> error ("expected precompiled atom function: " ++ T.unpack funcName)
140 Left _ -> error ("expected precompiled atom function: " ++ T.unpack funcName)
136 Right realFunc -> pure realFunc
141 Right realFunc -> pure realFunc
137 --handle a real Haskell scripted function- compile and load
142 --handle a real Haskell scripted function- compile and load
138− Just funcScript ->
143+ Just _funcScript ->
139− case mScriptSession of
144+#ifdef PM36_HASKELL_SCRIPTING
145+ case _mScriptSession of
140 Nothing -> error "attempted to read serialized AtomFunction without scripting enabled"
146 Nothing -> error "attempted to read serialized AtomFunction without scripting enabled"
141 Just scriptSession -> do
147 Just scriptSession -> do
142 --risk of GHC exception during compilation here
148 --risk of GHC exception during compilation here
143 eCompiledScript <- runGhc (Just libdir) $ do
149 eCompiledScript <- runGhc (Just libdir) $ do
144 setSession (hscEnv scriptSession)
150 setSession (hscEnv scriptSession)
145− compileScript (atomFunctionBodyType scriptSession) funcScript
151+ compileScript (atomFunctionBodyType scriptSession) _funcScript
146 case eCompiledScript of
152 case eCompiledScript of
147 Left err -> throwIO err
153 Left err -> throwIO err
148 Right compiledScript -> pure AtomFunction { atomFuncName = funcName,
154 Right compiledScript -> pure AtomFunction { atomFuncName = funcName,
149− atomFuncType = funcType,
155+ atomFuncType = _funcType,
150− atomFuncBody = AtomFunctionBody (Just funcScript) compiledScript }
156+ atomFuncBody = AtomFunctionBody (Just _funcScript) compiledScript }
158+ error "Haskell scripting is disabled"
152--if the script session is enabled, compile the script, otherwise, hard error!
161--if the script session is enabled, compile the script, otherwise, hard error!
154readAtomFunc :: FilePath -> AtomFunctionName -> Maybe ScriptSession -> AtomFunctions -> IO AtomFunction
163readAtomFunc :: FilePath -> AtomFunctionName -> Maybe ScriptSession -> AtomFunctions -> IO AtomFunction
164+#if !defined(PM36_HASKELL_SCRIPTING)
165+readAtomFunc _ _ _ _ = error "Haskell scripting is disabled"
155readAtomFunc transDir funcName mScriptSession precompiledFuncs = do
167readAtomFunc transDir funcName mScriptSession precompiledFuncs = do
156 let atomFuncPath = atomFuncsPath transDir
168 let atomFuncPath = atomFuncsPath transDir
157− (funcType, mFuncScript) <- B.decodeFile atomFuncPath
169+ (funcType, mFuncScript) <- B.decodeFile @([AtomType],Maybe T.Text) atomFuncPath
158 case mFuncScript of
170 case mFuncScript of
159 --handle pre-compiled case- pull it from the precompiled list
171 --handle pre-compiled case- pull it from the precompiled list
160 Nothing -> case atomFunctionForName funcName precompiledFuncs of
172 Nothing -> case atomFunctionForName funcName precompiledFuncs of
@@ -162,7 +174,8 @@ readAtomFunc transDir funcName mScriptSession precompiledFuncs = do
162 Left _ -> error ("expected precompiled atom function: " ++ T.unpack funcName)
174 Left _ -> error ("expected precompiled atom function: " ++ T.unpack funcName)
163 Right realFunc -> pure realFunc
175 Right realFunc -> pure realFunc
164 --handle a real Haskell scripted function- compile and load
176 --handle a real Haskell scripted function- compile and load
165− Just funcScript ->
177+ Just funcScript ->
166 case mScriptSession of
179 case mScriptSession of
167 Nothing -> error "attempted to read serialized AtomFunction without scripting enabled"
180 Nothing -> error "attempted to read serialized AtomFunction without scripting enabled"
168 Just scriptSession -> do
181 Just scriptSession -> do
@@ -175,6 +188,7 @@ readAtomFunc transDir funcName mScriptSession precompiledFuncs = do
175 Right compiledScript -> pure AtomFunction { atomFuncName = funcName,
188 Right compiledScript -> pure AtomFunction { atomFuncName = funcName,
176 atomFuncType = funcType,
189 atomFuncType = funcType,
177 atomFuncBody = AtomFunctionBody (Just funcScript) compiledScript }
190 atomFuncBody = AtomFunctionBody (Just funcScript) compiledScript }
180writeDBCFuncs :: DiskSync -> FilePath -> DatabaseContextFunctions -> IO ()
194writeDBCFuncs :: DiskSync -> FilePath -> DatabaseContextFunctions -> IO ()
@@ -193,26 +207,34 @@ readDBCFuncs transDir mScriptSession = do
193 let funcs = mapM ((\name -> readDBCFunc transDir name mScriptSession precompiledDatabaseContextFunctions) . T.pack) funcNames
207 let funcs = mapM ((\name -> readDBCFunc transDir name mScriptSession precompiledDatabaseContextFunctions) . T.pack) funcNames
194 HS.union basicDatabaseContextFunctions . HS.fromList <$> funcs
208 HS.union basicDatabaseContextFunctions . HS.fromList <$> funcs
196−readDBCFunc :: FilePath -> DatabaseContextFunctionName -> Maybe ScriptSession -> DatabaseContextFunctions -> IO DatabaseContextFunction
210+readDBCFunc :: FilePath -> DatabaseContextFunctionName -> Maybe ScriptSession -> DatabaseContextFunctions -> IO DatabaseContextFunction
211+#if !defined(PM36_HASKELL_SCRIPTING)
212+readDBCFunc transDir funcName _ precompiledFuncs = do
197readDBCFunc transDir funcName mScriptSession precompiledFuncs = do
214readDBCFunc transDir funcName mScriptSession precompiledFuncs = do
198 let dbcFuncPath = dbcFuncsDir transDir </> T.unpack funcName
216 let dbcFuncPath = dbcFuncsDir transDir </> T.unpack funcName
199− (funcType, mFuncScript) <- B.decodeFile dbcFuncPath
217+ (_funcType, mFuncScript) <- B.decodeFile @([AtomType], Maybe T.Text) dbcFuncPath
200 case mFuncScript of
218 case mFuncScript of
201 Nothing -> case databaseContextFunctionForName funcName precompiledFuncs of
219 Nothing -> case databaseContextFunctionForName funcName precompiledFuncs of
202 Left _ -> error ("expected precompiled dbc function: " ++ T.unpack funcName)
220 Left _ -> error ("expected precompiled dbc function: " ++ T.unpack funcName)
203 Right realFunc -> pure realFunc --return precompiled function
221 Right realFunc -> pure realFunc --return precompiled function
204− Just funcScript ->
222+ Just _funcScript ->
223+#ifdef PM36_HASKELL_SCRIPTING
205 case mScriptSession of
224 case mScriptSession of
206 Nothing -> error "attempted to read serialized AtomFunction without scripting enabled"
225 Nothing -> error "attempted to read serialized AtomFunction without scripting enabled"
207 Just scriptSession -> do
226 Just scriptSession -> do
208 eCompiledScript <- runGhc (Just libdir) $ do
227 eCompiledScript <- runGhc (Just libdir) $ do
209 setSession (hscEnv scriptSession)
228 setSession (hscEnv scriptSession)
210− compileScript (dbcFunctionBodyType scriptSession) funcScript
229+ compileScript (dbcFunctionBodyType scriptSession) _funcScript
211 case eCompiledScript of
230 case eCompiledScript of
212 Left err -> throwIO err
231 Left err -> throwIO err
213 Right compiledScript -> pure DatabaseContextFunction { dbcFuncName = funcName,
232 Right compiledScript -> pure DatabaseContextFunction { dbcFuncName = funcName,
214− dbcFuncType = funcType,
233+ dbcFuncType = _funcType,
215− dbcFuncBody = DatabaseContextFunctionBody (Just funcScript) compiledScript}
234+ dbcFuncBody = DatabaseContextFunctionBody (Just _funcScript) compiledScript}
236+ error "Haskell scripting is disabled"
217writeIncDep :: DiskSync -> FilePath -> (IncDepName, InclusionDependency) -> IO ()
239writeIncDep :: DiskSync -> FilePath -> (IncDepName, InclusionDependency) -> IO ()
218writeIncDep sync transDir (incDepName, incDep) =
240writeIncDep sync transDir (incDepName, incDep) =
@@ -252,4 +274,4 @@ readTypeConstructorMapping path = do
252 let atPath = typeConsPath path
274 let atPath = typeConsPath path
253 B.decodeFile atPath
275 B.decodeFile atPath