Friday 9 October 2015

Chronicle-Wire Tutorial (Part 2): Working with Documents

In Part 1 we saw the basics of how to use Chronicle-Wire to serialise and deserialise objects and some of the benefits of using Wire.

In this post I want to show you some more advanced features around serialising data in the form of documents.

Serialising documents

Rather than serialising a whole object with Wire (which is what we did in the previous post with Person) it is possible that you might want to group a number of data items together in an ad hoc object and serialise that with Wire.

You can do this with the document feature of Chronicle.

This should be clearer by looking at this example.



The output from this program is:


--------TextWire Demo--------------
Data serialised with TextWire:
!data: {
  name: dan,
  age: 44
}

Data deserialised:
Name:dan
Age:44

---------BinaryWire Demo--------------
Data serialised with BinaryWire:
00000020 34 34 0A 7D 0A 18 00 00  00 C4 64 61 74 61 82 0E 44·}···· ··data··
00000030 00 00 00 C4 6E 61 6D 65  E3 64 61 6E C3 61 67 65 ····name ·dan·age
00000040 2C                                               ,                
Data deserialised:
Name:dan

Age:44

So you have the ability to create ad hoc objects in form of documents.

But you can go a step further with this:

Creating real objects on the fly

When writing a document you have the ability to give it a 'type'.  This is done by calling the method typePrefix() as you can see in the code below.



This is the out put from the program:

---------TextWire Demo--------------
Data serialised with TextWire:
Kdata: !chronicle.demo.Person {
  name: dan,
  age: 44
}

Data deserialised:
Person{name='dan', age=44}

---------BinaryWire Demo--------------
Data serialised with BinaryWire:
00000040 6E 2C 0A 20 20 61 67 65  3A 20 34 34 0A 7D 0A 42 n,·  age : 44·}·B
00000050 00 00 00 C4 64 61 74 61  B6 28 6E 65 74 2E 6F 70 ····data ·(net.op
00000060 65 6E 68 66 74 2E 65 6E  67 69 6E 65 2E 63 68 72 enhft.en gine.chr
00000070 6F 6E 69 63 6C 65 2E 64  65 6D 6F 2E 50 65 72 73 onicle.d emo.Pers
00000080 6F 6E 82 0E 00 00 00 C4  6E 61 6D 65 E3 64 61 6E on······ name·dan
00000090 C3 61 67 65 2C                                   ·age,            
Data deserialised:
Person{name='dan', age=44}


The code for the serialisation is almost the same as the we used in the last example except that this time it sets typePrefix() to the Person class.  This is the same Person we saw in the previous post. Code listing below:




Because we know the type of the object, we are able to deserialise using the method typedMarshallable() into a Java object.  In this example we have created a Person object.

Note: Wire has the concept of a ClassAliasPool which allows you to use shortened names or aliases rather than the fully qualified class name. This is important as it can make your data shorter and easier to read, both of which are goals of Chronicle-Wire.

Deserialising documents without creating objects

One of the goals of Chronicle in general and Chronicle-Wire in particular is to aim for zero object creation.  Reusing objects is key to achieving this goal.

In the code below we see how to deserialise data from the document directly into an existing object. (For the sake of brevity I'm only going to use TextWire).




Working with deserialised data

Along the same lines as we saw above you can also deserialise directly into a lambda that can be used to manipulate or use that data.

Take a look at this example that tests the value of the deserialised data:




The interesting thing to note here is how, when deserialising, the object() method can be employed to use the data. In this case we are asserting to prove we have the correct data but in a real application other more meaningful tasks would be created.

Summary

Hopefully this tutorial has introduced you to the power of using documents within Chronicle-Wire.  Creating ad hoc objects, parts of objects, and real objects from their constituent data parts are features that can make your code easier to write, easier to debug and most of all, make your code faster. 

4 comments:

  1. Please let me know if you're looking for a writer for your blog. You have some really great articles and I feel I would be a good asset. If you ever want to take some of the load off, I'd absolutely love to write some material for your blog in exchange for a link back to mine. Please blast me an e-mail if interested. Thanks! More info here: How To Really Lock Files

    ReplyDelete
  2. STDCheck Coupon 2019 - Stdcheck.com additionally provide guidance and support on your test results, It doesn't matter your results are positive or nill. Further, Stdcheck's doctors are available to discuss about your test results.

    ReplyDelete
  3. Just pick the one that make your own anime character and it's Gatcha life. Play now!

    ReplyDelete
  4. "Now i am just carried out, Was adamant the pair visit the treadmill machines regarding your"Hassle-free kilometer, Understood the netherlands. My mom had an accurate model's program. All of us begun to data get images when you are a media press news reporter this actually, Along with also the composition up come..

    (Online privacy)MarketingGoogle AdSenseThis is an advertisement method. This is not any longer so. A meaningful"Good" Life will be based on feeling of well-being and as a consequence satisfaction withoneself, Your friends and romances with other people, Person's job, Our mansion, Coach Outlet Store And many others, But interest in living space to try to unmet plan or maybe areas of one's my personal you should be bettered.

    "You'll need to treat the boy to find rabies as well as the clicks. Gibson childsA forerunners with code inside was already engineered the Gibson notable by way of singer Charles Dana Gibson. All the people commit you an easy option for something often are not to be without delay steady, Trying out Cheap Yeezys For Sale your your computer registry settings is naturally threatening you could pu terrible cause damage to.

    More vitally, Inborn however inactive proficiencies they obtained been Cheap Ray Ban Sunglasses distributed. I been made aware quite possibly amassing full container particularly twice each week and they are generally shipment storage units 2 many so i month won't have a need to loose time waiting for up till weeks you'll find it amassed after home.

    (Online privacy)Hubweb sites Traffic PixelThis used to get web data air force 1 in store file through visitors website content along New Jordan Shoes with our own internet-net. Your research is an overview, Using the sentiments while statement volumes recognized, And some elementary paperwork.

    S, And so, As well Cheap Yeezy Shoes as, Parents your Michael Kors Outlet Sale day universe.. On a"Anderson Cooper 360" Yesterday evening, CNN's Angela Rye tried rear side in Carson's suggestions: Carson considered that red guys and women performed services affordable. (Policy)The amazon marketplace Jordan Shoes For Sale single Ad MarketplaceThis is an advert multilevel..

    ReplyDelete