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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
/*
* 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::fmt;
use std::fmt::{Display, Formatter};
use crate::core::store::{DataInput, DataOutput};
use crate::core::util::accountable::Accountable;
use crate::core::util::core_helper::CoreHelper;
use crate::core::util::error::lucene_error::{LuceneError, Result};
use crate::core::util::longs_ref::LongsRef;
use crate::core::util::packed::bulk_operation::of;
use crate::core::util::packed::bulk_operation_packed_enum::BulkOperationPackedEnum;
use crate::core::util::packed::format_behavior::{PackedImpl, PackedSingleBlockImpl};
use crate::core::util::packed::mutable_packed64_enum::MutablePacked64Enum;
use crate::core::util::packed::packed_reader_iterator::PackedReaderIterator;
use crate::core::util::packed::packed_writer::PackedWriter;
use crate::core::util::packed::packed64::Packed64;
use crate::core::util::packed::packed64_single_block::create;
pub struct PackedInts;
impl PackedInts {
/// At most 700% memory overhead, always select a direct implementation.
pub const FASTEST: f32 = 7.0;
/// At most 50% memory overhead, always select a reasonably fast
/// implementation.
pub const FAST: f32 = 0.5;
/// At most 25% memory overhead.
pub const DEFAULT: f32 = 0.25;
/// No memory overhead at all, but the returned implementation may be slow.
pub const COMPACT: f32 = 0.0;
/// Default amount of memory to use for bulk operations (1KB).
pub const DEFAULT_BUFFER_SIZE: i32 = 1024;
/// Codec name for PackedInts.
pub const CODEC_NAME: &'static str = "PackedInts";
/// Version constants.
pub const VERSION_MONOTONIC_WITHOUT_ZIGZAG: i32 = 2;
pub const VERSION_START: i32 = Self::VERSION_MONOTONIC_WITHOUT_ZIGZAG;
pub const VERSION_CURRENT: i32 = Self::VERSION_MONOTONIC_WITHOUT_ZIGZAG;
/// Get a [`Decoder`].
///
/// # Arguments
/// - `format`: The format used to store packed integers.
/// - `version`: The compatibility version.
/// - `bits_per_value`: The number of bits per value.
///
/// # Returns
/// A decoder.
pub(crate) fn get_decoder(
format: Format,
version: i32,
bits_per_value: i32,
) -> Result<&'static BulkOperationPackedEnum> {
check_version(version)?;
of(format, bits_per_value)
}
/// Get an [`Encoder`].
///
/// # Arguments
/// - `format`: The format used to store packed integers.
/// - `version`: The compatibility version.
/// - `bits_per_value`: The number of bits per value.
///
/// # Returns
/// A result containing a reference to the encoder, or an error if the
/// version is invalid.
pub(crate) fn get_encoder(
format: Format,
version: i32,
bits_per_value: i32,
) -> Result<&'static BulkOperationPackedEnum> {
PackedInts::get_decoder(format, version, bits_per_value)
}
/// Expert: Restore a [`ReaderIterator`] from a stream without reading
/// metadata at the beginning of the stream. This method is useful to
/// restore data from streams which have been created using
/// [`PackedInts`].
///
/// # Arguments
/// - `input`: The stream to read data from, positioned at the beginning of
/// the packed values.
/// - `format`: The format used to serialize.
/// - `version`: The version used to serialize the data.
/// - `value_count`: How many values the stream holds.
/// - `bits_per_value`: The number of bits per value.
/// - `mem`: How much memory the iterator is allowed to use to read-ahead
/// (likely to speed up iteration).
///
/// # Returns
/// A [`ReaderIterator`].
///
/// # Errors
/// Returns an error if the version is invalid.
pub fn get_reader_iterator_no_header<T>(
input: &mut T,
format: Format,
version: i32,
value_count: i32,
bits_per_value: i32,
mem: i32,
) -> Result<ReaderIteratorImpl<PackedReaderIterator<'_, T>>>
where
T: DataInput,
{
check_version(version)?;
let sub_reader =
PackedReaderIterator::new(format, version, value_count, bits_per_value, input, mem)?;
Ok(ReaderIteratorImpl::new(
value_count,
bits_per_value,
sub_reader,
))
}
/// Create a packed integer array with the given amount of values
/// initialized to 0. The `value_count` and the `bits_per_value` cannot
/// be changed after creation. All mutables known by this factory
/// are kept fully in RAM.
///
/// Positive values of `acceptable_overhead_ratio` will trade space for
/// speed by selecting a faster but potentially less memory-efficient
/// implementation. An `acceptable_overhead_ratio` of
/// [`PackedInts::COMPACT`] will make sure that the most memory-efficient
/// implementation is selected, whereas [`PackedInts::FASTEST`] will
/// make sure that the fastest implementation is selected.
///
/// # Arguments
/// - `value_count`: The number of elements.
/// - `bits_per_value`: The number of bits available for any given value.
/// - `acceptable_overhead_ratio`: An acceptable overhead ratio per value.
///
/// # Returns
/// A mutable packed integer array.
pub(crate) fn get_mutable(
value_count: usize,
bits_per_value: i32,
acceptable_overhead_ratio: f32,
) -> Result<MutablePacked64Enum> {
let format_and_bits = fastest_format_and_bits(
value_count as i32,
bits_per_value,
acceptable_overhead_ratio,
);
PackedInts::get_mutable_impl(
value_count,
format_and_bits.bits_per_value,
format_and_bits.format,
)
}
/// Creates a mutable packed array for a bit width that has already been
/// validated by the caller.
///
/// Growable writers use this helper from infallible `Mutable` methods. The
/// bit width produced by those methods is always in the supported range.
/// Same as [`get_mutable`](PackedInts::get_mutable) with a pre-computed
/// number of bits per value and format.
pub(crate) fn get_mutable_impl(
value_count: usize,
bits_per_value: i32,
format: Format,
) -> Result<MutablePacked64Enum> {
match format {
Format::PackedSingleBlock(_) => create(value_count, bits_per_value),
Format::Packed(_) => Ok(MutablePacked64Enum::P64(Packed64::new(
value_count,
bits_per_value,
))),
}
}
/// Expert: Create a packed integer array writer for the given output,
/// format, value count, and number of bits per value.
///
/// The resulting stream will be long-aligned. This means that depending on
/// the format which is used, up to 63 bits will be wasted. An easy way
/// to make sure that no space is lost is to always use a `value_count`
/// that is a multiple of 64.
///
/// This method does not write any metadata to the stream, meaning that it
/// is your responsibility to store it somewhere else in order to be
/// able to recover data from the stream later on:
///
/// - `format` (using
/// [`Format::get_id`](crate::core::util::packed::FormatBehavior::get_id))
/// - `value_count`
/// - `bits_per_value`
/// - [`PackedInts::VERSION_CURRENT`].
///
/// It is possible to start writing values without knowing how many of them
/// you are actually going to write. To do this, just pass `-1` as
/// `value_count`. On the other hand, for any positive
/// value of `value_count`, the returned writer will make sure that you
/// don't write more values than expected and pad the end of the stream
/// with zeros in case you have written less than `value_count`
/// when calling [`Writer::finish`].
///
/// The `mem` parameter lets you control how much memory can be used to
/// buffer changes in memory before flushing to disk. High values of
/// `mem` are likely to improve throughput. On the other hand, if speed
/// is not that important to you, a value of `0` will use as little memory
/// as possible and should already offer reasonable throughput.
///
/// # Arguments
/// - `out`: The data output.
/// - `format`: The format to use to serialize the values.
/// - `value_count`: The number of values.
/// - `bits_per_value`: The number of bits per value.
/// - `mem`: How much memory (in bytes) can be used to speed up
/// serialization.
///
/// # Returns
/// A [`Writer`](crate::core::util::packed::packed_ints::Writer) instance.
pub(crate) fn get_writer_no_header<T>(
out: &mut T,
format: Format,
value_count: i32,
bits_per_value: i32,
mem: i32,
) -> Result<PackedWriter<'_, T>>
where
T: DataOutput,
{
PackedWriter::new(format, out, value_count, bits_per_value, mem)
}
/// Returns how many bits are required to hold values up to and including
/// `max_value`.
///
/// This method will always return at least 1.
///
/// # Arguments
///
/// * `max_value` - The maximum value that should be representable.
///
/// # Returns
///
/// The number of bits needed to represent values from 0 to `max_value`.
pub fn bits_required(max_value: i64) -> Result<i32> {
if max_value < 0 {
return Err(LuceneError::illegal_argument(format!(
"max_value must be non-negative (got {max_value})"
)));
}
Ok(PackedInts::unsigned_bits_required(max_value))
}
/// Returns how many bits are required to store `bits`, interpreted as an
/// unsigned value. NOTE: This method returns at least 1.
///
/// # Arguments
/// - `bits`: The unsigned value for which to determine the required bit
/// count.
///
/// # Returns
/// The number of bits required to store `bits`.
pub const fn unsigned_bits_required(bits: i64) -> i32 {
let num_bits = 64 - bits.leading_zeros();
if num_bits < 1 { 1 } else { num_bits as i32 }
}
/// Calculates the maximum unsigned long that can be expressed with the
/// given number of bits.
///
/// # Arguments
///
/// * `bits_per_value` - The number of bits available for any given value.
///
/// # Returns
///
/// The maximum value for the given number of bits.
pub const fn max_value(bits_per_value: i32) -> i64 {
if bits_per_value == 64 {
i64::MAX
} else {
!(!0i64 << bits_per_value)
}
}
/// Copy `src[src_pos..src_pos+len]` into `dest[dest_pos..dest_pos+len]`
/// using at most `mem` bytes.
pub fn copy<T, T2>(
src: &mut T,
src_pos: usize,
dest: &mut T2,
dest_pos: usize,
len: usize,
mem: usize,
) -> Result<()>
where
T: Reader,
T2: Mutable,
{
debug_assert!(
src_pos + len <= src.size(),
"Source position and length out of bounds"
);
debug_assert!(
dest_pos + len <= dest.size(),
"Destination position and length out of bounds"
);
let capacity = mem >> 3;
if capacity == 0 {
for i in 0..len {
dest.set(dest_pos + i, src.get(src_pos + i))?;
}
} else if len > 0 {
// Use bulk operations
let buf_size = capacity.min(len);
stack_or_heap_buffer!(buf, i64, buf_size, 128, 0);
PackedInts::copy_with_buffer(src, src_pos, dest, dest_pos, len, buf)?;
}
Ok(())
}
/// Same as `copy` but uses a pre-allocated buffer.
pub fn copy_with_buffer<T, T2>(
src: &T,
mut src_pos: usize,
dest: &mut T2,
mut dest_pos: usize,
mut len: usize,
buf: &mut [i64],
) -> Result<()>
where
T: Reader,
T2: Mutable,
{
debug_assert!(!buf.is_empty(), "Buffer length must be greater than 0");
let mut remaining = 0;
while len > 0 {
debug_assert!(buf.len() <= i32::MAX as usize);
let read = src.get_bulk(src_pos, buf, remaining, len.min(buf.len() - remaining))?;
debug_assert!(read > 0, "Read operation failed");
src_pos += read;
len -= read;
remaining += read;
let written = dest.set_bulk(dest_pos, buf, 0, remaining)?;
debug_assert!(written > 0, "Write operation failed");
dest_pos += written;
if written < remaining {
buf.copy_within(written..remaining, 0);
}
remaining -= written;
}
while remaining > 0 {
let written = dest.set_bulk(dest_pos, buf, 0, remaining)?;
dest_pos += written;
remaining -= written;
if remaining > 0 {
buf.copy_within(written..written + remaining, 0);
}
}
Ok(())
}
/// Check that the block size is a power of 2, within the right bounds, and
/// return its log in base 2.
pub fn check_block_size(
block_size: i32,
min_block_size: i32,
max_block_size: i32,
) -> Result<i32> {
if block_size < min_block_size || block_size > max_block_size {
return Err(LuceneError::illegal_argument(format!(
"block_size must be >= {min_block_size} and <= {max_block_size}, got {block_size}"
)));
}
if block_size & (block_size - 1) != 0 {
return Err(LuceneError::illegal_argument(format!(
"block_size must be a power of two, got {block_size}"
)));
}
let result = block_size.trailing_zeros();
debug_assert!(result <= i32::MAX as u32);
Ok(result as i32)
}
/// Return the number of blocks required to store `size` values on
/// `block_size`.
pub fn num_blocks(size: usize, block_size: usize) -> Result<usize> {
let num_blocks = (size / block_size)
+ if size.is_multiple_of(block_size) {
0
} else {
1
};
let result = num_blocks.checked_mul(block_size);
match result {
Some(result) => {
if result < size || num_blocks > i32::MAX as usize {
return Err(LuceneError::illegal_argument(
"size is too large for this block size",
));
}
Ok(num_blocks)
},
None => Err(LuceneError::illegal_argument(format!(
"multiply overflow:block_size:{block_size}, num_blocks:{num_blocks} "
))),
}
}
}
/// Check the validity of a version number.
///
/// # Arguments
///
/// * `version` - The version number to check.
///
/// # Errors
///
/// Returns an [`IllegalArgumentError`](crate::core::util::error::IllegalArgumentError) if the version is out of bounds.
pub fn check_version(version: i32) -> Result<()> {
if version < PackedInts::VERSION_START {
return Err(LuceneError::illegal_argument(format!(
"Version is too old, should be at least {} (got {})",
PackedInts::VERSION_START,
version
)));
} else if version > PackedInts::VERSION_CURRENT {
return Err(LuceneError::illegal_argument(format!(
"Version is too new, should be at most {} (got {})",
PackedInts::VERSION_CURRENT,
version
)));
}
Ok(())
}
/// A format to write packed integers.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Format {
/// Compact format, all bits are written contiguously.
Packed(PackedImpl),
/// A format that may insert padding bits to improve encoding and decoding
/// speed. This format is deprecated; use `Packed` instead.
PackedSingleBlock(PackedSingleBlockImpl),
}
impl Default for Format {
fn default() -> Self {
Format::Packed(PackedImpl::new(0))
}
}
/// Represents a combination of Format and bitsPerValue.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FormatAndBits {
pub format: Format,
pub bits_per_value: i32,
}
/// Find the fastest [`Format`] and `bits_per_value` that would restore from
/// disk with overhead less than the specified acceptable overhead ratio.
///
/// # Arguments
///
/// * `value_count` - The number of values to write. Use `usize::MAX` if
/// unknown.
/// * `bits_per_value` - The number of bits per value.
/// * `acceptable_overhead_ratio` - The acceptable overhead ratio.
///
/// # Returns
///
/// A [`FormatAndBits`] struct containing the selected format and bits per value.
pub fn fastest_format_and_bits(
// TODO
_value_count: i32,
bits_per_value: i32,
mut acceptable_overhead_ratio: f32,
) -> FormatAndBits {
acceptable_overhead_ratio = CoreHelper::max_f32(PackedInts::COMPACT, acceptable_overhead_ratio);
acceptable_overhead_ratio = CoreHelper::min_f32(PackedInts::FASTEST, acceptable_overhead_ratio);
let acceptable_overhead_per_value = acceptable_overhead_ratio * bits_per_value as f32;
let max_bits_per_value = bits_per_value + acceptable_overhead_per_value as i32;
let actual_bits_per_value = if bits_per_value <= 8 && max_bits_per_value >= 8 {
8
} else if bits_per_value <= 16 && max_bits_per_value >= 16 {
16
} else if bits_per_value <= 32 && max_bits_per_value >= 32 {
32
} else if bits_per_value <= 64 && max_bits_per_value >= 64 {
64
} else {
bits_per_value
};
FormatAndBits {
format: Format::Packed(PackedImpl::new(0)),
bits_per_value: actual_bits_per_value,
}
}
/// A decoder for packed integers.
pub trait Decoder {
/// The minimum number of long blocks to encode in a single iteration, when
/// using `i64` encoding.
fn long_block_count(&self) -> usize;
/// The number of values that can be stored in `long_block_count()` long
/// blocks.
fn long_value_count(&self) -> usize;
/// The minimum number of byte blocks to encode in a single iteration, when
/// using byte encoding.
fn byte_block_count(&self) -> usize;
/// The number of values that can be stored in `byte_block_count()` byte
/// blocks.
fn byte_value_count(&self) -> usize;
/// Read `iterations * block_count()` blocks from `blocks`, decode them, and
/// write `iterations * value_count()` values into `values`.
///
/// # Arguments
///
/// * `blocks` - The long blocks that hold packed integer values.
/// * `blocks_offset` - The offset where to start reading blocks.
/// * `values` - The buffer to write the decoded values into.
/// * `values_offset` - The offset where to start writing values.
/// * `iterations` - Controls how much data to decode.
fn decode_u64_to_i64(
&self,
blocks: &[u64],
blocks_offset: usize,
values: &mut [i64],
values_offset: usize,
iterations: usize,
);
/// Read `8 * iterations * block_count()` blocks from `blocks`, decode them,
/// and write `iterations * value_count()` values into `values`.
///
/// # Arguments
///
/// * `blocks` - The long blocks that hold packed integer values.
/// * `blocks_offset` - The offset where to start reading blocks.
/// * `values` - The buffer to write the decoded values into.
/// * `values_offset` - The offset where to start writing values.
/// * `iterations` - Controls how much data to decode.
fn decode_u8_to_i64(
&self,
blocks: &[u8],
blocks_offset: usize,
values: &mut [i64],
values_offset: usize,
iterations: usize,
);
/// Read `iterations * block_count()` blocks from `blocks`, decode them, and
/// write `iterations * value_count()` values into `values`.
///
/// # Arguments
///
/// * `blocks` - The long blocks that hold packed integer values.
/// * `blocks_offset` - The offset where to start reading blocks.
/// * `values` - The buffer to write the decoded values into.
/// * `values_offset` - The offset where to start writing values.
/// * `iterations` - Controls how much data to decode.
fn decode_u64_to_i32(
&self,
blocks: &[u64],
blocks_offset: usize,
values: &mut [i32],
values_offset: usize,
iterations: usize,
) -> Result<()>;
/// Read `8 * iterations * block_count()` blocks from `blocks`, decode them,
/// and write `iterations * value_count()` values into `values`.
///
/// # Arguments
///
/// * `blocks` - The long blocks that hold packed integer values.
/// * `blocks_offset` - The offset where to start reading blocks.
/// * `values` - The buffer to write the decoded values into.
/// * `values_offset` - The offset where to start writing values.
/// * `iterations` - Controls how much data to decode.
fn decode_u8_to_i32(
&self,
blocks: &[u8],
blocks_offset: usize,
values: &mut [i32],
values_offset: usize,
iterations: usize,
) -> Result<()>;
}
/// An encoder for packed integers.
pub trait Encoder {
/// The minimum number of long blocks to encode in a single iteration, when
/// using `i64` encoding.
fn long_block_count(&self) -> usize;
/// The number of values that can be stored in `long_block_count()` long
/// blocks.
fn long_value_count(&self) -> usize;
/// The minimum number of byte blocks to encode in a single iteration, when
/// using byte encoding.
fn byte_block_count(&self) -> usize;
/// The number of values that can be stored in `byte_block_count()` byte
/// blocks.
fn byte_value_count(&self) -> usize;
/// Read `iterations * value_count()` values from `values`, encode them, and
/// write `iterations * block_count()` blocks into `blocks`.
///
/// # Arguments
///
/// * `values` - The buffer containing values to encode.
/// * `values_offset` - The offset where to start reading values.
/// * `blocks` - The buffer to write encoded blocks into.
/// * `blocks_offset` - The offset where to start writing blocks.
/// * `iterations` - Controls how much data to encode.
fn encode_i64_to_u64(
&self,
values: &[i64],
values_offset: usize,
blocks: &mut [u64],
blocks_offset: usize,
iterations: usize,
);
/// Read `iterations * value_count()` values from `values`, encode them, and
/// write `8 * iterations * block_count()` blocks into `blocks`.
///
/// # Arguments
///
/// * `values` - The buffer containing values to encode.
/// * `values_offset` - The offset where to start reading values.
/// * `blocks` - The buffer to write encoded blocks into.
/// * `blocks_offset` - The offset where to start writing blocks.
/// * `iterations` - Controls how much data to encode.
fn encode_i64_to_u8(
&self,
values: &[i64],
values_offset: usize,
blocks: &mut [u8],
blocks_offset: usize,
iterations: usize,
);
/// Read `iterations * value_count()` values from `values`, encode them, and
/// write `iterations * block_count()` blocks into `blocks`.
///
/// # Arguments
///
/// * `values` - The buffer containing values to encode.
/// * `values_offset` - The offset where to start reading values.
/// * `blocks` - The buffer to write encoded blocks into.
/// * `blocks_offset` - The offset where to start writing blocks.
/// * `iterations` - Controls how much data to encode.
fn encode_i32_to_u64(
&self,
values: &[i32],
values_offset: usize,
blocks: &mut [u64],
blocks_offset: usize,
iterations: usize,
);
/// Read `iterations * value_count()` values from `values`, encode them, and
/// write `8 * iterations * block_count()` blocks into `blocks`.
///
/// # Arguments
///
/// * `values` - The buffer containing values to encode.
/// * `values_offset` - The offset where to start reading values.
/// * `blocks` - The buffer to write encoded blocks into.
/// * `blocks_offset` - The offset where to start writing blocks.
/// * `iterations` - Controls how much data to encode.
fn encode_i32_to_u8(
&self,
values: &[i32],
values_offset: usize,
blocks: &mut [u8],
blocks_offset: usize,
iterations: usize,
);
}
/// A read-only random access array of positive integers.
pub trait Reader: Accountable {
/// Get the value at the given index.
fn get(&self, index: usize) -> i64;
/// Bulk get: read at least one and at most `len` values starting from
/// `index` into `arr[off.off+len]` and return the actual number of
/// values that have been read.
fn get_bulk(&self, index: usize, arr: &mut [i64], off: usize, len: usize) -> Result<usize> {
self.default_get_bulk(index, arr, off, len)
}
fn default_get_bulk(
&self,
index: usize,
arr: &mut [i64],
off: usize,
len: usize,
) -> Result<usize> {
debug_assert!(len > 0, "len must be > 0");
debug_assert!(index < self.size(), "index out of bounds: {index}");
debug_assert!(
(off + len) <= arr.len(),
"offset + len exceeds array length"
);
let gets = std::cmp::min(self.size() - index, len);
for (i, o) in (index..index + gets).zip(off..off + gets) {
arr[o] = self.get(i);
}
Ok(gets)
}
/// Returns the number of values in the reader.
fn size(&self) -> usize;
}
/// Run-once iterator trait for decoding previously saved packed integers.
pub trait ReaderIterator: Display {
/// Returns the next value.
///
/// # Errors
///
/// Returns an error if there is an issue decoding the next value.
fn next(&mut self) -> Result<i64> {
Err(LuceneError::not_implemented(""))
}
/// Returns at least 1 and at most `count` next values.
///
/// The returned reference MUST NOT be modified.
///
/// # Arguments
///
/// * `count` - The maximum number of values to retrieve.
///
/// # Errors
///
/// Returns an error if there is an issue decoding the values.
fn next_batch(&mut self, count: usize) -> Result<&mut LongsRef>;
/// Returns the number of bits per value.
fn get_bits_per_value(&self) -> Result<i32> {
Err(LuceneError::not_implemented(""))
}
/// Returns the total number of values.
fn size(&self) -> Result<i32> {
Err(LuceneError::not_implemented(""))
}
/// Returns the current position.
fn ord(&self) -> i32;
}
/// A base implementation of the [`ReaderIterator`] trait.
pub struct ReaderIteratorImpl<C> {
bits_per_value: i32,
value_count: i32,
sub_reader: C,
}
impl<C> ReaderIteratorImpl<C> {
/// Creates a new [`ReaderIteratorImpl`].
///
/// # Arguments
///
/// * `value_count` - Total number of values.
/// * `bits_per_value` - Number of bits per value.
pub fn new(value_count: i32, bits_per_value: i32, sub_reader: C) -> Self {
Self {
bits_per_value,
value_count,
sub_reader,
}
}
}
impl<C> ReaderIterator for ReaderIteratorImpl<C>
where
C: ReaderIterator,
{
fn next(&mut self) -> Result<i64> {
let next_values = self.next_batch(1)?;
debug_assert!(next_values.length > 0, "next_values buffer is empty");
let result = next_values.longs[next_values.offset];
next_values.offset += 1;
next_values.length -= 1;
Ok(result)
}
fn next_batch(&mut self, count: usize) -> Result<&mut LongsRef> {
self.sub_reader.next_batch(count)
}
fn get_bits_per_value(&self) -> Result<i32> {
Ok(self.bits_per_value)
}
fn size(&self) -> Result<i32> {
Ok(self.value_count)
}
fn ord(&self) -> i32 {
self.sub_reader.ord()
}
}
impl<C> Display for ReaderIteratorImpl<C>
where
C: Display,
{
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.sub_reader)
}
}
pub trait Mutable: Reader + Display {
/// Returns the number of bits used to store any given value.
///
/// Note: This does not imply that memory usage is `bits_per_value *
/// values, as implementations may use non-space-optimal
/// packing of bits.
fn get_bits_per_value(&self) -> i32;
/// Sets the value at the given index in the array.
///
/// # Arguments
///
/// * `index` - The position where the value should be set.
/// * `value` - The value to be stored, which must conform to the
/// constraints of the array.
fn set(&mut self, index: usize, value: i64) -> Result<()>;
/// Sets a range of values in the array.
///
/// # Arguments
///
/// * `index` - The starting index in the packed array where values will be
/// set.
/// * `arr` - The source array of values to set.
/// * `off` - The offset in the source array to start reading values from.
/// * `len` - The maximum number of values to set.
///
/// # Returns
///
/// The actual number of values that have been set.
fn set_bulk(&mut self, index: usize, arr: &[i64], off: usize, len: usize) -> Result<usize> {
self.default_set_bulk(index, arr, off, len)
}
fn default_set_bulk(
&mut self,
index: usize,
arr: &[i64],
off: usize,
len: usize,
) -> Result<usize> {
debug_assert!(len > 0, "len must be > 0 (got {len})");
debug_assert!(index < self.size(), "Index out of bounds: {index}");
let len = len.min(self.size() - index);
debug_assert!(
(off + len) <= arr.len(),
"Array offset and length out of bounds"
);
for (i, o) in (index..index + len).zip(off..off + len) {
self.set(i, arr[o])?;
}
Ok(len)
}
/// Fills a range in the packed array with a specific value.
///
/// # Arguments
///
/// * `from_index` - The start index of the range to fill (inclusive).
/// * `to_index` - The end index of the range to fill (exclusive).
/// * `val` - The value to fill with.
fn fill(&mut self, from_index: usize, to_index: usize, val: i64) -> Result<()> {
self.default_fill(from_index, to_index, val)
}
fn default_fill(&mut self, from_index: usize, to_index: usize, val: i64) -> Result<()> {
debug_assert!(val <= PackedInts::max_value(self.get_bits_per_value()));
debug_assert!(
from_index <= to_index,
"from_index must be <= to_index: {from_index} > {to_index}"
);
for i in from_index..to_index {
self.set(i, val)?;
}
Ok(())
}
/// Sets all values in the packed array to 0.
fn clear(&mut self) -> Result<()> {
self.fill(0, self.size(), 0)
}
}
#[allow(dead_code)]
pub struct MutableImpl;
pub struct NullReader {
value_count: usize,
}
impl NullReader {
pub fn for_count(value_count: usize) -> Self {
Self { value_count }
}
pub fn new(value_count: usize) -> Self {
Self::for_count(value_count)
}
}
impl Accountable for NullReader {
fn ram_bytes_used(&self) -> Result<i64> {
Ok(0)
}
}
impl Reader for NullReader {
fn get(&self, _index: usize) -> i64 {
0
}
fn get_bulk(&self, index: usize, arr: &mut [i64], off: usize, mut len: usize) -> Result<usize> {
debug_assert!(len > 0, "len must be > 0 (got {len})");
debug_assert!(
index < self.value_count,
"index out of bounds (index={}, valueCount={})",
index,
self.value_count
);
len = len.min(self.value_count - index);
debug_assert!(
(off + len) <= arr.len(),
"not enough space in destination array"
);
arr[off..(off + len)].fill(0);
Ok(len)
}
fn size(&self) -> usize {
self.value_count
}
}
pub trait Writer {
/// The format used to serialize values.
fn get_format(&self) -> &Format;
/// Add a value to the stream.
fn add(&mut self, v: i64) -> Result<()>;
/// The number of bits per value.
fn bits_per_values(&self) -> i32;
/// Perform end-of-stream operations.
fn finish(&mut self) -> Result<()>;
/// Returns the current ord in the stream (number of values that have been
/// written so far minus one).
fn ord(&self) -> i32;
}