六 hugegraph 源代码 之 存储模型(mysql为例)

2019-06-28  本文已影响0人  NazgulSun

存储模型Overview

serializer

在前面讲解 graphTransaction的时候提到了
*** this.doInsert(this.serializer.writeVertex(v)); ***

这个方法会调用对应存储引擎的serializer进行数据序列化。
我们看下 序列化的接口:

public interface GraphSerializer {

    public BackendEntry writeVertex(HugeVertex vertex);
    public BackendEntry writeVertexProperty(HugeVertexProperty<?> prop);
    public HugeVertex readVertex(HugeGraph graph, BackendEntry entry);

    public BackendEntry writeEdge(HugeEdge edge);
    public BackendEntry writeEdgeProperty(HugeEdgeProperty<?> prop);
    public HugeEdge readEdge(HugeGraph graph, BackendEntry entry);

    public BackendEntry writeIndex(HugeIndex index);
    public HugeIndex readIndex(HugeGraph graph, ConditionQuery query,
                               BackendEntry entry);

    public BackendEntry writeId(HugeType type, Id id);
    public Query writeQuery(Query query);
}

写的时候,将 图对象 转化成 backendEntity,读的时候将 backendEntity转化成图的对象。

顺藤摸瓜,看看 writeVertex 的实现。

writeVertex

    @Override
    public BackendEntry writeVertex(HugeVertex vertex) {
        TableBackendEntry entry = newBackendEntry(vertex);
        entry.column(HugeKeys.ID, IdUtil.writeString(vertex.id()));
        entry.column(HugeKeys.LABEL, vertex.schemaLabel().id().asLong());
        // Add all properties of a Vertex
        this.formatProperties(vertex, entry.row());
        return entry;
    }
    @Override
    protected void formatProperties(HugeElement element,
                                    TableBackendEntry.Row row) {
        Map<Number, Object> properties = new HashMap<>();
        // Add all properties of a Vertex
        for (HugeProperty<?> prop : element.getProperties().values()) {
            Number key = prop.propertyKey().id().asLong();
            Object val = prop.value();
            properties.put(key, val);
        }
        row.column(HugeKeys.PROPERTIES, JsonUtil.toJson(properties));
    }

backEndStore.mutate()

在graphTransaction里面提交的时候,都使用了 backendStore的mutate方法,我们看看
mysql Backedn的mute方法

private void mutate(Session session, BackendAction item) {
        MysqlBackendEntry entry = castBackendEntry(item.entry());
        MysqlTable table = this.table(entry.type());

        switch (item.action()) {
            case INSERT:
                table.insert(session, entry.row());
                break;
            case DELETE:
                table.delete(session, entry.row());
                break;
            case APPEND:
                table.append(session, entry.row());
                break;
            case ELIMINATE:
                table.eliminate(session, entry.row());
                break;
            default:
                throw new AssertionError(String.format(
                          "Unsupported mutate action: %s", item.action()));
        }
    }

这段代码,做了两件事

接下来看看 insert方法

    public void insert(Session session, MysqlBackendEntry.Row entry) {
        String template = this.buildInsertTemplate(entry);

        PreparedStatement insertStmt;
        try {
            // Create or get insert prepare statement
            insertStmt = session.prepareStatement(template);
            int i = 1;
            for (Object object : entry.columns().values()) {
                insertStmt.setObject(i++, object);
            }
        } catch (SQLException e) {
            throw new BackendException("Failed to prepare statement '%s'" +
                                       "for entry: %s", template, entry);
        }
        session.add(insertStmt);
    }

整个入库的过程就是这样,下面一张主要介绍,如何出库, 比如mysql 里面的数据是如何以图查询的。

上一篇 下一篇

猜你喜欢

热点阅读