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 crate::core::index::bytes_ref::BytesRefValueEnum;
use crate::core::index::impact::Impact;
use crate::core::index::impacts::Impacts;
use crate::core::index::impacts_enum::ImpactsEnum;
use crate::core::index::impacts_source::ImpactsSource;
use crate::core::index::postings_enum::PostingsEnum;
use crate::core::search::doc_id_set_iterator::DocIdSetIterator;
use crate::core::search::doc_id_set_iterator::NO_MORE_DOCS;
use crate::core::util::error::lucene_error::Result;

/// [`ImpactsEnum`] that doesn't index impacts but implements the API in a legal way.
/// This is typically used for short postings that do not need skipping.
pub struct SlowImpactsEnum<P> {
  pub(crate) delegate: P,
}
impl<P> SlowImpactsEnum<P> {
  pub fn new(delegate: P) -> Self {
    SlowImpactsEnum { delegate }
  }
}

impl<P> PostingsEnum for SlowImpactsEnum<P>
where
  P: PostingsEnum,
{
  fn freq(&mut self) -> Result<i32> {
    self.delegate.freq()
  }

  #[inline(always)]
  fn next_position(&mut self) -> Result<i32> {
    self.delegate.next_position()
  }

  fn start_offset(&self) -> Result<i32> {
    self.delegate.start_offset()
  }

  fn end_offset(&self) -> Result<i32> {
    self.delegate.end_offset()
  }

  fn get_payload(&self) -> Result<Option<BytesRefValueEnum<'_>>> {
    self.delegate.get_payload()
  }
}

impl<P> crate::core::search::doc_id_set_iterator::DocIdSetIteratorExtensions for SlowImpactsEnum<P> where
  P: PostingsEnum
{
}
impl<P> crate::core::search::doc_id_set_iterator::BitSetIteratorAccess for SlowImpactsEnum<P> where
  P: PostingsEnum
{
}

impl<P> DocIdSetIterator for SlowImpactsEnum<P>
where
  P: PostingsEnum,
{
  fn doc_id(&self) -> i32 {
    self.delegate.doc_id()
  }

  fn next_doc(&mut self) -> Result<i32> {
    self.delegate.next_doc()
  }

  fn advance(&mut self, target: i32) -> Result<i32> {
    self.delegate.advance(target)
  }

  fn cost(&self) -> Result<i64> {
    self.delegate.cost()
  }
}

impl<P> ImpactsSource for SlowImpactsEnum<P>
where
  P: PostingsEnum,
{
  fn advance_shallow(&mut self, _target: i32) -> Result<()> {
    Ok(())
  }

  type Impacts<'a>
    = DummyImpacts
  where
    Self: 'a;

  fn get_impacts(&self) -> Result<Self::Impacts<'_>> {
    Ok(DummyImpacts::new())
  }
}

impl<P> ImpactsEnum for SlowImpactsEnum<P> where P: PostingsEnum {}

pub struct DummyImpacts {
  impacts: [Impact; 1],
}
impl Default for DummyImpacts {
  fn default() -> Self {
    Self::new()
  }
}

impl DummyImpacts {
  pub fn new() -> Self {
    DummyImpacts {
      impacts: [Impact::new(i32::MAX, 1)],
    }
  }
}
impl Impacts for DummyImpacts {
  fn num_levels(&self) -> usize {
    1
  }

  fn get_doc_id_upto(&self, _level: usize) -> i32 {
    NO_MORE_DOCS
  }

  fn get_impacts(&self, _level: usize) -> Result<Vec<Impact>> {
    Ok(self.impacts.to_vec())
  }
}