Shortener
trait is an interface that defines core functions of shortener which is shorter
& taller
.
trait Shortener {
def shorter(url:String)(implicit dbClient : DBClient, tracerInfo: TracerInfo) : String Or BaseException
def taller(short:String)(implicit dbClient : DBClient, tracerInfo: TracerInfo) : String Or BaseException
}
One of shortener implementation at com.joecwu.shortener.hash.Shortener
which is a SHA hash implementation.
You are able to implement different way to provide shortener result, e.g: incremental number, different hash implementation...
All generated shorter URL have to save into a storage, and here defined a DBClient
trait for shortener.
trait DBClient {
def save(url:String,shorter:String)(implicit tracerInfo: TracerInfo) : Unit Or DBException
def getUrl(shorter:String)(implicit tracerInfo: TracerInfo) : String Or DBException
def getShorter(url:String)(implicit tracerInfo: TracerInfo) : Option[String] Or DBException
}
shortener provides a MemoryDBClient implmentation at com.joecwu.shortener.db.memory.MemoryDBClient
which uses HashMap as a storage. There is also a RedisDBClient definition at com.joecwu.shortener.db.redis.RedisDBClient
, but not yet implemented.
TracerInfo is an information collection for tracing specific request via whole system logic, which defined at com.joecwu.shortener.exception
.
case class TracerInfo(tid:String=java.util.UUID.randomUUID.toString)
Currently it has only one argument - tid
(TracerId), and it will be write into log so you can trace a request easier.
Shortener defined a super class called BaseException
and with related sub class of exception be located at com.joecwu.shortener.exception
.
class BaseException(msg : String = "", cause: Throwable = null)(implicit tracerInfo: TracerInfo) extends Exception(msg, cause) {
val tid = tracerInfo.tid
}
class DBException(msg : String = "", cause: Throwable = null)(implicit tracerInfo: TracerInfo) extends BaseException(msg,cause)
All exceptions has a implicit argument - TracerInfo
, which will provide some tracer information in error log.