2010-03-29 19:17:55 -04:00
|
|
|
package spark
|
|
|
|
|
|
|
|
import java.io._
|
2010-11-09 16:46:30 -05:00
|
|
|
import java.net.InetAddress
|
2010-10-16 19:14:13 -04:00
|
|
|
import java.util.UUID
|
2010-03-29 19:17:55 -04:00
|
|
|
|
2010-10-04 15:01:05 -04:00
|
|
|
import scala.collection.mutable.ArrayBuffer
|
2010-11-04 01:45:44 -04:00
|
|
|
import scala.util.Random
|
2010-10-04 15:01:05 -04:00
|
|
|
|
2010-10-16 19:14:13 -04:00
|
|
|
/**
|
|
|
|
* Various utility methods used by Spark.
|
|
|
|
*/
|
2010-10-04 15:01:05 -04:00
|
|
|
object Utils {
|
2010-03-29 19:17:55 -04:00
|
|
|
def serialize[T](o: T): Array[Byte] = {
|
2010-11-04 01:45:44 -04:00
|
|
|
val bos = new ByteArrayOutputStream()
|
2010-03-29 19:17:55 -04:00
|
|
|
val oos = new ObjectOutputStream(bos)
|
|
|
|
oos.writeObject(o)
|
|
|
|
oos.close
|
|
|
|
return bos.toByteArray
|
|
|
|
}
|
|
|
|
|
|
|
|
def deserialize[T](bytes: Array[Byte]): T = {
|
|
|
|
val bis = new ByteArrayInputStream(bytes)
|
|
|
|
val ois = new ObjectInputStream(bis)
|
|
|
|
return ois.readObject.asInstanceOf[T]
|
|
|
|
}
|
|
|
|
|
|
|
|
def deserialize[T](bytes: Array[Byte], loader: ClassLoader): T = {
|
|
|
|
val bis = new ByteArrayInputStream(bytes)
|
|
|
|
val ois = new ObjectInputStream(bis) {
|
|
|
|
override def resolveClass(desc: ObjectStreamClass) =
|
|
|
|
Class.forName(desc.getName, false, loader)
|
|
|
|
}
|
|
|
|
return ois.readObject.asInstanceOf[T]
|
|
|
|
}
|
2010-10-04 15:01:05 -04:00
|
|
|
|
|
|
|
def isAlpha(c: Char) = {
|
|
|
|
(c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')
|
|
|
|
}
|
|
|
|
|
|
|
|
def splitWords(s: String): Seq[String] = {
|
|
|
|
val buf = new ArrayBuffer[String]
|
|
|
|
var i = 0
|
|
|
|
while (i < s.length) {
|
|
|
|
var j = i
|
|
|
|
while (j < s.length && isAlpha(s.charAt(j))) {
|
|
|
|
j += 1
|
|
|
|
}
|
|
|
|
if (j > i) {
|
|
|
|
buf += s.substring(i, j);
|
|
|
|
}
|
|
|
|
i = j
|
|
|
|
while (i < s.length && !isAlpha(s.charAt(i))) {
|
|
|
|
i += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf
|
|
|
|
}
|
2010-10-16 19:14:13 -04:00
|
|
|
|
|
|
|
// Create a temporary directory inside the given parent directory
|
|
|
|
def createTempDir(root: String = System.getProperty("java.io.tmpdir")): File =
|
|
|
|
{
|
|
|
|
var attempts = 0
|
|
|
|
val maxAttempts = 10
|
|
|
|
var dir: File = null
|
|
|
|
while (dir == null) {
|
|
|
|
attempts += 1
|
|
|
|
if (attempts > maxAttempts) {
|
|
|
|
throw new IOException("Failed to create a temp directory " +
|
|
|
|
"after " + maxAttempts + " attempts!")
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
dir = new File(root, "spark-" + UUID.randomUUID.toString)
|
|
|
|
if (dir.exists() || !dir.mkdirs()) {
|
|
|
|
dir = null
|
|
|
|
}
|
|
|
|
} catch { case e: IOException => ; }
|
|
|
|
}
|
|
|
|
return dir
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy all data from an InputStream to an OutputStream
|
|
|
|
def copyStream(in: InputStream,
|
|
|
|
out: OutputStream,
|
|
|
|
closeStreams: Boolean = false)
|
|
|
|
{
|
|
|
|
val buf = new Array[Byte](8192)
|
|
|
|
var n = 0
|
|
|
|
while (n != -1) {
|
|
|
|
n = in.read(buf)
|
|
|
|
if (n != -1) {
|
|
|
|
out.write(buf, 0, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (closeStreams) {
|
|
|
|
in.close()
|
|
|
|
out.close()
|
|
|
|
}
|
|
|
|
}
|
2010-11-04 01:45:44 -04:00
|
|
|
|
|
|
|
// Shuffle the elements of a collection into a random order, returning the
|
|
|
|
// result in a new collection. Unlike scala.util.Random.shuffle, this method
|
|
|
|
// uses a local random number generator, avoiding inter-thread contention.
|
2010-11-08 03:45:02 -05:00
|
|
|
def shuffle[T](seq: TraversableOnce[T]): Seq[T] = {
|
|
|
|
val buf = new ArrayBuffer[T]()
|
|
|
|
buf ++= seq
|
2010-11-04 01:45:44 -04:00
|
|
|
val rand = new Random()
|
|
|
|
for (i <- (buf.size - 1) to 1 by -1) {
|
|
|
|
val j = rand.nextInt(i)
|
|
|
|
val tmp = buf(j)
|
|
|
|
buf(j) = buf(i)
|
|
|
|
buf(i) = tmp
|
|
|
|
}
|
|
|
|
buf
|
|
|
|
}
|
2010-11-09 16:46:30 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the local host's IP address in dotted-quad format (e.g. 1.2.3.4)
|
|
|
|
*/
|
|
|
|
def localIpAddress(): String = {
|
|
|
|
// Get local IP as an array of four bytes
|
|
|
|
val bytes = InetAddress.getLocalHost().getAddress()
|
|
|
|
// Convert the bytes to ints (keeping in mind that they may be negative)
|
|
|
|
// and join them into a string
|
|
|
|
return bytes.map(b => (b.toInt + 256) % 256).mkString(".")
|
|
|
|
}
|
2010-03-29 19:17:55 -04:00
|
|
|
}
|