Skip to content

Instantly share code, notes, and snippets.

@coderunner
Created January 31, 2013 19:35
Show Gist options
  • Select an option

  • Save coderunner/4685703 to your computer and use it in GitHub Desktop.

Select an option

Save coderunner/4685703 to your computer and use it in GitHub Desktop.

Revisions

  1. Felix Trepanier created this gist Jan 31, 2013.
    48 changes: 48 additions & 0 deletions patterns.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    // ** Ensure previous timestamp match last transaction event timestamp

    // before
    if (tx.previous != lastTimestamp) {
    throw new IOException("This transaction %s previous timestamp %s is not %s".format(
    tx, tx.previous, lastTimestamp))

    }

    // after
    require(tx.previous == lastTimestamp, "This transaction %s previous timestamp %s is not %s".format(
    tx, tx.previous, lastTimestamp))

    // ** Close all open streams but the most recent one
    // before
    val toClose = fileStreams.tail
    fileStreams = List(fileStreams.head)
    toClose.foreach(_.close())

    // after
    fileStreams match {
    case fileStream :: toClose => {
    fileStreams = fileStream :: Nil
    toClose.foreach(_.close())
    }
    case Nil => // Nothing to close
    }

    // ** Return the list of log files that contains transaction after a certain timestamp
    //before
    if (logFiles.isEmpty) {
    logFiles
    } else {
    val sortedFiles = logFiles.sortWith((f1, f2) => getTimestampFromName(f1.getName) < getTimestampFromName(f2.getName))
    sortedFiles.indexWhere(f => getTimestampFromName(f.getName) > timestamp) match {
    case -1 => List(sortedFiles.last)
    case 0 => sortedFiles
    case i => sortedFiles.drop(i - 1)
    }

    //after
    val sortedFiles = logFiles.sortWith((f1, f2) => getTimestampFromName(f1.getName) < getTimestampFromName(f2.getName))
    sortedFiles.indexWhere(f => getTimestampFromName(f.getName) > timestamp) match {
    case -1 if sortedFiles.isEmpty => sortedFiles
    case -1 => List(sortedFiles.last)
    case 0 => sortedFiles
    case i => sortedFiles.drop(i - 1)
    }