Skip to content

Commit 2a7951c

Browse files
authored
LUCENE-9907: Remove unused methods in PackedInts (#94)
1 parent bd8f182 commit 2a7951c

File tree

2 files changed

+38
-202
lines changed

2 files changed

+38
-202
lines changed

lucene/core/src/java/org/apache/lucene/util/packed/PackedInts.java

Lines changed: 3 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@
1818

1919
import java.io.IOException;
2020
import java.util.Arrays;
21-
import org.apache.lucene.codecs.CodecUtil;
2221
import org.apache.lucene.store.DataInput;
2322
import org.apache.lucene.store.DataOutput;
24-
import org.apache.lucene.store.IndexInput;
2523
import org.apache.lucene.util.Accountable;
2624
import org.apache.lucene.util.LongsRef;
2725
import org.apache.lucene.util.RamUsageEstimator;
@@ -506,13 +504,13 @@ public void clear() {
506504
}
507505

508506
/**
509-
* Save this mutable into <code>out</code>. Instantiating a reader from the generated data will
510-
* return a reader with the same number of bits per value.
507+
* Save this mutable into <code>out</code>. This method does not write any metadata to the
508+
* stream, meaning that it is your responsibility to store it somewhere else in order to be able
509+
* to recover data from the stream later on.
511510
*/
512511
public void save(DataOutput out) throws IOException {
513512
Writer writer =
514513
getWriterNoHeader(out, getFormat(), size(), getBitsPerValue(), DEFAULT_BUFFER_SIZE);
515-
writer.writeHeader();
516514
for (int i = 0; i < size(); ++i) {
517515
writer.add(get(i));
518516
}
@@ -632,14 +630,6 @@ protected Writer(DataOutput out, int valueCount, int bitsPerValue) {
632630
this.bitsPerValue = bitsPerValue;
633631
}
634632

635-
void writeHeader() throws IOException {
636-
assert valueCount != -1;
637-
CodecUtil.writeHeader(out, CODEC_NAME, VERSION_CURRENT);
638-
out.writeVInt(bitsPerValue);
639-
out.writeVInt(valueCount);
640-
out.writeVInt(getFormat().getId());
641-
}
642-
643633
/** The format used to serialize values. */
644634
protected abstract PackedInts.Format getFormat();
645635

@@ -716,24 +706,6 @@ public static Reader getReaderNoHeader(
716706
}
717707
}
718708

719-
/**
720-
* Restore a {@link Reader} from a stream.
721-
*
722-
* @param in the stream to read data from
723-
* @return a Reader
724-
* @throws IOException If there is a low-level I/O error
725-
* @lucene.internal
726-
*/
727-
public static Reader getReader(DataInput in) throws IOException {
728-
final int version = CodecUtil.checkHeader(in, CODEC_NAME, VERSION_START, VERSION_CURRENT);
729-
final int bitsPerValue = in.readVInt();
730-
assert bitsPerValue > 0 && bitsPerValue <= 64 : "bitsPerValue=" + bitsPerValue;
731-
final int valueCount = in.readVInt();
732-
final Format format = Format.byId(in.readVInt());
733-
734-
return getReaderNoHeader(in, format, version, valueCount, bitsPerValue);
735-
}
736-
737709
/**
738710
* Expert: Restore a {@link ReaderIterator} from a stream without reading metadata at the
739711
* beginning of the stream. This method is useful to restore data from streams which have been
@@ -756,76 +728,6 @@ public static ReaderIterator getReaderIteratorNoHeader(
756728
return new PackedReaderIterator(format, version, valueCount, bitsPerValue, in, mem);
757729
}
758730

759-
/**
760-
* Retrieve PackedInts as a {@link ReaderIterator}
761-
*
762-
* @param in positioned at the beginning of a stored packed int structure.
763-
* @param mem how much memory the iterator is allowed to use to read-ahead (likely to speed up
764-
* iteration)
765-
* @return an iterator to access the values
766-
* @throws IOException if the structure could not be retrieved.
767-
* @lucene.internal
768-
*/
769-
public static ReaderIterator getReaderIterator(DataInput in, int mem) throws IOException {
770-
final int version = CodecUtil.checkHeader(in, CODEC_NAME, VERSION_START, VERSION_CURRENT);
771-
final int bitsPerValue = in.readVInt();
772-
assert bitsPerValue > 0 && bitsPerValue <= 64 : "bitsPerValue=" + bitsPerValue;
773-
final int valueCount = in.readVInt();
774-
final Format format = Format.byId(in.readVInt());
775-
return getReaderIteratorNoHeader(in, format, version, valueCount, bitsPerValue, mem);
776-
}
777-
778-
/**
779-
* Expert: Construct a direct {@link Reader} from a stream without reading metadata at the
780-
* beginning of the stream. This method is useful to restore data from streams which have been
781-
* created using {@link PackedInts#getWriterNoHeader(DataOutput, Format, int, int, int)}.
782-
*
783-
* <p>The returned reader will have very little memory overhead, but every call to {@link
784-
* Reader#get(int)} is likely to perform a disk seek.
785-
*
786-
* @param in the stream to read data from
787-
* @param format the format used to serialize
788-
* @param version the version used to serialize the data
789-
* @param valueCount how many values the stream holds
790-
* @param bitsPerValue the number of bits per value
791-
* @return a direct Reader
792-
* @lucene.internal
793-
*/
794-
public static Reader getDirectReaderNoHeader(
795-
final IndexInput in, Format format, int version, int valueCount, int bitsPerValue) {
796-
checkVersion(version);
797-
switch (format) {
798-
case PACKED:
799-
return new DirectPackedReader(bitsPerValue, valueCount, in);
800-
case PACKED_SINGLE_BLOCK:
801-
return new DirectPacked64SingleBlockReader(bitsPerValue, valueCount, in);
802-
default:
803-
throw new AssertionError("Unknown format: " + format);
804-
}
805-
}
806-
807-
/**
808-
* Construct a direct {@link Reader} from an {@link IndexInput}. This method is useful to restore
809-
* data from streams which have been created using {@link PackedInts#getWriter(DataOutput, int,
810-
* int, float)}.
811-
*
812-
* <p>The returned reader will have very little memory overhead, but every call to {@link
813-
* Reader#get(int)} is likely to perform a disk seek.
814-
*
815-
* @param in the stream to read data from
816-
* @return a direct Reader
817-
* @throws IOException If there is a low-level I/O error
818-
* @lucene.internal
819-
*/
820-
public static Reader getDirectReader(IndexInput in) throws IOException {
821-
final int version = CodecUtil.checkHeader(in, CODEC_NAME, VERSION_START, VERSION_CURRENT);
822-
final int bitsPerValue = in.readVInt();
823-
assert bitsPerValue > 0 && bitsPerValue <= 64 : "bitsPerValue=" + bitsPerValue;
824-
final int valueCount = in.readVInt();
825-
final Format format = Format.byId(in.readVInt());
826-
return getDirectReaderNoHeader(in, format, version, valueCount, bitsPerValue);
827-
}
828-
829731
/**
830732
* Create a packed integer array with the given amount of values initialized to 0. the valueCount
831733
* and the bitsPerValue cannot be changed after creation. All Mutables known by this factory are
@@ -913,49 +815,6 @@ public static Writer getWriterNoHeader(
913815
return new PackedWriter(format, out, valueCount, bitsPerValue, mem);
914816
}
915817

916-
/**
917-
* Create a packed integer array writer for the given output, format, value count, and number of
918-
* bits per value.
919-
*
920-
* <p>The resulting stream will be long-aligned. This means that depending on the format which is
921-
* used under the hoods, up to 63 bits will be wasted. An easy way to make sure that no space is
922-
* lost is to always use a <code>valueCount</code> that is a multiple of 64.
923-
*
924-
* <p>This method writes metadata to the stream, so that the resulting stream is sufficient to
925-
* restore a {@link Reader} from it. You don't need to track <code>valueCount</code> or <code>
926-
* bitsPerValue</code> by yourself. In case this is a problem, you should probably look at {@link
927-
* #getWriterNoHeader(DataOutput, Format, int, int, int)}.
928-
*
929-
* <p>The <code>acceptableOverheadRatio</code> parameter controls how readers that will be
930-
* restored from this stream trade space for speed by selecting a faster but potentially less
931-
* memory-efficient implementation. An <code>acceptableOverheadRatio</code> of {@link
932-
* PackedInts#COMPACT} will make sure that the most memory-efficient implementation is selected
933-
* whereas {@link PackedInts#FASTEST} will make sure that the fastest implementation is selected.
934-
* In case you are only interested in reading this stream sequentially later on, you should
935-
* probably use {@link PackedInts#COMPACT}.
936-
*
937-
* @param out the data output
938-
* @param valueCount the number of values
939-
* @param bitsPerValue the number of bits per value
940-
* @param acceptableOverheadRatio an acceptable overhead ratio per value
941-
* @return a Writer
942-
* @throws IOException If there is a low-level I/O error
943-
* @lucene.internal
944-
*/
945-
public static Writer getWriter(
946-
DataOutput out, int valueCount, int bitsPerValue, float acceptableOverheadRatio)
947-
throws IOException {
948-
assert valueCount >= 0;
949-
950-
final FormatAndBits formatAndBits =
951-
fastestFormatAndBits(valueCount, bitsPerValue, acceptableOverheadRatio);
952-
final Writer writer =
953-
getWriterNoHeader(
954-
out, formatAndBits.format, valueCount, formatAndBits.bitsPerValue, DEFAULT_BUFFER_SIZE);
955-
writer.writeHeader();
956-
return writer;
957-
}
958-
959818
/**
960819
* Returns how many bits are required to hold values up to and including maxValue NOTE: This
961820
* method returns at least 1.

lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.java

Lines changed: 35 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.util.List;
2626
import java.util.Locale;
2727
import java.util.Random;
28-
import org.apache.lucene.codecs.CodecUtil;
2928
import org.apache.lucene.store.ByteArrayDataInput;
3029
import org.apache.lucene.store.ByteBuffersDirectory;
3130
import org.apache.lucene.store.DataInput;
@@ -103,14 +102,9 @@ public void testPackedInts() throws IOException {
103102
final Directory d = newDirectory();
104103

105104
IndexOutput out = d.createOutput("out.bin", newIOContext(random()));
106-
final float acceptableOverhead;
107-
if (iter == 0) {
108-
// have the first iteration go through exact nbits
109-
acceptableOverhead = 0.0f;
110-
} else {
111-
acceptableOverhead = random().nextFloat();
112-
}
113-
PackedInts.Writer w = PackedInts.getWriter(out, valueCount, nbits, acceptableOverhead);
105+
final int mem = random().nextInt(2 * PackedInts.DEFAULT_BUFFER_SIZE);
106+
PackedInts.Writer w =
107+
PackedInts.getWriterNoHeader(out, PackedInts.Format.PACKED, valueCount, nbits, mem);
114108
final long startFp = out.getFilePointer();
115109

116110
final int actualValueCount =
@@ -133,24 +127,11 @@ public void testPackedInts() throws IOException {
133127
w.getFormat().byteCount(PackedInts.VERSION_CURRENT, valueCount, w.bitsPerValue);
134128
assertEquals(bytes, fp - startFp);
135129

136-
{ // test header
137-
IndexInput in = d.openInput("out.bin", newIOContext(random()));
138-
// header = codec header | bitsPerValue | valueCount | format
139-
CodecUtil.checkHeader(
140-
in,
141-
PackedInts.CODEC_NAME,
142-
PackedInts.VERSION_START,
143-
PackedInts.VERSION_CURRENT); // codec header
144-
assertEquals(w.bitsPerValue, in.readVInt());
145-
assertEquals(valueCount, in.readVInt());
146-
assertEquals(w.getFormat().getId(), in.readVInt());
147-
assertEquals(startFp, in.getFilePointer());
148-
in.close();
149-
}
150-
151130
{ // test reader
152131
IndexInput in = d.openInput("out.bin", newIOContext(random()));
153-
PackedInts.Reader r = PackedInts.getReader(in);
132+
PackedInts.Reader r =
133+
PackedInts.getReaderNoHeader(
134+
in, PackedInts.Format.PACKED, PackedInts.VERSION_CURRENT, valueCount, nbits);
154135
assertEquals(fp, in.getFilePointer());
155136
for (int i = 0; i < valueCount; i++) {
156137
assertEquals(
@@ -177,7 +158,14 @@ public void testPackedInts() throws IOException {
177158

178159
{ // test reader iterator next
179160
IndexInput in = d.openInput("out.bin", newIOContext(random()));
180-
PackedInts.ReaderIterator r = PackedInts.getReaderIterator(in, bufferSize);
161+
PackedInts.ReaderIterator r =
162+
PackedInts.getReaderIteratorNoHeader(
163+
in,
164+
PackedInts.Format.PACKED,
165+
PackedInts.VERSION_CURRENT,
166+
valueCount,
167+
nbits,
168+
bufferSize);
181169
for (int i = 0; i < valueCount; i++) {
182170
assertEquals(
183171
"index="
@@ -198,7 +186,14 @@ public void testPackedInts() throws IOException {
198186

199187
{ // test reader iterator bulk next
200188
IndexInput in = d.openInput("out.bin", newIOContext(random()));
201-
PackedInts.ReaderIterator r = PackedInts.getReaderIterator(in, bufferSize);
189+
PackedInts.ReaderIterator r =
190+
PackedInts.getReaderIteratorNoHeader(
191+
in,
192+
PackedInts.Format.PACKED,
193+
PackedInts.VERSION_CURRENT,
194+
valueCount,
195+
nbits,
196+
bufferSize);
202197
int i = 0;
203198
while (i < valueCount) {
204199
final int count = TestUtil.nextInt(random(), 1, 95);
@@ -221,27 +216,6 @@ public void testPackedInts() throws IOException {
221216
assertEquals(fp, in.getFilePointer());
222217
in.close();
223218
}
224-
225-
{ // test direct reader get
226-
IndexInput in = d.openInput("out.bin", newIOContext(random()));
227-
PackedInts.Reader intsEnum = PackedInts.getDirectReader(in);
228-
for (int i = 0; i < valueCount; i++) {
229-
final String msg =
230-
"index="
231-
+ i
232-
+ " valueCount="
233-
+ valueCount
234-
+ " nbits="
235-
+ nbits
236-
+ " for "
237-
+ intsEnum.getClass().getSimpleName();
238-
final int index = random().nextInt(valueCount);
239-
assertEquals(msg, values[index], intsEnum.get(index));
240-
}
241-
intsEnum.get(intsEnum.size() - 1);
242-
assertEquals(fp, in.getFilePointer());
243-
in.close();
244-
}
245219
d.close();
246220
}
247221
}
@@ -288,13 +262,6 @@ public void testEndPointer() throws IOException {
288262
}
289263
assertEquals(msg, byteCount, in.getFilePointer());
290264

291-
// test direct reader
292-
in.seek(0L);
293-
final PackedInts.Reader directReader =
294-
PackedInts.getDirectReaderNoHeader(in, format, version, valueCount, bpv);
295-
directReader.get(valueCount - 1);
296-
assertEquals(msg, byteCount, in.getFilePointer());
297-
298265
// test reader
299266
in.seek(0L);
300267
PackedInts.getReaderNoHeader(in, format, version, valueCount, bpv);
@@ -480,15 +447,19 @@ public void testSingleValue() throws Exception {
480447
for (int bitsPerValue = 1; bitsPerValue <= 64; ++bitsPerValue) {
481448
Directory dir = newDirectory();
482449
IndexOutput out = dir.createOutput("out", newIOContext(random()));
483-
PackedInts.Writer w = PackedInts.getWriter(out, 1, bitsPerValue, PackedInts.DEFAULT);
450+
PackedInts.Writer w =
451+
PackedInts.getWriterNoHeader(
452+
out, PackedInts.Format.PACKED, 1, bitsPerValue, PackedInts.DEFAULT_BUFFER_SIZE);
484453
long value = 17L & PackedInts.maxValue(bitsPerValue);
485454
w.add(value);
486455
w.finish();
487456
final long end = out.getFilePointer();
488457
out.close();
489458

490459
IndexInput in = dir.openInput("out", newIOContext(random()));
491-
Reader reader = PackedInts.getReader(in);
460+
Reader reader =
461+
PackedInts.getReaderNoHeader(
462+
in, PackedInts.Format.PACKED, PackedInts.VERSION_CURRENT, 1, bitsPerValue);
492463
String msg = "Impl=" + w.getClass().getSimpleName() + ", bitsPerValue=" + bitsPerValue;
493464
assertEquals(msg, 1, reader.size());
494465
assertEquals(msg, value, reader.get(0));
@@ -910,7 +881,13 @@ public void testSave() throws IOException {
910881
out.close();
911882

912883
IndexInput in = directory.openInput("packed-ints.bin", IOContext.DEFAULT);
913-
PackedInts.Reader reader = PackedInts.getReader(in);
884+
PackedInts.Reader reader =
885+
PackedInts.getReaderNoHeader(
886+
in,
887+
mutable.getFormat(),
888+
PackedInts.VERSION_CURRENT,
889+
mutable.size(),
890+
mutable.getBitsPerValue());
914891
assertEquals(valueCount, reader.size());
915892
if (mutable instanceof Packed64SingleBlock) {
916893
// make sure that we used the right format so that the reader has

0 commit comments

Comments
 (0)