2020-10-24 13:46:06 +00:00
|
|
|
use super::*;
|
|
|
|
use crate::db::errors::{DataError, MigrationError};
|
|
|
|
use crate::db::Database;
|
|
|
|
use byteorder::LittleEndian;
|
|
|
|
use sled::transaction::TransactionError;
|
2020-10-30 20:16:08 +00:00
|
|
|
use sled::{Batch, IVec};
|
2020-10-24 13:46:06 +00:00
|
|
|
use zerocopy::byteorder::U32;
|
|
|
|
use zerocopy::AsBytes;
|
|
|
|
|
2020-10-30 20:16:08 +00:00
|
|
|
//Not to be confused with the super::RoomAndUser delineator.
|
|
|
|
#[derive(PartialEq, Eq, std::hash::Hash)]
|
|
|
|
struct RoomAndUser {
|
|
|
|
room_id: String,
|
|
|
|
username: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a version 0 user variable key.
|
|
|
|
fn v0_variable_key(info: &RoomAndUser, variable_name: &str) -> Vec<u8> {
|
|
|
|
let mut key = vec![];
|
|
|
|
key.extend_from_slice(info.room_id.as_bytes());
|
|
|
|
key.extend_from_slice(info.username.as_bytes());
|
|
|
|
key.extend_from_slice(variable_name.as_bytes());
|
|
|
|
key
|
|
|
|
}
|
|
|
|
|
|
|
|
fn map_value_to_room_and_user(
|
|
|
|
entry: sled::Result<(IVec, IVec)>,
|
|
|
|
) -> Result<RoomAndUser, MigrationError> {
|
|
|
|
if let Ok((key, _)) = entry {
|
|
|
|
let keys: Vec<Result<&str, _>> = key
|
|
|
|
.split(|&b| b == 0xff)
|
|
|
|
.map(|b| str::from_utf8(b))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
if let &[_, Ok(room_id), Ok(username), Ok(_variable)] = keys.as_slice() {
|
|
|
|
Ok(RoomAndUser {
|
|
|
|
room_id: room_id.to_owned(),
|
|
|
|
username: username.to_owned(),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Err(MigrationError::MigrationFailed(
|
|
|
|
"a key violates utf8 schema".to_string(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(MigrationError::MigrationFailed(
|
|
|
|
"encountered unexpected key".to_string(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(in crate::db) fn add_room_user_variable_count(db: &Database) -> Result<(), DataError> {
|
2020-10-24 13:46:06 +00:00
|
|
|
let tree = &db.variables.0;
|
|
|
|
let prefix = variables_space_prefix("");
|
|
|
|
|
|
|
|
//Extract a vec of tuples, consisting of room id + username.
|
2020-10-30 20:16:08 +00:00
|
|
|
let results: Vec<RoomAndUser> = tree
|
2020-10-24 13:46:06 +00:00
|
|
|
.scan_prefix(prefix)
|
2020-10-30 20:16:08 +00:00
|
|
|
.map(map_value_to_room_and_user)
|
2020-10-24 13:46:06 +00:00
|
|
|
.collect::<Result<Vec<_>, MigrationError>>()?;
|
|
|
|
|
2020-10-30 20:16:08 +00:00
|
|
|
let counts: HashMap<RoomAndUser, u32> =
|
2020-10-24 13:46:06 +00:00
|
|
|
results
|
|
|
|
.into_iter()
|
|
|
|
.fold(HashMap::new(), |mut count_map, room_and_user| {
|
|
|
|
let count = count_map.entry(room_and_user).or_insert(0);
|
|
|
|
*count += 1;
|
|
|
|
count_map
|
|
|
|
});
|
|
|
|
|
|
|
|
//Start a transaction on the variables tree.
|
|
|
|
let tx_result: Result<_, TransactionError<DataError>> = db.variables.0.transaction(|tx_vars| {
|
|
|
|
let batch = counts.iter().fold(Batch::default(), |mut batch, entry| {
|
2020-10-30 20:16:08 +00:00
|
|
|
let (info, count) = entry;
|
2020-10-24 13:46:06 +00:00
|
|
|
|
2020-10-30 20:16:08 +00:00
|
|
|
//Add variable count according to new schema.
|
|
|
|
let delineator = super::RoomAndUser(&info.room_id, &info.username);
|
|
|
|
let key = variables_space_key(delineator, VARIABLE_COUNT_KEY);
|
|
|
|
let db_value: U32<LittleEndian> = U32::new(*count);
|
2020-10-24 13:46:06 +00:00
|
|
|
batch.insert(key, db_value.as_bytes());
|
2020-10-30 20:16:08 +00:00
|
|
|
|
|
|
|
//Delete the old variable_count variable if exists.
|
|
|
|
let old_key = v0_variable_key(&info, "variable_count");
|
|
|
|
batch.remove(old_key);
|
2020-10-24 13:46:06 +00:00
|
|
|
batch
|
|
|
|
});
|
|
|
|
|
|
|
|
tx_vars.apply_batch(&batch)?;
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
|
|
|
|
tx_result?; //For some reason, it cannot infer the type
|
|
|
|
Ok(())
|
|
|
|
}
|