Exemplo n.º 1
0
BOOL togo_m_cache_replace(TOGO_THREAD_ITEM * socket_item, u_char * key,
		uint32_t expires, uint32_t vlen)
{
	TOGO_M_CACHE_ITEM * item;
	TOGO_HASHTABLE_ITEM * hitem;

	hitem = togo_hashtable_get(togo_m_cache_hashtable, key);
	if (hitem == NULL) {

		/* Skip to read the value!*/
		togo_read_skip(socket_item, vlen);

		togo_send_data(socket_item, TOGO_SBUF_NOT_EXIST,
				togo_strlen(TOGO_SBUF_NOT_EXIST));
		return TRUE;

	}

	item = (TOGO_M_CACHE_ITEM *) hitem->p;

	if (item->expires != 0 && item->expires < togo_get_time()) {

		/* Skip to read the value!*/
		togo_read_skip(socket_item, vlen);

		togo_m_cache_delete_comm(item);
		togo_send_data(socket_item, TOGO_SBUF_NOT_EXIST,
				togo_strlen(TOGO_SBUF_NOT_EXIST));
		return TRUE;
	}

	togo_m_cache_delete_comm(item);

	return togo_m_cache_set_comm(socket_item, key, expires, vlen);
}
Exemplo n.º 2
0
BOOL togo_m_cache_get(TOGO_THREAD_ITEM * socket_item, u_char * key)
{
	TOGO_M_CACHE_ITEM * item;
	TOGO_HASHTABLE_ITEM * hitem;
	uint32_t vlen;
	void * buf;

	if (togo_m_cache->is_flush == TRUE) {
		return FALSE;
	}
	hitem = togo_hashtable_get(togo_m_cache_hashtable, key);
	if (hitem == NULL) {
		togo_send_data(socket_item, TOGO_SBUF_NOT_EXIST,
				togo_strlen(TOGO_SBUF_NOT_EXIST));
		return TRUE;
	}

	item = (TOGO_M_CACHE_ITEM *) hitem->p;
	if (item->expires != 0 && item->expires < togo_get_time()) {
		togo_m_cache_delete_comm(item);
		togo_send_data(socket_item, TOGO_SBUF_NOT_EXIST,
				togo_strlen(TOGO_SBUF_NOT_EXIST));
		return TRUE;
	}

	buf = togo_m_cache_get_val_addr(item, item->klen);
	vlen = item->vlen;

	togo_send_dbig(socket_item, buf, vlen, togo_m_cache_get_cb, (void *) item);

	return TRUE;

}
Exemplo n.º 3
0
BOOL togo_m_queue_rpop(u_char * name, TOGO_THREAD_ITEM *socket_item)
{
	TOGO_M_QUEUE_ITEM * item;
	TOGO_M_QUEUE_BLOCK * block;
	TOGO_M_QUEUE * queue;
	TOGO_M_QUEUE_ITEM * pi;

	queue = togo_m_queue_get(name);
	if (queue == NULL) {
		return FALSE;
	}

	pthread_mutex_lock(&queue->qlock);

	/* Search the priority list */
	pi = togo_m_queue_pi_get(queue);
	if (pi != NULL) {
		item = pi;
	} else {
		if (queue->tail == NULL) {
			queue->head = NULL;
			pthread_mutex_unlock(&queue->qlock);
			togo_send_null(socket_item); /* Send "TOGO_NULL"*/
			return TRUE;
		}

		item = queue->tail;
		if (item->prev != NULL) {
			item->prev->next = NULL;
		}
		queue->tail = item->prev;
	}

	queue->total_elt--;
	queue->total_hit++;
	queue->total_read++;
	block = item->block;
	block->nelt--;

	if (queue->total_elt == 0) {
		queue->tail = NULL;
		queue->head = NULL;
	}

	togo_send_data(socket_item, item->val, item->size);

	if (block->nelt == 0) {
		togo_m_queue_block_free(block, queue->pool, queue);
		queue->total_block--;
		queue->total_size -= togo_m_queue_block_size();
	}

	pthread_mutex_unlock(&queue->qlock);
	return TRUE;
}
Exemplo n.º 4
0
BOOL togo_m_cache_delete(TOGO_THREAD_ITEM * socket_item, u_char * key)
{
	TOGO_M_CACHE_ITEM * item;
	TOGO_HASHTABLE_ITEM * hitem;

	hitem = togo_hashtable_get(togo_m_cache_hashtable, key);
	if (hitem == NULL) {

		togo_send_data(socket_item, TOGO_SBUF_NOT_EXIST,
				togo_strlen(TOGO_SBUF_NOT_EXIST));
		return TRUE;
	}

	item = (TOGO_M_CACHE_ITEM *) hitem->p;
	return togo_m_cache_delete_comm(item);
}
Exemplo n.º 5
0
BOOL togo_m_queue_count(u_char * name, TOGO_THREAD_ITEM * socket_item)
{
	uint32_t count;
	TOGO_M_QUEUE * queue;
	size_t str_size;

	queue = togo_m_queue_get(name);
	count = (queue == NULL) ? 0 : queue->total_elt;

	u_char * str = (u_char *) togo_pool_alloc(queue->pool, 12);
	togo_itoa(count, str, 10);

	togo_send_data(socket_item, str, togo_strlen(str));
	togo_pool_free_data(queue->pool, (void *) str);
	return TRUE;
}
Exemplo n.º 6
0
BOOL togo_m_queue_status(u_char * name, TOGO_THREAD_ITEM * socket_item)
{
	TOGO_M_QUEUE * queue;
	TOGO_STRING * togo_str;

	queue = togo_m_queue_get(name);
	if (queue == NULL) {
		return FALSE;
	}
	pthread_mutex_lock(&queue->qlock);

	togo_str = togo_string_init(queue->pool, TOGO_STRING_DEFAULT_SIZE);
	if (togo_str == NULL) {
		return FALSE;
	}

	togo_string_append_s(&togo_str, "total_elt:");
	togo_string_append_i(&togo_str, queue->total_elt);
	togo_string_append_s(&togo_str, ";total_block:");
	togo_string_append_i(&togo_str, queue->total_block);
	togo_string_append_s(&togo_str, ";total_hit:");
	togo_string_append_i(&togo_str, queue->total_hit);
	togo_string_append_s(&togo_str, ";total_write:");
	togo_string_append_i(&togo_str, queue->total_write);
	togo_string_append_s(&togo_str, ";total_read:");
	togo_string_append_i(&togo_str, queue->total_read);
	togo_string_append_s(&togo_str, ";total_size:");
	togo_string_append_i(&togo_str, queue->total_size);

	togo_send_data(socket_item, togo_str->buf, togo_str->str_size);

	togo_string_destroy(togo_str);

	pthread_mutex_unlock(&queue->qlock);

	return TRUE;
}
Exemplo n.º 7
0
void togo_dispatcher(TOGO_COMMAND_TAG command_tag[],
		TOGO_THREAD_ITEM *socket_item, int ntag)
{
	BOOL ret = FALSE;

	if (strcmp(command_tag[0].value, "VERSION") == 0) {

		togo_send_data(socket_item, TOGO_VERSION, togo_strlen(TOGO_VERSION));
		ret = TRUE;

	} else if (strcmp(command_tag[0].value, "QUIT") == 0) {

		togo_server_disconnect(socket_item);
		ret = TRUE;
	} else {

		/**
		 * command_tag[0] : Module
		 * command_tag[1] : Action
		 * command_tag[2] : Object
		 * command_tag[3] : Value
		 * command_tag[4] : Option
		 */
		if (ntag < 3 || command_tag[0].value == NULL) {
			ret = FALSE;

		} else if (strcmp(command_tag[0].value, "QUEUE") == 0) {

			/**
			 * command_tag[0] : Module  QUEUE
			 * command_tag[1] : Action  RPUSH|LPUSH|LPOP|RPOP|COUNT|STATUS
			 * command_tag[2] : Object  Queue name
			 * command_tag[3] : Value   value
			 * command_tag[4] : Option  priority:1|2|3
			 */
			ret = togo_m_queue_command(command_tag, socket_item, ntag);

		} else if (strcmp(command_tag[0].value, "COUNTER") == 0) {

			/**
			 * command_tag[0] : Module  COUNTER
			 * command_tag[1] : Action  PLUS|MINUS|GET|RESET
			 * command_tag[2] : Object  Count name
			 * command_tag[3] : Step    1  MAX:99999999
			 */
			ret = togo_m_counter_command(command_tag, socket_item, ntag);

		} else if (strcmp(command_tag[0].value, "LOCK") == 0) {

			/**
			 * command_tag[0] : Module  LOCK
			 * command_tag[1] : Action  LOCK|UNLOCK|STATUS
			 * command_tag[2] : Object  Count name
			 */
			ret = togo_m_lock_command(command_tag, socket_item, ntag);
		}
	}

	/* If fail, We will return "TOGO_FAIL"! */
	if (ret == FALSE) {
		togo_send_fail(socket_item);
	}
}