op_store: rename variables in upgrade code to be direction-agnostic

The code for migrating from ProtoBuf to Thrift is almost completely
independent of which direction the upgrade goes, so we can very easily
reuse it for migrating from Thrift to Protobuf. This patch renames
some variables to "old/new" instead of "proto/thrift", making the next
patch even simpler.
This commit is contained in:
Martin von Zweigbergk 2022-12-02 12:41:25 -08:00 committed by Martin von Zweigbergk
parent 472dee6403
commit 5a9d1e5fdd

View file

@ -45,7 +45,7 @@ pub struct SimpleOpStore {
fn upgrade_to_thrift(store_path: PathBuf) -> std::io::Result<()> { fn upgrade_to_thrift(store_path: PathBuf) -> std::io::Result<()> {
println!("Upgrading operation log to Thrift format..."); println!("Upgrading operation log to Thrift format...");
let proto_store = ProtoOpStore::load(store_path.clone()); let old_store = ProtoOpStore::load(store_path.clone());
let tmp_store_dir = tempfile::Builder::new() let tmp_store_dir = tempfile::Builder::new()
.prefix("jj-op-store-upgrade-") .prefix("jj-op-store-upgrade-")
.tempdir_in(store_path.parent().unwrap()) .tempdir_in(store_path.parent().unwrap())
@ -66,12 +66,12 @@ fn upgrade_to_thrift(store_path: PathBuf) -> std::io::Result<()> {
} }
// Do a DFS to rewrite the operations // Do a DFS to rewrite the operations
let thrift_store = ThriftOpStore::init(tmp_store_path.clone()); let new_store = ThriftOpStore::init(tmp_store_path.clone());
let mut converted: HashMap<OperationId, OperationId> = HashMap::new(); let mut converted: HashMap<OperationId, OperationId> = HashMap::new();
// The DFS stack // The DFS stack
let mut to_convert = old_op_heads let mut to_convert = old_op_heads
.iter() .iter()
.map(|op_id| (op_id.clone(), proto_store.read_operation(op_id).unwrap())) .map(|op_id| (op_id.clone(), old_store.read_operation(op_id).unwrap()))
.collect_vec(); .collect_vec();
while !to_convert.is_empty() { while !to_convert.is_empty() {
let (_, op) = to_convert.last().unwrap(); let (_, op) = to_convert.last().unwrap();
@ -83,20 +83,20 @@ fn upgrade_to_thrift(store_path: PathBuf) -> std::io::Result<()> {
if let Some(new_parent_id) = converted.get(parent_id) { if let Some(new_parent_id) = converted.get(parent_id) {
new_parent_ids.push(new_parent_id.clone()); new_parent_ids.push(new_parent_id.clone());
} else { } else {
let parent_op = proto_store.read_operation(parent_id).unwrap(); let parent_op = old_store.read_operation(parent_id).unwrap();
new_to_convert.push((parent_id.clone(), parent_op)); new_to_convert.push((parent_id.clone(), parent_op));
} }
} }
if new_to_convert.is_empty() { if new_to_convert.is_empty() {
// If all parents have already been converted, remove this operation from the // If all parents have already been converted, remove this operation from the
// stack and convert it // stack and convert it
let (op_id, mut op) = to_convert.pop().unwrap(); let (old_op_id, mut old_op) = to_convert.pop().unwrap();
op.parents = new_parent_ids; old_op.parents = new_parent_ids;
let view = proto_store.read_view(&op.view_id).unwrap(); let old_view = old_store.read_view(&old_op.view_id).unwrap();
let thrift_view_id = thrift_store.write_view(&view).unwrap(); let new_view_id = new_store.write_view(&old_view).unwrap();
op.view_id = thrift_view_id; old_op.view_id = new_view_id;
let thrift_op_id = thrift_store.write_operation(&op).unwrap(); let new_op_id = new_store.write_operation(&old_op).unwrap();
converted.insert(op_id, thrift_op_id); converted.insert(old_op_id, new_op_id);
} else { } else {
to_convert.extend(new_to_convert); to_convert.extend(new_to_convert);
} }