API: Lucene.Net.Documents.Field: Refactored GetNumericType() to be a property, NumericType, that is set to an enumeration value. Checking the underlying numeric field type is simpler this way, being that GetNumericValue() does boxing/unboxing and using it would therefore not be efficient.
Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/29cbc138 Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/29cbc138 Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/29cbc138 Branch: refs/heads/master Commit: 29cbc138b5096a0063d3bfa89a26e306c7f7b710 Parents: e1a2efb Author: Shad Storhaug <[email protected]> Authored: Tue Aug 15 04:48:00 2017 +0700 Committer: Shad Storhaug <[email protected]> Committed: Tue Aug 15 04:50:05 2017 +0700 ---------------------------------------------------------------------- .../Collation/ICUCollationDocValuesField.cs | 2 +- .../Utils/DatasetSplitter.cs | 14 +- .../SimpleText/SimpleTextStoredFieldsWriter.cs | 87 +++-- src/Lucene.Net.Misc/Document/LazyDocument.cs | 20 +- .../Lucene3x/PreFlexRWStoredFieldsWriter.cs | 122 +++---- .../Index/TestIndexWriterExceptions.cs | 4 +- .../Index/TestIndexableField.cs | 4 +- .../CompressingStoredFieldsWriter.cs | 76 +++-- .../Lucene40/Lucene40StoredFieldsWriter.cs | 77 +++-- src/Lucene.Net/Document/BinaryDocValuesField.cs | 2 +- src/Lucene.Net/Document/DoubleField.cs | 10 +- src/Lucene.Net/Document/Field.cs | 315 ++++++++++++------- src/Lucene.Net/Document/FloatField.cs | 10 +- src/Lucene.Net/Document/IntField.cs | 10 +- src/Lucene.Net/Document/LongField.cs | 10 +- .../Document/NumericDocValuesField.cs | 2 +- src/Lucene.Net/Document/SortedDocValuesField.cs | 2 +- .../Document/SortedSetDocValuesField.cs | 2 +- src/Lucene.Net/Document/StoredField.cs | 8 +- src/Lucene.Net/Index/DocValuesProcessor.cs | 5 +- src/Lucene.Net/Index/IndexableField.cs | 22 +- 21 files changed, 451 insertions(+), 353 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucenenet/blob/29cbc138/src/Lucene.Net.Analysis.ICU/Collation/ICUCollationDocValuesField.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Analysis.ICU/Collation/ICUCollationDocValuesField.cs b/src/Lucene.Net.Analysis.ICU/Collation/ICUCollationDocValuesField.cs index 4217ba4..e1de700 100644 --- a/src/Lucene.Net.Analysis.ICU/Collation/ICUCollationDocValuesField.cs +++ b/src/Lucene.Net.Analysis.ICU/Collation/ICUCollationDocValuesField.cs @@ -43,7 +43,7 @@ namespace Lucene.Net.Collation { this.name = name; this.collator = (Collator)collator.Clone(); - m_fieldsData = bytes; // so wrong setters cannot be called + FieldsData = bytes; // so wrong setters cannot be called } public override string Name http://git-wip-us.apache.org/repos/asf/lucenenet/blob/29cbc138/src/Lucene.Net.Classification/Utils/DatasetSplitter.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Classification/Utils/DatasetSplitter.cs b/src/Lucene.Net.Classification/Utils/DatasetSplitter.cs index 8204225..702e363 100644 --- a/src/Lucene.Net.Classification/Utils/DatasetSplitter.cs +++ b/src/Lucene.Net.Classification/Utils/DatasetSplitter.cs @@ -2,6 +2,7 @@ using Lucene.Net.Analysis; using Lucene.Net.Documents; using Lucene.Net.Index; using Lucene.Net.Search; +using Lucene.Net.Util; using System; using System.Globalization; using System.IO; @@ -60,9 +61,9 @@ namespace Lucene.Net.Classification.Utils { #pragma warning disable 612, 618 // create IWs for train / test / cv IDXs - IndexWriter testWriter = new IndexWriter(testIndex, new IndexWriterConfig(Util.LuceneVersion.LUCENE_CURRENT, analyzer)); - IndexWriter cvWriter = new IndexWriter(crossValidationIndex, new IndexWriterConfig(Util.LuceneVersion.LUCENE_CURRENT, analyzer)); - IndexWriter trainingWriter = new IndexWriter(trainingIndex, new IndexWriterConfig(Util.LuceneVersion.LUCENE_CURRENT, analyzer)); + IndexWriter testWriter = new IndexWriter(testIndex, new IndexWriterConfig(LuceneVersion.LUCENE_CURRENT, analyzer)); + IndexWriter cvWriter = new IndexWriter(crossValidationIndex, new IndexWriterConfig(LuceneVersion.LUCENE_CURRENT, analyzer)); + IndexWriter trainingWriter = new IndexWriter(trainingIndex, new IndexWriterConfig(LuceneVersion.LUCENE_CURRENT, analyzer)); #pragma warning restore 612, 618 try @@ -70,7 +71,7 @@ namespace Lucene.Net.Classification.Utils int size = originalIndex.MaxDoc; IndexSearcher indexSearcher = new IndexSearcher(originalIndex); - TopDocs topDocs = indexSearcher.Search(new MatchAllDocsQuery(), Int32.MaxValue); + TopDocs topDocs = indexSearcher.Search(new MatchAllDocsQuery(), int.MaxValue); // set the type to be indexed, stored, with term vectors FieldType ft = new FieldType(TextField.TYPE_STORED); @@ -108,12 +109,11 @@ namespace Lucene.Net.Classification.Utils { doc.Add(new Field(storableField.Name, storableField.GetStringValue(), ft)); } - else if (storableField.GetDoubleValue() != null) // LUCENENET specific - This will cast any numeric type an check whether there is a value without boxing/unboxing + else if (storableField.NumericType != NumericFieldType.NONE) // LUCENENET specific - checking the NumricType property is quicker than the type conversion { // LUCENENET specific - need to pass invariant culture here (we are assuming the Field will be stored) // and we need to round-trip floating point numbers so we don't lose precision. - Type numericType = storableField.GetNumericType(); - if (typeof(float).Equals(numericType) || typeof(double).Equals(numericType)) + if (storableField.NumericType == NumericFieldType.SINGLE || storableField.NumericType == NumericFieldType.DOUBLE) { // LUCENENET: Need to specify the "R" for round-trip: http://stackoverflow.com/a/611564 doc.Add(new Field(storableField.Name, storableField.GetStringValue("R", CultureInfo.InvariantCulture), ft)); http://git-wip-us.apache.org/repos/asf/lucenenet/blob/29cbc138/src/Lucene.Net.Codecs/SimpleText/SimpleTextStoredFieldsWriter.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Codecs/SimpleText/SimpleTextStoredFieldsWriter.cs b/src/Lucene.Net.Codecs/SimpleText/SimpleTextStoredFieldsWriter.cs index 0676811..a8e16cb 100644 --- a/src/Lucene.Net.Codecs/SimpleText/SimpleTextStoredFieldsWriter.cs +++ b/src/Lucene.Net.Codecs/SimpleText/SimpleTextStoredFieldsWriter.cs @@ -1,4 +1,5 @@ -using System; +using Lucene.Net.Documents; +using System; using System.Globalization; namespace Lucene.Net.Codecs.SimpleText @@ -108,52 +109,50 @@ namespace Lucene.Net.Codecs.SimpleText Write(TYPE); // LUCENENET specific - To avoid boxing/unboxing, we don't - // call GetNumericValue(). Instead, we check the field type and then + // call GetNumericValue(). Instead, we check the field.NumericType and then // call the appropriate conversion method. - Type numericType = field.GetNumericType(); - if (numericType != null) + if (field.NumericType != NumericFieldType.NONE) { - if (typeof(byte).Equals(numericType) || typeof(short).Equals(numericType) || typeof(int).Equals(numericType)) + switch (field.NumericType) { - Write(TYPE_INT); - NewLine(); - - Write(VALUE); - Write(field.GetStringValue(CultureInfo.InvariantCulture)); - NewLine(); - } - else if (typeof(long).Equals(numericType)) - { - Write(TYPE_LONG); - NewLine(); - - Write(VALUE); - Write(field.GetStringValue(CultureInfo.InvariantCulture)); - NewLine(); - } - else if (typeof(float).Equals(numericType)) - { - Write(TYPE_FLOAT); - NewLine(); - - Write(VALUE); - // LUCENENET: Need to specify the "R" for round-trip: http://stackoverflow.com/a/611564 - Write(field.GetStringValue("R", CultureInfo.InvariantCulture)); - NewLine(); - } - else if (typeof(double).Equals(numericType)) - { - Write(TYPE_DOUBLE); - NewLine(); - - Write(VALUE); - // LUCENENET: Need to specify the "R" for round-trip: http://stackoverflow.com/a/611564 - Write(field.GetStringValue("R", CultureInfo.InvariantCulture)); - NewLine(); - } - else - { - throw new ArgumentException("cannot store numeric type " + numericType); + case NumericFieldType.BYTE: + case NumericFieldType.INT16: + case NumericFieldType.INT32: + Write(TYPE_INT); + NewLine(); + + Write(VALUE); + Write(field.GetStringValue(CultureInfo.InvariantCulture)); + NewLine(); + break; + case NumericFieldType.INT64: + Write(TYPE_LONG); + NewLine(); + + Write(VALUE); + Write(field.GetStringValue(CultureInfo.InvariantCulture)); + NewLine(); + break; + case NumericFieldType.SINGLE: + Write(TYPE_FLOAT); + NewLine(); + + Write(VALUE); + // LUCENENET: Need to specify the "R" for round-trip: http://stackoverflow.com/a/611564 + Write(field.GetStringValue("R", CultureInfo.InvariantCulture)); + NewLine(); + break; + case NumericFieldType.DOUBLE: + Write(TYPE_DOUBLE); + NewLine(); + + Write(VALUE); + // LUCENENET: Need to specify the "R" for round-trip: http://stackoverflow.com/a/611564 + Write(field.GetStringValue("R", CultureInfo.InvariantCulture)); + NewLine(); + break; + default: + throw new System.ArgumentException("cannot store numeric type " + field.NumericType); } } else http://git-wip-us.apache.org/repos/asf/lucenenet/blob/29cbc138/src/Lucene.Net.Misc/Document/LazyDocument.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Misc/Document/LazyDocument.cs b/src/Lucene.Net.Misc/Document/LazyDocument.cs index f961bac..0f6a7a2 100644 --- a/src/Lucene.Net.Misc/Document/LazyDocument.cs +++ b/src/Lucene.Net.Misc/Document/LazyDocument.cs @@ -258,26 +258,34 @@ namespace Lucene.Net.Documents return GetRealValue().GetReaderValue(); } - [Obsolete("In .NET, use of this method will cause boxing/unboxing. Instead, call GetNumericType() to check the underlying type and call the appropriate GetXXXValue() method to retrieve the value.")] + [Obsolete("In .NET, use of this method will cause boxing/unboxing. Instead, use the NumericType property to check the underlying type and call the appropriate GetXXXValue() method to retrieve the value.")] public virtual object GetNumericValue() { return GetRealValue().GetNumericValue(); } /// <summary> - /// Gets the <see cref="Type"/> of the underlying value, or <c>null</c> if the value is not set or non-numeric. + /// Gets the <see cref="NumericFieldType"/> of the underlying value, or <see cref="NumericFieldType.NONE"/> if the value is not set or non-numeric. /// <para/> - /// LUCENENET specific. In Java, the numeric type was determined by checking the type of + /// Expert: The difference between this property and <see cref="FieldType.NumericType"/> is + /// this is represents the current state of the field (whether being written or read) and the + /// <see cref="FieldType"/> property represents instructions on how the field will be written, + /// but does not re-populate when reading back from an index (it is write-only). + /// <para/> + /// In Java, the numeric type was determined by checking the type of /// <see cref="GetNumericValue()"/>. However, since there are no reference number /// types in .NET, using <see cref="GetNumericValue()"/> so will cause boxing/unboxing. It is - /// therefore recommended to call this method to check the underlying type and the corresponding + /// therefore recommended to use this property to check the underlying type and the corresponding /// <c>Get*Value()</c> method to retrieve the value. + /// <para/> + /// NOTE: Since Lucene codecs do not support <see cref="NumericFieldType.BYTE"/> or <see cref="NumericFieldType.INT16"/>, + /// fields created with these types will always be <see cref="NumericFieldType.INT32"/> when read back from the index. /// </summary> // LUCENENET specific - Since we have no numeric reference types in .NET, this method was added to check // the numeric type of the inner field without boxing/unboxing. - public virtual Type GetNumericType() + public virtual NumericFieldType NumericType { - return GetRealValue().GetNumericType(); + get { return GetRealValue().NumericType; } } /// <summary> http://git-wip-us.apache.org/repos/asf/lucenenet/blob/29cbc138/src/Lucene.Net.TestFramework/Codecs/Lucene3x/PreFlexRWStoredFieldsWriter.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.TestFramework/Codecs/Lucene3x/PreFlexRWStoredFieldsWriter.cs b/src/Lucene.Net.TestFramework/Codecs/Lucene3x/PreFlexRWStoredFieldsWriter.cs index 5e37f25..6446e4f 100644 --- a/src/Lucene.Net.TestFramework/Codecs/Lucene3x/PreFlexRWStoredFieldsWriter.cs +++ b/src/Lucene.Net.TestFramework/Codecs/Lucene3x/PreFlexRWStoredFieldsWriter.cs @@ -1,3 +1,4 @@ +using Lucene.Net.Documents; using System; using System.Diagnostics; @@ -36,25 +37,25 @@ namespace Lucene.Net.Codecs.Lucene3x #pragma warning disable 612, 618 internal sealed class PreFlexRWStoredFieldsWriter : StoredFieldsWriter { - private readonly Directory Directory; - private readonly string Segment; - private IndexOutput FieldsStream; - private IndexOutput IndexStream; + private readonly Directory directory; + private readonly string segment; + private IndexOutput fieldsStream; + private IndexOutput indexStream; public PreFlexRWStoredFieldsWriter(Directory directory, string segment, IOContext context) { Debug.Assert(directory != null); - this.Directory = directory; - this.Segment = segment; + this.directory = directory; + this.segment = segment; bool success = false; try { - FieldsStream = directory.CreateOutput(IndexFileNames.SegmentFileName(segment, "", Lucene3xStoredFieldsReader.FIELDS_EXTENSION), context); - IndexStream = directory.CreateOutput(IndexFileNames.SegmentFileName(segment, "", Lucene3xStoredFieldsReader.FIELDS_INDEX_EXTENSION), context); + fieldsStream = directory.CreateOutput(IndexFileNames.SegmentFileName(segment, "", Lucene3xStoredFieldsReader.FIELDS_EXTENSION), context); + indexStream = directory.CreateOutput(IndexFileNames.SegmentFileName(segment, "", Lucene3xStoredFieldsReader.FIELDS_INDEX_EXTENSION), context); - FieldsStream.WriteInt32(Lucene3xStoredFieldsReader.FORMAT_CURRENT); - IndexStream.WriteInt32(Lucene3xStoredFieldsReader.FORMAT_CURRENT); + fieldsStream.WriteInt32(Lucene3xStoredFieldsReader.FORMAT_CURRENT); + indexStream.WriteInt32(Lucene3xStoredFieldsReader.FORMAT_CURRENT); success = true; } @@ -73,8 +74,8 @@ namespace Lucene.Net.Codecs.Lucene3x // in the correct fields format. public override void StartDocument(int numStoredFields) { - IndexStream.WriteInt64(FieldsStream.GetFilePointer()); - FieldsStream.WriteVInt32(numStoredFields); + indexStream.WriteInt64(fieldsStream.GetFilePointer()); + fieldsStream.WriteVInt32(numStoredFields); } protected override void Dispose(bool disposing) @@ -83,11 +84,11 @@ namespace Lucene.Net.Codecs.Lucene3x { try { - IOUtils.Dispose(FieldsStream, IndexStream); + IOUtils.Dispose(fieldsStream, indexStream); } finally { - FieldsStream = IndexStream = null; + fieldsStream = indexStream = null; } } } @@ -103,12 +104,12 @@ namespace Lucene.Net.Codecs.Lucene3x #pragma warning restore 168 { } - IOUtils.DeleteFilesIgnoringExceptions(Directory, IndexFileNames.SegmentFileName(Segment, "", Lucene3xStoredFieldsReader.FIELDS_EXTENSION), IndexFileNames.SegmentFileName(Segment, "", Lucene3xStoredFieldsReader.FIELDS_INDEX_EXTENSION)); + IOUtils.DeleteFilesIgnoringExceptions(directory, IndexFileNames.SegmentFileName(segment, "", Lucene3xStoredFieldsReader.FIELDS_EXTENSION), IndexFileNames.SegmentFileName(segment, "", Lucene3xStoredFieldsReader.FIELDS_INDEX_EXTENSION)); } public override void WriteField(FieldInfo info, IIndexableField field) { - FieldsStream.WriteVInt32(info.Number); + fieldsStream.WriteVInt32(info.Number); int bits = 0; BytesRef bytes; string @string; @@ -118,31 +119,30 @@ namespace Lucene.Net.Codecs.Lucene3x // can customize... // LUCENENET specific - To avoid boxing/unboxing, we don't - // call GetNumericValue(). Instead, we check the field type and then + // call GetNumericValue(). Instead, we check the field.NumericType and then // call the appropriate conversion method. - Type numericType = field.GetNumericType(); - if (numericType != null) + if (field.NumericType != NumericFieldType.NONE) { - if (typeof(byte).Equals(numericType) || typeof(short).Equals(numericType) || typeof(int).Equals(numericType)) + switch (field.NumericType) { - bits |= Lucene3xStoredFieldsReader.FIELD_IS_NUMERIC_INT; - } - else if (typeof(long).Equals(numericType)) - { - bits |= Lucene3xStoredFieldsReader.FIELD_IS_NUMERIC_LONG; - } - else if (typeof(float).Equals(numericType)) - { - bits |= Lucene3xStoredFieldsReader.FIELD_IS_NUMERIC_FLOAT; - } - else if (typeof(double).Equals(numericType)) - { - bits |= Lucene3xStoredFieldsReader.FIELD_IS_NUMERIC_DOUBLE; - } - else - { - throw new System.ArgumentException("cannot store numeric type " + numericType); + case NumericFieldType.BYTE: + case NumericFieldType.INT16: + case NumericFieldType.INT32: + bits |= Lucene3xStoredFieldsReader.FIELD_IS_NUMERIC_INT; + break; + case NumericFieldType.INT64: + bits |= Lucene3xStoredFieldsReader.FIELD_IS_NUMERIC_LONG; + break; + case NumericFieldType.SINGLE: + bits |= Lucene3xStoredFieldsReader.FIELD_IS_NUMERIC_FLOAT; + break; + case NumericFieldType.DOUBLE: + bits |= Lucene3xStoredFieldsReader.FIELD_IS_NUMERIC_DOUBLE; + break; + default: + throw new System.ArgumentException("cannot store numeric type " + field.NumericType); } + @string = null; bytes = null; } @@ -164,52 +164,52 @@ namespace Lucene.Net.Codecs.Lucene3x } } - FieldsStream.WriteByte((byte)(sbyte)bits); + fieldsStream.WriteByte((byte)(sbyte)bits); if (bytes != null) { - FieldsStream.WriteVInt32(bytes.Length); - FieldsStream.WriteBytes(bytes.Bytes, bytes.Offset, bytes.Length); + fieldsStream.WriteVInt32(bytes.Length); + fieldsStream.WriteBytes(bytes.Bytes, bytes.Offset, bytes.Length); } else if (@string != null) { - FieldsStream.WriteString(field.GetStringValue()); + fieldsStream.WriteString(field.GetStringValue()); } else { - if (typeof(byte).Equals(numericType) || typeof(short).Equals(numericType) || typeof(int).Equals(numericType)) - { - FieldsStream.WriteInt32(field.GetInt32Value().Value); - } - else if (typeof(long).Equals(numericType)) - { - FieldsStream.WriteInt64(field.GetInt64Value().Value); - } - else if (typeof(float).Equals(numericType)) - { - FieldsStream.WriteInt32(Number.SingleToInt32Bits(field.GetSingleValue().Value)); - } - else if (typeof(double).Equals(numericType)) - { - FieldsStream.WriteInt64(BitConverter.DoubleToInt64Bits(field.GetDoubleValue().Value)); - } - else + switch (field.NumericType) { - Debug.Assert(false); + case NumericFieldType.BYTE: + case NumericFieldType.INT16: + case NumericFieldType.INT32: + fieldsStream.WriteInt32(field.GetInt32Value().Value); + break; + case NumericFieldType.INT64: + fieldsStream.WriteInt64(field.GetInt64Value().Value); + break; + case NumericFieldType.SINGLE: + fieldsStream.WriteInt32(Number.SingleToInt32Bits(field.GetSingleValue().Value)); + break; + case NumericFieldType.DOUBLE: + fieldsStream.WriteInt64(BitConverter.DoubleToInt64Bits(field.GetDoubleValue().Value)); + break; + default: + Debug.Assert(false); + break; } } } public override void Finish(FieldInfos fis, int numDocs) { - if (4 + ((long)numDocs) * 8 != IndexStream.GetFilePointer()) + if (4 + ((long)numDocs) * 8 != indexStream.GetFilePointer()) // this is most likely a bug in Sun JRE 1.6.0_04/_05; // we detect that the bug has struck, here, and // throw an exception to prevent the corruption from // entering the index. See LUCENE-1282 for // details. { - throw new Exception("fdx size mismatch: docCount is " + numDocs + " but fdx file size is " + IndexStream.GetFilePointer() + " file=" + IndexStream.ToString() + "; now aborting this merge to prevent index corruption"); + throw new Exception("fdx size mismatch: docCount is " + numDocs + " but fdx file size is " + indexStream.GetFilePointer() + " file=" + indexStream.ToString() + "; now aborting this merge to prevent index corruption"); } } } http://git-wip-us.apache.org/repos/asf/lucenenet/blob/29cbc138/src/Lucene.Net.Tests/Index/TestIndexWriterExceptions.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Tests/Index/TestIndexWriterExceptions.cs b/src/Lucene.Net.Tests/Index/TestIndexWriterExceptions.cs index 71cc38a..cb0681a 100644 --- a/src/Lucene.Net.Tests/Index/TestIndexWriterExceptions.cs +++ b/src/Lucene.Net.Tests/Index/TestIndexWriterExceptions.cs @@ -2051,9 +2051,9 @@ namespace Lucene.Net.Index // LUCENENET specific - Since we have no numeric reference types in .NET, this method was added to check // the numeric type of the inner field without boxing/unboxing. - public virtual Type GetNumericType() + public virtual NumericFieldType NumericType { - return null; + get { return NumericFieldType.NONE; } } // LUCENENET specific - created overload for Byte, since we have no Number class in .NET http://git-wip-us.apache.org/repos/asf/lucenenet/blob/29cbc138/src/Lucene.Net.Tests/Index/TestIndexableField.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Tests/Index/TestIndexableField.cs b/src/Lucene.Net.Tests/Index/TestIndexableField.cs index 4be2e2e..976f45f 100644 --- a/src/Lucene.Net.Tests/Index/TestIndexableField.cs +++ b/src/Lucene.Net.Tests/Index/TestIndexableField.cs @@ -215,9 +215,9 @@ namespace Lucene.Net.Index // LUCENENET specific - Since we have no numeric reference types in .NET, this method was added to check // the numeric type of the inner field without boxing/unboxing. - public virtual Type GetNumericType() + public virtual NumericFieldType NumericType { - return null; + get { return NumericFieldType.NONE; } } // LUCENENET specific - created overload for Byte, since we have no Number class in .NET http://git-wip-us.apache.org/repos/asf/lucenenet/blob/29cbc138/src/Lucene.Net/Codecs/Compressing/CompressingStoredFieldsWriter.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Codecs/Compressing/CompressingStoredFieldsWriter.cs b/src/Lucene.Net/Codecs/Compressing/CompressingStoredFieldsWriter.cs index 6703e33..4d71a2d 100644 --- a/src/Lucene.Net/Codecs/Compressing/CompressingStoredFieldsWriter.cs +++ b/src/Lucene.Net/Codecs/Compressing/CompressingStoredFieldsWriter.cs @@ -1,4 +1,5 @@ using Lucene.Net.Codecs.Lucene40; +using Lucene.Net.Documents; using Lucene.Net.Index; using Lucene.Net.Store; using Lucene.Net.Support; @@ -271,30 +272,28 @@ namespace Lucene.Net.Codecs.Compressing string @string; // LUCENENET specific - To avoid boxing/unboxing, we don't - // call GetNumericValue(). Instead, we check the field type and then + // call GetNumericValue(). Instead, we check the field.NumericType and then // call the appropriate conversion method. - Type numericType = field.GetNumericType(); - if (numericType != null) + if (field.NumericType != NumericFieldType.NONE) { - if (typeof(byte).Equals(numericType) || typeof(short).Equals(numericType) || typeof(int).Equals(numericType)) + switch (field.NumericType) { - bits = NUMERIC_INT32; - } - else if (typeof(long).Equals(numericType)) - { - bits = NUMERIC_INT64; - } - else if (typeof(float).Equals(numericType)) - { - bits = NUMERIC_SINGLE; - } - else if (typeof(double).Equals(numericType)) - { - bits = NUMERIC_DOUBLE; - } - else - { - throw new System.ArgumentException("cannot store numeric type " + numericType); + case NumericFieldType.BYTE: + case NumericFieldType.INT16: + case NumericFieldType.INT32: + bits = NUMERIC_INT32; + break; + case NumericFieldType.INT64: + bits = NUMERIC_INT64; + break; + case NumericFieldType.SINGLE: + bits = NUMERIC_SINGLE; + break; + case NumericFieldType.DOUBLE: + bits = NUMERIC_DOUBLE; + break; + default: + throw new System.ArgumentException("cannot store numeric type " + field.NumericType); } @string = null; @@ -333,25 +332,24 @@ namespace Lucene.Net.Codecs.Compressing } else { - if (typeof(byte).Equals(numericType) || typeof(short).Equals(numericType) || typeof(int).Equals(numericType)) + switch (field.NumericType) { - bufferedDocs.WriteInt32(field.GetInt32Value().Value); - } - else if (typeof(long).Equals(numericType)) - { - bufferedDocs.WriteInt64(field.GetInt64Value().Value); - } - else if (typeof(float).Equals(numericType)) - { - bufferedDocs.WriteInt32(Number.SingleToInt32Bits(field.GetSingleValue().Value)); - } - else if (typeof(double).Equals(numericType)) - { - bufferedDocs.WriteInt64(BitConverter.DoubleToInt64Bits(field.GetDoubleValue().Value)); - } - else - { - throw new Exception("Cannot get here"); + case NumericFieldType.BYTE: + case NumericFieldType.INT16: + case NumericFieldType.INT32: + bufferedDocs.WriteInt32(field.GetInt32Value().Value); + break; + case NumericFieldType.INT64: + bufferedDocs.WriteInt64(field.GetInt64Value().Value); + break; + case NumericFieldType.SINGLE: + bufferedDocs.WriteInt32(Number.SingleToInt32Bits(field.GetSingleValue().Value)); + break; + case NumericFieldType.DOUBLE: + bufferedDocs.WriteInt64(BitConverter.DoubleToInt64Bits(field.GetDoubleValue().Value)); + break; + default: + throw new Exception("Cannot get here"); } } } http://git-wip-us.apache.org/repos/asf/lucenenet/blob/29cbc138/src/Lucene.Net/Codecs/Lucene40/Lucene40StoredFieldsWriter.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Codecs/Lucene40/Lucene40StoredFieldsWriter.cs b/src/Lucene.Net/Codecs/Lucene40/Lucene40StoredFieldsWriter.cs index 92a6e5f..81787be 100644 --- a/src/Lucene.Net/Codecs/Lucene40/Lucene40StoredFieldsWriter.cs +++ b/src/Lucene.Net/Codecs/Lucene40/Lucene40StoredFieldsWriter.cs @@ -1,3 +1,4 @@ +using Lucene.Net.Documents; using Lucene.Net.Support; using System; using System.Diagnostics; @@ -162,31 +163,30 @@ namespace Lucene.Net.Codecs.Lucene40 // can customize... // LUCENENET specific - To avoid boxing/unboxing, we don't - // call GetNumericValue(). Instead, we check the field type and then + // call GetNumericValue(). Instead, we check the field.NumericType and then // call the appropriate conversion method. - Type numericType = field.GetNumericType(); - if (numericType != null) + if (field.NumericType != NumericFieldType.NONE) { - if (typeof(byte).Equals(numericType) || typeof(short).Equals(numericType) || typeof(int).Equals(numericType)) + switch (field.NumericType) { - bits |= FIELD_IS_NUMERIC_INT; - } - else if (typeof(long).Equals(numericType)) - { - bits |= FIELD_IS_NUMERIC_LONG; - } - else if (typeof(float).Equals(numericType)) - { - bits |= FIELD_IS_NUMERIC_FLOAT; - } - else if (typeof(double).Equals(numericType)) - { - bits |= FIELD_IS_NUMERIC_DOUBLE; - } - else - { - throw new System.ArgumentException("cannot store numeric type " + numericType); + case NumericFieldType.BYTE: + case NumericFieldType.INT16: + case NumericFieldType.INT32: + bits |= FIELD_IS_NUMERIC_INT; + break; + case NumericFieldType.INT64: + bits |= FIELD_IS_NUMERIC_LONG; + break; + case NumericFieldType.SINGLE: + bits |= FIELD_IS_NUMERIC_FLOAT; + break; + case NumericFieldType.DOUBLE: + bits |= FIELD_IS_NUMERIC_DOUBLE; + break; + default: + throw new System.ArgumentException("cannot store numeric type " + field.NumericType); } + @string = null; bytes = null; } @@ -221,25 +221,24 @@ namespace Lucene.Net.Codecs.Lucene40 } else { - if (typeof(byte).Equals(numericType) || typeof(short).Equals(numericType) || typeof(int).Equals(numericType)) - { - fieldsStream.WriteInt32(field.GetInt32Value().Value); - } - else if (typeof(long).Equals(numericType)) - { - fieldsStream.WriteInt64(field.GetInt64Value().Value); - } - else if (typeof(float).Equals(numericType)) - { - fieldsStream.WriteInt32(Number.SingleToInt32Bits(field.GetSingleValue().Value)); - } - else if (typeof(double).Equals(numericType)) - { - fieldsStream.WriteInt64(BitConverter.DoubleToInt64Bits(field.GetDoubleValue().Value)); - } - else + switch (field.NumericType) { - throw new InvalidOperationException("Cannot get here"); + case NumericFieldType.BYTE: + case NumericFieldType.INT16: + case NumericFieldType.INT32: + fieldsStream.WriteInt32(field.GetInt32Value().Value); + break; + case NumericFieldType.INT64: + fieldsStream.WriteInt64(field.GetInt64Value().Value); + break; + case NumericFieldType.SINGLE: + fieldsStream.WriteInt32(Number.SingleToInt32Bits(field.GetSingleValue().Value)); + break; + case NumericFieldType.DOUBLE: + fieldsStream.WriteInt64(BitConverter.DoubleToInt64Bits(field.GetDoubleValue().Value)); + break; + default: + throw new InvalidOperationException("Cannot get here"); } } } http://git-wip-us.apache.org/repos/asf/lucenenet/blob/29cbc138/src/Lucene.Net/Document/BinaryDocValuesField.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Document/BinaryDocValuesField.cs b/src/Lucene.Net/Document/BinaryDocValuesField.cs index 0c05d60..cacc1b9 100644 --- a/src/Lucene.Net/Document/BinaryDocValuesField.cs +++ b/src/Lucene.Net/Document/BinaryDocValuesField.cs @@ -61,7 +61,7 @@ namespace Lucene.Net.Documents public BinaryDocValuesField(string name, BytesRef value) : base(name, fType) { - m_fieldsData = value; + FieldsData = value; } } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/29cbc138/src/Lucene.Net/Document/DoubleField.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Document/DoubleField.cs b/src/Lucene.Net/Document/DoubleField.cs index 16d089d..c7b7400 100644 --- a/src/Lucene.Net/Document/DoubleField.cs +++ b/src/Lucene.Net/Document/DoubleField.cs @@ -131,14 +131,14 @@ namespace Lucene.Net.Documents TYPE_NOT_STORED.IsTokenized = true; TYPE_NOT_STORED.OmitNorms = true; TYPE_NOT_STORED.IndexOptions = IndexOptions.DOCS_ONLY; - TYPE_NOT_STORED.NumericType = NumericType.DOUBLE; + TYPE_NOT_STORED.NumericType = Documents.NumericType.DOUBLE; TYPE_NOT_STORED.Freeze(); TYPE_STORED.IsIndexed = true; TYPE_STORED.IsTokenized = true; TYPE_STORED.OmitNorms = true; TYPE_STORED.IndexOptions = IndexOptions.DOCS_ONLY; - TYPE_STORED.NumericType = NumericType.DOUBLE; + TYPE_STORED.NumericType = Documents.NumericType.DOUBLE; TYPE_STORED.IsStored = true; TYPE_STORED.Freeze(); } @@ -155,7 +155,7 @@ namespace Lucene.Net.Documents public DoubleField(string name, double value, Store stored) : base(name, stored == Store.YES ? TYPE_STORED : TYPE_NOT_STORED) { - m_fieldsData = new Double(value); + FieldsData = new Double(value); } /// <summary> @@ -170,11 +170,11 @@ namespace Lucene.Net.Documents public DoubleField(string name, double value, FieldType type) : base(name, type) { - if (type.NumericType != NumericType.DOUBLE) + if (type.NumericType != Documents.NumericType.DOUBLE) { throw new System.ArgumentException("type.NumericType must be NumericType.DOUBLE but got " + type.NumericType); } - m_fieldsData = new Double(value); + FieldsData = new Double(value); } } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/29cbc138/src/Lucene.Net/Document/Field.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Document/Field.cs b/src/Lucene.Net/Document/Field.cs index d7fea34..bff1d9a 100644 --- a/src/Lucene.Net/Document/Field.cs +++ b/src/Lucene.Net/Document/Field.cs @@ -63,8 +63,60 @@ namespace Lucene.Net.Documents protected readonly string m_name; /// <summary> - /// Field's value </summary> - protected object m_fieldsData; + /// Field's value. + /// </summary> + private object fieldsData; + + /// <summary> + /// Field's value + /// <para/> + /// Setting this property will automatically set the backing field for the + /// <see cref="NumericType"/> property. + /// </summary> + // LUCENENET specific: Made into a property + // so we can set the data type when it is set. + protected object FieldsData + { + get { return fieldsData; } + set + { + fieldsData = value; + + if (value is Int32) + { + numericType = NumericFieldType.INT32; + } + else if (value is Int64) + { + numericType = NumericFieldType.INT64; + } + else if (value is Single) + { + numericType = NumericFieldType.SINGLE; + } + else if (value is Double) + { + numericType = NumericFieldType.DOUBLE; + } + else if (value is Int16) + { + numericType = NumericFieldType.INT16; + } + else if (value is Byte) + { + numericType = NumericFieldType.BYTE; + } + else + { + numericType = NumericFieldType.NONE; + } + } + } + + /// <summary> + /// Field's numeric data type (or <see cref="NumericFieldType.NONE"/> if field non-numeric). + /// </summary> + private NumericFieldType numericType; /// <summary> /// Pre-analyzed <see cref="TokenStream"/> for indexed fields; this is @@ -136,7 +188,7 @@ namespace Lucene.Net.Documents } this.m_name = name; - this.m_fieldsData = reader; + this.FieldsData = reader; this.m_type = type; } @@ -173,7 +225,7 @@ namespace Lucene.Net.Documents } this.m_name = name; - this.m_fieldsData = null; + this.FieldsData = null; this.m_tokenStream = tokenStream; this.m_type = type; } @@ -240,7 +292,7 @@ namespace Lucene.Net.Documents { throw new System.ArgumentException("Fields with BytesRef values cannot be indexed"); } - this.m_fieldsData = bytes; + this.FieldsData = bytes; this.m_type = type; this.m_name = name; } @@ -281,7 +333,7 @@ namespace Lucene.Net.Documents this.m_type = type; this.m_name = name; - this.m_fieldsData = value; + this.FieldsData = value; } /// <summary> @@ -292,9 +344,9 @@ namespace Lucene.Net.Documents /// <returns>The string representation of the value if it is either a <see cref="string"/> or numeric type.</returns> public virtual string GetStringValue() // LUCENENET specific: Added verb Get to make it more clear that this returns the value { - if (m_fieldsData is string || m_fieldsData is Number) + if (FieldsData is string || FieldsData is Number) { - return m_fieldsData.ToString(); + return FieldsData.ToString(); } else { @@ -312,13 +364,13 @@ namespace Lucene.Net.Documents // LUCENENET specific overload. public virtual string GetStringValue(IFormatProvider provider) { - if (m_fieldsData is string) + if (FieldsData is string) { - return m_fieldsData.ToString(); + return FieldsData.ToString(); } - else if (m_fieldsData is Number) + else if (FieldsData is Number) { - return ((Number)m_fieldsData).ToString(provider); + return ((Number)FieldsData).ToString(provider); } else { @@ -336,13 +388,13 @@ namespace Lucene.Net.Documents // LUCENENET specific overload. public virtual string GetStringValue(string format) { - if (m_fieldsData is string) + if (FieldsData is string) { - return m_fieldsData.ToString(); + return FieldsData.ToString(); } - else if (m_fieldsData is Number) + else if (FieldsData is Number) { - return ((Number)m_fieldsData).ToString(format); + return ((Number)FieldsData).ToString(format); } else { @@ -361,13 +413,13 @@ namespace Lucene.Net.Documents // LUCENENET specific overload. public virtual string GetStringValue(string format, IFormatProvider provider) { - if (m_fieldsData is string) + if (FieldsData is string) { - return m_fieldsData.ToString(); + return FieldsData.ToString(); } - else if (m_fieldsData is Number) + else if (FieldsData is Number) { - return ((Number)m_fieldsData).ToString(format, provider); + return ((Number)FieldsData).ToString(format, provider); } else { @@ -382,7 +434,7 @@ namespace Lucene.Net.Documents /// </summary> public virtual TextReader GetReaderValue() // LUCENENET specific: Added verb Get to make it more clear that this returns the value { - return m_fieldsData is TextReader ? (TextReader)m_fieldsData : null; + return FieldsData is TextReader ? (TextReader)FieldsData : null; } /// <summary> @@ -412,11 +464,11 @@ namespace Lucene.Net.Documents /// </summary> public virtual void SetStringValue(string value) { - if (!(m_fieldsData is string)) + if (!(FieldsData is string)) { - throw new ArgumentException("cannot change value type from " + m_fieldsData.GetType().Name + " to string"); + throw new ArgumentException("cannot change value type from " + FieldsData.GetType().Name + " to string"); } - m_fieldsData = value; + FieldsData = value; } /// <summary> @@ -425,11 +477,11 @@ namespace Lucene.Net.Documents /// </summary> public virtual void SetReaderValue(TextReader value) { - if (!(m_fieldsData is TextReader)) + if (!(FieldsData is TextReader)) { - throw new ArgumentException("cannot change value type from " + m_fieldsData.GetType().Name + " to TextReader"); + throw new ArgumentException("cannot change value type from " + FieldsData.GetType().Name + " to TextReader"); } - m_fieldsData = value; + FieldsData = value; } /// <summary> @@ -441,15 +493,15 @@ namespace Lucene.Net.Documents /// </summary> public virtual void SetBytesValue(BytesRef value) { - if (!(m_fieldsData is BytesRef)) + if (!(FieldsData is BytesRef)) { - throw new System.ArgumentException("cannot change value type from " + m_fieldsData.GetType().Name + " to BytesRef"); + throw new System.ArgumentException("cannot change value type from " + FieldsData.GetType().Name + " to BytesRef"); } if (m_type.IsIndexed) { throw new System.ArgumentException("cannot set a BytesRef value on an indexed field"); } - m_fieldsData = value; + FieldsData = value; } /// <summary> @@ -467,11 +519,11 @@ namespace Lucene.Net.Documents /// </summary> public virtual void SetByteValue(byte value) { - if (!(m_fieldsData is Byte)) + if (!(FieldsData is Byte)) { - throw new System.ArgumentException("cannot change value type from " + m_fieldsData.GetType().Name + " to Byte"); + throw new System.ArgumentException("cannot change value type from " + FieldsData.GetType().Name + " to Byte"); } - m_fieldsData = new Byte(value); + FieldsData = new Byte(value); } /// <summary> @@ -480,11 +532,11 @@ namespace Lucene.Net.Documents /// </summary> public virtual void SetInt16Value(short value) // LUCENENET specific: Renamed from SetShortValue to follow .NET conventions { - if (!(m_fieldsData is Int16)) + if (!(FieldsData is Int16)) { - throw new System.ArgumentException("cannot change value type from " + m_fieldsData.GetType().Name + " to Short"); + throw new System.ArgumentException("cannot change value type from " + FieldsData.GetType().Name + " to Short"); } - m_fieldsData = new Int16(value); + FieldsData = new Int16(value); } /// <summary> @@ -493,11 +545,11 @@ namespace Lucene.Net.Documents /// </summary> public virtual void SetInt32Value(int value) // LUCENENET specific: Renamed from SetIntValue to follow .NET conventions { - if (!(m_fieldsData is Int32)) + if (!(FieldsData is Int32)) { - throw new System.ArgumentException("cannot change value type from " + m_fieldsData.GetType().Name + " to Integer"); + throw new System.ArgumentException("cannot change value type from " + FieldsData.GetType().Name + " to Integer"); } - m_fieldsData = new Int32(value); + FieldsData = new Int32(value); } /// <summary> @@ -506,11 +558,11 @@ namespace Lucene.Net.Documents /// </summary> public virtual void SetInt64Value(long value) // LUCENENET specific: Renamed from SetLongValue to follow .NET conventions { - if (!(m_fieldsData is Int64)) + if (!(FieldsData is Int64)) { - throw new System.ArgumentException("cannot change value type from " + m_fieldsData.GetType().Name + " to Long"); + throw new System.ArgumentException("cannot change value type from " + FieldsData.GetType().Name + " to Long"); } - m_fieldsData = new Int64(value); + FieldsData = new Int64(value); } /// <summary> @@ -519,11 +571,11 @@ namespace Lucene.Net.Documents /// </summary> public virtual void SetSingleValue(float value) // LUCENENET specific: Renamed from SetFloatValue to follow .NET conventions { - if (!(m_fieldsData is Single)) + if (!(FieldsData is Single)) { - throw new System.ArgumentException("cannot change value type from " + m_fieldsData.GetType().Name + " to Float"); + throw new System.ArgumentException("cannot change value type from " + FieldsData.GetType().Name + " to Float"); } - m_fieldsData = new Single(value); + FieldsData = new Single(value); } /// <summary> @@ -532,11 +584,11 @@ namespace Lucene.Net.Documents /// </summary> public virtual void SetDoubleValue(double value) { - if (!(m_fieldsData is Double)) + if (!(FieldsData is Double)) { - throw new System.ArgumentException("cannot change value type from " + m_fieldsData.GetType().Name + " to Double"); + throw new System.ArgumentException("cannot change value type from " + FieldsData.GetType().Name + " to Double"); } - m_fieldsData = new Double(value); + FieldsData = new Double(value); } // LUCENENET TODO: Add SetValue() overloads for each type? @@ -554,7 +606,7 @@ namespace Lucene.Net.Documents { throw new System.ArgumentException("TokenStream fields must be indexed and tokenized"); } - if (m_type.NumericType != NumericType.NONE) + if (m_type.NumericType != Documents.NumericType.NONE) { throw new System.ArgumentException("cannot set private TokenStream on numeric fields"); } @@ -594,7 +646,7 @@ namespace Lucene.Net.Documents } } - [Obsolete("In .NET, use of this method will cause boxing/unboxing. Instead, call GetNumericType() to check the underlying type and call the appropriate GetXXXValue() method to retrieve the value.")] + [Obsolete("In .NET, use of this method will cause boxing/unboxing. Instead, use the NumericType property to check the underlying type and call the appropriate GetXXXValue() method to retrieve the value.")] public virtual object GetNumericValue() // LUCENENET specific: Added verb Get to make it more clear that this returns the value { // LUCENENET NOTE: Originally, there was a conversion from string to a numeric value here. @@ -603,71 +655,55 @@ namespace Lucene.Net.Documents // wrong StoredFieldsVisitor method will be called (in this case it was calling Int64Field() instead of StringField()). // This is an extremely difficult thing to track down and very confusing to end users. - if (m_fieldsData is Int32) + if (FieldsData is Int32) { - return ((Int32)m_fieldsData).GetInt32Value(); + return ((Int32)FieldsData).GetInt32Value(); } - else if (m_fieldsData is Int64) + else if (FieldsData is Int64) { - return ((Int64)m_fieldsData).GetInt64Value(); + return ((Int64)FieldsData).GetInt64Value(); } - else if (m_fieldsData is Single) + else if (FieldsData is Single) { - return ((Single)m_fieldsData).GetSingleValue(); + return ((Single)FieldsData).GetSingleValue(); } - else if (m_fieldsData is Double) + else if (FieldsData is Double) { - return ((Double)m_fieldsData).GetDoubleValue(); + return ((Double)FieldsData).GetDoubleValue(); } - else if (m_fieldsData is Int16) + else if (FieldsData is Int16) { - return ((Int16)m_fieldsData).GetInt16Value(); + return ((Int16)FieldsData).GetInt16Value(); } - else if (m_fieldsData is Byte) + else if (FieldsData is Byte) { - return ((Byte)m_fieldsData).GetByteValue(); + return ((Byte)FieldsData).GetByteValue(); } return null; } /// <summary> - /// Gets the <see cref="Type"/> of the underlying value, or <c>null</c> if the value is not set or non-numeric. + /// Gets the <see cref="NumericFieldType"/> of the underlying value, or <see cref="NumericFieldType.NONE"/> if the value is not set or non-numeric. + /// <para/> + /// Expert: The difference between this property and <see cref="FieldType.NumericType"/> is + /// this is represents the current state of the field (whether being written or read) and the + /// <see cref="FieldType"/> property represents instructions on how the field will be written, + /// but does not re-populate when reading back from an index (it is write-only). /// <para/> - /// LUCENENET specific. In Java, the numeric type was determined by checking the type of + /// In Java, the numeric type was determined by checking the type of /// <see cref="GetNumericValue()"/>. However, since there are no reference number /// types in .NET, using <see cref="GetNumericValue()"/> so will cause boxing/unboxing. It is - /// therefore recommended to call this method to check the underlying type and the corresponding + /// therefore recommended to use this property to check the underlying type and the corresponding /// <c>Get*Value()</c> method to retrieve the value. + /// <para/> + /// NOTE: Since Lucene codecs do not support <see cref="NumericFieldType.BYTE"/> or <see cref="NumericFieldType.INT16"/>, + /// fields created with these types will always be <see cref="NumericFieldType.INT32"/> when read back from the index. /// </summary> - public virtual Type GetNumericType() + // LUCENENET specific + public virtual NumericFieldType NumericType { - if (m_fieldsData is Int32) - { - return typeof(int); - } - else if (m_fieldsData is Int64) - { - return typeof(long); - } - else if (m_fieldsData is Single) - { - return typeof(float); - } - else if (m_fieldsData is Double) - { - return typeof(double); - } - else if (m_fieldsData is Int16) - { - return typeof(short); - } - else if (m_fieldsData is Byte) - { - return typeof(byte); - } - - return null; + get { return numericType; } } /// <summary> @@ -678,9 +714,9 @@ namespace Lucene.Net.Documents // LUCENENET specific - created overload for Byte, since we have no Number class in .NET public virtual byte? GetByteValue() { - if (m_fieldsData is Number) + if (FieldsData is Number) { - return ((Number)m_fieldsData).GetByteValue(); + return ((Number)FieldsData).GetByteValue(); } else { @@ -696,9 +732,9 @@ namespace Lucene.Net.Documents // LUCENENET specific - created overload for Short, since we have no Number class in .NET public virtual short? GetInt16Value() { - if (m_fieldsData is Number) + if (FieldsData is Number) { - return ((Number)m_fieldsData).GetInt16Value(); + return ((Number)FieldsData).GetInt16Value(); } else { @@ -714,9 +750,9 @@ namespace Lucene.Net.Documents // LUCENENET specific - created overload for Int32, since we have no Number class in .NET public virtual int? GetInt32Value() { - if (m_fieldsData is Number) + if (FieldsData is Number) { - return ((Number)m_fieldsData).GetInt32Value(); + return ((Number)FieldsData).GetInt32Value(); } else { @@ -732,9 +768,9 @@ namespace Lucene.Net.Documents // LUCENENET specific - created overload for Int64, since we have no Number class in .NET public virtual long? GetInt64Value() { - if (m_fieldsData is Number) + if (FieldsData is Number) { - return ((Number)m_fieldsData).GetInt64Value(); + return ((Number)FieldsData).GetInt64Value(); } else { @@ -750,9 +786,9 @@ namespace Lucene.Net.Documents // LUCENENET specific - created overload for Single, since we have no Number class in .NET public virtual float? GetSingleValue() { - if (m_fieldsData is Number) + if (FieldsData is Number) { - return ((Number)m_fieldsData).GetSingleValue(); + return ((Number)FieldsData).GetSingleValue(); } else { @@ -768,9 +804,9 @@ namespace Lucene.Net.Documents // LUCENENET specific - created overload for Double, since we have no Number class in .NET public virtual double? GetDoubleValue() { - if (m_fieldsData is Number) + if (FieldsData is Number) { - return ((Number)m_fieldsData).GetDoubleValue(); + return ((Number)FieldsData).GetDoubleValue(); } else { @@ -782,9 +818,9 @@ namespace Lucene.Net.Documents /// Non-null if this field has a binary value. </summary> public virtual BytesRef GetBinaryValue() // LUCENENET specific: Added verb Get to make it more clear that this returns the value { - if (m_fieldsData is BytesRef) + if (FieldsData is BytesRef) { - return (BytesRef)m_fieldsData; + return (BytesRef)FieldsData; } else { @@ -802,9 +838,9 @@ namespace Lucene.Net.Documents result.Append(m_name); result.Append(':'); - if (m_fieldsData != null) + if (FieldsData != null) { - result.Append(m_fieldsData); + result.Append(FieldsData); } result.Append('>'); @@ -834,7 +870,7 @@ namespace Lucene.Net.Documents return null; } NumericType numericType = FieldType.NumericType; - if (numericType != NumericType.NONE) + if (numericType != Documents.NumericType.NONE) { if (!(internalTokenStream is NumericTokenStream)) { @@ -844,22 +880,22 @@ namespace Lucene.Net.Documents } var nts = (NumericTokenStream)internalTokenStream; // initialize value in TokenStream - Number val = (Number)m_fieldsData; + Number val = (Number)FieldsData; switch (numericType) { - case NumericType.INT32: + case Documents.NumericType.INT32: nts.SetInt32Value(val.GetInt32Value()); break; - case NumericType.INT64: + case Documents.NumericType.INT64: nts.SetInt64Value(val.GetInt64Value()); break; - case NumericType.SINGLE: + case Documents.NumericType.SINGLE: nts.SetSingleValue(val.GetSingleValue()); break; - case NumericType.DOUBLE: + case Documents.NumericType.DOUBLE: nts.SetDoubleValue(val.GetDoubleValue()); break; @@ -871,7 +907,7 @@ namespace Lucene.Net.Documents // LUCENENET specific - If the underlying type is numeric, we need // to ensure it is setup to be round-tripped. - string format = (numericType == NumericType.SINGLE || numericType == NumericType.DOUBLE) ? "R" : null; + string format = (numericType == Documents.NumericType.SINGLE || numericType == Documents.NumericType.DOUBLE) ? "R" : null; string stringValue = GetStringValue(format, CultureInfo.InvariantCulture); if (!IndexableFieldType.IsTokenized) @@ -1474,4 +1510,53 @@ namespace Lucene.Net.Documents return Field.TermVector.YES; } } + + /// <summary> + /// Data type of the numeric <see cref="IIndexableField"/> value + /// </summary> + // LUCENENET specific + // Since we have more numeric types on Field than on FieldType, + // a new enumeration was created for .NET. In Java, this type was + // determined by checking the data type of the Field.numericValue() + // method. However, since the corresponding GetNumericValue() method + // in .NET returns type object (which would result in boxing/unboxing), + // this has been refactored to use an enumeration instead, which makes the + // API easier to use. + public enum NumericFieldType + { + /// <summary> + /// No numeric type (the field is not numeric). + /// </summary> + NONE, + + /// <summary> + /// 8-bit unsigned integer numeric type + /// </summary> + BYTE, + + /// <summary> + /// 16-bit short numeric type + /// </summary> + INT16, + + /// <summary> + /// 32-bit integer numeric type + /// </summary> + INT32, + + /// <summary> + /// 64-bit long numeric type + /// </summary> + INT64, + + /// <summary> + /// 32-bit float numeric type + /// </summary> + SINGLE, + + /// <summary> + /// 64-bit double numeric type + /// </summary> + DOUBLE + } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/29cbc138/src/Lucene.Net/Document/FloatField.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Document/FloatField.cs b/src/Lucene.Net/Document/FloatField.cs index a602c99..c6cefe5 100644 --- a/src/Lucene.Net/Document/FloatField.cs +++ b/src/Lucene.Net/Document/FloatField.cs @@ -129,14 +129,14 @@ namespace Lucene.Net.Documents TYPE_NOT_STORED.IsTokenized = true; TYPE_NOT_STORED.OmitNorms = true; TYPE_NOT_STORED.IndexOptions = IndexOptions.DOCS_ONLY; - TYPE_NOT_STORED.NumericType = NumericType.SINGLE; + TYPE_NOT_STORED.NumericType = Documents.NumericType.SINGLE; TYPE_NOT_STORED.Freeze(); TYPE_STORED.IsIndexed = true; TYPE_STORED.IsTokenized = true; TYPE_STORED.OmitNorms = true; TYPE_STORED.IndexOptions = IndexOptions.DOCS_ONLY; - TYPE_STORED.NumericType = NumericType.SINGLE; + TYPE_STORED.NumericType = Documents.NumericType.SINGLE; TYPE_STORED.IsStored = true; TYPE_STORED.Freeze(); } @@ -159,7 +159,7 @@ namespace Lucene.Net.Documents public SingleField(string name, float value, Store stored) : base(name, stored == Store.YES ? TYPE_STORED : TYPE_NOT_STORED) { - m_fieldsData = new Single(value); + FieldsData = new Single(value); } /// <summary> @@ -174,11 +174,11 @@ namespace Lucene.Net.Documents public SingleField(string name, float value, FieldType type) : base(name, type) { - if (type.NumericType != NumericType.SINGLE) + if (type.NumericType != Documents.NumericType.SINGLE) { throw new System.ArgumentException("type.NumericType must be NumericType.SINGLE but got " + type.NumericType); } - m_fieldsData = new Single(value); + FieldsData = new Single(value); } } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/29cbc138/src/Lucene.Net/Document/IntField.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Document/IntField.cs b/src/Lucene.Net/Document/IntField.cs index 2921900..485e6e9 100644 --- a/src/Lucene.Net/Document/IntField.cs +++ b/src/Lucene.Net/Document/IntField.cs @@ -128,14 +128,14 @@ namespace Lucene.Net.Documents TYPE_NOT_STORED.IsTokenized = true; TYPE_NOT_STORED.OmitNorms = true; TYPE_NOT_STORED.IndexOptions = IndexOptions.DOCS_ONLY; - TYPE_NOT_STORED.NumericType = NumericType.INT32; + TYPE_NOT_STORED.NumericType = Documents.NumericType.INT32; TYPE_NOT_STORED.Freeze(); TYPE_STORED.IsIndexed = true; TYPE_STORED.IsTokenized = true; TYPE_STORED.OmitNorms = true; TYPE_STORED.IndexOptions = IndexOptions.DOCS_ONLY; - TYPE_STORED.NumericType = NumericType.INT32; + TYPE_STORED.NumericType = Documents.NumericType.INT32; TYPE_STORED.IsStored = true; TYPE_STORED.Freeze(); } @@ -158,7 +158,7 @@ namespace Lucene.Net.Documents public Int32Field(string name, int value, Store stored) : base(name, stored == Store.YES ? TYPE_STORED : TYPE_NOT_STORED) { - m_fieldsData = new Int32(value); + FieldsData = new Int32(value); } /// <summary> @@ -175,11 +175,11 @@ namespace Lucene.Net.Documents public Int32Field(string name, int value, FieldType type) : base(name, type) { - if (type.NumericType != NumericType.INT32) + if (type.NumericType != Documents.NumericType.INT32) { throw new System.ArgumentException("type.NumericType must be NumericType.INT32 but got " + type.NumericType); } - m_fieldsData = new Int32(value); + FieldsData = new Int32(value); } } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/29cbc138/src/Lucene.Net/Document/LongField.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Document/LongField.cs b/src/Lucene.Net/Document/LongField.cs index 9249486..a93f018 100644 --- a/src/Lucene.Net/Document/LongField.cs +++ b/src/Lucene.Net/Document/LongField.cs @@ -139,14 +139,14 @@ namespace Lucene.Net.Documents TYPE_NOT_STORED.IsTokenized = true; TYPE_NOT_STORED.OmitNorms = true; TYPE_NOT_STORED.IndexOptions = IndexOptions.DOCS_ONLY; - TYPE_NOT_STORED.NumericType = NumericType.INT64; + TYPE_NOT_STORED.NumericType = Documents.NumericType.INT64; TYPE_NOT_STORED.Freeze(); TYPE_STORED.IsIndexed = true; TYPE_STORED.IsTokenized = true; TYPE_STORED.OmitNorms = true; TYPE_STORED.IndexOptions = IndexOptions.DOCS_ONLY; - TYPE_STORED.NumericType = NumericType.INT64; + TYPE_STORED.NumericType = Documents.NumericType.INT64; TYPE_STORED.IsStored = true; TYPE_STORED.Freeze(); } @@ -169,7 +169,7 @@ namespace Lucene.Net.Documents public Int64Field(string name, long value, Store stored) : base(name, stored == Store.YES ? TYPE_STORED : TYPE_NOT_STORED) { - m_fieldsData = new Int64(value); + FieldsData = new Int64(value); } /// <summary> @@ -185,11 +185,11 @@ namespace Lucene.Net.Documents public Int64Field(string name, long value, FieldType type) : base(name, type) { - if (type.NumericType != NumericType.INT64) + if (type.NumericType != Documents.NumericType.INT64) { throw new System.ArgumentException("type.NumericType must be NumericType.INT64 but got " + type.NumericType); } - m_fieldsData = new Int64(value); + FieldsData = new Int64(value); } } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/29cbc138/src/Lucene.Net/Document/NumericDocValuesField.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Document/NumericDocValuesField.cs b/src/Lucene.Net/Document/NumericDocValuesField.cs index 7b6eac2..1c4bcb1 100644 --- a/src/Lucene.Net/Document/NumericDocValuesField.cs +++ b/src/Lucene.Net/Document/NumericDocValuesField.cs @@ -55,7 +55,7 @@ namespace Lucene.Net.Documents public NumericDocValuesField(string name, long value) : base(name, TYPE) { - m_fieldsData = new Int64(value); + FieldsData = new Int64(value); } } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/29cbc138/src/Lucene.Net/Document/SortedDocValuesField.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Document/SortedDocValuesField.cs b/src/Lucene.Net/Document/SortedDocValuesField.cs index 645d8f3..b67b53b 100644 --- a/src/Lucene.Net/Document/SortedDocValuesField.cs +++ b/src/Lucene.Net/Document/SortedDocValuesField.cs @@ -59,7 +59,7 @@ namespace Lucene.Net.Documents public SortedDocValuesField(string name, BytesRef bytes) : base(name, TYPE) { - m_fieldsData = bytes; + FieldsData = bytes; } } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/29cbc138/src/Lucene.Net/Document/SortedSetDocValuesField.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Document/SortedSetDocValuesField.cs b/src/Lucene.Net/Document/SortedSetDocValuesField.cs index 328e32d..dfa46b4 100644 --- a/src/Lucene.Net/Document/SortedSetDocValuesField.cs +++ b/src/Lucene.Net/Document/SortedSetDocValuesField.cs @@ -61,7 +61,7 @@ namespace Lucene.Net.Documents public SortedSetDocValuesField(string name, BytesRef bytes) : base(name, TYPE) { - m_fieldsData = bytes; + FieldsData = bytes; } } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/29cbc138/src/Lucene.Net/Document/StoredField.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Document/StoredField.cs b/src/Lucene.Net/Document/StoredField.cs index dd3d8b2..f1609ab 100644 --- a/src/Lucene.Net/Document/StoredField.cs +++ b/src/Lucene.Net/Document/StoredField.cs @@ -101,7 +101,7 @@ namespace Lucene.Net.Documents public StoredField(string name, int value) : base(name, TYPE) { - m_fieldsData = new Int32(value); + FieldsData = new Int32(value); } /// <summary> @@ -112,7 +112,7 @@ namespace Lucene.Net.Documents public StoredField(string name, float value) : base(name, TYPE) { - m_fieldsData = new Single(value); + FieldsData = new Single(value); } /// <summary> @@ -123,7 +123,7 @@ namespace Lucene.Net.Documents public StoredField(string name, long value) : base(name, TYPE) { - m_fieldsData = new Int64(value); + FieldsData = new Int64(value); } /// <summary> @@ -134,7 +134,7 @@ namespace Lucene.Net.Documents public StoredField(string name, double value) : base(name, TYPE) { - m_fieldsData = new Double(value); + FieldsData = new Double(value); } } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/29cbc138/src/Lucene.Net/Index/DocValuesProcessor.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Index/DocValuesProcessor.cs b/src/Lucene.Net/Index/DocValuesProcessor.cs index 0c1835b..9d89b24 100644 --- a/src/Lucene.Net/Index/DocValuesProcessor.cs +++ b/src/Lucene.Net/Index/DocValuesProcessor.cs @@ -73,10 +73,9 @@ namespace Lucene.Net.Index } else if (dvType == DocValuesType.NUMERIC) { - Type numericType = field.GetNumericType(); - if (!(typeof(long).Equals(numericType))) + if (field.NumericType != NumericFieldType.INT64) { - throw new System.ArgumentException("illegal type " + numericType + ": DocValues types must be " + typeof(long)); + throw new System.ArgumentException("illegal type " + field.NumericType + ": DocValues types must be " + NumericFieldType.INT64); } AddNumericField(fieldInfo, docID, field.GetInt64ValueOrDefault()); } http://git-wip-us.apache.org/repos/asf/lucenenet/blob/29cbc138/src/Lucene.Net/Index/IndexableField.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Index/IndexableField.cs b/src/Lucene.Net/Index/IndexableField.cs index 7f913ca..18fc0ee 100644 --- a/src/Lucene.Net/Index/IndexableField.cs +++ b/src/Lucene.Net/Index/IndexableField.cs @@ -1,3 +1,4 @@ +using Lucene.Net.Documents; using System; using System.IO; @@ -120,19 +121,28 @@ namespace Lucene.Net.Index /// <summary> /// Non-null if this field has a numeric value. </summary> - [Obsolete("In .NET, use of this method will cause boxing/unboxing. Instead, call GetNumericType() to check the underlying type and call the appropriate GetXXXValue() method to retrieve the value.")] - object GetNumericValue(); + [Obsolete("In .NET, use of this method will cause boxing/unboxing. Instead, use the NumericType property to check the underlying type and call the appropriate GetXXXValue() method to retrieve the value.")] + object GetNumericValue(); /// <summary> - /// Gets the <see cref="Type"/> of the underlying value, or <c>null</c> if the value is not set or non-numeric. + /// Gets the <see cref="NumericFieldType"/> of the underlying value, or <see cref="NumericFieldType.NONE"/> if the value is not set or non-numeric. /// <para/> - /// LUCENENET specific. In Java, the numeric type was determined by checking the type of + /// Expert: The difference between this property and <see cref="FieldType.NumericType"/> is + /// this is represents the current state of the field (whether being written or read) and the + /// <see cref="FieldType"/> property represents instructions on how the field will be written, + /// but does not re-populate when reading back from an index (it is write-only). + /// <para/> + /// In Java, the numeric type was determined by checking the type of /// <see cref="GetNumericValue()"/>. However, since there are no reference number /// types in .NET, using <see cref="GetNumericValue()"/> so will cause boxing/unboxing. It is - /// therefore recommended to call this method to check the underlying type and the corresponding + /// therefore recommended to use this property to check the underlying type and the corresponding /// <c>Get*Value()</c> method to retrieve the value. + /// <para/> + /// NOTE: Since Lucene codecs do not support <see cref="NumericFieldType.BYTE"/> or <see cref="NumericFieldType.INT16"/>, + /// fields created with these types will always be <see cref="NumericFieldType.INT32"/> when read back from the index. /// </summary> - Type GetNumericType(); + // LUCENENET specific + NumericFieldType NumericType { get; } /// <summary> /// Returns the field value as <see cref="byte"/> or <c>null</c> if the type
