- 获取Traversal对象,并检索属性,以及添加删除step
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.janusgraph.core.ConfiguredGraphFactory;
import org.janusgraph.core.JanusGraph;
public static void main(String[] args) {
String graphName = "";
Long id = 1L;
JanusGraph configuredGraphFactory = ConfiguredGraphFactory.open(graphName);
GraphTraversalSource traversalSource = configuredGraphFactory.traversal();
GraphTraversal<Vertex,?> veretexTraversal = traversalSource.V(id);
veretexTraversal.properties("movie_id").drop().iterate();
}
- 调用Tranversal DefaultGraphTraversal --> GraphTraversal
GraphTraversal
- 从Traversal 到CacheVertexProperty :
- 添加过滤与删除step;
- 构建删除点事件Event,
- 执行相关删除事件。
Traversal --> AbstractStep --> FilterStep --> DropStep -->SimpleJanusGraphProperty -->CacheVertexProperty
调用iterate方法,迭代执行所有的实例(queryStep,dorpStep … )
AbstractStep
FilterStep
DropStep
public class DropStep<S> extends FilterStep<S> implements Mutating<Event> {
private CallbackRegistry<Event> callbackRegistry;
public DropStep(final Traversal.Admin traversal) {
super(traversal);
}
@Override
protected boolean filter(Traverser.Admin<S> traverser) {
final S s = traverser.get();
//判断是否是元素或者属性
if (s instanceof Element) {
final Element toRemove = (Element) s;
if (callbackRegistry != null) {
final EventStrategy eventStrategy = getTraversal().getStrategies().getStrategy(EventStrategy.class).get();
final Event removeEvent;
if (s instanceof Vertex)
removeEvent = new Event.VertexRemovedEvent(eventStrategy.detach((Vertex) s));
else if (s instanceof Edge)
removeEvent = new Event.EdgeRemovedEvent(eventStrategy.detach((Edge) s));
else if (s instanceof VertexProperty)
removeEvent = new Event.VertexPropertyRemovedEvent(eventStrategy.detach((VertexProperty) s));
else
throw new IllegalStateException("The incoming object is not removable: " + s);
callbackRegistry.getCallbacks().forEach(c -> c.accept(removeEvent));
}
toRemove.remove();
} else if (s instanceof Property) {
//如果是属性,则强转属性类型
final Property toRemove = (Property) s;
if (callbackRegistry != null) {
//获取执行事件(event)
final EventStrategy eventStrategy = getTraversal().getStrategies().getStrategy(EventStrategy.class).get();
final Event.ElementPropertyEvent removeEvent;
//判断是删除点还是删除边
if (toRemove.element() instanceof Edge)
//构建删除边属性事件
removeEvent = new Event.EdgePropertyRemovedEvent(eventStrategy.detach((Edge) toRemove.element()), eventStrategy.detach(toRemove));
else if (toRemove.element() instanceof VertexProperty)
//构建删除点属性事件
removeEvent = new Event.VertexPropertyPropertyRemovedEvent(eventStrategy.detach((VertexProperty) toRemove.element()), eventStrategy.detach(toRemove));
else
throw new IllegalStateException("The incoming object is not removable: " + s);
callbackRegistry.getCallbacks().forEach(c -> c.accept(removeEvent));
}
//调用具体实现类,执行删除
toRemove.remove();
} else
throw new IllegalStateException("The incoming object is not removable: " + s);
return false;
}
@Override
public CallbackRegistry<Event> getMutatingCallbackRegistry() {
if (null == callbackRegistry) callbackRegistry = new ListCallbackRegistry<>();
return callbackRegistry;
}
/**
* This method doesn't do anything as {@code drop()} doesn't take property mutation arguments.
*/
@Override
public void configure(final Object... keyValues) {
// do nothing
}
@Override
public Parameters getParameters() {
return Parameters.EMPTY;
}
}
SimpleJanusGraphProperty.remove()
- CacheVertexProperty --> StandardVertexProperty
- 从库中检索属性关系,并构建StandardVertexProperty 对象
- 删除属性与标签的关联关系
- 再次将属性拷贝到新创建的边对象中,并返回进行删除边属性操作对象
- 返回 StandardVertexProperty 对象;
- 调用 StandardVertexProperty 对象的 removePropertyDirect方法,进行属性删除操作。
CacheVertexProperty.removePropertyDirect()
CacheVertexProperty.update()
1. 构建StandardVertexProperty对象;
2. 从库中检索属性,并拷贝到StandardVertex对象的属性缓存中;
3. 删除关联关系(属性与标签之间的关联)
4. 添加属性,并返回该 StandardVertexProperty对象
5. 从库中检索属性,并拷贝到 StandardVertex 对象的属性缓存中;
6. 并返回 StandardVertex 对象
StandardVertexProperty .copyProperties() 检索库中顶点存在的属性key,并过来掉JanusGraph内置属性key;
setPropertyDirect() ,将检索到的属性信息写入缓存 EMPTY_PROPERTIES 对象中;
remove()方法 : 执行删除关联关系;
public class StandardVertexProperty extends AbstractVertexProperty implements StandardRelation, ReassignableRelation {
//缓存顶点属性
private static final Map<PropertyKey, Object> EMPTY_PROPERTIES = ImmutableMap.of();
private void copyProperties(InternalRelation to) {
for (LongObjectCursor<Object> entry : getPropertyMap()) {
//
PropertyKey type = tx().getExistingPropertyKey(entry.key);
if (!(type instanceof ImplicitKey))
to.setPropertyDirect(type, entry.value);
}
}
//copyProperties 方法调用 setPropertyDirect() 方法
@Override
public void setPropertyDirect(PropertyKey key, Object value) {
Preconditions.checkArgument(!(key instanceof ImplicitKey), "Cannot use implicit type [%s] when setting property", key.name());
if (properties == EMPTY_PROPERTIES) {
if (tx().getConfiguration().isSingleThreaded()) {
properties = new HashMap<>(5);
} else {
synchronized (this) {
if (properties == EMPTY_PROPERTIES) {
properties = Collections.synchronizedMap(new HashMap<PropertyKey, Object>(5));
}
}
}
}
properties.put(key, value);
}
//删除关联属性
@Override
public synchronized void remove() {
if (!ElementLifeCycle.isRemoved(lifecycle)) {
tx().removeRelation(this);
lifecycle = ElementLifeCycle.update(lifecycle, ElementLifeCycle.Event.REMOVED);
if (isUpsert) {
VertexProperty.Cardinality cardinality = ((PropertyKey) type).cardinality().convert();
Consumer<JanusGraphVertexProperty> propertyRemover = JanusGraphVertexProperty.getRemover(cardinality, value());
element().query().types(type.name()).properties().forEach(propertyRemover);
}
} //else throw InvalidElementException.removedException(this);
}
}
StandardJanusGraphTx.removeRelation()
public void removeRelation(InternalRelation relation) {
Preconditions.checkArgument(!relation.isRemoved());
relation = relation.it();
for (int i = 0; i < relation.getLen(); i++)
verifyWriteAccess(relation.getVertex(i));
//Delete from Vertex
for (int i = 0; i < relation.getLen(); i++) {
relation.getVertex(i).removeRelation(relation);
}
//Update transaction data structures
if (relation.isNew()) {
addedRelations.remove(relation);
if (TypeUtil.hasSimpleInternalVertexKeyIndex(relation)) newVertexIndexEntries.remove((JanusGraphVertexProperty) relation);
} else {
Preconditions.checkArgument(relation.isLoaded());
Map<Long, InternalRelation> result = deletedRelations;
if (result == EMPTY_DELETED_RELATIONS) {
if (config.isSingleThreaded()) {
deletedRelations = result = new HashMap<>();
} else {
synchronized (this) {
result = deletedRelations;
if (result == EMPTY_DELETED_RELATIONS)
deletedRelations = result = new ConcurrentHashMap<>();
}
}
}
result.put(relation.longId(), relation);
}
}
- StandardVertexProperty. removePropertyDirect(): 移除属性key
@Override
public <O> O removePropertyDirect(PropertyKey key) {
if (!properties.isEmpty())
return (O) properties.remove(key);
else return null;
}