2022-11-26 23:57:50 +00:00
|
|
|
// Copyright 2020 The Jujutsu Authors
|
2020-12-12 08:00:42 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2023-03-11 19:25:13 +00:00
|
|
|
use std::any::Any;
|
2023-03-01 05:50:04 +00:00
|
|
|
use std::fmt::Debug;
|
2023-03-01 06:12:18 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use thiserror::Error;
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2023-03-01 05:50:04 +00:00
|
|
|
use crate::backend::{CommitId, ObjectId};
|
2023-03-11 20:52:55 +00:00
|
|
|
use crate::commit::Commit;
|
2023-03-23 05:02:25 +00:00
|
|
|
use crate::default_index_store::{IndexEntry, RevWalk};
|
2023-03-01 06:12:18 +00:00
|
|
|
use crate::op_store::OperationId;
|
|
|
|
use crate::operation::Operation;
|
2023-04-02 01:59:22 +00:00
|
|
|
use crate::revset::{Revset, RevsetEvaluationError, RevsetExpression};
|
2023-03-01 06:12:18 +00:00
|
|
|
use crate::store::Store;
|
|
|
|
|
|
|
|
#[derive(Debug, Error)]
|
|
|
|
pub enum IndexWriteError {
|
|
|
|
#[error("{0}")]
|
|
|
|
Other(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait IndexStore: Send + Sync + Debug {
|
2023-03-12 06:05:45 +00:00
|
|
|
fn as_any(&self) -> &dyn Any;
|
|
|
|
|
2023-03-01 06:12:18 +00:00
|
|
|
fn name(&self) -> &str;
|
|
|
|
|
2023-03-11 19:25:13 +00:00
|
|
|
fn get_index_at_op(&self, op: &Operation, store: &Arc<Store>) -> Box<dyn ReadonlyIndex>;
|
2023-03-01 06:12:18 +00:00
|
|
|
|
|
|
|
fn write_index(
|
|
|
|
&self,
|
2023-03-11 20:52:55 +00:00
|
|
|
index: Box<dyn MutableIndex>,
|
2023-03-01 06:12:18 +00:00
|
|
|
op_id: &OperationId,
|
2023-03-11 19:25:13 +00:00
|
|
|
) -> Result<Box<dyn ReadonlyIndex>, IndexWriteError>;
|
2023-03-01 06:12:18 +00:00
|
|
|
}
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2023-03-21 05:04:21 +00:00
|
|
|
pub trait Index: Send + Sync {
|
2023-03-12 17:37:20 +00:00
|
|
|
fn as_any(&self) -> &dyn Any;
|
|
|
|
|
2023-02-14 06:32:11 +00:00
|
|
|
fn shortest_unique_commit_id_prefix_len(&self, commit_id: &CommitId) -> usize;
|
|
|
|
|
|
|
|
fn resolve_prefix(&self, prefix: &HexPrefix) -> PrefixResolution<CommitId>;
|
|
|
|
|
|
|
|
fn entry_by_id(&self, commit_id: &CommitId) -> Option<IndexEntry>;
|
|
|
|
|
|
|
|
fn has_id(&self, commit_id: &CommitId) -> bool;
|
|
|
|
|
|
|
|
fn is_ancestor(&self, ancestor_id: &CommitId, descendant_id: &CommitId) -> bool;
|
|
|
|
|
|
|
|
fn common_ancestors(&self, set1: &[CommitId], set2: &[CommitId]) -> Vec<CommitId>;
|
|
|
|
|
|
|
|
fn walk_revs(&self, wanted: &[CommitId], unwanted: &[CommitId]) -> RevWalk;
|
|
|
|
|
|
|
|
fn heads(&self, candidates: &mut dyn Iterator<Item = &CommitId>) -> Vec<CommitId>;
|
|
|
|
|
2023-02-19 04:49:58 +00:00
|
|
|
/// Parents before children
|
2023-03-23 05:14:03 +00:00
|
|
|
fn topo_order(&self, input: &mut dyn Iterator<Item = &CommitId>) -> Vec<CommitId>;
|
2023-03-13 06:00:02 +00:00
|
|
|
|
|
|
|
fn evaluate_revset<'index>(
|
|
|
|
&'index self,
|
|
|
|
expression: &RevsetExpression,
|
2023-03-30 17:04:15 +00:00
|
|
|
store: &Arc<Store>,
|
|
|
|
visible_heads: &[CommitId],
|
2023-04-02 01:59:22 +00:00
|
|
|
) -> Result<Box<dyn Revset<'index> + 'index>, RevsetEvaluationError>;
|
2023-02-14 06:32:11 +00:00
|
|
|
}
|
|
|
|
|
2023-03-11 19:25:13 +00:00
|
|
|
pub trait ReadonlyIndex: Send + Sync {
|
|
|
|
fn as_any(&self) -> &dyn Any;
|
|
|
|
|
|
|
|
fn as_index(&self) -> &dyn Index;
|
|
|
|
|
2023-03-11 20:52:55 +00:00
|
|
|
fn start_modification(&self) -> Box<dyn MutableIndex>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait MutableIndex: Any {
|
|
|
|
fn into_any(self: Box<Self>) -> Box<dyn Any>;
|
|
|
|
|
|
|
|
fn as_index(&self) -> &dyn Index;
|
|
|
|
|
|
|
|
fn add_commit(&mut self, commit: &Commit);
|
|
|
|
|
|
|
|
fn merge_in(&mut self, other: &dyn ReadonlyIndex);
|
2023-03-11 19:25:13 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2023-01-18 11:48:50 +00:00
|
|
|
pub struct HexPrefix {
|
|
|
|
// For odd-length prefix, lower 4 bits of the last byte is padded with 0
|
|
|
|
min_prefix_bytes: Vec<u8>,
|
|
|
|
has_odd_byte: bool,
|
|
|
|
}
|
2020-12-12 08:00:42 +00:00
|
|
|
|
|
|
|
impl HexPrefix {
|
2023-01-18 11:48:50 +00:00
|
|
|
pub fn new(prefix: &str) -> Option<HexPrefix> {
|
|
|
|
let has_odd_byte = prefix.len() & 1 != 0;
|
|
|
|
let min_prefix_bytes = if has_odd_byte {
|
|
|
|
hex::decode(prefix.to_owned() + "0").ok()?
|
2021-04-10 06:51:37 +00:00
|
|
|
} else {
|
2023-01-18 11:48:50 +00:00
|
|
|
hex::decode(prefix).ok()?
|
|
|
|
};
|
|
|
|
Some(HexPrefix {
|
|
|
|
min_prefix_bytes,
|
|
|
|
has_odd_byte,
|
|
|
|
})
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2023-01-25 09:46:38 +00:00
|
|
|
pub fn from_bytes(bytes: &[u8]) -> Self {
|
|
|
|
HexPrefix {
|
|
|
|
min_prefix_bytes: bytes.to_owned(),
|
|
|
|
has_odd_byte: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-18 11:48:50 +00:00
|
|
|
pub fn hex(&self) -> String {
|
|
|
|
let mut hex_string = hex::encode(&self.min_prefix_bytes);
|
|
|
|
if self.has_odd_byte {
|
|
|
|
hex_string.pop().unwrap();
|
|
|
|
}
|
|
|
|
hex_string
|
2021-04-21 23:41:09 +00:00
|
|
|
}
|
|
|
|
|
2023-01-26 06:45:23 +00:00
|
|
|
/// Minimum bytes that would match this prefix. (e.g. "abc0" for "abc")
|
|
|
|
///
|
|
|
|
/// Use this to partition a sorted slice, and test `matches(id)` from there.
|
|
|
|
pub fn min_prefix_bytes(&self) -> &[u8] {
|
|
|
|
&self.min_prefix_bytes
|
|
|
|
}
|
|
|
|
|
2023-01-18 11:48:50 +00:00
|
|
|
fn split_odd_byte(&self) -> (Option<u8>, &[u8]) {
|
|
|
|
if self.has_odd_byte {
|
|
|
|
let (&odd, prefix) = self.min_prefix_bytes.split_last().unwrap();
|
|
|
|
(Some(odd), prefix)
|
2020-12-12 08:00:42 +00:00
|
|
|
} else {
|
2023-01-18 11:48:50 +00:00
|
|
|
(None, &self.min_prefix_bytes)
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-18 11:38:09 +00:00
|
|
|
pub fn matches<Q: ObjectId>(&self, id: &Q) -> bool {
|
2023-01-18 11:48:50 +00:00
|
|
|
let id_bytes = id.as_bytes();
|
|
|
|
let (maybe_odd, prefix) = self.split_odd_byte();
|
|
|
|
if id_bytes.starts_with(prefix) {
|
|
|
|
if let Some(odd) = maybe_odd {
|
|
|
|
matches!(id_bytes.get(prefix.len()), Some(v) if v & 0xf0 == odd)
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2021-05-30 06:52:50 +00:00
|
|
|
pub enum PrefixResolution<T> {
|
2020-12-12 08:00:42 +00:00
|
|
|
NoMatch,
|
2021-05-30 06:52:50 +00:00
|
|
|
SingleMatch(T),
|
2020-12-12 08:00:42 +00:00
|
|
|
AmbiguousMatch,
|
|
|
|
}
|
|
|
|
|
2021-05-30 06:52:50 +00:00
|
|
|
impl<T: Clone> PrefixResolution<T> {
|
2023-03-01 05:50:04 +00:00
|
|
|
pub fn plus(&self, other: &PrefixResolution<T>) -> PrefixResolution<T> {
|
2020-12-12 08:00:42 +00:00
|
|
|
match (self, other) {
|
|
|
|
(PrefixResolution::NoMatch, other) => other.clone(),
|
|
|
|
(local, PrefixResolution::NoMatch) => local.clone(),
|
|
|
|
(PrefixResolution::AmbiguousMatch, _) => PrefixResolution::AmbiguousMatch,
|
|
|
|
(_, PrefixResolution::AmbiguousMatch) => PrefixResolution::AmbiguousMatch,
|
|
|
|
(PrefixResolution::SingleMatch(_), PrefixResolution::SingleMatch(_)) => {
|
|
|
|
PrefixResolution::AmbiguousMatch
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-03-14 17:37:28 +00:00
|
|
|
use super::*;
|
2023-01-18 11:48:50 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_hex_prefix_prefixes() {
|
2023-01-26 06:45:23 +00:00
|
|
|
let prefix = HexPrefix::new("").unwrap();
|
|
|
|
assert_eq!(prefix.min_prefix_bytes(), b"");
|
2023-01-18 11:48:50 +00:00
|
|
|
|
2023-01-26 06:45:23 +00:00
|
|
|
let prefix = HexPrefix::new("1").unwrap();
|
|
|
|
assert_eq!(prefix.min_prefix_bytes(), b"\x10");
|
2023-01-18 11:48:50 +00:00
|
|
|
|
2023-01-26 06:45:23 +00:00
|
|
|
let prefix = HexPrefix::new("12").unwrap();
|
|
|
|
assert_eq!(prefix.min_prefix_bytes(), b"\x12");
|
2023-01-18 11:48:50 +00:00
|
|
|
|
2023-01-26 06:45:23 +00:00
|
|
|
let prefix = HexPrefix::new("123").unwrap();
|
|
|
|
assert_eq!(prefix.min_prefix_bytes(), b"\x12\x30");
|
2023-01-18 11:48:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_hex_prefix_matches() {
|
|
|
|
let id = CommitId::from_hex("1234");
|
|
|
|
|
|
|
|
assert!(HexPrefix::new("").unwrap().matches(&id));
|
|
|
|
assert!(HexPrefix::new("1").unwrap().matches(&id));
|
|
|
|
assert!(HexPrefix::new("12").unwrap().matches(&id));
|
|
|
|
assert!(HexPrefix::new("123").unwrap().matches(&id));
|
|
|
|
assert!(HexPrefix::new("1234").unwrap().matches(&id));
|
|
|
|
assert!(!HexPrefix::new("12345").unwrap().matches(&id));
|
|
|
|
|
|
|
|
assert!(!HexPrefix::new("a").unwrap().matches(&id));
|
|
|
|
assert!(!HexPrefix::new("1a").unwrap().matches(&id));
|
|
|
|
assert!(!HexPrefix::new("12a").unwrap().matches(&id));
|
|
|
|
assert!(!HexPrefix::new("123a").unwrap().matches(&id));
|
|
|
|
}
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|