Commits

Luke Plant committed 346d78e

Use ByteString in Comment instead of String

This should have the effect of avoiding creation of Strings for talking
to the DB, which is inefficient

Comments (0)

Files changed (6)

src/Blog/Comment.hs

 module Blog.Comment where
 
 import Blog.Formats (Format)
+import qualified Data.ByteString.Lazy as LB
 import Data.Data
 import Data.Typeable
 
       uid :: Int
     , post_id :: Int
     , timestamp :: Int
-    , name :: String
-    , email :: String
-    , text_raw :: String
-    , text_formatted :: String
+    , name :: LB.ByteString
+    , email :: LB.ByteString
+    , text_raw :: LB.ByteString
+    , text_formatted :: LB.ByteString
     , format :: Format
     , hidden :: Bool
-    , response :: String
+    , response :: LB.ByteString
     } deriving (Show, Eq, Data, Typeable)

src/Blog/Feeds.hs

 import qualified Blog.Comment as Cm
 import qualified Blog.Post as P
 import qualified Blog.Settings as Settings
+import qualified Data.ByteString.Lazy.UTF8 as UTF8
 
 rfc3339 :: UTCTime -> String
 rfc3339 d = formatTime defaultTimeLocale "%FT%TZ" d
     Entry { entryId = fullUrl $ commentUrl comment post
           , entryTitle = TextString ("Comment #" ++ (show $ Cm.uid comment) ++ " on post " ++ (P.title post))
           , entryUpdated = formatTimestamp $ Cm.timestamp comment
-          , entryAuthors = [commentAuthor $ Cm.name comment]
+          , entryAuthors = [commentAuthor $ UTF8.toString $ Cm.name comment]
           , entryCategories = []
-          , entryContent = Just $ HTMLContent $ Cm.text_formatted comment
+          , entryContent = Just $ HTMLContent $ UTF8.toString $ Cm.text_formatted comment
           , entryContributor = []
           , entryLinks = [ htmlLink $ commentUrl comment post
                          ]

src/Blog/Forms.hs

 import Control.Monad (liftM)
 import Data.Maybe (fromJust, isNothing, catMaybes)
 import Ella.Forms.Base
-import Ella.GenUtils (exactParse, getTimestamp)
+import Ella.GenUtils (exactParse, getTimestamp, utf8)
 import Ella.Param (captureOrDefault, Param(..))
 import Ella.Request (getPOST, getPOSTlist, hasPOST)
 import Data.String.Utils (strip)
 import qualified Blog.Post as P
 import qualified Blog.Settings as Settings
 import qualified Data.Map as Map
+import qualified Data.ByteString.Lazy.Char8 as LB
 import qualified Ella.Forms.Widgets.RadioButtonList as RBL
 import qualified Ella.Forms.Widgets.OptionList as OL
 import qualified Text.XHtml as X
                  uid = undefined
                , post_id = undefined
                , timestamp = undefined
-               , name = ""
-               , email = ""
-               , text_raw = ""
+               , name = LB.empty
+               , email = LB.empty
+               , text_raw = LB.empty
                , text_formatted = undefined
                , format = Plaintext
                , hidden = False
-               , response = ""
+               , response = LB.empty
                }
 
 instance Param Format where
                       uid = 0 -- for sake of preview
                     , post_id = P.uid blogpost
                     , timestamp = ts
-                    , name = name
-                    , email = email
-                    , text_raw = text
-                    , text_formatted = getFormatter format $ text
+                    , name = utf8 name
+                    , email = utf8 email
+                    , text_raw = utf8 text
+                    , text_formatted = utf8 $ getFormatter format $ text
                     , format = format
                     , hidden = False
-                    , response = ""
+                    , response = LB.empty
                     }
              , errors
              , if test_ts > 0 then test_ts else ts

src/Blog/Templates.hs

 import qualified Blog.Post as P
 import qualified Blog.Settings as Settings
 import qualified Data.ByteString.Lazy.UTF8 as UTF8
-import qualified Data.ByteString.Lazy as LB
+import qualified Data.ByteString.Lazy.Char8 as LB
 import qualified Data.Map as Map
 import qualified Data.Text.Lazy as LT
 
      )
     where makeLink url page text = toHtml (hotlink (url ++ "?p=" ++ (show page)) << text)
 
-formatName name = if null name
-                  then "Anonymous Coward"
+formatName :: LB.ByteString -> LB.ByteString
+formatName name = if LB.null name
+                  then LB.pack "Anonymous Coward"
                   else name
 
 showDate timestamp = formatTime defaultTimeLocale "%e %B %Y" $ posixSecondsToUTCTime $ realToFrac timestamp
 commentTemplateInfo :: Cm.Comment -> Map.Map String ToSElemD
 commentTemplateInfo cm = Map.fromList [ ("name", ToSElemD $ Cm.name cm)
                                       , ("formattedName", ToSElemD $ formatName $ Cm.name cm)
-                                      , ("isAuthor", ToSElemD $ Cm.name cm == Settings.blog_author_name)
+                                      , ("isAuthor", ToSElemD $ Cm.name cm == utf8 Settings.blog_author_name)
                                       , ("date", ToSElemD $ showDate $ Cm.timestamp cm)
                                       , ("textFormatted", ToSElemD $ Cm.text_formatted cm)
                                       , ("email", ToSElemD $ Cm.email cm)
                                       , ("uid", ToSElemD $ Cm.uid cm)
                                       , ("hidden", ToSElemD $ Cm.hidden cm)
-                                      , ("response", ToSElemD $ emptyToNothing $ Cm.response cm)
+                                      , ("response", ToSElemD $ emptyToNothingBS $ Cm.response cm)
                                       ]
 
 emptyToNothing s = if null s then Nothing else Just s
+emptyToNothingBS s = if LB.null s then Nothing else Just s

src/Blog/settingslocal.hs

 module Blog.Settings where
 
+import qualified Data.ByteString.Lazy.Char8 as LB
+
 cgi_root_path = "/home/luke/httpd/lukeplant.me.uk/web/cgi-bin"
 sqlite_path = cgi_root_path ++ "/data/test1.db"
 template_path = cgi_root_path ++ "/data/blogtemplates/"
  -- Fix dodgy stuff, and reinterpret as UTF8
 fixCodes txt = UTF8.toString $ regexReplace (LB.pack "&#10;") (LB.pack "\n") (LB.pack txt)
 
+fixCodes' :: String -> LB.ByteString
+fixCodes' txt = regexReplace (LB.pack "&#10;") (LB.pack "\n") (LB.pack txt)
+
 readPostCategories = makeItems "postcategories.txt" mkPostCategory
     where mkPostCategory row = (read (row !! 0),
                                 read (row !! 1)) :: (Int, Int)
     where mkComment row = Cm.Comment { Cm.uid = read (row !! 0)
                                      , Cm.post_id = read (row !! 1)
                                      , Cm.timestamp = read (row !! 2)
-                                     , Cm.name = fixCodes $ row !! 3
-                                     , Cm.email = row !! 4
-                                     , Cm.text_raw = fixCodes $ row !! 5
-                                     , Cm.text_formatted = fixCodes $ row !! 5
+                                     , Cm.name = fixCodes' $ row !! 3
+                                     , Cm.email = LB.pack $ row !! 4
+                                     , Cm.text_raw = fixCodes' $ row !! 5
+                                     , Cm.text_formatted = fixCodes' $ row !! 5
                                      , Cm.format = Formats.Rawhtml
                                      , Cm.hidden = False
-                                     , Cm.response = ""
+                                     , Cm.response = utf8 ""
                                      }
 -- Writing