]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Database/Schema/Node.hs
[OPTIM] after profiling, optimize serialisation
[gargantext.git] / src / Gargantext / Database / Schema / Node.hs
1 {-|
2 Module : Gargantext.Database.Schema.Node
3 Description : Main requests of Node to the database
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9 -}
10
11 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
12 {-# OPTIONS_GHC -fno-warn-orphans #-}
13
14 {-# LANGUAGE Arrows #-}
15 {-# LANGUAGE DeriveGeneric #-}
16 {-# LANGUAGE ConstraintKinds #-}
17 {-# LANGUAGE FlexibleContexts #-}
18 {-# LANGUAGE FlexibleInstances #-}
19 {-# LANGUAGE FunctionalDependencies #-}
20 {-# LANGUAGE OverloadedStrings #-}
21 {-# LANGUAGE MultiParamTypeClasses #-}
22 {-# LANGUAGE NoImplicitPrelude #-}
23 {-# LANGUAGE RankNTypes #-}
24 {-# LANGUAGE TemplateHaskell #-}
25 {-# LANGUAGE TypeFamilies #-}
26
27 module Gargantext.Database.Schema.Node where
28
29 import Control.Lens hiding (elements, (&))
30 import Gargantext.Database.Schema.Prelude
31 import Prelude hiding (null, id, map, sum)
32
33 ------------------------------------------------------------------------
34 -- Main polymorphic Node definition
35
36 data NodePoly id
37 typename
38 userId
39 parentId
40 name
41 date
42 hyperdata =
43 Node { _node_id :: !id
44 , _node_typename :: !typename
45
46 , _node_userId :: !userId
47 , _node_parentId :: !parentId
48
49 , _node_name :: !name
50 , _node_date :: !date
51
52 , _node_hyperdata :: !hyperdata
53 } deriving (Show, Generic)
54
55 ------------------------------------------------------------------------
56 -- Automatic instances derivation
57 $(deriveJSON (unPrefix "_node_") ''NodePoly)
58 $(makeLenses ''NodePoly)
59
60 $(makeAdaptorAndInstance "pNode" ''NodePoly)
61 $(makeLensesWith abbreviatedFields ''NodePoly)
62
63 ------------------------------------------------------------------------
64 nodeTable :: Table NodeWrite NodeRead
65 nodeTable = Table "nodes" (pNode Node { _node_id = optional "id"
66 , _node_typename = required "typename"
67 , _node_userId = required "user_id"
68
69 , _node_parentId = optional "parent_id"
70 , _node_name = required "name"
71 , _node_date = optional "date"
72
73 , _node_hyperdata = required "hyperdata"
74 -- ignoring ts_vector field here
75 }
76 )
77
78 queryNodeTable :: Query NodeRead
79 queryNodeTable = queryTable nodeTable
80 ------------------------------------------------------------------------
81 type NodeWrite = NodePoly (Maybe (Column PGInt4) )
82 (Column PGInt4)
83 (Column PGInt4)
84 (Maybe (Column PGInt4) )
85 (Column PGText)
86 (Maybe (Column PGTimestamptz))
87 (Column PGJsonb)
88
89 type NodeRead = NodePoly (Column PGInt4 )
90 (Column PGInt4 )
91 (Column PGInt4 )
92 (Column PGInt4 )
93 (Column PGText )
94 (Column PGTimestamptz )
95 (Column PGJsonb )
96
97 type NodeReadNull = NodePoly (Column (Nullable PGInt4))
98 (Column (Nullable PGInt4))
99 (Column (Nullable PGInt4))
100 (Column (Nullable PGInt4))
101 (Column (Nullable PGText))
102 (Column (Nullable PGTimestamptz))
103 (Column (Nullable PGJsonb))
104 ------------------------------------------------------------------------
105 -- | Node(Read|Write)Search is slower than Node(Write|Read) use it
106 -- for full text search only
107
108 type NodeSearchWrite =
109 NodePolySearch
110 (Maybe (Column PGInt4) )
111 (Column PGInt4 )
112 (Column PGInt4 )
113 (Column (Nullable PGInt4) )
114 (Column PGText )
115 (Maybe (Column PGTimestamptz))
116 (Column PGJsonb )
117 (Maybe (Column PGTSVector) )
118
119 type NodeSearchRead =
120 NodePolySearch
121 (Column PGInt4 )
122 (Column PGInt4 )
123 (Column PGInt4 )
124 (Column (Nullable PGInt4 ))
125 (Column PGText )
126 (Column PGTimestamptz )
127 (Column PGJsonb )
128 (Column PGTSVector )
129
130 type NodeSearchReadNull =
131 NodePolySearch
132 (Column (Nullable PGInt4) )
133 (Column (Nullable PGInt4) )
134 (Column (Nullable PGInt4) )
135 (Column (Nullable PGInt4) )
136 (Column (Nullable PGText) )
137 (Column (Nullable PGTimestamptz))
138 (Column (Nullable PGJsonb) )
139 (Column (Nullable PGTSVector) )
140
141
142 data NodePolySearch id
143 typename
144 userId
145 parentId
146 name
147 date
148 hyperdata
149 search =
150 NodeSearch { _ns_id :: id
151 , _ns_typename :: typename
152 , _ns_userId :: userId
153 -- , nodeUniqId :: shaId
154 , _ns_parentId :: parentId
155 , _ns_name :: name
156 , _ns_date :: date
157
158 , _ns_hyperdata :: hyperdata
159 , _ns_search :: search
160 } deriving (Show, Generic)
161
162 $(makeAdaptorAndInstance "pNodeSearch" ''NodePolySearch)
163 $(makeLensesWith abbreviatedFields ''NodePolySearch)
164 $(deriveJSON (unPrefix "_ns_") ''NodePolySearch)
165 $(makeLenses ''NodePolySearch)
166
167 nodeTableSearch :: Table NodeSearchWrite NodeSearchRead
168 nodeTableSearch = Table "nodes" (pNodeSearch NodeSearch { _ns_id = optional "id"
169 , _ns_typename = required "typename"
170 , _ns_userId = required "user_id"
171
172 , _ns_parentId = required "parent_id"
173 , _ns_name = required "name"
174 , _ns_date = optional "date"
175
176 , _ns_hyperdata = required "hyperdata"
177 , _ns_search = optional "search"
178 }
179 )
180 ------------------------------------------------------------------------