-
Notifications
You must be signed in to change notification settings - Fork 1
BlockIO
laforge49 edited this page Sep 14, 2011
·
8 revisions
Given the RandomIO service, we should be able to read and write serialized blocks. We will implement two operations: Writeit, which writes an IncDesString set to the string "abc" and Readit, which returns the string held by the IncDesString. The block that is serialized and written to disk is set to the IncDesString. Here are the messages:
case class Writeit()
case class Readit()
We will write a driver which binds these messages:
class Driver extends Actor {
bind(classOf[Writeit], writeit)
bind(classOf[Readit], readit)
...
}
Here then is the code for the writeit method:
def writeit(msg: AnyRef, rf: Any => Unit) {
val block = Block(mailbox)
block.setSystemServices(systemServices)
val incDesString = IncDesString(null)
val blockLength = IncDesInt(null)
val results = new Results
val chain = new Chain(results)
chain.op(block, Set(null, incDesString))
chain.op(incDesString, Set(null, "abc"))
chain.op(block, Bytes(), "bytes")
chain.op(blockLength,
Unit => {
val blkLen = results("bytes").asInstanceOf[Array[Byte]].length
Set(null, blkLen)
})
chain.op(blockLength, Bytes(), "header")
chain.op(systemServices,
Unit => WriteBytes(0L, results("header").asInstanceOf[Array[Byte]]))
chain.op(systemServices,
Unit => WriteBytes(4L, results("bytes").asInstanceOf[Array[Byte]]))
this(chain)(rf)
}
The above code does the following:
- Create a Block, block, with the same mailbox and system services as the driver.
- Create an IncDesString, incDesString to hold the string.
- Create an IncDesInt, blockLength, for serializing the length of the block.
- Set block to incDesString. The Block now contains the IncDesString.
- Set incDesString to "abc".
- Serialize block.
- Set blockLength to the length of the serialized block.
- Serialize blockLength.
- Write the serialized blockLength to the file.
- Write the serialized block to the file.
And here is the code for the readit method:
def readit(msg: AnyRef, rf: Any => Unit) {
val block = Block(mailbox)
block.setSystemServices(systemServices)
val blockLength = IncDesInt(null)
val results = new Results
val chain = new Chain(results)
chain.op(systemServices, ReadBytes(0L, 4), "header")
chain.op(blockLength,
Unit => {
blockLength.load(results("header").asInstanceOf[Array[Byte]])
Value()
}, "length")
chain.op(systemServices,
Unit => {
val blkLen = results("length").asInstanceOf[Int]
ReadBytes(4L, blkLen)
}, "bytes")
chain.op(block,
Unit => {
block.load(results("bytes").asInstanceOf[Array[Byte]])
Value()
}, "incDesString")
chain.op(Unit => results("incDesString").asInstanceOf[Actor], Value())
this(chain)(rf)
}