001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.needle.gigaspaces.store;
018:
019: import java.io.IOException;
020:
021: import com.j_spaces.core.IJSpace;
022: import com.j_spaces.core.client.SpaceFinder;
023: import org.apache.lucene.store.Directory;
024: import org.compass.core.CompassException;
025: import org.compass.core.config.CompassConfigurable;
026: import org.compass.core.config.CompassEnvironment;
027: import org.compass.core.config.CompassSettings;
028: import org.compass.core.config.ConfigurationException;
029: import org.compass.core.engine.SearchEngineException;
030: import org.compass.core.lucene.engine.store.AbstractDirectoryStore;
031: import org.compass.core.lucene.engine.store.CopyFromHolder;
032:
033: /**
034: * A plugin lucene store for Compass. Uses {@link GigaSpaceDirectory}
035: * as Lucene directory implementation.
036: *
037: * @author kimchy
038: */
039: public class GigaSpaceDirectoryStore extends AbstractDirectoryStore
040: implements CompassConfigurable {
041:
042: public static final String PROTOCOL = "space://";
043:
044: public static final String BUCKET_SIZE_PROP = "compass.engine.store.space.bucketSize";
045:
046: private String indexName;
047:
048: private IJSpace space;
049:
050: private int bucketSize;
051:
052: public void configure(CompassSettings settings)
053: throws CompassException {
054: String connection = settings.getSetting(
055: CompassEnvironment.CONNECTION).substring(
056: PROTOCOL.length());
057: int index = connection.indexOf(':');
058: this .indexName = connection.substring(0, index);
059:
060: String spaceUrl = connection.substring(index + 1);
061: bucketSize = settings.getSettingAsInt(BUCKET_SIZE_PROP,
062: GigaSpaceDirectory.DEFAULT_BUCKET_SIZE);
063: try {
064: space = (IJSpace) SpaceFinder.find(spaceUrl, settings
065: .getProperties());
066: } catch (Exception e) {
067: throw new ConfigurationException("Failed to find Space ["
068: + spaceUrl + "]", e);
069: }
070: }
071:
072: public Directory open(String subContext, String subIndex)
073: throws SearchEngineException {
074: return new GigaSpaceDirectory(space, buildFullIndexName(
075: subContext, subIndex), bucketSize);
076: }
077:
078: public void deleteIndex(Directory dir, String subContext,
079: String subIndex) throws SearchEngineException {
080: cleanIndex(dir, subContext, subIndex);
081: }
082:
083: public void cleanIndex(Directory dir, String subContext,
084: String subIndex) throws SearchEngineException {
085: try {
086: ((GigaSpaceDirectory) dir).deleteContent();
087: } catch (IOException e) {
088: throw new SearchEngineException(
089: "Failed to delete index for sub context ["
090: + subContext + "] and sub index ["
091: + subIndex + "]", e);
092: }
093: }
094:
095: public CopyFromHolder beforeCopyFrom(String subContext,
096: String subIndex, Directory dir)
097: throws SearchEngineException {
098: try {
099: ((GigaSpaceDirectory) dir).deleteContent();
100: } catch (IOException e) {
101: throw new SearchEngineException(
102: "Failed to delete context before copy from", e);
103: }
104: return new CopyFromHolder();
105: }
106:
107: private String buildFullIndexName(String subContext, String subIndex) {
108: return indexName + "X" + subContext + "X" + subIndex;
109: }
110: }
|