Exemplo n.º 1
0
static void display_reduce_exprs( struct deltadb *db, time_t current )
{
	struct list_node *n;

	/* Reset all reductions. */
	for(n=db->reduce_exprs->head;n;n=n->next) {
		deltadb_reduction_reset(n->data);
	}

	/* For each item in the hash table: */

	char *key;
	struct jx *jobject;
	hash_table_firstkey(db->table);
	while(hash_table_nextkey(db->table,&key,(void**)&jobject)) {

		/* Skip if the where expression doesn't match */
		if(!deltadb_boolean_expr(db->where_expr,jobject)) continue;

		/* Update each reduction with its value. */
		for(n=db->reduce_exprs->head;n;n=n->next) {
			struct deltadb_reduction *r = n->data;
			struct jx *value = jx_eval(r->expr,jobject);
			if(value && !jx_istype(value, JX_ERROR)) {
				if(value->type==JX_INTEGER) {
					deltadb_reduction_update(n->data,(double)value->u.integer_value);
				} else if(value->type==JX_DOUBLE) {
					deltadb_reduction_update(n->data,value->u.double_value);
				} else {
					// treat non-numerics as 1, to facilitate operations like COUNT
					deltadb_reduction_update(n->data,1);
				}

				jx_delete(value);
			}
		}
	}

	/* Emit the current time */

	if(db->epoch_mode) {
		printf("%lld\t",(long long) current);
	} else {
		char str[32];
		strftime(str,sizeof(str),"%F %T",localtime(&current));
		printf("%s\t",str);
	}

	/* For each reduction, display the final value. */
	for(n=db->reduce_exprs->head;n;n=n->next) {
		printf("%lf\t",deltadb_reduction_value(n->data));
	}

	printf("\n");

}
void emit_all_deltadb_reductions( struct deltadb *db, time_t current, int first_output )
{
	int i;
	struct nvpair *nv;
	char *key;
	const char *value;

	/* Reset all deltadb_reduction state. */
	for(i=0;i<db->ndeltadb_reductions;i++) {
		deltadb_reduction_reset(db->deltadb_reductions[i]);
	}

	/* After each event, iterate over all objects... */

	hash_table_firstkey(db->table);
	while(hash_table_nextkey(db->table,&key,(void**)&nv)) {

		/* Update all deltadb_reductions for that object. */
		for(i=0;i<db->ndeltadb_reductions;i++) {
			struct deltadb_reduction *r = db->deltadb_reductions[i];
			value = nvpair_lookup_string(nv,r->attr);
			if(value) deltadb_reduction_update(r,value);
		}
	}

	if(first_output) {
		/* The first time we do this, make it a checkpoint record. */
		printf("T %ld\n",(long)current);
		printf("C 0\n");
		for(i=0;i<db->ndeltadb_reductions;i++) {
			struct deltadb_reduction *r = db->deltadb_reductions[i];
			deltadb_reduction_print(r);
		}
		printf("\n");
		//printf(".Checkpoint End.\n");
		first_output = 0;
	} else {
		/* After that, make it an update record. */
		printf("T %ld\n",(long)current);
		for(i=0;i<db->ndeltadb_reductions;i++) {
			struct deltadb_reduction *r = db->deltadb_reductions[i];
			printf("U 0 ");
			deltadb_reduction_print(r);
		}
	}
}