Skip to content

Commit

Permalink
MODE-1291 MODE-1581 Updated InfinispanBinaryStore implementation to r…
Browse files Browse the repository at this point in the history
…eflect recent backup service changes

For MODE-1581, a new method was added to the BinaryStore interface to enable the backup service to
obtain all of the BinaryKey values from the store. This commit incorporates the change into the
now-implemented InfinispanBinaryStore.
  • Loading branch information
rhauch committed Aug 18, 2012
2 parents 0a19491 + 5b89f44 commit db9266e
Show file tree
Hide file tree
Showing 18 changed files with 1,582 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,21 @@
*/
package org.modeshape.common.util;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import org.modeshape.common.annotation.Immutable;
import org.modeshape.common.logging.Logger;

/**
* A set of utilities for more easily performing I/O.
*/
@Immutable
public class IoUtil {

private static final Logger LOGGER = Logger.getLogger(IoUtil.class);

/**
* Read and return the entire contents of the supplied {@link InputStream stream}. This method always closes the stream when
* finished reading.
Expand Down Expand Up @@ -156,6 +147,19 @@ public static String read( InputStream stream ) throws IOException {
return stream == null ? "" : read(new InputStreamReader(stream));
}

/**
* Read and return the entire contents of the supplied {@link InputStream}. This method always closes the stream when finished
* reading.
*
* @param stream the streamed contents; may be null
* @param charset charset of the stream data; may not be null
* @return the contents, or an empty string if the supplied stream is null
* @throws IOException if there is an error reading the content
*/
public static String read( InputStream stream, String charset ) throws IOException {
return stream == null ? "" : read(new InputStreamReader(stream, charset));
}

/**
* Read and return the entire contents of the supplied {@link File}.
*
Expand Down Expand Up @@ -503,6 +507,20 @@ public static InputStream getResourceAsStream( String resourcePath,
return result;
}

/**
* Closes the closable silently. Any exceptions are ignored.
*/
public static void closeQuietly(Closeable closeable){
if(closeable == null){
return;
}
try {
closeable.close();
} catch (Throwable t){
LOGGER.debug(t, "Ignored error at closing stream");
}
}

private IoUtil() {
// Prevent construction
}
Expand Down
6 changes: 6 additions & 0 deletions modeshape-jcr/src/main/java/org/modeshape/jcr/JcrI18n.java
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,9 @@ public final class JcrI18n {
public static I18n errorDuringGarbageCollection;
public static I18n errorMarkingBinaryValuesUnused;
public static I18n errorMarkingBinaryValuesUsed;
public static I18n errorStoringMimeType;
public static I18n errorStoringExtractedText;
public static I18n errorReadingExtractedText;

public static I18n unableToReadTemporaryDirectory;
public static I18n unableToWriteTemporaryDirectory;
Expand All @@ -401,6 +404,9 @@ public final class JcrI18n {
public static I18n unableToFindBinaryValue;
public static I18n tempDirectorySystemPropertyMustBeSet;
public static I18n tempDirectoryLocation;
public static I18n errorReadingBinaryValue;
public static I18n errorStoringBinaryValue;
public static I18n errorLockingBinaryValue;

public static I18n errorKillingRepository;
public static I18n errorKillingEngine;
Expand Down
10 changes: 2 additions & 8 deletions modeshape-jcr/src/main/java/org/modeshape/jcr/JcrRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
import org.modeshape.jcr.value.binary.AbstractBinaryStore;
import org.modeshape.jcr.value.binary.BinaryStore;
import org.modeshape.jcr.value.binary.BinaryUsageChangeSetListener;
import org.modeshape.jcr.value.binary.InfinispanBinaryStore;
import org.modeshape.jcr.value.binary.infinispan.InfinispanBinaryStore;

/**
*
Expand Down Expand Up @@ -889,8 +889,7 @@ void cleanUp() {
BinaryStore store = running.binaryStore();
if (store instanceof InfinispanBinaryStore) {
InfinispanBinaryStore ispnStore = (InfinispanBinaryStore)store;
Cache<?, ?> binaryCache = ispnStore.getCache();
if (binaryCache != null) caches.add(binaryCache);
caches.addAll(ispnStore.getCaches());
}

return caches;
Expand Down Expand Up @@ -1045,11 +1044,6 @@ protected RunningState( JcrRepository.RunningState other,
// Set up the binary store ...
BinaryStorage binaryStorageConfig = config.getBinaryStorage();
binaryStore = binaryStorageConfig.getBinaryStore();
if (binaryStore instanceof InfinispanBinaryStore) {
InfinispanBinaryStore ispanBinStore = (InfinispanBinaryStore)binaryStore;
ispanBinStore.setContentCacheName(cacheName);
ispanBinStore.setContentCacheContainer(container);
}
binaryStore.start();
tempContext = tempContext.with(binaryStore);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
import org.modeshape.jcr.value.binary.AbstractBinaryStore;
import org.modeshape.jcr.value.binary.DatabaseBinaryStore;
import org.modeshape.jcr.value.binary.FileSystemBinaryStore;
import org.modeshape.jcr.value.binary.InfinispanBinaryStore;
import org.modeshape.jcr.value.binary.infinispan.InfinispanBinaryStore;
import org.modeshape.jcr.value.binary.TransientBinaryStore;

/**
Expand Down Expand Up @@ -958,14 +958,20 @@ public AbstractBinaryStore getBinaryStore() throws NamingException, IOException
store = new DatabaseBinaryStore(dataSourceJndi);
}
} else if (type.equalsIgnoreCase("cache")) {
String cacheName = binaryStorage.getString(FieldName.CACHE_NAME, getName());
String metadataCacheName = binaryStorage.getString(FieldName.METADATA_CACHE_NAME, getName());
String blobCacheName = binaryStorage.getString(FieldName.DATA_CACHE_NAME, getName());
String cacheConfiguration = binaryStorage.getString(FieldName.CACHE_CONFIGURATION); // may be null
if (cacheConfiguration == null) cacheConfiguration = getCacheConfiguration();
boolean dedicatedCacheContainer = false;
if (cacheConfiguration == null) {
cacheConfiguration = getCacheConfiguration();
} else {
dedicatedCacheContainer = true;
}
CacheContainer cacheContainer = getCacheContainer(cacheConfiguration);

// String cacheTransactionManagerLookupClass = binaryStorage.getString(FieldName.CACHE_TRANSACTION_MANAGER_LOOKUP,
// Default.CACHE_TRANSACTION_MANAGER_LOOKUP);
store = new InfinispanBinaryStore(cacheName, cacheContainer);
store = new InfinispanBinaryStore(cacheContainer, dedicatedCacheContainer, metadataCacheName, blobCacheName);
}
if (store == null) store = TransientBinaryStore.get();
store.setMinimumBinarySizeInBytes(getMinimumBinarySizeInBytes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

import java.io.Serializable;
import java.security.NoSuchAlgorithmException;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import org.modeshape.common.SystemFailureException;
import org.modeshape.common.annotation.Immutable;
import org.modeshape.common.util.SecureHash;
Expand All @@ -39,12 +37,9 @@
public class BinaryKey implements Serializable, Comparable<BinaryKey> {
private static final long serialVersionUID = 1L;

protected static final Set<String> ALGORITHMS_NOT_FOUND_AND_LOGGED = new CopyOnWriteArraySet<String>();
private static final SecureHash.Algorithm ALGORITHM = SecureHash.Algorithm.SHA_1;

public static BinaryKey keyFor( byte[] content ) {
try {
byte[] hash = SecureHash.getHash(ALGORITHM, content);
byte[] hash = SecureHash.getHash(SecureHash.Algorithm.SHA_1, content);
return new BinaryKey(hash);
} catch (NoSuchAlgorithmException e) {
throw new SystemFailureException(e);
Expand All @@ -60,7 +55,7 @@ public BinaryKey( String key ) {
}

public BinaryKey( byte[] hash ) {
this.key = StringUtil.getHexString(hash);
this(StringUtil.getHexString(hash));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public StoredBinaryValue( BinaryStore store,
this.store = store;
this.size = size;
assert this.store != null;
assert this.size > 0L;
assert this.size >= 0L;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* ModeShape (http://www.modeshape.org)
* See the COPYRIGHT.txt file distributed with this work for information
* regarding copyright ownership. Some portions may be licensed
* to Red Hat, Inc. under one or more contributor license agreements.
* See the AUTHORS.txt file in the distribution for a full listing of
* individual contributors.
*
* ModeShape is free software. Unless otherwise indicated, all code in ModeShape
* is licensed to you under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* ModeShape is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.modeshape.jcr.value.binary.infinispan;

import org.infinispan.Cache;
import org.modeshape.common.logging.Logger;

import java.io.IOException;
import java.io.InputStream;

/**
* Merges chunks from cache and provides InputStream-feeling.
*/
class ChunkInputStream extends InputStream {

private final Logger logger;
private final Cache<String, byte[]> blobCache;
private final String key;

protected int indexInBuffer;
protected byte[] buffer;
private int chunkNumber;


public ChunkInputStream(Cache<String, byte[]> blobCache, String key){
logger = Logger.getLogger(getClass());
this.blobCache = blobCache;
this.key = key;
}

@Override
public int read() throws IOException {
if(indexInBuffer == -1){
return -1;
}
if(buffer == null || indexInBuffer >= buffer.length){
fillBuffer();
return read();
}
return buffer[indexInBuffer++] & 0xff;
}

@Override
public int read(byte[] b, int off, int len) throws IOException {
if(indexInBuffer == -1){
return -1;
}
if(buffer == null){
fillBuffer();
return read(b, off, len);
}
if(indexInBuffer >= buffer.length){
return -1;
}
if (indexInBuffer + len > buffer.length){
len = buffer.length - indexInBuffer;
}
System.arraycopy(buffer, indexInBuffer, b, off, len);
indexInBuffer += len;
if(indexInBuffer >= buffer.length){
fillBuffer();
}
return len;
}

@Override
public int available() {
return buffer.length - indexInBuffer;
}

@Override
public final long skip(long n) throws IOException {
if(n <= 0 || indexInBuffer == -1){
return 0;
}
if(buffer == null){
fillBuffer();
return skip(n);
}
if (buffer.length + n > indexInBuffer){
n = buffer.length - indexInBuffer;
}
if (n < 0){
return 0;
}
indexInBuffer += n;
return n;
}

private void fillBuffer() throws IOException {

buffer = nextChunk();
if(buffer == null){
buffer = new byte[0];
indexInBuffer = -1;
} else {
indexInBuffer = 0;
}
}

protected byte[] nextChunk() throws IOException {
String chunkKey = key+"-"+chunkNumber++;
logger.debug("Read chunk {0}", chunkKey);
return blobCache.get(chunkKey);
}
}
Loading

0 comments on commit db9266e

Please sign in to comment.