[CDRIVER-165] BCON Notation for insert key/value only when value meets condition Created: 02/Aug/12  Updated: 19/Oct/16  Resolved: 30/Aug/12

Status: Closed
Project: C Driver
Component/s: None
Affects Version/s: 0.6
Fix Version/s: 0.7

Type: Improvement Priority: Minor - P4
Reporter: Samuel Chou Assignee: Gary Murakami
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: File bson-special-fun.diff     File bson-special-q.diff    

 Description   

Hi gjmurakami,

BCON looks amazing, thanks for writing this! I see the potential of much clearer C code in our futures =).

Looking at how we currently use libmongoc, it looks like we can't switch over to BCON because we have many places in our code where we test if the value passes a condition before we insert the key/value pair. Simple example:

bson doc[1];
bson_init(doc);
bson_append_new_oid(doc, "_id");
if (match != -1) bson_append_bool(doc, "match", match);
if (domain != NULL) bson_append_string(doc, "dom", domain);
if (user != NULL) bson_append_string(doc, "user", user);
...

I wonder if it would be possible to somehow satisfy this use case as well. The BCON spec is nice and clean, and I don't really have a solution in mind that wouldn't clutter it up somehow. Some food for thought, I guess =).

Sam



 Comments   
Comment by Gary Murakami [ 30/Aug/12 ]

Resolving, please reopen if there is a remaining issue, or open a new ticket.

Comment by Gary Murakami [ 17/Aug/12 ]

Sam:

Glad to help. Please let me know how it works out, so we can close out this ticket, or add work items to satisfy it.

Blessings,

-Gary

Comment by Samuel Chou [ 16/Aug/12 ]

Hi Gary,

Sorry for taking so long to respond. This looks great! I'm really excited... thanks for simplifying our use case.

Sam

Comment by Gary Murakami [ 09/Aug/12 ]

Sam:
Please see the latest commit to github for a general purpose conditional "pointer" interpolation. If you set the pointer to 0 (NULL), the element will not be interpolated. It's still a bit more complex than I'd like due to C array semantics, but it's simpler than the function experiment. Also, I've attached a diff with more specific conditional interpolation for ints != -1.

Please let me know what combinations of the above work for you, and any other feedback is welcome and solicited.

Blessings,

-Gary

Comment by Gary Murakami [ 09/Aug/12 ]

Experimental conditional element interpolation for int32 and int64.

Comment by auto [ 09/Aug/12 ]

Author:

{u'date': u'2012-08-09T13:44:16-07:00', u'email': u'gary.murakami@10gen.com', u'name': u'Gary Murakami'}

Message: CDRIVER-165 BCON Notation for insert key/value only when value meets condition
Reference interpolation for basic interpolation
Pointer(=to=reference) interpolation for conditional interpolation.
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/dfb15c7a2269333fcf9617bb5d615b819d7438fc

Comment by Samuel Chou [ 07/Aug/12 ]

Hi Gary,

Wow, thanks for responding so quickly! For our simple use case, this might be too complicated. At a glance, would we probably have to do something like:

struct mongo_int {
  char *key;
  uint32_t value;
};
struct mongo_str {
  char *key;
  char *value;
};
 
void check_int(bson *b, void *v) {
  struct mongo_int = (struct mongo_int *)v;
  if (v->value != -1)
    bson_append_int(b, mongo_int->key, mongo_int->value);
}
 
void check_str(bson *b, void *v) {
  struct mongo_int = (struct mongo_int *)v;
  if (v->value != NULL)
    bson_append_string(b, mongo_int->key, mongo_int->value);
}
 
bcon doc[] = { 
   "..", BFUN(check_int, (void *)(<initialized mongo_int struct>), 
   "..", BFUN(check_str, (void *)(<initialized mongo_str struct>),
   "." 
};

Comment by Gary Murakami [ 06/Aug/12 ]

Sam:
Thanks for your interest and feedback, and I'm glad to hear that you like BCON and find it useful.
Attached please find bcon-special-fun.diff inspired by your request. I had previous attempts at interpolation toward your purpose, but they where all value types that didn't work well. Your request and simple use case prompted me to revisit this with the result in the diff. Please give it a look and get back to me with your thoughts and suggestions. I'm interested in any improvements, including the naming of things.

Also, I recently ran the performance tests again with -O3 and found only a 1.1 to 1.2 X cost overhead.

Blessings,

-Gary

Comment by Gary Murakami [ 06/Aug/12 ]

diff for special function interpolation

Generated at Wed Feb 07 21:08:38 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.