IndexSanityChecker { public boolean check() throws IOException { boolean allFine = true; if (isThereASizeMismatch()){ deleteAllFiles(local); allFine = false; } else { for (String fileName : local.listAll()) { if (!remote.fileExists(fileName)) { local.deleteFile(fileName); } } } if (allFine) { log.info("Local index directory content found to be valid for index [{}]. " + "Stats Local: {} files ({}), Remote: {} files ({})", indexPath, localFileCount, IOUtils.humanReadableByteCount(localDirSize), remoteFileCount, IOUtils.humanReadableByteCount(remoteDirSize)); } else { log.warn("Local index directory content were not found to be in sync with remote for index [{}]. " + "Local directory content has been purged and would be synced again from remote", indexPath); } return allFine; } IndexSanityChecker(String indexPath, Directory local, Directory remote); boolean check();  }
@Test public void validDirs() throws Exception{ byte[] t1 = writeFile(local, "t1", 100); writeFile(remote, "t1", t1); assertTrue(new IndexSanityChecker("/foo", local, remote).check()); assertTrue(local.fileExists("t1")); assertTrue(remote.fileExists("t1")); }
@Test public void sizeMismatch() throws Exception{ byte[] t1L = writeFile(local, "t1", 100); writeFile(remote, "t1", randomBytes(t1L.length + rnd.nextInt(10) + 1)); byte[] t2R = writeFile(remote, "t2", 120); byte[] t3R = writeFile(remote, "t3", 140); writeFile(local, "t3", t3R); assertFalse(new IndexSanityChecker("/foo", local, remote).check()); assertTrue(remote.fileExists("t3")); assertFalse(local.fileExists("t1")); assertFalse(local.fileExists("t3")); }
@Test public void extraLocalFiles() throws Exception{ byte[] t1L = writeFile(local, "t1", 100); byte[] t3R = writeFile(remote, "t3", 140); writeFile(local, "t3", t3R); new IndexSanityChecker("/foo", local, remote).check(); assertFalse(local.fileExists("t1")); assertTrue(remote.fileExists("t3")); }