aboutsummaryrefslogtreecommitdiffstats
path: root/src/Store/Query.hs
blob: b63b176418954ae572c6f6d94ee9506b66df2791 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
module Store.Query
  ( module Store.Query.Type,
    query,
  )
where

import Control.Exception (throw)
import Data.Aeson qualified as J
import Data.Aeson.Key qualified as JK
import Data.Aeson.KeyMap qualified as JM
import Data.List (foldl', isSuffixOf)
import Data.List.NonEmpty qualified as N
import Data.Maybe (fromMaybe)
import Data.Vector qualified as V
import Store.Exception (DecodeException (DecodeException))
import Store.Query.Parser ()
import Store.Query.Printer ()
import Store.Query.Record
import Store.Query.Type
import Store.Store qualified as S
import System.FilePath ((</>))

query :: Query -> IO [J.Value]
query (Select fs c js es w) = do
  c' <-
    mapM (fmap (fromValue c) . decodeFile . (c </>)) =<< ls c
  js' <-
    mapM
      ( \(JoinClause t c w) ->
          fmap (\j' -> JoinClause t (map (fromValue c) j') w) . mapM (decodeFile . (c </>))
            =<< ls c
      )
      js
  es' <-
    mapM
      ( \(EmbedClause c w) ->
          fmap (\e' -> EmbedClause (fromValue c e') w) . mapM (decodeFile . (c </>))
            =<< ls c
      )
      es
  pure $ map (Store.Query.select fs) $ where_ w $ embeds es' $ joins js' c'
  where
    ls c =
      filter (not . (isSuffixOf "/"))
        <$> S.withStore "." "HEAD" do
          S.listDirectory c

embeds ::
  EmbedClauses (Record [J.Value]) ->
  [Records J.Value] ->
  [Records J.Value]
embeds = flip (foldl' embed)

embed ::
  [Records J.Value] ->
  EmbedClause (Record [J.Value]) ->
  [Records J.Value]
embed vss (EmbedClause (Record c es) w) =
  map
    ( \vs ->
        vs
          ++ [ fromValue
                 c
                 ( J.Object
                     ( JM.singleton
                         (JK.fromString c)
                         ( J.Array
                             ( V.fromList
                                 [ e
                                   | e <- es,
                                     satisfies w (vs ++ [Record c e])
                                 ]
                             )
                         )
                     )
                 )
             ]
    )
    vss

joins ::
  JoinClauses (Records J.Value) ->
  [Record J.Value] ->
  [Records J.Value]
joins js (map (: []) -> vss) = foldl' join vss js

join ::
  [Records J.Value] ->
  JoinClause (Records J.Value) ->
  [Records J.Value]
join vss (JoinClause JoinLeft js w) =
  concatMap
    ( \vs -> case filter (satisfies w) $ map (\j -> vs ++ [j]) js of
        [] -> [vs]
        vs' -> vs'
    )
    vss
join vss (JoinClause JoinRight js w) =
  concatMap
    ( \j -> case filter (satisfies w) $ map (\vs -> vs ++ [j]) vss of
        [] -> [[j]]
        vs' -> vs'
    )
    js
join vss (JoinClause JoinFull js w) =
  concatMap
    ( \vs -> case filter (satisfies w) $ map (\j -> vs ++ [j]) js of
        [] -> [vs]
        vs' -> vs'
    )
    vss
    ++ concatMap
      ( \j -> case filter (satisfies w) $ map (\vs -> vs ++ [j]) vss of
          [] -> [[j]]
          _ -> []
      )
      js

select :: FieldSelector -> Records J.Value -> J.Value
select All vs = disjointUnions (map toValue vs)
select (Only fs) vs = Store.Query.Record.select (N.toList fs) vs

where_ :: Maybe WhereClause -> [Records J.Value] -> [Records J.Value]
where_ w = filter (satisfies w)

satisfies :: Maybe WhereClause -> Records J.Value -> Bool
satisfies Nothing _ = True
satisfies (Just (And ws)) vs = all (\w -> satisfies (Just w) vs) ws
satisfies (Just (Or ws)) vs = any (\w -> satisfies (Just w) vs) ws
satisfies (Just (Where (Eq f g))) vs = lookups f vs == lookups g vs

decodeFile :: J.FromJSON a => Collection -> IO a
decodeFile fp = S.withStore "." "HEAD" do
  fromMaybe (throw DecodeException) . J.decode <$> S.readFile fp