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.apache.lucene.index;
018:
019: import java.io.IOException;
020:
021: import org.apache.lucene.store.Directory;
022: import org.apache.lucene.store.IndexInput;
023: import org.apache.lucene.store.IndexOutput;
024:
025: /**
026: * @author kimchy
027: */
028: public abstract class LuceneUtils {
029:
030: /**
031: * Copies one directory contents to the other. Will automatically compound or uncompound the contents of the
032: * src directory into the dest directory.
033: *
034: * @param src The src directory to copy from
035: * @param dest The dest directory to copy to
036: * @param buffer The buffer to use when copying over
037: * @throws IOException
038: */
039: public static void copy(final Directory src, final Directory dest,
040: final byte[] buffer) throws IOException {
041: String[] files = src.list();
042: if (files != null) {
043: for (String name : files) {
044: copy(src, dest, name, buffer);
045: }
046: }
047: }
048:
049: /**
050: * Copies over the contents of the name entry from the src directory into the dest directory.
051: *
052: * @param src The src directory to copy from
053: * @param dest The dest directory to copy to
054: * @param name the name of the entry
055: * @param buffer The buffer to use
056: * @throws IOException
057: */
058: public static void copy(final Directory src, final Directory dest,
059: final String name, byte[] buffer) throws IOException {
060: if (!src.fileExists(name)) {
061: return;
062: }
063: IndexInput indexInput = null;
064: IndexOutput indexOutput = null;
065: try {
066: indexInput = src.openInput(name);
067: indexOutput = dest.createOutput(name);
068:
069: copy(indexInput, indexOutput, name, buffer);
070:
071: } finally {
072: if (indexInput != null) {
073: indexInput.close();
074: }
075: if (indexOutput != null) {
076: indexOutput.close();
077: }
078: }
079: }
080:
081: /**
082: * Copies the contents of the <code>IndexInput</code> into the <code>IndexOutput</code>.
083: *
084: * @param indexInput The content to copy from
085: * @param indexOutput The output to write to
086: * @param name The name of the file
087: * @param buffer The buffer to use
088: * @throws IOException
089: */
090: public static void copy(final IndexInput indexInput,
091: final IndexOutput indexOutput, String name, byte[] buffer)
092: throws IOException {
093: long length = indexInput.length();
094: long remainder = length;
095: int chunk = buffer.length;
096:
097: while (remainder > 0) {
098: int len = (int) Math.min(chunk, remainder);
099: indexInput.readBytes(buffer, 0, len);
100: indexOutput.writeBytes(buffer, len);
101: remainder -= len;
102: }
103:
104: // Verify that remainder is 0
105: if (remainder != 0)
106: throw new IOException(
107: "Non-zero remainder length after copying ["
108: + remainder + "] (id [" + name
109: + "] length [" + length + "] buffer size ["
110: + chunk + "])");
111: }
112:
113: /**
114: * Returns <code>true</code> if all the segments of the directory are in compound format.
115: * Will return <code>true</code> if the index does not exists or there are no segments.
116: */
117: public static boolean isCompound(final Directory directory)
118: throws IOException {
119: if (!IndexReader.indexExists(directory)) {
120: return true;
121: }
122: final SegmentInfos segmentInfos = new SegmentInfos();
123: segmentInfos.read(directory);
124: if (segmentInfos.isEmpty()) {
125: return true;
126: }
127: for (int i = 0; i < segmentInfos.size(); i++) {
128: SegmentInfo segmentInfo = segmentInfos.info(i);
129: if (!segmentInfo.getUseCompoundFile()) {
130: return false;
131: }
132: }
133: return true;
134: }
135:
136: /**
137: * Returns <code>true</code> if all the segments of the directory are in un-compound format.
138: * Will return <code>true</code> if the index does not exists or there are no segments.
139: */
140: public static boolean isUnCompound(final Directory directory)
141: throws IOException {
142: if (!IndexReader.indexExists(directory)) {
143: return true;
144: }
145: final SegmentInfos segmentInfos = new SegmentInfos();
146: segmentInfos.read(directory);
147: if (segmentInfos.isEmpty()) {
148: return true;
149: }
150: for (int i = 0; i < segmentInfos.size(); i++) {
151: SegmentInfo segmentInfo = segmentInfos.info(i);
152: if (segmentInfo.getUseCompoundFile()) {
153: return false;
154: }
155: }
156: return true;
157: }
158: }
|