Lines 169-209
public abstract class IndexReader
Link Here
|
169 |
protected ArrayIntCompressed getPage(int page) |
169 |
protected ArrayIntCompressed getPage(int page) |
170 |
{ |
170 |
{ |
171 |
SoftReference<ArrayIntCompressed> ref = pages.get(page); |
171 |
SoftReference<ArrayIntCompressed> ref = pages.get(page); |
172 |
ArrayIntCompressed array = ref == null ? null : ref.get(); |
172 |
if (ref != null && ref.get() != null) |
173 |
if (array == null) |
|
|
174 |
{ |
173 |
{ |
175 |
synchronized (LOCK) |
174 |
return ref.get(); |
176 |
{ |
175 |
} |
177 |
ref = pages.get(page); |
|
|
178 |
array = ref == null ? null : ref.get(); |
179 |
|
180 |
if (array == null) |
181 |
{ |
182 |
try |
183 |
{ |
184 |
byte[] buffer = null; |
185 |
|
186 |
this.in.seek(pageStart[page]); |
187 |
|
188 |
buffer = new byte[(int) (pageStart[page + 1] - pageStart[page])]; |
189 |
if (this.in.read(buffer) != buffer.length) |
190 |
throw new IOException(); |
191 |
|
176 |
|
192 |
array = new ArrayIntCompressed(buffer); |
177 |
// no read is ready with a complete read; do a read |
|
|
178 |
long ltoRead = pageStart[page + 1] - pageStart[page]; |
179 |
if (ltoRead >= Integer.MAX_VALUE) |
180 |
{ |
181 |
throw new RuntimeException(new IOException("want to read too many bytes")); |
182 |
} |
183 |
int toRead = (int) ltoRead; |
184 |
byte[] buffer; |
185 |
try |
186 |
{ |
187 |
buffer = this.in.readDirect(pageStart[page], toRead); |
188 |
} |
189 |
catch (IOException e) |
190 |
{ |
191 |
throw new RuntimeException(e); |
192 |
} |
193 |
|
193 |
|
194 |
synchronized (pages) |
194 |
synchronized(pages) |
195 |
{ |
195 |
{ |
196 |
pages.put(page, new SoftReference<ArrayIntCompressed>(array)); |
196 |
// if another thread finished a concurrent read, use it |
197 |
} |
197 |
ref = pages.get(page); |
198 |
} |
198 |
if (ref != null && ref.get() != null) |
199 |
catch (IOException e) |
199 |
{ |
200 |
{ |
200 |
return ref.get(); |
201 |
throw new RuntimeException(e); |
|
|
202 |
} |
203 |
} |
204 |
} |
201 |
} |
|
|
202 |
|
203 |
// if this thread is first, it can store |
204 |
ArrayIntCompressed array = new ArrayIntCompressed(buffer); |
205 |
pages.put(page, new SoftReference<>(array)); |
206 |
return array; |
205 |
} |
207 |
} |
206 |
return array; |
|
|
207 |
} |
208 |
} |
208 |
|
209 |
|
209 |
public void delete() |
210 |
public void delete() |