Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/dev/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- [Python bindings](python/index.md)
- [Setup Build Environment](python/setting-up-python-build-environment.md)
- [Rust Component](core/index.md)
- [Style Guide](core/style-guide.md)
- [Documentation guidelines](core/documentation-guidelines.md)
- [Dependency Management](core/dependency-management.md)
- [Dependency Vetting](core/dependency-vetting.md)
Expand Down
64 changes: 64 additions & 0 deletions docs/dev/core/style-guide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Style Guide

## Code

All Rust code is formatted using [`rustfmt`](https://github.com/rust-lang/rustfmt).
Run `make fmt-rust` to format code in your local checkout.
This is enforced in CI.

### Strings in code

Multi-line strings in Rust code should use double-quotes where possible, or raw string markers (`r#" "#`) if needed.

When indentation doesn't matter, the double-quote should be on its own line and the start of the string indented by 4 spaces below.
The closing double-quote should be aligned with the beginning of the line that opened the string.
Unfortunately `rustfmt` will not enforce the intended formatting.

**Good**:

```rust
let query = "
SELECT *
FROM table
WHERE id IS NOT NULL
";
```


**Bad**:

```rust
let query = "SELECT
* FROM table
WHERE id IS NOT NULL
";
```

When the multi-line string is within a macro (e.g. `format!`), the opening and closing double-quote go on their own line, indented by 4 spaces below the identifier.
`rustfmt` will enforce the quote on its own line, but not the indentation.

**Good**:

```rust
let query = format!(
"
SELECT *
FROM {table}
WHERE id IS NOT NULL
"
);
```


**Bad**:

```rust
let query = format!("SELECT
* FROM {table}
WHERE id IS NOT NULL
");
```

## Documentation

See [Documentation Guidelines](documentation-guidelines.md) for details.
188 changes: 95 additions & 93 deletions glean-core/src/database/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,9 @@ impl Database {
"INSERT INTO lifetime_ping.telemetry SELECT * FROM telemetry WHERE lifetime = 'ping'";
let res = self.conn.write(|tx| tx.execute_one(copy_sql));
if let Err(err) = res {
log::error!("Could not load ping lifetime data into memory: {err:?}. Disabling ping lifetime IO delay.");
log::error!(
"Could not load ping lifetime data into memory: {err:?}. Disabling ping lifetime IO delay."
);
self.delay_ping_lifetime_io = false;
}
}
Expand Down Expand Up @@ -466,16 +468,16 @@ impl Database {
let table = self.table_for_lifetime(lifetime);

let iter_sql = format!(
r#"
SELECT
id,
value,
labels
FROM {table}
WHERE
lifetime = ?1
AND ping = ?2
"#
"
SELECT
id,
value,
labels
FROM {table}
WHERE
lifetime = ?1
AND ping = ?2
"
);

self.conn.read(|conn| {
Expand Down Expand Up @@ -514,16 +516,16 @@ impl Database {

// TODO(bug 2048194): Remove the `LIMIT 1` and error out when more than 1 row is returned.
let get_metric_sql = format!(
r#"
SELECT
value
FROM {table}
WHERE
id = ?1
AND ping = ?2
AND labels = ?3
LIMIT 1
"#
"
SELECT
value
FROM {table}
WHERE
id = ?1
AND ping = ?2
AND labels = ?3
LIMIT 1
"
);

let metric_identifier = &data.base_identifier();
Expand Down Expand Up @@ -566,14 +568,14 @@ impl Database {
let table = self.table_for_lifetime(lifetime);

let has_metric_sql = format!(
r#"
SELECT id
FROM {table}
WHERE
lifetime = ?1
AND ping = ?2
AND id = ?3
"#
"
SELECT id
FROM {table}
WHERE
lifetime = ?1
AND ping = ?2
AND id = ?3
"
);

self.conn
Expand Down Expand Up @@ -647,15 +649,15 @@ impl Database {
let table = self.table_for_lifetime(lifetime);

let insert_sql = format!(
r#"
INSERT INTO
{table} (id, ping, lifetime, labels, value)
VALUES
(?1, ?2, ?3, ?4, ?5)
ON CONFLICT(id, ping, labels) DO UPDATE SET
lifetime = excluded.lifetime,
value = excluded.value
"#
"
INSERT INTO
{table} (id, ping, lifetime, labels, value)
VALUES
(?1, ?2, ?3, ?4, ?5)
ON CONFLICT(id, ping, labels) DO UPDATE SET
lifetime = excluded.lifetime,
value = excluded.value
"
);

{
Expand Down Expand Up @@ -758,16 +760,16 @@ impl Database {

// TODO(bug 2048194): Remove the `LIMIT 1` and error out when more than 1 row is returned.
let value_sql = format!(
r#"
SELECT value
FROM {table}
WHERE
id = ?1
AND ping = ?2
AND lifetime = ?3
AND labels = ?4
LIMIT 1
"#
"
SELECT value
FROM {table}
WHERE
id = ?1
AND ping = ?2
AND lifetime = ?3
AND labels = ?4
LIMIT 1
"
);

let new_value = {
Expand All @@ -789,15 +791,15 @@ impl Database {
};

let insert_sql = format!(
r#"
INSERT INTO
{table} (id, ping, lifetime, labels, value)
VALUES
(?1, ?2, ?3, ?4, ?5)
ON CONFLICT(id, ping, labels) DO UPDATE SET
lifetime = excluded.lifetime,
value = excluded.value
"#
"
INSERT INTO
{table} (id, ping, lifetime, labels, value)
VALUES
(?1, ?2, ?3, ?4, ?5)
ON CONFLICT(id, ping, labels) DO UPDATE SET
lifetime = excluded.lifetime,
value = excluded.value
"
);

{
Expand Down Expand Up @@ -1058,17 +1060,17 @@ impl Database {

impl StoredSubmittedPingHandler for Database {
fn get_all_submitted_pings(&self) -> Vec<crate::SubmittedPing> {
let get_all_submitted_pings_sql = r#"
SELECT
document_id,
ping,
date_submitted,
date_uploaded,
upload_failed,
payload
FROM submitted_pings
ORDER BY date_submitted DESC
"#;
let get_all_submitted_pings_sql = "
SELECT
document_id,
ping,
date_submitted,
date_uploaded,
upload_failed,
payload
FROM submitted_pings
ORDER BY date_submitted DESC
";
self.conn
.read(|conn| {
let Ok(mut stmt) = conn.prepare_cached(get_all_submitted_pings_sql) else {
Expand Down Expand Up @@ -1096,19 +1098,19 @@ impl StoredSubmittedPingHandler for Database {
}

fn get_submitted_pings_by_name(&self, ping: &str) -> Vec<crate::SubmittedPing> {
let get_submitted_pings_sql = r#"
SELECT
document_id,
ping,
date_submitted,
date_uploaded,
upload_failed,
payload
FROM submitted_pings
WHERE
ping = ?1
ORDER BY date_submitted DESC
"#;
let get_submitted_pings_sql = "
SELECT
document_id,
ping,
date_submitted,
date_uploaded,
upload_failed,
payload
FROM submitted_pings
WHERE
ping = ?1
ORDER BY date_submitted DESC
";
self.conn
.read(|conn| {
let Ok(mut stmt) = conn.prepare_cached(get_submitted_pings_sql) else {
Expand Down Expand Up @@ -1171,18 +1173,18 @@ impl StoredSubmittedPingHandler for Database {
payload: JsonValue,
) -> Result<()> {
self.conn.write(|tx| {
let insert_sql = r#"
INSERT INTO
submitted_pings (document_id, ping, date_submitted, date_uploaded, upload_failed, payload)
VALUES
(?1, ?2, ?3, ?4, ?5, ?6)
ON CONFLICT(document_id) DO UPDATE SET
ping = excluded.ping,
date_submitted = excluded.date_submitted,
date_uploaded = excluded.date_uploaded,
upload_failed = excluded.upload_failed,
payload = excluded.payload
"#;
let insert_sql = "
INSERT INTO
submitted_pings (document_id, ping, date_submitted, date_uploaded, upload_failed, payload)
VALUES
(?1, ?2, ?3, ?4, ?5, ?6)
ON CONFLICT(document_id) DO UPDATE SET
ping = excluded.ping,
date_submitted = excluded.date_submitted,
date_uploaded = excluded.date_uploaded,
upload_failed = excluded.upload_failed,
payload = excluded.payload
";
let mut stmt = tx.prepare_cached(insert_sql)?;
stmt.execute(params![
document_id,
Expand Down
Loading
Loading