Ejemplo n.º 1
0
        public static BPlusTree <int, ContentNodeKit> GetTree(string filepath, bool exists, ContentDataSerializer contentDataSerializer = null)
        {
            var keySerializer   = new PrimitiveSerializer();
            var valueSerializer = new ContentNodeKitSerializer(contentDataSerializer);
            var options         = new BPlusTree <int, ContentNodeKit> .OptionsV2(keySerializer, valueSerializer)
            {
                CreateFile = exists ? CreatePolicy.IfNeeded : CreatePolicy.Always,
                FileName   = filepath,

                // read or write but do *not* keep in memory
                CachePolicy = CachePolicy.None,

                // default is 4096, min 2^9 = 512, max 2^16 = 64K
                FileBlockSize = GetBlockSize(),

                // other options?
            };

            var tree = new BPlusTree <int, ContentNodeKit>(options);

            // anything?
            //btree.

            return(tree);
        }
Ejemplo n.º 2
0
        public static BPlusTree <int, ContentNodeKit> GetTree(string filepath, bool exists, NuCacheSettings settings, ContentDataSerializer?contentDataSerializer = null)
        {
            var keySerializer   = new PrimitiveSerializer();
            var valueSerializer = new ContentNodeKitSerializer(contentDataSerializer);
            var options         = new BPlusTree <int, ContentNodeKit> .OptionsV2(keySerializer, valueSerializer)
            {
                CreateFile = exists ? CreatePolicy.IfNeeded : CreatePolicy.Always,
                FileName   = filepath,

                // read or write but do *not* keep in memory
                CachePolicy = CachePolicy.None,

                // default is 4096, min 2^9 = 512, max 2^16 = 64K
                FileBlockSize = GetBlockSize(settings),

                //HACK: Forces FileOptions to be WriteThrough here: https://github.com/mamift/CSharpTest.Net.Collections/blob/9f93733b3af7ee0e2de353e822ff54d908209b0b/src/CSharpTest.Net.Collections/IO/TransactedCompoundFile.cs#L316-L327,
                // as the reflection uses otherwise will failed in .NET Core as the "_handle" field in FileStream is renamed to "_fileHandle".
                StoragePerformance = StoragePerformance.CommitToDisk,



                // other options?
            };

            var tree = new BPlusTree <int, ContentNodeKit>(options);

            // anything?
            //btree.

            return(tree);
        }
Ejemplo n.º 3
0
        public YamlValue(object value, IYamlSchema schema = null)
        {
            var valueString = PrimitiveSerializer.ConvertValue(value);

            if (schema == null)
            {
                schema = CoreSchema.Instance;
            }

            Scalar = new Scalar(schema.GetDefaultTag(value.GetType()), valueString);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Speichert einen beliebigen Wert in der Registry. Einzigste
        /// Bedingung ist, dass der Typ serialisierbar ist (IsSerializable).
        /// </summary>
        /// <param name="ownRootNodeName">Gibt den Hauptknoten an. Kann null oder leer sein, dann wird WW-Ziel + YXZ genommen.</param>
        /// <param name="section">Unterschlüssel (noch unterhalb des Dialogs). Kann null sein, und wird dann ignoriert.</param>
        /// <param name="key">Schlüssel</param>
        /// <param name="value">Wert</param>
        public static void SetValue(string section, string key, object value)
        {
            if (key == null || key == string.Empty)
            {
                throw new ArgumentException("Der Schlüssel darf nicht leer sein.");
            }

            if (value == null)
            {
                m_Provider.SetValue(null, section, key);
                return;
            }

            if (!value.GetType().IsSerializable)
            {
                throw new ArgumentException("Werte müssen serialisierbar sein (typeof(...).IsSerializable == true)");
            }

            if (value.GetType().IsEnum)
            {
                value = Convert.ToInt32(value);
            }

            // Wir unterscheiden verschiedene Arten von Daten
            // - "Einfache" (sog. primitive) Typen: string, bool, int, float (Verarbeitung mit .Parse() und ToString()
            //   - Enum-Werte werden ebenfalls so verarbeitet
            // - Allgemeine Typen: Point, Size, PointF, SizeF (Selbstgeschriebene Funktionen)
            // - Sonstige serialisierbare Typen: Werden mit dem BinarySerializer verarbeitet

            SettingsSerializer serializer = null;

            if (value.GetType().IsPrimitive)
            {
                serializer = new PrimitiveSerializer();
            }
            else if (CommonSerializer.SupportsType(value.GetType()))
            {
                // Allgemeine Typen
                serializer = new CommonSerializer();
            }
            else
            {
                // Binär als letzte Chance
                serializer = new BinarySerializer();
            }

            if (serializer == null)
            {
                throw new NotImplementedException("Kein passender Serialisierer verfügbar.");
            }
            m_Provider.SetValue(serializer.Serialize(value), section, key);
        }
Ejemplo n.º 5
0
        public static BPlusTree <int, ContentNodeKit> GetTree(string filepath, bool exists)
        {
            var keySerializer   = new PrimitiveSerializer();
            var valueSerializer = new ContentNodeKitSerializer();
            var options         = new BPlusTree <int, ContentNodeKit> .OptionsV2(keySerializer, valueSerializer)
            {
                CreateFile = exists ? CreatePolicy.IfNeeded : CreatePolicy.Always,
                FileName   = filepath,

                // other options?
            };

            var tree = new BPlusTree <int, ContentNodeKit>(options);

            // anything?
            //btree.

            return(tree);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Serializes the provided object to a string
 /// </summary>
 /// <param name="values">Object to be serialized</param>
 /// <param name="serializedData">When this method returns contains value serialized if the serialization succeeded, or zero if the serialization failed. The serialization fails if the serializedData parameter is a null reference (Nothing in Visual Basic) or outside the specification for the type. This parameter is passed uninitialized</param>
 /// <returns>true if value was serialized successfully; otherwise, false</returns>
 protected override bool Serialize(byte[] values, out string serializedData)
 {
     return(PrimitiveSerializer.SerializeHexByteArray(values, out serializedData));
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Deserializes an array of integers from a string
 /// </summary>
 /// <param name="serializedData">String containing the serialized object</param>
 /// <param name="values">When this method returns contains the value stored in serializedData the deserialization succeeded, or zero if the deserialization failed. The deserialization fails if the serializedData parameter is a null reference (Nothing in Visual Basic) or its content could not be parsed. This parameter is passed uninitialized</param>
 /// <returns>true if serializedData was deserialized successfully; otherwise, false</returns>
 protected override bool Deserialize(string serializedData, out byte[] values)
 {
     return(PrimitiveSerializer.DeserializeHexByteArray(serializedData, out values));
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Serializes the provided object to a string
 /// </summary>
 /// <param name="value">Object to be serialized</param>
 /// <param name="serializedData">When this method returns contains value serialized if the serialization succeeded, or zero if the serialization failed. The serialization fails if the serializedData parameter is a null reference (Nothing in Visual Basic) or outside the specification for the type. This parameter is passed uninitialized</param>
 /// <returns>true if value was serialized successfully; otherwise, false</returns>
 protected override bool Serialize(MimeFile value, out string serializedData)
 {
     return(PrimitiveSerializer.Serialize(value, out serializedData));
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Deserializes an array of integers from a string
 /// </summary>
 /// <param name="serializedData">String containing the serialized object</param>
 /// <param name="value">When this method returns contains the value stored in serializedData the deserialization succeeded, or zero if the deserialization failed. The deserialization fails if the serializedData parameter is a null reference (Nothing in Visual Basic) or its content could not be parsed. This parameter is passed uninitialized</param>
 /// <returns>true if serializedData was deserialized successfully; otherwise, false</returns>
 protected override bool Deserialize(string serializedData, out MimeFile value)
 {
     return(PrimitiveSerializer.Deserialize(serializedData, out value));
 }
        /// <summary>
        /// Construct a new <see cref="UmbracoXmlParser"/> instance by parsing the supplied
        /// umbraco.config XML cache file or NuCache database file.
        /// </summary>
        /// <param name="umbracoConfigOrNuCacheDb">Full path to umbraco.config XML cache file or NuCache database file.</param>
        /// <param name="options">Options to provide mappings for URL prefixes, doctypes (Umbraco 8 only) and users (Umbraco 8 only).</param>
        public UmbracoXmlParser(string umbracoConfigOrNuCacheDb, UmbracoParsingOptions options)
        {
            // Save options
            if (options != null)
            {
                Options = options;
            }

            // Remove any trailing slashes from URL prefixes as we don't want them
            if (Options.UrlPrefixMapping != null)
            {
                foreach (var key in Options.UrlPrefixMapping.Keys.ToList())
                {
                    if (Options.UrlPrefixMapping[key].EndsWith("/"))
                    {
                        Options.UrlPrefixMapping[key] = Options.UrlPrefixMapping[key].TrimEnd('/');
                    }
                }
            }

            // No file?
            if (string.IsNullOrEmpty(umbracoConfigOrNuCacheDb))
            {
                throw new ArgumentException(umbracoConfigOrNuCacheDb);
            }

            // Check first few bytes. If it's XML it will start with '<' (potentially after a BOM)
            byte[] buffer = new byte[10];
            using (var stream = new FileStream(umbracoConfigOrNuCacheDb, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                stream.Read(buffer, 0, 10);
            }

            // It's an umbraco 4 through 7 XML cache file
            if (buffer[0] == '<' ||
                buffer[0] == 0xef && buffer[1] == 0xbb && buffer[2] == 0xbf && buffer[3] == '<') // UTF-8 BOM
            {
                try
                {
                    // Load XML into an XDocument
                    ParsedXml = XDocument.Load(umbracoConfigOrNuCacheDb);

                    // Parse content into an in-memory dictionary of node ID and node information
                    ParseXmlIntoUmbracoNodes();

                    // Destroy
                    ParsedXml = null;
                    return;
                }
                catch (UmbracoXmlParsingException ex)
                {
                    ParsedXml = null;
                    throw new UmbracoXmlParsingException($"Could not parse {umbracoConfigOrNuCacheDb} as XML - {ex.Message}");
                }
                catch
                {
                    ParsedXml = null;
                    // Might be a NuCache file
                }
            }

            // Umbraco 8.0.1 or later NuCache db file
            try
            {
                var keySerializer    = new PrimitiveSerializer();
                var valueSerializer  = new ContentNodeKitSerializer();
                var bPlusTreeOptions = new BPlusTree <int, ContentNodeKit> .OptionsV2(keySerializer, valueSerializer)
                {
                    CreateFile = CreatePolicy.Never,
                    FileName   = umbracoConfigOrNuCacheDb,
                    ReadOnly   = true
                };

                // Read the file into a BPlusTreeObject
                ParsedTree = new BPlusTree <int, ContentNodeKit>(bPlusTreeOptions);
            }
            catch (Exception ex)
            {
                throw new UmbracoXmlParsingException($"Could not parse {umbracoConfigOrNuCacheDb} as a NuCache DB - {ex.Message}");
            }

            // Parse content into an in-memory dictionary of node ID and node information
            ParseTreeIntoUmbracoNodes();

            // Destroy
            ParsedTree.Dispose();
            ParsedTree = null;
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Speichert einen beliebigen Wert in der Registry. Einzigste
        /// Bedingung ist, dass der Typ serialisierbar ist (IsSerializable).
        /// </summary>
        /// <param name="ownRootNodeName">Gibt den Hauptknoten an. Kann null oder leer sein, dann wird WW-Ziel + YXZ genommen.</param>
        /// <param name="section">Unterschlüssel (noch unterhalb des Dialogs). Kann null sein, und wird dann ignoriert.</param>
        /// <param name="key">Schlüssel</param>
        /// <param name="value">Wert</param>
        public static void SetValue (string section, string key, object value) {
            if (key == null || key == string.Empty) {
                throw new ArgumentException ("Der Schlüssel darf nicht leer sein.");
            }

            if (value == null)
            {
                m_Provider.SetValue(null, section, key);
                return;
            }

            if (!value.GetType ().IsSerializable) {
                throw new ArgumentException ("Werte müssen serialisierbar sein (typeof(...).IsSerializable == true)");
            }

            if (value.GetType().IsEnum)
            {
                value = Convert.ToInt32(value);
            }

            // Wir unterscheiden verschiedene Arten von Daten
            // - "Einfache" (sog. primitive) Typen: string, bool, int, float (Verarbeitung mit .Parse() und ToString()
            //   - Enum-Werte werden ebenfalls so verarbeitet
            // - Allgemeine Typen: Point, Size, PointF, SizeF (Selbstgeschriebene Funktionen)
            // - Sonstige serialisierbare Typen: Werden mit dem BinarySerializer verarbeitet

            SettingsSerializer serializer = null;

            if (value.GetType ().IsPrimitive) {
                serializer = new PrimitiveSerializer ();
            }
            else if (CommonSerializer.SupportsType (value.GetType ())) {
                // Allgemeine Typen
                serializer = new CommonSerializer ();
            }
            else {
                // Binär als letzte Chance
                serializer = new BinarySerializer ();
            }

            if (serializer == null) {
                throw new NotImplementedException ("Kein passender Serialisierer verfügbar.");
            }
            m_Provider.SetValue(serializer.Serialize(value), section, key);
        }
Ejemplo n.º 12
0
        public HttpResponseMessage GetNuCacheFile(string contentType)
        {
            var filePath     = Path.Combine(globalSettings.LocalTempPath, "NuCache\\NuCache." + contentType + ".db");
            var tempFileName = filePath.Replace(".db", ".Explorer.Temp.db");

            try
            {
                //Check for valid filepath
                if (File.Exists(filePath) == false)
                {
                    var message = $"No file exists on disk at {filePath}";
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, message));
                }

                //Check for file extension ends with .db
                //Don't want to attempt to any old file type
                if (Path.GetExtension(filePath) != ".db")
                {
                    var message = $"The file {filePath} is not a .db file";
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, message));
                }

                //We need to create a temp copy of the nucache DB - to avoid file locks if its in use whilst we try to read it
                //'NuCache.Content.db' will become 'NuCache.Content.Explorer.Temp.db'
                File.Copy(filePath, tempFileName, true);

                var keySerializer   = new PrimitiveSerializer();
                var valueSerializer = new ContentNodeKitSerializer();
                var options         = new BPlusTree <int, ContentNodeKit> .OptionsV2(keySerializer, valueSerializer)
                {
                    CreateFile = CreatePolicy.Never,
                    FileName   = tempFileName,

                    // default is 4096, min 2^9 = 512, max 2^16 = 64K
                    FileBlockSize = GetBlockSize(),
                };

                //Read the file into a BPlusTreeObject & select the kits
                var tree = new BPlusTree <int, ContentNodeKit>(options);
                var sw   = Stopwatch.StartNew();
                var kits = tree.Select(x => x.Value).ToArray();
                sw.Stop();
                tree.Dispose();

                DeleteTempFile(tempFileName);

                //Add to our JSON object the stopwatch clock to read the DB/dictionary file
                var response = new ApiResponse
                {
                    Items      = kits,
                    TotalItems = kits.Length,
                    StopClock  = new StopClock
                    {
                        Hours        = sw.Elapsed.Hours,
                        Minutes      = sw.Elapsed.Minutes,
                        Seconds      = sw.Elapsed.Seconds,
                        Milliseconds = sw.Elapsed.Milliseconds,
                        Ticks        = sw.Elapsed.Ticks
                    }
                };

                return(Request.CreateResponse(HttpStatusCode.OK, response));
            }
            catch (Exception e)
            {
                DeleteTempFile(tempFileName);
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e.Message));
            }
        }
Ejemplo n.º 13
0
        public HttpResponseMessage GetNuCacheData(string filePath)
        {
            //Check for valid filepath
            if (File.Exists(filePath) == false)
            {
                var message = $"No file exists on disk at {filePath}";
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, message));
            }

            //Check for file extension ends with .db
            //Don't want to attempt to any old file type
            if (Path.GetExtension(filePath) != ".db")
            {
                var message = $"The file {filePath} is not a .db file";
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, message));
            }


            //We need to create a temp copy of the nucache DB - to avoid file locks if its in use whilst we try to read it
            //'NuCache.Content.db' will become 'NuCache.Content.Explorer.Temp.db'
            var tempFileName = filePath.Replace(".db", ".Explorer.Temp.db");

            File.Copy(filePath, tempFileName, true);

            var keySerializer   = new PrimitiveSerializer();
            var valueSerializer = new ContentNodeKitSerializer();
            var options         = new BPlusTree <int, ContentNodeKit> .OptionsV2(keySerializer, valueSerializer)
            {
                CreateFile = CreatePolicy.Never,
                FileName   = tempFileName
            };

            //Read the file into a BPlusTreeObject & select the kits
            var tree = new BPlusTree <int, ContentNodeKit>(options);
            var sw   = Stopwatch.StartNew();
            var kits = tree.Select(x => x.Value).ToArray();

            sw.Stop();
            tree.Dispose();

            //Delete the file (seems like could be a lock, so we wait 100ms between each attempt upto 10 times)
            var ok       = false;
            var attempts = 0;

            while (!ok)
            {
                System.Threading.Thread.Sleep(100);
                try
                {
                    attempts++;
                    File.Delete(tempFileName);
                    ok = true;
                }
                catch
                {
                    if (attempts == 10)
                    {
                        throw;
                    }
                }
            }

            //Add to our JSON object the stopwatch clock to read the DB/dictionary file
            var response = new ApiResponse
            {
                Items      = kits,
                TotalItems = kits.Length,
                StopClock  = new StopClock
                {
                    Hours        = sw.Elapsed.Hours,
                    Minutes      = sw.Elapsed.Minutes,
                    Seconds      = sw.Elapsed.Seconds,
                    Milliseconds = sw.Elapsed.Milliseconds,
                    Ticks        = sw.Elapsed.Ticks
                }
            };

            return(Request.CreateResponse(HttpStatusCode.OK, response));
        }