Posts

Showing posts from 2015

Presenting the File Tracker

Image
This project goal is to track changes in files and manage those changes as a byte array in asynchronous way using Akka actors and Java NIO library. This is done by registering directory for the  WatchService  and filtering the files using  PathMatcher  . For each change in the file the requester will receive the byte array reflecting that change. Currently this project supports only addition to file, i.e deletion of characters in file is not supported. The Complete Source Code can be found here Let's dive in. The Ingredients : FileSyncIo . This part is copied from the  FileAsyncIo project  with some adjustments, and it is very handy for reading files asynchronously. In order to read the file we use Java NIO  AsynchronousFileChannel  . Since we are only reading the file, we open the channel with the  Read  option. The AsynchronousFileChannel.read method accepts buffer, the start position and a handler : val p = Promise [ Array [ Byte ]]() val buffe

Harness Scala Type Classes and Implicits

In my  previous blog , I presented an Expression ADT. In this article I will extend its functionality and serialize it using other ADT while maintaining decoupling using Scala's magic a.k.a implicits and type classes . Full code is in  this repository . Let's start by building our JSON serializer ADT sealed trait JSON case class JSeq ( elms : List [ JSON ]) extends JSON case class JObj ( bindings : Map [ String , JSON ]) extends JSON case class JNum ( num : Double ) extends JSON case class JStr ( str : String ) extends JSON case class JBool ( b : Boolean ) extends JSON case object JNull extends JSON and now we can create our JSONWriter to convert JSON objects to nice JSON String object JSONWriter { def write ( j : JSON ) : String = { j match { case JSeq ( e ) => e . map ( write ). mkString ( "[" , "," , "]" ) case JObj ( obj ) => obj . map ( o => &q