com.qwazr.graph.GraphManager Maven / Gradle / Ivy
/**
* Copyright 2015-2017 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.qwazr.database.store.Table;
import com.qwazr.database.store.Tables;
import com.qwazr.graph.model.GraphDefinition;
import com.qwazr.server.ApplicationBuilder;
import com.qwazr.server.GenericServer;
import com.qwazr.server.ServerException;
import com.qwazr.utils.FileUtils;
import com.qwazr.utils.LockUtils;
import com.qwazr.utils.json.DirectoryJsonManager;
import javax.ws.rs.core.Response.Status;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class GraphManager extends DirectoryJsonManager {
private final LockUtils.ReadWriteLock rwl = new LockUtils.ReadWriteLock();
private final GraphServiceInterface service;
private final Map graphMap;
public GraphManager(final Path graphesDirectory) throws ServerException, IOException {
super(graphesDirectory.toFile(), GraphDefinition.class);
graphMap = new HashMap<>();
for (String name : nameSet())
addNewInstance(name, get(name));
service = new GraphServiceImpl(this);
}
public GraphManager registerContextAttribute(final GenericServer.Builder builder) {
builder.contextAttribute(this);
return this;
}
public GraphManager registerWebService(final ApplicationBuilder builder) {
builder.singletons(service);
return this;
}
public static Path checkGraphesDirectory(final Path dataDirectory) throws IOException {
final Path tablesDirectory = dataDirectory.resolve(GraphServiceInterface.SERVICE_NAME);
if (!Files.exists(tablesDirectory))
Files.createDirectory(tablesDirectory);
return tablesDirectory;
}
public GraphServiceInterface getService() {
return service;
}
private GraphInstance addNewInstance(final String graphName, final GraphDefinition graphDef)
throws IOException, ServerException {
final File dbDirectory = new File(directory, graphName);
if (!dbDirectory.exists())
dbDirectory.mkdir();
final Table table = Tables.getInstance(dbDirectory, graphDef.implementation);
GraphInstance graphInstance = new GraphInstance(graphName, table, graphDef);
graphInstance.checkFields();
graphMap.put(graphName, graphInstance);
return graphInstance;
}
public GraphInstance getGraphInstance(final String graphName) throws ServerException {
return rwl.readEx(() -> {
GraphInstance graphInstance = graphMap.get(graphName);
if (graphInstance == null)
throw new ServerException(Status.NOT_FOUND, "Graph instance not found");
return graphInstance;
});
}
@Override
public Set nameSet() {
return super.nameSet();
}
@Override
public GraphDefinition get(final String name) throws IOException {
return super.get(name);
}
public void createUpdateGraph(final String graphName, final GraphDefinition graphDef)
throws IOException, ServerException {
rwl.writeEx(() -> {
super.set(graphName, graphDef);
graphMap.remove(graphName);
addNewInstance(graphName, graphDef);
});
}
@Override
public GraphDefinition delete(String graphName) throws ServerException, IOException {
return rwl.writeEx(() -> {
GraphDefinition graphDef = super.delete(graphName);
File dbDirectory = new File(directory, graphName);
Tables.delete(dbDirectory);
FileUtils.deleteDirectory(dbDirectory);
graphMap.remove(graphName);
return graphDef;
});
}
}