static Map<String, String> readChecksums(Directory[] dirs, Map<String, String> defaultValue) throws IOException { long lastFound = -1; Directory lastDir = null; for (Directory dir : dirs) { for (String name : dir.listAll()) { if (!isChecksum(name)) { continue; } long current = Long.parseLong(name.substring(CHECKSUMS_PREFIX.length())); if (current > lastFound) { lastFound = current; lastDir = dir; } } } if (lastFound == -1) { return defaultValue; } IndexInput indexInput = lastDir.openInput(CHECKSUMS_PREFIX + lastFound, IOContext.READONCE); try { indexInput.readInt(); // version return indexInput.readStringStringMap(); } catch (Exception e) { // failed to load checksums, ignore and return an empty map return defaultValue; } finally { indexInput.close(); } }
// LUCENE-1196 public void testIllegalEOF() throws Exception { RAMDirectory dir = new RAMDirectory(); IndexOutput o = dir.createOutput("out"); byte[] b = new byte[1024]; o.writeBytes(b, 0, 1024); o.close(); IndexInput i = dir.openInput("out"); i.seek(1024); i.close(); dir.close(); }
private void verifyData(ByteBufferDirectory dir) throws IOException { byte[] test = new byte[] {1, 2, 3, 4, 5, 6, 7, 8}; assertThat(dir.fileExists("value1"), equalTo(true)); assertThat(dir.fileLength("value1"), equalTo(38l)); IndexInput indexInput = dir.openInput("value1", IOContext.DEFAULT); indexInput.readBytes(test, 0, 5); assertThat(test[0], equalTo((byte) 8)); assertThat(indexInput.readInt(), equalTo(-1)); assertThat(indexInput.readLong(), equalTo((long) 10)); assertThat(indexInput.readInt(), equalTo(0)); assertThat(indexInput.readInt(), equalTo(0)); indexInput.readBytes(test, 0, 8); assertThat(test[0], equalTo((byte) 1)); assertThat(test[7], equalTo((byte) 8)); indexInput.readBytes(test, 0, 5); assertThat(test[0], equalTo((byte) 1)); assertThat(test[4], equalTo((byte) 5)); indexInput.seek(28); assertThat(indexInput.readByte(), equalTo((byte) 4)); indexInput.seek(30); assertThat(indexInput.readByte(), equalTo((byte) 6)); indexInput.seek(0); indexInput.readBytes(test, 0, 5); assertThat(test[0], equalTo((byte) 8)); indexInput.close(); indexInput = dir.openInput("value1", IOContext.DEFAULT); // iterate over all the data for (int i = 0; i < 38; i++) { indexInput.readByte(); } indexInput.close(); }
// LUCENE-2852 public void testSeekToEOFThenBack() throws Exception { RAMDirectory dir = new RAMDirectory(); IndexOutput o = dir.createOutput("out"); byte[] bytes = new byte[3 * RAMInputStream.BUFFER_SIZE]; o.writeBytes(bytes, 0, bytes.length); o.close(); IndexInput i = dir.openInput("out"); i.seek(2 * RAMInputStream.BUFFER_SIZE - 1); i.seek(3 * RAMInputStream.BUFFER_SIZE); i.seek(RAMInputStream.BUFFER_SIZE); i.readBytes(bytes, 0, 2 * RAMInputStream.BUFFER_SIZE); i.close(); dir.close(); }
@Override public void close() throws IOException { main.close(); }