oxilite-cli 0.3.1

oxilite command line: load, query, and a SPARQL HTTP endpoint compatible with `oxigraph serve`
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
//! `oxilite`: an interactive SPARQL shell (the default command), load and query a store from the
//! command line, or serve it over the SPARQL 1.1 protocol with the same routes as
//! `oxigraph serve` (`/query`, `/update`, `/store`).
//!
//! The store is a SQLite file (bundled SQLite), the same file through a SQLite shared library
//! (`--library`), or a D1 database behind the local sidecar (`--d1-sidecar`, used by the
//! benchmarks).
//!
// @lat: [[architecture#Command line and HTTP endpoint]]

mod db;
mod shell;
mod studio;

use clap::{Parser, Subcommand};
use db::Db;
use oxilite::io::RdfFormat;
use std::sync::Arc;
use std::time::Instant;
use tiny_http::{Header, Method, Request, Response, Server};

#[derive(Parser)]
#[command(
    name = "oxilite",
    version,
    about = "oxilite: an Oxigraph-compatible SPARQL store on SQLite",
    long_about = "oxilite: an Oxigraph-compatible SPARQL store on SQLite.\n\n\
        Without a command, opens an interactive SPARQL shell on DATABASE (a SQLite file, created \
        with the schema if missing) or on a transient in-memory store.",
    args_conflicts_with_subcommands = true
)]
struct Args {
    #[command(subcommand)]
    command: Option<Command>,
    /// The shell's SQLite database file (created if missing; in memory when omitted).
    database: Option<String>,
    #[command(flatten)]
    location: Location,
}

#[derive(clap::Args, Clone)]
struct Location {
    /// SQLite database file (created if missing).
    #[arg(long, short)]
    location: Option<String>,
    /// Load this SQLite shared library instead of the bundled SQLite.
    #[arg(long)]
    library: Option<String>,
    /// Use the D1 database of a local sidecar (`testsuite/d1-sidecar`) at this URL.
    #[arg(long)]
    d1_sidecar: Option<String>,
    /// Create the store without the graph index (fewer writes if you only use the default graph).
    #[arg(long)]
    no_graph_index: bool,
    /// Create the full-text index over string literals (for `oxl:textMatch`).
    #[arg(long)]
    text_index: bool,
}

#[derive(Subcommand)]
enum Command {
    /// Serves the store over the SPARQL 1.1 protocol.
    Serve {
        #[command(flatten)]
        location: Location,
        #[arg(long, short, default_value = "127.0.0.1:7879")]
        bind: String,
        /// Worker threads.
        #[arg(long, default_value_t = 8)]
        threads: usize,
    },
    /// Bulk-loads RDF files, then refreshes planner statistics.
    Load {
        #[command(flatten)]
        location: Location,
        /// Files to load; the format comes from the extension unless `--format` is given.
        #[arg(long, short, required = true, num_args = 1..)]
        file: Vec<String>,
        #[arg(long)]
        format: Option<String>,
    },
    /// Runs a SPARQL query and prints the results.
    Query {
        #[command(flatten)]
        location: Location,
        #[arg(long, short)]
        query: String,
        /// Results format (json, xml, csv, tsv, or an RDF format for graph results).
        #[arg(long, default_value = "json")]
        results_format: String,
    },
    /// Prints the SQL a query compiles to, with the planner's notes.
    Explain {
        #[command(flatten)]
        location: Location,
        #[arg(long, short)]
        query: String,
    },
    /// Runs a SPARQL update.
    Update {
        #[command(flatten)]
        location: Location,
        #[arg(long, short)]
        update: String,
    },
    /// Runs a Datalog program: recursive rules with stratified negation.
    ///
    /// The program is read from `--program`, or from the file named by `--file`, or from
    /// standard input when neither is given.
    Datalog {
        #[command(flatten)]
        location: Location,
        /// The program text.
        #[arg(long, short, conflicts_with = "file")]
        program: Option<String>,
        /// A file holding the program.
        #[arg(long, short)]
        file: Option<String>,
        /// Print the strata, the strategy per recursive component and the SQL, and run nothing.
        #[arg(long)]
        explain: bool,
        /// Store what the program derives as inferences instead of returning its goal.
        #[arg(long, conflicts_with = "explain")]
        materialize: bool,
    },
    /// Refreshes planner statistics and the reasoning closure.
    Optimize {
        #[command(flatten)]
        location: Location,
    },
    /// Checks a project like oxilite studio does: load errors, rule errors, SHACL results and the
    /// tests in `oxilite.toml`. Exits with status 1 when anything fails.
    Check {
        /// The project root (default: the current directory).
        #[arg(default_value = ".")]
        root: String,
        /// Print the report as JSON.
        #[arg(long)]
        json: bool,
    },
    /// Serves the studio's tools (query, schema, validate, why) to agents over the Model Context
    /// Protocol on standard input and output.
    Mcp {
        /// The project root to load like oxilite studio's Project store (default: here).
        #[arg(long)]
        root: Option<String>,
        /// Open this store file read-only instead of loading a project.
        #[arg(long, conflicts_with = "root")]
        location: Option<String>,
    },
    /// Runs the language server behind oxilite studio (LSP over standard input and output).
    StudioServer {
        /// Scratch store location (default `<workspace>/.oxilite/studio.sqlite`; `:memory:` works).
        #[arg(long)]
        store: Option<String>,
    },
}

fn main() {
    let args = Args::parse();
    let result = match args.command {
        Some(command) => run(command),
        None => {
            let mut location = args.location;
            if let Some(database) = args.database {
                location.location = Some(database);
            }
            match shell::run(location) {
                Ok(0) => Ok(()),
                Ok(code) => std::process::exit(code),
                Err(e) => Err(e),
            }
        }
    };
    if let Err(e) = result {
        eprintln!("error: {e}");
        std::process::exit(1);
    }
}

fn run(command: Command) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    match command {
        Command::Serve {
            location,
            bind,
            threads,
        } => {
            let db = Arc::new(Db::open(&location)?);
            let server = Arc::new(Server::http(&bind).map_err(|e| e.to_string())?);
            eprintln!("oxilite listening on http://{bind}/ (query endpoint /query, update endpoint /update)");
            let workers: Vec<_> = (0..threads.max(1))
                .map(|_| {
                    let (server, db) = (Arc::clone(&server), Arc::clone(&db));
                    std::thread::spawn(move || {
                        for request in server.incoming_requests() {
                            handle(&db, request);
                        }
                    })
                })
                .collect();
            for w in workers {
                let _ = w.join();
            }
            Ok(())
        }
        Command::Load {
            location,
            file,
            format,
        } => {
            let db = Db::open(&location)?;
            for f in file {
                let format = match &format {
                    Some(m) => parse_format(m)?,
                    None => RdfFormat::from_extension(f.rsplit('.').next().unwrap_or_default())
                        .ok_or_else(|| format!("unknown format of {f}"))?,
                };
                let start = Instant::now();
                let data = std::fs::read(&f)?;
                db.load(format, &data, None, true)?;
                eprintln!("loaded {f} in {:.2}s", start.elapsed().as_secs_f64());
            }
            Ok(())
        }
        Command::Query {
            location,
            query,
            results_format,
        } => {
            let db = Db::open(&location)?;
            let out = db.query(&query, &[], &[])?;
            print!(
                "{}",
                oxilite_core::json::output_to_format(&out, &results_format)?
            );
            Ok(())
        }
        Command::Explain { location, query } => {
            println!("{}", Db::open(&location)?.explain(&query)?);
            Ok(())
        }
        Command::Update { location, update } => Ok(Db::open(&location)?.update(&update, &[])?),
        Command::Datalog {
            location,
            program,
            file,
            explain,
            materialize,
        } => {
            let source = read_program(program, file)?;
            let db = Db::open(&location)?;
            if explain {
                println!("{}", db.explain_datalog(&source)?);
                return Ok(());
            }
            if materialize {
                let stats = db.datalog_materialize(&source)?;
                println!(
                    "{} inferred triple(s) from {} relation(s)",
                    stats.inferred, stats.relations
                );
                return Ok(());
            }
            let r = db.datalog(&source)?;
            println!("{}", r.variables.join("\t"));
            for row in &r.rows {
                let line: Vec<String> = row
                    .iter()
                    .map(|c| c.as_ref().map(ToString::to_string).unwrap_or_default())
                    .collect();
                println!("{}", line.join("\t"));
            }
            Ok(())
        }
        Command::Optimize { location } => Ok(Db::open(&location)?.optimize()?),
        Command::StudioServer { store } => studio::run(store),
        Command::Mcp { root, location } => studio::mcp::serve(root.map(Into::into), location),
        Command::Check { root, json } => {
            let (passed, report) = studio::check::check(root.into())?;
            if json {
                println!("{}", serde_json::to_string_pretty(&report)?);
            } else {
                print!("{}", studio::check::render(&report));
            }
            if !passed {
                std::process::exit(1);
            }
            Ok(())
        }
    }
}

/// A program given inline, in a file, or on standard input.
fn read_program(
    program: Option<String>,
    file: Option<String>,
) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
    if let Some(p) = program {
        return Ok(p);
    }
    if let Some(f) = file {
        return Ok(std::fs::read_to_string(f)?);
    }
    let mut buf = String::new();
    std::io::Read::read_to_string(&mut std::io::stdin(), &mut buf)?;
    if buf.trim().is_empty() {
        return Err("no program given: use --program, --file, or pipe one in".into());
    }
    Ok(buf)
}

fn parse_format(media: &str) -> Result<RdfFormat, String> {
    let m = media.split(';').next().unwrap_or_default().trim();
    RdfFormat::from_media_type(m)
        .or_else(|| RdfFormat::from_extension(m))
        .ok_or_else(|| format!("unsupported RDF format {media}"))
}

fn header(request: &Request, name: &str) -> Option<String> {
    request
        .headers()
        .iter()
        .find(|h| h.field.to_string().eq_ignore_ascii_case(name))
        .map(|h| h.value.as_str().to_string())
}

fn respond(request: Request, status: u16, content_type: &str, body: String) {
    let r = Response::from_string(body)
        .with_status_code(status)
        .with_header(Header::from_bytes("Content-Type", content_type).expect("valid header"));
    let _ = request.respond(r);
}

/// Picks the response media type from the `Accept` header.
fn negotiate(accept: Option<&str>, graph: bool) -> &'static str {
    let candidates: &[&str] = if graph {
        &[
            "application/n-triples",
            "text/turtle",
            "application/rdf+xml",
            "application/n-quads",
            "application/trig",
        ]
    } else {
        &[
            "application/sparql-results+json",
            "application/sparql-results+xml",
            "text/csv",
            "text/tab-separated-values",
        ]
    };
    if let Some(accept) = accept {
        for part in accept.split(',') {
            let m = part.split(';').next().unwrap_or_default().trim();
            if let Some(c) = candidates.iter().find(|c| **c == m) {
                return c;
            }
            if m == "application/json" && !graph {
                return "application/sparql-results+json";
            }
            if m == "application/xml" && !graph {
                return "application/sparql-results+xml";
            }
        }
    }
    candidates[if graph { 1 } else { 0 }]
}

fn handle(db: &Db, mut request: Request) {
    let url = request.url().to_string();
    let (path, query_string) = url.split_once('?').unwrap_or((url.as_str(), ""));
    let mut params: Vec<(String, String)> = form_urlencoded::parse(query_string.as_bytes())
        .into_owned()
        .collect();
    let content_type = header(&request, "Content-Type").unwrap_or_default();
    let mut body = Vec::new();
    if request.method() == &Method::Post {
        if let Err(e) = request.as_reader().read_to_end(&mut body) {
            return respond(request, 400, "text/plain", e.to_string());
        }
    }
    let form = content_type.starts_with("application/x-www-form-urlencoded");
    if form {
        params.extend(form_urlencoded::parse(&body).into_owned());
    }
    let get = |k: &str| params.iter().find(|(n, _)| n == k).map(|(_, v)| v.clone());
    let all = |k: &str| {
        params
            .iter()
            .filter(|(n, _)| n == k)
            .map(|(_, v)| v.clone())
            .collect::<Vec<_>>()
    };
    let result: Result<(u16, String, String), String> = (|| match (request.method(), path) {
        (Method::Get | Method::Post, "/query") => {
            let query = match get("query") {
                Some(q) => q,
                None if content_type.starts_with("application/sparql-query") => {
                    String::from_utf8(body.clone()).map_err(|e| e.to_string())?
                }
                None => return Err("missing query".into()),
            };
            let out = db
                .query(&query, &all("default-graph-uri"), &all("named-graph-uri"))
                .map_err(|e| e.to_string())?;
            let graph = matches!(out, oxilite_core::QueryOutput::Graph(_));
            let media = negotiate(header(&request, "Accept").as_deref(), graph);
            let text =
                oxilite_core::json::output_to_format(&out, media).map_err(|e| e.to_string())?;
            Ok((200, media.to_string(), text))
        }
        (Method::Post, "/update") => {
            let update = match get("update") {
                Some(u) => u,
                None if content_type.starts_with("application/sparql-update") => {
                    String::from_utf8(body.clone()).map_err(|e| e.to_string())?
                }
                None => return Err("missing update".into()),
            };
            db.update(&update, &all("using-graph-uri"))
                .map_err(|e| e.to_string())?;
            Ok((204, "text/plain".into(), String::new()))
        }
        (Method::Post, "/store") => {
            let format = parse_format(&content_type)?;
            let graph = get("graph");
            let bulk = params.iter().any(|(n, _)| n == "no_transaction");
            db.load(format, &body, graph.as_deref(), bulk)
                .map_err(|e| e.to_string())?;
            Ok((204, "text/plain".into(), String::new()))
        }
        (Method::Get, "/") => Ok((
            200,
            "text/plain".into(),
            "oxilite SPARQL endpoint: /query, /update, /store\n".into(),
        )),
        _ => Ok((404, "text/plain".into(), "not found\n".into())),
    })();
    match result {
        Ok((status, media, text)) => respond(request, status, &media, text),
        Err(e) => respond(request, 400, "text/plain", e),
    }
}