-
Notifications
You must be signed in to change notification settings - Fork 6
/
SQLiteIndexer.hs
509 lines (463 loc) · 17.5 KB
/
SQLiteIndexer.hs
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-unused-matches #-}
{- |
On-disk indexer backed by a sqlite database.
See "Marconi.Core" for documentation.
-}
module Marconi.Core.Indexer.SQLiteIndexer (
SQLiteIndexer (SQLiteIndexer),
SQLiteDBLocation (Memory, Storage),
ExpectedPersistentDB (..),
inMemoryDB,
parseDBLocation,
databasePath,
writeConnection,
readConnectionPool,
insertPlan,
InsertPointQuery (InsertPointQuery, getInsertPointQuery),
SetLastStablePointQuery (
SetLastStablePointQuery,
getSetLastStablePointQuery
),
GetLastStablePointQuery (
GetLastStablePointQuery,
getLastStablePointQuery
),
mkSqliteIndexer,
mkSingleInsertSqliteIndexer,
querySQLiteIndexerWith,
queryLatestSQLiteIndexerWith,
querySyncedOnlySQLiteIndexerWith,
handleSQLErrors,
dbLastSync,
SQLInsertPlan (SQLInsertPlan, planInsert, planExtractor),
SQLRollbackPlan (SQLRollbackPlan, tableName, pointName, pointExtractor),
-- * Reexport from SQLite
SQL.ToRow (..),
-- * Concurrent and read only connection for sqlite-simple connection
readOnlyConnection,
readWriteConnection,
) where
import Control.Concurrent.Async qualified as Async
import Control.Exception (Exception, Handler (Handler), catches, try)
import Control.Lens (makeLenses)
import Control.Lens.Operators ((&), (.~), (^.))
import Control.Monad (when, (<=<))
import Control.Monad.Except (MonadError (throwError))
import Control.Monad.IO.Class (MonadIO (liftIO))
import Data.Foldable (Foldable (toList), traverse_)
import Data.Maybe (catMaybes, fromMaybe, listToMaybe)
import Data.Pool (Pool)
import Data.Pool qualified as Pool
import Data.Text (Text)
import Data.Text qualified as Text
import Database.SQLite.Simple qualified as SQL
import Database.SQLite.Simple.ToField qualified as SQL
import Database.SQLite3 qualified as SQL3
import Marconi.Core.Class (
Closeable (close),
HasGenesis (genesis),
IsIndex (index, indexAllDescending, rollback, setLastStablePoint),
IsSync (lastStablePoint, lastSyncPoint),
)
import Marconi.Core.Type (
IndexerError (IndexerInternalError, InvalidIndexer),
Point,
QueryError (AheadOfLastSync),
Result,
Timed,
point,
)
data SQLiteDBLocation
= Memory
| Storage !FilePath
unparseDBLocation :: SQLiteDBLocation -> Text
unparseDBLocation Memory = ":memory:"
unparseDBLocation (Storage path) = Text.pack path
parseDBLocation :: String -> SQLiteDBLocation
parseDBLocation "" = Memory
parseDBLocation ":memory:" = Memory
parseDBLocation str = Storage str
inMemoryDB :: SQLiteDBLocation
inMemoryDB = Memory
data ExpectedPersistentDB = ExpectedPersistentDB
deriving (Show)
instance Exception ExpectedPersistentDB
-- | A 'SQLInsertPlan' provides a piece information about how an event should be inserted in the database
data SQLInsertPlan event = forall a.
(SQL.ToRow a) =>
SQLInsertPlan
{ planExtractor :: Timed (Point event) event -> [a]
-- ^ How to transform the event into a type that can be handle by the database
, planInsert :: SQL.Query
-- ^ The insert statement for the extracted data
}
newtype InsertPointQuery = InsertPointQuery {getInsertPointQuery :: SQL.Query}
{- | A 'SQLRollbackPlan' provides a piece of information on how to perform a rollback on the data
inserted in the database.
-}
data SQLRollbackPlan point = forall a.
(SQL.ToField a) =>
SQLRollbackPlan
{ tableName :: String
-- ^ the table to rollback
, pointName :: String
-- ^ The name of the point field in the table
, pointExtractor :: point -> Maybe a
-- ^ How we transform the data to the point field. Returning 'Nothing' essentially means that we
-- delete all information from the database. Returning 'Just a' means that we will delete all
-- rows with a point higher than 'point'.
}
-- | A newtype to set the last stable point of an indexer.
newtype SetLastStablePointQuery = SetLastStablePointQuery
{ getSetLastStablePointQuery :: SQL.Query
}
-- | A newtype to get the last stable point from an indexer.
newtype GetLastStablePointQuery = GetLastStablePointQuery
{ getLastStablePointQuery :: SQL.Query
}
-- | Provide the minimal elements required to use a SQLite database to back an indexer.
data SQLiteIndexer event = SQLiteIndexer
{ _databasePath :: SQLiteDBLocation
-- ^ The location of the database
, _writeConnection :: SQL.Connection
-- ^ The connection used to index events into the database (write)
, _readConnectionPool :: Pool SQL.Connection
-- ^ The connection pool used to query the database (read)
, _insertPlan :: [[SQLInsertPlan event]]
-- ^ A plan is a list of lists : each 'SQLInsertPlan' in a list is executed concurrently.
-- The different @[SQLInsertPlan]@ are executed in sequence.
, _rollbackPlan :: [SQLRollbackPlan (Point event)]
-- ^ The list of tables we update on rollback, with the information required to update them
, _setLastStablePointQuery :: SetLastStablePointQuery
-- ^ The SQL query to fetch the last sync points from the indexer.
, _dbLastSync :: Point event
-- ^ We keep the last sync point in memory to avoid an SQL query to retrieve it
, _dbLastStable :: Point event
-- ^ We keep the last stable point in memory to avoid an SQL query to retrieve it
}
makeLenses ''SQLiteIndexer
-- | Create an SQL connection with ReadWrite permission and concurrent connections
readWriteConnection
:: (MonadIO io, MonadError IndexerError io)
=> SQLiteDBLocation
-> io SQL.Connection
readWriteConnection loc = do
con <-
liftIO $
try $
SQL3.open2
(unparseDBLocation loc)
[SQL3.SQLOpenReadWrite, SQL3.SQLOpenCreate, SQL3.SQLOpenNoMutex]
SQL3.SQLVFSDefault
case con of
Left (err :: SQL.SQLError) ->
throwError $ IndexerInternalError $ Text.pack $ show err
Right con' -> pure $ SQL.Connection con'
-- | Create an SQL connection with ReadOnly permission and concurrent connections
readOnlyConnection :: (MonadIO io) => SQLiteDBLocation -> io SQL.Connection
readOnlyConnection loc =
liftIO $
SQL.Connection
<$> SQL3.open2
(unparseDBLocation loc)
[SQL3.SQLOpenReadOnly, SQL3.SQLOpenNoMutex]
SQL3.SQLVFSDefault
{- | Start a new indexer or resume an existing SQLite indexer
The main difference with 'SQLiteIndexer' is that we set 'dbLastSync' thanks to the provided query.
It helps resuming an existing indexer.
-}
mkSqliteIndexer
:: forall event m
. ( MonadIO m
, MonadError IndexerError m
, HasGenesis (Point event)
, SQL.FromRow (Point event)
, SQL.ToRow (Point event)
, Ord (Point event)
)
=> SQLiteDBLocation
-> [SQL.Query]
-- ^ creation statement
-> [[SQLInsertPlan event]]
-- ^ extract @param@ out of a 'Timed'
-> [SQLRollbackPlan (Point event)]
-- ^ the rollbackQuery
-> SetLastStablePointQuery
-- ^ The SQL query to set the last stable point of the indexer.
-> GetLastStablePointQuery
-- ^ The SQL query to fetch the last stable point from the indexer.
-> m (SQLiteIndexer event)
mkSqliteIndexer
_databasePath
_creationStatements
_insertPlan
_rollbackPlan
_setLastStablePointQuery
lastStablePointQuery =
let getLastStablePoint :: SQL.Connection -> m (Point event)
getLastStablePoint h = do
res <- runLastStablePointQuery h lastStablePointQuery
pure $ fromMaybe genesis res
in do
_writeConnection <- readWriteConnection _databasePath
let tenHours = 36000
poolSize = 100
poolConfig = case _databasePath of
Memory ->
-- For in memory databases, we reuse the write connection for queries
Pool.defaultPoolConfig
(pure _writeConnection)
-- we never close the connection as it's used by the write part
(const $ pure ())
tenHours
1
Storage _file ->
Pool.defaultPoolConfig
(readOnlyConnection _databasePath)
SQL.close
tenHours
poolSize
_readConnectionPool <- liftIO $ Pool.newPool poolConfig
traverse_ (liftIO . SQL.execute_ _writeConnection) _creationStatements
-- allow for concurrent insert/query.
-- see SQLite WAL, https://www.sqlite.org/wal.html
liftIO $ SQL.execute_ _writeConnection "PRAGMA journal_mode=WAL"
_dbLastStable <- getLastStablePoint _writeConnection
let indexer =
SQLiteIndexer
{ _databasePath
, _writeConnection
, _readConnectionPool
, _insertPlan
, _rollbackPlan
, _setLastStablePointQuery
, _dbLastSync = _dbLastStable
, _dbLastStable
}
rollback _dbLastStable indexer
{- | A smart constructor for indexer that want to map an event to a single table.
We just have to set the type family of `InsertRecord event` to `[param]` and
then to provide the expected parameters.
It is monomorphic restriction of 'mkSqliteIndexer'
-}
mkSingleInsertSqliteIndexer
:: forall m event param
. ( MonadIO m
, MonadError IndexerError m
, HasGenesis (Point event)
, SQL.FromRow (Point event)
, SQL.ToRow (Point event)
, SQL.ToRow param
, Ord (Point event)
)
=> SQLiteDBLocation
-> (Timed (Point event) event -> param)
-- ^ extract @param@ out of a 'Timed'
-> SQL.Query
-- ^ the creation query
-> SQL.Query
-- ^ the insert query
-> SQLRollbackPlan (Point event)
-- ^ the rollback query
-> SetLastStablePointQuery
-- ^ The SQL query to set the last stable point of the indexer.
-> GetLastStablePointQuery
-- ^ The SQL query to fetch the last stable point from the indexer.
-> m (SQLiteIndexer event)
mkSingleInsertSqliteIndexer path extract create insert rollback' =
mkSqliteIndexer path [create] [[SQLInsertPlan (pure . extract) insert]] [rollback']
-- | Map SQLite errors to an indexer error
handleSQLErrors :: IO a -> IO (Either IndexerError a)
handleSQLErrors value =
fmap Right value
`catches` [ Handler (\(x :: SQL.FormatError) -> pure . Left . InvalidIndexer . Text.pack $ show x)
, Handler (\(x :: SQL.ResultError) -> pure . Left . InvalidIndexer . Text.pack $ show x)
, Handler (\(x :: SQL.SQLError) -> pure . Left . IndexerInternalError . Text.pack $ show x)
]
-- | Run a list of insert queries in one single transaction.
runIndexQueriesStep
:: SQL.Connection
-> [Timed (Point event) event]
-> [SQLInsertPlan event]
-> IO ()
runIndexQueriesStep _ _ [] = pure ()
runIndexQueriesStep c events plan =
let runIndexQuery (SQLInsertPlan planExtractor planInsert) = do
let rows = planExtractor =<< events
case rows of
[] -> pure ()
[x] -> SQL.execute c planInsert x
_nonEmpty -> SQL.executeMany c planInsert rows
in Async.mapConcurrently_ runIndexQuery plan
-- | Run a list of insert queries in one single transaction.
runIndexPlan
:: SQL.Connection
-> [Timed (Point event) event]
-> [[SQLInsertPlan event]]
-> IO ()
runIndexPlan c = traverse_ . runIndexQueriesStep c
-- | Run a list of insert queries in one single transaction.
runIndexQueries
:: (MonadIO m, MonadError IndexerError m)
=> SQL.Connection
-> [Timed (Point event) (Maybe event)]
-> [[SQLInsertPlan event]]
-> m ()
runIndexQueries c events' plan =
let nonEmptyEvents = (catMaybes . toList $ sequence <$> events')
indexEvent = case nonEmptyEvents of
[] -> Nothing
_nonEmpty -> Just $ runIndexPlan c nonEmptyEvents plan
in case indexEvent of
Nothing -> pure ()
Just runIndexers ->
either throwError pure <=< liftIO $
handleSQLErrors (SQL.withTransaction c runIndexers)
indexEvents
:: (MonadIO m, MonadError IndexerError m)
=> [Timed (Point event) (Maybe event)]
-> SQLiteIndexer event
-> m (SQLiteIndexer event)
indexEvents [] indexer = pure indexer
indexEvents evts@(e : _) indexer = do
let setDbLastSync p = pure . (dbLastSync .~ p)
runIndexQueries (indexer ^. writeConnection) evts (indexer ^. insertPlan)
setDbLastSync (e ^. point) indexer
runLastStablePointQuery
:: (MonadError IndexerError m, MonadIO m, SQL.FromRow r)
=> SQL.Connection
-> GetLastStablePointQuery
-> m (Maybe r)
runLastStablePointQuery conn (GetLastStablePointQuery q) =
either throwError (pure . listToMaybe) <=< liftIO $
handleSQLErrors (SQL.query_ conn q)
instance
(MonadIO m, MonadError IndexerError m, SQL.ToRow (Point event))
=> IsIndex m event SQLiteIndexer
where
index = indexEvents . pure
indexAllDescending = indexEvents . toList
rollback p indexer = do
let c = indexer ^. writeConnection
deleteAllQuery tName = "DELETE FROM " <> tName
deleteAll = SQL.execute_ c . deleteAllQuery . SQL.Query . Text.pack
deleteUntilQuery tName pName =
deleteAllQuery tName <> " WHERE " <> pName <> " > :point"
deleteUntil :: (SQL.ToField a) => String -> String -> a -> IO ()
deleteUntil tName pName pt =
SQL.executeNamed
c
(deleteUntilQuery (SQL.Query $ Text.pack tName) (SQL.Query $ Text.pack pName))
[":point" SQL.:= pt]
rollbackTable (SQLRollbackPlan tableName pointName extractor) =
case extractor p of
Nothing -> deleteAll tableName
Just pt -> deleteUntil tableName pointName pt
liftIO $
SQL.withTransaction c $
traverse_ rollbackTable (indexer ^. rollbackPlan)
pure $ indexer & dbLastSync .~ p
setLastStablePoint p indexer = do
let c = indexer ^. writeConnection
SetLastStablePointQuery query = indexer ^. setLastStablePointQuery
liftIO $ SQL.execute c query p
pure $ indexer & dbLastStable .~ p
instance (Monad m) => IsSync m event SQLiteIndexer where
lastStablePoint indexer = pure $ indexer ^. dbLastStable
lastSyncPoint indexer = pure $ indexer ^. dbLastSync
instance (MonadIO m) => Closeable m SQLiteIndexer where
close indexer = liftIO $ do
SQL.close $ indexer ^. writeConnection
Pool.destroyAllResources $ indexer ^. readConnectionPool
{- | A helper for the definition of the @Queryable@ typeclass for 'SQLiteIndexer'
The helper just remove a bit of the boilerplate needed to transform data
to query the database.
It doesn't contain any logic, except a check for 'AheadOfLastSync' error,
in which case it throws the 'AheadOfLastSync' exception with a partial result.
If you don't want to query the database on a partial result,
use 'querySyncedOnlySQLiteIndexerWith'
It doesn't filter the result based on the given data point.
-}
querySQLiteIndexerWith
:: ( MonadIO m
, MonadError (QueryError query) m
, Ord (Point event)
, SQL.FromRow r
)
=> (Point event -> query -> [SQL.NamedParam])
-- ^ A preprocessing of the query, to obtain SQL parameters
-> (query -> SQL.Query)
-- ^ The sqlite query statement
-> (query -> [r] -> Result query)
-- ^ Post processing of the result, to obtain the final result
-> Point event
-> query
-> SQLiteIndexer event
-> m (Result query)
querySQLiteIndexerWith toNamedParam sqlQuery fromRows p q indexer =
do
res <- liftIO $
Pool.withResource (indexer ^. readConnectionPool) $
\c -> SQL.queryNamed c (sqlQuery q) (toNamedParam p q)
when (p > indexer ^. dbLastSync) $
throwError (AheadOfLastSync $ Just $ fromRows q res)
pure $ fromRows q res
{- | A helper for the definition of 'queryLatest' in the @Queryable@ typeclass
for 'SQLiteIndexer'.
The helper just remove a bit of the boilerplate needed to transform data
to query the database.
It also assumes that the SQL query will deal with the latest part
(we don't use a side query to access the latest sync point).
-}
queryLatestSQLiteIndexerWith
:: (MonadIO m)
=> (SQL.FromRow r)
=> (query -> [SQL.NamedParam])
-- ^ A preprocessing of the query, to obtain SQL parameters
-> (query -> SQL.Query)
-- ^ The sqlite query statement
-> (query -> [r] -> Result query)
-- ^ Post processing of the result, to obtain the final result
-> query
-> SQLiteIndexer event
-> m (Result query)
queryLatestSQLiteIndexerWith toNamedParam sqlQuery fromRows q indexer =
do
res <- liftIO $
Pool.withResource (indexer ^. readConnectionPool) $
\c -> SQL.queryNamed c (sqlQuery q) (toNamedParam q)
pure $ fromRows q res
{- | A helper for the definition of the 'Queryable' typeclass for 'SQLiteIndexer'.
The helper just remove a bit of the boilerplate needed to transform data
to query the database.
It doesn't contain any logic, except a check for 'AheadOfLastSync' error,
in which case it throws the 'AheadOfLastSync' without any result attached.
It doesn't filter the result based on the given data point.
-}
querySyncedOnlySQLiteIndexerWith
:: (MonadIO m)
=> (MonadError (QueryError query) m)
=> (Ord (Point event))
=> (SQL.FromRow r)
=> (Point event -> query -> [SQL.NamedParam])
-- ^ A preprocessing of the query, to obtain SQL parameters
-> (query -> SQL.Query)
-- ^ The sqlite query statement
-> (query -> [r] -> Result query)
-- ^ Post processing of the result, to obtain the final result
-> Point event
-> query
-> SQLiteIndexer event
-> m (Result query)
querySyncedOnlySQLiteIndexerWith toNamedParam sqlQuery fromRows p q indexer =
do
when (p > indexer ^. dbLastSync) $
throwError (AheadOfLastSync Nothing)
res <- liftIO $
Pool.withResource (indexer ^. readConnectionPool) $
\c -> SQL.queryNamed c (sqlQuery q) (toNamedParam p q)
pure $ fromRows q res