summaryrefslogtreecommitdiff
path: root/graph-checker/src/main.rs
blob: f38724c505a4d2cfdfb95439daf37a18f6246344 (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
use sqlx::{migrate::MigrateDatabase, QueryBuilder, Sqlite};
use std::time::Instant;
use tokio;

mod graph;

mod geng;
use crate::geng::GengIterator;

mod compute;

#[tokio::main]
async fn main() -> Result<(), sqlx::Error> {
    dotenv::dotenv().ok();
    let database_url =
        std::env::var("DATABASE_URL").expect("Expected DATABASE_URL in env");
    if !sqlx::Sqlite::database_exists(&database_url).await? {
        sqlx::Sqlite::create_database(&database_url).await?;
    }
    let db = sqlx::SqlitePool::connect(&database_url).await?;
    let err = sqlx::query(
        "CREATE TABLE IF NOT EXISTS graphs (g6 VARCHAR NOT NULL, ind_dom INT NOT NULL, forced_geod INT NOT NULL);"
    )
    .execute(&db)
    .await;
    if let Err(e) = err {
        println!("Error while creating table: {e}");
    }

    let gi = GengIterator::new(9);

    println!("Started");
    let start = Instant::now();
    const BATCH_SIZE: usize = 1000;
    let mut count = 0;
    loop {
        let graphs = gi.take(BATCH_SIZE);
        let batch = Instant::now();
        let tasks: Vec<_> = graphs
            .map(|g| tokio::spawn(compute::dominating_numbers(g)))
            .collect();
        let part = futures::future::join_all(tasks).await;
        let sz = part.len();

        let mut query = QueryBuilder::<Sqlite>::new(
            "INSERT INTO graphs (g6, ind_dom, forced_geod) ",
        );
        query.push_values(part, |mut b, res| {
            match res {
                Ok((g6, ind_dom, fg)) => {
                    b.push_bind(g6)
                        .push_bind(ind_dom.unwrap_or(0))
                        .push_bind(fg.unwrap_or(0));
                }
                Err(_) => println!("Unable to push into query"),
            };
        });

        let err = query.build().execute(&db).await;
        if let Err(e) = err {
            println!("Error while inserting: {e}");
        }

        count += sz;
        println!(
            "Counted {count} in {}s",
            batch.elapsed().as_nanos() as f64 / 1e9
        );
        if sz < BATCH_SIZE {
            break;
        }
    }

    let elapsed = start.elapsed();
    let time = elapsed.as_nanos();
    println!("Time elapsed: {}s", time as f64 / 1e9);

    Ok(())
}