/
CompositeBinaryStore.java
452 lines (366 loc) · 15.7 KB
/
CompositeBinaryStore.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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
* ModeShape (http://www.modeshape.org)
*
* 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 org.modeshape.jcr.value.binary;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.jcr.RepositoryException;
import org.modeshape.common.collection.Collections;
import org.modeshape.common.logging.Logger;
import org.modeshape.common.util.CheckArg;
import org.modeshape.jcr.JcrI18n;
import org.modeshape.jcr.TextExtractors;
import org.modeshape.jcr.mimetype.MimeTypeDetector;
import org.modeshape.jcr.mimetype.NullMimeTypeDetector;
import org.modeshape.jcr.text.TextExtractorContext;
import org.modeshape.jcr.value.BinaryKey;
import org.modeshape.jcr.value.BinaryValue;
/**
* A {@link BinaryStore} implementation that stores files in other BinaryStores. This store is initialized with a map of number of
* BinaryStores. On retrieval, the CompositeBinaryStore will look in all the other BinaryStores for the value. When storing a
* value, the CompositeBinaryStore may receive a StorageHint that MAY be used when determining which named BinaryStore to write
* to. If a storage hint is not provided (or doesn't match a store), the value will be stored in the default store.
*/
public class CompositeBinaryStore implements BinaryStore {
private static final String DEFAULT_STRATEGY_HINT = "default";
private volatile TextExtractors extractors;
private volatile MimeTypeDetector detector = NullMimeTypeDetector.INSTANCE;
protected Logger logger = Logger.getLogger(getClass());
private Map<String, BinaryStore> namedStores;
private BinaryStore defaultBinaryStore;
/**
* Initialize a new CompositeBinaryStore using a Map of other BinaryKeys that are keyed by an implementer-provided key. The
* named stores must include a default BinaryStore that will be used in the absence of storage hints.
*
* @param namedStores a {@code Map} of inner stores, grouped by the hint.
*/
public CompositeBinaryStore( Map<String, BinaryStore> namedStores ) {
this.namedStores = namedStores;
this.defaultBinaryStore = null;
}
/**
* Initialize the store, and initialize all the named stores.
*/
@Override
public void start() {
Iterator<Map.Entry<String, BinaryStore>> it = getNamedStoreIterator();
while (it.hasNext()) {
BinaryStore bs = it.next().getValue();
bs.start();
}
}
/**
* Shut down all the named stores
*/
@Override
public void shutdown() {
Iterator<Map.Entry<String, BinaryStore>> it = getNamedStoreIterator();
while (it.hasNext()) {
BinaryStore bs = it.next().getValue();
bs.shutdown();
}
}
@Override
public long getMinimumBinarySizeInBytes() {
long minimumBinarySize = Long.MAX_VALUE;
Iterator<Map.Entry<String, BinaryStore>> it = getNamedStoreIterator();
while (it.hasNext()) {
BinaryStore bs = it.next().getValue();
if (minimumBinarySize > bs.getMinimumBinarySizeInBytes()) {
minimumBinarySize = bs.getMinimumBinarySizeInBytes();
}
}
return minimumBinarySize;
}
@Override
public void setMinimumBinarySizeInBytes( long minSizeInBytes ) {
Iterator<Map.Entry<String, BinaryStore>> it = getNamedStoreIterator();
while (it.hasNext()) {
BinaryStore bs = it.next().getValue();
bs.setMinimumBinarySizeInBytes(minSizeInBytes);
}
}
@Override
public void setTextExtractors( TextExtractors textExtractors ) {
CheckArg.isNotNull(textExtractors, "textExtractors");
this.extractors = textExtractors;
Iterator<Map.Entry<String, BinaryStore>> it = getNamedStoreIterator();
while (it.hasNext()) {
BinaryStore bs = it.next().getValue();
bs.setTextExtractors(textExtractors);
}
}
@Override
public void setMimeTypeDetector( MimeTypeDetector mimeTypeDetector ) {
CheckArg.isNotNull(mimeTypeDetector, "mimeTypeDetector");
this.detector = mimeTypeDetector;
Iterator<Map.Entry<String, BinaryStore>> it = getNamedStoreIterator();
while (it.hasNext()) {
BinaryStore bs = it.next().getValue();
bs.setMimeTypeDetector(mimeTypeDetector);
}
}
@Override
public BinaryValue storeValue( InputStream stream, boolean markAsUnused ) throws BinaryStoreException {
return storeValue(stream, DEFAULT_STRATEGY_HINT, markAsUnused);
}
@Override
public BinaryValue storeValue( InputStream stream, String hint, boolean markAsUnused ) throws BinaryStoreException {
BinaryStore binaryStore = selectBinaryStore(hint);
BinaryValue bv = binaryStore.storeValue(stream, markAsUnused);
logger.debug("Stored binary " + bv.getKey() + " into binary store " + binaryStore + " used=" + markAsUnused);
return bv;
}
/**
* Move a value from one named store to another store
*
* @param key Binary key to transfer from the source store to the destination store
* @param source a hint for discovering the source repository; may be null
* @param destination a hint for discovering the destination repository
* @return the {@link BinaryKey} value of the moved binary, never {@code null}
* @throws BinaryStoreException if a source store cannot be found or the source store does not contain the binary key
*/
public BinaryKey moveValue( BinaryKey key,
String source,
String destination ) throws BinaryStoreException {
final BinaryStore sourceStore;
if (source == null) {
sourceStore = findBinaryStoreContainingKey(key);
} else {
sourceStore = selectBinaryStore(source);
}
// could not find source store, or
if (sourceStore == null || !sourceStore.hasBinary(key)) {
throw new BinaryStoreException(JcrI18n.unableToFindBinaryValue.text(key, sourceStore));
}
BinaryStore destinationStore = selectBinaryStore(destination);
// key is already in the destination store
if (sourceStore.equals(destinationStore)) {
return key;
}
final BinaryValue binaryValue = storeValue(sourceStore.getInputStream(key), destination, false);
sourceStore.markAsUnused(java.util.Collections.singleton(key));
return binaryValue.getKey();
}
/**
* Move a BinaryKey to a named store
*
* @param key Binary key to transfer from the source store to the destination store
* @param destination a hint for discovering the destination repository
* @throws BinaryStoreException if anything unexpected fails
*/
public void moveValue( BinaryKey key,
String destination ) throws BinaryStoreException {
moveValue(key, null, destination);
}
@Override
public InputStream getInputStream( BinaryKey key ) throws BinaryStoreException {
Iterator<Map.Entry<String, BinaryStore>> it = getNamedStoreIterator();
while (it.hasNext()) {
final Map.Entry<String, BinaryStore> entry = it.next();
final String binaryStoreKey = entry.getKey();
BinaryStore binaryStore = entry.getValue();
logger.trace("Checking binary store " + binaryStoreKey + " for key " + key);
try {
return binaryStore.getInputStream(key);
} catch (BinaryStoreException e) {
// this exception is "normal", and is thrown
logger.trace(e, "The named store " + binaryStoreKey + " raised exception");
}
}
throw new BinaryStoreException(JcrI18n.unableToFindBinaryValue.text(key, this.toString()));
}
@Override
public boolean hasBinary( BinaryKey key ) {
Iterator<Map.Entry<String, BinaryStore>> it = getNamedStoreIterator();
while (it.hasNext()) {
BinaryStore bs = it.next().getValue();
if (bs.hasBinary(key)) {
return true;
}
}
return false;
}
@SuppressWarnings( "unused" )
@Override
public void markAsUnused( Iterable<BinaryKey> keys ) throws BinaryStoreException {
Iterator<Map.Entry<String, BinaryStore>> it = getNamedStoreIterator();
while (it.hasNext()) {
Map.Entry<String, BinaryStore> entry = it.next();
final String binaryStoreKey = entry.getKey();
BinaryStore bs = entry.getValue();
try {
bs.markAsUnused(keys);
} catch (BinaryStoreException e) {
logger.debug(e, "The named store " + binaryStoreKey + " raised exception");
}
}
}
@Override
public void markAsUsed( Iterable<BinaryKey> keys ) {
Iterator<Map.Entry<String, BinaryStore>> it = getNamedStoreIterator();
while (it.hasNext()) {
Map.Entry<String, BinaryStore> entry = it.next();
final String binaryStoreKey = entry.getKey();
BinaryStore bs = entry.getValue();
try {
bs.markAsUsed(keys);
} catch (BinaryStoreException e) {
logger.debug(e, "The named store " + binaryStoreKey + " raised exception");
}
}
}
@SuppressWarnings( "unused" )
@Override
public void removeValuesUnusedLongerThan( long minimumAge,
TimeUnit unit ) throws BinaryStoreException {
Iterator<Map.Entry<String, BinaryStore>> it = getNamedStoreIterator();
while (it.hasNext()) {
Map.Entry<String, BinaryStore> entry = it.next();
final String binaryStoreKey = entry.getKey();
BinaryStore bs = entry.getValue();
try {
bs.removeValuesUnusedLongerThan(minimumAge, unit);
} catch (BinaryStoreException e) {
logger.debug(e, "The named store " + binaryStoreKey + " raised exception");
}
}
}
@Override
public String getText( BinaryValue binary ) throws BinaryStoreException {
if (binary instanceof InMemoryBinaryValue) {
if (extractors == null || !extractors.extractionEnabled()) {
return null;
}
// The extracted text will never be stored, so try directly using the text extractors ...
return extractors.extract((InMemoryBinaryValue)binary, new TextExtractorContext(detector));
}
Iterator<Map.Entry<String, BinaryStore>> it = getNamedStoreIterator();
while (it.hasNext()) {
Map.Entry<String, BinaryStore> entry = it.next();
final String binaryStoreKey = entry.getKey();
BinaryStore bs = entry.getValue();
try {
if (bs.hasBinary(binary.getKey())) {
return bs.getText(binary);
}
} catch (BinaryStoreException e) {
logger.debug(e, "The named store " + binaryStoreKey + " raised exception");
if (!it.hasNext()) {
throw e;
}
}
}
throw new BinaryStoreException(JcrI18n.unableToFindBinaryValue.text(binary.getKey(), this));
}
@Override
public String getMimeType( BinaryValue binary,
String name ) throws IOException, RepositoryException {
if (detector == null) {
return null;
}
String detectedMimeType = detector.mimeTypeOf(name, binary);
if (binary instanceof InMemoryBinaryValue) {
return detectedMimeType;
}
Iterator<Map.Entry<String, BinaryStore>> it = getNamedStoreIterator();
while (it.hasNext()) {
Map.Entry<String, BinaryStore> entry = it.next();
final String binaryStoreKey = entry.getKey();
BinaryStore bs = entry.getValue();
try {
if (bs.hasBinary(binary.getKey())) {
return bs.getMimeType(binary, name);
}
} catch (BinaryStoreException e) {
logger.debug(e, "The named store " + binaryStoreKey + " raised exception");
if (!it.hasNext()) {
throw e;
}
}
}
throw new BinaryStoreException(JcrI18n.unableToFindBinaryValue.text(binary.getKey(), this));
}
@Override
public Iterable<BinaryKey> getAllBinaryKeys() throws BinaryStoreException {
Iterable<BinaryKey> generatedIterable = new HashSet<BinaryKey>();
Iterator<Map.Entry<String, BinaryStore>> binaryStoreIterator = getNamedStoreIterator();
while (binaryStoreIterator.hasNext()) {
BinaryStore bs = binaryStoreIterator.next().getValue();
generatedIterable = Collections.concat(generatedIterable, bs.getAllBinaryKeys());
}
return generatedIterable;
}
/**
* Get an iterator over all the named stores
*
* @return an iterator over the map of binary stores and their given names
*/
public Iterator<Map.Entry<String, BinaryStore>> getNamedStoreIterator() {
return namedStores.entrySet().iterator();
}
/**
* Get the named binary store that contains the key
*
* @param key the key to the binary content; never null
* @return the BinaryStore that contains the given key
*/
public BinaryStore findBinaryStoreContainingKey( BinaryKey key ) {
Iterator<Map.Entry<String, BinaryStore>> binaryStoreIterator = getNamedStoreIterator();
while (binaryStoreIterator.hasNext()) {
BinaryStore bs = binaryStoreIterator.next().getValue();
if (bs.hasBinary(key)) {
return bs;
}
}
return null;
}
/**
* Select a named binary store for the given hint
*
* @param hint a hint to a binary store; possibly null
* @return a named BinaryStore from the hint, or the default store
*/
private BinaryStore selectBinaryStore( String hint ) {
BinaryStore namedBinaryStore = null;
if (hint != null) {
logger.trace("Selecting named binary store for hint: " + hint);
namedBinaryStore = namedStores.get(hint);
}
if (namedBinaryStore == null) {
namedBinaryStore = getDefaultBinaryStore();
}
logger.trace("Selected binary store: " + namedBinaryStore.toString());
return namedBinaryStore;
}
private BinaryStore getDefaultBinaryStore() {
if (defaultBinaryStore == null) {
if (namedStores.containsKey(DEFAULT_STRATEGY_HINT)) {
defaultBinaryStore = namedStores.get(DEFAULT_STRATEGY_HINT);
} else {
logger.trace("Did not find a named binary store with the key 'default', picking the first binary store in the list");
final Iterator<BinaryStore> iterator = namedStores.values().iterator();
if (iterator.hasNext()) {
defaultBinaryStore = iterator.next();
}
}
}
return defaultBinaryStore;
}
}