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

#389 jsonb#390

Open
willbasky wants to merge 13 commits intodevelopfrom
willbasky/jsonb
Open

#389 jsonb#390
willbasky wants to merge 13 commits intodevelopfrom
willbasky/jsonb

Conversation

@willbasky
Copy link
Copy Markdown
Contributor

@willbasky willbasky commented Aug 29, 2019

Resolve #389


This change is Reviewable

@willbasky willbasky requested a review from neongreen August 29, 2019 15:36
Comment thread back/benchmarks/Main.hs Outdated
@@ -0,0 +1,49 @@
-- | Module contains all stuff to migrate from AcidState to Postgres.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Wrong description?

Comment thread back/benchmarks/Main.hs Outdated
"Database"
[ bench "select" $ nfIO $
runTransactionExceptT conn Read $ selectCategory "category1111"
, bench "updete" $ nfIO $
Copy link
Copy Markdown
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 $

Comment thread back/benchmarks/Main.hs Outdated
bgroup
"Database"
[ bench "select" $ nfIO $
runTransactionExceptT conn Read $ selectCategory "category1111"
Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread back/benchmarks/Main.hs Outdated

main :: IO ()
main = do
conn <- connect
Copy link
Copy Markdown
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.

Comment thread back/benchmarks/Main.hs Outdated
runTransactionExceptT conn Write $ updateCategory "category1111" update
]
update :: Category -> Category
update = _categoryTitle .~ "title10"
Copy link
Copy Markdown
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
Copy Markdown
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

toJSON = Aeson.genericToJSON Aeson.defaultOptions {
Aeson.fieldLabelModifier = over _head toLower . drop (T.length "trait") }

instance Aeson.FromJSON Trait where
Copy link
Copy Markdown
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.

Comment thread back/src/Guide/Types/Core.hs Outdated
instance Aeson.FromJSON Trait where
parseJSON = Aeson.withObject "Trait" $ \o -> do
traitUid <- o Aeson..: "uid"
content <- o Aeson..: "content"
Copy link
Copy Markdown
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".

Comment thread back/src/Guide/Types/Core.hs Outdated
traitContent <- toMarkdownInline <$> content Aeson..: "text"
pure Trait{..}

instance ToPostgres Trait where
Copy link
Copy Markdown
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?

Comment thread back/src/Guide/Types/Core.hs Outdated
pure Trait{..}

instance ToPostgres Trait where
toPostgres = toByteString . Aeson.encode >$< HE.jsonbBytes
Copy link
Copy Markdown
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.)

Comment thread back/src/Guide/Types/Core.hs Outdated
itemCreated <- o Aeson..: "created"
itemHackage <- o Aeson..:? "hackage"
summary <- o Aeson..: "summary"
itemSummary <- toMarkdownBlock <$> summary Aeson..: "text"
Copy link
Copy Markdown
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.

Comment thread back/package.yaml Outdated
- hasql-transaction

ghc-options:
- -O
Copy link
Copy Markdown
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.

Comment thread back/src/Guide/Database/Import.hs Outdated


-- | Load categories and deleted categories from acid state to postgres
-- | Load categories and archives categories from acid state to postgres
Copy link
Copy Markdown
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?

Comment thread back/src/Guide/Database/Import.hs Outdated
<- runTransactionExceptT conn Read getCategories
catPostgres <- runTransactionExceptT conn Read $
selectCategories (#archived False)
catarchivedPostgres <- runTransactionExceptT conn Read $
Copy link
Copy Markdown
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 $

Comment thread back/src/Guide/Database/Import.hs Outdated
-- Check identity of archived categories
let checkedCatDeleted =
sortOn categoryUid catDeletedPostgres ==
sortOn categoryUid catarchivedPostgres ==
Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread back/src/Guide/Markdown.hs Outdated
instance Aeson.FromJSON MarkdownTree where
parseJSON = Aeson.withObject "MarkdownTree" $ \o -> do
txt <- o Aeson..: "text"
prefix <- o Aeson..:? "prefix" Aeson..!= T.empty
Copy link
Copy Markdown
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.

Comment thread back/src/Guide/Types/Core.hs Outdated
-- | 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
Copy Markdown
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.

@willbasky willbasky self-assigned this Sep 4, 2019
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