From 2243f4b6539577a6534e11bee66052ae5cda7e71 Mon Sep 17 00:00:00 2001 From: George Trudeau <9too@users.noreply.github.com> Date: Sat, 5 Aug 2017 15:04:52 -0400 Subject: [PATCH] Improve relations initialization time (#924) (#926) --- CHANGELOG.md | 1 + src/PostgREST/DbStructure.hs | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 787b942da..a73d34b1a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - #877, Base64 secret read from a file ending with a newline - @eric-brechemier - #896, Boolean env var interpolation in config file - @begriffs - #885, OpenAPI repetition reduced by using more definitions- @ldesgoui +- #924, Improve relations initialization time - @9too ## [0.4.2.0] - 2017-06-11 diff --git a/src/PostgREST/DbStructure.hs b/src/PostgREST/DbStructure.hs index 5b59d609f..e877372ae 100644 --- a/src/PostgREST/DbStructure.hs +++ b/src/PostgREST/DbStructure.hs @@ -253,7 +253,11 @@ addManyToManyRelations rels = rels ++ addMirrorRelation (mapMaybe link2Relation links = join $ map (combinations 2) $ filter (not . null) $ groupWith groupFn $ filter ( (==Child). relType) rels groupFn :: Relation -> Text groupFn Relation{relTable=Table{tableSchema=s, tableName=t}} = s<>"_"<>t - combinations k ns = filter ((k==).length) (subsequences ns) + -- Reference : https://wiki.haskell.org/99_questions/Solutions/26 + combinations :: Int -> [a] -> [[a]] + combinations 0 _ = [ [] ] + combinations n xs = [ y:ys | y:xs' <- tails xs + , ys <- combinations (n-1) xs'] addMirrorRelation [] = [] addMirrorRelation (rel@(Relation t c ft fc _ lt lc1 lc2):rels') = Relation ft fc t c Many lt lc2 lc1 : rel : addMirrorRelation rels' link2Relation [