Maintenance effort with protobuf

50 views
Skip to first unread message

Florian Johannßen

unread,
May 6, 2013, 5:50:09 AM5/6/13
to prot...@googlegroups.com

Hi folks,

I am just integrating protobuf in our distributed system to replace the slowly java serialization. Our system is written in Scala and we have much traffic of sending immutable data (Updates) among client and server.
So, I think we can improve our performance with protobuf.
Firstly I have serialized some easy objects, like a person:

case class Person(id: Int, name: String, email: String = "", phoneNumbers: List[PhoneNumber], address: Address)
Than, the java compiler generates java code.
If I want to write my person into a stream, i have to bind the application object (MyPerson) to the generated java object (Person).

So, I extend my class MyPerson with the method to create a java-person. Note, that we have written a bind method for every application object which we want to serialize.
def bind(): Person = {
      Person.newBuilder()
         .setId(id)
         .setName(name)
         .setEmail(email)
         .addPhone(this.phoneNumbers.first.bind())
         .setAddress(address.bind())
         .build()
   }
 
Now, we can write a myPerson into an output stream:
val myPerson = MyPerson(1234, "John Doe", "jd...@example.com", List(PhoneNumber("00003", PhoneType.HOME)), Address("B1", 1111, 14))
val out = new ByteArrayOutputStream()
myPerson.bind().writeTo(out)

The next step is to unbind the java generated to our application object (MyPerson).
val deseriPerson = Person.parseFrom(in)
val result = Unbinder.unbind(deseriPerson)

Our unbinder creates a scala-MyPerson by the given generated java person. We have written an unbinder for each application object as well.

class PersonUnbinder {

   def unbind(aMsg: Person): MyPerson = {
      val phones = aMsg.getPhoneList().asScala.map(msg => Unbinder.unbind(msg).asInstanceOf[PhoneNumber]).toList
      val address = Unbinder.unbind(aMsg.getAddress()).asInstanceOf[Address]
      MyPerson(aMsg.getId(), aMsg.getName(), aMsg.getEmail(), phones, address)
   }
}
The unbinde of the person calls the unbind methods of his child’s.

So, that is our developing progress to use protobuf in our system. You see, we have a lot of maintenance extra work which we don´t have if we are use the java serialization.
If we change our application object, we have to change the .proto file, the bind method and the unbind method as well.
What do you think about this?
Are there some ideas or tools for improving this progress?
Thanks

Best regards Florian

Reply all
Reply to author
Forward
0 new messages