Esempio n. 1
0
File: table.c Progetto: def44/mate
int table_next_key(struct table *t) {
	int n, prev;

	if (t == NULL) {
		fprintf(stderr, "mvm: table not initialized!\n");
		mvm_halt();
	}

#ifdef DMP
	if (t->dmp != NULL)
		table_dmp_load(t->dmp);
#endif

	/* lock */
	table_wrlock(t);

	if (t->iterator_is_running == 0) {
		/* unlock */
		table_unlock(t);
		return 0;
	}

#ifdef DMP
	if (t->dmp != NULL)
		table_dmp_store(t->dmp);
#endif

	prev = t->iterator_entry->key;

	t->iterator_entry = t->iterator_entry->next;
	if (t->iterator_entry != NULL) {
		/* unlock */
		table_unlock(t);
		return prev;
	}

	for (n = t->iterator_bucket+1; n < t->current_capacity; n++) {
		if (t->buckets[n] != NULL) {
			t->iterator_bucket = n;
			t->iterator_entry = t->buckets[n];
			break;
		}
	}

	if (n >= t->current_capacity)
		t->iterator_is_running = 0;

	/* unlock */
	table_unlock(t);

	return prev;
}
Esempio n. 2
0
File: table.c Progetto: def44/mate
int table_put(struct table *t, struct object *k, struct object *v) {
	int hash;

	if (t == NULL) {
		fprintf(stderr, "mvm: table not initialized!\n");
		mvm_halt();
	}

#ifdef DMP
	if (t->dmp != NULL)
		table_dmp_load(t->dmp);
#endif

	/* lock */
	table_rdlock(t);

	/* check if iterator is running */
	if (t->iterator_is_running == 1) {
		fprintf(stderr, "mvm: cannot put, Table's iterator is running!\n");
		mvm_halt();
	}

	/* unlock */
	table_unlock(t);

	hash = abs(table_run_hash_code(t, k));

#ifdef DMP
	if (t->dmp != NULL)
		table_dmp_load(t->dmp);
#endif

	/* lock */
	table_rdlock(t);

	if (t->current_capacity <= 0) {
		table_resize(t, TABLE_DEFAULT_INITIAL_CAPACITY, &t);
	}

	/* unlock */
	table_unlock(t);

#ifdef DMP
	if (t->dmp != NULL)
		table_dmp_load(t->dmp);
#endif

	return table_put_aux(t, k, v, hash);
}
Esempio n. 3
0
static int sse50etf_exec(void *data, void *data2) {
	RAII_VAR(struct msg *, msg, (struct msg *)data, msg_decr);
	Quote *quote = (Quote *)msg->data;
	struct wp *wp;
	NOT_USED(data2);

	if (isnan(cru) || isnan(ecc)) {
		xcb_log(XCB_LOG_WARNING, "No valid value for CRU or ECC");
		goto end;
	}
	table_lock(contracts);
	if ((wp = table_get_value(contracts, quote->thyquote.m_cHYDM)) &&
		(isnan(wp->price) || fabs(wp->price - quote->thyquote.m_dZXJ) > 0.000001)) {
		table_iter_t iter;
		table_node_t node;
		double sum = 0.0;
		time_t t = (time_t)quote->thyquote.m_nTime;
		struct tm lt;
		char datestr[64], res[512];

		if (wp->type == 2) {
			table_unlock(contracts);
			goto end;
		}
		wp->price = quote->thyquote.m_dZXJ;
		iter = table_iter_new(contracts);
		while ((node = table_next(iter))) {
			wp = table_node_value(node);
			if (isnan(wp->price)) {
				xcb_log(XCB_LOG_WARNING, "No price info for '%s'", table_node_key(node));
				table_iter_free(&iter);
				table_unlock(contracts);
				goto end;
			}
			sum += wp->weight * wp->price;
		}
		table_iter_free(&iter);
		strftime(datestr, sizeof datestr, "%F %T", localtime_r(&t, &lt));
		snprintf(res, sizeof res, "SSE50ETF,%s.%03d|%f",
			datestr,
			quote->m_nMSec,
			(sum + ecc) / cru);
		out2rmp(res);
	}
	table_unlock(contracts);

end:
	return 0;
}
Esempio n. 4
0
File: table.c Progetto: def44/mate
int table_populate_ref_set(struct table *t, struct ref_set *r) {
	int i;
	struct table_entry *p;

	if (t == NULL) {
		fprintf(stderr, "mvm: table not initialized!\n");
		mvm_halt();
	}

	/* lock */
	table_rdlock(t);

	for (i = 0; i < t->current_capacity; i++) {
		for (p = t->buckets[i]; p != NULL; p = p->next) {
			if (p->key != 0)
				ref_set_add(r, p->key);
			if (p->value != 0)
				ref_set_add(r, p->value);
		}
	}

	/* unlock */
	table_unlock(t);

	return 0;
}
Esempio n. 5
0
File: table.c Progetto: def44/mate
int table_get(struct table *t, struct object *k) {
	struct table_entry *r;
	int n, hash, value;

	if (t == NULL) {
		fprintf(stderr, "mvm: table not initialized!\n");
		mvm_halt();
	}

	hash = abs(table_run_hash_code(t, k));

#ifdef DMP
	if (t->dmp != NULL)
		table_dmp_load(t->dmp);
#endif

	/* lock */
	table_rdlock(t);

	n = hash % t->current_capacity;

	for (r = t->buckets[n]; r != NULL; r = r->next) {
		if (table_run_equals(t, k, heap_fetch_object(heap, r->key)) == 1) {
#ifdef DMP
			if (t->dmp != NULL)
				table_dmp_load(t->dmp);
#endif

			value = r->value;

			/* unlock */
			table_unlock(t);
			return value;
		}
		
#ifdef DMP
		if (t->dmp != NULL)
			table_dmp_load(t->dmp);
#endif
	}

	/* unlock */
	table_unlock(t);

	return 0;
}
Esempio n. 6
0
/* FIXME */
void index_command(client c) {
	const char *fmt;

	table_lock(idxfmts);
	if ((fmt = table_get_value(idxfmts, c->argv[1])))
		add_reply_string_format(c, "%s\r\n", fmt);
	else
		add_reply_error(c, "format unavailable");
	table_unlock(idxfmts);
	add_reply_string(c, "\r\n", 2);
}
Esempio n. 7
0
File: table.c Progetto: def44/mate
int table_put_aux(struct table *t, struct object *k, struct object *v, int hash) {
	int value, n, old_value;
	struct table_entry *new_entry, *r;

	n = hash % t->current_capacity;
	old_value = 0;

	for (r = t->buckets[n]; r != NULL; r = r->next) {
		value = table_run_equals(t, k, heap_fetch_object(heap, r->key));
		if (value == 1)
			break;

#ifdef DMP
		if (t->dmp != NULL)
			table_dmp_load(t->dmp);
#endif
	}

#ifdef DMP
	if (t->dmp != NULL)
		table_dmp_store(t->dmp);
#endif

	/* lock */
	table_wrlock(t);

	if (r != NULL) {
		old_value = r->value;
		r->key = object_get_ref(k);
		r->value = object_get_ref(v);
		r->hash = hash;
	} else {
		if ((new_entry =
		     table_entry_create(object_get_ref(k), object_get_ref(v), hash)) == NULL) {
			mvm_halt();
		}

		new_entry->next = t->buckets[n];
		t->buckets[n] = new_entry;
		t->num_entries++;
	}

	if (table_entries_exceeds_load_factor(t->num_entries,
					      t->load_factor,
					      t->current_capacity)) {
		table_resize(t, t->current_capacity*2, &t);
		object_set_table(t->object, t);
	}

	/* unlock */
	table_unlock(t);

	return old_value;
}
Esempio n. 8
0
/* FIXME */
void qc_command(client c) {
	char buf[64];
	dstr rid;
	struct crss *crss;

	snprintf(buf, sizeof buf, "%p,", c);
	rid = dstr_new(buf);
	rid = dstr_cat(rid, c->argv[1]);
	if (rids == NULL)
		rids = table_new(cmpstr, hashmurmur2, kfree, NULL);
	table_lock(rids);
	if ((crss = table_get_value(rids, rid))) {
		crss->cancel = 1;
		xcb_log(XCB_LOG_DEBUG, "Query with rid '%s' got cancelled", c->argv[1]);
	}
	table_unlock(rids);
	add_reply_string(c, "\r\n", 2);
	dstr_free(rid);
}
Esempio n. 9
0
File: table.c Progetto: def44/mate
int table_resize(struct table *t, int n, struct table **nt) {
	int i;
	struct table_entry *r;
	struct table *new_table;

	if (t == NULL) {
		fprintf(stderr, "mvm: table not initialized!\n");
		mvm_halt();
	}

	/* lock */
	table_wrlock(t);

	if ((new_table = table_create(n, t->object)) == NULL)
		mvm_halt();

	for (i = 0; i < t->current_capacity; i++ ) {
		for (r = t->buckets[i]; r != NULL; r = r->next) {
			table_put_aux(new_table,
				      heap_fetch_object(heap, r->key),
				      heap_fetch_object(heap, r->value),
				      r->hash);
		}
	}

	table_clear(t);
	heap_free(heap, t->buckets);
	memcpy(t, new_table, sizeof(struct table));
	heap_free(heap, new_table);

	if (nt != NULL)
		*nt = new_table;

	object_set_table(t->object, t);

	/* unlock */
	table_unlock(t);

	return 0;
}
Esempio n. 10
0
File: table.c Progetto: def44/mate
int table_first_key(struct table *t) {
	int ref, i;
	int32_t value;

	if (t == NULL) {
		fprintf(stderr, "mvm: table not initialized!\n");
		mvm_halt();
	}

#ifdef DMP
	if (t->dmp != NULL)
		table_dmp_store(t->dmp);
#endif

	/* lock */
	table_wrlock(t);

	for (i = 0; i < t->current_capacity; i++) {
		if (t->buckets[i] != NULL)
			break;
	}

	if (i >= t->current_capacity) {
		value = 1;
		t->iterator_is_running = 0;
	} else {
		value = 0;
		t->iterator_is_running = 1;
		t->iterator_bucket = i;
		t->iterator_entry = t->buckets[i];
	}

	/* unlock */
	table_unlock(t);

	if ((ref = class_table_new_integer(class_table, value, NULL)) == 0)
		mvm_halt();

	return ref;
}
Esempio n. 11
0
/* FIXME */
void s_command(client c) {
	dstr pkey, skey;
	int i;
	dlist_t dlist;
	struct kvd *kvd;

	if (dstr_length(c->argv[0]) == 1) {
		add_reply_error(c, "index can't be empty\r\n");
		return;
	}
	pkey = dstr_new(c->argv[0] + 1);
	skey = dstr_new(pkey);
	if (c->argc > 1)
		for (i = 1; i < c->argc; ++i) {
			skey = dstr_cat(skey, ",");
			skey = dstr_cat(skey, c->argv[i]);
		}
	table_lock(cache);
	if ((dlist = table_get_value(cache, pkey))) {
		dlist_iter_t iter = dlist_iter_new(dlist, DLIST_START_HEAD);
		dlist_node_t node;

		/* FIXME: still has room to improve */
		while ((node = dlist_next(iter))) {
			kvd = (struct kvd *)dlist_node_value(node);
			if (dstr_length(kvd->key) >= dstr_length(skey) &&
				!memcmp(kvd->key, skey, dstr_length(skey))) {
				dstr *fields = NULL;
				int nfield = 0;
				dstr res, contracts = NULL;

				fields = dstr_split_len(kvd->key, dstr_length(kvd->key), ",", 1, &nfield);
				res = dstr_new(fields[0]);
				if (nfield > 1) {
					contracts = dstr_new(fields[1]);
					for (i = 2; i < nfield; ++i) {
						contracts = dstr_cat(contracts, ",");
						contracts = dstr_cat(contracts, fields[i]);
					}
				}
				dstr_free_tokens(fields, nfield);
				fields = NULL, nfield = 0;
				fields = dstr_split_len(kvd->u.value, dstr_length(kvd->u.value),
					",", 1, &nfield);
				res = dstr_cat(res, ",");
				res = dstr_cat(res, fields[0]);
				if (contracts) {
					res = dstr_cat(res, ",");
					res = dstr_cat(res, contracts);
				}
				for (i = 1; i < nfield; ++i) {
					res = dstr_cat(res, ",");
					res = dstr_cat(res, fields[i]);
				}
				res = dstr_cat(res, "\r\n");
				pthread_spin_lock(&c->lock);
				if (!(c->flags & CLIENT_CLOSE_ASAP)) {
					if (net_try_write(c->fd, res, dstr_length(res),
						10, NET_NONBLOCK) == -1) {
						xcb_log(XCB_LOG_WARNING, "Writing to client '%p': %s",
							c, strerror(errno));
						if (++c->eagcount >= 3) {
							client_free_async(c);
							pthread_spin_unlock(&c->lock);
							dstr_free(contracts);
							dstr_free(res);
							dstr_free_tokens(fields, nfield);
							table_unlock(cache);
							dstr_free(skey);
							dstr_free(pkey);
							return;
						}
					} else if (c->eagcount)
						c->eagcount = 0;
				}
				pthread_spin_unlock(&c->lock);
				dstr_free(contracts);
				dstr_free(res);
				dstr_free_tokens(fields, nfield);
			}
		}
		dlist_iter_free(&iter);
	}
	table_unlock(cache);
	table_rwlock_wrlock(subscribers);
	if ((dlist = table_get_value(subscribers, pkey)) == NULL) {
		if (NEW(kvd)) {
			kvd->key     = skey;
			kvd->u.dlist = dlist_new(NULL, NULL);
			dlist_insert_tail(kvd->u.dlist, c);
			dlist = dlist_new(cmpkvd, kdfree);
			dlist_insert_sort(dlist, kvd);
			table_insert(subscribers, pkey, dlist);
		} else {
			add_reply_error(c, "error allocating memory for kvd\r\n");
			dstr_free(skey);
			dstr_free(pkey);
		}
	} else {
		if (NEW(kvd)) {
			dlist_node_t node;

			kvd->key     = skey;
			kvd->u.dlist = dlist_new(NULL, NULL);
			if ((node = dlist_find(dlist, kvd)) == NULL) {
				dlist_insert_tail(kvd->u.dlist, c);
				dlist_insert_sort(dlist, kvd);
			} else {
				kdfree(kvd);
				kvd = (struct kvd *)dlist_node_value(node);
				if (dlist_find(kvd->u.dlist, c) == NULL)
					dlist_insert_tail(kvd->u.dlist, c);
			}
		} else {
			add_reply_error(c, "error allocating memory for kvd\r\n");
			dstr_free(skey);
		}
		dstr_free(pkey);
	}
	table_rwlock_unlock(subscribers);
}
Esempio n. 12
0
static int impvbaw_exec(void *data, void *data2) {
	RAII_VAR(struct msg *, msg, (struct msg *)data, msg_decr);
	Quote *quote = (Quote *)msg->data;
	struct msgs *out = (struct msgs *)data2;
	dstr contract;
	float last;
	char *p;
	table_node_t node;

	contract = dstr_new(quote->thyquote.m_cHYDM);
	if (!strcmp(contract, "") || (fabs(quote->thyquote.m_dZXJ) <= 0.000001 &&
		fabs(quote->thyquote.m_dMRJG1) <= 0.000001 &&
		fabs(quote->thyquote.m_dMCJG1) <= 0.000001)) {
		xcb_log(XCB_LOG_WARNING, "Invalid quote: '%d,%d,%s,%.2f,%.2f,%.2f'",
			quote->thyquote.m_nTime,
			quote->m_nMSec,
			contract,
			quote->thyquote.m_dZXJ,
			quote->thyquote.m_dMRJG1,
			quote->thyquote.m_dMCJG1);
		goto end;
	}
	/* FIXME */
	if (!strncasecmp(contract, "IO", 2) || !strncasecmp(contract, "HO", 2) ||
		!strncasecmp(contract, "00", 2) || !strncasecmp(contract, "60", 2))
		goto end;
	last = quote->thyquote.m_dZXJ;
	if ((p = strrchr(contract, 'C')) == NULL)
		p = strrchr(contract, 'P');
	/* FIXME: option quote */
	if (p && p != contract && p != contract + dstr_length(contract) - 1 &&
		((*(p - 1) == '-' && *(p + 1) == '-') || (isdigit(*(p - 1)) && isdigit(*(p + 1))))) {
		dstr spotname, type, strike;
		float spot;
		struct prices *prices;
		double expiry, vol, vol2, vol3;
		struct tm lt;
		char *res;

		spotname = *(p - 1) == '-' ? dstr_new_len(contract, p - contract - 1) :
			dstr_new_len(contract, p - contract);
		type     = dstr_new_len(p, 1);
		strike   = *(p + 1) == '-' ? dstr_new_len(p + 2, contract + dstr_length(contract) - p - 2) :
			dstr_new_len(p + 1, contract + dstr_length(contract) - p - 1);
		table_rwlock_rdlock(spots);
		if ((node = table_find(spots, spotname)) == NULL) {
			table_rwlock_unlock(spots);
			dstr_free(strike);
			dstr_free(type);
			dstr_free(spotname);
			goto end;
		}
		spot = table_node_float(node);
		table_rwlock_unlock(spots);
		if (fabs(spot) <= 0.000001) {
			xcb_log(XCB_LOG_WARNING, "The price of spot '%s' be zero", spotname);
			dstr_free(strike);
			dstr_free(type);
			dstr_free(spotname);
			goto end;
		}
		table_lock(optns);
		if ((node = table_find(optns, contract)) == NULL) {
			if (NEW(prices)) {
				prices->prelast = last;
				prices->prebid1 = quote->thyquote.m_dMRJG1;
				prices->preask1 = quote->thyquote.m_dMCJG1;
				prices->prespot = spot;
				table_insert(optns, dstr_new(contract), prices);
			} else
				xcb_log(XCB_LOG_WARNING, "Error allocating memory for prices");
		} else {
			prices = (struct prices *)table_node_value(node);

			if (fabs(prices->prelast - last) <= 0.000001 &&
				fabs(prices->prebid1 - quote->thyquote.m_dMRJG1) <= 0.000001 &&
				fabs(prices->preask1 - quote->thyquote.m_dMCJG1) <= 0.000001 &&
				fabs(prices->prespot - spot) <= 0.000001) {
				table_unlock(optns);
				dstr_free(strike);
				dstr_free(type);
				dstr_free(spotname);
				goto end;
			} else {
				prices->prelast = last;
				prices->prebid1 = quote->thyquote.m_dMRJG1;
				prices->preask1 = quote->thyquote.m_dMCJG1;
				prices->prespot = spot;
			}
		}
		table_unlock(optns);
		if ((node = table_find(expiries, contract)))
			expiry = table_node_double(node);
		else {
			char *month;
			struct timeval tv;
			int diff;

			xcb_log(XCB_LOG_WARNING, "No exact expiry date for '%s'", contract);
			month = spotname + dstr_length(spotname) - 2;
			gettimeofday(&tv, NULL);
			if ((diff = atoi(month) - localtime_r(&tv.tv_sec, &lt)->tm_mon - 1) < 0)
				diff += 12;
			if (diff == 0)
				diff = 1;
			expiry = diff / 12.0;
		}
		/* FIXME: last price */
		if (fabs(last) <= 0.000001)
			vol = NAN;
		else
			vol = impv_baw(spot, atof(strike), r, r, expiry, last,
				!strcasecmp(type, "C") ? AMER_CALL : AMER_PUT);
		/* FIXME: bid price 1 */
		if (fabs(quote->thyquote.m_dMRJG1) <= 0.000001)
			vol2 = NAN;
		else if (fabs(quote->thyquote.m_dMRJG1 - last) <= 0.000001)
			vol2 = vol;
		else
			vol2 = impv_baw(spot, atof(strike), r, r, expiry, quote->thyquote.m_dMRJG1,
				!strcasecmp(type, "C") ? AMER_CALL : AMER_PUT);
		/* FIXME: ask price 1 */
		if (fabs(quote->thyquote.m_dMCJG1) <= 0.000001)
			vol3 = NAN;
		else if (fabs(quote->thyquote.m_dMCJG1 - last) <= 0.000001)
			vol3 = vol;
		else
			vol3 = impv_baw(spot, atof(strike), r, r, expiry, quote->thyquote.m_dMCJG1,
				!strcasecmp(type, "C") ? AMER_CALL : AMER_PUT);
		if ((res = ALLOC(512))) {
			time_t t = (time_t)quote->thyquote.m_nTime;
			char datestr[64];

			strftime(datestr, sizeof datestr, "%F %T", localtime_r(&t, &lt));
			snprintf(res, 512, "IMPVBAW,%s.%03d,%s|%.2f,%f,%.2f,%f,%.2f,%f,%.2f",
				datestr,
				quote->m_nMSec,
				contract,
				last,
				vol,
				quote->thyquote.m_dMRJG1,
				vol2,
				quote->thyquote.m_dMCJG1,
				vol3,
				spot);
			out2rmp(res);
			snprintf(res, 512, "IMPVBAW,%d,%d,%s,%.2f,%f,%.2f,%f,%.2f,%f,%.2f,%s,%s,%s,%f,%f,%d,0,0",
				quote->thyquote.m_nTime,
				quote->m_nMSec,
				contract,
				last,
				vol,
				quote->thyquote.m_dMRJG1,
				vol2,
				quote->thyquote.m_dMCJG1,
				vol3,
				spot,
				spotname,
				type,
				strike,
				r,
				expiry,
				quote->thyquote.m_nCJSL);
			if (out2msgs(res, out) == -1)
				FREE(res);
		} else
			xcb_log(XCB_LOG_WARNING, "Error allocating memory for result");
		dstr_free(strike);
		dstr_free(type);
		dstr_free(spotname);
	/* future quote */
	} else {
		table_rwlock_wrlock(spots);
		if ((node = table_find(spots, contract)) == NULL) {
			if ((node = table_insert_raw(spots, contract)))
				table_set_float(node, last);
		} else {
			table_set_float(node, last);
			dstr_free(contract);
		}
		table_rwlock_unlock(spots);
		return 0;
	}

end:
	dstr_free(contract);
	return 0;
}
Esempio n. 13
0
static int mm_impvbaw_exec(void *data, void *data2) {
	RAII_VAR(struct msg *, msg, (struct msg *)data, msg_decr);
	dstr *fields = NULL;
	int nfield = 0;
	time_t t;
	dstr spotname;
	double vol;
	NOT_USED(data2);

	fields = dstr_split_len(msg->data, strlen(msg->data), ",", 1, &nfield);
	/* FIXME */
	if (nfield != 19) {
		xcb_log(XCB_LOG_WARNING, "Message '%s' garbled", msg->data);
		goto end;
	}
	t        = (time_t)atoi(fields[1]);
	spotname = dstr_new(fields[11]);
	vol      = atof(fields[5]);
	if (!isnan(vol)) {
		struct mm *mm;
		int flag = 0;

		table_lock(contracts);
		if ((mm = table_get_value(contracts, spotname)) == NULL) {
			if (NEW(mm)) {
				mm->min = mm->max = vol;
				flag = 1;
				table_insert(contracts, spotname, mm);
			} else
				xcb_log(XCB_LOG_WARNING, "Error allocating memory for mm");
		} else {
			if (mm->min > vol) {
				mm->min = vol;
				flag = 1;
			} else if (mm->max < vol) {
				mm->max = vol;
				flag = 1;
			}
			dstr_free(spotname);
		}
		if (mm && flag) {
			struct tm lt;
			char datestr[64], res[512];

			localtime_r(&t, &lt);
			lt.tm_hour = lt.tm_min = lt.tm_sec = 0;
			strftime(datestr, sizeof datestr, "%F %T", &lt);
			snprintf(res, sizeof res, "MM_IMPVBAW,%s.000,%s|%f,%f",
				datestr,
				fields[11],
				mm->min,
				mm->max);
			out2rmp(res);
		}
		table_unlock(contracts);
	}

end:
	dstr_free_tokens(fields, nfield);
	return 0;
}
Esempio n. 14
0
static int vxo_exec(void *data, void *data2) {
	RAII_VAR(struct msg *, msg, (struct msg *)data, msg_decr);
	struct msgs *out = (struct msgs *)data2;
	dstr *fields = NULL;
	int nfield = 0;
	double vol, vol2, vol3, spot, strike, r, expiry;
	int sec, msec;
	dstr spotname;
	char *type;
	struct pd *pd;
	struct scp *scp;

	fields = dstr_split_len(msg->data, strlen(msg->data), ",", 1, &nfield);
	/* FIXME */
	if (nfield != 19) {
		xcb_log(XCB_LOG_WARNING, "Message '%s' garbled", msg->data);
		goto end;
	}
	vol      = !strcasecmp(fields[5], "nan") ? NAN : atof(fields[5]);
	vol2     = !strcasecmp(fields[7], "nan") ? NAN : atof(fields[7]);
	vol3     = !strcasecmp(fields[9], "nan") ? NAN : atof(fields[9]);
	if (isnan(vol) && isnan(vol2) && isnan(vol3))
		goto end;
	sec      = atoi(fields[1]);
	msec     = atoi(fields[2]);
	spot     = atof(fields[10]);
	spotname = dstr_new(fields[11]);
	type     = fields[12];
	strike   = atof(fields[13]);
	r        = atof(fields[14]);
	expiry   = atof(fields[15]);
	table_lock(spots);
	if ((pd = table_get_value(spots, spotname)) == NULL) {
		/* can't happen */
		if (NEW(scp) == NULL) {
			xcb_log(XCB_LOG_WARNING, "Error allocating memory for scp");
			table_unlock(spots);
			goto end;
		}
		scp->strike = strike;
		if (!strcasecmp(type, "C")) {
			scp->cvol  = vol;
			scp->pvol  = NAN;
			scp->cvol2 = vol2;
			scp->pvol2 = NAN;
			scp->cvol3 = vol3;
			scp->pvol3 = NAN;
		} else {
			scp->cvol  = NAN;
			scp->pvol  = vol;
			scp->cvol2 = NAN;
			scp->pvol2 = vol2;
			scp->cvol3 = NAN;
			scp->pvol3 = vol3;
		}
		if (NEW(pd) == NULL) {
			xcb_log(XCB_LOG_WARNING, "Error allocating memory for pd");
			FREE(scp);
			table_unlock(spots);
			goto end;
		}
		pd->prevxo = pd->prevxo2 = pd->prevxo3 = NAN;
		pd->sep    = strchr(fields[3], '-') ? "-" : "";
		pd->dlist  = dlist_new(NULL, scpfree);
		dlist_insert_tail(pd->dlist, scp);
		table_insert(spots, spotname, pd);
	} else {
		dlist_iter_t iter = dlist_iter_new(pd->dlist, DLIST_START_HEAD);
		dlist_node_t node;

		while ((node = dlist_next(iter))) {
			scp = (struct scp *)dlist_node_value(node);
			if (scp->strike > strike || fabs(scp->strike - strike) <= 0.000001)
				break;
		}
		if (node && fabs(scp->strike - strike) <= 0.000001) {
			if (!strcasecmp(type, "C")) {
				scp->cvol  = vol;
				scp->cvol2 = vol2;
				scp->cvol3 = vol3;
			} else {
				scp->pvol  = vol;
				scp->pvol2 = vol2;
				scp->pvol3 = vol3;
			}
		} else {
			/* can't happen */
			if (NEW(scp) == NULL) {
				xcb_log(XCB_LOG_WARNING, "Error allocating memory for scp");
				table_unlock(spots);
				goto end;
			}
			scp->strike = strike;
			if (!strcasecmp(type, "C")) {
				scp->cvol  = vol;
				scp->pvol  = NAN;
				scp->cvol2 = vol2;
				scp->pvol2 = NAN;
				scp->cvol3 = vol3;
				scp->pvol3 = NAN;
			} else {
				scp->cvol  = NAN;
				scp->pvol  = vol;
				scp->cvol2 = NAN;
				scp->pvol2 = vol2;
				scp->cvol3 = NAN;
				scp->pvol3 = vol3;
			}
			if (node == NULL)
				dlist_insert_tail(pd->dlist, scp);
			else
				dlist_insert(pd->dlist, node, scp, 0);
		}
		dlist_iter_rewind_head(iter, pd->dlist);
		while ((node = dlist_next(iter))) {
			scp = (struct scp *)dlist_node_value(node);
			if (scp->strike > spot || fabs(scp->strike - spot) <= 0.000001)
				break;
		}
		dlist_iter_free(&iter);
		if (node && dlist_node_prev(node)) {
			struct scp *scp1 = (struct scp *)dlist_node_value(node);
			struct scp *scp2 = (struct scp *)dlist_node_value(dlist_node_prev(node));
			double vxo = NAN, vxo2 = NAN, vxo3 = NAN;
			int flag1, flag2, flag3;

			if (!isnan(scp1->cvol) && !isnan(scp1->pvol) && !isnan(scp2->cvol) && !isnan(scp2->pvol))
				vxo  = ((scp1->cvol + scp1->pvol) / 2) *
					(spot - scp2->strike) / (scp1->strike - scp2->strike) +
					((scp2->cvol + scp2->pvol) / 2) *
					(scp1->strike - spot) / (scp1->strike - scp2->strike);
			if (!isnan(scp1->cvol2) && !isnan(scp1->pvol2) && !isnan(scp2->cvol2) && !isnan(scp2->pvol2))
				vxo2 = ((scp1->cvol2 + scp1->pvol2) / 2) *
					(spot - scp2->strike) / (scp1->strike - scp2->strike) +
					((scp2->cvol2 + scp2->pvol2) / 2) *
					(scp1->strike - spot) / (scp1->strike - scp2->strike);
			if (!isnan(scp1->cvol3) && !isnan(scp1->pvol3) && !isnan(scp2->cvol3) && !isnan(scp2->pvol3))
				vxo3 = ((scp1->cvol3 + scp1->pvol3) / 2) *
					(spot - scp2->strike) / (scp1->strike - scp2->strike) +
					((scp2->cvol3 + scp2->pvol3) / 2) *
					(scp1->strike - spot) / (scp1->strike - scp2->strike);
			if ((flag1 = (isnan(pd->prevxo) && !isnan(vxo)) || (!isnan(pd->prevxo) && isnan(vxo)) ||
				(!isnan(pd->prevxo) && !isnan(vxo) && fabs(pd->prevxo - vxo) > 0.000001) ? 1 : 0))
				pd->prevxo  = vxo;
			if ((flag2 = (isnan(pd->prevxo2) && !isnan(vxo2)) || (!isnan(pd->prevxo2) && isnan(vxo2)) ||
				(!isnan(pd->prevxo2) && !isnan(vxo2) && fabs(pd->prevxo2 - vxo2) > 0.000001) ? 1 : 0))
				pd->prevxo2 = vxo2;
			if ((flag3 = (isnan(pd->prevxo3) && !isnan(vxo3)) || (!isnan(pd->prevxo3) && isnan(vxo3)) ||
				(!isnan(pd->prevxo3) && !isnan(vxo3) && fabs(pd->prevxo3 - vxo3) > 0.000001) ? 1 : 0))
				pd->prevxo3 = vxo3;
			if (flag1 || flag2 || flag3) {
				char *res;

				if ((res = ALLOC(4096))) {
					time_t t = (time_t)sec;
					struct tm lt;
					char datestr[64];
					int off;

					strftime(datestr, sizeof datestr, "%F %T", localtime_r(&t, &lt));
					snprintf(res, 4096, "VXO,%s.%03d,%s|%f,%f,%f",
						datestr,
						msec,
						spotname,
						vxo,
						vxo2,
						vxo3);
					out2rmp(res);
					off = snprintf(res, 4096, "VXO,%d,%d,%s,",
						sec,
						msec,
						spotname);
					iter = dlist_iter_new(pd->dlist, DLIST_START_HEAD);
					while ((node = dlist_next(iter))) {
						scp = (struct scp *)dlist_node_value(node);
						off += snprintf(res + off, 4096 - off, "%.f,%f,%f,%f,",
							scp->strike,
							vxo,
							vxo2,
							vxo3);
					}
					dlist_iter_free(&iter);
					snprintf(res + off, 4096 - off, "%.2f,%f,%f,%s",
						spot,
						r,
						expiry,
						pd->sep);
					if (out2msgs(res, out) == -1)
						FREE(res);
				}
			}
		}
		dstr_free(spotname);
	}
	table_unlock(spots);

end:
	dstr_free_tokens(fields, nfield);
	return 0;
}
Esempio n. 15
0
/* FIXME */
void q_command(client c) {
	struct tm tm;
	char *end;
	char buf[64];
	dstr rid;
	struct crss *crss;

	if (!persistence) {
		add_reply_error(c, "database not open\r\n");
		return;
	}
	if (dstr_length(c->argv[0]) == 1) {
		add_reply_error(c, "index can't be empty\r\n");
		return;
	}
	if ((end = strptime(c->argv[3], "%F %T", &tm)) && *end == '\0')
		c->argv[3] = dstr_cat(c->argv[3], ".000");
	else if ((end = strptime(c->argv[3], "%F %R", &tm)) && *end == '\0')
		c->argv[3] = dstr_cat(c->argv[3], ":00.000");
	else {
		add_reply_error(c, "invalid time format, "
			"please use 'YYYY-mm-dd HH:MM:SS' or 'YYYY-mm-dd HH:MM'.\r\n");
		return;
	}
	if ((end = strptime(c->argv[4], "%F %T", &tm)) && *end == '\0')
		c->argv[4] = dstr_cat(c->argv[4], ".999");
	else if ((end = strptime(c->argv[4], "%F %R", &tm)) && *end == '\0')
		c->argv[4] = dstr_cat(c->argv[4], ":59.999");
	else {
		add_reply_error(c, "invalid time format, "
			"please use 'YYYY-mm-dd HH:MM:SS' or 'YYYY-mm-dd HH:MM'.\r\n");
		return;
	}
	snprintf(buf, sizeof buf, "%p,", c);
	rid = dstr_new(buf);
	rid = dstr_cat(rid, c->argv[1]);
	if (rids == NULL)
		rids = table_new(cmpstr, hashmurmur2, kfree, NULL);
	table_lock(rids);
	if ((crss = table_get_value(rids, rid))) {
		add_reply_error_format(c, "query with rid '%s' already exists\r\n", c->argv[1]);
		dstr_free(rid);
	} else if (NEW(crss)) {
		pthread_t thread;

		crss->c      = c;
		crss->rid    = dstr_new(c->argv[1]);
		crss->match  = dstr_new(c->argv[2]);
		crss->start  = dstr_new(c->argv[0] + 1);
		crss->start  = dstr_cat(crss->start, ",");
		crss->start  = dstr_cat(crss->start, c->argv[3]);
		crss->stop   = dstr_new(c->argv[0] + 1);
		crss->stop   = dstr_cat(crss->stop, ",");
		crss->stop   = dstr_cat(crss->stop, c->argv[4]);
		crss->cancel = 0;
		if (pthread_create(&thread, NULL, q_thread, crss) != 0) {
			add_reply_error(c, strerror(errno));
			add_reply_string(c, "\r\n", 2);
			dstr_free(crss->rid);
			dstr_free(crss->match);
			dstr_free(crss->start);
			dstr_free(crss->stop);
			FREE(crss);
			dstr_free(rid);
		} else {
			client_incr(crss->c);
			table_insert(rids, rid, crss);
		}
	} else {
		add_reply_error(c, "error allocating memory for crss\r\n");
		dstr_free(rid);
	}
	table_unlock(rids);
}
Esempio n. 16
0
File: table.c Progetto: def44/mate
int table_remove(struct table *t, struct object *k) {
	int hash, n, old_value;
	struct table_entry *r, *s;

	if (t == NULL) {
		fprintf(stderr, "mvm: table not initialized!\n");
		mvm_halt();
	}
	
#ifdef DMP
	if (t->dmp != NULL)
		table_dmp_load(t->dmp);
#endif

	/* lock */
	table_wrlock(t);

	/* check if iterator is running */
	if (t->iterator_is_running == 1) {
		fprintf(stderr, "mvm: cannot remove, Table's iterator is running!\n");
		mvm_halt();
	}

	hash = abs(table_run_hash_code(t, k));

#ifdef DMP
	if (t->dmp != NULL)
		table_dmp_load(t->dmp);
#endif
	
	n = hash % t->current_capacity;
	old_value = 0;

	for (s = NULL, r = t->buckets[n]; r != NULL; s = r, r = r->next) {
		if (table_run_equals(t, k, heap_fetch_object(heap, r->key)) == 1) {
#ifdef DMP
			if (t->dmp != NULL)
				table_dmp_load(t->dmp);
#endif
			
			old_value = r->value;
			break;
		}

#ifdef DMP
		if (t->dmp != NULL)
			table_dmp_load(t->dmp);
#endif
	}

	if (r == NULL) {
		/* unlock */
		table_unlock(t);
		return 0;
	}

#ifdef DMP
	if (t->dmp != NULL)
		table_dmp_store(t->dmp);
#endif

	if (r == t->buckets[n])
		t->buckets[n] = r->next;
	else
		s->next = r->next;

	table_entry_destroy(r);

	t->num_entries--;

	/* unlock */
	table_unlock(t);

	return old_value;
}
Esempio n. 17
0
/* FIXME */
static void *q_thread(void *data) {
	struct crss *crss = (struct crss *)data;
	char buf[64];
	dstr rid, res = NULL;
	db_iterator_t *it;
	const char *key, *value;
	size_t klen, vlen;

	snprintf(buf, sizeof buf, "%p,", crss->c);
	rid = dstr_new(buf);
	rid = dstr_cat(rid, crss->rid);
	it = db_iterator_create(db, db_ro);
	db_iterator_seek(it, crss->start, dstr_length(crss->start));
	/* seek failed */
	if (!db_iterator_valid(it)) {
		res = dstr_new(crss->rid);
		res = dstr_cat(res, ",1\r\n\r\n");
		pthread_spin_lock(&crss->c->lock);
		if (net_try_write(crss->c->fd, res, dstr_length(res), 10, NET_NONBLOCK) == -1)
			xcb_log(XCB_LOG_WARNING, "Writing to client '%p': %s", crss->c, strerror(errno));
		pthread_spin_unlock(&crss->c->lock);
		goto end;
	}
	key = db_iterator_key(it, &klen);
	while (memcmp(key, crss->stop, dstr_length(crss->stop)) <= 0) {
		if (crss->cancel)
			break;
		if (!strcmp(crss->match, "") || strstr(key, crss->match)) {
			value = db_iterator_value(it, &vlen);
			res = dstr_new(crss->rid);
			res = dstr_cat(res, ",0,");
			res = dstr_cat_len(res, key, klen);
			res = dstr_cat(res, ",");
			res = dstr_cat_len(res, value, vlen);
			res = dstr_cat(res, "\r\n");
			pthread_spin_lock(&crss->c->lock);
			if (net_try_write(crss->c->fd, res, dstr_length(res), 10, NET_NONBLOCK) == -1) {
				xcb_log(XCB_LOG_WARNING, "Writing to client '%p': %s",
					crss->c, strerror(errno));
				pthread_spin_unlock(&crss->c->lock);
				goto end;
			}
			pthread_spin_unlock(&crss->c->lock);
			dstr_free(res);
		}
		db_iterator_next(it);
		if (!db_iterator_valid(it) || crss->cancel)
			break;
		key = db_iterator_key(it, &klen);
	}
	res = dstr_new(crss->rid);
	res = dstr_cat(res, ",1\r\n\r\n");
	pthread_spin_lock(&crss->c->lock);
	if (net_try_write(crss->c->fd, res, dstr_length(res), 10, NET_NONBLOCK) == -1)
		xcb_log(XCB_LOG_WARNING, "Writing to client '%p': %s", crss->c, strerror(errno));
	pthread_spin_unlock(&crss->c->lock);

end:
	db_iterator_destroy(&it);
	dstr_free(res);
	table_lock(rids);
	table_remove(rids, rid);
	table_unlock(rids);
	dstr_free(rid);
	client_decr(crss->c);
	dstr_free(crss->rid);
	dstr_free(crss->match);
	dstr_free(crss->start);
	dstr_free(crss->stop);
	FREE(crss);
	return NULL;
}