// Copyright 2017 JanusGraph Authors
//
// 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 org.janusgraph.hadoop.formats.util.input.current;
import com.google.common.base.Preconditions;
import org.janusgraph.core.JanusGraphFactory;
import org.janusgraph.core.JanusGraphVertex;
import org.janusgraph.diskstorage.configuration.BasicConfiguration;
import org.janusgraph.graphdb.database.RelationReader;
import org.janusgraph.graphdb.database.StandardJanusGraph;
import org.janusgraph.graphdb.idmanagement.IDManager;
import org.janusgraph.graphdb.internal.JanusGraphSchemaCategory;
import org.janusgraph.graphdb.query.QueryUtil;
import org.janusgraph.graphdb.transaction.StandardJanusGraphTx;
import org.janusgraph.graphdb.types.TypeDefinitionCategory;
import org.janusgraph.graphdb.types.TypeDefinitionMap;
import org.janusgraph.graphdb.types.TypeInspector;
import org.janusgraph.graphdb.types.system.BaseKey;
import org.janusgraph.graphdb.types.system.BaseLabel;
import org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex;
import org.janusgraph.hadoop.config.ModifiableHadoopConfiguration;
import org.janusgraph.hadoop.config.JanusGraphHadoopConfiguration;
import org.janusgraph.hadoop.formats.util.input.SystemTypeInspector;
import org.janusgraph.hadoop.formats.util.input.JanusGraphHadoopSetupCommon;
import org.apache.tinkerpop.gremlin.structure.Direction;
import org.apache.hadoop.conf.Configuration;
/**
* @author Matthias Broecheler (me@matthiasb.com)
*/
public class JanusGraphHadoopSetupImpl extends JanusGraphHadoopSetupCommon {
private final ModifiableHadoopConfiguration scanConf;
private final StandardJanusGraph graph;
private final StandardJanusGraphTx tx;
public JanusGraphHadoopSetupImpl(final Configuration config) {
scanConf = ModifiableHadoopConfiguration.of(JanusGraphHadoopConfiguration.MAPRED_NS, config);
BasicConfiguration bc = scanConf.getJanusGraphConf();
graph = (StandardJanusGraph) JanusGraphFactory.open(bc);
tx = (StandardJanusGraphTx)graph.buildTransaction().readOnly().vertexCacheSize(200).start();
}
@Override
public TypeInspector getTypeInspector() {
//Pre-load schema
for (JanusGraphSchemaCategory sc : JanusGraphSchemaCategory.values()) {
for (JanusGraphVertex k : QueryUtil.getVertices(tx, BaseKey.SchemaCategory, sc)) {
assert k instanceof JanusGraphSchemaVertex;
JanusGraphSchemaVertex s = (JanusGraphSchemaVertex)k;
if (sc.hasName()) {
String name = s.name();
Preconditions.checkNotNull(name);
}
TypeDefinitionMap dm = s.getDefinition();
Preconditions.checkNotNull(dm);
s.getRelated(TypeDefinitionCategory.TYPE_MODIFIER, Direction.OUT);
s.getRelated(TypeDefinitionCategory.TYPE_MODIFIER, Direction.IN);
}
}
return tx;
}
@Override
public SystemTypeInspector getSystemTypeInspector() {
return new SystemTypeInspector() {
@Override
public boolean isSystemType(long typeid) {
return IDManager.isSystemRelationTypeId(typeid);
}
@Override
public boolean isVertexExistsSystemType(long typeid) {
return typeid == BaseKey.VertexExists.longId();
}
@Override
public boolean isVertexLabelSystemType(long typeid) {
return typeid == BaseLabel.VertexLabelEdge.longId();
}
@Override
public boolean isTypeSystemType(long typeid) {
return typeid == BaseKey.SchemaCategory.longId() ||
typeid == BaseKey.SchemaDefinitionProperty.longId() ||
typeid == BaseKey.SchemaDefinitionDesc.longId() ||
typeid == BaseKey.SchemaName.longId() ||
typeid == BaseLabel.SchemaDefinitionEdge.longId();
}
};
}
@Override
public IDManager getIDManager() {
return graph.getIDManager();
}
@Override
public RelationReader getRelationReader(long vertexid) {
return graph.getEdgeSerializer();
}
@Override
public void close() {
tx.rollback();
graph.close();
}
@Override
public boolean getFilterPartitionedVertices() {
return scanConf.get(JanusGraphHadoopConfiguration.FILTER_PARTITIONED_VERTICES);
}
}