|
| 1 | +/* Copyright 2010-present MongoDB Inc. |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +using System; |
| 17 | +using System.Globalization; |
| 18 | + |
| 19 | +namespace MongoDB.Bson.IO; |
| 20 | + |
| 21 | +/// <summary> |
| 22 | +/// Provides utility methods for working with <see cref="IBsonReader"/> instances in binary BSON format. |
| 23 | +/// </summary> |
| 24 | +public static class BsonBinaryReaderUtils |
| 25 | +{ |
| 26 | + /// <summary> |
| 27 | + /// Creates an instance of <see cref="IBsonReader"/> for the given byte buffer and reader settings. |
| 28 | + /// The result <see cref="IBsonReader"/> instance does not own the byte buffer, and will not Dispose it. |
| 29 | + /// For continuous single chunk buffers an optimized implementation of <see cref="IBsonReader"/> is created. |
| 30 | + /// </summary> |
| 31 | + /// <param name="byteBuffer">The byte buffer containing BSON data.</param> |
| 32 | + /// <param name="settings">The settings to configure the BSON reader.</param> |
| 33 | + /// <returns>An <see cref="IBsonReader"/>Bson reader.</returns> |
| 34 | + public static IBsonReader CreateBinaryReader(IByteBuffer byteBuffer, BsonBinaryReaderSettings settings) |
| 35 | + { |
| 36 | + if (byteBuffer is ReadOnlyMemoryBuffer readOnlyMemoryBuffer) |
| 37 | + { |
| 38 | + return new ReadOnlyMemoryBsonReader(readOnlyMemoryBuffer.Memory, new ReadOnlyMemoryReaderSettings(settings)); |
| 39 | + } |
| 40 | + |
| 41 | + var backingBytes = byteBuffer.AccessBackingBytes(0); |
| 42 | + if (backingBytes.Count == byteBuffer.Length) |
| 43 | + { |
| 44 | + return new ReadOnlyMemoryBsonReader(backingBytes, new ByteBufferSlicer(byteBuffer), new ReadOnlyMemoryReaderSettings(settings)); |
| 45 | + } |
| 46 | + |
| 47 | + var stream = new ByteBufferStream(byteBuffer, ownsBuffer: false); |
| 48 | + return new BsonBinaryReader(stream, settings); |
| 49 | + } |
| 50 | + |
| 51 | + internal static string GenerateDottedElementName(BsonBinaryReaderContext context, BsonBinaryReaderContext[] parentContexts, Func<string> elementNameReader) |
| 52 | + { |
| 53 | + string elementName; |
| 54 | + if (context.ContextType == ContextType.Document) |
| 55 | + { |
| 56 | + try |
| 57 | + { |
| 58 | + elementName = elementNameReader(); |
| 59 | + } |
| 60 | + catch |
| 61 | + { |
| 62 | + elementName = "?"; // ignore exception |
| 63 | + } |
| 64 | + } |
| 65 | + else if (context.ContextType == ContextType.Array) |
| 66 | + { |
| 67 | + elementName = context.ArrayIndex.ToString(NumberFormatInfo.InvariantInfo); |
| 68 | + } |
| 69 | + else |
| 70 | + { |
| 71 | + elementName = "?"; |
| 72 | + } |
| 73 | + |
| 74 | + return GenerateDottedElementName(parentContexts, 0, elementName); |
| 75 | + } |
| 76 | + |
| 77 | + private static string GenerateDottedElementName(BsonBinaryReaderContext[] contexts, int currentContextIndex, string elementName) |
| 78 | + { |
| 79 | + if (currentContextIndex >= contexts.Length) |
| 80 | + return elementName; |
| 81 | + |
| 82 | + var context = contexts[currentContextIndex]; |
| 83 | + var nextIndex = currentContextIndex + 1; |
| 84 | + |
| 85 | + if (context.ContextType == ContextType.Document) |
| 86 | + { |
| 87 | + return GenerateDottedElementName(contexts, nextIndex, (context.ElementName ?? "?") + "." + elementName); |
| 88 | + } |
| 89 | + |
| 90 | + if (context.ContextType == ContextType.Array) |
| 91 | + { |
| 92 | + var indexElementName = context.ArrayIndex.ToString(NumberFormatInfo.InvariantInfo); |
| 93 | + return GenerateDottedElementName(contexts, nextIndex, indexElementName + "." + elementName); |
| 94 | + } |
| 95 | + |
| 96 | + if (nextIndex < contexts.Length) |
| 97 | + { |
| 98 | + return GenerateDottedElementName(contexts, nextIndex, "?." + elementName); |
| 99 | + } |
| 100 | + |
| 101 | + return elementName; |
| 102 | + } |
| 103 | +} |
0 commit comments