rlucene 0.1.0-alpha.3

A Rust port of Apache Lucene, a full-text search engine library
Documentation
/*
 * 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()
  }
}