/// <summary> Returns an array of bytes for the first (or only) field that has the name /// specified as the method parameter. This method will return <code>null</code> /// if no binary fields with the specified name are available. /// There may be non-binary fields with the same name. /// /// </summary> /// <param name="name">the name of the field. /// </param> /// <returns> a <code>byte[]</code> containing the binary field value or <code>null</code> /// </returns> public byte[] GetBinaryValue(System.String name) { for (int i = 0; i < fields.Count; i++) { Fieldable field = (Fieldable)fields[i]; if (field.Name().Equals(name) && (field.IsBinary())) { return(field.BinaryValue()); } } return(null); }
/// <summary> Returns an array of byte arrays for of the fields that have the name specified /// as the method parameter. This method will return <code>null</code> if no /// binary fields with the specified name are available. /// /// </summary> /// <param name="name">the name of the field /// </param> /// <returns> a <code>byte[][]</code> of binary field values or <code>null</code> /// </returns> public byte[][] GetBinaryValues(System.String name) { System.Collections.IList result = new System.Collections.ArrayList(); for (int i = 0; i < fields.Count; i++) { Fieldable field = (Fieldable)fields[i]; if (field.Name().Equals(name) && (field.IsBinary())) { byte[] byteArray = field.BinaryValue(); byte[] resultByteArray = new byte[byteArray.Length]; for (int index = 0; index < byteArray.Length; index++) { resultByteArray[index] = (byte)byteArray[index]; } result.Add(resultByteArray); } } if (result.Count == 0) { return(null); } System.Collections.ICollection c = result; System.Object[] objects = new byte[result.Count][]; System.Type type = objects.GetType().GetElementType(); System.Object[] objs = (System.Object[])Array.CreateInstance(type, c.Count); System.Collections.IEnumerator e = c.GetEnumerator(); int ii = 0; while (e.MoveNext()) { objs[ii++] = e.Current; } // If objects is smaller than c then do not return the new array in the parameter if (objects.Length >= c.Count) { objs.CopyTo(objects, 0); } return((byte[][])objs); }
public virtual void TestLoadSize() { FieldsReader reader = new FieldsReader(dir, TEST_SEGMENT_NAME, fieldInfos); Document doc; doc = reader.Doc(0, new AnonymousClassFieldSelector(this)); Fieldable f1 = doc.GetFieldable(DocHelper.TEXT_FIELD_1_KEY); Fieldable f3 = doc.GetFieldable(DocHelper.TEXT_FIELD_3_KEY); Fieldable fb = doc.GetFieldable(DocHelper.LAZY_FIELD_BINARY_KEY); Assert.IsTrue(f1.IsBinary()); Assert.IsTrue(!f3.IsBinary()); Assert.IsTrue(fb.IsBinary()); AssertSizeEquals(2 * DocHelper.FIELD_1_TEXT.Length, f1.BinaryValue()); Assert.AreEqual(DocHelper.FIELD_3_TEXT, f3.StringValue()); AssertSizeEquals(DocHelper.LAZY_FIELD_BINARY_BYTES.Length, fb.BinaryValue()); reader.Close(); }
/// <summary> Returns an array of byte arrays for of the fields that have the name specified /// as the method parameter. This method returns an empty /// array when there are no matching fields. It never /// returns null. /// /// </summary> /// <param name="name">the name of the field /// </param> /// <returns> a <code>byte[][]</code> of binary field values /// </returns> public byte[][] GetBinaryValues(System.String name) { List <byte[]> result = new List <byte[]>(); for (int i = 0; i < fields.Count; i++) { Fieldable field = fields[i]; if (field.Name().Equals(name) && (field.IsBinary())) { result.Add(field.BinaryValue()); } } if (result.Count == 0) { return(NO_BYTES); } return(result.ToArray()); }
public virtual void TestLazyFields() { Assert.IsTrue(dir != null); Assert.IsTrue(fieldInfos != null); FieldsReader reader = new FieldsReader(dir, TEST_SEGMENT_NAME, fieldInfos); Assert.IsTrue(reader != null); Assert.IsTrue(reader.Size() == 1); System.Collections.Hashtable loadFieldNames = new System.Collections.Hashtable(); SupportClass.CollectionsHelper.AddIfNotContains(loadFieldNames, DocHelper.TEXT_FIELD_1_KEY); SupportClass.CollectionsHelper.AddIfNotContains(loadFieldNames, DocHelper.TEXT_FIELD_UTF1_KEY); System.Collections.Hashtable lazyFieldNames = new System.Collections.Hashtable(); //new String[]{DocHelper.LARGE_LAZY_FIELD_KEY, DocHelper.LAZY_FIELD_KEY, DocHelper.LAZY_FIELD_BINARY_KEY}; SupportClass.CollectionsHelper.AddIfNotContains(lazyFieldNames, DocHelper.LARGE_LAZY_FIELD_KEY); SupportClass.CollectionsHelper.AddIfNotContains(lazyFieldNames, DocHelper.LAZY_FIELD_KEY); SupportClass.CollectionsHelper.AddIfNotContains(lazyFieldNames, DocHelper.LAZY_FIELD_BINARY_KEY); SupportClass.CollectionsHelper.AddIfNotContains(lazyFieldNames, DocHelper.TEXT_FIELD_UTF2_KEY); SupportClass.CollectionsHelper.AddIfNotContains(lazyFieldNames, DocHelper.COMPRESSED_TEXT_FIELD_2_KEY); SetBasedFieldSelector fieldSelector = new SetBasedFieldSelector(loadFieldNames, lazyFieldNames); Document doc = reader.Doc(0, fieldSelector); Assert.IsTrue(doc != null, "doc is null and it shouldn't be"); Fieldable field = doc.GetFieldable(DocHelper.LAZY_FIELD_KEY); Assert.IsTrue(field != null, "field is null and it shouldn't be"); Assert.IsTrue(field.IsLazy(), "field is not lazy and it should be"); System.String value_Renamed = field.StringValue(); Assert.IsTrue(value_Renamed != null, "value is null and it shouldn't be"); Assert.IsTrue(value_Renamed.Equals(DocHelper.LAZY_FIELD_TEXT) == true, value_Renamed + " is not equal to " + DocHelper.LAZY_FIELD_TEXT); field = doc.GetFieldable(DocHelper.COMPRESSED_TEXT_FIELD_2_KEY); Assert.IsTrue(field != null, "field is null and it shouldn't be"); Assert.IsTrue(field.IsLazy(), "field is not lazy and it should be"); Assert.IsTrue(field.BinaryValue() == null, "binaryValue isn't null for lazy string field"); value_Renamed = field.StringValue(); Assert.IsTrue(value_Renamed != null, "value is null and it shouldn't be"); Assert.IsTrue(value_Renamed.Equals(DocHelper.FIELD_2_COMPRESSED_TEXT) == true, value_Renamed + " is not equal to " + DocHelper.FIELD_2_COMPRESSED_TEXT); field = doc.GetFieldable(DocHelper.TEXT_FIELD_1_KEY); Assert.IsTrue(field != null, "field is null and it shouldn't be"); Assert.IsTrue(field.IsLazy() == false, "Field is lazy and it should not be"); field = doc.GetFieldable(DocHelper.TEXT_FIELD_UTF1_KEY); Assert.IsTrue(field != null, "field is null and it shouldn't be"); Assert.IsTrue(field.IsLazy() == false, "Field is lazy and it should not be"); Assert.IsTrue(field.StringValue().Equals(DocHelper.FIELD_UTF1_TEXT) == true, field.StringValue() + " is not equal to " + DocHelper.FIELD_UTF1_TEXT); field = doc.GetFieldable(DocHelper.TEXT_FIELD_UTF2_KEY); Assert.IsTrue(field != null, "field is null and it shouldn't be"); Assert.IsTrue(field.IsLazy() == true, "Field is lazy and it should not be"); Assert.IsTrue(field.StringValue().Equals(DocHelper.FIELD_UTF2_TEXT) == true, field.StringValue() + " is not equal to " + DocHelper.FIELD_UTF2_TEXT); field = doc.GetFieldable(DocHelper.LAZY_FIELD_BINARY_KEY); Assert.IsTrue(field != null, "field is null and it shouldn't be"); Assert.IsTrue(field.StringValue() == null, "stringValue isn't null for lazy binary field"); byte[] bytes = field.BinaryValue(); Assert.IsTrue(bytes != null, "bytes is null and it shouldn't be"); Assert.IsTrue(DocHelper.LAZY_FIELD_BINARY_BYTES.Length == bytes.Length, ""); for (int i = 0; i < bytes.Length; i++) { Assert.IsTrue(bytes[i] == DocHelper.LAZY_FIELD_BINARY_BYTES[i], "byte[" + i + "] is mismatched"); } }