Skip to content

Instantly share code, notes, and snippets.

@bkyrlach
Created January 6, 2012 20:19
Show Gist options
  • Save bkyrlach/1572234 to your computer and use it in GitHub Desktop.
Save bkyrlach/1572234 to your computer and use it in GitHub Desktop.
Faillerization...
package org.cincyfp.actor
import scala.actors.Actor
import scala.actors.Actor._
object GenFile {
def main(args:Array[String]):Unit = {
val r = new java.util.Random
val f = new java.io.File("test.data")
val fos = new java.io.FileOutputStream(f)
1 to 3201000 foreach { x =>
val data = ((1 to 30 map { y =>
r.nextInt(10).toString
} reduceLeft { _ + _ }) + "\n").getBytes
fos.write(data,0, data.length)
}
fos.flush
fos.close
}
}
object SeqFileSearch {
def main(args:Array[String]):Unit = {
val f = new java.io.File("test.data")
val fis = new java.io.FileInputStream(f)
val data = new Array[Byte](31)
var list = List[String]()
while(fis.available() > 0) {
fis.read(data,0,31)
list = new String(data) :: list
}
list filter { _.contains("1234") } foreach { print(_) }
fis.close
}
}
object ParFileSearch {
def main(args:Array[String]):Unit = {
val workers = 1 to 4 map { x => new FileSearchActor }
val f = new java.io.File("test.data")
val fis = new java.io.FileInputStream(f)
val data = new Array[Byte](31)
var list = List[String]()
while(fis.available() > 0) {
fis.read(data,0,31)
list = new String(data) :: list
}
fis.close
1 to 4 foreach { x =>
workers(x - 1) ! list.slice((x - 1) * (list.size / 4), x * (list.size / 4))
}
}
}
class FileSearchActor extends Actor {
def act {
loop {
react {
case l:List[String] => {
l filter { _.contains("1234") } foreach { print(_) }
}
case _ =>
}
}
}
start
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment