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
use necsim_core::{
    landscape::{IndexedLocation, Location},
    lineage::GlobalLineageReference,
};
use necsim_core_bond::PositiveF64;

use rusqlite::{named_params, types::Value};

use crate::{LastEventState, SpeciesIdentity};

use super::{IndividualSpeciesSQLiteReporter, SpeciesLocationsMode};

const METADATA_TABLE: &str = "__SPECIES_REPORTER_META";

impl IndividualSpeciesSQLiteReporter {
    pub(super) fn store_individual_origin(
        &mut self,
        lineage: &GlobalLineageReference,
        origin: &IndexedLocation,
    ) {
        self.origins.insert(lineage.clone(), origin.clone());
    }

    pub(super) fn store_individual_speciation(
        &mut self,
        lineage: &GlobalLineageReference,
        origin: &IndexedLocation,
        time: PositiveF64,
    ) {
        // Resolve the actual parent, irrespective of duplicate individuals
        let mut parent = lineage;
        while let Some(parent_parent) = self.parents.get(parent) {
            parent = parent_parent;
        }

        self.species.insert(
            parent.clone(),
            SpeciesIdentity::from_speciation(origin, time),
        );
    }

    pub(super) fn store_individual_coalescence(
        &mut self,
        child: &GlobalLineageReference,
        parent: &GlobalLineageReference,
    ) {
        // Resolve the actual child, irrespective of duplicate individuals
        let mut child = child;
        while let Some(child_parent) = self.parents.get(child) {
            child = child_parent;
        }
        let child = child.clone();

        // Resolve the actual parent, irrespective of duplicate individuals
        let mut parent = parent;
        while let Some(parent_parent) = self.parents.get(parent) {
            parent = parent_parent;
        }
        let parent = parent.clone();

        // Prevent a lookup-loop, can occur after `Resume`
        if child != parent {
            self.parents.insert(child, parent);
        }
    }

    #[allow(clippy::too_many_lines)]
    pub(super) fn initialise_sqlite_connection(&mut self) -> rusqlite::Result<()> {
        self.connection
            .pragma_update(None, "cache_size", self.cache.get())?;

        // Create the species locations table in `Create` mode
        if let SpeciesLocationsMode::Create = self.mode {
            self.connection.execute_batch(&format!(
                "CREATE TABLE {} (
                            id      INTEGER PRIMARY KEY NOT NULL,
                            x       INTEGER NOT NULL,
                            y       INTEGER NOT NULL,
                            i       INTEGER NOT NULL,
                            parent  INTEGER,
                            species TEXT
                        );
                        CREATE TABLE {METADATA_TABLE} (
                            key     TEXT PRIMARY KEY NOT NULL,
                            value   TEXT NOT NULL
                        );",
                self.table,
            ))?;
        }

        let mut schema: Vec<Vec<Value>> = Vec::new();

        // Collect the schema information for the species locations table
        self.connection
            .pragma(None, "table_info", &self.table, |row| {
                let mut schema_row = Vec::new();

                for col in 0..row.as_ref().column_count() {
                    schema_row.push(Value::from(row.get_ref(col)?));
                }

                schema.push(schema_row);

                Ok(())
            })?;

        // Check that the schema of the species locations table matches
        if schema
            != vec![
                vec![
                    Value::Integer(0),
                    Value::Text(String::from("id")),
                    Value::Text(String::from("INTEGER")),
                    Value::Integer(1),
                    Value::Null,
                    Value::Integer(1),
                ],
                vec![
                    Value::Integer(1),
                    Value::Text(String::from("x")),
                    Value::Text(String::from("INTEGER")),
                    Value::Integer(1),
                    Value::Null,
                    Value::Integer(0),
                ],
                vec![
                    Value::Integer(2),
                    Value::Text(String::from("y")),
                    Value::Text(String::from("INTEGER")),
                    Value::Integer(1),
                    Value::Null,
                    Value::Integer(0),
                ],
                vec![
                    Value::Integer(3),
                    Value::Text(String::from("i")),
                    Value::Text(String::from("INTEGER")),
                    Value::Integer(1),
                    Value::Null,
                    Value::Integer(0),
                ],
                vec![
                    Value::Integer(4),
                    Value::Text(String::from("parent")),
                    Value::Text(String::from("INTEGER")),
                    Value::Integer(0),
                    Value::Null,
                    Value::Integer(0),
                ],
                vec![
                    Value::Integer(5),
                    Value::Text(String::from("species")),
                    Value::Text(String::from("TEXT")),
                    Value::Integer(0),
                    Value::Null,
                    Value::Integer(0),
                ],
            ]
        {
            return Err(rusqlite::Error::SqliteFailure(
                rusqlite::ffi::Error {
                    code: rusqlite::ffi::ErrorCode::SchemaChanged,
                    extended_code: 0,
                },
                Some(format!(
                    "Invalid schema for the species locations table {}",
                    self.table
                )),
            ));
        }

        let mut schema: Vec<Vec<Value>> = Vec::new();

        // Collect the schema information for the metadata table
        self.connection
            .pragma(None, "table_info", METADATA_TABLE, |row| {
                let mut schema_row = Vec::new();

                for col in 0..row.as_ref().column_count() {
                    schema_row.push(Value::from(row.get_ref(col)?));
                }

                schema.push(schema_row);

                Ok(())
            })?;

        // Check that the schema of the metadata table matches
        if schema
            != vec![
                vec![
                    Value::Integer(0),
                    Value::Text(String::from("key")),
                    Value::Text(String::from("TEXT")),
                    Value::Integer(1),
                    Value::Null,
                    Value::Integer(1),
                ],
                vec![
                    Value::Integer(1),
                    Value::Text(String::from("value")),
                    Value::Text(String::from("TEXT")),
                    Value::Integer(1),
                    Value::Null,
                    Value::Integer(0),
                ],
            ]
        {
            return Err(rusqlite::Error::SqliteFailure(
                rusqlite::ffi::Error {
                    code: rusqlite::ffi::ErrorCode::SchemaChanged,
                    extended_code: 0,
                },
                Some(String::from(
                    "Invalid schema for the internal metadata table",
                )),
            ));
        }

        // Early return for the `Create` mode
        if let SpeciesLocationsMode::Create = self.mode {
            return Ok(());
        }

        let mut statement = self.connection.prepare(&format!(
            "SELECT id, x, y, i, parent, species FROM {}",
            self.table
        ))?;
        let mut query = statement.query([])?;

        // Resume from the existing species locations in the table
        while let Some(row) = query.next()? {
            let id: i64 = row.get("id")?;
            let x: i32 = row.get("x")?;
            let y: i32 = row.get("y")?;
            let i: i32 = row.get("i")?;

            let parent: Option<i64> = row.get("parent")?;
            let species: Option<String> = row.get("species")?;

            let id = unsafe { GlobalLineageReference::from_inner(from_i64(id)) };

            // Populate the individual `origins` lookup
            self.origins.insert(
                id.clone(),
                IndexedLocation::new(Location::new(from_i32(x), from_i32(y)), from_i32(i)),
            );

            if let Some(parent) = parent {
                let parent = unsafe { GlobalLineageReference::from_inner(from_i64(parent)) };

                // Populate the individual `parents` lookup
                self.parents.insert(id.clone(), parent);
            }

            if let Some(species) = species {
                let mut identity = [0_u8; 24];

                // Try to parse the species identity from its String form
                hex::decode_to_slice(&species, &mut identity).map_err(|_| {
                    rusqlite::Error::SqliteFailure(
                        rusqlite::ffi::Error {
                            code: rusqlite::ffi::ErrorCode::TypeMismatch,
                            extended_code: 0,
                        },
                        Some(format!(
                            "Invalid species identity {species:?} for individual #{id}",
                        )),
                    )
                })?;

                // Populate the individual `species` lookup
                self.species.insert(id, SpeciesIdentity::from(identity));
            }
        }

        let last_event: String = self
            .connection
            .query_row(
                &format!("SELECT value FROM {METADATA_TABLE} WHERE key='last-event'",),
                [],
                |row| row.get("value"),
            )
            .map_err(|_| {
                rusqlite::Error::SqliteFailure(
                    rusqlite::ffi::Error {
                        code: rusqlite::ffi::ErrorCode::NotFound,
                        extended_code: 0,
                    },
                    Some(String::from("Failed to fetch the reporter resume metadata")),
                )
            })?;

        let LastEventState {
            last_parent_prior_time,
            last_speciation_event,
            last_dispersal_event,
        } = LastEventState::from_string(&last_event).map_err(|()| {
            rusqlite::Error::SqliteFailure(
                rusqlite::ffi::Error {
                    code: rusqlite::ffi::ErrorCode::TypeMismatch,
                    extended_code: 0,
                },
                Some(String::from(
                    "Failed to decode the reporter resume metadata",
                )),
            )
        })?;

        self.last_parent_prior_time = last_parent_prior_time;
        self.last_speciation_event = last_speciation_event;
        self.last_dispersal_event = last_dispersal_event;

        Ok(())
    }

    pub(super) fn output_to_database(mut self) -> rusqlite::Result<()> {
        let tx = self
            .connection
            .transaction_with_behavior(rusqlite::TransactionBehavior::Exclusive)?;

        let mut insertion = tx.prepare(&format!(
            "INSERT OR REPLACE INTO {} VALUES (:id, :x, :y, :i, :parent, :species)",
            self.table,
        ))?;

        // Lineage ancestor union-find with path compression
        let mut ancestors = self.parents.clone();

        let mut family = Vec::new();

        for (lineage, origin) in self.origins {
            // Find the ancestor that originated the species
            let mut ancestor = lineage.clone();
            while let Some(ancestor_parent) = ancestors.get(&ancestor) {
                family.push(ancestor.clone());
                ancestor = ancestor_parent.clone();
            }

            // Compress the ancestry paths for all visited lineages
            for child in family.drain(..) {
                ancestors.insert(child, ancestor.clone());
            }

            // Positional parameters boost performance
            insertion.execute(rusqlite::params![
                /* :id */ to_i64(unsafe { lineage.clone().into_inner() }),
                /* :x */ to_i32(origin.location().x()),
                /* :y */ to_i32(origin.location().y()),
                /* :i */ to_i32(origin.index()),
                /* :parent */
                self.parents
                    .get(&lineage)
                    .map(|parent| to_i64(unsafe { parent.clone().into_inner() })),
                /* :species */
                self.species
                    .get(&ancestor)
                    .map(|species| hex::encode(**species)),
            ])?;
        }

        insertion.finalize()?;

        let last_event_state = LastEventState {
            last_parent_prior_time: self.last_parent_prior_time,
            last_speciation_event: self.last_speciation_event,
            last_dispersal_event: self.last_dispersal_event,
        }
        .into_string()
        .map_err(|()| {
            rusqlite::Error::SqliteFailure(
                rusqlite::ffi::Error {
                    code: rusqlite::ffi::ErrorCode::TypeMismatch,
                    extended_code: 0,
                },
                Some(String::from(
                    "Failed to encode the reporter resume metadata",
                )),
            )
        })?;

        tx.execute(
            &format!("INSERT OR REPLACE INTO {METADATA_TABLE} VALUES (:key, :value)",),
            named_params! { ":key": "last-event", ":value": last_event_state },
        )?;

        tx.commit()?;

        self.connection.close().map_err(|(_, err)| err)
    }
}

const fn to_i32(x: u32) -> i32 {
    i32::from_ne_bytes(x.to_ne_bytes())
}

const fn to_i64(x: u64) -> i64 {
    i64::from_ne_bytes(x.to_ne_bytes())
}

const fn from_i32(x: i32) -> u32 {
    u32::from_ne_bytes(x.to_ne_bytes())
}

const fn from_i64(x: i64) -> u64 {
    u64::from_ne_bytes(x.to_ne_bytes())
}