All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.arangodb.impl.InternalCursorDriverImpl Maven / Gradle / Ivy

There is a newer version: 7.15.0
Show newest version
/*
 * Copyright (C) 2012 tamtam180
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.arangodb.impl;

import java.util.Collections;
import java.util.Map;

import com.arangodb.ArangoConfigure;
import com.arangodb.ArangoException;
import com.arangodb.CursorRawResult;
import com.arangodb.CursorResult;
import com.arangodb.CursorResultSet;
import com.arangodb.DocumentCursorResult;
import com.arangodb.entity.CursorEntity;
import com.arangodb.entity.DefaultEntity;
import com.arangodb.entity.DocumentEntity;
import com.arangodb.entity.EntityFactory;
import com.arangodb.entity.QueriesResultEntity;
import com.arangodb.entity.QueryTrackingPropertiesEntity;
import com.arangodb.entity.ShortestPathEntity;
import com.arangodb.http.HttpManager;
import com.arangodb.http.HttpResponseEntity;
import com.arangodb.util.AqlQueryOptions;
import com.arangodb.util.MapBuilder;
import com.arangodb.util.ShortestPathOptions;
import com.google.gson.JsonObject;

/**
 * @author tamtam180 - kirscheless at gmail.com
 */
public class InternalCursorDriverImpl extends BaseArangoDriverImpl implements com.arangodb.InternalCursorDriver {

	InternalCursorDriverImpl(ArangoConfigure configure, HttpManager httpManager) {
		super(configure, httpManager);
	}

	@Override
	public CursorEntity validateQuery(String database, String query) throws ArangoException {
		HttpResponseEntity res = httpManager.doPost(createEndpointUrl(database, "/_api/query"), null,
			EntityFactory.toJsonString(new MapBuilder("query", query).get()));

		return createEntity(res, CursorEntity.class);
	}

	@Override
	public String executeAqlQueryJSON(
		String database,
		String query,
		Map bindVars,
		AqlQueryOptions aqlQueryOptions) throws ArangoException {

		return getJSONResponseText(getCursor(database, query, bindVars, aqlQueryOptions));
	}

	@SuppressWarnings("unchecked")
	@Override
	public  CursorEntity executeCursorEntityQuery(
		String database,
		String query,
		Map bindVars,
		AqlQueryOptions aqlQueryOptions,
		Class... clazz) throws ArangoException {

		HttpResponseEntity res = getCursor(database, query, bindVars, aqlQueryOptions);

		return createEntity(res, CursorEntity.class, clazz);
	}

	private HttpResponseEntity getCursor(
		String database,
		String query,
		Map bindVars,
		AqlQueryOptions aqlQueryOptions) throws ArangoException {

		Map map = aqlQueryOptions.toMap();
		map.put("query", query);
		map.put("bindVars", bindVars == null ? Collections.emptyMap() : bindVars);

		return httpManager.doPost(createEndpointUrl(database, "/_api/cursor"), null, EntityFactory.toJsonString(map));
	}

	@SuppressWarnings("unchecked")
	@Override
	public  CursorEntity continueQuery(String database, long cursorId, Class... clazz) throws ArangoException {

		HttpResponseEntity res = httpManager.doPut(createEndpointUrl(database, "/_api/cursor", cursorId), null, null);

		return createEntity(res, CursorEntity.class, clazz);
	}

	@Override
	public DefaultEntity finishQuery(String database, long cursorId) throws ArangoException {
		HttpResponseEntity res = httpManager.doDelete(createEndpointUrl(database, "/_api/cursor/", cursorId), null);

		try {
			return createEntity(res, DefaultEntity.class);
		} catch (ArangoException e) {
			// TODO Mode
			if (e.getErrorNumber() == 1600) {
				// 既に削除されている
				return (DefaultEntity) e.getEntity();
			}
			throw e;
		}
	}

	@Override
	public > DocumentCursorResult executeBaseCursorQuery(
		String database,
		String query,
		Map bindVars,
		AqlQueryOptions aqlQueryOptions,
		Class classDocumentEntity,
		Class clazz) throws ArangoException {

		CursorEntity entity = executeCursorEntityQuery(database, query, bindVars, aqlQueryOptions,
			classDocumentEntity, clazz);

		return new DocumentCursorResult(database, this, entity, classDocumentEntity, clazz);
	}

	@Override
	public  CursorResult executeAqlQuery(
		String database,
		String query,
		Map bindVars,
		AqlQueryOptions aqlQueryOptions,
		Class clazz) throws ArangoException {

		CursorEntity entity = executeCursorEntityQuery(database, query, bindVars, aqlQueryOptions, clazz);

		return new CursorResult(database, this, entity, clazz);
	}

	@Override
	public CursorRawResult executeAqlQueryRaw(
		String database,
		String query,
		Map bindVars,
		AqlQueryOptions aqlQueryOptions) throws ArangoException {

		CursorEntity entity = executeCursorEntityQuery(database, query, bindVars, aqlQueryOptions,
			JsonObject.class);

		return new CursorRawResult(database, this, entity);
	}

	@SuppressWarnings("unchecked")
	@Override
	public  ShortestPathEntity getShortestPath(
		String database,
		String graphName,
		Object startVertexExample,
		Object endVertexExample,
		ShortestPathOptions shortestPathOptions,
		AqlQueryOptions aqlQueryOptions,
		Class vertexClass,
		Class edgeClass) throws ArangoException {

		validateCollectionName(graphName);

		String query = "for i in graph_shortest_path(@graphName, @startVertexExample, @endVertexExample, @options) return i";

		Map options = shortestPathOptions == null ? new MapBuilder().get()
				: shortestPathOptions.toMap();

		Map bindVars = new MapBuilder().put("graphName", graphName)
				.put("startVertexExample", startVertexExample).put("endVertexExample", endVertexExample)
				.put("options", options).get();

		HttpResponseEntity res = getCursor(database, query, bindVars, aqlQueryOptions);

		return createEntity(res, ShortestPathEntity.class, vertexClass, edgeClass);
	}

	@Deprecated
	@SuppressWarnings("unchecked")
	@Override
	public  CursorEntity executeQuery(
		String database,
		String query,
		Map bindVars,
		Class clazz,
		Boolean calcCount,
		Integer batchSize,
		Boolean fullCount) throws ArangoException {

		AqlQueryOptions aqlQueryOptions = new AqlQueryOptions().setCount(calcCount).setBatchSize(batchSize)
				.setFullCount(fullCount);

		HttpResponseEntity res = getCursor(database, query, bindVars, aqlQueryOptions);
		return createEntity(res, CursorEntity.class, clazz);
	}

	@Deprecated
	@Override
	public  CursorResultSet executeQueryWithResultSet(
		String database,
		String query,
		Map bindVars,
		Class clazz,
		Boolean calcCount,
		Integer batchSize,
		Boolean fullCount) throws ArangoException {

		CursorEntity entity = executeQuery(database, query, bindVars, clazz, calcCount, batchSize, fullCount);
		return new CursorResultSet(database, this, entity, clazz);
	}

	@Deprecated
	@Override
	public  CursorResultSet executeQueryWithResultSet(
		String database,
		String query,
		Map bindVars,
		Class clazz,
		Boolean calcCount,
		Integer batchSize) throws ArangoException {

		CursorEntity entity = executeQuery(database, query, bindVars, clazz, calcCount, batchSize, false);
		return new CursorResultSet(database, this, entity, clazz);
	}

	@Override
	public QueryTrackingPropertiesEntity getQueryTrackingProperties(String database) throws ArangoException {

		HttpResponseEntity res = httpManager.doGet(createEndpointUrl(database, "/_api/query/properties"), null, null);

		return createEntity(res, QueryTrackingPropertiesEntity.class);
	}

	@Override
	public QueryTrackingPropertiesEntity setQueryTrackingProperties(
		String database,
		QueryTrackingPropertiesEntity properties) throws ArangoException {
		HttpResponseEntity res = httpManager.doPut(createEndpointUrl(database, "/_api/query/properties"), null,
			EntityFactory.toJsonString(properties));

		return createEntity(res, QueryTrackingPropertiesEntity.class);
	}

	@Override
	public QueriesResultEntity getCurrentlyRunningQueries(String database) throws ArangoException {
		HttpResponseEntity res = httpManager.doGet(createEndpointUrl(database, "/_api/query/current"), null, null);

		return createEntity(res, QueriesResultEntity.class);
	}

	@Override
	public QueriesResultEntity getSlowQueries(String database) throws ArangoException {
		HttpResponseEntity res = httpManager.doGet(createEndpointUrl(database, "/_api/query/slow"), null, null);

		return createEntity(res, QueriesResultEntity.class);
	}

	@Override
	public DefaultEntity deleteSlowQueries(String database) throws ArangoException {
		HttpResponseEntity res = httpManager.doDelete(createEndpointUrl(database, "/_api/query/slow"), null, null);

		return createEntity(res, DefaultEntity.class);
	}

	@Override
	public DefaultEntity killQuery(String database, String id) throws ArangoException {
		HttpResponseEntity res = httpManager.doDelete(createEndpointUrl(database, "/_api/query", id), null, null);

		return createEntity(res, DefaultEntity.class);
	}

}