21
21
import org .junit .BeforeClass ;
22
22
23
23
import java .io .IOException ;
24
+ import java .nio .ByteBuffer ;
25
+ import java .nio .ByteOrder ;
24
26
import java .util .concurrent .CountDownLatch ;
25
27
import java .util .concurrent .atomic .AtomicLong ;
26
28
@@ -60,26 +62,37 @@ protected byte[] randomReadAndSlice(IndexInput indexInput, int length) throws IO
60
62
int readPos = (int ) indexInput .getFilePointer ();
61
63
byte [] output = new byte [length ];
62
64
while (readPos < length ) {
63
- switch (randomIntBetween (0 , 5 )) {
64
- case 0 :
65
- // Read by one byte at a time
66
- output [readPos ++] = indexInput .readByte ();
65
+ final var readStrategy = randomFrom (2 , 6 );
66
+ switch (readStrategy ) {
67
+ case 0 , 1 , 2 , 3 :
68
+ if (length - readPos >= Long .BYTES && readStrategy <= 0 ) {
69
+ ByteBuffer .wrap (output , readPos , Long .BYTES ).order (ByteOrder .LITTLE_ENDIAN ).putLong (indexInput .readLong ());
70
+ readPos += Long .BYTES ;
71
+ } else if (length - readPos >= Integer .BYTES && readStrategy <= 1 ) {
72
+ ByteBuffer .wrap (output , readPos , Integer .BYTES ).order (ByteOrder .LITTLE_ENDIAN ).putInt (indexInput .readInt ());
73
+ readPos += Integer .BYTES ;
74
+ } else if (length - readPos >= Short .BYTES && readStrategy <= 2 ) {
75
+ ByteBuffer .wrap (output , readPos , Short .BYTES ).order (ByteOrder .LITTLE_ENDIAN ).putShort (indexInput .readShort ());
76
+ readPos += Short .BYTES ;
77
+ } else {
78
+ output [readPos ++] = indexInput .readByte ();
79
+ }
67
80
break ;
68
- case 1 :
81
+ case 4 :
69
82
// Read several bytes into target
70
83
int len = randomIntBetween (1 , length - readPos );
71
84
indexInput .readBytes (output , readPos , len );
72
85
readPos += len ;
73
86
break ;
74
- case 2 :
87
+ case 5 :
75
88
// Read several bytes into 0-offset target
76
89
len = randomIntBetween (1 , length - readPos );
77
90
byte [] temp = new byte [len ];
78
91
indexInput .readBytes (temp , 0 , len );
79
92
System .arraycopy (temp , 0 , output , readPos , len );
80
93
readPos += len ;
81
94
break ;
82
- case 3 :
95
+ case 6 :
83
96
// Read using slice
84
97
len = randomIntBetween (1 , length - readPos );
85
98
final String sliceExtension = randomValueOtherThan (".cfs" , ESIndexInputTestCase ::randomFileExtension );
@@ -92,7 +105,7 @@ protected byte[] randomReadAndSlice(IndexInput indexInput, int length) throws IO
92
105
indexInput .seek (readPos );
93
106
assertEquals (readPos , indexInput .getFilePointer ());
94
107
break ;
95
- case 4 :
108
+ case 7 :
96
109
// Seek at a random position and read a single byte,
97
110
// then seek back to original position
98
111
final int lastReadPos = readPos ;
@@ -106,7 +119,7 @@ protected byte[] randomReadAndSlice(IndexInput indexInput, int length) throws IO
106
119
indexInput .seek (readPos );
107
120
assertEquals (readPos , indexInput .getFilePointer ());
108
121
break ;
109
- case 5 :
122
+ case 8 :
110
123
// Read clone or slice concurrently
111
124
final int cloneCount = between (1 , 3 );
112
125
final CountDownLatch startLatch = new CountDownLatch (1 + cloneCount );
0 commit comments