Exemplo n.º 1
0
void elliptics_service_t::on_bulk_read_completed(deferred<std::map<std::string, std::string> > promise,
	const key_name_map &keys,
	const ioremap::elliptics::sync_read_result &result,
	const ioremap::elliptics::error_info &error)
{
	if (error) {
		promise.abort(-error.code(), error.message());
	} else {
		std::map<std::string, std::string> read_result;

		for (size_t i = 0; i < result.size(); ++i) {
			const auto &entry = result[i];
			const auto &id = reinterpret_cast<const dnet_raw_id &>(entry.command()->id);

			auto it = keys.find(id);

			if (it == keys.end()) {
				continue;
			}

			read_result[it->second] = entry.file().to_string();
		}

		promise.write(read_result);
	}
}
Exemplo n.º 2
0
void elliptics_service_t::on_remove_completed(deferred<void> promise,
	const ioremap::elliptics::sync_remove_result &,
	const ioremap::elliptics::error_info &error)
{
	if (error) {
		promise.abort(-error.code(), error.message());
	} else {
		promise.close();
	}
}
Exemplo n.º 3
0
void elliptics_service_t::on_read_completed(deferred<std::string> promise,
	const ioremap::elliptics::sync_read_result &result,
	const ioremap::elliptics::error_info &error)
{
	if (error) {
		promise.abort(-error.code(), error.message());
	} else {
		promise.write(result[0].file().to_string());
	}
}
Exemplo n.º 4
0
void elliptics_service_t::on_find_completed(deferred<std::vector<std::string> > promise,
	const ioremap::elliptics::sync_find_indexes_result &result,
	const ioremap::elliptics::error_info &error)
{
	if (error) {
		promise.abort(-error.code(), error.message());
	} else {
		promise.write(storage::elliptics_storage_t::convert_list_result(result));
	}
}
Exemplo n.º 5
0
		void on_search_finished(wookie::find_result &fobj, const ioremap::elliptics::error_info &err) {
			if (err) {
				log(ioremap::swarm::LOG_ERROR, "Failed to search: %s", err.message().c_str());
				send_reply(ioremap::swarm::url_fetcher::response::service_unavailable);
				return;
			}

			ioremap::thevoid::elliptics::JsonValue result_object;

			rapidjson::Value indexes;
			indexes.SetArray();

			auto ids = fobj.results_array();
			for (size_t i = 0; i < ids.size(); ++i) {
				char id_str[2 * DNET_ID_SIZE + 1];
				dnet_dump_id_len_raw(ids[i].id, DNET_ID_SIZE, id_str);
				rapidjson::Value index(id_str, result_object.GetAllocator());
				indexes.PushBack(index, result_object.GetAllocator());
			}

			result_object.AddMember("result", indexes, result_object.GetAllocator());

			auto data = result_object.ToString();

			swarm::url_fetcher::response reply;
			reply.set_code(ioremap::swarm::url_fetcher::response::ok);
			reply.headers().set_content_length(data.size());

			send_reply(std::move(reply), std::move(data));
		}
Exemplo n.º 6
0
	virtual void on_read_finished(const ioremap::elliptics::sync_read_result &result,
			const ioremap::elliptics::error_info &error) {
		if (error.code() == -ENOENT) {
			this->send_reply(swarm::url_fetcher::response::not_found);
			return;
		} else if (error) {
			this->send_reply(swarm::url_fetcher::response::service_unavailable);
			return;
		}

		const ioremap::elliptics::read_result_entry &entry = result[0];

		document doc = storage::unpack_document(entry.file());

		const swarm::http_request &request = this->request();

		if (auto modified_since = request.headers().if_modified_since()) {
			if ((time_t)doc.ts.tsec <= *modified_since) {
				this->send_reply(swarm::url_fetcher::response::not_modified);
				return;
			}
		}

		swarm::url_fetcher::response reply;
		reply.set_code(swarm::url_fetcher::response::ok);
		reply.headers().set_content_length(doc.data.size());
		reply.headers().set_content_type("text/plain");
		reply.headers().set_last_modified(doc.ts.tsec);

		this->send_reply(std::move(reply), std::move(doc.data));
	}
Exemplo n.º 7
0
void proxy::req_get::on_read_chunk(const ioremap::elliptics::sync_read_result &srr, const ioremap::elliptics::error_info &error) {
	if (error) {
		BH_LOG(logger(), SWARM_LOG_ERROR
				, "Get %s %s: on_read_chunk: %s"
				, m_key.remote().c_str(), m_key.to_string().c_str(), error.message().c_str());
		HANDY_MDS_GET_REPLY(500);
		send_reply(500);
		return;
	}

	BH_LOG(logger(), SWARM_LOG_INFO
			, "Get %s %s: on_read_chunk: chunk was read"
			, m_key.remote().c_str(), m_key.to_string().c_str());

	const auto &rr = srr.front();
	auto file = rr.file();

	std::string data = file.to_string();

	if (m_first_chunk) {
		m_first_chunk = false;

		ioremap::thevoid::http_response reply;
		reply.set_code(200);
		reply.headers().set_content_length(m_size);

		if (NULL == server()->m_magic.get()) {
			server()->m_magic.reset(new magic_provider());
		}

		reply.headers().set_content_type(server()->m_magic->type(data));

		{
			time_t timestamp = (time_t)(rr.io_attribute()->timestamp.tsec);

			char ts_str[128] = {0};
			struct tm tmp;
			strftime(ts_str, sizeof (ts_str), "%a, %d %b %Y %T %Z", gmtime_r(&timestamp, &tmp));

			if (m_if_modified_since) {
				if (*m_if_modified_since == ts_str) {
					HANDY_MDS_GET_REPLY(304);
					send_reply(304);
					return;
				}
			}

			reply.headers().set_last_modified(ts_str);
		}

		HANDY_MDS_GET_REPLY(reply.code());
		send_headers(std::move(reply), std::function<void (const boost::system::error_code &)>());
	}

	send_data(std::move(data), std::bind(&proxy::req_get::on_sent_chunk, shared_from_this(), std::placeholders::_1));
	m_offset += file.size();
}
Exemplo n.º 8
0
void proxy::req_get::on_lookup(const ioremap::elliptics::sync_lookup_result &slr, const ioremap::elliptics::error_info &error) {
	if (error) {
		if (error.code() == -ENOENT) {
			BH_LOG(logger(), SWARM_LOG_INFO
					, "Get %s %s: on_lookup: file not found"
					, m_key.remote().c_str(), m_key.to_string().c_str());
			HANDY_MDS_GET_REPLY(404);
			send_reply(404);
		} else {
			BH_LOG(logger(), SWARM_LOG_ERROR
					, "Get %s %s: on_lookup: %s"
					, m_key.remote().c_str(), m_key.to_string().c_str(), error.message().c_str());
			HANDY_MDS_GET_REPLY(500);
			send_reply(500);
		}
		return;
	}
	const auto &entry = slr.front();
	auto total_size = entry.file_info()->size;

	if (m_offset >= total_size) {
		BH_LOG(logger(), SWARM_LOG_INFO
				, "Get %s %s: offset greater than total_size"
				, m_key.remote().c_str()
				, m_key.to_string().c_str());
		HANDY_MDS_GET_REPLY(400);
		send_reply(400);
		return;
	}

	total_size -= m_offset;

	if (m_size == 0 || m_size > total_size) {
		m_size = total_size;
	}

	std::vector<int> groups;
	for (auto it = slr.begin(), end = slr.end(); it != end; ++it) {
		groups.push_back(it->command()->id.group_id);
	}
	m_session->set_groups(groups);

	read_chunk();
}
Exemplo n.º 9
0
void proxy::req_delete::on_lookup(const ioremap::elliptics::sync_lookup_result &slr, const ioremap::elliptics::error_info &error) {

	if (error) {
		BH_LOG(logger(), SWARM_LOG_ERROR, "Delete request=\"%s\" lookup error: %s"
				, url_str.c_str(), error.message().c_str());
		HANDY_MDS_DELETE_REPLY(error.code() == -ENOENT ? 404 : 500);
		send_reply(error.code() == -ENOENT ? 404 : 500);
		return;
	}

	const auto &entry = slr.front();
	total_size = entry.file_info()->size;

	// all_with_ack because all doesn't mean all in some cases e.g. remove act
	session->set_filter(ioremap::elliptics::filters::all_with_ack);
	session->set_timeout(server()->timeout.remove);

	BH_LOG(logger(), SWARM_LOG_DEBUG, "Delete %s: data size %d"
			, url_str.c_str(), static_cast<int>(total_size));
	session->remove(key).connect(wrap(std::bind(&req_delete::on_finished, shared_from_this(), std::placeholders::_1, std::placeholders::_2)));
}
Exemplo n.º 10
0
	void request_complete(std::shared_ptr<request> req, const ioremap::elliptics::error_info &error)
	{
		if (error) {
			fprintf(stderr, "%s %d: queue request completion error: %s\n", dnet_dump_id(&req->id), req->src_key, error.message().c_str());
		} else {
			//fprintf(stderr, "%s %d: queue request completed\n", dnet_dump_id(&req->id), req->src_key);
		}

		--running_requests;
		condition.notify_one();
	}