| 60 | |
| 61 | impl WorkingCopyMutTxnT for WriteTxn<'_> { |
| 62 | fn put_working_copy(&mut self, record: &WorkingCopyRecord) -> PristineResult<()> { |
| 63 | let encoded = encode_working_copy_record(record)?; |
| 64 | |
| 65 | if ViewTxnT::get_view_by_id(self, record.desired_view)?.is_none() { |
| 66 | return Err(PristineError::ViewNotFound { |
| 67 | name: format!("desired view id={}", record.desired_view), |
| 68 | }); |
| 69 | } |
| 70 | |
| 71 | if let Some(existing) = WorkingCopyTxnT::get_working_copy(self, record.id)? { |
| 72 | if existing.location_fingerprint != record.location_fingerprint { |
| 73 | return Err(PristineError::WorkingCopyIdentityConflict { |
| 74 | requested_id: record.id.to_string(), |
| 75 | existing_id: existing.id.to_string(), |
| 76 | }); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | if let Some(existing) = |
| 81 | WorkingCopyTxnT::find_working_copy_by_location(self, &record.location_fingerprint)? |
| 82 | { |
| 83 | if existing.id != record.id { |
| 84 | return Err(PristineError::WorkingCopyIdentityConflict { |
| 85 | requested_id: record.id.to_string(), |
| 86 | existing_id: existing.id.to_string(), |
| 87 | }); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | let mut table = match self.txn.open_table(WORKING_COPIES) { |
| 92 | Ok(table) => table, |
| 93 | Err(redb::TableError::TableDoesNotExist(_)) => { |
| 94 | return Err(PristineError::WorkingCopySchemaUnavailable); |
| 95 | } |
| 96 | Err(error) => return Err(error.into()), |
| 97 | }; |
| 98 | table.insert(record.id.as_bytes(), encoded.as_slice())?; |
| 99 | Ok(()) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | #[cfg(test)] |