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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
/*
* Copyright 2006-2012 Amazon Technologies, Inc. or its affiliates.
* Amazon, Amazon.com and Carbonado are trademarks or registered trademarks
* of Amazon Technologies, Inc. or its affiliates. All rights reserved.
*
* Licensed 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.amazon.carbonado.repo.indexed;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import com.amazon.carbonado.Cursor;
import com.amazon.carbonado.IsolationLevel;
import com.amazon.carbonado.MalformedTypeException;
import com.amazon.carbonado.Repository;
import com.amazon.carbonado.RepositoryException;
import com.amazon.carbonado.Storable;
import com.amazon.carbonado.Storage;
import com.amazon.carbonado.SupportException;
import com.amazon.carbonado.Transaction;
import com.amazon.carbonado.capability.Capability;
import com.amazon.carbonado.capability.IndexInfo;
import com.amazon.carbonado.capability.IndexInfoCapability;
import com.amazon.carbonado.capability.StorableInfoCapability;
import com.amazon.carbonado.info.StorableIntrospector;
import com.amazon.carbonado.qe.RepositoryAccess;
import com.amazon.carbonado.qe.StorageAccess;
import com.amazon.carbonado.spi.StoragePool;
/**
* Wraps another repository in order to make it support indexes. The wrapped
* repository must support creation of new types.
*
* @author Brian S O'Neill
*/
class IndexedRepository implements Repository,
RepositoryAccess,
IndexInfoCapability,
StorableInfoCapability,
IndexEntryAccessCapability
{
private final AtomicReference<Repository> mRootRef;
private final Repository mRepository;
private final String mName;
private final boolean mIndexRepairEnabled;
private final double mIndexThrottle;
private final boolean mIndexDiscardDuplicates;
private final boolean mIndexRepairVerifyOnly;
private final boolean mAllClustered;
private final boolean mStrictTriggers;
private final StoragePool mStoragePool;
private final IndexAnalysisPool mIndexAnalysisPool;
IndexedRepository(AtomicReference<Repository> rootRef, String name,
Repository repository,
boolean indexRepairEnabled,
double indexThrottle,
boolean indexDiscardDuplicates,
boolean indexRepairVerifyOnly,
boolean allClustered,
boolean strictTriggers)
{
if (repository.getCapability(IndexInfoCapability.class) == null) {
throw new UnsupportedOperationException
("Wrapped repository doesn't support being indexed -- " +
"it must support IndexInfoCapability.");
}
mRootRef = rootRef;
mRepository = repository;
mName = name;
mIndexRepairEnabled = indexRepairEnabled;
mIndexThrottle = indexThrottle;
mIndexDiscardDuplicates = indexDiscardDuplicates;
mIndexRepairVerifyOnly = indexRepairVerifyOnly;
mAllClustered = allClustered;
mStrictTriggers = strictTriggers;
mIndexAnalysisPool = new IndexAnalysisPool(this);
mStoragePool = new StoragePool() {
@Override
protected <S extends Storable> Storage<S> createStorage(Class<S> type)
throws RepositoryException
{
Storage<S> masterStorage = mRepository.storageFor(type);
if (Unindexed.class.isAssignableFrom(type)) {
// Verify no indexes.
int indexCount = IndexAnalysis
.gatherDesiredIndexes(StorableIntrospector.examine(type)).size();
if (indexCount > 0) {
throw new MalformedTypeException
(type, "Storable cannot have any indexes: " + type +
", " + indexCount);
}
return masterStorage;
}
IndexAnalysis<S> analysis = mIndexAnalysisPool.get(masterStorage);
return new IndexedStorage<S>(analysis);
}
};
}
public String getName() {
return mName;
}
@SuppressWarnings("unchecked")
public <S extends Storable> Storage<S> storageFor(Class<S> type)
throws MalformedTypeException, SupportException, RepositoryException
{
return mStoragePool.get(type);
}
public Transaction enterTransaction() {
return mRepository.enterTransaction();
}
public Transaction enterTransaction(IsolationLevel level) {
return mRepository.enterTransaction(level);
}
public Transaction enterTopTransaction(IsolationLevel level) {
return mRepository.enterTopTransaction(level);
}
public IsolationLevel getTransactionIsolationLevel() {
return mRepository.getTransactionIsolationLevel();
}
@SuppressWarnings("unchecked")
public <C extends Capability> C getCapability(Class<C> capabilityType) {
if (capabilityType.isInstance(this)) {
return (C) this;
}
return mRepository.getCapability(capabilityType);
}
// Required by IndexInfoCapability.
public <S extends Storable> IndexInfo[] getIndexInfo(Class<S> storableType)
throws RepositoryException
{
if (Unindexed.class.isAssignableFrom(storableType)) {
return new IndexInfo[0];
}
Storage<S> masterStorage = mRepository.storageFor(storableType);
IndexAnalysis<S> analysis = mIndexAnalysisPool.get(masterStorage);
IndexInfo[] infos = new IndexInfo[analysis.allIndexInfoMap.size()];
return analysis.allIndexInfoMap.values().toArray(infos);
}
// Required by IndexEntryAccessCapability.
public <S extends Storable> IndexEntryAccessor<S>[]
getIndexEntryAccessors(Class<S> storableType)
throws RepositoryException
{
if (Unindexed.class.isAssignableFrom(storableType)) {
return new IndexEntryAccessor[0];
}
Storage<S> masterStorage = mRepository.storageFor(storableType);
IndexAnalysis<S> analysis = mIndexAnalysisPool.get(masterStorage);
List<IndexEntryAccessor<S>> accessors =
new ArrayList<IndexEntryAccessor<S>>(analysis.allIndexInfoMap.size());
for (IndexInfo info : analysis.allIndexInfoMap.values()) {
if (info instanceof IndexEntryAccessor) {
accessors.add((IndexEntryAccessor<S>) info);
}
}
return accessors.toArray(new IndexEntryAccessor[accessors.size()]);
}
public String[] getUserStorableTypeNames() throws RepositoryException {
StorableInfoCapability cap = mRepository.getCapability(StorableInfoCapability.class);
if (cap == null) {
return new String[0];
}
ArrayList<String> names =
new ArrayList<String>(Arrays.asList(cap.getUserStorableTypeNames()));
// Exclude our own metadata types as well as indexes.
names.remove(StoredIndexInfo.class.getName());
Cursor<StoredIndexInfo> cursor =
mRepository.storageFor(StoredIndexInfo.class)
.query().fetch();
try {
while (cursor.hasNext()) {
StoredIndexInfo info = cursor.next();
names.remove(info.getIndexName());
}
} finally {
cursor.close();
}
return names.toArray(new String[names.size()]);
}
public boolean isSupported(Class<Storable> type) {
StorableInfoCapability cap = mRepository.getCapability(StorableInfoCapability.class);
return (cap == null) ? false : cap.isSupported(type);
}
public boolean isPropertySupported(Class<Storable> type, String name) {
StorableInfoCapability cap = mRepository.getCapability(StorableInfoCapability.class);
return (cap == null) ? false : cap.isPropertySupported(type, name);
}
public void close() {
mRepository.close();
}
/*
public boolean isClosed() {
return mRepository.isClosed();
}
*/
public Repository getRootRepository() {
return mRootRef.get();
}
public <S extends Storable> StorageAccess<S> storageAccessFor(Class<S> type)
throws SupportException, RepositoryException
{
return (StorageAccess<S>) storageFor(type);
}
Storage<?> getIndexEntryStorageFor(Class<? extends Storable> indexEntryClass)
throws RepositoryException
{
return mRepository.storageFor(indexEntryClass);
}
Repository getWrappedRepository() {
return mRepository;
}
boolean isIndexRepairEnabled() {
return mIndexRepairEnabled;
}
double getIndexRepairThrottle() {
return mIndexThrottle;
}
boolean getIndexDiscardDuplicates() {
return mIndexDiscardDuplicates;
}
boolean getIndexRepairVerifyOnly() {
return mIndexRepairVerifyOnly;
}
boolean isAllClustered() {
return mAllClustered;
}
boolean isStrictTriggers() {
return mStrictTriggers;
}
}
|