Esempio n. 1
0
        /// <summary>
        ///  Build the JsonMap for a given Json file, given a stream provider.
        ///  Returns null if the source file was too small for any map nodes to fit the size budget.
        /// </summary>
        /// <param name="streamProvider">A function which will open the stream for the desired file</param>
        /// <param name="settings">JsonMapSettings for map; null for defaults</param>
        /// <returns>JsonMap for file or null if file too small for map</returns>
        public static JsonMapNode Build(Func <Stream> streamProvider, JsonMapSettings settings = null)
        {
            JsonMapRunSettings runSettings = null;

            using (Stream stream = streamProvider())
            {
                long length = stream.Length;

                // Compute JsonMapSettings for this specific file
                runSettings = new JsonMapRunSettings(length, settings ?? JsonMapRunSettings.DefaultSettings);

                // Don't build the map at all if the file is too small for anything to be mapped
                if (length <= runSettings.MinimumSizeForNode)
                {
                    return(null);
                }
            }

            // Parse file using JsonPositionedTextReader so map can get byte locations of elements
            using (JsonPositionedTextReader reader = new JsonPositionedTextReader(streamProvider))
            {
                if (!reader.Read())
                {
                    return(null);
                }
                return(Build(reader, runSettings, startPosition: 0, out long _));
            }
        }
Esempio n. 2
0
        public JsonMapRunSettings(double fileSizeBytes, JsonMapSettings userSettings)
            : base(userSettings.MapDesiredSizeRatio, userSettings.MapMaximumSizeBytes)
        {
            double expectedSize = fileSizeBytes * MapDesiredSizeRatio;

            // Calculate real ratio to use for this file (desired if file size permits)
            if (MapMaximumSizeBytes > 0 && expectedSize > MapMaximumSizeBytes)
            {
                CurrentSizeRatio = MapMaximumSizeBytes / fileSizeBytes;
            }
            else
            {
                CurrentSizeRatio = MapDesiredSizeRatio;
            }

            // Calculate the minimum node size which can fit into the map given the specific ratio
            MinimumSizeForNode = (int)(NodeSizeEstimateBytes / CurrentSizeRatio);
        }
Esempio n. 3
0
 /// <summary>
 ///  Build the JsonMap for a given Json file, by path.
 ///  Returns null if the source file was too small for any map nodes to fit the size budget.
 /// </summary>
 /// <param name="filePath">File Path to the Json file to build a map from</param>
 /// <param name="settings">JsonMapSettings for map; null for defaults</param>
 /// <returns>JsonMap for file or null if file too small for map</returns>
 public static JsonMapNode Build(string filePath, JsonMapSettings settings = null)
 {
     return(Build(() => File.OpenRead(filePath), settings));
 }