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

com.qwazr.graph.GraphServiceImpl Maven / Gradle / Ivy

There is a newer version: 1.4.0
Show newest version
/**
 * Copyright 2015-2016 Emmanuel Keller / QWAZR
 * 

* 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.qwazr.graph; import com.fasterxml.jackson.core.type.TypeReference; import com.qwazr.graph.model.GraphDefinition; import com.qwazr.graph.model.GraphNode; import com.qwazr.graph.model.GraphNodeResult; import com.qwazr.graph.model.GraphRequest; import com.qwazr.graph.model.GraphResult; import com.qwazr.server.AbstractServiceImpl; import com.qwazr.server.ServerException; import com.qwazr.utils.json.JsonMapper; import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.annotation.PostConstruct; import javax.ws.rs.core.Response.Status; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URISyntaxException; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; class GraphServiceImpl extends AbstractServiceImpl implements GraphServiceInterface { private static final Logger LOGGER = LoggerFactory.getLogger(GraphServiceImpl.class); private volatile GraphManager graphManager; GraphServiceImpl(GraphManager graphManager) { this.graphManager = graphManager; } public GraphServiceImpl() { } @PostConstruct public void init() { graphManager = getContextAttribute(GraphManager.class); } @Override public Set list() { return graphManager.nameSet(); } @Override public GraphDefinition createUpdateGraph(final String graphName, final GraphDefinition graphDef) { try { graphManager.createUpdateGraph(graphName, graphDef); return graphDef; } catch (IOException | ServerException e) { throw ServerException.getJsonException(LOGGER, e); } } private GraphDefinition getGraphOrNotFound(final String graphName) throws ServerException, IOException { GraphDefinition graphDef = graphManager.get(graphName); if (graphDef == null) throw new ServerException(Status.NOT_FOUND, "Graph not found: " + graphName); return graphDef; } @Override public GraphResult getGraph(String graphName) { try { GraphDefinition graphDef = getGraphOrNotFound(graphName); GraphInstance graphInstance = graphManager.getGraphInstance(graphName); return new GraphResult(graphDef, graphInstance.getSize()); } catch (ServerException | IOException e) { throw ServerException.getJsonException(LOGGER, e); } } private GraphDefinition deleteGraphLocal(String graphName) throws IOException, URISyntaxException, ServerException { GraphDefinition base = getGraphOrNotFound(graphName); graphManager.delete(graphName); return base; } @Override public GraphDefinition deleteGraph(String graphName) { try { return deleteGraphLocal(graphName); } catch (IOException | URISyntaxException | ServerException e) { throw ServerException.getJsonException(LOGGER, e); } } @Override public GraphNode createUpdateNode(String graphName, String node_id, GraphNode node, Boolean upsert) { try { graphManager.getGraphInstance(graphName).createUpdateNode(node_id, node, upsert); return node; } catch (IOException | ServerException e) { throw ServerException.getJsonException(LOGGER, e); } } @Override public Set createUpdateNodes(String graphName, LinkedHashMap nodes, Boolean upsert) { try { graphManager.getGraphInstance(graphName).createUpdateNodes(nodes, upsert); return nodes.keySet(); } catch (URISyntaxException | IOException | ServerException e) { throw ServerException.getJsonException(LOGGER, e); } } public final static TypeReference> MapStringGraphNodeTypeRef = new TypeReference>() { }; @Override public Long createUpdateNodes(String graphName, Boolean upsert, InputStream inputStream) { try { final GraphInstance graphInstance = graphManager.getGraphInstance(graphName); InputStreamReader irs = null; BufferedReader br = null; try { irs = new InputStreamReader(inputStream, "UTF-8"); br = new BufferedReader(irs); long count = 0; String line; while ((line = br.readLine()) != null) { line = line.trim(); if (line.isEmpty()) continue; final Map nodeMap = JsonMapper.MAPPER.readValue(line, MapStringGraphNodeTypeRef); graphInstance.createUpdateNodes(nodeMap, upsert); count += nodeMap.size(); } return count; } finally { if (br != null) IOUtils.closeQuietly(br); if (irs != null) IOUtils.closeQuietly(irs); } } catch (URISyntaxException | IOException | ServerException e) { throw ServerException.getJsonException(LOGGER, e); } } private GraphNode getNodeOrNotFound(GraphInstance graphInstance, String node_id) throws IOException, URISyntaxException { final GraphNode node = graphInstance.getNode(node_id); if (node != null) return node; throw new ServerException(Status.NOT_FOUND, "Graph node not found: " + node_id); } @Override public GraphNode getNode(String graphName, String node_id) { try { final GraphInstance graphInstance = graphManager.getGraphInstance(graphName); return getNodeOrNotFound(graphInstance, node_id); } catch (URISyntaxException | IOException | ServerException e) { throw ServerException.getJsonException(LOGGER, e); } } @Override public GraphNode deleteNode(String graphName, String node_id) { try { GraphInstance graphInstance = graphManager.getGraphInstance(graphName); GraphNode node = getNodeOrNotFound(graphInstance, node_id); graphInstance.deleteNode(node_id); return node; } catch (URISyntaxException | IOException | ServerException e) { throw ServerException.getJsonException(LOGGER, e); } } @Override public GraphNode createEdge(String graphName, String node_id, String edge_type, String to_node_id) { try { GraphInstance graphInstance = graphManager.getGraphInstance(graphName); return graphInstance.createEdge(node_id, edge_type, to_node_id); } catch (IOException | ServerException e) { throw ServerException.getJsonException(LOGGER, e); } } @Override public GraphNode deleteEdge(String graphName, String node_id, String edge_type, String to_node_id) { try { GraphInstance graphInstance = graphManager.getGraphInstance(graphName); return graphInstance.deleteEdge(node_id, edge_type, to_node_id); } catch (URISyntaxException | IOException | ServerException e) { throw ServerException.getJsonException(LOGGER, e); } } @Override public List requestNodes(String graphName, GraphRequest request) { try { GraphInstance graphInstance = graphManager.getGraphInstance(graphName); return graphInstance.request(request); } catch (URISyntaxException | IOException | ServerException e) { throw ServerException.getJsonException(LOGGER, e); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy