blob: 7f74032ac4dd2dcf486aaecba2944ede5d97bcad [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.s2graph.core
import java.util.regex.Pattern
import org.apache.tinkerpop.gremlin.structure.Direction
import play.api.libs.json.Json
import scala.util.hashing.MurmurHash3
object GraphUtil {
private val TOKEN_DELIMITER = Pattern.compile("[\t]")
val operations = Map("i" -> 0, "insert" -> 0, "u" -> 1, "update" -> 1,
"increment" -> 2, "d" -> 3, "delete" -> 3,
"deleteAll" -> 4, "insertBulk" -> 5, "incrementCount" -> 6).map {
case (k, v) =>
k -> v.toByte
}
val BitsForMurMurHash = 16
val bytesForMurMurHash = 2
val defaultOpByte = operations("insert")
val directions = Map("out" -> 0, "in" -> 1, "undirected" -> 2, "u" -> 2, "directed" -> 0, "d" -> 0)
val consistencyLevel = Map("weak" -> 0, "strong" -> 1)
def toType(t: String) = {
t.trim().toLowerCase match {
case "e" | "edge" => "edge"
case "v" | "vertex" => "vertex"
}
}
def toDir(direction: String): Option[Byte] = {
val d = direction.trim().toLowerCase match {
case "directed" | "d" => Some(0)
case "undirected" | "u" => Some(2)
case "out" | "o" => Some(0)
case "in" | "i" => Some(1)
case _ => None
}
d.map(x => x.toByte)
}
def toDirection(direction: String): Int = {
direction.trim().toLowerCase match {
case "directed" | "d" => 0
case "undirected" | "u" => 2
case "out" | "o" => 0
case "in" | "i" => 1
case _ => 2
}
}
def toDirection(direction: Direction): Int = {
direction.name() match {
case "out" => 0
case "in" => 1
case "both" => throw new IllegalArgumentException("only in/out direction is supported.")
}
}
def fromDirection(direction: Int): String = {
direction match {
case 0 => "out"
case 1 => "in"
case 2 => "undirected"
}
}
def toggleDir(dir: Int) = {
dir match {
case 0 => 1
case 1 => 0
case 2 => 2
case _ => throw new UnsupportedOperationException(s"toggleDirection: $dir")
}
}
def toOp(op: String): Option[Byte] = {
op.trim() match {
case "i" | "insert" => Some(0)
case "d" | "delete" => Some(3)
case "u" | "update" => Some(1)
case "increment" => Some(2)
case "deleteAll" => Some(4)
case "insertBulk" => Some(5)
case "incrementCount" => Option(6)
case _ => None
}
}
def fromOp(op: Byte): String = {
op match {
case 0 => "insert"
case 3 => "delete"
case 1 => "update"
case 2 => "increment"
case 4 => "deleteAll"
case 5 => "insertBulk"
case 6 => "incrementCount"
case _ =>
throw new UnsupportedOperationException(s"op : $op (only support 0(insert),1(delete),2(updaet),3(increment))")
}
}
// def toggleOp(op: Byte): Byte = {
// val ret = op match {
// case 0 => 1
// case 1 => 0
// case x: Byte => x
// }
// ret.toByte
// }
// 2^31 - 1
def transformHash(h: Int): Int = {
// h / 2 + (Int.MaxValue / 2 - 1)
if (h < 0) -1 * (h + 1) else h
}
def murmur3Int(s: String): Int = {
val hash = MurmurHash3.stringHash(s)
transformHash(hash)
}
def murmur3(s: String): Short = {
val hash = MurmurHash3.stringHash(s)
val positiveHash = transformHash(hash) >> BitsForMurMurHash
positiveHash.toShort
// Random.nextInt(Short.MaxValue).toShort
}
def smartSplit(s: String, delemiter: String) = {
val trimed_string = s.trim()
if (trimed_string.equals("")) {
Seq[String]()
} else {
trimed_string.split(delemiter).toList
}
}
def split(line: String) = TOKEN_DELIMITER.split(line)
def parseString(s: String): List[String] = {
if (!s.startsWith("[")) {
s.split("\n").toList
} else {
Json.parse(s).asOpt[List[String]].getOrElse(List.empty[String])
}
}
def stringToOption(s: Option[String]): Option[String] = {
s.filter(_.trim.nonEmpty)
}
def stringToOption(s: String): Option[String] = {
Option(s).filter(_.trim.nonEmpty)
}
def toLabelMapping(lableMapping: String): Map[String, String] = {
(for {
token <- lableMapping.split(",")
inner = token.split(":") if inner.length == 2
} yield {
(inner.head, inner.last)
}).toMap
}
}