Пример #1
0
/* Process a SETDB command. */
yerr_t command_setdb(tcp_thread_t *thread, ydynabin_t *buff) {
	unsigned char *pdbname_len, dbname_len;
	char *dbname = NULL;
	void *ptr;
	yerr_t result = YENOERR;

	YLOG_ADD(YLOG_DEBUG, "SETDB command");
	// read dbname length
	if (connection_read_data(thread, buff, sizeof(dbname_len)) != YENOERR)
		goto error;
	pdbname_len = ydynabin_forward(buff, sizeof(dbname_len));
	dbname_len = *pdbname_len;
	YFREE(thread->dbname);
	if (dbname_len > 0) {
		// read dbname
		if (connection_read_data(thread, buff, (size_t)dbname_len) != YENOERR)
			goto error;
		ptr = ydynabin_forward(buff, (size_t)dbname_len);
		if ((dbname = YMALLOC((size_t)dbname_len + 1)) == NULL)
			goto error;
		memcpy(dbname, ptr, (size_t)dbname_len);
		thread->dbname = dbname;
	}
	// send the response to the client
	YLOG_ADD(YLOG_DEBUG, "SETDB command OK");
	CONNECTION_SEND_OK(thread);
	return (result);
error:
	YLOG_ADD(YLOG_WARN, "SETDB error");
	CONNECTION_SEND_ERROR(thread, RESP_ERR_SERVER);
	return (YEIO);
}
Пример #2
0
/* Process a STOP command. */
yerr_t command_stop(tcp_thread_t *thread, ybool_t sync, ybool_t compress, ybool_t serialized, ydynabin_t *buff) {
	YLOG_ADD(YLOG_DEBUG, "STOP command");
	// check running transaction
	if (thread->transaction == NULL)
		goto error;
	// rollback transaction
	database_transaction_rollback(thread->transaction);
	thread->transaction = NULL;
	CONNECTION_SEND_OK(thread);
	return (YENOERR);
error:
	YLOG_ADD(YLOG_WARN, "STOP error");
	CONNECTION_SEND_ERROR(thread, RESP_ERR_TRANSACTION);
	return (YEACCESS);
}
Пример #3
0
/* Process a START command. */
yerr_t command_start(tcp_thread_t *thread, ybool_t sync, ybool_t compress, ybool_t serialized, ydynabin_t *buff) {
	YLOG_ADD(YLOG_DEBUG, "START command");
	// rollback previous transaction
	if (thread->transaction != NULL)
		database_transaction_rollback(thread->transaction);
	// open transaction
	thread->transaction = database_transaction_start(thread->finedb->database, YTRUE);
	if (thread->transaction == NULL)
		goto error;
	CONNECTION_SEND_OK(thread);
	return (YENOERR);
error:
	YLOG_ADD(YLOG_WARN, "START error");
	CONNECTION_SEND_ERROR(thread, RESP_ERR_TRANSACTION);
	return (YEACCESS);
}
Пример #4
0
/* Process a LIST command. */
yerr_t command_list(tcp_thread_t *thread, ybool_t sync, ybool_t compress, ybool_t serialized, ydynabin_t *buff) {
	char last_byte = 0;
	yerr_t result;

	YLOG_ADD(YLOG_DEBUG, "LIST command");
	// send the response to the client
	result = CONNECTION_SEND_OK(thread);
	if (result != YENOERR)
		goto error;
	// send data
	if (database_list(thread->finedb->database, thread->transaction, thread->dbname, _command_list_loop, thread) != YENOERR)
		goto error;
	// send last byte
	if (write(thread->fd, &last_byte, 1) != 1)
		goto error;
	YLOG_ADD(YLOG_DEBUG, "LIST command OK");
	return (result);
error:
	YLOG_ADD(YLOG_WARN, "LIST error");
	CONNECTION_SEND_ERROR(thread, RESP_ERR_SERVER);
	return (YEIO);
}
Пример #5
0
/* Process a DEL command. */
yerr_t command_del(tcp_thread_t *thread, ybool_t sync, ybool_t compress, ybool_t serialized, ydynabin_t *buff) {
	uint16_t *pkey_len, key_len;
	void *ptr, *key = NULL;
	writer_msg_t *msg = NULL;
	char answer;

	YLOG_ADD(YLOG_DEBUG, "DEL command");
	// read key length
	if (connection_read_data(thread, buff, sizeof(key_len)) != YENOERR)
		goto error;
	pkey_len = ydynabin_forward(buff, sizeof(key_len));
	key_len = ntohs(*pkey_len);
	// read key
	if (connection_read_data(thread, buff, (size_t)key_len) != YENOERR)
		goto error;
	ptr = ydynabin_forward(buff, (size_t)key_len);
	if ((key = YMALLOC((size_t)key_len)) == NULL)
		goto error;
	memcpy(key, ptr, (size_t)key_len);

	if (!sync) {
		// send the response
		connection_send_response(thread, RESP_OK, YFALSE, YFALSE, NULL, 0);
	}

	// creation of the message
	if ((msg = YMALLOC(sizeof(writer_msg_t))) == NULL)
		goto error;
	msg->type = WRITE_DEL;
	ybin_set(&msg->name, key, key_len);
	if (!sync) {
		msg->dbname = thread->dbname ? strdup(thread->dbname) : NULL;
		// send the message to the writer thread
		if (nn_send(thread->write_sock, &msg, sizeof(msg), 0) < 0) {
			YLOG_ADD(YLOG_WARN, "Unable to send message to writer thread.");
			goto error;
		}
		return (YENOERR);
	}
	// synchronized
	if (database_del(thread->finedb->database, thread->transaction, thread->dbname, msg->name) == YENOERR) {
		YLOG_ADD(YLOG_DEBUG, "Deletion done on database.");
		answer = 1;
	} else {
		YLOG_ADD(YLOG_WARN, "Unable to delete data on database.");
		answer = 0;
	}
	YFREE(key);
	YFREE(msg);
	YLOG_ADD(YLOG_DEBUG, "DEL command %s", (answer ? "OK" : "failed"));
	if (!sync)
		return (YENOERR);
	return (connection_send_response(thread, (answer ? RESP_OK : RESP_ERR_BAD_NAME),
	                                 YFALSE, YFALSE, NULL, 0));
error:
	YLOG_ADD(YLOG_WARN, "PUT error");
	YFREE(key);
	YFREE(msg);
	CONNECTION_SEND_ERROR(thread, RESP_ERR_SERVER);
	return (YEIO);
}