-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
region-hash to region-name mapping is now stored in a own table and t…
…he region-name can now be as lengthy as 65536 characters #27
- Loading branch information
Nils Kübler
committed
May 20, 2014
1 parent
fa29526
commit a95cfee
Showing
4 changed files
with
250 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package models | ||
|
||
import utils.ByteUtil | ||
import java.security.MessageDigest | ||
import org.apache.hadoop.hbase.util.Bytes | ||
import scala.collection.mutable | ||
import anorm._ | ||
import play.api.db.DB | ||
import play.api.Logger | ||
import play.api.Play.current | ||
|
||
/** | ||
* Created by nkuebler on 20/05/14. | ||
*/ | ||
case class RegionHash(name:String, hash:String) { | ||
} | ||
|
||
object RegionHash { | ||
|
||
def byName(name:String) = { | ||
val hash:String = nameToHash.get(name).getOrElse { | ||
val hashedName = this.hash(name) | ||
DB.withConnection { implicit c => | ||
val stream = SQL_FIND_NAME.on("hash" -> hashedName)() | ||
if(stream.isEmpty) { // add record if not yet exists | ||
SQL_INSERT_NAME.on("hash" -> hashedName, "name" -> name).executeInsert() | ||
} | ||
} | ||
hashToName.put(hashedName, name) | ||
nameToHash.put(name, hashedName) | ||
hashedName | ||
} | ||
RegionHash(name, hash) | ||
} | ||
|
||
def byHash(hash:String) : RegionHash = { | ||
val name = hashToName.get(hash).getOrElse { | ||
DB.withConnection { implicit c => | ||
val stream = SQL_FIND_NAME.on("hash" -> hash)() | ||
if(stream.isEmpty) { | ||
Logger.info("no region found for hash: " +hash) | ||
"-unknown-" | ||
} else { | ||
val row = stream.head | ||
val name = row[String]("name") | ||
hashToName.put(hash, name) | ||
nameToHash.put(name, hash) | ||
name | ||
} | ||
} | ||
} | ||
RegionHash(name, hash) | ||
} | ||
|
||
def hash(value:String) = ByteUtil.toHexString(MessageDigest.getInstance("MD5").digest(Bytes.toBytes(value))) | ||
|
||
val hashToName:mutable.HashMap[String, String] = new mutable.HashMap() | ||
val nameToHash:mutable.HashMap[String, String] = new mutable.HashMap() | ||
|
||
val SQL_FIND_NAME = SQL(""" | ||
SELECT | ||
name | ||
FROM | ||
region | ||
WHERE | ||
hash={hash} | ||
""") | ||
|
||
val SQL_INSERT_NAME = SQL(""" | ||
INSERT INTO | ||
region(hash, name) | ||
VALUES | ||
({hash}, {name}) | ||
""") | ||
} |
Oops, something went wrong.