Пример #1
0
        private Field CreateBinaryFieldWithCaching(string name, byte[] value, Field.Store store, Field.Index index, Field.TermVector termVector)
        {
            if (value.Length > 1024)
            {
                throw new ArgumentException("Binary values must be smaller than 1Kb");
            }

            var   cacheKey = new FieldCacheKey(name, null, store, termVector, multipleItemsSameFieldCount.ToArray());
            Field field;
            var   stringWriter = new StringWriter();

            JsonExtensions.CreateDefaultJsonSerializer().Serialize(stringWriter, value);
            var sb = stringWriter.GetStringBuilder();

            sb.Remove(0, 1);             // remove prefix "
            sb.Remove(sb.Length - 1, 1); // remove postfix "
            var val = sb.ToString();

            if (fieldsCache.TryGetValue(cacheKey, out field) == false)
            {
                fieldsCache[cacheKey] = field = new Field(name, val, store, index, termVector);
            }
            field.SetValue(val);
            field.Boost     = 1;
            field.OmitNorms = true;
            return(field);
        }
 protected bool Equals(FieldCacheKey other)
 {
     return(string.Equals(name, other.name) &&
            Equals(index, other.index) &&
            Equals(store, other.store) &&
            Equals(termVector, other.termVector) &&
            multipleItemsSameField.SequenceEqual(other.multipleItemsSameField));
 }
        private IEnumerable <AbstractField> CreateNumericFieldWithCaching(string name, object value,
                                                                          Field.Store defaultStorage, Field.TermVector termVector)
        {
            var          fieldName = name + "_Range";
            var          storage   = indexDefinition.GetStorage(name, defaultStorage);
            var          cacheKey  = new FieldCacheKey(name, null, storage, termVector, multipleItemsSameFieldCount.ToArray());
            NumericField numericField;

            if (numericFieldsCache.TryGetValue(cacheKey, out numericField) == false)
            {
                numericFieldsCache[cacheKey] = numericField = new NumericField(fieldName, storage, true);
            }

            if (value is TimeSpan)
            {
                yield return(numericField.SetLongValue(((TimeSpan)value).Ticks));
            }
            else if (value is int)
            {
                if (indexDefinition.GetSortOption(name) == SortOptions.Long)
                {
                    yield return(numericField.SetLongValue((int)value));
                }
                else
                {
                    yield return(numericField.SetIntValue((int)value));
                }
            }
            else if (value is long)
            {
                yield return(numericField
                             .SetLongValue((long)value));
            }
            else if (value is decimal)
            {
                yield return(numericField
                             .SetDoubleValue((double)(decimal)value));
            }
            else if (value is float)
            {
                if (indexDefinition.GetSortOption(name) == SortOptions.Double)
                {
                    yield return(numericField.SetDoubleValue((float)value));
                }
                else
                {
                    yield return(numericField.SetFloatValue((float)value));
                }
            }
            else if (value is double)
            {
                yield return(numericField
                             .SetDoubleValue((double)value));
            }
        }
Пример #4
0
        public Field CreateFieldWithCaching(string name, string value, Field.Store store, Field.Index index)
        {
            var   cacheKey = new FieldCacheKey(name, index, store, multipleItemsSameFieldCount.ToArray());
            Field field;

            if (fieldsCache.TryGetValue(cacheKey, out field) == false)
            {
                fieldsCache[cacheKey] = field = new Field(name, value, store, index);
            }
            field.SetValue(value);
            field.Boost     = 1;
            field.OmitNorms = true;
            return(field);
        }
Пример #5
0
        private Field CreateBinaryFieldWithCaching(string name, byte[] value, Field.Store store)
        {
            var   cacheKey = new FieldCacheKey(name, null, store, multipleItemsSameFieldCount.ToArray());
            Field field;

            if (fieldsCache.TryGetValue(cacheKey, out field) == false)
            {
                fieldsCache[cacheKey] = field = new Field(name, value, store);
            }
            field.SetValue(value);
            field.SetBoost(1);
            field.SetOmitNorms(true);
            return(field);
        }
        private IEnumerable <AbstractField> GetOrCreateNumericField(IndexField field, object value, Field.Store storage, Field.TermVector termVector = Field.TermVector.NO)
        {
            var fieldName = field.Name + Constants.Indexing.Fields.RangeFieldSuffix;

            var cacheKey = new FieldCacheKey(field.Name, null, storage, termVector,
                                             _multipleItemsSameFieldCount.ToArray());

            NumericField numericField;
            CachedFieldItem <NumericField> cached;

            if (_numericFieldsCache.TryGetValue(cacheKey, out cached) == false)
            {
                _numericFieldsCache[cacheKey] = cached = new CachedFieldItem <NumericField>
                {
                    Field = numericField = new NumericField(CreateFieldName(fieldName), storage, true)
                };
            }
            else
            {
                numericField = cached.Field;
            }

            double doubleValue;
            long   longValue;

            switch (BlittableNumber.Parse(value, out doubleValue, out longValue))
            {
            case NumberParseResult.Double:
                if (field.SortOption == SortOptions.NumericLong)
                {
                    yield return(numericField.SetLongValue((long)doubleValue));
                }
                else
                {
                    yield return(numericField.SetDoubleValue(doubleValue));
                }
                break;

            case NumberParseResult.Long:
                if (field.SortOption == SortOptions.NumericDouble)
                {
                    yield return(numericField.SetDoubleValue(longValue));
                }
                else
                {
                    yield return(numericField.SetLongValue(longValue));
                }
                break;
            }
        }
        protected Field GetOrCreateField(string name, string value, LazyStringValue lazyValue, BlittableJsonReaderObject blittableValue, Field.Store store, Field.Index index, Field.TermVector termVector)
        {
            int cacheKey = FieldCacheKey.GetHashCode(name, index, store, termVector, _multipleItemsSameFieldCount);

            Field field;
            if (_fieldsCache.TryGetValue(cacheKey, out CachedFieldItem<Field> cached) == false ||
                !cached.Key.IsSame(name, index, store, termVector, _multipleItemsSameFieldCount))
            {
                LazyStringReader stringReader = null;
                BlittableObjectReader blittableReader = null;

                if ((lazyValue != null || blittableValue != null) && store.IsStored() == false && index.IsIndexed() && index.IsAnalyzed())
                {
                    TextReader reader;
                    if (lazyValue != null)
                    {
                        stringReader = new LazyStringReader();
                        reader = stringReader.GetTextReaderFor(lazyValue);
                    }
                    else
                    {
                        blittableReader = Scope.GetBlittableReader();
                        reader = blittableReader.GetTextReaderFor(blittableValue);
                    }

                    field = new Field(name, reader, termVector);
                }
                else
                {
                    if (value == null && lazyValue == null)
                        blittableReader = Scope.GetBlittableReader();

                    field = new Field(name,
                        value ?? LazyStringReader.GetStringFor(lazyValue) ?? blittableReader.GetStringFor(blittableValue),
                        store, index, termVector);
                }

                field.Boost = 1;
                field.OmitNorms = true;

                _fieldsCache[cacheKey] = new CachedFieldItem<Field>
                {
                    Key = new FieldCacheKey(name, index, store, termVector, _multipleItemsSameFieldCount.ToArray()),
                    Field = field,
                    LazyStringReader = stringReader
                };
            }
            else
            {
                BlittableObjectReader blittableReader = null;

                field = cached.Field;
                if (lazyValue != null && cached.LazyStringReader == null)
                    cached.LazyStringReader = new LazyStringReader();
                if (blittableValue != null)
                    blittableReader = Scope.GetBlittableReader();

                if ((lazyValue != null || blittableValue != null) && store.IsStored() == false && index.IsIndexed() && index.IsAnalyzed())
                {
                    field.SetValue(lazyValue != null
                        ? cached.LazyStringReader.GetTextReaderFor(lazyValue)
                        : blittableReader.GetTextReaderFor(blittableValue));
                }
                else
                {
                    field.SetValue(value ?? LazyStringReader.GetStringFor(lazyValue) ?? blittableReader.GetStringFor(blittableValue));
                }
            }

            return field;
        }
		private Field CreateFieldWithCaching(string name, string value, Field.Store store, Field.Index index)
		{
			var cacheKey = new FieldCacheKey(name, index, store, multipleItemsSameFieldCount.ToArray());
			Field field;
			if (fieldsCache.TryGetValue(cacheKey, out field) == false)
			{
				fieldsCache[cacheKey] = field = new Field(name, value, store, index);
			}
			field.SetValue(value);
			field.Boost = 1;
			field.OmitNorms = true;
			return field;
		}
			protected bool Equals(FieldCacheKey other)
			{
				return string.Equals(name, other.name) && Equals(index, other.index) && Equals(store, other.store) &&
					multipleItemsSameField.SequenceEqual(other.multipleItemsSameField);
			}
		private Field CreateBinaryFieldWithCaching(string name, byte[] value, Field.Store store, Field.Index index)
		{
			if (value.Length > 1024)
				throw new ArgumentException("Binary values must be smaller than 1Kb");

			var cacheKey = new FieldCacheKey(name, null, store, multipleItemsSameFieldCount.ToArray());
			Field field;
			var stringWriter = new StringWriter();
			JsonExtensions.CreateDefaultJsonSerializer().Serialize(stringWriter, value);
			var sb = stringWriter.GetStringBuilder();
			sb.Remove(0, 1); // remove prefix "
			sb.Remove(sb.Length - 1, 1); // remove postfix "
			var val = sb.ToString();

			if (fieldsCache.TryGetValue(cacheKey, out field) == false)
			{
				fieldsCache[cacheKey] = field = new Field(name, val, store, index);
			}
			field.SetValue(val);
			field.Boost = 1;
			field.OmitNorms = true;
			return field;
		}
		private IEnumerable<AbstractField> CreateNumericFieldWithCaching(string name, object value,
			Field.Store defaultStorage)
		{

			var fieldName = name + "_Range";
			var storage = indexDefinition.GetStorage(name, defaultStorage);
			var cacheKey = new FieldCacheKey(name, null, storage, multipleItemsSameFieldCount.ToArray());
			NumericField numericField;
			if (numericFieldsCache.TryGetValue(cacheKey, out numericField) == false)
			{
				numericFieldsCache[cacheKey] = numericField = new NumericField(fieldName, storage, true);
			}

			if (value is int)
			{
				if (indexDefinition.GetSortOption(name) == SortOptions.Long)
					yield return numericField.SetLongValue((int)value);
				else
					yield return numericField.SetIntValue((int)value);
			}
			if (value is long)
			{
				yield return numericField
					.SetLongValue((long)value);
			}
			if (value is decimal)
			{
				yield return numericField
					.SetDoubleValue((double)(decimal)value);
			}
			if (value is float)
			{
				if (indexDefinition.GetSortOption(name) == SortOptions.Double)
					yield return numericField.SetDoubleValue((float)value);
				else
					yield return numericField.SetFloatValue((float)value);
			}
			if (value is double)
			{
				yield return numericField
					.SetDoubleValue((double)value);
			}
		}
Пример #12
0
        private static FieldInfo GetFieldInfoFromPropertyPath(Type host, string path)
        {
            if (s_FieldInfoCache == null)
            {
                s_FieldInfoCache = new Dictionary <FieldCacheKey, FieldInfo>();
            }

            FieldInfo     fieldInfo = null;
            FieldCacheKey key       = new FieldCacheKey(host, path);

            if (!s_FieldInfoCache.TryGetValue(key, out fieldInfo))
            {
                Type     type  = host;
                string[] array = path.Split('.');

                for (int i = 0; i < array.Length; i++)
                {
                    string text = array[i];
                    if (i < array.Length - 1 && text == "Array")
                    {
                        if (array[i + 1].StartsWith("data["))
                        {
                            if (IsArrayOrList(type))
                            {
                                type = GetArrayOrListElementType(type);
                            }

                            i++;
                        }
                        else if (array[i + 1] == "size")
                        {
                            if (IsArrayOrList(type))
                            {
                                type = GetArrayOrListElementType(type);
                            }

                            i++;
                        }
                    }
                    else
                    {
                        FieldInfo fieldInfo2 = null;
                        Type      type2      = type;
                        while (fieldInfo2 == null && type2 != null)
                        {
                            fieldInfo2 = type2.GetField(text, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                            type2      = type2.BaseType;
                        }

                        if (fieldInfo2 == null)
                        {
                            fieldInfo = null;
                            break; // Failed to find anything
                        }

                        fieldInfo = fieldInfo2;
                        type      = fieldInfo.FieldType;
                    }
                }
            }

            s_FieldInfoCache[key] = fieldInfo;

            return(fieldInfo);
        }
		private Field CreateBinaryFieldWithCaching(string name, byte[] value, Field.Store store)
		{
			var cacheKey = new FieldCacheKey(name, null, store, multipleItemsSameFieldCount.ToArray());
			Field field;
			if (fieldsCache.TryGetValue(cacheKey, out field) == false)
			{
				fieldsCache[cacheKey] = field = new Field(name, value, store);
			}
			field.SetValue(value);
			field.SetBoost(1);
			field.SetOmitNorms(true);
			return field;
		}
		private Field CreateFieldWithCaching(string name, string value, Field.Store store, Field.Index index)
		{
		    var termVector = store == Field.Store.YES && (index == Field.Index.ANALYZED || index == Field.Index.ANALYZED_NO_NORMS)
		        ? Field.TermVector.WITH_POSITIONS_OFFSETS // For FastVectorHighlighter
		        : Field.TermVector.NO; 

			var cacheKey = new FieldCacheKey(name, index, store, termVector, multipleItemsSameFieldCount.ToArray());
			Field field;

		    if (fieldsCache.TryGetValue(cacheKey, out field) == false)
		        fieldsCache[cacheKey] = field = new Field(name, value, store, index, termVector);

		    field.SetValue(value);
			field.Boost = 1;
			field.OmitNorms = true;
			return field;
		}
Пример #15
0
        protected Field GetOrCreateField(string name, string value, LazyStringValue lazyValue, Stream streamValue, BlittableJsonReaderObject blittableValue, Field.Store store, Field.Index index, Field.TermVector termVector)
        {
            RuntimeHelpers.EnsureSufficientExecutionStack();

            int cacheKey = FieldCacheKey.CalculateHashCode(name, index, store, termVector, _multipleItemsSameFieldCount);

            Field field;

            if (_fieldsCache.TryGetValue(cacheKey, out CachedFieldItem <Field> cached) == false ||
                !cached.Key.IsSame(name, index, store, termVector, _multipleItemsSameFieldCount))
            {
                LazyStringReader      stringReader    = null;
                BlittableObjectReader blittableReader = null;

                if (streamValue == null)
                {
                    if ((lazyValue != null || blittableValue != null) && store.IsStored() == false && index.IsIndexed() && index.IsAnalyzed())
                    {
                        TextReader reader;
                        if (lazyValue != null)
                        {
                            stringReader = new LazyStringReader();
                            reader       = stringReader.GetTextReaderFor(lazyValue);
                        }
                        else
                        {
                            blittableReader = Scope.GetBlittableReader();
                            reader          = blittableReader.GetTextReaderFor(blittableValue);
                        }

                        field = new Field(name, reader, termVector);
                    }
                    else
                    {
                        if (value == null && lazyValue == null)
                        {
                            blittableReader = Scope.GetBlittableReader();
                        }

                        field = new Field(name,
                                          value ?? LazyStringReader.GetStringFor(lazyValue) ?? blittableReader.GetStringFor(blittableValue),
                                          store, index, termVector);
                    }
                }
                else
                {
                    var streamBuffer = ToArray(Scope, streamValue, out var streamLength);

                    field = new Field(name, streamBuffer, 0, streamLength, store);
                }

                field.Boost     = 1;
                field.OmitNorms = true;

                AddToFieldsCache(cacheKey, _multipleItemsSameFieldCount.Count > 0, cached, new CachedFieldItem <Field>
                {
                    Key              = new FieldCacheKey(name, index, store, termVector, _multipleItemsSameFieldCount.ToArray()),
                    Field            = field,
                    LazyStringReader = stringReader
                });
            }
            else
            {
                BlittableObjectReader blittableReader = null;

                field = cached.Field;

                if (streamValue == null)
                {
                    if (lazyValue != null && cached.LazyStringReader == null)
                    {
                        cached.LazyStringReader = new LazyStringReader();
                    }
                    if (blittableValue != null)
                    {
                        blittableReader = Scope.GetBlittableReader();
                    }

                    if ((lazyValue != null || blittableValue != null) && store.IsStored() == false && index.IsIndexed() && index.IsAnalyzed())
                    {
                        field.SetValue(lazyValue != null
                            ? cached.LazyStringReader.GetTextReaderFor(lazyValue)
                            : blittableReader.GetTextReaderFor(blittableValue));
                    }
                    else
                    {
                        field.SetValue(value ?? LazyStringReader.GetStringFor(lazyValue) ?? blittableReader.GetStringFor(blittableValue));
                    }
                }
                else
                {
                    var streamBuffer = ToArray(Scope, streamValue, out var streamLength);

                    field.SetValue(streamBuffer, 0, streamLength);
                }
            }

            return(field);