manby 0.2.6

Builds vector tilesets from geographic features, like tippecanoe, several times faster
Documentation
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
//! MBTiles, written by a single thread in large transactions.
//!
//! The database has tippecanoe's schema, pragmas, rows and metadata. Callers
//! hash their tiles on their own threads and hand them to the writer thread
//! over a bounded channel; the writer inserts them with prepared statements and
//! commits every so often instead of after every tile, as tippecanoe does.
//! WebAssembly has no threads, so there the callers write the tiles
//! themselves, one at a time.

use std::collections::HashSet;
use std::ffi::{CStr, c_double, c_int};
use std::fs;
use std::sync::mpsc::{Receiver, SyncSender, sync_channel};
use std::sync::{Arc, Mutex};
use std::thread::JoinHandle;

use rusqlite::Connection;
use tippecanoe::errors::{EXIT_CLOSE, EXIT_EXISTS, EXIT_IMPOSSIBLE, EXIT_OPEN, EXIT_SQLITE};
use tippecanoe::mbtiles::{Metadata, c_str, sqlite3_errmsg, sqlite3_exec, sqlite3_open};
use tippecanoe::text::fnv1a;
use tippecanoe::write_json::JsonWriter;

use super::{SinkSettings, TileSink, check_tile, tileset_metadata};
use crate::{Error, Result};

/// Tiles or bytes after which the writer commits and starts a new transaction.
const BATCH_TILES: usize = 100_000;
const BATCH_BYTES: usize = 256 << 20;

/// `sqlite3_mprintf(fmt, ...)`, returning an owned copy. Each argument must
/// have the C type its conversion expects.
macro_rules! mprintf {
    ($fmt:literal $(, $arg:expr)* $(,)?) => {
        // SAFETY: the format is a C string literal, the arguments have the C
        // types its conversions expect and outlive the call, and sqlite3_take
        // takes over the result.
        unsafe {
            tippecanoe::mbtiles::sqlite3_take(rusqlite::ffi::sqlite3_mprintf(
                ($fmt as &CStr).as_ptr() $(, $arg)*
            ))
        }
    };
}

/// More metadata keys of manby's own, and their values.
type Extra = Vec<(String, String)>;

enum Message {
    Tile {
        z: u8,
        x: u32,
        y: u32,
        hash: u64,
        data: Vec<u8>,
    },
    Erase {
        z: u8,
        reply: SyncSender<Result<()>>,
    },
    Finish {
        metadata: Box<Metadata>,
        extra: Extra,
    },
}

pub struct MbtilesSink {
    sender: Option<SyncSender<Message>>,
    writer: Option<JoinHandle<Result<()>>>,
    /// On WebAssembly, which has no threads, the writer itself, which takes
    /// each message as it is sent.
    here: Option<Mutex<Writer>>,
    /// The writer's error, once it has stopped because of one.
    failure: Arc<Mutex<Option<(i32, String)>>>,
    tile_format: crate::mlt::TileFormat,
}

fn sqlite_error(message: String) -> Error {
    Error::new(EXIT_SQLITE, message)
}

impl MbtilesSink {
    /// Opens or creates `path` the way tippecanoe does for `-o x.mbtiles`
    /// (`mbtiles_open`), and starts the writer thread.
    pub fn create(path: &str, settings: &SinkSettings) -> Result<MbtilesSink> {
        if settings.force {
            let _ = fs::remove_file(path);
        }
        let db = mbtiles_open(path, settings)?;
        let failure = Arc::new(Mutex::new(None));
        let tile_format = settings.tile_format;
        let allow_existing = settings.allow_existing;
        if cfg!(target_family = "wasm") {
            return Ok(MbtilesSink {
                sender: None,
                writer: None,
                here: Some(Mutex::new(Writer::new(db, allow_existing)?)),
                failure,
                tile_format,
            });
        }

        let (sender, receiver) = sync_channel(4096);
        let writer_failure = failure.clone();
        let writer = std::thread::Builder::new()
            .name("mbtiles writer".to_string())
            .spawn(move || {
                let result = write_tiles(db, receiver, allow_existing);
                if let Err(e) = &result {
                    *writer_failure.lock().unwrap_or_else(|p| p.into_inner()) =
                        Some((e.exit_code, e.message.clone()));
                }
                result
            })
            .map_err(|e| Error::new(EXIT_IMPOSSIBLE, format!("mbtiles writer thread: {}", e)))?;

        Ok(MbtilesSink {
            sender: Some(sender),
            writer: Some(writer),
            here: None,
            failure,
            tile_format,
        })
    }

    fn send(&self, message: Message) -> Result<()> {
        if let Some(here) = &self.here {
            if let Some((code, message)) = &*self.failure.lock().unwrap_or_else(|p| p.into_inner())
            {
                return Err(Error::new(*code, message.clone()));
            }
            let taken = here.lock().unwrap_or_else(|p| p.into_inner()).take(message);
            if let Err(e) = &taken {
                *self.failure.lock().unwrap_or_else(|p| p.into_inner()) =
                    Some((e.exit_code, e.message.clone()));
            }
            return taken.map(|_| ());
        }
        let sent = self.sender.as_ref().map(|s| s.send(message));
        match sent {
            Some(Ok(())) => Ok(()),
            _ => Err(self.writer_error()),
        }
    }

    fn writer_error(&self) -> Error {
        match &*self.failure.lock().unwrap_or_else(|p| p.into_inner()) {
            Some((code, message)) => Error::new(*code, message.clone()),
            None => Error::new(EXIT_IMPOSSIBLE, "mbtiles writer stopped"),
        }
    }

    /// Closes the database of a writer without a thread, unless it failed.
    fn close_here(&mut self) -> Result<()> {
        match self.here.take() {
            Some(here)
                if self
                    .failure
                    .lock()
                    .unwrap_or_else(|p| p.into_inner())
                    .is_none() =>
            {
                here.into_inner().unwrap_or_else(|p| p.into_inner()).close()
            }
            _ => Ok(()),
        }
    }
}

impl Drop for MbtilesSink {
    fn drop(&mut self) {
        // Without finish(), the writer commits what it has and closes.
        self.sender.take();
        if let Some(writer) = self.writer.take() {
            let _ = writer.join();
        }
        let _ = self.close_here();
    }
}

impl TileSink for MbtilesSink {
    fn write_tile(&self, z: u8, x: u32, y: u32, data: &[u8]) -> Result<()> {
        check_tile(z, x, y)?;
        self.send(Message::Tile {
            z,
            x,
            y,
            hash: fnv1a(data),
            data: data.to_vec(),
        })
    }

    fn erase_zoom(&self, z: u8) -> Result<()> {
        let (reply, answer) = sync_channel(1);
        self.send(Message::Erase { z, reply })?;
        answer.recv().unwrap_or_else(|_| Err(self.writer_error()))
    }

    fn finish(self: Box<Self>, metadata: &Metadata) -> Result<()> {
        self.finish_with(metadata, &[])
    }

    fn finish_with(
        mut self: Box<Self>,
        metadata: &Metadata,
        extra: &[(String, String)],
    ) -> Result<()> {
        let sent = self.send(Message::Finish {
            metadata: Box::new(tileset_metadata(metadata, self.tile_format).into_owned()),
            extra: extra.to_vec(),
        });
        self.sender.take();
        let joined = match self.writer.take() {
            Some(writer) => writer
                .join()
                .unwrap_or_else(|_| Err(Error::new(EXIT_IMPOSSIBLE, "mbtiles writer panicked"))),
            None => self.close_here(),
        };
        sent.and(joined)
    }
}

/// tippecanoe's `mbtiles_open`, returning errors instead of exiting. With
/// `allow_existing`, the complaints about tables that already exist are
/// printed and the database is used as it is, as in tippecanoe.
pub fn mbtiles_open(path: &str, settings: &SinkSettings) -> Result<Connection> {
    let program = &settings.program;
    let db = sqlite3_open(path)
        .map_err(|msg| Error::new(EXIT_OPEN, format!("{}: {}: {}", program, path, msg)))?;

    for pragma in [
        c"PRAGMA synchronous=0",
        c"PRAGMA locking_mode=EXCLUSIVE",
        c"PRAGMA journal_mode=DELETE",
    ] {
        sqlite3_exec(&db, pragma)
            .map_err(|err| sqlite_error(format!("{}: async: {}", program, err)))?;
    }

    let creates: [(&CStr, &str); 7] = [
        (c"CREATE TABLE metadata (name text, value text);", ""),
        (c"create unique index name on metadata (name);", "index metadata"),
        // "map" maps z/x/y coordinates to a content hash
        (
            c"CREATE TABLE map (zoom_level INTEGER, tile_column INTEGER, tile_row INTEGER, tile_id TEXT);",
            "create map table",
        ),
        (
            c"CREATE UNIQUE INDEX map_index ON map (zoom_level, tile_column, tile_row);",
            "create map index",
        ),
        // "images" maps a content hash to tile contents, per zoom level
        (
            c"CREATE TABLE images (zoom_level integer, tile_data blob, tile_id text);",
            "create images table",
        ),
        (
            c"CREATE UNIQUE INDEX images_id ON images (zoom_level, tile_id);",
            "create images index",
        ),
        // "tiles" is a view that retrieves content from "images"
        // via the content hash looked up from "map".
        (
            c"CREATE VIEW tiles AS SELECT map.zoom_level AS zoom_level, map.tile_column AS tile_column, map.tile_row AS tile_row, images.tile_data AS tile_data FROM map JOIN images ON images.tile_id = map.tile_id and images.zoom_level = map.zoom_level;",
            "create tiles view",
        ),
    ];
    for (sql, what) in creates {
        if let Err(err) = sqlite3_exec(&db, sql) {
            let message = if what.is_empty() {
                format!(
                    "{}: Tileset \"{}\" already exists. You can use --force if you want to delete the old tileset.\n{}: {}",
                    program, path, program, err
                )
            } else {
                format!("{}: {}: {}", program, what, err)
            };
            if !settings.allow_existing {
                return Err(Error::new(EXIT_EXISTS, message));
            }
            eprintln!("{}", message);
        }
    }

    // A larger page cache makes the inserts faster; it is not stored in the file.
    sqlite3_exec(&db, c"PRAGMA cache_size=-262144")
        .map_err(|err| sqlite_error(format!("cache_size: {}", err)))?;

    Ok(db)
}

/// The writer thread: inserts tiles until `Finish`, then writes the metadata
/// and closes the database as `mbtiles_close` does.
fn write_tiles(db: Connection, receiver: Receiver<Message>, allow_existing: bool) -> Result<()> {
    let mut writer = Writer::new(db, allow_existing)?;
    for message in receiver.iter() {
        if !writer.take(message)? {
            break;
        }
    }
    writer.close()
}

/// The database and what has been written to it, in a transaction that is
/// committed every so often.
struct Writer {
    db: Connection,
    allow_existing: bool,
    /// (zoom, hash) of the images this writer has stored, so that a tile
    /// seen before at the same zoom does not replace its row again.
    stored: HashSet<(u8, u64)>,
    tiles_in_batch: usize,
    bytes_in_batch: usize,
    finish: Option<(Box<Metadata>, Extra)>,
}

impl Writer {
    fn new(db: Connection, allow_existing: bool) -> Result<Writer> {
        let writer = Writer {
            db,
            allow_existing,
            stored: HashSet::new(),
            tiles_in_batch: 0,
            bytes_in_batch: 0,
            finish: None,
        };
        for (sql, what) in [
            (INSERT_IMAGE, "images"),
            (INSERT_MAP, "map"),
            (DELETE_MAP, "delete map"),
            (DELETE_IMAGES, "delete images"),
        ] {
            writer.prepare(sql, what)?;
        }
        writer.begin()?;
        Ok(writer)
    }

    fn prepare(&self, sql: &str, what: &str) -> Result<rusqlite::CachedStatement<'_>> {
        self.db
            .prepare_cached(sql)
            .map_err(|_| sqlite_error(format!("sqlite3 {} prep failed", what)))
    }

    fn begin(&self) -> Result<()> {
        sqlite3_exec(&self.db, c"BEGIN").map_err(|e| sqlite_error(format!("BEGIN: {}", e)))
    }

    fn commit(&self) -> Result<()> {
        sqlite3_exec(&self.db, c"COMMIT").map_err(|e| sqlite_error(format!("COMMIT: {}", e)))
    }

    fn failed(&self, what: &str) -> Error {
        sqlite_error(format!(
            "sqlite3 {} failed: {}",
            what,
            sqlite3_errmsg(&self.db)
        ))
    }

    /// Takes one message, and returns whether more may follow.
    fn take(&mut self, message: Message) -> Result<bool> {
        match message {
            Message::Tile {
                z,
                x,
                y,
                hash,
                data,
            } => {
                let hash_text = hash.to_string();
                if self.stored.insert((z, hash)) {
                    self.prepare(INSERT_IMAGE, "images")?
                        .execute(rusqlite::params![z as i32, hash_text, data])
                        .map_err(|_| self.failed("images insert"))?;
                }
                let tile_row = ((1i64 << z) - 1 - y as i64) as i32;
                self.prepare(INSERT_MAP, "map")?
                    .execute(rusqlite::params![z as i32, x as i32, tile_row, hash_text])
                    .map_err(|_| self.failed("map insert"))?;

                self.tiles_in_batch += 1;
                self.bytes_in_batch += data.len();
                if self.tiles_in_batch >= BATCH_TILES || self.bytes_in_batch >= BATCH_BYTES {
                    self.commit()?;
                    self.begin()?;
                    self.tiles_in_batch = 0;
                    self.bytes_in_batch = 0;
                }
                Ok(true)
            }
            Message::Erase { z, reply } => {
                let erased = self
                    .prepare(DELETE_MAP, "delete map")
                    .and_then(|mut s| s.execute([z as i32]).map_err(|_| self.failed("delete map")))
                    .and_then(|_| {
                        self.prepare(DELETE_IMAGES, "delete images")
                            .and_then(|mut s| {
                                s.execute([z as i32])
                                    .map_err(|_| self.failed("delete images"))
                            })
                    })
                    .map(|_| ());
                self.stored.retain(|&(zoom, _)| zoom != z);
                let failed = erased
                    .as_ref()
                    .err()
                    .map(|e| Error::new(e.exit_code, e.message.clone()));
                let _ = reply.send(erased);
                match failed {
                    Some(e) => Err(e),
                    None => Ok(true),
                }
            }
            Message::Finish { metadata, extra } => {
                self.finish = Some((metadata, extra));
                Ok(false)
            }
        }
    }

    /// Commits, writes the metadata if `Finish` came, and closes.
    fn close(self) -> Result<()> {
        self.commit()?;
        let Writer {
            db,
            allow_existing,
            finish,
            ..
        } = self;
        db.flush_prepared_statement_cache();
        if let Some((m, extra)) = finish {
            mbtiles_write_metadata(&db, &m, allow_existing)?;
            for (k, v) in extra {
                db.execute(
                    "INSERT INTO metadata (name, value) VALUES (?1, ?2)",
                    (&k, &v),
                )
                .map_err(|e| sqlite_error(format!("set {} in metadata: {}", k, e)))?;
            }
        }
        mbtiles_close(db)
    }
}

const INSERT_IMAGE: &str = "replace into images (zoom_level, tile_id, tile_data) values (?, ?, ?)";
const INSERT_MAP: &str =
    "insert into map (zoom_level, tile_column, tile_row, tile_id) values (?, ?, ?, ?)";
const DELETE_MAP: &str = "delete from map where zoom_level = ?";
const DELETE_IMAGES: &str = "delete from images where zoom_level = ?";

/// tippecanoe's `mbtiles_close`.
fn mbtiles_close(db: Connection) -> Result<()> {
    sqlite3_exec(&db, c"ANALYZE;")
        .map_err(|err| sqlite_error(format!("ANALYZE failed: {}", err)))?;
    db.close().map_err(|(db, _)| {
        Error::new(
            EXIT_CLOSE,
            format!("could not close database: {}", sqlite3_errmsg(&db)),
        )
    })
}

/// Runs one metadata `INSERT`. On failure, the error is returned, or with
/// `forcetable` printed and passed over, as in tippecanoe.
fn exec_metadata(db: &Connection, sql: &CStr, what: &str, forcetable: bool) -> Result<()> {
    if let Err(err) = sqlite3_exec(db, sql) {
        let message = format!("{}: {}", what, err);
        if !forcetable {
            return Err(sqlite_error(message));
        }
        eprintln!("{}", message);
    }
    Ok(())
}

/// tippecanoe's `mbtiles_write_metadata`, returning errors instead of exiting.
pub fn mbtiles_write_metadata(db: &Connection, m: &Metadata, forcetable: bool) -> Result<()> {
    let name = c_str(&m.name);
    let sql = mprintf!(
        c"INSERT INTO metadata (name, value) VALUES ('name', %Q);",
        name.as_ptr()
    );
    exec_metadata(db, &sql, "set name in metadata", forcetable)?;

    let description = c_str(&m.description);
    let sql = mprintf!(
        c"INSERT INTO metadata (name, value) VALUES ('description', %Q);",
        description.as_ptr()
    );
    exec_metadata(db, &sql, "set description in metadata", forcetable)?;

    let sql = mprintf!(
        c"INSERT INTO metadata (name, value) VALUES ('version', %d);",
        m.version as c_int
    );
    exec_metadata(db, &sql, "set version ", forcetable)?;

    let sql = mprintf!(
        c"INSERT INTO metadata (name, value) VALUES ('minzoom', %d);",
        m.minzoom as c_int
    );
    exec_metadata(db, &sql, "set minzoom", forcetable)?;

    let sql = mprintf!(
        c"INSERT INTO metadata (name, value) VALUES ('maxzoom', %d);",
        m.maxzoom as c_int
    );
    exec_metadata(db, &sql, "set maxzoom", forcetable)?;

    let sql = mprintf!(
        c"INSERT INTO metadata (name, value) VALUES ('center', '%f,%f,%d');",
        m.center_lon as c_double,
        m.center_lat as c_double,
        m.center_z as c_int
    );
    exec_metadata(db, &sql, "set center", forcetable)?;

    let sql = mprintf!(
        c"INSERT INTO metadata (name, value) VALUES ('bounds', '%f,%f,%f,%f');",
        m.minlon as c_double,
        m.minlat as c_double,
        m.maxlon as c_double,
        m.maxlat as c_double
    );
    exec_metadata(db, &sql, "set bounds", forcetable)?;

    let sql = mprintf!(
        c"INSERT INTO metadata (name, value) VALUES ('antimeridian_adjusted_bounds', '%f,%f,%f,%f');",
        m.minlon2 as c_double,
        m.minlat2 as c_double,
        m.maxlon2 as c_double,
        m.maxlat2 as c_double
    );
    exec_metadata(db, &sql, "set bounds", forcetable)?;

    let type_ = c_str(&m.type_);
    let sql = mprintf!(
        c"INSERT INTO metadata (name, value) VALUES ('type', %Q);",
        type_.as_ptr()
    );
    exec_metadata(db, &sql, "set type", forcetable)?;

    if !m.attribution.is_empty() {
        let attribution = c_str(&m.attribution);
        let sql = mprintf!(
            c"INSERT INTO metadata (name, value) VALUES ('attribution', %Q);",
            attribution.as_ptr()
        );
        exec_metadata(db, &sql, "set attribution", forcetable)?;
    }

    let format = c_str(&m.format);
    let sql = mprintf!(
        c"INSERT INTO metadata (name, value) VALUES ('format', %Q);",
        format.as_ptr()
    );
    exec_metadata(db, &sql, "set format", forcetable)?;

    let generator = c_str(&m.generator);
    let sql = mprintf!(
        c"INSERT INTO metadata (name, value) VALUES ('generator', %Q);",
        generator.as_ptr()
    );
    exec_metadata(db, &sql, "set generator", forcetable)?;

    let generator_options = c_str(&m.generator_options);
    let sql = mprintf!(
        c"INSERT INTO metadata (name, value) VALUES ('generator_options', %Q);",
        generator_options.as_ptr()
    );
    exec_metadata(db, &sql, "set commandline", forcetable)?;

    if !m.strategies_json.is_empty() {
        let strategies = c_str(&m.strategies_json);
        let sql = mprintf!(
            c"INSERT INTO metadata (name, value) VALUES ('strategies', %Q);",
            strategies.as_ptr()
        );
        exec_metadata(db, &sql, "set strategies", forcetable)?;
    }

    if !m.decisions_json.is_empty() {
        let decisions = c_str(&m.decisions_json);
        let sql = mprintf!(
            c"INSERT INTO metadata (name, value) VALUES ('tippecanoe_decisions', %Q);",
            decisions.as_ptr()
        );
        exec_metadata(db, &sql, "set decisions", forcetable)?;
    }

    if !m.vector_layers_json.is_empty() || !m.tilestats_json.is_empty() {
        let mut json = String::new();
        {
            let mut state = JsonWriter::new_string(&mut json);
            state.nospace = true;
            state.json_write_hash();

            if !m.vector_layers_json.is_empty() {
                state.nospace = true;
                state.json_write_string("vector_layers");
                state.nospace = true;
                state.json_write_json(&m.vector_layers_json);
            }
            if !m.tilestats_json.is_empty() {
                state.nospace = true;
                state.json_write_string("tilestats");
                state.nospace = true;
                state.json_write_json(&m.tilestats_json);
            }

            state.nospace = true;
            state.json_end_hash();
        }

        let json = c_str(&json);
        let sql = mprintf!(
            c"INSERT INTO metadata (name, value) VALUES ('json', %Q);",
            json.as_ptr()
        );
        exec_metadata(db, &sql, "set json", forcetable)?;
    }
    Ok(())
}