[关闭]
@contribute 2016-09-23T03:41:06.000000Z 字数 6695 阅读 2128

数据模型

tinkerpop3



1. 数据模型设计总则

2. OLTP需要实现的接口

OLTP需要实现的接口无外乎包含以下几种类型:

  • 对vertex、edge、property的增删改查操作。
  • 获取vertex、edge或property的集合的Iterator。

2.1 Edge

  1. /**
  2. * Retrieve the vertex (or vertices) associated with this edge as defined by the direction.
  3. * If the direction is {@link Direction#BOTH} then the iterator order is: {@link Direction#OUT} then {@link Direction#IN}.
  4. *
  5. * @param direction Get the incoming vertex, outgoing vertex, or both vertices
  6. * @return An iterator with 1 or 2 vertices
  7. */
  8. public Iterator<Vertex> vertices(final Direction direction);
  1. @Override
  2. public <V> Iterator<Property<V>> properties(final String... propertyKeys);

2.2 Vertex

  1. /**
  2. * Add an outgoing edge to the vertex with provided label and edge properties as key/value pairs.
  3. * These key/values must be provided in an even number where the odd numbered arguments are {@link String}
  4. * property keys and the even numbered arguments are the related property values.
  5. *
  6. * @param label The label of the edge
  7. * @param inVertex The vertex to receive an incoming edge from the current vertex
  8. * @param keyValues The key/value pairs to turn into edge properties
  9. * @return the newly created edge
  10. */
  11. public Edge addEdge(final String label, final Vertex inVertex, final Object... keyValues);
  12. /**
  13. * Create a new vertex property. If the cardinality is {@link VertexProperty.Cardinality#single}, then set the key
  14. * to the value. If the cardinality is {@link VertexProperty.Cardinality#list}, then add a new value to the key.
  15. * If the cardinality is {@link VertexProperty.Cardinality#set}, then only add a new value if that value doesn't
  16. * already exist for the key. If the value already exists for the key, add the provided key value vertex property
  17. * properties to it.
  18. *
  19. * @param cardinality the desired cardinality of the property key
  20. * @param key the key of the vertex property
  21. * @param value The value of the vertex property
  22. * @param keyValues the key/value pairs to turn into vertex property properties
  23. * @param <V> the type of the value of the vertex property
  24. * @return the newly created vertex property
  25. */
  26. public <V> VertexProperty<V> property(final VertexProperty.Cardinality cardinality, final String key, final V value, final Object... keyValues);
  27. /**
  28. * Gets an {@link Iterator} of incident edges.
  29. *
  30. * @param direction The incident direction of the edges to retrieve off this vertex
  31. * @param edgeLabels The labels of the edges to retrieve. If no labels are provided, then get all edges.
  32. * @return An iterator of edges meeting the provided specification
  33. */
  34. public Iterator<Edge> edges(final Direction direction, final String... edgeLabels);
  35. /**
  36. * Gets an {@link Iterator} of adjacent vertices.
  37. *
  38. * @param direction The adjacency direction of the vertices to retrieve off this vertex
  39. * @param edgeLabels The labels of the edges associated with the vertices to retrieve. If no labels are provided,
  40. * then get all edges.
  41. * @return An iterator of vertices meeting the provided specification
  42. */
  43. public Iterator<Vertex> vertices(final Direction direction, final String... edgeLabels);
  44. /**
  45. * {@inheritDoc}
  46. */
  47. @Override
  48. public <V> Iterator<VertexProperty<V>> properties(final String... propertyKeys);

2.3 VertexProperty

  1. /**
  2. * {@inheritDoc}
  3. */
  4. @Override
  5. public <U> Iterator<Property<U>> properties(final String... propertyKeys);

2.4 Element

  1. /**
  2. * Gets the unique identifier for the graph {@code Element}.
  3. *
  4. * @return The id of the element
  5. */
  6. public Object id();
  7. /**
  8. * Gets the label for the graph {@code Element} which helps categorize it.
  9. *
  10. * @return The label of the element
  11. */
  12. public String label();
  13. /**
  14. * Get the graph that this element is within.
  15. *
  16. * @return the graph of this element
  17. */
  18. public Graph graph();
  19. /**
  20. * Add or set a property value for the {@code Element} given its key.
  21. */
  22. public <V> Property<V> property(final String key, final V value);
  23. /**
  24. * Get an {@link Iterator} of properties.
  25. */
  26. public <V> Iterator<? extends Property<V>> properties(final String... propertyKeys);
  27. /**
  28. * Removes the {@code Element} from the graph.
  29. */
  30. public void remove();

3. 数据模型采用Object

KuazVertex

  1. public class KuazVertex {
  2. private Object id;
  3. private String label;
  4. private List<Object> outEdgeIds;
  5. private List<Object> inEdgeIds;
  6. }

KuazEdge

  1. public class KuazEdge {
  2. private Object id;
  3. private String label;
  4. private Object outVertexId;
  5. private Object inVertexId;
  6. }

KuazProperty

  1. public class KuazProperty {
  2. private static String value = "value";
  3. private static String property = "property";
  4. /**
  5. * 属性所属的点或边的id.
  6. */
  7. private Object elementId;
  8. /**
  9. * 属性id.
  10. */
  11. private String propertyId;
  12. /**
  13. * 属性的类型是key/value还是属性中有属性。
  14. */
  15. private String propertyType = value;
  16. /**
  17. * 属性名
  18. */
  19. private String propertyKey;
  20. /**
  21. * 属性值
  22. */
  23. private Object propertyValue;
  24. /**
  25. * 用于存多值属性和属性中的属性的id。
  26. */
  27. private List<Object> proerties;
  28. }

4. 数据模型采用Map

Vertex.png-53.6kB

5. 个人建议的数据模型设计

  • 采用 Object + Map 的设计设计方案。
  • 只建立vertexCache和edgeCache两个Cache。
  • vertex和edge的属性分别存在对应的Object中的Map中。

5.1 优点

5.2 数据模型

5.2.1 第一版

  1. public class KuazEdge {
  2. private Object id;
  3. @QuerySqlField(index = true)
  4. private String label;
  5. @AffinityKeyMapped
  6. private Object outVertexId;
  7. @AffinityKeyMapped
  8. private Object inVertexId;
  9. private Map<String, Object> properties;
  10. }
  1. public class KuazVertex {
  2. private Object id;
  3. @QuerySqlField(index = true)
  4. private String label;
  5. private List<Object> outEdgeIds;
  6. private List<Object> inEdgeIds;
  7. private Map<String, Map<String, Object>> properties;
  8. }

5.2.2 第二版

  1. public class KuazEdge {
  2. private Object id;
  3. private String label;
  4. private Object outVid;
  5. private Object inVid;
  6. private List<Property> properties;
  7. }
  1. public class KuazVertex {
  2. private Object id;
  3. private String label;
  4. private Object outEid;
  5. private Object inEid;
  6. private List<VertexProperty> property;
  7. }
  1. public class Property {
  2. protected Object id;
  3. protected String key;
  4. protected String value;
  5. }
  1. public class VertexProperty extends Property {
  2. private List<Property> properties;
  3. }

5.2.3 第三版

  1. public class KuazEdge {
  2. private Object id;
  3. private String label;
  4. private Object outVid;
  5. private Object inVid;
  6. private List<Property> properties;
  7. }
  1. public class KuazVertex {
  2. private Object id;
  3. private String label;
  4. private KuazEdge outEdge;
  5. private KuazEdge inEdge;
  6. private List<VertexProperty> property;
  7. }
  1. public class Property {
  2. protected Object id;
  3. protected String key;
  4. protected String value;
  5. }
  1. public class VertexProperty extends Property {
  2. private List<Property> properties;
  3. }

5.2.3 第四版

  1. public class KuazEdge {
  2. private Object id;
  3. private String label;
  4. private Object outVid;
  5. private Object inVid;
  6. private List<Property> properties;
  7. }
  1. public class KuazVertex {
  2. private Object id;
  3. private String label;
  4. private KuazEdge outEdge;
  5. private KuazEdge inEdge;
  6. private List<VertexProperty> property;
  7. }
  1. public class Property {
  2. protected Object id;
  3. protected String key;
  4. protected String value;
  5. }
  1. public class VertexProperty extends Property {
  2. private List<Property> properties;
  3. }

说明:采用两个edgeCache来存储KuzaEdge,一个cache跟出点做并置,另一个cache跟入点做并置。此设计是针对优化computer进行优化的。

6. 索引

  1. g.V().hasLabel('person').count()
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注