/
CreateIndexOp.java
178 lines (152 loc) · 5.87 KB
/
CreateIndexOp.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
/* Copyright (c) 2017 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 (Boundless) - initial implementation
*/
package org.locationtech.geogig.porcelain.index;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import java.util.Map;
import java.util.Optional;
import org.eclipse.jdt.annotation.Nullable;
import org.locationtech.geogig.model.ObjectId;
import org.locationtech.geogig.model.RevTree;
import org.locationtech.geogig.plumbing.index.BuildFullHistoryIndexOp;
import org.locationtech.geogig.plumbing.index.BuildIndexOp;
import org.locationtech.geogig.plumbing.index.CreateIndexInfoOp;
import org.locationtech.geogig.repository.AbstractGeoGigOp;
import org.locationtech.geogig.repository.IndexInfo;
import org.locationtech.geogig.repository.IndexInfo.IndexType;
import com.google.common.base.Throwables;
/**
* Creates a new index using the provided parameters and metadata.
*/
public class CreateIndexOp extends AbstractGeoGigOp<Index> {
private String treeName;
private String attributeName;
private IndexType indexType;
private @Nullable Map<String, Object> metadata;
private RevTree canonicalTypeTree;
private ObjectId featureTypeId;
private boolean indexHistory = false;
/**
* Performs the operation.
*
* @return an {@link Index} that represents the newly created index
*/
@Override
protected Index _call() {
checkArgument(treeName != null, "treeName not provided");
checkArgument(attributeName != null, "attributeName not provided");
checkArgument(indexType != null, "indexType not provided");
checkArgument(canonicalTypeTree != null, "canonicalTypeTree not provided");
checkArgument(featureTypeId != null, "featureTypeId not provided");
final IndexInfo indexInfo = command(CreateIndexInfoOp.class)//
.setTreeName(treeName)//
.setAttributeName(attributeName)//
.setIndexType(indexType)//
.setMetadata(metadata)//
.call();
ObjectId indexedTreeId = null;
try {
if (indexHistory) {
command(BuildFullHistoryIndexOp.class)//
.setTreeRefSpec(treeName)//
.setAttributeName(attributeName)//
.setProgressListener(getProgressListener())//
.call();
if (!getProgressListener().isCanceled()) {
Optional<ObjectId> headIndexedTreeId = indexDatabase()
.resolveIndexedTree(indexInfo, canonicalTypeTree.getId());
checkState(headIndexedTreeId.isPresent(),
"HEAD indexed tree could not be resolved after building history indexes.");
indexedTreeId = headIndexedTreeId.get();
}
} else {
indexedTreeId = command(BuildIndexOp.class)//
.setIndex(indexInfo)//
.setOldCanonicalTree(RevTree.EMPTY)//
.setNewCanonicalTree(canonicalTypeTree)//
.setRevFeatureTypeId(featureTypeId)//
.setProgressListener(getProgressListener())//
.call().getId();
}
} catch (Exception e) {
// rollback
rollback();
Throwables.throwIfUnchecked(e);
throw new RuntimeException(e);
}
if (getProgressListener().isCanceled()) {
rollback();
return null;
}
return new Index(indexInfo, indexedTreeId, indexDatabase());
}
private void rollback() {
command(DropIndexOp.class).setTreeRefSpec(treeName).setAttributeName(attributeName).call();
}
/**
* @param treeName the name of the feature tree
* @return {@code this}
*/
public CreateIndexOp setTreeName(String treeName) {
this.treeName = treeName;
return this;
}
/**
* @param attributeName the name of the indexed attribute
* @return {@code this}
*/
public CreateIndexOp setAttributeName(String attributeName) {
this.attributeName = attributeName;
return this;
}
/**
* @param indexType the type of index to create
* @return {@code this}
*/
public CreateIndexOp setIndexType(IndexType indexType) {
this.indexType = indexType;
return this;
}
/**
* @param metadata extra data that can be used by the index
* @return {@code this}
*/
public CreateIndexOp setMetadata(Map<String, Object> metadata) {
this.metadata = metadata;
return this;
}
/**
* @param canonicalTypeTree the canonical {@link RevTree} of the feature type
* @return {@code this}
*/
public CreateIndexOp setCanonicalTypeTree(RevTree canonicalTypeTree) {
this.canonicalTypeTree = canonicalTypeTree;
return this;
}
/**
* @param featureTypeId the {@link ObjectId} of the feature type
* @return {@code this}
*/
public CreateIndexOp setFeatureTypeId(ObjectId featureTypeId) {
this.featureTypeId = featureTypeId;
return this;
}
/**
* Build index trees for the full history of the feature tree.
*
* @param indexHistory if {@code true}, index trees will be built for every commit that contains
* the given feature type tree
* @return {@code this}
*/
public CreateIndexOp setIndexHistory(boolean indexHistory) {
this.indexHistory = indexHistory;
return this;
}
}