[protobuf] Re: How to retrieve parameters using tag numbers using CodedInputStream
I tried YourProtocolMessage.getDefaultInstance() and as you said it is of zero byte size. I've a required field called ID with default cluase in pnroto file (with default value set to -1). When i send the default instance, I see ID set to -1 (which is default value specified in proto file) but on the client side while parsing it says required field ID is not initialized. It looks like if we specify required field in proto file then we can't return default instance. Then i tried creating a proto message with -1 value in ID and as you said it doesn't much cost. On the client side if ID is -1 then i assume it's a null response. Thanks for your response. Regards, Prakash On Aug 16, 8:46 pm, Evan Jones ev...@mit.edu wrote: On Aug 16, 2010, at 10:56 , Prakash Rao wrote: I'm just looking for a easy way to write null response if data is not present in DB and write proto message object if data is present parse these in client side appropriately. I didn't get a easy way to do this using CodedInputStream. Currently i'm creating a empty proto object on server side and checking for key attribute at client side as stated above. The empty protocol buffer message serializes to zero bytes, so if your message has no content, you could just send a zero byte message. This would avoid creating a protocol buffer message. However, I suspect that isn't really a big overhead. You can also use YourProtocolMessage.getDefaultInstance() to avoid creating a message. Hope this helps, Evan Jones -- Evan Joneshttp://evanjones.ca/ -- You received this message because you are subscribed to the Google Groups Protocol Buffers group. To post to this group, send email to proto...@googlegroups.com. To unsubscribe from this group, send email to protobuf+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.
[protobuf] Re: How to retrieve parameters using tag numbers using CodedInputStream
Thanks for your response. I had one more question. Is it possible to write a null value to output stream? I'm writing a client/server application and using PB as layer to transfer data. In certain cases response would be null if required data is not there in my DB. For null responses, i don't want to create a empty proto object (which is an overhead as there would be many cases where response is going to be null) and check on client side if key attribute is not populated then treat as null. I'm just looking for a easy way to write null response if data is not present in DB and write proto message object if data is present parse these in client side appropriately. I didn't get a easy way to do this using CodedInputStream. Currently i'm creating a empty proto object on server side and checking for key attribute at client side as stated above. Regards, Prakash On Aug 12, 9:37 pm, Jason Hsueh jas...@google.com wrote: A tag indicates to the decoder what is coming next in the input stream, and allows the decoder to handle it appropriately. This is a key feature of the protobuf wire format - this enables forward and backward compatibility, as well omitting fields that aren't present from the serialization. The APIs that we have are built around this. A readString(int tag) method would never be used by the generated code. If you really want to take this approach, you can still call readTag(), check that the tag number is what you want, and then read the string. However, I would suggest just using a message. You may find that over time those two or three parameters will grow to the point that maintaining this is unwieldy. On Wed, Aug 11, 2010 at 10:55 PM, Prakash Rao prakashrao1...@gmail.comwrote: I don't have .proto message created for these. Most of my APIs takes .proto messages as parameters (as they represent my business data objects) and in 1-2 APIs I need to pass only 2-3 primitive parameters as arguments. In these cases I’m avoiding creation of .proto messages (as these don’t represent my business data object). As an alternative, I thought of using CodedOutputstream to pass parameters along with tag so that retrieval is easy on the receiving end using CodedInputStream but I don't see a way to retrieve parameter using tags with CodedInputStream. Currently I've the below code and thought the tag approach would make it simple but CodedInputStream doesn't support parameter retrieval based on tags. // Client Side code - which sends s1 s2 strings to server int s1Size = CodedOutputStream.computeStringSizeNoTag(s1); codedOutputStream.writeRawVarint32(s1Size); codedOutputStream.writeStringNoTag(s1); int s2Size = CodedOutputStream.computeStringSizeNoTag(s2); Note: computeStringSizeNoTag() gives the size of the string as well as the encoded length. If you write the result as the encoded length, you will end up reading more bytes than you should. You simply want to write the size of the string. codedOutputStream.writeRawVarint32(s2Size); codedOutputStream.writeStringNoTag(s2); // Server side code - which reads s1 s2 string CodedInputStream codedInputStream = CodedInputStream.newInstance(inputStream); int s1Size = codedInputStream.readRawVarint32(); int s1SizeEntry = codedInputStream.pushLimit(s1Size); String s1 = codedInputStream.readString(); codedInputStream.popLimit(s1SizeEntry); int s2Size = codedInputStream.readRawVarint32(); int s2SizeEntry = codedInputStream.pushLimit(s2Size); String s2 = codedInputStream.readString(); codedInputStream.popLimit(modifiedBySizeEntry); Regards, Prakash On Aug 11, 9:17 pm, Jason Hsueh jas...@google.com wrote: Suppose those strings are all defined as optional fields in the message. They may not be present in the serialization, so the decoder needs to read a tag to determine the field number and type before handling the value. On Wed, Aug 11, 2010 at 8:37 AM, Prakash Rao prakashrao1...@gmail.com wrote: Hi, I was just looking at CodedInputStream class and there are APIs to write primitives with or without tag (for example, writeString writeStringNoTag). I thought if we set unique tags for multiple parameters then we would be able to retrieve these parameters using tag numbers specified using CodedInputStream class. I don't see any APIs to read values based on tags in CodedInputStream. Am I missing something? For the time being I’m using pushLimit popLimit APIs to get different parameter values (based on serializedSize). For example, int test(String s1, String s2, String s3) { } CodedOutputStream out = CodedOutputStream.newIInstance(Some Output Stream); out.writeString(1, s1); out.writeString(2, s2); out.writeString(3, s3); out.flush(); Why these are not possible? CodedInputStream in = CodedInputStream.newIInstance(Some Input Stream); String s1 = in.readString(1); String
Re: [protobuf] Re: How to retrieve parameters using tag numbers using CodedInputStream
On Aug 16, 2010, at 10:56 , Prakash Rao wrote: I'm just looking for a easy way to write null response if data is not present in DB and write proto message object if data is present parse these in client side appropriately. I didn't get a easy way to do this using CodedInputStream. Currently i'm creating a empty proto object on server side and checking for key attribute at client side as stated above. The empty protocol buffer message serializes to zero bytes, so if your message has no content, you could just send a zero byte message. This would avoid creating a protocol buffer message. However, I suspect that isn't really a big overhead. You can also use YourProtocolMessage.getDefaultInstance() to avoid creating a message. Hope this helps, Evan Jones -- Evan Jones http://evanjones.ca/ -- You received this message because you are subscribed to the Google Groups Protocol Buffers group. To post to this group, send email to proto...@googlegroups.com. To unsubscribe from this group, send email to protobuf+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.
Re: [protobuf] Re: How to retrieve parameters using tag numbers using CodedInputStream
A tag indicates to the decoder what is coming next in the input stream, and allows the decoder to handle it appropriately. This is a key feature of the protobuf wire format - this enables forward and backward compatibility, as well omitting fields that aren't present from the serialization. The APIs that we have are built around this. A readString(int tag) method would never be used by the generated code. If you really want to take this approach, you can still call readTag(), check that the tag number is what you want, and then read the string. However, I would suggest just using a message. You may find that over time those two or three parameters will grow to the point that maintaining this is unwieldy. On Wed, Aug 11, 2010 at 10:55 PM, Prakash Rao prakashrao1...@gmail.comwrote: I don't have .proto message created for these. Most of my APIs takes .proto messages as parameters (as they represent my business data objects) and in 1-2 APIs I need to pass only 2-3 primitive parameters as arguments. In these cases I’m avoiding creation of .proto messages (as these don’t represent my business data object). As an alternative, I thought of using CodedOutputstream to pass parameters along with tag so that retrieval is easy on the receiving end using CodedInputStream but I don't see a way to retrieve parameter using tags with CodedInputStream. Currently I've the below code and thought the tag approach would make it simple but CodedInputStream doesn't support parameter retrieval based on tags. // Client Side code - which sends s1 s2 strings to server int s1Size = CodedOutputStream.computeStringSizeNoTag(s1); codedOutputStream.writeRawVarint32(s1Size); codedOutputStream.writeStringNoTag(s1); int s2Size = CodedOutputStream.computeStringSizeNoTag(s2); Note: computeStringSizeNoTag() gives the size of the string as well as the encoded length. If you write the result as the encoded length, you will end up reading more bytes than you should. You simply want to write the size of the string. codedOutputStream.writeRawVarint32(s2Size); codedOutputStream.writeStringNoTag(s2); // Server side code - which reads s1 s2 string CodedInputStream codedInputStream = CodedInputStream.newInstance(inputStream); int s1Size = codedInputStream.readRawVarint32(); int s1SizeEntry = codedInputStream.pushLimit(s1Size); String s1 = codedInputStream.readString(); codedInputStream.popLimit(s1SizeEntry); int s2Size = codedInputStream.readRawVarint32(); int s2SizeEntry = codedInputStream.pushLimit(s2Size); String s2 = codedInputStream.readString(); codedInputStream.popLimit(modifiedBySizeEntry); Regards, Prakash On Aug 11, 9:17 pm, Jason Hsueh jas...@google.com wrote: Suppose those strings are all defined as optional fields in the message. They may not be present in the serialization, so the decoder needs to read a tag to determine the field number and type before handling the value. On Wed, Aug 11, 2010 at 8:37 AM, Prakash Rao prakashrao1...@gmail.com wrote: Hi, I was just looking at CodedInputStream class and there are APIs to write primitives with or without tag (for example, writeString writeStringNoTag). I thought if we set unique tags for multiple parameters then we would be able to retrieve these parameters using tag numbers specified using CodedInputStream class. I don't see any APIs to read values based on tags in CodedInputStream. Am I missing something? For the time being I’m using pushLimit popLimit APIs to get different parameter values (based on serializedSize). For example, int test(String s1, String s2, String s3) { } CodedOutputStream out = CodedOutputStream.newIInstance(Some Output Stream); out.writeString(1, s1); out.writeString(2, s2); out.writeString(3, s3); out.flush(); Why these are not possible? CodedInputStream in = CodedInputStream.newIInstance(Some Input Stream); String s1 = in.readString(1); String s2 = in.readString(2); String s3 = in.readString(3); Regards, Prakash -- You received this message because you are subscribed to the Google Groups Protocol Buffers group. To post to this group, send email to proto...@googlegroups.com. To unsubscribe from this group, send email to protobuf+unsubscr...@googlegroups.comprotobuf%2bunsubscr...@googlegroups.com protobuf%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.- Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Protocol Buffers group. To post to this group, send email to proto...@googlegroups.com. To unsubscribe from this group, send email to protobuf+unsubscr...@googlegroups.comprotobuf%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/protobuf?hl=en. -- You received this message because you