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 net.jini.core.lease.Lease;
023: import org.apache.lucene.store.Directory;
024: import org.apache.lucene.store.IndexInput;
025: import org.apache.lucene.store.IndexOutput;
026: import org.openspaces.core.GigaSpace;
027:
028: /**
029: * GigaSpace Directory is a Lucene directory built on top of GigaSpaces.
030: *
031: * <p>The direcotry implementation uses {@link FileEntry}
032: * as the meta data of a file written to the Space. And one or more {@link FileBucketEntry}
033: * to hold the entry data. Note, the bucket size can eb controlled during index creation, but if connecting
034: * to an existing index, the bucket index must be the same.
035: *
036: * @author kimchy
037: */
038: public class GigaSpaceDirectory extends Directory {
039:
040: public static final int DEFAULT_BUCKET_SIZE = 20 * 1024;
041:
042: private IJSpace space;
043:
044: private String indexName;
045:
046: private int bucketSize = DEFAULT_BUCKET_SIZE;
047:
048: public GigaSpaceDirectory(GigaSpace gigaSpace, String indexName) {
049: this (gigaSpace.getSpace(), indexName);
050: }
051:
052: public GigaSpaceDirectory(IJSpace space, String indexName) {
053: this (space, indexName, DEFAULT_BUCKET_SIZE);
054: }
055:
056: public GigaSpaceDirectory(IJSpace space, String indexName,
057: int bucketSize) {
058: this .space = space;
059: this .indexName = indexName;
060: this .bucketSize = bucketSize;
061: setLockFactory(new GigaSpaceLockFactory(space, indexName));
062: }
063:
064: public void deleteContent() throws IOException {
065: FileEntry fileEntry = new FileEntry(indexName, null);
066: try {
067: space.clear(fileEntry, null);
068: } catch (Exception e) {
069: throw new GigaSpaceDirectoryException(indexName, null,
070: "Delete failed", e);
071: }
072: FileBucketEntry fileBucketEntry = new FileBucketEntry(
073: indexName, null);
074: try {
075: space.clear(fileBucketEntry, null);
076: } catch (Exception e) {
077: throw new GigaSpaceDirectoryException(indexName, null,
078: "Delete failed", e);
079: }
080: }
081:
082: public IJSpace getSpace() {
083: return space;
084: }
085:
086: public String getIndexName() {
087: return indexName;
088: }
089:
090: public int getBucketSize() {
091: return bucketSize;
092: }
093:
094: public void deleteFile(String fileName) throws IOException {
095: FileEntry fileEntry = new FileEntry(indexName, fileName);
096: try {
097: space.clear(fileEntry, null);
098: } catch (Exception e) {
099: throw new GigaSpaceDirectoryException(indexName, fileName,
100: "Delete failed", e);
101: }
102: FileBucketEntry fileBucketEntry = new FileBucketEntry(
103: indexName, fileName);
104: try {
105: space.clear(fileBucketEntry, null);
106: } catch (Exception e) {
107: throw new GigaSpaceDirectoryException(indexName, fileName,
108: "Delete failed", e);
109: }
110: }
111:
112: public boolean fileExists(String fileName) throws IOException {
113: FileEntry fileEntry = new FileEntry(indexName, fileName);
114: try {
115: int count = space.count(fileEntry, null);
116: return count > 0;
117: } catch (Exception e) {
118: throw new GigaSpaceDirectoryException(indexName, fileName,
119: "File exists failed", e);
120: }
121: }
122:
123: public long fileLength(String fileName) throws IOException {
124: FileEntry fileEntryTemplate = new FileEntry(indexName, fileName);
125: try {
126: FileEntry fileEntry = (FileEntry) space.read(
127: fileEntryTemplate, null, 0);
128: if (fileEntry == null) {
129: return 0;
130: }
131: return fileEntry.getSize();
132: } catch (Exception e) {
133: throw new GigaSpaceDirectoryException(indexName, fileName,
134: "File length failed", e);
135: }
136: }
137:
138: public long fileModified(String fileName) throws IOException {
139: FileEntry fileEntryTemplate = new FileEntry(indexName, fileName);
140: try {
141: FileEntry fileEntry = (FileEntry) space.read(
142: fileEntryTemplate, null, 0);
143: if (fileEntry == null) {
144: return 0;
145: }
146: return fileEntry.getLastModified();
147: } catch (Exception e) {
148: throw new GigaSpaceDirectoryException(indexName, fileName,
149: "File modified failed", e);
150: }
151: }
152:
153: public String[] list() throws IOException {
154: FileEntry fileEntryTemplate = new FileEntry(indexName, null);
155: try {
156: Object[] results = space.readMultiple(fileEntryTemplate,
157: null, Integer.MAX_VALUE);
158: String[] retVal = new String[results.length];
159: for (int i = 0; i < results.length; i++) {
160: retVal[i] = ((FileEntry) results[i]).getFileName();
161: }
162: return retVal;
163: } catch (Exception e) {
164: throw new GigaSpaceDirectoryException(indexName, null,
165: "File list failed", e);
166: }
167: }
168:
169: public void renameFile(String from, String to) throws IOException {
170: // renameFile is not used within Lucene anymore
171: throw new UnsupportedOperationException(
172: "rename file not supported with GigaSpace direcotry");
173: }
174:
175: public void touchFile(String fileName) throws IOException {
176: FileEntry fileEntryTemplate = new FileEntry(indexName, fileName);
177: try {
178: FileEntry fileEntry = (FileEntry) space.take(
179: fileEntryTemplate, null, 0);
180: if (fileEntry == null) {
181: fileEntry = new FileEntry(indexName, fileName, 0);
182: }
183: fileEntry.touch();
184: space.write(fileEntry, null, Lease.FOREVER);
185: } catch (Exception e) {
186: throw new GigaSpaceDirectoryException(indexName, fileName,
187: "File touch failed", e);
188: }
189: }
190:
191: public IndexInput openInput(String fileName) throws IOException {
192: FileEntry fileEntry = new FileEntry(indexName, fileName);
193: try {
194: fileEntry = (FileEntry) space.read(fileEntry, null, 0);
195: } catch (Exception e) {
196: throw new GigaSpaceDirectoryException(indexName, fileName,
197: "File to read file entry", e);
198: }
199: if (fileEntry == null) {
200: throw new GigaSpaceDirectoryException(indexName, fileName,
201: "Failed to find file entry");
202: }
203: return new GigaSpaceIndexInput(this , fileEntry);
204: }
205:
206: public IndexOutput createOutput(String fileName) throws IOException {
207: return new GigaSpaceMemIndexOutput(this , fileName);
208: }
209:
210: /**
211: * Does nothing since an already constructed Space is passes to this
212: * directory.
213: */
214: public void close() throws IOException {
215: }
216:
217: }
|