/
BuiltinGraphFactory.java
179 lines (156 loc) · 5.85 KB
/
BuiltinGraphFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* Copyright 2017 HugeGraph Authors
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You 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 com.baidu.hugegraph.computer.core.graph;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import com.baidu.hugegraph.computer.core.common.Constants;
import com.baidu.hugegraph.computer.core.common.SerialEnum;
import com.baidu.hugegraph.computer.core.common.exception.ComputerException;
import com.baidu.hugegraph.computer.core.config.ComputerOptions;
import com.baidu.hugegraph.computer.core.config.Config;
import com.baidu.hugegraph.computer.core.graph.edge.DefaultEdge;
import com.baidu.hugegraph.computer.core.graph.edge.DefaultEdges;
import com.baidu.hugegraph.computer.core.graph.edge.Edge;
import com.baidu.hugegraph.computer.core.graph.edge.Edges;
import com.baidu.hugegraph.computer.core.graph.id.Id;
import com.baidu.hugegraph.computer.core.graph.id.LongId;
import com.baidu.hugegraph.computer.core.graph.id.Utf8Id;
import com.baidu.hugegraph.computer.core.graph.id.UuidId;
import com.baidu.hugegraph.computer.core.graph.properties.DefaultProperties;
import com.baidu.hugegraph.computer.core.graph.properties.Properties;
import com.baidu.hugegraph.computer.core.graph.value.BooleanValue;
import com.baidu.hugegraph.computer.core.graph.value.DoubleValue;
import com.baidu.hugegraph.computer.core.graph.value.FloatValue;
import com.baidu.hugegraph.computer.core.graph.value.IdValue;
import com.baidu.hugegraph.computer.core.graph.value.IdValueList;
import com.baidu.hugegraph.computer.core.graph.value.IdValueListList;
import com.baidu.hugegraph.computer.core.graph.value.IntValue;
import com.baidu.hugegraph.computer.core.graph.value.ListValue;
import com.baidu.hugegraph.computer.core.graph.value.LongValue;
import com.baidu.hugegraph.computer.core.graph.value.NullValue;
import com.baidu.hugegraph.computer.core.graph.value.Value;
import com.baidu.hugegraph.computer.core.graph.value.ValueType;
import com.baidu.hugegraph.computer.core.graph.vertex.DefaultVertex;
import com.baidu.hugegraph.computer.core.graph.vertex.Vertex;
public final class BuiltinGraphFactory implements GraphFactory {
private final Config config;
public BuiltinGraphFactory(Config config) {
this.config = config;
}
@Override
public Id createId(long id) {
return new LongId(id);
}
@Override
public Id createId(String id) {
return new Utf8Id(id);
}
@Override
public Id createId(UUID id) {
return new UuidId(id);
}
@Override
public Vertex createVertex() {
return new DefaultVertex(this);
}
@Override
public <V extends Value<?>> Vertex createVertex(Id id, V value) {
return new DefaultVertex(this, id, value);
}
@Override
public Edges createEdges() {
int averageDegree = this.config.get(
ComputerOptions.VERTEX_AVERAGE_DEGREE);
return createEdges(averageDegree);
}
@Override
public Edges createEdges(int capacity) {
return new DefaultEdges(this, capacity);
}
@Override
public Edge createEdge() {
return new DefaultEdge(this);
}
@Override
public Edge createEdge(Id targetId) {
return new DefaultEdge(this, Constants.EMPTY_STR,
Constants.EMPTY_STR, targetId);
}
@Override
public Edge createEdge(String label, Id targetId) {
return new DefaultEdge(this, label, Constants.EMPTY_STR, targetId);
}
@Override
public Edge createEdge(String label, String name, Id targetId) {
return new DefaultEdge(this, label, name, targetId);
}
@Override
public <V> List<V> createList() {
return new ArrayList<>();
}
@Override
public <V> List<V> createList(int capacity) {
return new ArrayList<>(capacity);
}
@Override
public <K, V> Map<K, V> createMap() {
return new HashMap<>();
}
@Override
public Properties createProperties() {
return new DefaultProperties(this);
}
public Value<?> createValue(byte code) {
ValueType type = SerialEnum.fromCode(ValueType.class, code);
return createValue(type);
}
/**
* Create property value by type.
*/
public Value<?> createValue(ValueType type) {
switch (type) {
case NULL:
return NullValue.get();
case BOOLEAN:
return new BooleanValue();
case INT:
return new IntValue();
case LONG:
return new LongValue();
case FLOAT:
return new FloatValue();
case DOUBLE:
return new DoubleValue();
case ID_VALUE:
return new IdValue();
case ID_VALUE_LIST:
return new IdValueList();
case ID_VALUE_LIST_LIST:
return new IdValueListList();
case LIST_VALUE:
return new ListValue<>();
default:
throw new ComputerException("Can't create Value for %s",
type.name());
}
}
}