wip fixing dispose
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/41ae0148 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/41ae0148 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/41ae0148 Branch: refs/heads/ignite-2324 Commit: 41ae01482bfe7533af5f63803af613dc412ba6ed Parents: 04786dc Author: Pavel Tupitsyn <ptupit...@gridgain.com> Authored: Thu Jan 14 11:42:06 2016 +0300 Committer: Pavel Tupitsyn <ptupit...@gridgain.com> Committed: Thu Jan 14 11:42:06 2016 +0300 ---------------------------------------------------------------------- .../Apache.Ignite.Core/Impl/Binary/Binary.cs | 41 ++++++----- .../Impl/Binary/BinaryObject.cs | 45 ++++++------ .../Impl/Binary/BinaryObjectBuilder.cs | 75 ++++++++++---------- .../Impl/Binary/Marshaller.cs | 19 +++-- .../Impl/Binary/SerializableObjectHolder.cs | 13 +++- 5 files changed, 108 insertions(+), 85 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/41ae0148/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/Binary.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/Binary.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/Binary.cs index e96720e..7163486 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/Binary.cs +++ b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/Binary.cs @@ -48,25 +48,27 @@ namespace Apache.Ignite.Core.Impl.Binary if (obj is IBinaryObject) return (T)obj; - IBinaryStream stream = new BinaryHeapStream(1024); + using (var stream = new BinaryHeapStream(1024)) + { - // Serialize. - BinaryWriter writer = _marsh.StartMarshal(stream); + // Serialize. + BinaryWriter writer = _marsh.StartMarshal(stream); - try - { - writer.Write(obj); - } - finally - { - // Save metadata. - _marsh.FinishMarshal(writer); - } + try + { + writer.Write(obj); + } + finally + { + // Save metadata. + _marsh.FinishMarshal(writer); + } - // Deserialize. - stream.Seek(0, SeekOrigin.Begin); + // Deserialize. + stream.Seek(0, SeekOrigin.Begin); - return _marsh.Unmarshal<T>(stream, BinaryMode.ForceBinary); + return _marsh.Unmarshal<T>(stream, BinaryMode.ForceBinary); + } } /** <inheritDoc /> */ @@ -192,11 +194,12 @@ namespace Apache.Ignite.Core.Impl.Binary var hdr = new BinaryObjectHeader(desc.TypeId, 0, len, 0, len, desc.UserType ? BinaryObjectHeader.Flag.UserType : BinaryObjectHeader.Flag.None); - var stream = new BinaryHeapStream(len); - - BinaryObjectHeader.Write(hdr, stream, 0); + using (var stream = new BinaryHeapStream(len)) + { + BinaryObjectHeader.Write(hdr, stream, 0); - return new BinaryObject(_marsh, stream.InternalArray, 0, hdr); + return new BinaryObject(_marsh, stream.InternalArray, 0, hdr); + } } /// <summary> http://git-wip-us.apache.org/repos/asf/ignite/blob/41ae0148/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryObject.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryObject.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryObject.cs index 90607dd..2f68a0a 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryObject.cs +++ b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryObject.cs @@ -110,11 +110,12 @@ namespace Apache.Ignite.Core.Impl.Binary /// <returns>Field value.</returns> public T GetField<T>(int pos, BinaryObjectBuilder builder) { - IBinaryStream stream = new BinaryHeapStream(_data); - - stream.Seek(pos + _offset, SeekOrigin.Begin); + using (IBinaryStream stream = new BinaryHeapStream(_data)) + { + stream.Seek(pos + _offset, SeekOrigin.Begin); - return _marsh.Unmarshal<T>(stream, BinaryMode.ForceBinary, builder); + return _marsh.Unmarshal<T>(stream, BinaryMode.ForceBinary, builder); + } } /** <inheritdoc /> */ @@ -144,13 +145,15 @@ namespace Apache.Ignite.Core.Impl.Binary { if (_deserialized == null) { - IBinaryStream stream = new BinaryHeapStream(_data); - - stream.Seek(_offset, SeekOrigin.Begin); + T res; + using (IBinaryStream stream = new BinaryHeapStream(_data)) + { + stream.Seek(_offset, SeekOrigin.Begin); - T res = _marsh.Unmarshal<T>(stream, mode); + res = _marsh.Unmarshal<T>(stream, mode); + } - IBinaryTypeDescriptor desc = _marsh.GetDescriptor(true, _header.TypeId); + var desc = _marsh.GetDescriptor(true, _header.TypeId); if (!desc.KeepDeserialized) return res; @@ -202,11 +205,12 @@ namespace Apache.Ignite.Core.Impl.Binary if (_fields != null) return; - var stream = new BinaryHeapStream(_data); - - var hdr = BinaryObjectHeader.Read(stream, _offset); + using (var stream = new BinaryHeapStream(_data)) + { + var hdr = BinaryObjectHeader.Read(stream, _offset); - _fields = hdr.ReadSchemaAsDictionary(stream, _offset) ?? EmptyFields; + _fields = hdr.ReadSchemaAsDictionary(stream, _offset) ?? EmptyFields; + } } /** <inheritdoc /> */ @@ -256,15 +260,16 @@ namespace Apache.Ignite.Core.Impl.Binary // 4. Check if objects have the same raw data. // ReSharper disable ImpureMethodCallOnReadonlyValueField (method is not impure) - var stream = new BinaryHeapStream(_data); - var rawOffset = _header.GetRawOffset(stream, _offset); + using (var stream = new BinaryHeapStream(_data)) + using (var thatStream = new BinaryHeapStream(that._data)) + { + var rawOffset = _header.GetRawOffset(stream, _offset); + var thatRawOffset = that._header.GetRawOffset(thatStream, that._offset); - var thatStream = new BinaryHeapStream(that._data); - var thatRawOffset = that._header.GetRawOffset(thatStream, that._offset); + return BinaryUtils.CompareArrays(_data, _offset + rawOffset, _header.Length - rawOffset, + that._data, that._offset + thatRawOffset, that._header.Length - thatRawOffset); + } // ReSharper restore ImpureMethodCallOnReadonlyValueField - - return BinaryUtils.CompareArrays(_data, _offset + rawOffset, _header.Length - rawOffset, - that._data, that._offset + thatRawOffset, that._header.Length - thatRawOffset); } } http://git-wip-us.apache.org/repos/asf/ignite/blob/41ae0148/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryObjectBuilder.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryObjectBuilder.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryObjectBuilder.cs index f41514f..0f1c0bd 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryObjectBuilder.cs +++ b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryObjectBuilder.cs @@ -352,38 +352,35 @@ namespace Apache.Ignite.Core.Impl.Binary /** <inheritDoc /> */ public IBinaryObject Build() { - BinaryHeapStream inStream = new BinaryHeapStream(_obj.Data); - - inStream.Seek(_obj.Offset, SeekOrigin.Begin); - // Assume that resulting length will be no less than header + [fields_cnt] * 12; int estimatedCapacity = BinaryObjectHeader.Size + (_vals == null ? 0 : _vals.Count*12); - BinaryHeapStream outStream = new BinaryHeapStream(estimatedCapacity); - - BinaryWriter writer = _binary.Marshaller.StartMarshal(outStream); + using (var outStream = new BinaryHeapStream(estimatedCapacity)) + { + BinaryWriter writer = _binary.Marshaller.StartMarshal(outStream); - writer.SetBuilder(this); + writer.SetBuilder(this); - // All related builders will work in this context with this writer. - _parent._ctx = new Context(writer); + // All related builders will work in this context with this writer. + _parent._ctx = new Context(writer); - try - { - // Write. - writer.Write(this); + try + { + // Write. + writer.Write(this); - // Process metadata. - _binary.Marshaller.FinishMarshal(writer); + // Process metadata. + _binary.Marshaller.FinishMarshal(writer); - // Create binary object once metadata is processed. - return new BinaryObject(_binary.Marshaller, outStream.InternalArray, 0, - BinaryObjectHeader.Read(outStream, 0)); - } - finally - { - // Cleanup. - _parent._ctx.Closed = true; + // Create binary object once metadata is processed. + return new BinaryObject(_binary.Marshaller, outStream.InternalArray, 0, + BinaryObjectHeader.Read(outStream, 0)); + } + finally + { + // Cleanup. + _parent._ctx.Closed = true; + } } } @@ -783,12 +780,13 @@ namespace Apache.Ignite.Core.Impl.Binary internal void ProcessBinary(IBinaryStream outStream, BinaryObject port) { // Special case: writing binary object with correct inversions. - BinaryHeapStream inStream = new BinaryHeapStream(port.Data); - - inStream.Seek(port.Offset, SeekOrigin.Begin); + using (var inStream = new BinaryHeapStream(port.Data)) + { + inStream.Seek(port.Offset, SeekOrigin.Begin); - // Use fresh context to ensure correct binary inversion. - Mutate0(new Context(), inStream, outStream, false, 0, EmptyVals); + // Use fresh context to ensure correct binary inversion. + Mutate0(new Context(), inStream, outStream, false, 0, EmptyVals); + } } /// <summary> @@ -798,18 +796,19 @@ namespace Apache.Ignite.Core.Impl.Binary /// <param name="builder">Builder.</param> internal void ProcessBuilder(IBinaryStream outStream, BinaryObjectBuilder builder) { - BinaryHeapStream inStream = new BinaryHeapStream(builder._obj.Data); - - inStream.Seek(builder._obj.Offset, SeekOrigin.Begin); + using (var inStream = new BinaryHeapStream(builder._obj.Data)) + { + inStream.Seek(builder._obj.Offset, SeekOrigin.Begin); - // Builder parent context might be null only in one case: if we never met this group of - // builders before. In this case we set context to their parent and track it. Context - // cleanup will be performed at the very end of build process. - if (builder._parent._ctx == null || builder._parent._ctx.Closed) - builder._parent._ctx = new Context(_parent._ctx); + // Builder parent context might be null only in one case: if we never met this group of + // builders before. In this case we set context to their parent and track it. Context + // cleanup will be performed at the very end of build process. + if (builder._parent._ctx == null || builder._parent._ctx.Closed) + builder._parent._ctx = new Context(_parent._ctx); - builder.Mutate(inStream, outStream as BinaryHeapStream, builder._desc, + builder.Mutate(inStream, (BinaryHeapStream) outStream, builder._desc, builder._hashCode, builder._vals); + } } /// <summary> http://git-wip-us.apache.org/repos/asf/ignite/blob/41ae0148/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/Marshaller.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/Marshaller.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/Marshaller.cs index 457830f..4274744 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/Marshaller.cs +++ b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/Marshaller.cs @@ -112,11 +112,12 @@ namespace Apache.Ignite.Core.Impl.Binary /// <returns>Serialized data as byte array.</returns> public byte[] Marshal<T>(T val) { - BinaryHeapStream stream = new BinaryHeapStream(128); - - Marshal(val, stream); + using (var stream = new BinaryHeapStream(128)) + { + Marshal(val, stream); - return stream.GetArrayCopy(); + return stream.GetArrayCopy(); + } } /// <summary> @@ -170,7 +171,10 @@ namespace Apache.Ignite.Core.Impl.Binary /// </returns> public T Unmarshal<T>(byte[] data, bool keepBinary) { - return Unmarshal<T>(new BinaryHeapStream(data), keepBinary); + using (var stream = new BinaryHeapStream(data)) + { + return Unmarshal<T>(stream, keepBinary); + } } /// <summary> @@ -183,7 +187,10 @@ namespace Apache.Ignite.Core.Impl.Binary /// </returns> public T Unmarshal<T>(byte[] data, BinaryMode mode = BinaryMode.Deserialize) { - return Unmarshal<T>(new BinaryHeapStream(data), mode); + using (var stream = new BinaryHeapStream(data)) + { + return Unmarshal<T>(stream, mode); + } } /// <summary> http://git-wip-us.apache.org/repos/asf/ignite/blob/41ae0148/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/SerializableObjectHolder.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/SerializableObjectHolder.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/SerializableObjectHolder.cs index 2da854f..08b44df 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/SerializableObjectHolder.cs +++ b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/SerializableObjectHolder.cs @@ -54,7 +54,13 @@ namespace Apache.Ignite.Core.Impl.Binary var writer0 = (BinaryWriter)writer.GetRawWriter(); - writer0.WithDetach(w => new BinaryFormatter().Serialize(new BinaryStreamAdapter(w.Stream), Item)); + writer0.WithDetach(w => + { + using (var streamAdapter = new BinaryStreamAdapter(w.Stream)) + { + new BinaryFormatter().Serialize(streamAdapter, Item); + } + }); } /// <summary> @@ -67,7 +73,10 @@ namespace Apache.Ignite.Core.Impl.Binary var reader0 = (BinaryReader) reader.GetRawReader(); - _item = new BinaryFormatter().Deserialize(new BinaryStreamAdapter(reader0.Stream), null); + using (var streamAdapter = new BinaryStreamAdapter(reader0.Stream)) + { + _item = new BinaryFormatter().Deserialize(streamAdapter, null); + } } } } \ No newline at end of file