Change "client key" to "client id" (#3130)
In #3118 @ryneeverett mentioned that "key" suggests that this is a secret, when in truth it's just a user identifier. So "ID" is a better word for it than "key".
This commit is contained in:
committed by
GitHub
parent
8097e28318
commit
7f68441916
@@ -3,16 +3,16 @@ use std::collections::HashMap;
|
||||
use std::sync::{Mutex, MutexGuard};
|
||||
|
||||
struct Inner {
|
||||
/// Clients, indexed by client_key
|
||||
/// Clients, indexed by client_id
|
||||
clients: HashMap<Uuid, Client>,
|
||||
|
||||
/// Snapshot data, indexed by client key
|
||||
/// Snapshot data, indexed by client id
|
||||
snapshots: HashMap<Uuid, Vec<u8>>,
|
||||
|
||||
/// Versions, indexed by (client_key, version_id)
|
||||
/// Versions, indexed by (client_id, version_id)
|
||||
versions: HashMap<(Uuid, Uuid), Version>,
|
||||
|
||||
/// Child versions, indexed by (client_key, parent_version_id)
|
||||
/// Child versions, indexed by (client_id, parent_version_id)
|
||||
children: HashMap<(Uuid, Uuid), Uuid>,
|
||||
}
|
||||
|
||||
@@ -42,16 +42,16 @@ impl Storage for InMemoryStorage {
|
||||
}
|
||||
|
||||
impl<'a> StorageTxn for InnerTxn<'a> {
|
||||
fn get_client(&mut self, client_key: Uuid) -> anyhow::Result<Option<Client>> {
|
||||
Ok(self.0.clients.get(&client_key).cloned())
|
||||
fn get_client(&mut self, client_id: Uuid) -> anyhow::Result<Option<Client>> {
|
||||
Ok(self.0.clients.get(&client_id).cloned())
|
||||
}
|
||||
|
||||
fn new_client(&mut self, client_key: Uuid, latest_version_id: Uuid) -> anyhow::Result<()> {
|
||||
if self.0.clients.get(&client_key).is_some() {
|
||||
return Err(anyhow::anyhow!("Client {} already exists", client_key));
|
||||
fn new_client(&mut self, client_id: Uuid, latest_version_id: Uuid) -> anyhow::Result<()> {
|
||||
if self.0.clients.get(&client_id).is_some() {
|
||||
return Err(anyhow::anyhow!("Client {} already exists", client_id));
|
||||
}
|
||||
self.0.clients.insert(
|
||||
client_key,
|
||||
client_id,
|
||||
Client {
|
||||
latest_version_id,
|
||||
snapshot: None,
|
||||
@@ -62,44 +62,44 @@ impl<'a> StorageTxn for InnerTxn<'a> {
|
||||
|
||||
fn set_snapshot(
|
||||
&mut self,
|
||||
client_key: Uuid,
|
||||
client_id: Uuid,
|
||||
snapshot: Snapshot,
|
||||
data: Vec<u8>,
|
||||
) -> anyhow::Result<()> {
|
||||
let mut client = self
|
||||
.0
|
||||
.clients
|
||||
.get_mut(&client_key)
|
||||
.get_mut(&client_id)
|
||||
.ok_or_else(|| anyhow::anyhow!("no such client"))?;
|
||||
client.snapshot = Some(snapshot);
|
||||
self.0.snapshots.insert(client_key, data);
|
||||
self.0.snapshots.insert(client_id, data);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_snapshot_data(
|
||||
&mut self,
|
||||
client_key: Uuid,
|
||||
client_id: Uuid,
|
||||
version_id: Uuid,
|
||||
) -> anyhow::Result<Option<Vec<u8>>> {
|
||||
// sanity check
|
||||
let client = self.0.clients.get(&client_key);
|
||||
let client = self.0.clients.get(&client_id);
|
||||
let client = client.ok_or_else(|| anyhow::anyhow!("no such client"))?;
|
||||
if Some(&version_id) != client.snapshot.as_ref().map(|snap| &snap.version_id) {
|
||||
return Err(anyhow::anyhow!("unexpected snapshot_version_id"));
|
||||
}
|
||||
Ok(self.0.snapshots.get(&client_key).cloned())
|
||||
Ok(self.0.snapshots.get(&client_id).cloned())
|
||||
}
|
||||
|
||||
fn get_version_by_parent(
|
||||
&mut self,
|
||||
client_key: Uuid,
|
||||
client_id: Uuid,
|
||||
parent_version_id: Uuid,
|
||||
) -> anyhow::Result<Option<Version>> {
|
||||
if let Some(parent_version_id) = self.0.children.get(&(client_key, parent_version_id)) {
|
||||
if let Some(parent_version_id) = self.0.children.get(&(client_id, parent_version_id)) {
|
||||
Ok(self
|
||||
.0
|
||||
.versions
|
||||
.get(&(client_key, *parent_version_id))
|
||||
.get(&(client_id, *parent_version_id))
|
||||
.cloned())
|
||||
} else {
|
||||
Ok(None)
|
||||
@@ -108,15 +108,15 @@ impl<'a> StorageTxn for InnerTxn<'a> {
|
||||
|
||||
fn get_version(
|
||||
&mut self,
|
||||
client_key: Uuid,
|
||||
client_id: Uuid,
|
||||
version_id: Uuid,
|
||||
) -> anyhow::Result<Option<Version>> {
|
||||
Ok(self.0.versions.get(&(client_key, version_id)).cloned())
|
||||
Ok(self.0.versions.get(&(client_id, version_id)).cloned())
|
||||
}
|
||||
|
||||
fn add_version(
|
||||
&mut self,
|
||||
client_key: Uuid,
|
||||
client_id: Uuid,
|
||||
version_id: Uuid,
|
||||
parent_version_id: Uuid,
|
||||
history_segment: Vec<u8>,
|
||||
@@ -128,19 +128,19 @@ impl<'a> StorageTxn for InnerTxn<'a> {
|
||||
history_segment,
|
||||
};
|
||||
|
||||
if let Some(client) = self.0.clients.get_mut(&client_key) {
|
||||
if let Some(client) = self.0.clients.get_mut(&client_id) {
|
||||
client.latest_version_id = version_id;
|
||||
if let Some(ref mut snap) = client.snapshot {
|
||||
snap.versions_since += 1;
|
||||
}
|
||||
} else {
|
||||
return Err(anyhow::anyhow!("Client {} does not exist", client_key));
|
||||
return Err(anyhow::anyhow!("Client {} does not exist", client_id));
|
||||
}
|
||||
|
||||
self.0
|
||||
.children
|
||||
.insert((client_key, parent_version_id), version_id);
|
||||
self.0.versions.insert((client_key, version_id), version);
|
||||
.insert((client_id, parent_version_id), version_id);
|
||||
self.0.versions.insert((client_id, version_id), version);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -169,18 +169,18 @@ mod test {
|
||||
let storage = InMemoryStorage::new();
|
||||
let mut txn = storage.txn()?;
|
||||
|
||||
let client_key = Uuid::new_v4();
|
||||
let client_id = Uuid::new_v4();
|
||||
let latest_version_id = Uuid::new_v4();
|
||||
txn.new_client(client_key, latest_version_id)?;
|
||||
txn.new_client(client_id, latest_version_id)?;
|
||||
|
||||
let client = txn.get_client(client_key)?.unwrap();
|
||||
let client = txn.get_client(client_id)?.unwrap();
|
||||
assert_eq!(client.latest_version_id, latest_version_id);
|
||||
assert!(client.snapshot.is_none());
|
||||
|
||||
let latest_version_id = Uuid::new_v4();
|
||||
txn.add_version(client_key, latest_version_id, Uuid::new_v4(), vec![1, 1])?;
|
||||
txn.add_version(client_id, latest_version_id, Uuid::new_v4(), vec![1, 1])?;
|
||||
|
||||
let client = txn.get_client(client_key)?.unwrap();
|
||||
let client = txn.get_client(client_id)?.unwrap();
|
||||
assert_eq!(client.latest_version_id, latest_version_id);
|
||||
assert!(client.snapshot.is_none());
|
||||
|
||||
@@ -189,9 +189,9 @@ mod test {
|
||||
timestamp: Utc::now(),
|
||||
versions_since: 4,
|
||||
};
|
||||
txn.set_snapshot(client_key, snap.clone(), vec![1, 2, 3])?;
|
||||
txn.set_snapshot(client_id, snap.clone(), vec![1, 2, 3])?;
|
||||
|
||||
let client = txn.get_client(client_key)?.unwrap();
|
||||
let client = txn.get_client(client_id)?.unwrap();
|
||||
assert_eq!(client.latest_version_id, latest_version_id);
|
||||
assert_eq!(client.snapshot.unwrap(), snap);
|
||||
|
||||
@@ -212,14 +212,14 @@ mod test {
|
||||
let storage = InMemoryStorage::new();
|
||||
let mut txn = storage.txn()?;
|
||||
|
||||
let client_key = Uuid::new_v4();
|
||||
let client_id = Uuid::new_v4();
|
||||
let version_id = Uuid::new_v4();
|
||||
let parent_version_id = Uuid::new_v4();
|
||||
let history_segment = b"abc".to_vec();
|
||||
|
||||
txn.new_client(client_key, parent_version_id)?;
|
||||
txn.new_client(client_id, parent_version_id)?;
|
||||
txn.add_version(
|
||||
client_key,
|
||||
client_id,
|
||||
version_id,
|
||||
parent_version_id,
|
||||
history_segment.clone(),
|
||||
@@ -232,11 +232,11 @@ mod test {
|
||||
};
|
||||
|
||||
let version = txn
|
||||
.get_version_by_parent(client_key, parent_version_id)?
|
||||
.get_version_by_parent(client_id, parent_version_id)?
|
||||
.unwrap();
|
||||
assert_eq!(version, expected);
|
||||
|
||||
let version = txn.get_version(client_key, version_id)?.unwrap();
|
||||
let version = txn.get_version(client_id, version_id)?.unwrap();
|
||||
assert_eq!(version, expected);
|
||||
|
||||
Ok(())
|
||||
@@ -247,40 +247,39 @@ mod test {
|
||||
let storage = InMemoryStorage::new();
|
||||
let mut txn = storage.txn()?;
|
||||
|
||||
let client_key = Uuid::new_v4();
|
||||
let client_id = Uuid::new_v4();
|
||||
|
||||
txn.new_client(client_key, Uuid::new_v4())?;
|
||||
assert!(txn.get_client(client_key)?.unwrap().snapshot.is_none());
|
||||
txn.new_client(client_id, Uuid::new_v4())?;
|
||||
assert!(txn.get_client(client_id)?.unwrap().snapshot.is_none());
|
||||
|
||||
let snap = Snapshot {
|
||||
version_id: Uuid::new_v4(),
|
||||
timestamp: Utc::now(),
|
||||
versions_since: 3,
|
||||
};
|
||||
txn.set_snapshot(client_key, snap.clone(), vec![9, 8, 9])?;
|
||||
txn.set_snapshot(client_id, snap.clone(), vec![9, 8, 9])?;
|
||||
|
||||
assert_eq!(
|
||||
txn.get_snapshot_data(client_key, snap.version_id)?.unwrap(),
|
||||
txn.get_snapshot_data(client_id, snap.version_id)?.unwrap(),
|
||||
vec![9, 8, 9]
|
||||
);
|
||||
assert_eq!(txn.get_client(client_key)?.unwrap().snapshot, Some(snap));
|
||||
assert_eq!(txn.get_client(client_id)?.unwrap().snapshot, Some(snap));
|
||||
|
||||
let snap2 = Snapshot {
|
||||
version_id: Uuid::new_v4(),
|
||||
timestamp: Utc::now(),
|
||||
versions_since: 10,
|
||||
};
|
||||
txn.set_snapshot(client_key, snap2.clone(), vec![0, 2, 4, 6])?;
|
||||
txn.set_snapshot(client_id, snap2.clone(), vec![0, 2, 4, 6])?;
|
||||
|
||||
assert_eq!(
|
||||
txn.get_snapshot_data(client_key, snap2.version_id)?
|
||||
.unwrap(),
|
||||
txn.get_snapshot_data(client_id, snap2.version_id)?.unwrap(),
|
||||
vec![0, 2, 4, 6]
|
||||
);
|
||||
assert_eq!(txn.get_client(client_key)?.unwrap().snapshot, Some(snap2));
|
||||
assert_eq!(txn.get_client(client_id)?.unwrap().snapshot, Some(snap2));
|
||||
|
||||
// check that mismatched version is detected
|
||||
assert!(txn.get_snapshot_data(client_key, Uuid::new_v4()).is_err());
|
||||
assert!(txn.get_snapshot_data(client_id, Uuid::new_v4()).is_err());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user