Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

index: split IndexEntry, etc. to sub module #2695

Merged
merged 4 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 39 additions & 6 deletions lib/src/default_index/composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,48 @@ use std::sync::Arc;

use itertools::Itertools;

use super::entry::{IndexEntry, IndexPosition, IndexPositionByGeneration, SmallIndexPositionsVec};
use super::readonly::ReadonlyIndexSegment;
use super::rev_walk::RevWalk;
use super::{IndexEntry, IndexPosition, IndexPositionByGeneration, IndexSegment};
use crate::backend::{CommitId, ObjectId};
use crate::backend::{ChangeId, CommitId, ObjectId};
use crate::index::{HexPrefix, Index, PrefixResolution};
use crate::revset::{ResolvedExpression, Revset, RevsetEvaluationError};
use crate::store::Store;
use crate::{backend, default_revset_engine};

pub(super) trait IndexSegment: Send + Sync {
fn segment_num_parent_commits(&self) -> u32;

fn segment_num_commits(&self) -> u32;

fn segment_parent_file(&self) -> Option<&Arc<ReadonlyIndexSegment>>;

fn segment_name(&self) -> Option<String>;

fn segment_commit_id_to_pos(&self, commit_id: &CommitId) -> Option<IndexPosition>;

/// Suppose the given `commit_id` exists, returns the positions of the
/// previous and next commit ids in lexicographical order.
fn segment_commit_id_to_neighbor_positions(
&self,
commit_id: &CommitId,
) -> (Option<IndexPosition>, Option<IndexPosition>);

fn segment_resolve_prefix(&self, prefix: &HexPrefix) -> PrefixResolution<CommitId>;

fn segment_generation_number(&self, local_pos: u32) -> u32;

fn segment_commit_id(&self, local_pos: u32) -> CommitId;

fn segment_change_id(&self, local_pos: u32) -> ChangeId;

fn segment_num_parents(&self, local_pos: u32) -> u32;

fn segment_parent_positions(&self, local_pos: u32) -> SmallIndexPositionsVec;

fn segment_entry_by_pos(&self, pos: IndexPosition, local_pos: u32) -> IndexEntry;
}

#[derive(Clone, Copy)]
pub struct CompositeIndex<'a>(&'a dyn IndexSegment);

Expand Down Expand Up @@ -150,7 +183,7 @@ impl<'a> CompositeIndex<'a> {
if descendant_pos == ancestor_pos {
return true;
}
if !visited.insert(descendant_entry.pos) {
if !visited.insert(descendant_entry.position()) {
continue;
}
if descendant_entry.generation_number() <= ancestor_generation {
Expand Down Expand Up @@ -182,15 +215,15 @@ impl<'a> CompositeIndex<'a> {
let item1 = dedup_pop(&mut items1).unwrap();
let entry1 = self.entry_by_pos(item1.pos);
for parent_entry in entry1.parents() {
assert!(parent_entry.pos < entry1.pos);
assert!(parent_entry.position() < entry1.position());
items1.push(IndexPositionByGeneration::from(&parent_entry));
}
}
Ordering::Less => {
let item2 = dedup_pop(&mut items2).unwrap();
let entry2 = self.entry_by_pos(item2.pos);
for parent_entry in entry2.parents() {
assert!(parent_entry.pos < entry2.pos);
assert!(parent_entry.position() < entry2.position());
items2.push(IndexPositionByGeneration::from(&parent_entry));
}
}
Expand Down Expand Up @@ -238,7 +271,7 @@ impl<'a> CompositeIndex<'a> {
candidate_positions.remove(&item.pos);
let entry = self.entry_by_pos(item.pos);
for parent_entry in entry.parents() {
assert!(parent_entry.pos < entry.pos);
assert!(parent_entry.position() < entry.position());
work.push(IndexPositionByGeneration::from(&parent_entry));
}
}
Expand Down
150 changes: 150 additions & 0 deletions lib/src/default_index/entry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// Copyright 2023 The Jujutsu Authors
//
// 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.

#![allow(missing_docs)]

use std::cmp::Ordering;
use std::fmt::{Debug, Formatter};
use std::hash::{Hash, Hasher};

use smallvec::SmallVec;

use super::composite::{CompositeIndex, IndexSegment};
use crate::backend::{ChangeId, CommitId, ObjectId};

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
pub struct IndexPosition(pub(super) u32);

impl IndexPosition {
pub const MAX: Self = IndexPosition(u32::MAX);
}

// SmallVec reuses two pointer-size fields as inline area, which meas we can
// inline up to 16 bytes (on 64-bit platform) for free.
pub(super) type SmallIndexPositionsVec = SmallVec<[IndexPosition; 4]>;

#[derive(Clone)]
pub struct IndexEntry<'a> {
source: &'a dyn IndexSegment,
pos: IndexPosition,
// Position within the source segment
local_pos: u32,
}

impl Debug for IndexEntry<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("IndexEntry")
.field("pos", &self.pos)
.field("local_pos", &self.local_pos)
.field("commit_id", &self.commit_id().hex())
.finish()
}
}

impl PartialEq for IndexEntry<'_> {
fn eq(&self, other: &Self) -> bool {
self.pos == other.pos
}
}

impl Eq for IndexEntry<'_> {}

impl Hash for IndexEntry<'_> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.pos.hash(state)
}
}

impl<'a> IndexEntry<'a> {
pub(super) fn new(source: &'a dyn IndexSegment, pos: IndexPosition, local_pos: u32) -> Self {
IndexEntry {
source,
pos,
local_pos,
}
}

pub fn position(&self) -> IndexPosition {
self.pos
}

pub fn generation_number(&self) -> u32 {
self.source.segment_generation_number(self.local_pos)
}

pub fn commit_id(&self) -> CommitId {
self.source.segment_commit_id(self.local_pos)
}

pub fn change_id(&self) -> ChangeId {
self.source.segment_change_id(self.local_pos)
}

pub fn num_parents(&self) -> u32 {
self.source.segment_num_parents(self.local_pos)
}

pub fn parent_positions(&self) -> SmallIndexPositionsVec {
self.source.segment_parent_positions(self.local_pos)
}

pub fn parents(&self) -> impl ExactSizeIterator<Item = IndexEntry<'a>> {
let composite = CompositeIndex::new(self.source);
self.parent_positions()
.into_iter()
.map(move |pos| composite.entry_by_pos(pos))
}
}

#[derive(Clone, Eq, PartialEq)]
pub struct IndexEntryByPosition<'a>(pub IndexEntry<'a>);

impl Ord for IndexEntryByPosition<'_> {
fn cmp(&self, other: &Self) -> Ordering {
self.0.pos.cmp(&other.0.pos)
}
}

impl PartialOrd for IndexEntryByPosition<'_> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

/// Wrapper to sort `IndexPosition` by its generation number.
///
/// This is similar to `IndexEntry` newtypes, but optimized for size and cache
/// locality. The original `IndexEntry` will have to be looked up when needed.
#[derive(Clone, Copy, Debug, Ord, PartialOrd)]
pub(super) struct IndexPositionByGeneration {
pub generation: u32, // order by generation number
pub pos: IndexPosition, // tie breaker
}

impl Eq for IndexPositionByGeneration {}

impl PartialEq for IndexPositionByGeneration {
fn eq(&self, other: &Self) -> bool {
self.pos == other.pos
}
}

impl From<&IndexEntry<'_>> for IndexPositionByGeneration {
fn from(entry: &IndexEntry<'_>) -> Self {
IndexPositionByGeneration {
generation: entry.generation_number(),
pos: entry.position(),
}
}
}
Loading