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::store::{DataOutput, IndexInput, write_group_vints_i32};
use crate::core::util::error::lucene_error::Result;
use crate::core::util::group_vint_util::GroupVIntUtil;
/// Utility struct to encode/decode postings block.
pub(crate) struct PostingsUtil;

impl PostingsUtil {
  /// Read values that have been written using variable-length encoding and
  /// group-varint encoding instead of bit-packing.
  pub(crate) fn read_vint_block<II>(
    doc_in: &mut II,
    doc_buffer: &mut [i32],
    freq_buffer: &mut [i32],
    num: usize,
    index_has_freq: bool,
    decode_freq: bool,
  ) -> Result<()>
  where
    II: IndexInput,
  {
    GroupVIntUtil::read_group_vints_i32(doc_in, doc_buffer, num)?;
    if index_has_freq && decode_freq {
      if freq_buffer.len() < num {
        for i in 0..num {
          freq_buffer[i] = doc_buffer[i] & 1;
          doc_buffer[i] = ((doc_buffer[i] as u32) >> 1) as i32;
          if freq_buffer[i] == 0 {
            freq_buffer[i] = doc_in.read_vint()?;
          }
        }
        return Ok(());
      }
      let doc_buffer = &mut doc_buffer[..num];
      let freq_buffer = &mut freq_buffer[..num];
      for i in 0..num {
        freq_buffer[i] = doc_buffer[i] & 0x01;
        doc_buffer[i] = ((doc_buffer[i] as u32) >> 1) as i32;
        if freq_buffer[i] == 0 {
          freq_buffer[i] = doc_in.read_vint()?;
        }
      }
    } else if index_has_freq {
      for val in doc_buffer.iter_mut().take(num) {
        *val = ((*val as u32) >> 1) as i32;
      }
    }
    Ok(())
  }
  /// Write freq buffer with variable-length encoding and doc buffer with
  /// group-varint encoding.
  pub(crate) fn write_vint_block<DO>(
    doc_out: &mut DO,
    doc_buffer: &mut [i32],
    freq_buffer: &[i32],
    num: usize,
    write_freqs: bool,
  ) -> Result<()>
  where
    DO: DataOutput,
  {
    if write_freqs {
      if (4..=16).contains(&num) && doc_buffer.len() >= num && freq_buffer.len() >= num {
        let (docs, doc_tail) = doc_buffer[..num].as_chunks_mut::<4>();
        let (freqs, freq_tail) = freq_buffer[..num].as_chunks::<4>();
        for (docs, freqs) in docs.iter_mut().zip(freqs) {
          let values = wide::i32x4::new(*docs);
          let flags = wide::i32x4::new(*freqs).simd_eq(wide::i32x4::splat(1));
          *docs = ((values << 1u32) - flags).to_array();
        }
        for (doc, &freq) in doc_tail.iter_mut().zip(freq_tail) {
          *doc = (*doc << 1) | if freq == 1 { 1 } else { 0 };
        }
      } else {
        for i in 0..num {
          doc_buffer[i] = (doc_buffer[i] << 1) | if freq_buffer[i] == 1 { 1 } else { 0 };
        }
      }
    }
    write_group_vints_i32(doc_out, doc_buffer, num)?;

    if write_freqs {
      for &freq in freq_buffer.iter().take(num) {
        if freq != 1 {
          doc_out.write_vint(freq)?;
        }
      }
    }

    Ok(())
  }
}