TrailFilePositionSetter { public synchronized FilePositionResult locateFilePosition(long scn, TransactionSCNFinderCallback callback) throws IOException { TrailFileNotifier notifier = new TrailFileNotifier(_dir, _filter, null, 0, null); List<File> orderedTrailFiles = notifier.getCandidateTrailFiles(); _log.info("Initial set of Trail Files :" + orderedTrailFiles); if ( (null == orderedTrailFiles) || orderedTrailFiles.isEmpty()) { return FilePositionResult.createNoTxnsFoundResult(); } FilePositionResult res = null; if ( scn == USE_EARLIEST_SCN) { res = getFilePosition(scn, callback); } else { for (int i = orderedTrailFiles.size() - 1; i >= 0; i--) { callback.reset(); File startFile = orderedTrailFiles.get(i); _log.info("Locating the SCN (" + scn + ") starting from the trail file :" + startFile); res = getFilePosition(scn, callback,startFile.getName()); _log.info("Result of the location operation for SCN (" + scn + ") starting from trail file (" + startFile + ") is : " + res); if (((res.getStatus() == Status.EXACT_SCN_NOT_FOUND) || (res.getStatus() == Status.FOUND)) && (res.getTxnPos().getTxnRank() > 0)) { break; } if ((0 == i) && (res != null)) { ScnTxnPos scnTxnPos = res.getTxnPos(); if ( (scnTxnPos != null) && (scnTxnPos.getTxnRank() <= 0)) { return FilePositionResult.createErrorResult(new DatabusException("A transaction with scn less than requested SCN was not found. " + "Without this txn, we cannot identify if all transactions for requested SCN " + "have been located. Requested SinceSCN is :" + scn)); } } } } return res; } TrailFilePositionSetter(String dir, String filePrefix);  TrailFilePositionSetter(String dir, String filePrefix, String phySourceName); synchronized FilePositionResult locateFilePosition(long scn, TransactionSCNFinderCallback callback); public Logger _log; static final long USE_LATEST_SCN; static final long USE_EARLIEST_SCN; static final String NEW_LINE_PATTERN_STR; static final String DOUBLE_CHAR_NEWLINE; static final int NO_NEWLINE_LEN; }
@Test public void testDeletedDirectory() throws Exception { final Logger log = Logger.getLogger("TestTrailFilePositionSetter.testDeletedDirectory"); log.info("starting"); File dir = createTempDir(); TrailFilePositionSetter posSetter = new TrailFilePositionSetter(dir.getAbsolutePath(), TRAIL_FILENAME_PREFIX); GGXMLTrailTransactionFinder finder = new GGXMLTrailTransactionFinder(); boolean deleted = dir.delete(); Assert.assertTrue(deleted, "Deleted the trail directory successfully"); FilePositionResult res = posSetter.locateFilePosition(100,finder); Assert.assertEquals(res.getStatus(), Status.NO_TXNS_FOUND, "File Position Result Status"); log.info(DONE_STRING); }
@Test public void testFailureMode_MalformedFirstTransactionInFirstFile() throws Exception { final Logger log = Logger.getLogger("TestTrailFilePositionSetter.testFailureMode_MalformedFirstTransactionInFirstFile"); log.info("starting"); File dir = createTempDir(); createTrailFiles(dir.getAbsolutePath(), TRAIL_FILENAME_PREFIX, 150 , 1250 , 1 , "\n", 0, 100 , "xyzzy", false, ""); TrailFilePositionSetter posSetter = new TrailFilePositionSetter(dir.getAbsolutePath(), TRAIL_FILENAME_PREFIX); GGXMLTrailTransactionFinder finder = new GGXMLTrailTransactionFinder(); FilePositionResult res; res = posSetter.locateFilePosition(100, finder); Assert.assertEquals(res.getStatus(), FilePositionResult.Status.ERROR, "expected error for exact-match SCN that's corrupted and oldest in all trail files."); finder.reset(); res = posSetter.locateFilePosition(TrailFilePositionSetter.USE_EARLIEST_SCN, finder); assertFilePositionResult(res, dir, 101, FilePositionResult.Status.FOUND); log.info(DONE_STRING); }
@Test public void testFailureMode_MalformedLastTransactionInMiddleFile() throws Exception { final Logger log = Logger.getLogger("TestTrailFilePositionSetter.testFailureMode_MalformedLastTransactionInMiddleFile"); log.info("starting"); File dir = createTempDir(); createTrailFiles(dir.getAbsolutePath(), TRAIL_FILENAME_PREFIX, 150 , 1250 , 1 , "\n", 0, 307 , "plugh", false, ""); TrailFilePositionSetter posSetter = new TrailFilePositionSetter(dir.getAbsolutePath(), TRAIL_FILENAME_PREFIX); GGXMLTrailTransactionFinder finder = new GGXMLTrailTransactionFinder(); FilePositionResult res; res = posSetter.locateFilePosition(299, finder); assertFilePositionResult(res, dir, 299, FilePositionResult.Status.FOUND); finder.reset(); res = posSetter.locateFilePosition(306, finder); assertFilePositionResult(res, dir, 306, FilePositionResult.Status.FOUND); log.info(DONE_STRING); }
@Test public void testFailureMode_MalformedLastTransactionInLastFile() throws Exception { final Logger log = Logger.getLogger("TestTrailFilePositionSetter.testFailureMode_MalformedLastTransactionInLastFile"); log.info("starting"); File dir = createTempDir(); createTrailFiles(dir.getAbsolutePath(), TRAIL_FILENAME_PREFIX, 150 , 1250 , 1 , "\n", 0, 399 , "quux", false, ""); TrailFilePositionSetter posSetter = new TrailFilePositionSetter(dir.getAbsolutePath(), TRAIL_FILENAME_PREFIX); GGXMLTrailTransactionFinder finder = new GGXMLTrailTransactionFinder(); FilePositionResult res; res = posSetter.locateFilePosition(TrailFilePositionSetter.USE_LATEST_SCN, finder); assertFilePositionResult(res, dir, 398, FilePositionResult.Status.FOUND); log.info(DONE_STRING); }
@Test public void testFailureMode_MalformedTransactionForExactScn() throws Exception { final Logger log = Logger.getLogger("TestTrailFilePositionSetter.testFailureMode_MalformedTransactionForExactScn"); log.info("starting"); File dir = createTempDir(); createTrailFiles(dir.getAbsolutePath(), TRAIL_FILENAME_PREFIX, 150 , 1250 , 1 , "\n", 0, 377 , "", true, "kilroy"); TrailFilePositionSetter posSetter = new TrailFilePositionSetter(dir.getAbsolutePath(), TRAIL_FILENAME_PREFIX); GGXMLTrailTransactionFinder finder = new GGXMLTrailTransactionFinder(); FilePositionResult res; finder.reset(); res = posSetter.locateFilePosition(377, finder); assertFilePositionResult(res, dir, 377, FilePositionResult.Status.FOUND); log.info(DONE_STRING); }
@Test public void testFailureMode_MalformedTransactionNoValidScnsInMiddleOfMiddleFile() throws Exception { final Logger log = Logger.getLogger("TestTrailFilePositionSetter.testFailureMode_MalformedTransactionNoValidScns"); log.info("starting"); File dir = createTempDir(); HashSet<Long> corruptedScns = new HashSet<Long>(10); corruptedScns.add(new Long(100)); corruptedScns.add(new Long(101)); corruptedScns.add(new Long(204)); corruptedScns.add(new Long(205)); corruptedScns.add(new Long(206)); corruptedScns.add(new Long(207)); corruptedScns.add(new Long(250)); corruptedScns.add(new Long(251)); corruptedScns.add(new Long(310)); corruptedScns.add(new Long(311)); corruptedScns.add(new Long(398)); corruptedScns.add(new Long(399)); createTrailFiles(dir.getAbsolutePath(), TRAIL_FILENAME_PREFIX, 150 , 1250 , 1 , "\n", 0, corruptedScns, "blargh", false, ""); TrailFilePositionSetter posSetter = new TrailFilePositionSetter(dir.getAbsolutePath(), TRAIL_FILENAME_PREFIX); GGXMLTrailTransactionFinder finder = new GGXMLTrailTransactionFinder(); FilePositionResult res; res = posSetter.locateFilePosition(101, finder); Assert.assertEquals(res.getStatus(), FilePositionResult.Status.ERROR, "expected error for exact-match SCN that's 'too old'."); finder.reset(); res = posSetter.locateFilePosition(102, finder); Assert.assertEquals(res.getStatus(), FilePositionResult.Status.ERROR, "expected error for exact-match SCN that's 'too old'."); finder.reset(); res = posSetter.locateFilePosition(TrailFilePositionSetter.USE_EARLIEST_SCN, finder); assertFilePositionResult(res, dir, 102, FilePositionResult.Status.FOUND); finder.reset(); res = posSetter.locateFilePosition(107, finder); assertFilePositionResult(res, dir, 107, FilePositionResult.Status.FOUND); finder.reset(); res = posSetter.locateFilePosition(203, finder); assertFilePositionResult(res, dir, 203, FilePositionResult.Status.FOUND); finder.reset(); res = posSetter.locateFilePosition(204, finder); assertFilePositionResult(res, dir, 209, FilePositionResult.Status.EXACT_SCN_NOT_FOUND); finder.reset(); res = posSetter.locateFilePosition(250, finder); assertFilePositionResult(res, dir, 253, FilePositionResult.Status.EXACT_SCN_NOT_FOUND); finder.reset(); res = posSetter.locateFilePosition(251, finder); assertFilePositionResult(res, dir, 253, FilePositionResult.Status.EXACT_SCN_NOT_FOUND); finder.reset(); res = posSetter.locateFilePosition(252, finder); assertFilePositionResult(res, dir, 252, FilePositionResult.Status.EXACT_SCN_NOT_FOUND); finder.reset(); res = posSetter.locateFilePosition(253, finder); assertFilePositionResult(res, dir, 253, FilePositionResult.Status.FOUND); finder.reset(); res = posSetter.locateFilePosition(309, finder); assertFilePositionResult(res, dir, 309, FilePositionResult.Status.FOUND); finder.reset(); res = posSetter.locateFilePosition(310, finder); assertFilePositionResult(res, dir, 313, FilePositionResult.Status.EXACT_SCN_NOT_FOUND); finder.reset(); res = posSetter.locateFilePosition(311, finder); assertFilePositionResult(res, dir, 313, FilePositionResult.Status.EXACT_SCN_NOT_FOUND); finder.reset(); res = posSetter.locateFilePosition(398, finder); assertFilePositionResult(res, dir, 397, FilePositionResult.Status.EXACT_SCN_NOT_FOUND); finder.reset(); res = posSetter.locateFilePosition(405, finder); assertFilePositionResult(res, dir, 397, FilePositionResult.Status.EXACT_SCN_NOT_FOUND); finder.reset(); res = posSetter.locateFilePosition(TrailFilePositionSetter.USE_LATEST_SCN, finder); assertFilePositionResult(res, dir, 397, FilePositionResult.Status.FOUND); log.info(DONE_STRING); }
@Test public void testRequestedScnOlderThanOldestTransaction() throws Exception { final Logger log = Logger.getLogger("TestTrailFilePositionSetter.testRequestedScnOlderThanOldestTransaction"); log.info("starting"); File dir = createTempDir(); createTrailFiles(dir.getAbsolutePath(), TRAIL_FILENAME_PREFIX, 150 , 1250 , 1 , "\n", 0, -1 , "", false, ""); TrailFilePositionSetter posSetter = new TrailFilePositionSetter(dir.getAbsolutePath(), TRAIL_FILENAME_PREFIX); GGXMLTrailTransactionFinder finder = new GGXMLTrailTransactionFinder(); FilePositionResult res; res = posSetter.locateFilePosition(99, finder); Assert.assertEquals(res.getStatus(), FilePositionResult.Status.ERROR, "expected error for exact-match SCN that's too old."); finder.reset(); res = posSetter.locateFilePosition(120, finder); assertFilePositionResult(res, dir, 120, FilePositionResult.Status.EXACT_SCN_NOT_FOUND); finder.reset(); res = posSetter.locateFilePosition(100, finder); Assert.assertEquals(res.getStatus(), FilePositionResult.Status.ERROR, "expected error for exact-match SCN that's too old."); finder.reset(); res = posSetter.locateFilePosition(TrailFilePositionSetter.USE_EARLIEST_SCN, finder); assertFilePositionResult(res, dir, 100, FilePositionResult.Status.FOUND); finder.reset(); res = posSetter.locateFilePosition(101, finder); Assert.assertEquals(res.getStatus(), FilePositionResult.Status.ERROR, "expected error for exact-match SCN that's too old."); log.info(DONE_STRING); }
@Test public void testRepeatSCN() throws Exception { final Logger log = Logger.getLogger("TestTrailFilePositionSetter.testRepeatSCN"); File dir = createTempDir(); long startScn = 100; int numTxnsPerFile = 10; createTrailFiles(dir.getAbsolutePath(), TRAIL_FILENAME_PREFIX, 100L, _txnPattern.length * numTxnsPerFile, 1L, "\n", 0, _txnPattern, 2 * numTxnsPerFile * 2, startScn); log.info("Directory is: " + dir); TrailFilePositionSetter posSetter = null; GGXMLTrailTransactionFinder finder = new GGXMLTrailTransactionFinder(); posSetter = new TrailFilePositionSetter(dir.getAbsolutePath(), TRAIL_FILENAME_PREFIX); FilePositionResult res = posSetter.locateFilePosition(100, finder); Assert.assertEquals(res.getStatus(), FilePositionResult.Status.ERROR, "Result Status for SCN: " + 100 + ", Result: " + res); res = posSetter.locateFilePosition(101, finder); Assert.assertEquals(res.getStatus(), FilePositionResult.Status.FOUND, "Result Status"); Assert.assertEquals(res.getTxnPos().getFile(), "x302", "File found"); Assert.assertEquals(res.getTxnPos().getFileOffset(), 0, "File offset found"); Assert.assertEquals(res.getTxnPos().getLineNumber(), 1, "File line number found"); Assert.assertEquals(res.getTxnPos().getMinScn(), 101, "MinScn check"); Assert.assertEquals(res.getTxnPos().getMaxScn(), 101, "MinScn check"); Assert.assertEquals(res.getTxnPos().getTxnRank(), 10, "Txn Rank"); res = posSetter.locateFilePosition(102, finder); Assert.assertEquals(res.getStatus(), FilePositionResult.Status.FOUND, "Result Status"); Assert.assertEquals(res.getTxnPos().getFile(), "x304", "File found"); Assert.assertEquals(res.getTxnPos().getFileOffset(), 0, "File offset found"); Assert.assertEquals(res.getTxnPos().getLineNumber(), 1, "File line number found"); Assert.assertEquals(res.getTxnPos().getMinScn(), 102, "MinScn check"); Assert.assertEquals(res.getTxnPos().getMaxScn(), 102, "MinScn check"); Assert.assertEquals(res.getTxnPos().getTxnRank(), 10, "Txn Rank"); res = posSetter.locateFilePosition(103, finder); Assert.assertEquals(res.getStatus(), FilePositionResult.Status.FOUND, "Result Status"); Assert.assertEquals(res.getTxnPos().getFile(), "x306", "File found"); Assert.assertEquals(res.getTxnPos().getFileOffset(), 0, "File offset found"); Assert.assertEquals(res.getTxnPos().getLineNumber(), 1, "File line number found"); Assert.assertEquals(res.getTxnPos().getMinScn(), 103, "MinScn check"); Assert.assertEquals(res.getTxnPos().getMaxScn(), 103, "MinScn check"); Assert.assertEquals(res.getTxnPos().getTxnRank(), 10, "Txn Rank"); res = posSetter.locateFilePosition(104, finder); Assert.assertEquals(res.getStatus(), FilePositionResult.Status.FOUND, "Result Status"); Assert.assertEquals(res.getTxnPos().getFile(), "x308", "File found"); Assert.assertEquals(res.getTxnPos().getFileOffset(), 0, "File offset found"); Assert.assertEquals(res.getTxnPos().getLineNumber(), 1, "File line number found"); Assert.assertEquals(res.getTxnPos().getMinScn(), 104, "MinScn check"); Assert.assertEquals(res.getTxnPos().getMaxScn(), 104, "MinScn check"); Assert.assertEquals(res.getTxnPos().getTxnRank(), 10, "Txn Rank"); }