@@ -126,39 +126,45 @@ private static PagesRec generatePageSet(BlockFactory blockFactory) {
126
126
127
127
// Generates a list of Pages with one BytesRef block, each of different positions, filled with random bytes.
128
128
private static PagesRec generatePages (int minBytes , int maxBytes , BlockFactory blockFactory ) {
129
- BytesRefBlock .Builder builder = blockFactory .newBytesRefBlockBuilder (maxBytes );
130
-
131
- byte [] buffer = new byte [maxBytes ];
132
- List <Page > pages = new ArrayList <>();
133
-
134
- int producedBytes = 0 ;
135
- int producedRows = 0 ;
136
- int rowsPerPage = randomIntBetween (1 , 100 );
137
- int rows = 0 ;
138
- while (producedBytes < maxBytes ) {
139
- int rowBytes = Math .min (randomIntBetween (1 , maxBytes / minBytes ), maxBytes - producedBytes );
140
- byte [] rowValue = randomByteArrayOfLength (rowBytes );
141
-
142
- builder .appendBytesRef (new BytesRef (rowValue ));
143
- System .arraycopy (rowValue , 0 , buffer , producedBytes , rowBytes );
144
-
145
- producedBytes += rowBytes ;
146
- rows ++;
147
-
148
- if (rows > rowsPerPage ) {
129
+ BytesRefBlock .Builder builder = null ;
130
+ try {
131
+ builder = blockFactory .newBytesRefBlockBuilder (maxBytes );
132
+
133
+ byte [] buffer = new byte [maxBytes ];
134
+ List <Page > pages = new ArrayList <>();
135
+
136
+ int producedBytes = 0 ;
137
+ int producedRows = 0 ;
138
+ int rowsPerPage = randomIntBetween (1 , 100 );
139
+ int rows = 0 ;
140
+ while (producedBytes < maxBytes ) {
141
+ int rowBytes = Math .min (randomIntBetween (1 , maxBytes / minBytes ), maxBytes - producedBytes );
142
+ byte [] rowValue = randomByteArrayOfLength (rowBytes );
143
+
144
+ builder .appendBytesRef (new BytesRef (rowValue ));
145
+ System .arraycopy (rowValue , 0 , buffer , producedBytes , rowBytes );
146
+
147
+ producedBytes += rowBytes ;
148
+ rows ++;
149
+
150
+ if (rows > rowsPerPage ) {
151
+ producedRows += rows ;
152
+ rows = 0 ;
153
+ enqueueBlock (builder , pages );
154
+ Releasables .close (builder );
155
+ builder = blockFactory .newBytesRefBlockBuilder (maxBytes );
156
+ rowsPerPage = randomIntBetween (1 , 100 );
157
+ }
158
+ }
159
+ if (rows > 0 ) {
149
160
producedRows += rows ;
150
- rows = 0 ;
151
161
enqueueBlock (builder , pages );
152
- builder = blockFactory .newBytesRefBlockBuilder (maxBytes );
153
- rowsPerPage = randomIntBetween (1 , 100 );
154
162
}
155
- }
156
- if (rows > 0 ) {
157
- producedRows += rows ;
158
- enqueueBlock (builder , pages );
159
- }
160
163
161
- return new PagesRec (pages , buffer , producedBytes , producedRows );
164
+ return new PagesRec (pages , buffer , producedBytes , producedRows );
165
+ } finally {
166
+ Releasables .close (builder );
167
+ }
162
168
}
163
169
164
170
private BlockFactory blockFactory (long maxBytes ) {
@@ -172,6 +178,5 @@ private BlockFactory blockFactory(long maxBytes) {
172
178
private static void enqueueBlock (BytesRefBlock .Builder builder , List <Page > pages ) {
173
179
Block block = builder .build ();
174
180
pages .add (new Page (block ));
175
- Releasables .close (builder );
176
181
}
177
182
}
0 commit comments