Switch to uuidv7 from uuidv4

Why?

uuidv4 is totally random. uuidv7 has a time-based prefix, ie, they
_sort_.

This is important when we start to think about database indices. btree
indices have shit performance when the elements are totally random, but
good insertion + perf when they are sorted. Plus it's just kinda useful
to be able to sort them

I don't think it's worth re-ID-ing past data, but at least from hereon
out future data will be nice

TODO: Update the database schema to use the UUID type, and not just a
string.
This commit is contained in:
Ellie Huxtable
2023-03-13 21:43:50 +00:00
parent 1f21fa3338
commit 1cb41cb1ab
6 changed files with 27 additions and 13 deletions

16
Cargo.lock generated
View File

@@ -57,6 +57,15 @@ dependencies = [
"num-traits", "num-traits",
] ]
[[package]]
name = "atomic"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b88d82667eca772c4aa12f0f1348b3ae643424c8876448f3f7bd5787032e234c"
dependencies = [
"autocfg",
]
[[package]] [[package]]
name = "atty" name = "atty"
version = "0.2.14" version = "0.2.14"
@@ -150,7 +159,9 @@ name = "atuin-common"
version = "13.0.1" version = "13.0.1"
dependencies = [ dependencies = [
"chrono", "chrono",
"hex",
"serde", "serde",
"sha2",
"uuid", "uuid",
] ]
@@ -2540,10 +2551,11 @@ checksum = "e8db7427f936968176eaa7cdf81b7f98b980b18495ec28f1b5791ac3bfe3eea9"
[[package]] [[package]]
name = "uuid" name = "uuid"
version = "1.2.1" version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "feb41e78f93363bb2df8b0e86a2ca30eed7806ea16ea0c790d757cf93f79be83" checksum = "1674845326ee10d37ca60470760d4288a6f80f304007d92e5c53bab78c9cfd79"
dependencies = [ dependencies = [
"atomic",
"getrandom", "getrandom",
] ]

View File

@@ -2,7 +2,7 @@ use chrono::Utc;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::history::History; use crate::history::History;
use atuin_common::utils::uuid_v4; use atuin_common::utils::uuid_v7;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum EventType { pub enum EventType {
@@ -23,7 +23,7 @@ pub struct Event {
impl Event { impl Event {
pub fn new_create(history: &History) -> Event { pub fn new_create(history: &History) -> Event {
Event { Event {
id: uuid_v4(), id: uuid_v7(),
timestamp: history.timestamp, timestamp: history.timestamp,
hostname: history.hostname.clone(), hostname: history.hostname.clone(),
event_type: EventType::Create, event_type: EventType::Create,
@@ -36,7 +36,7 @@ impl Event {
let hostname = format!("{}:{}", whoami::hostname(), whoami::username()); let hostname = format!("{}:{}", whoami::hostname(), whoami::username());
Event { Event {
id: uuid_v4(), id: uuid_v7(),
timestamp: chrono::Utc::now(), timestamp: chrono::Utc::now(),
hostname, hostname,
event_type: EventType::Create, event_type: EventType::Create,

View File

@@ -3,7 +3,7 @@ use std::env;
use chrono::Utc; use chrono::Utc;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use atuin_common::utils::uuid_v4; use atuin_common::utils::uuid_v7;
// Any new fields MUST be Optional<>! // Any new fields MUST be Optional<>!
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, sqlx::FromRow)] #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, sqlx::FromRow)]
@@ -30,12 +30,12 @@ impl History {
) -> Self { ) -> Self {
let session = session let session = session
.or_else(|| env::var("ATUIN_SESSION").ok()) .or_else(|| env::var("ATUIN_SESSION").ok())
.unwrap_or_else(uuid_v4); .unwrap_or_else(uuid_v7);
let hostname = let hostname =
hostname.unwrap_or_else(|| format!("{}:{}", whoami::hostname(), whoami::username())); hostname.unwrap_or_else(|| format!("{}:{}", whoami::hostname(), whoami::username()));
Self { Self {
id: uuid_v4(), id: uuid_v7(),
timestamp, timestamp,
command, command,
cwd, cwd,

View File

@@ -6,7 +6,7 @@ use directories::UserDirs;
use eyre::{eyre, Result}; use eyre::{eyre, Result};
use serde::Deserialize; use serde::Deserialize;
use atuin_common::utils::uuid_v4; use atuin_common::utils::uuid_v7;
use super::{get_histpath, unix_byte_lines, Importer, Loader}; use super::{get_histpath, unix_byte_lines, Importer, Loader};
use crate::history::History; use crate::history::History;
@@ -123,13 +123,13 @@ impl Importer for Resh {
}; };
h.push(History { h.push(History {
id: uuid_v4(), id: uuid_v7(),
timestamp, timestamp,
duration, duration,
exit: entry.exit_code, exit: entry.exit_code,
command: entry.cmd_line, command: entry.cmd_line,
cwd: entry.pwd, cwd: entry.pwd,
session: uuid_v4(), session: uuid_v7(),
hostname: entry.host, hostname: entry.host,
}) })
.await?; .await?;

View File

@@ -13,4 +13,6 @@ repository = "https://github.com/ellie/atuin"
[dependencies] [dependencies]
chrono = { version = "0.4", features = ["serde"] } chrono = { version = "0.4", features = ["serde"] }
serde = { version = "1.0.145", features = ["derive"] } serde = { version = "1.0.145", features = ["derive"] }
uuid = { version = "1.2", features = ["v4"] } uuid = { version = "1.3", features = ["v7"] }
sha2 = { version = "0.10" }
hex = { version = "0.4" }

View File

@@ -60,7 +60,7 @@ impl AtuinCmd {
Ok(()) Ok(())
} }
Self::Uuid => { Self::Uuid => {
println!("{}", atuin_common::utils::uuid_v4()); println!("{}", atuin_common::utils::uuid_v7());
Ok(()) Ok(())
} }
Self::GenCompletions { shell, out_dir } => { Self::GenCompletions { shell, out_dir } => {