// 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.vertices;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import org.janusgraph.diskstorage.EntryList;
import org.janusgraph.diskstorage.keycolumnvalue.SliceQuery;
import org.janusgraph.graphdb.internal.ElementLifeCycle;
import org.janusgraph.graphdb.internal.InternalRelation;
import org.janusgraph.graphdb.transaction.StandardJanusGraphTx;
import org.janusgraph.graphdb.transaction.addedrelations.AddedRelationsContainer;
import org.janusgraph.graphdb.transaction.addedrelations.ConcurrentAddedRelations;
import org.janusgraph.graphdb.transaction.addedrelations.SimpleAddedRelations;
import org.janusgraph.util.datastructures.Retriever;
import java.util.List;
/**
* @author Matthias Broecheler (me@matthiasb.com)
*/
public class StandardVertex extends AbstractVertex {
private byte lifecycle;
private volatile AddedRelationsContainer addedRelations=AddedRelationsContainer.EMPTY;
public StandardVertex(final StandardJanusGraphTx tx, final long id, byte lifecycle) {
super(tx, id);
this.lifecycle=lifecycle;
}
public synchronized final void updateLifeCycle(ElementLifeCycle.Event event) {
this.lifecycle = ElementLifeCycle.update(lifecycle,event);
}
@Override
public void removeRelation(InternalRelation r) {
if (r.isNew()) addedRelations.remove(r);
else if (r.isLoaded()) updateLifeCycle(ElementLifeCycle.Event.REMOVED_RELATION);
else throw new IllegalArgumentException("Unexpected relation status: " + r.isRemoved());
}
@Override
public boolean addRelation(InternalRelation r) {
Preconditions.checkArgument(r.isNew());
if (addedRelations==AddedRelationsContainer.EMPTY) {
if (tx().getConfiguration().isSingleThreaded()) {
addedRelations=new SimpleAddedRelations();
} else {
synchronized (this) {
if (addedRelations==AddedRelationsContainer.EMPTY)
addedRelations=new ConcurrentAddedRelations();
}
}
}
if (addedRelations.add(r)) {
updateLifeCycle(ElementLifeCycle.Event.ADDED_RELATION);
return true;
} else return false;
}
@Override
public List<InternalRelation> getAddedRelations(Predicate<InternalRelation> query) {
return addedRelations.getView(query);
}
@Override
public EntryList loadRelations(SliceQuery query, Retriever<SliceQuery, EntryList> lookup) {
return (isNew()) ? EntryList.EMPTY_LIST : lookup.get(query);
}
@Override
public boolean hasLoadedRelations(SliceQuery query) {
return false;
}
@Override
public boolean hasRemovedRelations() {
return ElementLifeCycle.hasRemovedRelations(lifecycle);
}
@Override
public boolean hasAddedRelations() {
return ElementLifeCycle.hasAddedRelations(lifecycle);
}
@Override
public synchronized void remove() {
super.remove();
((StandardVertex)it()).updateLifeCycle(ElementLifeCycle.Event.REMOVED);
}
@Override
public byte getLifeCycle() {
return lifecycle;
}
}