1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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.
*/
use std::collections::HashMap;
use std::fmt::Display;
use std::sync::Arc;
use crate::core::store::directory::Directory;
use crate::core::util::HasIdentity;
use crate::core::util::error::lucene_error::Result;
pub trait IndexCommit: PartialEq + Eq + PartialOrd + Ord + Display {
/// Returns the segments file (`segments_N`) associated with this commit point.
fn get_segments_file_name(&self) -> &str;
/// Returns all index files referenced by this commit point.
fn get_file_names(&self) -> Result<&[String]>;
type Directory: Directory;
/// Returns the [`Directory`] for the index.
fn get_directory(&self) -> Self::Directory;
/// Delete this commit point. This only applies when using the commit point in the context of
/// [`IndexWriter`](crate::core::index::index_writer::IndexWriter)’s [`IndexDeletionPolicy`](crate::core::index::index_deletion_policy::IndexDeletionPolicy).
///
/// Upon calling this, the writer is notified that this commit point should be deleted.
///
/// Decision that a commit-point should be deleted is taken by the [`IndexDeletionPolicy`](crate::core::index::index_deletion_policy::IndexDeletionPolicy)
/// in effect and therefore this should only be called by its
/// [`IndexDeletionPolicy::on_init()`](crate::core::index::index_deletion_policy::IndexDeletionPolicy::on_init) or [`IndexDeletionPolicy::on_commit()`](crate::core::index::index_deletion_policy::IndexDeletionPolicy::on_commit) methods.
fn delete(&self) -> Result<()>;
/// Returns `true` if this commit should be deleted; this is only used by [`IndexWriter`](crate::core::index::index_writer::IndexWriter) after
/// invoking the [`IndexDeletionPolicy`](crate::core::index::index_deletion_policy::IndexDeletionPolicy).
fn is_deleted(&self) -> bool;
/// Returns number of segments referenced by this commit.
fn get_segment_count(&self) -> usize;
/// Returns the generation (the _N in segments_N) for this IndexCommit
fn get_generation(&self) -> i64;
/// Returns `user_data`, previously passed to [`IndexWriter::set_live_commit_data()`](crate::core::index::index_writer::IndexWriter::set_live_commit_data) for this commit. The map is `String` → `String`.
fn get_user_data(&self) -> &HashMap<String, String>;
}
use std::cmp::Ordering;
pub fn is_same_commit<T>(a: &T, b: &T) -> bool
where
T: IndexCommit,
{
let a_directory = a.get_directory();
let b_directory = b.get_directory();
a_directory.is_same_identity(&b_directory) && a.get_generation() == b.get_generation()
}
pub fn cmp_commit<T>(a: &T, b: &T) -> Ordering
where
T: IndexCommit,
{
let a_directory = a.get_directory();
let b_directory = b.get_directory();
debug_assert!(a_directory.is_same_identity(&b_directory));
a.get_generation().cmp(&b.get_generation())
}
impl<IC> IndexCommit for Arc<IC>
where
IC: IndexCommit + ?Sized,
{
fn get_segments_file_name(&self) -> &str {
(**self).get_segments_file_name()
}
fn get_file_names(&self) -> Result<&[String]> {
(**self).get_file_names()
}
type Directory = IC::Directory;
fn get_directory(&self) -> Self::Directory {
(**self).get_directory()
}
fn delete(&self) -> Result<()> {
(**self).delete()
}
fn is_deleted(&self) -> bool {
(**self).is_deleted()
}
fn get_segment_count(&self) -> usize {
(**self).get_segment_count()
}
fn get_generation(&self) -> i64 {
(**self).get_generation()
}
fn get_user_data(&self) -> &HashMap<String, String> {
(**self).get_user_data()
}
}