/
IndexDuplicator.java
194 lines (162 loc) · 6.68 KB
/
IndexDuplicator.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
/* Copyright (c) 2018 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:
* Gabriel Roldan - initial implementation
*/
package org.locationtech.geogig.storage;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.ForkJoinWorkerThread;
import java.util.concurrent.RecursiveAction;
import org.locationtech.geogig.model.ObjectId;
import org.locationtech.geogig.model.RevTree;
import org.locationtech.geogig.repository.IndexInfo;
import org.locationtech.geogig.storage.IndexDatabase.IndexTreeMapping;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
/**
* Helper class to copy all the spatial indexes from one {@link IndexDatabase} to another.
*
* @see IndexDatabase#copyIndexesTo(IndexDatabase)
*/
public class IndexDuplicator {
private static final ForkJoinPool FORK_JOIN_POOL;
static {
ForkJoinPool.ForkJoinWorkerThreadFactory threadFactoryShared = pool -> {
final ForkJoinWorkerThread worker = ForkJoinPool.defaultForkJoinWorkerThreadFactory
.newThread(pool);
worker.setName("Index-duplicator-" + worker.getPoolIndex());
return worker;
};
int parallelism = Math.max(2, Runtime.getRuntime().availableProcessors() / 2);
UncaughtExceptionHandler eh = (t, e) -> {
System.err.println("Uncaught ForkJoinPool exception at thread " + t.getName());
e.printStackTrace();
};
FORK_JOIN_POOL = new ForkJoinPool(parallelism, threadFactoryShared, eh, false);
}
private final IndexDatabase srcIndex;
private final IndexDatabase targetIndex;
private IndexInfo index;
public IndexDuplicator(IndexDatabase srcIndex, IndexDatabase targetIndex) {
this(srcIndex, targetIndex, null);
}
public IndexDuplicator(IndexDatabase srcIndex, IndexDatabase targetIndex, IndexInfo index) {
this.srcIndex = srcIndex;
this.targetIndex = targetIndex;
this.index = index;
}
public void run() {
List<ForkJoinTask<?>> tasks = new ArrayList<>();
List<IndexInfo> indexInfos = index == null ? srcIndex.getIndexInfos()
: Collections.singletonList(index);
for (IndexInfo index : indexInfos) {
ForkJoinTask<Void> task = FORK_JOIN_POOL
.submit(new IndexCopyTask(index, srcIndex, targetIndex));
tasks.add(task);
}
RuntimeException err = null;
for (ForkJoinTask<?> t : tasks) {
try {
if (err == null) {
t.join();
} else {
t.cancel(true);
}
} catch (RuntimeException e) {
err = e;
}
}
if (err != null) {
throw err;
}
}
private static class IndexCopyTask extends RecursiveAction {
private static final long serialVersionUID = -392580386346607639L;
private IndexInfo index;
private IndexDatabase src;
private IndexDatabase target;
public IndexCopyTask(IndexInfo index, IndexDatabase srcIndex, IndexDatabase targetIndex) {
this.index = index;
this.src = srcIndex;
this.target = targetIndex;
}
protected @Override void compute() {
List<TreeCopyTask> treeTasks = new ArrayList<>();
Set<IndexTreeMapping> mappings = new HashSet<>();
try (AutoCloseableIterator<IndexTreeMapping> indexMappings = src
.resolveIndexedTrees(index)) {
mappings = Sets.newHashSet(indexMappings);
}
for (IndexTreeMapping mapping : mappings) {
ObjectId indexTreeId = mapping.indexTree;
if (!target.exists(indexTreeId)) {
TreeCopyTask task = new TreeCopyTask(src.getTree(indexTreeId), src, target);
treeTasks.add(task);
}
}
super.invokeAll(treeTasks);
IndexInfo targetIndex = target.createIndexInfo(index.getTreeName(),
index.getAttributeName(), index.getIndexType(), index.getMetadata());
mappings.forEach(m -> target.addIndexedTree(targetIndex, m.featureTree, m.indexTree));
}
}
private static class TreeCopyTask extends RecursiveAction {
private static final long serialVersionUID = 2886427162222070369L;
private RevTree tree;
private IndexDatabase src;
private IndexDatabase target;
public TreeCopyTask(RevTree tree, IndexDatabase src, IndexDatabase target) {
this.tree = tree;
this.src = src;
this.target = target;
}
protected @Override void compute() {
List<RevTree> save = new ArrayList<>();
save.add(tree);
List<ForkJoinTask<?>> subTasks = new ArrayList<>();
if (tree.bucketsSize() > 0) {
Iterable<ObjectId> bucketIds;
Iterator<RevTree> buckets;
bucketIds = Iterables.transform(tree.buckets().values(), b -> b.getObjectId());
buckets = src.getAll(bucketIds, BulkOpListener.NOOP_LISTENER, RevTree.class);
while (buckets.hasNext()) {
RevTree bucket = buckets.next();
if (bucket.bucketsSize() == 0) {
save.add(bucket);
} else {
if (!target.exists(bucket.getId())) {
TreeCopyTask task = new TreeCopyTask(bucket, src, target);
subTasks.add(task);
}
}
}
}
subTasks.add(new SaveTask(save, target));
super.invokeAll(subTasks);
}
}
private static class SaveTask extends RecursiveAction {
private static final long serialVersionUID = 2067245580819452473L;
private List<RevTree> save;
private IndexDatabase target;
public SaveTask(List<RevTree> save, IndexDatabase target) {
this.save = save;
this.target = target;
}
protected @Override void compute() {
target.putAll(save.iterator());
}
}
}