public void Invalid_hash_calculation_on_null() { using (var bufferPool = new UnmanagedBuffersPoolWithLowMemoryHandling("RavenDB_9535")) using (var bsc = new ByteStringContext(SharedMultipleUseFlag.None)) { var sut = new ReduceKeyProcessor(1, bufferPool); try { sut.Reset(); sut.Process(bsc, null); Assert.Equal((ulong)0, sut.Hash); sut.Reset(); sut.Process(bsc, 1); Assert.NotEqual((ulong)0, sut.Hash); sut.Reset(); sut.Process(bsc, null); Assert.Equal((ulong)0, sut.Hash); } finally { sut.ReleaseBuffer(); } } }
public bool MoveNext() { if (_enumerator.MoveNext() == false) { return(false); } var output = _enumerator.Current; _parent._indexContext.CachedProperties.NewDocument(); using (_createBlittableResult.Start()) using (var writer = new BlittableJsonWriter(_parent._indexContext)) { IPropertyAccessor accessor; if (_parent._isMultiMap == false) { accessor = _parent._propertyAccessor ?? (_parent._propertyAccessor = PropertyAccessor.CreateMapReduceOutputAccessor(output.GetType(), output, _groupByFields)); } else { accessor = TypeConverter.GetPropertyAccessorForMapReduceOutput(output, _groupByFields); } _reduceKeyProcessor.Reset(); writer.WriteStartObject(); foreach (var property in accessor.GetPropertiesInOrder(output)) { var value = property.Value; var blittableValue = TypeConverter.ToBlittableSupportedType(value, context: _parent._indexContext); writer.WritePropertyName(property.Key); WriteValue(writer, blittableValue); if (property.IsGroupByField) { var valueForProcessor = property.GroupByField.GetValue(value, blittableValue); _reduceKeyProcessor.Process(_parent._indexContext.Allocator, valueForProcessor); } } writer.WriteEndObject(); if (_reduceKeyProcessor.ProcessedFields != _groupByFields.Count) { ThrowMissingGroupByFieldsInMapOutput(output, _groupByFields, _parent._compiledIndex); } var reduceHashKey = _reduceKeyProcessor.Hash; writer.FinalizeDocument(); Current.Data = writer.CreateReader(); Current.ReduceKeyHash = reduceHashKey; } return(true);
public bool MoveNext() { if (_enumerator.MoveNext() == false) { return(false); } var output = _enumerator.Current; using (_createBlittableResult.Start()) { PropertyAccessor accessor; if (_parent._isMultiMap == false) { accessor = _parent._propertyAccessor ?? (_parent._propertyAccessor = PropertyAccessor.CreateMapReduceOutputAccessor(output.GetType(), output, _groupByFields)); } else { accessor = TypeConverter.GetPropertyAccessorForMapReduceOutput(output, _groupByFields); } var mapResult = new DynamicJsonValue(); _reduceKeyProcessor.Reset(); var propertiesInOrder = accessor.PropertiesInOrder; int properties = propertiesInOrder.Count; for (int i = 0; i < properties; i++) { var field = propertiesInOrder[i]; var value = field.Value.GetValue(output); var blittableValue = TypeConverter.ToBlittableSupportedType(value); mapResult[field.Key] = blittableValue; if (field.Value.IsGroupByField) { var valueForProcessor = field.Value.GroupByField.GetValue(value, blittableValue); _reduceKeyProcessor.Process(_parent._indexContext.Allocator, valueForProcessor); } } if (_reduceKeyProcessor.ProcessedFields != _groupByFields.Count) { ThrowMissingGroupByFieldsInMapOutput(output, _groupByFields, _parent._compiledIndex); } var reduceHashKey = _reduceKeyProcessor.Hash; Current.Data = _parent._indexContext.ReadObject(mapResult, "map-result"); Current.ReduceKeyHash = reduceHashKey; } return(true); }
public void Can_handle_values_of_different_types() { using (var bufferPool = new UnmanagedBuffersPoolWithLowMemoryHandling("ReduceKeyProcessorTests")) using (var context = JsonOperationContext.ShortTermSingleUse()) using (var bsc = new ByteStringContext(SharedMultipleUseFlag.None)) { var sut = new ReduceKeyProcessor(9, bufferPool); sut.Reset(); sut.Process(bsc, 1); sut.Process(bsc, (long)1); sut.Process(bsc, 1.0); sut.Process(bsc, (decimal)1.0); sut.Process(bsc, "abc"); using (var lazyStringValue = context.GetLazyString("abc")) { sut.Process(bsc, lazyStringValue); } sut.Process(bsc, context.ReadObject(new DynamicJsonValue() { ["Name"] = "Arek", ["Age"] = null }, "foo")); sut.Process(bsc, new DynamicJsonArray() { 1, 2, null, 3 }); sut.Process(bsc, SystemTime.UtcNow); Assert.NotEqual((ulong)0, sut.Hash); sut.ReleaseBuffer(); } }
public bool MoveNext() { if (_enumerator.MoveNext() == false) return false; var output = _enumerator.Current; using (_createBlittableResult.Start()) { IPropertyAccessor accessor; if (_parent._isMultiMap == false) accessor = _parent._propertyAccessor ?? (_parent._propertyAccessor = PropertyAccessor.CreateMapReduceOutputAccessor(output.GetType(), output, _groupByFields)); else accessor = TypeConverter.GetPropertyAccessorForMapReduceOutput(output, _groupByFields); var mapResult = new DynamicJsonValue(); _reduceKeyProcessor.Reset(); foreach (var property in accessor.GetPropertiesInOrder(output)) { var value = property.Value; var blittableValue = TypeConverter.ToBlittableSupportedType(value, context: _parent._indexContext); mapResult[property.Key] = blittableValue; if (property.IsGroupByField) { var valueForProcessor = property.GroupByField.GetValue(value, blittableValue); _reduceKeyProcessor.Process(_parent._indexContext.Allocator, valueForProcessor); } } if (_reduceKeyProcessor.ProcessedFields != _groupByFields.Count) ThrowMissingGroupByFieldsInMapOutput(output, _groupByFields, _parent._compiledIndex); var reduceHashKey = _reduceKeyProcessor.Hash; Current.Data = _parent._indexContext.ReadObject(mapResult, "map-result"); Current.ReduceKeyHash = reduceHashKey; } return true; }
public override int HandleMap(IndexItem indexItem, IEnumerable mapResults, IndexWriteOperation writer, TransactionOperationContext indexContext, IndexingStatsScope stats) { EnsureValidStats(stats); var document = ((Document[])mapResults)[0]; Debug.Assert(indexItem.LowerId == document.LowerId); using (_stats.BlittableJsonAggregation.Start()) { DynamicJsonValue singleResult = null; var groupByFieldsCount = Definition.OrderedGroupByFields.Length; for (var i = 0; i < groupByFieldsCount; i++) { var groupByField = Definition.OrderedGroupByFields[i]; BlittableJsonTraverserHelper.TryRead(BlittableJsonTraverser.Default, document, groupByField.Name, out object result); if (_isFanout == false) { if (singleResult == null) { singleResult = new DynamicJsonValue(); } singleResult[groupByField.Name] = result; _reduceKeyProcessor.Process(indexContext.Allocator, result); } else { if (result is IEnumerable array && groupByField.GroupByArrayBehavior == GroupByArrayBehavior.ByIndividualValues) { // fanout foreach (var item in array) { _output.AddGroupByValue(groupByField.Name, item); } }
public bool MoveNext() { if (_enumerator.MoveNext() == false) { return(false); } var document = _enumerator.Current; using (_createBlittableResult.Start()) { var accessor = _parent._propertyAccessor ?? (_parent._propertyAccessor = PropertyAccessor.Create(document.GetType())); var mapResult = new DynamicJsonValue(); _reduceKeyProcessor.Reset(); foreach (var field in accessor.PropertiesInOrder) { var value = field.Value.GetValue(document); var blittableValue = TypeConverter.ToBlittableSupportedType(value); mapResult[field.Key] = blittableValue; if (_groupByFields.Contains(field.Key)) { _reduceKeyProcessor.Process(_parent._indexContext.Allocator, blittableValue); } } var reduceHashKey = _reduceKeyProcessor.Hash; Current.Data = _parent._indexContext.ReadObject(mapResult, "map-result"); Current.ReduceKeyHash = reduceHashKey; } return(true); }
public unsafe bool Equals(BlittableJsonReaderObject x, BlittableJsonReaderObject y) { var xCalCulated = ReferenceEquals(x, _lastUsedBucket); //Only y is calculated, x is the value in the bucket var yCalculated = ReferenceEquals(y, _lastUsedBlittable); if (xCalCulated == false) { _xKey.Reset(); } if (yCalculated == false) { _yKey.Reset(); } foreach (var field in _parent._groupByFields) { bool xHasField = false; object xVal = null; if (xCalCulated == false) { xHasField = x.TryGet(field, out xVal); } object yVal = null; if (yCalculated == false && xCalCulated == false) { var yHasField = y.TryGet(field, out yVal); if (xHasField != yHasField) { return(false); } } if (xCalCulated == false) { _xKey.Process(_allocator, xVal); } if (yCalculated == false) { _yKey.Process(_allocator, yVal); } } var xIsNotAllNulls = _xKey.IsBufferSet; var yIsNotAllNulls = _yKey.IsBufferSet; // null == null if (xIsNotAllNulls == false && yIsNotAllNulls == false) { return(true); } // x == null and y != null or y != null and y == null if (xIsNotAllNulls == false || yIsNotAllNulls == false) { return(false); } //At this point both buffer should be populated var xBuffer = _xKey.GetBuffer(); var yBuffer = _yKey.GetBuffer(); _lastUsedBucket = x; if (xBuffer.Size != yBuffer.Size) { return(false); } return(Memory.Compare(xBuffer.Address, yBuffer.Address, xBuffer.Size) == 0); }