Skip to content

Instantly share code, notes, and snippets.

@joecwu
Created August 5, 2015 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joecwu/ff4dd622c7e570fd12f2 to your computer and use it in GitHub Desktop.
Save joecwu/ff4dd622c7e570fd12f2 to your computer and use it in GitHub Desktop.

Shortener Design Concept

Shortener

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...

DB

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

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.

Exception

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment