Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

#389 jsonb #390

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
Open

#389 jsonb #390

wants to merge 13 commits into from

Conversation

willbasky
Copy link
Contributor

@willbasky willbasky commented Aug 29, 2019

Resolve #389


This change is Reviewable

@@ -0,0 +1,49 @@
-- | Module contains all stuff to migrate from AcidState to Postgres.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong description?

"Database"
[ bench "select" $ nfIO $
runTransactionExceptT conn Read $ selectCategory "category1111"
, bench "updete" $ nfIO $
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
, bench "updete" $ nfIO $
, bench "update" $ nfIO $

bgroup
"Database"
[ bench "select" $ nfIO $
runTransactionExceptT conn Read $ selectCategory "category1111"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please let's not have such names in code, they look sloppy. "category" or "uid1" would be better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

length "category1111" == 12 as Uid needed.


main :: IO ()
main = do
conn <- connect
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have two-space indentation.

runTransactionExceptT conn Write $ updateCategory "category1111" update
]
update :: Category -> Category
update = _categoryTitle .~ "title10"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After the first application of update the title will be "title10", so update will be equivalent to id and updateCategory will not do any writes. You should do something that would change the category every time, e.g. _categoryTitle <>~ "+", and then re-run the benchmarks.

Copy link
Contributor Author

@willbasky willbasky Sep 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. then speed is
Screenshot_20190901_182126

@@ -74,6 +76,20 @@ instance Aeson.ToJSON Trait where
toJSON = Aeson.genericToJSON Aeson.defaultOptions {
Aeson.fieldLabelModifier = over _head toLower . drop (T.length "trait") }

instance Aeson.FromJSON Trait where
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this instance is not using generic, the ToJSON instance should also not use generic.

instance Aeson.FromJSON Trait where
parseJSON = Aeson.withObject "Trait" $ \o -> do
traitUid <- o Aeson..: "uid"
content <- o Aeson..: "content"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since MarkdownInline has a FromJSON instance, you can just write traitContent <- o Aeson..: "content".

traitContent <- toMarkdownInline <$> content Aeson..: "text"
pure Trait{..}

instance ToPostgres Trait where
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this instance used anywhere?

pure Trait{..}

instance ToPostgres Trait where
toPostgres = toByteString . Aeson.encode >$< HE.jsonbBytes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you using jsonbBytes instead of just jsonb? (Same for all other instances.)

itemCreated <- o Aeson..: "created"
itemHackage <- o Aeson..:? "hackage"
summary <- o Aeson..: "summary"
itemSummary <- toMarkdownBlock <$> summary Aeson..: "text"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, toMarkdownBlock is not needed because MarkdownBlock already has a FromJSON instance.

- hasql-transaction

ghc-options:
- -O
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this won't have any effect if the library is not compiled with -O, so you can just remove it and add a short instruction on how to run benchmarks properly to the README.

import Guide.Config
import Guide.Logger


-- | Load categories and deleted categories from acid state to postgres
-- | Load categories and archives categories from acid state to postgres
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
-- | Load categories and archives categories from acid state to postgres
-- | Load categories and archived categories from acid state to postgres

Is this what you meant?

<- runTransactionExceptT conn Read getCategories
catPostgres <- runTransactionExceptT conn Read $
selectCategories (#archived False)
catarchivedPostgres <- runTransactionExceptT conn Read $
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
catarchivedPostgres <- runTransactionExceptT conn Read $
catArchivedPostgres <- runTransactionExceptT conn Read $

let checkedCatDeleted =
sortOn categoryUid catDeletedPostgres ==
sortOn categoryUid catarchivedPostgres ==
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
sortOn categoryUid catarchivedPostgres ==
sortOn categoryUid catArchivedPostgres ==

-> "archived" :! Bool
-> ExceptT DatabaseError Transaction ()
updateCategoryArchived catId (arg #archived -> archived) = do
isArchived <- isCategoryArchived catId
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"old_archived" and "new_archived" would be more consistent.

instance Aeson.FromJSON MarkdownTree where
parseJSON = Aeson.withObject "MarkdownTree" $ \o -> do
txt <- o Aeson..: "text"
prefix <- o Aeson..:? "prefix" Aeson..!= T.empty
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use "" instead of T.empty, I don't think there is a reason to not use overloaded strings here.

-- | Unwarp result to category or fail.
resultToEither :: Aeson.Result Category -> Category
resultToEither (Aeson.Success category) = category
resultToEither (Aeson.Error s) = error $ "fromJSON failed with error: " ++ s
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, error is verboten. Apparently we'll have to use jsonbBytes after all because it would let us give back the decoding error (as Left) without using error.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Change Postgres to store data in JSON
2 participants