You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by ar...@apache.org on 2006/06/04 04:41:25 UTC
svn commit: r411501 [22/30] - in /incubator/lucene.net/trunk/C#/src: ./
Demo/DeleteFiles/ Demo/DemoLib/ Demo/DemoLib/HTML/ Demo/IndexFiles/
Demo/IndexHtml/ Demo/SearchFiles/ Lucene.Net/ Lucene.Net/Analysis/
Lucene.Net/Analysis/Standard/ Lucene.Net/Docu...
Modified: incubator/lucene.net/trunk/C#/src/Lucene.Net/Search/Weight.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Lucene.Net/Search/Weight.cs?rev=411501&r1=411500&r2=411501&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Lucene.Net/Search/Weight.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Lucene.Net/Search/Weight.cs Sat Jun 3 19:41:13 2006
@@ -13,34 +13,40 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
using System;
using IndexReader = Lucene.Net.Index.IndexReader;
+
namespace Lucene.Net.Search
{
/// <summary>Expert: Calculate query weights and build query scorers.
- ///
- /// <p>A Weight is constructed by a query, given a Searcher ({@link
- /// Query#CreateWeight(Searcher)}). The {@link #SumOfSquaredWeights()} method
- /// is then called on the top-level query to compute the query normalization
- /// factor (@link Similarity#queryNorm(float)}). This factor is then passed to
- /// {@link #Normalize(float)}. At this point the weighting is complete and a
- /// scorer may be constructed by calling {@link #Scorer(IndexReader)}.
+ /// <p>
+ /// The purpose of Weight is to make it so that searching does not modify
+ /// a Query, so that a Query instance can be reused. <br>
+ /// Searcher dependent state of the query should reside in the Weight. <br>
+ /// IndexReader dependent state should reside in the Scorer.
+ /// <p>
+ /// A <code>Weight</code> is used in the following way:
+ /// <ol>
+ /// <li>A <code>Weight</code> is constructed by a top-level query,
+ /// given a <code>Searcher</code> ({@link Query#CreateWeight(Searcher)}).
+ /// <li>The {@link #SumOfSquaredWeights()} method is called
+ /// on the <code>Weight</code> to compute
+ /// the query normalization factor {@link Similarity#QueryNorm(float)}
+ /// of the query clauses contained in the query.
+ /// <li>The query normalization factor is passed to {@link #Normalize(float)}.
+ /// At this point the weighting is complete.
+ /// <li>A <code>Scorer</code> is constructed by {@link #Scorer(IndexReader)}.
+ /// </ol>
/// </summary>
public interface Weight
{
- /// <summary>The query that this concerns. </summary>
- Query Query
- {
- get;
-
- }
- /// <summary>The weight for this query. </summary>
- float Value
- {
- get;
-
- }
+ /// <summary>The query that this concerns. </summary>
+ Query GetQuery();
+
+ /// <summary>The weight for this query. </summary>
+ float GetValue();
/// <summary>The sum of squared weights of contained query clauses. </summary>
float SumOfSquaredWeights();
Modified: incubator/lucene.net/trunk/C#/src/Lucene.Net/Search/WildcardQuery.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Lucene.Net/Search/WildcardQuery.cs?rev=411501&r1=411500&r2=411501&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Lucene.Net/Search/WildcardQuery.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Lucene.Net/Search/WildcardQuery.cs Sat Jun 3 19:41:13 2006
@@ -13,32 +13,46 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
using System;
using IndexReader = Lucene.Net.Index.IndexReader;
using Term = Lucene.Net.Index.Term;
+
namespace Lucene.Net.Search
{
/// <summary>Implements the wildcard search query. Supported wildcards are <code>*</code>, which
/// matches any character sequence (including the empty one), and <code>?</code>,
/// which matches any single character. Note this query can be slow, as it
- /// needs to iterate over all terms. In order to prevent extremely slow WildcardQueries,
- /// a Wildcard term must not start with one of the wildcards <code>*</code> or
+ /// needs to iterate over many terms. In order to prevent extremely slow WildcardQueries,
+ /// a Wildcard term should not start with one of the wildcards <code>*</code> or
/// <code>?</code>.
///
/// </summary>
/// <seealso cref="WildcardTermEnum">
/// </seealso>
[Serializable]
- public class WildcardQuery:MultiTermQuery
+ public class WildcardQuery : MultiTermQuery
{
- public WildcardQuery(Term term):base(term)
+ public WildcardQuery(Term term) : base(term)
{
}
protected internal override FilteredTermEnum GetEnum(IndexReader reader)
{
return new WildcardTermEnum(reader, GetTerm());
+ }
+
+ public override bool Equals(System.Object o)
+ {
+ if (o is WildcardQuery)
+ return base.Equals(o);
+
+ return false;
+ }
+ public override int GetHashCode()
+ {
+ return base.GetHashCode();
}
}
}
Modified: incubator/lucene.net/trunk/C#/src/Lucene.Net/Search/WildcardTermEnum.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Lucene.Net/Search/WildcardTermEnum.cs?rev=411501&r1=411500&r2=411501&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Lucene.Net/Search/WildcardTermEnum.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Lucene.Net/Search/WildcardTermEnum.cs Sat Jun 3 19:41:13 2006
@@ -13,9 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
using System;
using IndexReader = Lucene.Net.Index.IndexReader;
using Term = Lucene.Net.Index.Term;
+
namespace Lucene.Net.Search
{
@@ -26,21 +28,23 @@
/// the enumeration is greater than all that precede it.
///
/// </summary>
- /// <version> $Id: WildcardTermEnum.java,v 1.8 2004/05/11 17:23:21 otis Exp $
+ /// <version> $Id: WildcardTermEnum.java 329859 2005-10-31 17:05:36Z bmesser $
/// </version>
- public class WildcardTermEnum:FilteredTermEnum
+ public class WildcardTermEnum : FilteredTermEnum
{
internal Term searchTerm;
internal System.String field = "";
internal System.String text = "";
internal System.String pre = "";
internal int preLen = 0;
- internal bool fieldMatch = false;
internal bool endEnum = false;
/// <summary> Creates a new <code>WildcardTermEnum</code>. Passing in a
- /// {@link Lucene.Net.Index.Term Term} that does not contain a
+ /// {@link Lucene.Net.index.Term Term} that does not contain a
/// <code>WILDCARD_CHAR</code> will cause an exception to be thrown.
+ /// <p>
+ /// After calling the constructor the enumeration is already pointing to the first
+ /// valid term if such a term exists.
/// </summary>
public WildcardTermEnum(IndexReader reader, Term term):base()
{
@@ -104,86 +108,91 @@
/// </summary>
public static bool WildcardEquals(System.String pattern, int patternIdx, System.String string_Renamed, int stringIdx)
{
- for (int p = patternIdx; ; ++p)
+ int p = patternIdx;
+
+ for (int s = stringIdx; ; ++p, ++s)
{
- for (int s = stringIdx; ; ++p, ++s)
+ // End of string yet?
+ bool sEnd = (s >= string_Renamed.Length);
+ // End of pattern yet?
+ bool pEnd = (p >= pattern.Length);
+
+ // If we're looking at the end of the string...
+ if (sEnd)
{
- // End of string yet?
- bool sEnd = (s >= string_Renamed.Length);
- // End of pattern yet?
- bool pEnd = (p >= pattern.Length);
+ // Assume the only thing left on the pattern is/are wildcards
+ bool justWildcardsLeft = true;
- // If we're looking at the end of the string...
- if (sEnd)
+ // Current wildcard position
+ int wildcardSearchPos = p;
+ // While we haven't found the end of the pattern,
+ // and haven't encountered any non-wildcard characters
+ while (wildcardSearchPos < pattern.Length && justWildcardsLeft)
{
- // Assume the only thing left on the pattern is/are wildcards
- bool justWildcardsLeft = true;
+ // Check the character at the current position
+ char wildchar = pattern[wildcardSearchPos];
- // Current wildcard position
- int wildcardSearchPos = p;
- // While we haven't found the end of the pattern,
- // and haven't encountered any non-wildcard characters
- while (wildcardSearchPos < pattern.Length && justWildcardsLeft)
+ // If it's not a wildcard character, then there is more
+ // pattern information after this/these wildcards.
+ if (wildchar != WILDCARD_CHAR && wildchar != WILDCARD_STRING)
{
- // Check the character at the current position
- char wildchar = pattern[wildcardSearchPos];
- // If it's not a wildcard character, then there is more
- // pattern information after this/these wildcards.
-
- if (wildchar != WILDCARD_CHAR && wildchar != WILDCARD_STRING)
- {
- justWildcardsLeft = false;
- }
- else
- {
- // Look at the next character
- wildcardSearchPos++;
- }
+ justWildcardsLeft = false;
}
-
- // This was a prefix wildcard search, and we've matched, so
- // return true.
- if (justWildcardsLeft)
+ else
{
- return true;
+ // to prevent "cat" matches "ca??"
+ if (wildchar == WILDCARD_CHAR)
+ {
+ return false;
+ }
+
+ // Look at the next character
+ wildcardSearchPos++;
}
}
- // If we've gone past the end of the string, or the pattern,
- // return false.
- if (sEnd || pEnd)
+ // This was a prefix wildcard search, and we've matched, so
+ // return true.
+ if (justWildcardsLeft)
{
- break;
+ return true;
}
-
- // Match a single character, so continue.
- if (pattern[p] == WILDCARD_CHAR)
- {
- continue;
- }
-
- //
- if (pattern[p] == WILDCARD_STRING)
+ }
+
+ // If we've gone past the end of the string, or the pattern,
+ // return false.
+ if (sEnd || pEnd)
+ {
+ break;
+ }
+
+ // Match a single character, so continue.
+ if (pattern[p] == WILDCARD_CHAR)
+ {
+ continue;
+ }
+
+ //
+ if (pattern[p] == WILDCARD_STRING)
+ {
+ // Look at the character beyond the '*'.
+ ++p;
+ // Examine the string, starting at the last character.
+ for (int i = string_Renamed.Length; i >= s; --i)
{
- // Look at the character beyond the '*'.
- ++p;
- // Examine the string, starting at the last character.
- for (int i = string_Renamed.Length; i >= s; --i)
+ if (WildcardEquals(pattern, p, string_Renamed, i))
{
- if (WildcardEquals(pattern, p, string_Renamed, i))
- {
- return true;
- }
+ return true;
}
- break;
- }
- if (pattern[p] != string_Renamed[s])
- {
- break;
}
+ break;
+ }
+ if (pattern[p] != string_Renamed[s])
+ {
+ break;
}
- return false;
}
+ return false;
}
public override void Close()
Added: incubator/lucene.net/trunk/C#/src/Lucene.Net/SharpZipLibAdapter.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Lucene.Net/SharpZipLibAdapter.cs?rev=411501&view=auto
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Lucene.Net/SharpZipLibAdapter.cs (added)
+++ incubator/lucene.net/trunk/C#/src/Lucene.Net/SharpZipLibAdapter.cs Sat Jun 3 19:41:13 2006
@@ -0,0 +1,90 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * 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.
+ */
+
+/// To enable compression support in Lucene.Net 1.9 using .NET 1.1. Framework,
+/// you will need to define 'SHARP_ZIP_LIB' and referance the SharpLibZip
+/// library. The SharpLibZip library can be downloaded from:
+/// http://www.icsharpcode.net/OpenSource/SharpZipLib/
+///
+/// You can use any other cmpression library you have by plugging it into this
+/// code or by providing your own adapter as in this one.
+
+
+#if SHARP_ZIP_LIB
+
+using System;
+
+using ICSharpCode.SharpZipLib;
+using ICSharpCode.SharpZipLib.Zip.Compression;
+using System.IO;
+
+namespace Lucene.Net.Index.Compression
+{
+ public class SharpZipLibAdapter : ICompressionAdapter
+ {
+ public byte[] Compress(byte[] input)
+ {
+ // Create the compressor with highest level of compression
+ Deflater compressor = new Deflater();
+ compressor.SetLevel(Deflater.BEST_COMPRESSION);
+
+ // Give the compressor the data to compress
+ compressor.SetInput(input);
+ compressor.Finish();
+
+ /*
+ * Create an expandable byte array to hold the compressed data.
+ * You cannot use an array that's the same size as the orginal because
+ * there is no guarantee that the compressed data will be smaller than
+ * the uncompressed data.
+ */
+ MemoryStream bos = new MemoryStream(input.Length);
+
+ // Compress the data
+ byte[] buf = new byte[1024];
+ while (!compressor.IsFinished)
+ {
+ int count = compressor.Deflate(buf);
+ bos.Write(buf, 0, count);
+ }
+
+ // Get the compressed data
+ return bos.ToArray();
+ }
+
+ public byte[] Uncompress(byte[] input)
+ {
+ Inflater decompressor = new Inflater();
+ decompressor.SetInput(input);
+
+ // Create an expandable byte array to hold the decompressed data
+ MemoryStream bos = new MemoryStream(input.Length);
+
+ // Decompress the data
+ byte[] buf = new byte[1024];
+ while (!decompressor.IsFinished)
+ {
+ int count = decompressor.Inflate(buf);
+ bos.Write(buf, 0, count);
+ }
+
+ // Get the decompressed data
+ return bos.ToArray();
+ }
+ }
+}
+
+#endif
\ No newline at end of file
Added: incubator/lucene.net/trunk/C#/src/Lucene.Net/Store/BufferedIndexInput.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Lucene.Net/Store/BufferedIndexInput.cs?rev=411501&view=auto
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Lucene.Net/Store/BufferedIndexInput.cs (added)
+++ incubator/lucene.net/trunk/C#/src/Lucene.Net/Store/BufferedIndexInput.cs Sat Jun 3 19:41:13 2006
@@ -0,0 +1,135 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * 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.
+ */
+
+using System;
+
+namespace Lucene.Net.Store
+{
+
+ /// <summary>Base implementation class for buffered {@link IndexInput}. </summary>
+ public abstract class BufferedIndexInput : IndexInput, System.ICloneable
+ {
+ internal static readonly int BUFFER_SIZE;
+
+ private byte[] buffer;
+
+ private long bufferStart = 0; // position in file of buffer
+ private int bufferLength = 0; // end of valid bytes
+ private int bufferPosition = 0; // next byte to read
+
+ public override byte ReadByte()
+ {
+ if (bufferPosition >= bufferLength)
+ Refill();
+ return buffer[bufferPosition++];
+ }
+
+ public override void ReadBytes(byte[] b, int offset, int len)
+ {
+ if (len < BUFFER_SIZE)
+ {
+ for (int i = 0; i < len; i++)
+ // read byte-by-byte
+ b[i + offset] = (byte) ReadByte();
+ }
+ else
+ {
+ // read all-at-once
+ long start = GetFilePointer();
+ SeekInternal(start);
+ ReadInternal(b, offset, len);
+
+ bufferStart = start + len; // adjust stream variables
+ bufferPosition = 0;
+ bufferLength = 0; // trigger refill() on read
+ }
+ }
+
+ private void Refill()
+ {
+ long start = bufferStart + bufferPosition;
+ long end = start + BUFFER_SIZE;
+ if (end > Length())
+ // don't read past EOF
+ end = Length();
+ bufferLength = (int) (end - start);
+ if (bufferLength <= 0)
+ throw new System.IO.IOException("read past EOF");
+
+ if (buffer == null)
+ buffer = new byte[BUFFER_SIZE]; // allocate buffer lazily
+ ReadInternal(buffer, 0, bufferLength);
+
+ bufferStart = start;
+ bufferPosition = 0;
+ }
+
+ /// <summary>Expert: implements buffer refill. Reads bytes from the current position
+ /// in the input.
+ /// </summary>
+ /// <param name="b">the array to read bytes into
+ /// </param>
+ /// <param name="offset">the offset in the array to start storing bytes
+ /// </param>
+ /// <param name="length">the number of bytes to read
+ /// </param>
+ public abstract void ReadInternal(byte[] b, int offset, int length);
+
+ public override long GetFilePointer()
+ {
+ return bufferStart + bufferPosition;
+ }
+
+ public override void Seek(long pos)
+ {
+ if (pos >= bufferStart && pos < (bufferStart + bufferLength))
+ bufferPosition = (int) (pos - bufferStart);
+ // seek within buffer
+ else
+ {
+ bufferStart = pos;
+ bufferPosition = 0;
+ bufferLength = 0; // trigger refill() on read()
+ SeekInternal(pos);
+ }
+ }
+
+ /// <summary>Expert: implements seek. Sets current position in this file, where the
+ /// next {@link #ReadInternal(byte[],int,int)} will occur.
+ /// </summary>
+ /// <seealso cref="ReadInternal(byte[],int,int)">
+ /// </seealso>
+ public abstract void SeekInternal(long pos);
+
+ public override System.Object Clone()
+ {
+ BufferedIndexInput clone = (BufferedIndexInput) base.Clone();
+
+ if (buffer != null)
+ {
+ clone.buffer = new byte[BUFFER_SIZE];
+ Array.Copy(buffer, 0, clone.buffer, 0, bufferLength);
+ }
+
+ return clone;
+ }
+
+ static BufferedIndexInput()
+ {
+ BUFFER_SIZE = BufferedIndexOutput.BUFFER_SIZE;
+ }
+ }
+}
\ No newline at end of file
Added: incubator/lucene.net/trunk/C#/src/Lucene.Net/Store/BufferedIndexOutput.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Lucene.Net/Store/BufferedIndexOutput.cs?rev=411501&view=auto
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Lucene.Net/Store/BufferedIndexOutput.cs (added)
+++ incubator/lucene.net/trunk/C#/src/Lucene.Net/Store/BufferedIndexOutput.cs Sat Jun 3 19:41:13 2006
@@ -0,0 +1,99 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * 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.
+ */
+
+using System;
+
+namespace Lucene.Net.Store
+{
+
+ /// <summary>Base implementation class for buffered {@link IndexOutput}. </summary>
+ public abstract class BufferedIndexOutput:IndexOutput
+ {
+ internal const int BUFFER_SIZE = 1024;
+
+ private byte[] buffer = new byte[BUFFER_SIZE];
+ private long bufferStart = 0; // position in file of buffer
+ private int bufferPosition = 0; // position in buffer
+
+ /// <summary>Writes a single byte.</summary>
+ /// <seealso cref="IndexInput.ReadByte()">
+ /// </seealso>
+ public override void WriteByte(byte b)
+ {
+ if (bufferPosition >= BUFFER_SIZE)
+ Flush();
+ buffer[bufferPosition++] = b;
+ }
+
+ /// <summary>Writes an array of bytes.</summary>
+ /// <param name="b">the bytes to write
+ /// </param>
+ /// <param name="length">the number of bytes to write
+ /// </param>
+ /// <seealso cref="IndexInput.ReadBytes(byte[],int,int)">
+ /// </seealso>
+ public override void WriteBytes(byte[] b, int length)
+ {
+ for (int i = 0; i < length; i++)
+ WriteByte(b[i]);
+ }
+
+ /// <summary>Forces any buffered output to be written. </summary>
+ public override void Flush()
+ {
+ FlushBuffer(buffer, bufferPosition);
+ bufferStart += bufferPosition;
+ bufferPosition = 0;
+ }
+
+ /// <summary>Expert: implements buffer write. Writes bytes at the current position in
+ /// the output.
+ /// </summary>
+ /// <param name="b">the bytes to write
+ /// </param>
+ /// <param name="len">the number of bytes to write
+ /// </param>
+ public abstract void FlushBuffer(byte[] b, int len);
+
+ /// <summary>Closes this stream to further operations. </summary>
+ public override void Close()
+ {
+ Flush();
+ }
+
+ /// <summary>Returns the current position in this file, where the next write will
+ /// occur.
+ /// </summary>
+ /// <seealso cref="Seek(long)">
+ /// </seealso>
+ public override long GetFilePointer()
+ {
+ return bufferStart + bufferPosition;
+ }
+
+ /// <summary>Sets current position in this file, where the next write will occur.</summary>
+ /// <seealso cref="GetFilePointer()">
+ /// </seealso>
+ public override void Seek(long pos)
+ {
+ Flush();
+ bufferStart = pos;
+ }
+
+ /// <summary>The number of bytes in the file. </summary>
+ public abstract override long Length();
+ }
+}
\ No newline at end of file
Modified: incubator/lucene.net/trunk/C#/src/Lucene.Net/Store/Directory.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Lucene.Net/Store/Directory.cs?rev=411501&r1=411500&r2=411501&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Lucene.Net/Store/Directory.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Lucene.Net/Store/Directory.cs Sat Jun 3 19:41:13 2006
@@ -13,7 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
using System;
+
namespace Lucene.Net.Store
{
@@ -57,13 +59,37 @@
/// <summary>Returns the length of a file in the directory. </summary>
public abstract long FileLength(System.String name);
+ /// <deprecated> use {@link #CreateOutput(String)}
+ /// </deprecated>
+ public virtual OutputStream createFile(System.String name)
+ {
+ return (OutputStream) CreateOutput(name);
+ }
+
/// <summary>Creates a new, empty file in the directory with the given name.
/// Returns a stream writing this file.
/// </summary>
- public abstract OutputStream CreateFile(System.String name);
+ public virtual IndexOutput CreateOutput(System.String name)
+ {
+ // default implementation for back compatibility
+ // this method should be abstract
+ return (IndexOutput) createFile(name);
+ }
+
+ /// <deprecated> use {@link #OpenInput(String)}
+ /// </deprecated>
+ public virtual InputStream OpenFile(System.String name)
+ {
+ return (InputStream) OpenInput(name);
+ }
/// <summary>Returns a stream reading an existing file. </summary>
- public abstract InputStream OpenFile(System.String name);
+ public virtual IndexInput OpenInput(System.String name)
+ {
+ // default implementation for back compatibility
+ // this method should be abstract
+ return (IndexInput) OpenFile(name);
+ }
/// <summary>Construct a {@link Lock}.</summary>
/// <param name="name">the name of the lock file
Modified: incubator/lucene.net/trunk/C#/src/Lucene.Net/Store/FSDirectory.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Lucene.Net/Store/FSDirectory.cs?rev=411501&r1=411500&r2=411501&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Lucene.Net/Store/FSDirectory.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Lucene.Net/Store/FSDirectory.cs Sat Jun 3 19:41:13 2006
@@ -13,21 +13,21 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
using System;
-using Constants = Lucene.Net.Util.Constants;
+using IndexFileNameFilter = Lucene.Net.Index.IndexFileNameFilter;
+
namespace Lucene.Net.Store
{
/// <summary> Straightforward implementation of {@link Directory} as a directory of files.
- /// <p>If the system property 'disableLuceneLocks' has the String value of
- /// "true", lock creation will be disabled.
///
/// </summary>
/// <seealso cref="Directory">
/// </seealso>
/// <author> Doug Cutting
/// </author>
- public sealed class FSDirectory : Directory
+ public class FSDirectory : Directory
{
private class AnonymousClassLock : Lock
{
@@ -52,7 +52,7 @@
}
public override bool Obtain()
{
- if (Lucene.Net.Store.FSDirectory.DISABLE_LOCKS)
+ if (Lucene.Net.Store.FSDirectory.disableLocks)
return true;
bool tmpBool;
@@ -64,7 +64,7 @@
{
try
{
- System.IO.Directory.CreateDirectory(Enclosing_Instance.lockDir.FullName);
+ System.IO.Directory.CreateDirectory(Enclosing_Instance.lockDir.FullName);
}
catch (Exception)
{
@@ -82,10 +82,10 @@
{
return false;
}
- }
+ }
public override void Release()
{
- if (Lucene.Net.Store.FSDirectory.DISABLE_LOCKS)
+ if (Lucene.Net.Store.FSDirectory.disableLocks)
return ;
bool tmpBool;
if (System.IO.File.Exists(lockFile.FullName))
@@ -104,7 +104,7 @@
}
public override bool IsLocked()
{
- if (Lucene.Net.Store.FSDirectory.DISABLE_LOCKS)
+ if (Lucene.Net.Store.FSDirectory.disableLocks)
return false;
bool tmpBool;
if (System.IO.File.Exists(lockFile.FullName))
@@ -119,6 +119,7 @@
return "Lock@" + lockFile;
}
}
+
/// <summary>This cache of directories ensures that there is a unique Directory
/// instance per path, so that synchronization on the Directory can be used to
/// synchronize access between readers and writers.
@@ -128,14 +129,34 @@
/// </summary>
private static readonly System.Collections.Hashtable DIRECTORIES = System.Collections.Hashtable.Synchronized(new System.Collections.Hashtable());
- private static readonly bool DISABLE_LOCKS;
+ private static bool disableLocks = false;
- /// <summary> Directory specified by <code>Lucene.Net.lockdir</code>
+ /// <summary> Set whether Lucene's use of lock files is disabled. By default,
+ /// lock files are enabled. They should only be disabled if the index
+ /// is on a read-only medium like a CD-ROM.
+ /// </summary>
+ public static void SetDisableLocks(bool doDisableLocks)
+ {
+ FSDirectory.disableLocks = doDisableLocks;
+ }
+
+ /// <summary> Returns whether Lucene's use of lock files is disabled.</summary>
+ /// <returns> true if locks are disabled, false if locks are enabled.
+ /// </returns>
+ public static bool GetDisableLocks()
+ {
+ return FSDirectory.disableLocks;
+ }
+
+ /// <summary> Directory specified by <code>Lucene.Net.lockDir</code>
/// or <code>java.io.tmpdir</code> system property
/// </summary>
- public static readonly System.String LOCK_DIR = SupportClass.AppSettings.Get("Lucene.Net.lockdir", System.IO.Path.GetTempPath());
+ public static readonly System.String LOCK_DIR = SupportClass.AppSettings.Get("Lucene.Net.lockDir", System.IO.Path.GetTempPath());
+
+ /// <summary>The default class which implements filesystem-based directories. </summary>
+ private static System.Type IMPL;
- private static System.Security.Cryptography.MD5 DIGESTER;
+ private static System.Security.Cryptography.MD5 DIGESTER;
/// <summary>A buffer optionally used in renameTo method </summary>
private byte[] buffer = null;
@@ -180,7 +201,15 @@
dir = (FSDirectory) DIRECTORIES[file];
if (dir == null)
{
- dir = new FSDirectory(file, create);
+ try
+ {
+ dir = (FSDirectory) System.Activator.CreateInstance(IMPL);
+ }
+ catch (System.Exception e)
+ {
+ throw new System.SystemException("cannot load FSDirectory class: " + e.ToString());
+ }
+ dir.Init(file, create);
DIRECTORIES[file] = dir;
}
else if (create)
@@ -199,7 +228,13 @@
private int refCount;
private System.IO.FileInfo lockDir;
- private FSDirectory(System.IO.FileInfo path, bool create)
+ public FSDirectory() // protected internal FSDirectory() // {{Aroush}} this shouldn't be 'public' but if it's not the line 'System.Activator.CreateInstance(IMPL);' in function GetDirectory() will fail.
+ {
+ }
+
+ // permit subclassing
+
+ private void Init(System.IO.FileInfo path, bool create)
{
directory = path;
@@ -211,6 +246,27 @@
{
lockDir = new System.IO.FileInfo(LOCK_DIR);
}
+ // Ensure that lockDir exists and is a directory.
+ bool tmpBool;
+ if (System.IO.File.Exists(lockDir.FullName))
+ tmpBool = true;
+ else
+ tmpBool = System.IO.Directory.Exists(lockDir.FullName);
+ if (!tmpBool)
+ {
+ try
+ {
+ System.IO.Directory.CreateDirectory(lockDir.FullName);
+ }
+ catch (Exception)
+ {
+ throw new System.IO.IOException("Cannot create directory: " + lockDir);
+ }
+ }
+ else if (!System.IO.Directory.Exists(lockDir.FullName))
+ {
+ throw new System.IO.IOException("Found regular file where directory expected: " + lockDir);
+ }
if (create)
{
Create();
@@ -241,10 +297,20 @@
}
}
- System.String[] files = System.IO.Directory.GetFileSystemEntries(directory.FullName); // clear old files
+ try
+ {
+ System.IO.Directory.Exists(directory.FullName);
+ }
+ catch (Exception)
+ {
+ throw new System.IO.IOException(directory + " not a directory");
+ }
+
+ System.String[] files = System.IO.Directory.GetFileSystemEntries(directory.FullName); // clear old files // {{Aroush-1.9}} we want the line below, not this one; how do we make the line below work in C#?!
+ //// System.String[] files = System.IO.Directory.GetFileSystemEntries(new IndexFileNameFilter()); // clear old files
for (int i = 0; i < files.Length; i++)
{
- System.IO.FileInfo file = new System.IO.FileInfo(files[i]);
+ System.IO.FileInfo file = new System.IO.FileInfo(System.IO.Path.Combine(directory.FullName, files[i]));
bool tmpBool2;
if (System.IO.File.Exists(file.FullName))
{
@@ -264,6 +330,8 @@
System.String lockPrefix = GetLockPrefix().ToString(); // clear old locks
files = System.IO.Directory.GetFileSystemEntries(lockDir.FullName);
+ if (files == null)
+ throw new System.IO.IOException("Cannot read lock directory " + lockDir.FullName);
for (int i = 0; i < files.Length; i++)
{
if (!files[i].StartsWith(lockPrefix))
@@ -310,14 +378,14 @@
public override long FileModified(System.String name)
{
System.IO.FileInfo file = new System.IO.FileInfo(System.IO.Path.Combine(directory.FullName, name));
- return ((file.LastWriteTime.Ticks - 621355968000000000) / 10000);
+ return (file.LastWriteTime.Ticks);
}
/// <summary>Returns the time the named file was last modified. </summary>
public static long FileModified(System.IO.FileInfo directory, System.String name)
{
System.IO.FileInfo file = new System.IO.FileInfo(System.IO.Path.Combine(directory.FullName, name));
- return ((file.LastWriteTime.Ticks - 621355968000000000) / 10000);
+ return (file.LastWriteTime.Ticks);
}
/// <summary>Set the modified time of an existing file to now. </summary>
@@ -331,7 +399,7 @@
public override long FileLength(System.String name)
{
System.IO.FileInfo file = new System.IO.FileInfo(System.IO.Path.Combine(directory.FullName, name));
- return file.Exists ? file.Length : 0;
+ return file.Exists ? file.Length : 0;
}
/// <summary>Removes an existing file in the directory. </summary>
@@ -352,7 +420,7 @@
else
tmpBool = false;
if (!tmpBool)
- throw new System.IO.IOException("Cannot delete " + name);
+ throw new System.IO.IOException("Cannot delete " + file);
}
/// <summary>Renames an existing file in the directory. </summary>
@@ -388,7 +456,7 @@
else
tmpBool2 = false;
if (!tmpBool2)
- throw new System.IO.IOException("Cannot delete " + to);
+ throw new System.IO.IOException("Cannot delete " + nu);
}
// Rename the old file to the new one. Unfortunately, the renameTo()
@@ -398,98 +466,116 @@
{
old.MoveTo(nu.FullName);
}
- catch (System.Exception ex)
- {
- System.IO.BinaryReader in_Renamed = null;
- System.IO.Stream out_Renamed = null;
- try
- {
- in_Renamed = new System.IO.BinaryReader(System.IO.File.Open(old.FullName, System.IO.FileMode.Open, System.IO.FileAccess.Read));
- out_Renamed = new System.IO.FileStream(nu.FullName, System.IO.FileMode.Create);
- // see if the buffer needs to be initialized. Initialization is
- // only done on-demand since many VM's will never run into the renameTo
- // bug and hence shouldn't waste 1K of mem for no reason.
- if (buffer == null)
- {
- buffer = new byte[1024];
- }
- int len;
+ catch (System.Exception)
+ {
+ System.IO.Stream in_Renamed = null;
+ System.IO.Stream out_Renamed = null;
+ try
+ {
+ in_Renamed = new System.IO.FileStream(old.FullName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
+ out_Renamed = new System.IO.FileStream(nu.FullName, System.IO.FileMode.Create);
+ // see if the buffer needs to be initialized. Initialization is
+ // only done on-demand since many VM's will never run into the renameTo
+ // bug and hence shouldn't waste 1K of mem for no reason.
+ if (buffer == null)
+ {
+ buffer = new byte[1024];
+ }
+ int len;
len = in_Renamed.Read(buffer, 0, buffer.Length);
out_Renamed.Write(buffer, 0, len);
- // delete the old file.
- bool tmpBool3;
- if (System.IO.File.Exists(old.FullName))
- {
- System.IO.File.Delete(old.FullName);
- tmpBool3 = true;
- }
- else if (System.IO.Directory.Exists(old.FullName))
- {
- System.IO.Directory.Delete(old.FullName);
- tmpBool3 = true;
- }
- else
- tmpBool3 = false;
- bool generatedAux = tmpBool3;
- }
- catch (System.IO.IOException e)
- {
- throw new System.IO.IOException("Cannot rename " + from + " to " + to);
- }
- finally
- {
- if (in_Renamed != null)
- {
- try
- {
- in_Renamed.Close();
- }
- catch (System.IO.IOException e)
- {
- throw new System.SystemException("Cannot close input stream: " + e.Message);
- }
- }
- if (out_Renamed != null)
- {
- try
- {
- out_Renamed.Close();
- }
- catch (System.IO.IOException e)
- {
- throw new System.SystemException("Cannot close output stream: " + e.Message);
- }
- }
- }
- }
+ // delete the old file.
+ bool tmpBool3;
+ if (System.IO.File.Exists(old.FullName))
+ {
+ System.IO.File.Delete(old.FullName);
+ tmpBool3 = true;
+ }
+ else if (System.IO.Directory.Exists(old.FullName))
+ {
+ System.IO.Directory.Delete(old.FullName);
+ tmpBool3 = true;
+ }
+ else
+ tmpBool3 = false;
+ bool generatedAux = tmpBool3;
+ }
+ catch (System.IO.IOException ioe)
+ {
+ throw new System.IO.IOException("Cannot rename " + old + " to " + nu);
+ }
+ finally
+ {
+ if (in_Renamed != null)
+ {
+ try
+ {
+ in_Renamed.Close();
+ }
+ catch (System.IO.IOException e)
+ {
+ throw new System.SystemException("Cannot close input stream: " + e.ToString());
+ }
+ }
+ if (out_Renamed != null)
+ {
+ try
+ {
+ out_Renamed.Close();
+ }
+ catch (System.IO.IOException e)
+ {
+ throw new System.SystemException("Cannot close output stream: " + e.ToString());
+ }
+ }
+ }
+ }
}
}
/// <summary>Creates a new, empty file in the directory with the given name.
/// Returns a stream writing this file.
/// </summary>
- public override OutputStream CreateFile(System.String name)
+ public override IndexOutput CreateOutput(System.String name)
{
- return new FSOutputStream(new System.IO.FileInfo(System.IO.Path.Combine(directory.FullName, name)));
+ System.IO.FileInfo file = new System.IO.FileInfo(System.IO.Path.Combine(directory.FullName, name));
+ bool tmpBool;
+ if (System.IO.File.Exists(file.FullName))
+ tmpBool = true;
+ else
+ tmpBool = System.IO.Directory.Exists(file.FullName);
+ bool tmpBool2;
+ if (System.IO.File.Exists(file.FullName))
+ {
+ System.IO.File.Delete(file.FullName);
+ tmpBool2 = true;
+ }
+ else if (System.IO.Directory.Exists(file.FullName))
+ {
+ System.IO.Directory.Delete(file.FullName);
+ tmpBool2 = true;
+ }
+ else
+ tmpBool2 = false;
+ if (tmpBool && !tmpBool2)
+ // delete existing, if any
+ throw new System.IO.IOException("Cannot overwrite: " + file);
+
+ return new FSIndexOutput(file);
}
/// <summary>Returns a stream reading an existing file. </summary>
- public override InputStream OpenFile(System.String name)
+ public override IndexInput OpenInput(System.String name)
{
- return new FSInputStream(new System.IO.FileInfo(System.IO.Path.Combine(directory.FullName, name)));
+ return new FSIndexInput(new System.IO.FileInfo(System.IO.Path.Combine(directory.FullName, name)));
}
/// <summary> So we can do some byte-to-hexchar conversion below</summary>
private static readonly char[] HEX_DIGITS = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
/// <summary>Constructs a {@link Lock} with the specified name. Locks are implemented
- /// with {@link File#createNewFile() }.
- ///
- /// <p>In JDK 1.1 or if system property <I>disableLuceneLocks</I> is the
- /// string "true", locks are disabled. Assigning this property any other
- /// string will <B>not</B> prevent creation of lock files. This is useful for
- /// using Lucene on read-only medium, such as CD-ROM.
+ /// with {@link File#createNewFile()}.
///
/// </summary>
/// <param name="name">the name of the lock file
@@ -552,7 +638,7 @@
}
}
- public System.IO.FileInfo GetFile()
+ public virtual System.IO.FileInfo GetFile()
{
return directory;
}
@@ -560,11 +646,33 @@
/// <summary>For debug output. </summary>
public override System.String ToString()
{
- return "FSDirectory@" + directory;
+ return this.GetType().FullName + "@" + directory;
}
+
static FSDirectory()
{
- DISABLE_LOCKS = System.Configuration.ConfigurationSettings.AppSettings.Get("disableLuceneLocks") != null;
+ {
+ try
+ {
+ System.String name = SupportClass.AppSettings.Get("Lucene.Net.FSDirectory.class", typeof(FSDirectory).FullName);
+ IMPL = System.Type.GetType(name);
+ }
+ catch (System.Security.SecurityException)
+ {
+ try
+ {
+ IMPL = System.Type.GetType(typeof(FSDirectory).FullName);
+ }
+ catch (System.Exception e)
+ {
+ throw new System.SystemException("cannot load default FSDirectory class: " + e.ToString());
+ }
+ }
+ catch (System.Exception e)
+ {
+ throw new System.SystemException("cannot load FSDirectory class: " + e.ToString());
+ }
+ }
{
try
{
@@ -579,16 +687,16 @@
}
- sealed public class FSInputStream : InputStream, System.ICloneable
+ public class FSIndexInput : BufferedIndexInput, System.ICloneable
{
- internal class Descriptor : System.IO.BinaryReader
+ private class Descriptor : System.IO.BinaryReader
{
- private void InitBlock(FSInputStream enclosingInstance)
+ private void InitBlock(FSIndexInput enclosingInstance)
{
this.enclosingInstance = enclosingInstance;
}
- private FSInputStream enclosingInstance;
- public FSInputStream Enclosing_Instance
+ private FSIndexInput enclosingInstance;
+ public FSIndexInput Enclosing_Instance
{
get
{
@@ -596,54 +704,30 @@
}
}
- /* DEBUG */
- //private String name;
- /* DEBUG */
- // {{Aroush
public long position;
- public Descriptor(FSInputStream enclosingInstance, System.IO.FileInfo file, System.IO.FileAccess fileAccess)
- : base(new System.IO.FileStream(file.FullName, System.IO.FileMode.Open, fileAccess, System.IO.FileShare.ReadWrite))
- {
- }
-
- //{{}}// public Descriptor(FSInputStream enclosingInstance, System.IO.FileInfo file, System.String mode) : base(file, mode)
- //{{}}// {
- //{{}}// InitBlock(enclosingInstance);
- //{{}}// /* DEBUG */
- //{{}}// //name = file.ToString();
- //{{}}// //debug_printInfo("OPEN");
- //{{}}// /* DEBUG */
- //{{}}// }
- // Aroush}}
-
- /* DEBUG */
- //public void close() throws IOException {
- // debug_printInfo("CLOSE");
- // super.close();
- //}
- //
- //private void debug_printInfo(String op) {
- // try { throw new Exception(op + " <" + name + ">");
- // } catch (Exception e) {
- // java.io.StringWriter sw = new java.io.StringWriter();
- // java.io.PrintWriter pw = new java.io.PrintWriter(sw);
- // e.printStackTrace(pw);
- // System.out.println(sw.getBuffer().ToString());
- // }
- //}
- /* DEBUG */
+ public Descriptor(FSIndexInput enclosingInstance, System.IO.FileInfo file, System.IO.FileAccess mode)
+ : base(new System.IO.FileStream(file.FullName, System.IO.FileMode.Open, mode, System.IO.FileShare.ReadWrite))
+ {
+ InitBlock(enclosingInstance);
+ }
}
- internal Descriptor file = null;
- public /*internal*/ bool isClone;
+ private Descriptor file = null;
+ internal bool isClone;
+ private long length;
- public FSInputStream(System.IO.FileInfo path)
+ public bool IsClone
+ {
+ get { return (isClone); }
+ }
+
+ public FSIndexInput(System.IO.FileInfo path)
{
file = new Descriptor(this, path, System.IO.FileAccess.Read);
length = file.BaseStream.Length;
}
- /// <summary>InputStream methods </summary>
+ /// <summary>IndexInput methods </summary>
public override void ReadInternal(byte[] b, int offset, int len)
{
lock (file)
@@ -657,7 +741,7 @@
int total = 0;
do
{
- int i = file.Read(b, offset + total, len - total);
+ int i = file.Read(b, offset + total, len - total);
if (i <= 0)
throw new System.IO.IOException("read past EOF");
file.position += i;
@@ -671,22 +755,26 @@
{
if (!isClone && file != null)
file.Close();
- System.GC.SuppressFinalize(this);
+ System.GC.SuppressFinalize(this);
}
- /// <summary>Random-access methods </summary>
public override void SeekInternal(long position)
{
}
- ~FSInputStream()
+ public override long Length()
+ {
+ return length;
+ }
+
+ ~FSIndexInput()
{
Close(); // close the file
}
public override System.Object Clone()
{
- FSInputStream clone = (FSInputStream) base.Clone();
+ FSIndexInput clone = (FSIndexInput) base.Clone();
clone.isClone = true;
return clone;
}
@@ -694,26 +782,28 @@
/// <summary>Method used for testing. Returns true if the underlying
/// file descriptor is valid.
/// </summary>
- public /*internal*/ bool IsFDValid()
+ public /*internal*/ virtual bool IsFDValid()
{
+ if (file.BaseStream == null)
+ return false;
return file.BaseStream.CanRead;
}
}
- sealed class FSOutputStream : OutputStream
+ class FSIndexOutput : BufferedIndexOutput
{
internal System.IO.BinaryWriter file = null;
- public FSOutputStream(System.IO.FileInfo path)
+ public FSIndexOutput(System.IO.FileInfo path)
{
- file = new System.IO.BinaryWriter(new System.IO.FileStream(path.FullName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite));
+ file = new System.IO.BinaryWriter(new System.IO.FileStream(path.FullName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite));
}
/// <summary>output methods: </summary>
public override void FlushBuffer(byte[] b, int size)
{
- file.Write(b, 0, size);
+ file.Write(b, 0, size);
}
public override void Close()
{
@@ -733,7 +823,7 @@
return file.BaseStream.Length;
}
- ~FSOutputStream()
+ ~FSIndexOutput()
{
file.Close(); // close the file
}
Added: incubator/lucene.net/trunk/C#/src/Lucene.Net/Store/IndexInput.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Lucene.Net/Store/IndexInput.cs?rev=411501&view=auto
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Lucene.Net/Store/IndexInput.cs (added)
+++ incubator/lucene.net/trunk/C#/src/Lucene.Net/Store/IndexInput.cs Sat Jun 3 19:41:13 2006
@@ -0,0 +1,178 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * 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.
+ */
+
+using System;
+
+namespace Lucene.Net.Store
+{
+
+ /// <summary>Abstract base class for input from a file in a {@link Directory}. A
+ /// random-access input stream. Used for all Lucene index input operations.
+ /// </summary>
+ /// <seealso cref="Directory">
+ /// </seealso>
+ public abstract class IndexInput : System.ICloneable
+ {
+ private char[] chars; // used by readString()
+
+ /// <summary>Reads and returns a single byte.</summary>
+ /// <seealso cref="IndexOutput.WriteByte(byte)">
+ /// </seealso>
+ public abstract byte ReadByte();
+
+ /// <summary>Reads a specified number of bytes into an array at the specified offset.</summary>
+ /// <param name="b">the array to read bytes into
+ /// </param>
+ /// <param name="offset">the offset in the array to start storing bytes
+ /// </param>
+ /// <param name="len">the number of bytes to read
+ /// </param>
+ /// <seealso cref="IndexOutput.WriteBytes(byte[],int)">
+ /// </seealso>
+ public abstract void ReadBytes(byte[] b, int offset, int len);
+
+ /// <summary>Reads four bytes and returns an int.</summary>
+ /// <seealso cref="IndexOutput.WriteInt(int)">
+ /// </seealso>
+ public virtual int ReadInt()
+ {
+ return ((ReadByte() & 0xFF) << 24) | ((ReadByte() & 0xFF) << 16) | ((ReadByte() & 0xFF) << 8) | (ReadByte() & 0xFF);
+ }
+
+ /// <summary>Reads an int stored in variable-length format. Reads between one and
+ /// five bytes. Smaller values take fewer bytes. Negative numbers are not
+ /// supported.
+ /// </summary>
+ /// <seealso cref="IndexOutput.WriteVInt(int)">
+ /// </seealso>
+ public virtual int ReadVInt()
+ {
+ byte b = ReadByte();
+ int i = b & 0x7F;
+ for (int shift = 7; (b & 0x80) != 0; shift += 7)
+ {
+ b = ReadByte();
+ i |= (b & 0x7F) << shift;
+ }
+ return i;
+ }
+
+ /// <summary>Reads eight bytes and returns a long.</summary>
+ /// <seealso cref="IndexOutput.WriteLong(long)">
+ /// </seealso>
+ public virtual long ReadLong()
+ {
+ return (((long) ReadInt()) << 32) | (ReadInt() & 0xFFFFFFFFL);
+ }
+
+ /// <summary>Reads a long stored in variable-length format. Reads between one and
+ /// nine bytes. Smaller values take fewer bytes. Negative numbers are not
+ /// supported.
+ /// </summary>
+ public virtual long ReadVLong()
+ {
+ byte b = ReadByte();
+ long i = b & 0x7F;
+ for (int shift = 7; (b & 0x80) != 0; shift += 7)
+ {
+ b = ReadByte();
+ i |= (b & 0x7FL) << shift;
+ }
+ return i;
+ }
+
+ /// <summary>Reads a string.</summary>
+ /// <seealso cref="IndexOutput.WriteString(String)">
+ /// </seealso>
+ public virtual System.String ReadString()
+ {
+ int length = ReadVInt();
+ if (chars == null || length > chars.Length)
+ chars = new char[length];
+ ReadChars(chars, 0, length);
+ return new System.String(chars, 0, length);
+ }
+
+ /// <summary>Reads UTF-8 encoded characters into an array.</summary>
+ /// <param name="buffer">the array to read characters into
+ /// </param>
+ /// <param name="start">the offset in the array to start storing characters
+ /// </param>
+ /// <param name="length">the number of characters to read
+ /// </param>
+ /// <seealso cref="IndexOutput.WriteChars(String,int,int)">
+ /// </seealso>
+ public virtual void ReadChars(char[] buffer, int start, int length)
+ {
+ int end = start + length;
+ for (int i = start; i < end; i++)
+ {
+ byte b = ReadByte();
+ if ((b & 0x80) == 0)
+ buffer[i] = (char) (b & 0x7F);
+ else if ((b & 0xE0) != 0xE0)
+ {
+ buffer[i] = (char) (((b & 0x1F) << 6) | (ReadByte() & 0x3F));
+ }
+ else
+ buffer[i] = (char) (((b & 0x0F) << 12) | ((ReadByte() & 0x3F) << 6) | (ReadByte() & 0x3F));
+ }
+ }
+
+ /// <summary>Closes the stream to futher operations. </summary>
+ public abstract void Close();
+
+ /// <summary>Returns the current position in this file, where the next read will
+ /// occur.
+ /// </summary>
+ /// <seealso cref="Seek(long)">
+ /// </seealso>
+ public abstract long GetFilePointer();
+
+ /// <summary>Sets current position in this file, where the next read will occur.</summary>
+ /// <seealso cref="GetFilePointer()">
+ /// </seealso>
+ public abstract void Seek(long pos);
+
+ /// <summary>The number of bytes in the file. </summary>
+ public abstract long Length();
+
+ /// <summary>Returns a clone of this stream.
+ ///
+ /// <p>Clones of a stream access the same data, and are positioned at the same
+ /// point as the stream they were cloned from.
+ ///
+ /// <p>Expert: Subclasses must ensure that clones may be positioned at
+ /// different points in the input from each other and from the stream they
+ /// were cloned from.
+ /// </summary>
+ public virtual System.Object Clone()
+ {
+ IndexInput clone = null;
+ try
+ {
+ clone = (IndexInput) base.MemberwiseClone();
+ }
+ catch (System.Exception)
+ {
+ }
+
+ clone.chars = null;
+
+ return clone;
+ }
+ }
+}
\ No newline at end of file
Added: incubator/lucene.net/trunk/C#/src/Lucene.Net/Store/IndexOutput.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Lucene.Net/Store/IndexOutput.cs?rev=411501&view=auto
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Lucene.Net/Store/IndexOutput.cs (added)
+++ incubator/lucene.net/trunk/C#/src/Lucene.Net/Store/IndexOutput.cs Sat Jun 3 19:41:13 2006
@@ -0,0 +1,160 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * 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.
+ */
+
+using System;
+
+namespace Lucene.Net.Store
+{
+
+ /// <summary>Abstract base class for output to a file in a Directory. A random-access
+ /// output stream. Used for all Lucene index output operations.
+ /// </summary>
+ /// <seealso cref="Directory">
+ /// </seealso>
+ /// <seealso cref="IndexInput">
+ /// </seealso>
+ public abstract class IndexOutput
+ {
+
+ /// <summary>Writes a single byte.</summary>
+ /// <seealso cref="IndexInput.ReadByte()">
+ /// </seealso>
+ public abstract void WriteByte(byte b);
+
+ /// <summary>Writes an array of bytes.</summary>
+ /// <param name="b">the bytes to write
+ /// </param>
+ /// <param name="length">the number of bytes to write
+ /// </param>
+ /// <seealso cref="IndexInput.ReadBytes(byte[],int,int)">
+ /// </seealso>
+ public abstract void WriteBytes(byte[] b, int length);
+
+ /// <summary>Writes an int as four bytes.</summary>
+ /// <seealso cref="IndexInput.ReadInt()">
+ /// </seealso>
+ public virtual void WriteInt(int i)
+ {
+ WriteByte((byte) (i >> 24));
+ WriteByte((byte) (i >> 16));
+ WriteByte((byte) (i >> 8));
+ WriteByte((byte) i);
+ }
+
+ /// <summary>Writes an int in a variable-length format. Writes between one and
+ /// five bytes. Smaller values take fewer bytes. Negative numbers are not
+ /// supported.
+ /// </summary>
+ /// <seealso cref="IndexInput.ReadVInt()">
+ /// </seealso>
+ public virtual void WriteVInt(int i)
+ {
+ while ((i & ~ 0x7F) != 0)
+ {
+ WriteByte((byte) ((i & 0x7f) | 0x80));
+ i = SupportClass.Number.URShift(i, 7);
+ }
+ WriteByte((byte) i);
+ }
+
+ /// <summary>Writes a long as eight bytes.</summary>
+ /// <seealso cref="IndexInput.ReadLong()">
+ /// </seealso>
+ public virtual void WriteLong(long i)
+ {
+ WriteInt((int) (i >> 32));
+ WriteInt((int) i);
+ }
+
+ /// <summary>Writes an long in a variable-length format. Writes between one and five
+ /// bytes. Smaller values take fewer bytes. Negative numbers are not
+ /// supported.
+ /// </summary>
+ /// <seealso cref="IndexInput.ReadVLong()">
+ /// </seealso>
+ public virtual void WriteVLong(long i)
+ {
+ while ((i & ~ 0x7F) != 0)
+ {
+ WriteByte((byte) ((i & 0x7f) | 0x80));
+ i = (int) (((uint) i) >> 7); // {{Aroush-1.9}} Is this OK?! long to uint, to int conversion.
+ }
+ WriteByte((byte) i);
+ }
+
+ /// <summary>Writes a string.</summary>
+ /// <seealso cref="IndexInput.ReadString()">
+ /// </seealso>
+ public virtual void WriteString(System.String s)
+ {
+ int length = s.Length;
+ WriteVInt(length);
+ WriteChars(s, 0, length);
+ }
+
+ /// <summary>Writes a sequence of UTF-8 encoded characters from a string.</summary>
+ /// <param name="s">the source of the characters
+ /// </param>
+ /// <param name="start">the first character in the sequence
+ /// </param>
+ /// <param name="length">the number of characters in the sequence
+ /// </param>
+ /// <seealso cref="IndexInput.ReadChars(char[],int,int)">
+ /// </seealso>
+ public virtual void WriteChars(System.String s, int start, int length)
+ {
+ int end = start + length;
+ for (int i = start; i < end; i++)
+ {
+ int code = (int) s[i];
+ if (code >= 0x01 && code <= 0x7F)
+ WriteByte((byte) code);
+ else if (((code >= 0x80) && (code <= 0x7FF)) || code == 0)
+ {
+ WriteByte((byte) (0xC0 | (code >> 6)));
+ WriteByte((byte) (0x80 | (code & 0x3F)));
+ }
+ else
+ {
+ WriteByte((byte) (0xE0 | (SupportClass.Number.URShift(code, 12))));
+ WriteByte((byte) (0x80 | ((code >> 6) & 0x3F)));
+ WriteByte((byte) (0x80 | (code & 0x3F)));
+ }
+ }
+ }
+
+ /// <summary>Forces any buffered output to be written. </summary>
+ public abstract void Flush();
+
+ /// <summary>Closes this stream to further operations. </summary>
+ public abstract void Close();
+
+ /// <summary>Returns the current position in this file, where the next write will
+ /// occur.
+ /// </summary>
+ /// <seealso cref="Seek(long)">
+ /// </seealso>
+ public abstract long GetFilePointer();
+
+ /// <summary>Sets current position in this file, where the next write will occur.</summary>
+ /// <seealso cref="GetFilePointer()">
+ /// </seealso>
+ public abstract void Seek(long pos);
+
+ /// <summary>The number of bytes in the file. </summary>
+ public abstract long Length();
+ }
+}
\ No newline at end of file
Modified: incubator/lucene.net/trunk/C#/src/Lucene.Net/Store/InputStream.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Lucene.Net/Store/InputStream.cs?rev=411501&r1=411500&r2=411501&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Lucene.Net/Store/InputStream.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Lucene.Net/Store/InputStream.cs Sat Jun 3 19:41:13 2006
@@ -13,267 +13,22 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
using System;
+
namespace Lucene.Net.Store
{
- /// <summary>Abstract base class for input from a file in a {@link Directory}. A
- /// random-access input stream. Used for all Lucene index input operations.
- /// </summary>
- /// <seealso cref="Directory">
- /// </seealso>
- /// <seealso cref="OutputStream">
- /// </seealso>
- public abstract class InputStream : System.ICloneable
+ /// <deprecated> Use {@link IndexInput} or {@link BufferedIndexInput} instead.
+ /// </deprecated>
+ public abstract class InputStream : BufferedIndexInput
{
- internal static readonly int BUFFER_SIZE;
-
- private byte[] buffer;
- private char[] chars;
-
- private long bufferStart = 0; // position in file of buffer
- private int bufferLength = 0; // end of valid bytes
- private int bufferPosition = 0; // next byte to read
protected internal long length; // set by subclasses
- /// <summary>Reads and returns a single byte.</summary>
- /// <seealso cref="OutputStream#WriteByte(byte)">
- /// </seealso>
- public byte ReadByte()
- {
- if (bufferPosition >= bufferLength)
- Refill();
- return buffer[bufferPosition++];
- }
-
- /// <summary>Reads a specified number of bytes into an array at the specified offset.</summary>
- /// <param name="b">the array to read bytes into
- /// </param>
- /// <param name="offset">the offset in the array to start storing bytes
- /// </param>
- /// <param name="len">the number of bytes to read
- /// </param>
- /// <seealso cref="OutputStream#WriteBytes(byte[],int)">
- /// </seealso>
- public void ReadBytes(byte[] b, int offset, int len)
- {
- if (len < BUFFER_SIZE)
- {
- for (int i = 0; i < len; i++)
- // read byte-by-byte
- b[i + offset] = (byte) ReadByte();
- }
- else
- {
- // read all-at-once
- long start = GetFilePointer();
- SeekInternal(start);
- ReadInternal(b, offset, len);
-
- bufferStart = start + len; // adjust stream variables
- bufferPosition = 0;
- bufferLength = 0; // trigger refill() on read
- }
- }
-
- /// <summary>Reads four bytes and returns an int.</summary>
- /// <seealso cref="OutputStream#WriteInt(int)">
- /// </seealso>
- public int ReadInt()
- {
- return ((ReadByte() & 0xFF) << 24) | ((ReadByte() & 0xFF) << 16) | ((ReadByte() & 0xFF) << 8) | (ReadByte() & 0xFF);
- }
-
- /// <summary>Reads an int stored in variable-length format. Reads between one and
- /// five bytes. Smaller values take fewer bytes. Negative numbers are not
- /// supported.
- /// </summary>
- /// <seealso cref="OutputStream#WriteVInt(int)">
- /// </seealso>
- public int ReadVInt()
- {
- byte b = ReadByte();
- int i = b & 0x7F;
- for (int shift = 7; (b & 0x80) != 0; shift += 7)
- {
- b = ReadByte();
- i |= (b & 0x7F) << shift;
- }
- return i;
- }
-
- /// <summary>Reads eight bytes and returns a long.</summary>
- /// <seealso cref="OutputStream#WriteLong(long)">
- /// </seealso>
- public long ReadLong()
- {
- return (((long) ReadInt()) << 32) | (ReadInt() & 0xFFFFFFFFL);
- }
-
- /// <summary>Reads a long stored in variable-length format. Reads between one and
- /// nine bytes. Smaller values take fewer bytes. Negative numbers are not
- /// supported.
- /// </summary>
- public long ReadVLong()
- {
- byte b = ReadByte();
- long i = b & 0x7F;
- for (int shift = 7; (b & 0x80) != 0; shift += 7)
- {
- b = ReadByte();
- i |= (b & 0x7FL) << shift;
- }
- return i;
- }
-
- /// <summary>Reads a string.</summary>
- /// <seealso cref="OutputStream#WriteString(String)">
- /// </seealso>
- public System.String ReadString()
- {
- int length = ReadVInt();
- if (chars == null || length > chars.Length)
- chars = new char[length];
- ReadChars(chars, 0, length);
- return new System.String(chars, 0, length);
- }
-
- /// <summary>Reads UTF-8 encoded characters into an array.</summary>
- /// <param name="buffer">the array to read characters into
- /// </param>
- /// <param name="start">the offset in the array to start storing characters
- /// </param>
- /// <param name="length">the number of characters to read
- /// </param>
- /// <seealso cref="OutputStream#WriteChars(String,int,int)">
- /// </seealso>
- public void ReadChars(char[] buffer, int start, int length)
- {
- int end = start + length;
- for (int i = start; i < end; i++)
- {
- byte b = ReadByte();
- if ((b & 0x80) == 0)
- buffer[i] = (char) (b & 0x7F);
- else if ((b & 0xE0) != 0xE0)
- {
- buffer[i] = (char) (((b & 0x1F) << 6) | (ReadByte() & 0x3F));
- }
- else
- buffer[i] = (char) (((b & 0x0F) << 12) | ((ReadByte() & 0x3F) << 6) | (ReadByte() & 0x3F));
- }
- }
-
-
- private void Refill()
- {
- long start = bufferStart + bufferPosition;
- long end = start + BUFFER_SIZE;
- if (end > length)
- // don't read past EOF
- end = length;
- bufferLength = (int) (end - start);
- if (bufferLength == 0)
- throw new System.IO.IOException("read past EOF");
-
- if (buffer == null)
- buffer = new byte[BUFFER_SIZE]; // allocate buffer lazily
- ReadInternal(buffer, 0, bufferLength);
-
- bufferStart = start;
- bufferPosition = 0;
- }
-
- /// <summary>Expert: implements buffer refill. Reads bytes from the current position
- /// in the input.
- /// </summary>
- /// <param name="b">the array to read bytes into
- /// </param>
- /// <param name="offset">the offset in the array to start storing bytes
- /// </param>
- /// <param name="length">the number of bytes to read
- /// </param>
- public abstract void ReadInternal(byte[] b, int offset, int length);
-
- /// <summary>Closes the stream to futher operations. </summary>
- public abstract void Close();
-
- /// <summary>Returns the current position in this file, where the next read will
- /// occur.
- /// </summary>
- /// <seealso cref="#Seek(long)">
- /// </seealso>
- public long GetFilePointer()
- {
- return bufferStart + bufferPosition;
- }
-
- /// <summary>Sets current position in this file, where the next read will occur.</summary>
- /// <seealso cref="#GetFilePointer()">
- /// </seealso>
- public void Seek(long pos)
- {
- if (pos >= bufferStart && pos < (bufferStart + bufferLength))
- bufferPosition = (int) (pos - bufferStart);
- // seek within buffer
- else
- {
- bufferStart = pos;
- bufferPosition = 0;
- bufferLength = 0; // trigger refill() on read()
- SeekInternal(pos);
- }
- }
-
- /// <summary>Expert: implements seek. Sets current position in this file, where the
- /// next {@link #ReadInternal(byte[],int,int)} will occur.
- /// </summary>
- /// <seealso cref="#ReadInternal(byte[],int,int)">
- /// </seealso>
- public abstract void SeekInternal(long pos);
-
- /// <summary>The number of bytes in the file. </summary>
- public long Length()
+ public override long Length()
{
return length;
- }
-
- /// <summary>Returns a clone of this stream.
- ///
- /// <p>Clones of a stream access the same data, and are positioned at the same
- /// point as the stream they were cloned from.
- ///
- /// <p>Expert: Subclasses must ensure that clones may be positioned at
- /// different points in the input from each other and from the stream they
- /// were cloned from.
- /// </summary>
- public virtual System.Object Clone()
- {
- InputStream clone = null;
- try
- {
- clone = (InputStream) this.MemberwiseClone();
- }
- catch (System.Exception e)
- {
- throw new Exception("Can't clone InputStream.", e);
- }
-
- if (buffer != null)
- {
- clone.buffer = new byte[BUFFER_SIZE];
- Array.Copy(buffer, 0, clone.buffer, 0, bufferLength);
- }
-
- clone.chars = null;
-
- return clone;
- }
-
- static InputStream()
- {
- BUFFER_SIZE = OutputStream.BUFFER_SIZE;
}
}
}
Modified: incubator/lucene.net/trunk/C#/src/Lucene.Net/Store/Lock.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Lucene.Net/Store/Lock.cs?rev=411501&r1=411500&r2=411501&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Lucene.Net/Store/Lock.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Lucene.Net/Store/Lock.cs Sat Jun 3 19:41:13 2006
@@ -13,8 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
using System;
using IndexWriter = Lucene.Net.Index.IndexWriter;
+
namespace Lucene.Net.Store
{
@@ -30,9 +32,9 @@
/// </summary>
/// <author> Doug Cutting
/// </author>
- /// <version> $Id: Lock.java,v 1.12 2004/05/11 17:43:28 cutting Exp $
+ /// <version> $Id: Lock.java 179414 2005-06-01 20:10:58Z dnaber $
/// </version>
- /// <seealso cref="Directory#MakeLock(String)">
+ /// <seealso cref="Directory.MakeLock(String)">
/// </seealso>
public abstract class Lock
{
@@ -54,14 +56,14 @@
/// <returns> true if lock was obtained
/// </returns>
/// <throws> IOException if lock wait times out or obtain() throws an IOException </throws>
- public virtual bool Obtain(long lockWaitTimeout)
+ public virtual bool obtain(long lockWaitTimeout)
{
bool locked = Obtain();
int maxSleepCount = (int) (lockWaitTimeout / LOCK_POLL_INTERVAL);
int sleepCount = 0;
while (!locked)
{
- if (++sleepCount == maxSleepCount)
+ if (sleepCount++ == maxSleepCount)
{
throw new System.IO.IOException("Lock obtain timed out: " + this.ToString());
}
@@ -98,7 +100,7 @@
/// </summary>
/// <deprecated> Kept only to avoid breaking existing code.
/// </deprecated>
- public With(Lock lock_Renamed):this(lock_Renamed, IndexWriter.COMMIT_LOCK_TIMEOUT)
+ public With(Lock lock_Renamed) : this(lock_Renamed, IndexWriter.COMMIT_LOCK_TIMEOUT)
{
}
@@ -122,7 +124,7 @@
bool locked = false;
try
{
- locked = lock_Renamed.Obtain(lockWaitTimeout);
+ locked = lock_Renamed.obtain(lockWaitTimeout);
return DoBody();
}
finally
Added: incubator/lucene.net/trunk/C#/src/Lucene.Net/Store/MMapDirectory.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Lucene.Net/Store/MMapDirectory.cs?rev=411501&view=auto
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Lucene.Net/Store/MMapDirectory.cs (added)
+++ incubator/lucene.net/trunk/C#/src/Lucene.Net/Store/MMapDirectory.cs Sat Jun 3 19:41:13 2006
@@ -0,0 +1,228 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * 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.
+ */
+
+using System;
+// using ByteBuffer = java.nio.ByteBuffer; // {{Aroush-1.9}}
+// using FileChannel = java.nio.channels.FileChannel; // {{Aroush-1.9}}
+// using MapMode = java.nio.channels.FileChannel.MapMode; // {{Aroush-1.9}}
+
+namespace Lucene.Net.Store
+{
+
+ /// <summary>File-based {@link Directory} implementation that uses mmap for input.
+ ///
+ /// <p>To use this, invoke Java with the System property
+ /// Lucene.Net.FSDirectory.class set to
+ /// Lucene.Net.store.MMapDirectory. This will cause {@link
+ /// FSDirectory#GetDirectory(File,boolean)} to return instances of this class.
+ /// </summary>
+ public class MMapDirectory : FSDirectory
+ {
+
+ private class MMapIndexInput : IndexInput, System.ICloneable
+ {
+
+ private System.IO.FileStream buffer; // private ByteBuffer buffer; // {{Aroush-1.9}}
+ private long length;
+
+ internal MMapIndexInput(System.IO.FileStream raf)
+ {
+ this.length = raf.Length;
+ // this.buffer = raf.getChannel().map(MapMode.READ_ONLY, 0, length); // {{Aroush-1.9}}
+ }
+
+ public override byte ReadByte()
+ {
+ return 0; // return buffer.get_Renamed(); // {{Aroush-1.9}}
+ }
+
+ public override void ReadBytes(byte[] b, int offset, int len)
+ {
+ // buffer.get_Renamed(b, offset, len); // {{Aroush-1.9}}
+ }
+
+ public override long GetFilePointer()
+ {
+ return buffer.Position;
+ }
+
+ public override void Seek(long pos)
+ {
+ buffer.Seek(pos, System.IO.SeekOrigin.Begin);
+ }
+
+ public override long Length()
+ {
+ return length;
+ }
+
+ public override System.Object Clone()
+ {
+ MMapIndexInput clone = (MMapIndexInput) base.Clone();
+ // clone.buffer = buffer.duplicate(); // {{Aroush-1.9}}
+ return clone;
+ }
+
+ public override void Close()
+ {
+ }
+ }
+
+ /* Added class MultiMMapIndexInput, Paul Elschot.
+ * Slightly adapted constructor of MMapIndexInput.
+ * Licensed under the Apache License, Version 2.0.
+ */
+ private class MultiMMapIndexInput:IndexInput, System.ICloneable
+ {
+
+ private System.IO.FileStream[] buffers; // private ByteBuffer[] buffers; // {{Aroush-1.9}}
+ private int[] bufSizes; // keep here, ByteBuffer.size() method is optional
+
+ private long length;
+
+ private int curBufIndex;
+ private int maxBufSize;
+
+ private System.IO.FileStream curBuf; // private ByteBuffer curBuf; // {{Aroush-1.9}} // redundant for speed: buffers[curBufIndex]
+ private int curAvail; // redundant for speed: (bufSizes[curBufIndex] - curBuf.position())
+
+
+ public MultiMMapIndexInput(System.IO.FileStream raf, int maxBufSize)
+ {
+ this.length = raf.Length;
+ this.maxBufSize = maxBufSize;
+
+ if (maxBufSize <= 0)
+ throw new System.ArgumentException("Non positive maxBufSize: " + maxBufSize);
+
+ if ((length / maxBufSize) > System.Int32.MaxValue)
+ {
+ throw new System.ArgumentException("RandomAccessFile too big for maximum buffer size: " + raf.ToString());
+ }
+
+ int nrBuffers = (int) (length / maxBufSize);
+ if ((nrBuffers * maxBufSize) < length)
+ nrBuffers++;
+
+ this.buffers = new System.IO.FileStream[nrBuffers]; // this.buffers = new ByteBuffer[nrBuffers]; // {{Aroush-1.9}}
+ this.bufSizes = new int[nrBuffers];
+
+ long bufferStart = 0;
+ System.IO.FileStream rafc = null; // FileChannel rafc = raf.getChannel(); // {{Aroush-1.9}}
+ for (int bufNr = 0; bufNr < nrBuffers; bufNr++)
+ {
+ int bufSize = (length > (bufferStart + maxBufSize))?maxBufSize:(int) (length - bufferStart);
+ // this.buffers[bufNr] = rafc.map(MapMode.READ_ONLY, bufferStart, bufSize); // {{Aroush-1.9}}
+ this.bufSizes[bufNr] = bufSize;
+ bufferStart += bufSize;
+ }
+ Seek(0L);
+ }
+
+ public override byte ReadByte()
+ {
+ // Performance might be improved by reading ahead into an array of
+ // eg. 128 bytes and readByte() from there.
+ if (curAvail == 0)
+ {
+ curBufIndex++;
+ curBuf = buffers[curBufIndex]; // index out of bounds when too many bytes requested
+ curBuf.Seek(0, System.IO.SeekOrigin.Begin);
+ curAvail = bufSizes[curBufIndex];
+ }
+ curAvail--;
+ return 0; // return curBuf.get_Renamed(); // {{Aroush-1.9}}
+ }
+
+ public override void ReadBytes(byte[] b, int offset, int len)
+ {
+ while (len > curAvail)
+ {
+ // curBuf.get_Renamed(b, offset, curAvail); // {{Aroush-1.9}}
+ len -= curAvail;
+ offset += curAvail;
+ curBufIndex++;
+ curBuf = buffers[curBufIndex]; // index out of bounds when too many bytes requested
+ curBuf.Seek(0, System.IO.SeekOrigin.Begin);
+ curAvail = bufSizes[curBufIndex];
+ }
+ // curBuf.get_Renamed(b, offset, len); // {{Aroush-1.9}}
+ curAvail -= len;
+ }
+
+ public override long GetFilePointer()
+ {
+ return (curBufIndex * (long) maxBufSize) + curBuf.Position;
+ }
+
+ public override void Seek(long pos)
+ {
+ curBufIndex = (int) (pos / maxBufSize);
+ curBuf = buffers[curBufIndex];
+ int bufOffset = (int) (pos - (curBufIndex * maxBufSize));
+ curBuf.Seek(bufOffset, System.IO.SeekOrigin.Begin);
+ curAvail = bufSizes[curBufIndex] - bufOffset;
+ }
+
+ public override long Length()
+ {
+ return length;
+ }
+
+ public override System.Object Clone()
+ {
+ MultiMMapIndexInput clone = (MultiMMapIndexInput) base.Clone();
+ // clone.buffers = new ByteBuffer[buffers.length]; // {{Aroush-1.9}}
+ // No need to clone bufSizes.
+ // Since most clones will use only one buffer, duplicate() could also be
+ // done lazy in clones, eg. when adapting curBuf.
+ for (int bufNr = 0; bufNr < buffers.Length; bufNr++)
+ {
+ // clone.buffers[bufNr] = buffers[bufNr].duplicate(); // {{Aroush-1.9}}
+ }
+ try
+ {
+ clone.Seek(GetFilePointer());
+ }
+ catch (System.IO.IOException ioe)
+ {
+ throw new System.Exception(ioe.ToString()); // {{Aroush-1.9}} should be re-thrown as RuntimeException
+ }
+ return clone;
+ }
+
+ public override void Close()
+ {
+ }
+ }
+
+ private int MAX_BBUF = System.Int32.MaxValue;
+
+ public override IndexInput OpenInput(System.String name)
+ {
+ System.IO.FileInfo f = new System.IO.FileInfo(System.IO.Path.Combine(GetFile().FullName, name));
+ System.IO.FileStream raf = new System.IO.FileStream(f.FullName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
+ try
+ {
+ return (raf.Length <= MAX_BBUF) ? (IndexInput) new MMapIndexInput(raf) : (IndexInput) new MultiMMapIndexInput(raf, MAX_BBUF);
+ }
+ finally
+ {
+ raf.Close();
+ }
+ }
+ }
+}
\ No newline at end of file
Modified: incubator/lucene.net/trunk/C#/src/Lucene.Net/Store/OutputStream.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Lucene.Net/Store/OutputStream.cs?rev=411501&r1=411500&r2=411501&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Lucene.Net/Store/OutputStream.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Lucene.Net/Store/OutputStream.cs Sat Jun 3 19:41:13 2006
@@ -13,184 +13,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
using System;
+
namespace Lucene.Net.Store
{
- /// <summary>Abstract class for output to a file in a Directory. A random-access output
- /// stream. Used for all Lucene index output operations.
- /// </summary>
- /// <seealso cref="Directory">
- /// </seealso>
- /// <seealso cref="InputStream">
- /// </seealso>
- public abstract class OutputStream
+ /// <deprecated> Use {@link IndexOutput} or {@link BufferedIndexOutput}
+ /// instead.
+ /// </deprecated>
+ public abstract class OutputStream : BufferedIndexOutput
{
- internal const int BUFFER_SIZE = 1024;
-
- private byte[] buffer = new byte[BUFFER_SIZE];
- private long bufferStart = 0; // position in file of buffer
- private int bufferPosition = 0; // position in buffer
-
- /// <summary>Writes a single byte.</summary>
- /// <seealso cref="InputStream#ReadByte()">
- /// </seealso>
- public void WriteByte(byte b)
- {
- if (bufferPosition >= BUFFER_SIZE)
- Flush();
- buffer[bufferPosition++] = b;
- }
-
- /// <summary>Writes an array of bytes.</summary>
- /// <param name="b">the bytes to write
- /// </param>
- /// <param name="length">the number of bytes to write
- /// </param>
- /// <seealso cref="InputStream#ReadBytes(byte[],int,int)">
- /// </seealso>
- public void WriteBytes(byte[] b, int length)
- {
- for (int i = 0; i < length; i++)
- WriteByte(b[i]);
- }
-
- /// <summary>Writes an int as four bytes.</summary>
- /// <seealso cref="InputStream#ReadInt()">
- /// </seealso>
- public void WriteInt(int i)
- {
- WriteByte((byte) (i >> 24));
- WriteByte((byte) (i >> 16));
- WriteByte((byte) (i >> 8));
- WriteByte((byte) i);
- }
-
- /// <summary>Writes an int in a variable-length format. Writes between one and
- /// five bytes. Smaller values take fewer bytes. Negative numbers are not
- /// supported.
- /// </summary>
- /// <seealso cref="InputStream#ReadVInt()">
- /// </seealso>
- public void WriteVInt(int i)
- {
- while ((i & ~ 0x7F) != 0)
- {
- WriteByte((byte) ((i & 0x7f) | 0x80));
- i = (int) (((uint) i) >> 7);
- }
- WriteByte((byte) i);
- }
-
- /// <summary>Writes a long as eight bytes.</summary>
- /// <seealso cref="InputStream#ReadLong()">
- /// </seealso>
- public void WriteLong(long i)
- {
- WriteInt((int) (i >> 32));
- WriteInt((int) i);
- }
-
- /// <summary>Writes an long in a variable-length format. Writes between one and five
- /// bytes. Smaller values take fewer bytes. Negative numbers are not
- /// supported.
- /// </summary>
- /// <seealso cref="InputStream#ReadVLong()">
- /// </seealso>
- public void WriteVLong(long i)
- {
- while ((i & ~ 0x7F) != 0)
- {
- WriteByte((byte) ((i & 0x7f) | 0x80));
- i = (long) (((long) i) >> 7);
- }
- WriteByte((byte) i);
- }
-
- /// <summary>Writes a string.</summary>
- /// <seealso cref="InputStream#ReadString()">
- /// </seealso>
- public void WriteString(System.String s)
- {
- int length = s.Length;
- WriteVInt(length);
- WriteChars(s, 0, length);
- }
-
- /// <summary>Writes a sequence of UTF-8 encoded characters from a string.</summary>
- /// <param name="s">the source of the characters
- /// </param>
- /// <param name="start">the first character in the sequence
- /// </param>
- /// <param name="length">the number of characters in the sequence
- /// </param>
- /// <seealso cref="InputStream#ReadChars(char[],int,int)">
- /// </seealso>
- public void WriteChars(System.String s, int start, int length)
- {
- int end = start + length;
- for (int i = start; i < end; i++)
- {
- int code = (int) s[i];
- if (code >= 0x01 && code <= 0x7F)
- WriteByte((byte) code);
- else if (((code >= 0x80) && (code <= 0x7FF)) || code == 0)
- {
- WriteByte((byte) (0xC0 | (code >> 6)));
- WriteByte((byte) (0x80 | (code & 0x3F)));
- }
- else
- {
- WriteByte((byte) (0xE0 | (((uint) code) >> 12)));
- WriteByte((byte) (0x80 | ((code >> 6) & 0x3F)));
- WriteByte((byte) (0x80 | (code & 0x3F)));
- }
- }
- }
-
- /// <summary>Forces any buffered output to be written. </summary>
- protected internal void Flush()
- {
- FlushBuffer(buffer, bufferPosition);
- bufferStart += bufferPosition;
- bufferPosition = 0;
- }
-
- /// <summary>Expert: implements buffer write. Writes bytes at the current position in
- /// the output.
- /// </summary>
- /// <param name="b">the bytes to write
- /// </param>
- /// <param name="len">the number of bytes to write
- /// </param>
- public abstract void FlushBuffer(byte[] b, int len);
-
- /// <summary>Closes this stream to further operations. </summary>
- public virtual void Close()
- {
- Flush();
- }
-
- /// <summary>Returns the current position in this file, where the next write will
- /// occur.
- /// </summary>
- /// <seealso cref="#Seek(long)">
- /// </seealso>
- public long GetFilePointer()
- {
- return bufferStart + bufferPosition;
- }
-
- /// <summary>Sets current position in this file, where the next write will occur.</summary>
- /// <seealso cref="#GetFilePointer()">
- /// </seealso>
- public virtual void Seek(long pos)
- {
- Flush();
- bufferStart = pos;
- }
-
- /// <summary>The number of bytes in the file. </summary>
- public abstract long Length();
}
}