void update_dbase(time_t TimeStamp, char *NodeName, json_object *jobj) { int DBTimeStamp = -1; int blobid = -1; int overwrite = 0; //printf("NodeName - %s, TimeStamp - %ld\n", NodeName, TimeStamp); // Now check if the NodeName exists in the datastore table // If so compare the timestamp values and decide what to do. if ((DBTimeStamp = NodeTS_fromDB(NodeName, db)) == -1) { // PKT with data from a new node insert_json(NodeName, TimeStamp, jobj, db); blobid = NodeBID_fromDB(NodeName, db); insertLookups(blobid, jobj, db); } else if (DBTimeStamp < TimeStamp) { // PKT with newer time stamp blobid = NodeBID_fromDB(NodeName, db); overwrite = 1; update_insertLookups(blobid, jobj, db, overwrite); merge_json(NodeName, TimeStamp, jobj, db, overwrite); #ifdef WWDEBUG printf("Just finished processing PKT with newer time stamp\n"); #endif } else if (DBTimeStamp >= TimeStamp) { // PKT with same time stamp or with older time stamp blobid = NodeBID_fromDB(NodeName, db); overwrite = 0; #ifdef WWDEBUG printf("About to update & insert\n"); #endif update_insertLookups(blobid, jobj, db, overwrite); #ifdef WWDEBUG printf("Just finished half processing PKT with older or same time stamp\n"); #endif merge_json(NodeName, TimeStamp, jobj, db, overwrite); #ifdef WWDEBUG printf("Just finished processing PKT with older or same time stamp\n"); #endif } }
/** * Merges two JSON objects * * On conflicts, object a will be preferred. * * Internally, this functions merges all entries from object a into object b, * so merging a small object a with a big object b is faster than vice-versa. */ static struct json_object * merge_json(struct json_object *a, struct json_object *b) { if (!json_object_is_type(a, json_type_object) || !json_object_is_type(b, json_type_object)) { json_object_put(b); return a; } json_object_object_foreach(a, key, val_a) { struct json_object *val_b; json_object_get(val_a); if (!json_object_object_get_ex(b, key, &val_b)) { json_object_object_add(b, key, val_a); continue; } json_object_get(val_b); json_object_object_add(b, key, merge_json(val_a, val_b)); } json_object_put(a); return b; }