From dadfc2616a05959c013c782273b909c541cfcb34 Mon Sep 17 00:00:00 2001 From: Iustin Pop <iustin@google.com> Date: Fri, 30 Jul 2010 22:46:37 -0400 Subject: [PATCH] Text loader: split parsing from loadData This change, which will be followed by similar changes in the other loaders, splits the parsing of the data from the actual loading from disk. Since the parsing doesn't usually involve IO actions, we will be able to better test the parsing. The loading becomes a smaller part of the code and thus inability to test it has a smaller impact. --- Ganeti/HTools/Text.hs | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/Ganeti/HTools/Text.hs b/Ganeti/HTools/Text.hs index b9832cbad..c9aac174a 100644 --- a/Ganeti/HTools/Text.hs +++ b/Ganeti/HTools/Text.hs @@ -29,6 +29,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA module Ganeti.HTools.Text ( loadData + , parseData , loadInst , loadNode , serializeInstances @@ -136,19 +137,27 @@ loadTabular lines_data convert_fn = do kerows <- mapM convert_fn rows return $ assignIndices kerows +-- | Load the cluser data from disk. +readData :: String -- ^ Path to the text file + -> IO String +readData = readFile + -- | Builds the cluster data from text input. -loadData :: String -- ^ Path to the text file - -> IO (Result (Node.AssocList, Instance.AssocList, [String])) -loadData afile = do -- IO monad - fdata <- readFile afile +parseData :: String -- ^ Text data + -> Result (Node.AssocList, Instance.AssocList, [String]) +parseData fdata = do let flines = lines fdata (nlines, ilines) = break null flines - return $ do - ifixed <- case ilines of - [] -> Bad "Invalid format of the input file (no instance data)" - _:xs -> Ok xs - {- node file: name t_mem n_mem f_mem t_disk f_disk -} - (ktn, nl) <- loadTabular nlines loadNode - {- instance file: name mem disk status pnode snode -} - (_, il) <- loadTabular ifixed (loadInst ktn) - return (nl, il, []) + ifixed <- case ilines of + [] -> Bad "Invalid format of the input file (no instance data)" + _:xs -> Ok xs + {- node file: name t_mem n_mem f_mem t_disk f_disk -} + (ktn, nl) <- loadTabular nlines loadNode + {- instance file: name mem disk status pnode snode -} + (_, il) <- loadTabular ifixed (loadInst ktn) + return (nl, il, []) + +-- | Top level function for data loading +loadData :: String -- ^ Path to the text file + -> IO (Result (Node.AssocList, Instance.AssocList, [String])) +loadData afile = readData afile >>= return . parseData -- GitLab