0

Lets sat I have an object i'd like to store in a direct byte buffer. I'd like to able access parts of the object from the direct byte buffer without de-serializing the whole object. Is there a safe way to do this?

I'm thinking you could somehow capture the byte array offsets when serializing the object, then once its been written to the direct byte buffer you would adjust these offsets according to the offset of the direct byte buffer. I'm not sure if its possible to do this...

newlogic
  • 807
  • 8
  • 25

2 Answers2

0

The real question is not, is this possible, since it almost certainly is, but why do you want to do this in the first place?

If you just want to access a few fields from the object, the easiest way to do that will be to deserialize it and then copy those few fields out.

The only reason you might want to avoid the (de-)serialization is for speed, but if this is in one of your busy loops, then you are lost anyway. If the network (de-)serialization is the issue, then you should design your protocol better.

Paul Wagland
  • 27,756
  • 10
  • 52
  • 74
  • Because if you are really doing serialization in your "inner loop", and you want to do it as fast as possible, then you should redesign your communication with that in mind. In 99% of cases, the communication is the bottleneck, and not the serialization. – Paul Wagland Oct 18 '13 at 10:28
  • I'm not doing network serialization, I simply want to write an object into a direct byte buffer and then access parts of that object quickly without have to de-serialize the whole object, for the purpose of speed. – newlogic Oct 18 '13 at 10:31
  • I am sure that you have a perfectly logical reason for wanting to do this, however without knowing what that reason is, there is no way for me to refute you. I don't think that what you want makes sense, based purely on the provided information. There may well be more information that would make this more logical that I don't have. – Paul Wagland Oct 18 '13 at 13:25
  • Ok rather than creating objects on the heap and manipulating them there i'd like to create objects off the heap (hence in direct byte buffers) so that the GC does not need to run, thus preventing latency. I want to be able to manipulate these off heap objects safely. I believe obtaining the byte offsets during serialization is the way to do this but i'm not sure how. What are your thoughts? – newlogic Oct 18 '13 at 13:33
  • 1
    Do you have to use serialization? That is, are you in control of the source of the data? If so, then the problem becomes trivial, since you can insert the markers yourself, for example by using something like https://developers.google.com/protocol-buffers/. – Paul Wagland Oct 18 '13 at 21:18
0

I think the best way of doing is like so, its a bit of a work around, but should be effective.

 interface OffsetMemberMap {
     Map<String,Long> offsetMemberMap();
}

The idea is to create objects that implement the above interface, this map will store memory addresses against Strings for each member. Child objects would be created first and once added to a DirectByteBuffer the offset position would be stored in the parent within this map. In order to access a specific member the user would need to supply the string which addresses that member and thus only what's needed would be de-serialized. This would allow you to store large linked objects in DirectByteBuffers whilst being able to only serialize/de-serialize the bits you need when writing/reading.

newlogic
  • 807
  • 8
  • 25