16
16
*/
17
17
package org .apache .lucene .codecs .lucene90 ;
18
18
19
+ import java .io .IOException ;
20
+ import java .util .ArrayList ;
21
+ import java .util .Collections ;
22
+ import java .util .List ;
19
23
import org .apache .lucene .codecs .Codec ;
24
+ import org .apache .lucene .codecs .CodecUtil ;
25
+ import org .apache .lucene .index .IndexFileNames ;
26
+ import org .apache .lucene .index .SegmentInfo ;
27
+ import org .apache .lucene .store .ChecksumIndexInput ;
28
+ import org .apache .lucene .store .Directory ;
29
+ import org .apache .lucene .store .IOContext ;
20
30
import org .apache .lucene .tests .index .BaseCompoundFormatTestCase ;
21
31
import org .apache .lucene .tests .util .TestUtil ;
22
32
@@ -27,4 +37,61 @@ public class TestLucene90CompoundFormat extends BaseCompoundFormatTestCase {
27
37
protected Codec getCodec () {
28
38
return codec ;
29
39
}
40
+
41
+ public void testFileLengthOrdering () throws IOException {
42
+ Directory dir = newDirectory ();
43
+ // Setup the test segment
44
+ String segment = "_123" ;
45
+ int chunk = 1024 ; // internal buffer size used by the stream
46
+ SegmentInfo si = newSegmentInfo (dir , segment );
47
+ byte [] segId = si .getId ();
48
+ List <String > orderedFiles = new ArrayList <>();
49
+ int randomFileSize = random ().nextInt (chunk );
50
+ for (int i = 0 ; i < 10 ; i ++) {
51
+ String filename = segment + "." + i ;
52
+ createRandomFile (dir , filename , randomFileSize , segId );
53
+ // increase the next files size by a random amount
54
+ randomFileSize += random ().nextInt (100 ) + 1 ;
55
+ orderedFiles .add (filename );
56
+ }
57
+ List <String > shuffledFiles = new ArrayList <>(orderedFiles );
58
+ Collections .shuffle (shuffledFiles , random ());
59
+ si .setFiles (shuffledFiles );
60
+ si .getCodec ().compoundFormat ().write (dir , si , IOContext .DEFAULT );
61
+
62
+ // entries file should contain files ordered by their size
63
+ String entriesFileName =
64
+ IndexFileNames .segmentFileName (si .name , "" , Lucene90CompoundFormat .ENTRIES_EXTENSION );
65
+ try (ChecksumIndexInput entriesStream =
66
+ dir .openChecksumInput (entriesFileName , IOContext .READ )) {
67
+ Throwable priorE = null ;
68
+ try {
69
+ CodecUtil .checkIndexHeader (
70
+ entriesStream ,
71
+ Lucene90CompoundFormat .ENTRY_CODEC ,
72
+ Lucene90CompoundFormat .VERSION_START ,
73
+ Lucene90CompoundFormat .VERSION_CURRENT ,
74
+ si .getId (),
75
+ "" );
76
+ final int numEntries = entriesStream .readVInt ();
77
+ long lastOffset = 0 ;
78
+ long lastLength = 0 ;
79
+ for (int i = 0 ; i < numEntries ; i ++) {
80
+ final String id = entriesStream .readString ();
81
+ assertEquals (orderedFiles .get (i ), segment + id );
82
+ long offset = entriesStream .readLong ();
83
+ assertTrue (offset > lastOffset );
84
+ lastOffset = offset ;
85
+ long length = entriesStream .readLong ();
86
+ assertTrue (length >= lastLength );
87
+ lastLength = length ;
88
+ }
89
+ } catch (Throwable exception ) {
90
+ priorE = exception ;
91
+ } finally {
92
+ CodecUtil .checkFooter (entriesStream , priorE );
93
+ }
94
+ }
95
+ dir .close ();
96
+ }
30
97
}
0 commit comments