/
HeapGraphDatabase.java
258 lines (218 loc) · 7.39 KB
/
HeapGraphDatabase.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* Copyright (c) 2013-2016 Boundless and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/edl-v10.html
*
* Contributors:
* David Winslow (Boundless) - initial implementation
*/
package org.locationtech.geogig.storage.memory;
import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.transform;
import java.net.URI;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.eclipse.jdt.annotation.Nullable;
import org.locationtech.geogig.model.ObjectId;
import org.locationtech.geogig.plumbing.ResolveGeogigURI;
import org.locationtech.geogig.repository.Platform;
import org.locationtech.geogig.storage.GraphDatabase;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
import com.google.common.collect.Maps;
/**
* Provides an default in memory implementation of a GeoGig Graph Database.
*/
public class HeapGraphDatabase implements GraphDatabase {
static final Function<Node, ObjectId> NODE_TO_ID = (n) -> n.id;
static final Map<URI, Ref> graphs = Maps.newConcurrentMap();
final @Nullable Platform platform;
Graph graph;
public HeapGraphDatabase(@Nullable Platform platform) {
this.platform = platform;
}
@Override
public void open() {
if (isOpen()) {
return;
}
Optional<URI> url = platform == null ? Optional.empty()
: new ResolveGeogigURI(platform, null).call();
if (url.isPresent()) {
synchronized (graphs) {
URI key = url.get();
if (!graphs.containsKey(key)) {
graphs.put(key, new Ref(new Graph()));
}
graph = graphs.get(key).acquire();
}
} else {
graph = new Graph();
}
}
@Override
public boolean isOpen() {
return graph != null;
}
@Override
public void close() {
if (!isOpen()) {
return;
}
graph = null;
Optional<URI> url = platform == null ? Optional.empty()
: new ResolveGeogigURI(platform, null).call();
if (url.isPresent()) {
synchronized (graphs) {
URI key = url.get();
Ref ref = graphs.get(key);
if (ref != null && ref.release() <= -1) {
ref.destroy();
graphs.remove(key);
}
}
}
}
@Override
public boolean exists(ObjectId commitId) {
return graph.get(commitId).isPresent();
}
@Override
public ImmutableList<ObjectId> getParents(ObjectId commitId) throws IllegalArgumentException {
// transform outgoing nodes to id
// filter for null to skip fake root node
return graph.get(commitId).map(
n -> ImmutableList.copyOf( filter( transform(n.to(), NODE_TO_ID), i->i!=null) )).orElse(ImmutableList.of());
}
@Override
public ImmutableList<ObjectId> getChildren(ObjectId commitId) throws IllegalArgumentException {
return graph.get(commitId).map(n->ImmutableList.copyOf(transform(n.from(), NODE_TO_ID))).orElse(ImmutableList.of());
}
@Override
public boolean put(ObjectId commitId, ImmutableList<ObjectId> parentIds) {
Node n = graph.getOrAdd(commitId);
synchronized (n) {
if (parentIds.isEmpty()) {
// the root node, only update on first addition
if (!n.isRoot()) {
n.setRoot(true);
return true;
}
}
// has the node been attached to graph?
if (Iterables.isEmpty(n.to())) {
// nope, attach it
for (ObjectId parent : parentIds) {
Node p = graph.getOrAdd(parent);
graph.newEdge(n, p);
}
// only mark as updated if it is actually attached
boolean added = !Iterables.isEmpty(n.to());
return added;
}
}
return false;
}
@Override
public void map(ObjectId mapped, ObjectId original) {
graph.map(mapped, original);
}
@Override
public ObjectId getMapping(ObjectId commitId) {
return Optional.ofNullable(graph.getMapping(commitId)).orElse(ObjectId.NULL);
}
@Override
public int getDepth(ObjectId commitId) {
Preconditions.checkNotNull(commitId);
Optional<Node> nodeOpt = graph.get(commitId);
Preconditions.checkArgument(nodeOpt.isPresent(), "No graph entry for commit %s on %s",
commitId, this.toString());
Node node = nodeOpt.get();
PathToRootWalker walker = new PathToRootWalker(node);
int depth = 0;
O: while (walker.hasNext()) {
for (Node n : walker.next()) {
if (Iterables.size(n.to()) == 0) {
break O;
}
}
depth++;
}
return depth;
}
@Override
public void setProperty(ObjectId commitId, String propertyName, String propertyValue) {
graph.get(commitId).get().put(propertyName, propertyValue);
;
}
@Override
public void truncate() {
graph.clear();
}
static class Ref {
int count;
Graph graph;
Ref(Graph g) {
graph = g;
count = 0;
}
Graph acquire() {
count++;
return graph;
}
int release() {
return --count;
}
void destroy() {
graph = null;
}
}
protected class HeapGraphNode extends GraphNode {
Node node;
public HeapGraphNode(Node node) {
this.node = node;
}
@Override
public ObjectId getIdentifier() {
return node.id;
}
@Override
public Iterator<GraphEdge> getEdges(final Direction direction) {
Iterator<Edge> nodeEdges;
switch (direction) {
case OUT:
nodeEdges = node.out.iterator();
break;
case IN:
nodeEdges = node.in.iterator();
break;
default:
nodeEdges = Iterators.concat(node.in.iterator(), node.out.iterator());
}
List<GraphEdge> edges = new LinkedList<GraphEdge>();
while (nodeEdges.hasNext()) {
Edge nodeEdge = nodeEdges.next();
Node src = nodeEdge.src;
Node dst = nodeEdge.dst;
edges.add(new GraphEdge(new HeapGraphNode(src), new HeapGraphNode(dst)));
}
return edges.iterator();
}
@Override
public boolean isSparse() {
return node.props != null && node.props.containsKey(SPARSE_FLAG)
&& Boolean.valueOf(node.props.get(SPARSE_FLAG));
}
}
@Override
public GraphNode getNode(ObjectId id) {
return new HeapGraphNode(graph.get(id).get());
}
}