【JanusGraph】点属性删除流程源码解析

  1. 获取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();
}
  1. 调用Tranversal DefaultGraphTraversal --> GraphTraversal
    在这里插入图片描述
    GraphTraversal
    在这里插入图片描述
    在这里插入图片描述
  2. 从Traversal 到CacheVertexProperty :
    1. 添加过滤与删除step;
    2. 构建删除点事件Event,
    3. 执行相关删除事件。
      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()
在这里插入图片描述

  1. CacheVertexProperty --> StandardVertexProperty
    1. 从库中检索属性关系,并构建StandardVertexProperty 对象
    2. 删除属性与标签的关联关系
    3. 再次将属性拷贝到新创建的边对象中,并返回进行删除边属性操作对象
    4. 返回 StandardVertexProperty 对象;
    5. 调用 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);
    }
}
  1. StandardVertexProperty. removePropertyDirect(): 移除属性key
@Override
public <O> O removePropertyDirect(PropertyKey key) {
    if (!properties.isEmpty())
        return (O) properties.remove(key);
    else return null;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

stay_running

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值