public void Reads_unicode(string expected) { using (var lazyString = _ctx.GetLazyString(expected)) { var stringResult = LazyStringReader.GetStringFor(lazyString); var readerResult = _sut.GetTextReaderFor(lazyString); Assert.Equal(expected, stringResult); Assert.Equal(expected, readerResult.ReadToEnd()); } }
public void Reads_very_long_text(int length) { var expected = new string('a', length); using (var lazyString = _ctx.GetLazyString(expected)) { var stringResult = LazyStringReader.GetStringFor(lazyString); var readerResult = _sut.GetTextReaderFor(lazyString); Assert.Equal(expected, stringResult); Assert.Equal(expected, readerResult.ReadToEnd()); } }
public void Can_reuse_reader_multiple_times() { var r = new Random(); for (int i = 0; i < 10; i++) { var bytes = RandomString(2000); var expected = Encoding.UTF8.GetString(bytes); var lazyString = _ctx.GetLazyString(expected); var stringResult = LazyStringReader.GetStringFor(lazyString); var readerResult = _sut.GetTextReaderFor(lazyString); Assert.Equal(expected, stringResult); Assert.Equal(expected, readerResult.ReadToEnd()); } }
public void Can_reuse_reader_multiple_times() { var r = new Random(); for (int i = 0; i < 10; i++) { var bytes = new byte[r.Next(1, 2000)]; r.NextBytes(bytes); var expected = Encoding.UTF8.GetString(bytes); var lazyString = _ctx.GetLazyString(expected); var stringResult = LazyStringReader.GetStringFor(lazyString); var readerResult = _sut.GetTextReaderFor(lazyString); Assert.Equal(expected, stringResult); Assert.Equal(expected, readerResult.ReadToEnd()); _ctx.ReturnMemory(lazyString.AllocatedMemoryData); } }
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; }
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);