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;
        }
Пример #2
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);