// 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.graphdb.types.system;
import com.google.common.base.Preconditions;
import org.janusgraph.core.schema.ConsistencyModifier;
import org.janusgraph.graphdb.idmanagement.IDManager;
import org.janusgraph.graphdb.internal.JanusGraphSchemaCategory;
import org.janusgraph.graphdb.internal.Token;
import org.apache.commons.lang.StringUtils;
public abstract class BaseRelationType extends EmptyRelationType implements SystemRelationType {
private final String name;
private final long id;
BaseRelationType(String name, long id, JanusGraphSchemaCategory type) {
Preconditions.checkArgument(StringUtils.isNotBlank(name));
this.name = Token.systemETprefix + name;
this.id = getSystemTypeId(id, type);
}
@Override
public String name() {
return name;
}
@Override
public long longId() {
return id;
}
@Override
public boolean hasId() {
return true;
}
@Override
public void setId(long id) {
throw new IllegalStateException("SystemType has already been assigned an id");
}
@Override
public ConsistencyModifier getConsistencyModifier() {
return ConsistencyModifier.LOCK;
}
@Override
public boolean isInvisibleType() {
return true;
}
static long getSystemTypeId(long id, JanusGraphSchemaCategory type) {
Preconditions.checkArgument(id > 0);
Preconditions.checkArgument(type.isRelationType());
switch (type) {
case EDGELABEL:
return IDManager.getSchemaId(IDManager.VertexIDType.SystemEdgeLabel, id);
case PROPERTYKEY:
return IDManager.getSchemaId(IDManager.VertexIDType.SystemPropertyKey,id);
default:
throw new AssertionError("Illegal argument: " + type);
}
}
}