Ejemplo n.º 1
0
        public virtual void Load(string directoryPath)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();

            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }

            // Tempファイルを削除する。
            {
                foreach (var path in Directory.GetFiles(directoryPath, "*", SearchOption.TopDirectoryOnly))
                {
                    var ext = Path.GetExtension(path);

                    if (ext == ".tmp")
                    {
                        try
                        {
                            File.Delete(path);
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
            }

            LockedHashSet <string> successNames = new LockedHashSet <string>();

            // DataContractSerializerのBinaryバージョン
            foreach (var extension in new string[] { ".v2", ".v2.bak" })
            {
                Parallel.ForEach(Directory.GetFiles(directoryPath), new ParallelOptions()
                {
                    MaxDegreeOfParallelism = 8
                }, configPath =>
                {
                    if (!configPath.EndsWith(extension))
                    {
                        return;
                    }

                    var name = Path.GetFileName(configPath.Substring(0, configPath.Length - extension.Length));
                    if (successNames.Contains(name))
                    {
                        return;
                    }

                    Content content = null;
                    if (!_dic.TryGetValue(name, out content))
                    {
                        return;
                    }

                    try
                    {
                        using (FileStream stream = new FileStream(configPath, FileMode.Open))
                            using (CacheStream cacheStream = new CacheStream(stream, _cacheSize, BufferManager.Instance))
                                using (GZipStream decompressStream = new GZipStream(cacheStream, CompressionMode.Decompress))
                                {
                                    //using (XmlDictionaryReader xmlDictionaryReader = XmlDictionaryReader.CreateTextReader(decompressStream, XmlDictionaryReaderQuotas.Max))
                                    using (XmlDictionaryReader xml = XmlDictionaryReader.CreateBinaryReader(decompressStream, XmlDictionaryReaderQuotas.Max))
                                    {
                                        var deserializer = new DataContractSerializer(content.Type);
                                        content.Value    = deserializer.ReadObject(xml);
                                    }
                                }

                        successNames.Add(name);
                    }
                    catch (Exception e)
                    {
                        Log.Warning(e);
                    }
                });
            }

            // DataContractSerializerのTextバージョン
            // 互換性は高いが処理速度が遅い。
            foreach (var extension in new string[] { ".gz", ".gz.bak" })
            {
                foreach (var configPath in Directory.GetFiles(directoryPath))
                {
                    if (!configPath.EndsWith(extension))
                    {
                        continue;
                    }

                    var name = Path.GetFileName(configPath.Substring(0, configPath.Length - extension.Length));
                    if (successNames.Contains(name))
                    {
                        continue;
                    }

                    Content content = null;
                    if (!_dic.TryGetValue(name, out content))
                    {
                        continue;
                    }

                    try
                    {
                        using (FileStream stream = new FileStream(configPath, FileMode.Open))
                            using (CacheStream cacheStream = new CacheStream(stream, _cacheSize, BufferManager.Instance))
                                using (GZipStream decompressStream = new GZipStream(cacheStream, CompressionMode.Decompress))
                                {
                                    using (XmlDictionaryReader xml = XmlDictionaryReader.CreateTextReader(decompressStream, XmlDictionaryReaderQuotas.Max))
                                    {
                                        var deserializer = new DataContractSerializer(content.Type);
                                        content.Value = deserializer.ReadObject(xml);
                                    }
                                }

                        successNames.Add(name);
                    }
                    catch (Exception e)
                    {
                        Log.Warning(e);
                    }
                }
            }

            sw.Stop();
            Debug.WriteLine("Settings Load {0} {1}", Path.GetFileName(directoryPath), sw.ElapsedMilliseconds);
        }
Ejemplo n.º 2
0
        private void MessageUploadAndDownload(IEnumerable <CoreManager> coreManagers)
        {
            _callback.Invoke("----- CoreManager Message Send and Receive Test -----");
            _callback.Invoke("");

            var coreManagerList = coreManagers.ToList();

            Parallel.ForEach(coreManagerList, coreManager =>
            {
                coreManager.Resize((long)1024 * 1024 * 1024 * 32).Wait();
            });

            var hashList     = new LockedHashSet <Hash>();
            var metadataList = new LockedList <Metadata>();

            Parallel.For(0, 30, new ParallelOptions()
            {
                MaxDegreeOfParallelism = 3
            }, i =>
            {
                var random = RandomProvider.GetThreadRandom();

                using (var stream = new BufferStream(_bufferManager))
                {
                    using (var safeBuffer = _bufferManager.CreateSafeBuffer(1024 * 4))
                    {
                        for (long remain = (long)1024 * 1024 * 32; remain > 0; remain = Math.Max(0, remain - safeBuffer.Value.Length))
                        {
                            int length = (int)Math.Min(remain, safeBuffer.Value.Length);

                            random.NextBytes(safeBuffer.Value);
                            stream.Write(safeBuffer.Value, 0, length);
                        }
                    }

                    stream.Seek(0, SeekOrigin.Begin);
                    hashList.Add(new Hash(HashAlgorithm.Sha256, Sha256.ComputeHash(new WrapperStream(stream, true))));

                    stream.Seek(0, SeekOrigin.Begin);
                    using (var tokenSource = new CancellationTokenSource())
                    {
                        metadataList.Add(coreManagerList[i].VolatileSetStream(stream, new TimeSpan(1, 0, 0), tokenSource.Token).Result);
                    }
                }
            });

            var sw = Stopwatch.StartNew();

            Parallel.ForEach(metadataList, metadata =>
            {
                for (; ;)
                {
                    Thread.Sleep(1000);

                    Stream stream = null;

                    try
                    {
                        stream = coreManagerList[0].VolatileGetStream(metadata, 1024 * 1024 * 256);
                        if (stream == null)
                        {
                            continue;
                        }

                        var hash = new Hash(HashAlgorithm.Sha256, Sha256.ComputeHash(stream));
                        if (!hashList.Contains(hash))
                        {
                            throw new ArgumentException("Broken");
                        }

                        _callback.Invoke($"{sw.Elapsed.ToString("hh\\:mm\\:ss")}: Success {NetworkConverter.ToBase64UrlString(metadata.Hash.Value)}");

                        return;
                    }
                    finally
                    {
                        if (stream != null)
                        {
                            stream.Dispose();
                        }
                    }
                }
            });

            _callback.Invoke("----- End -----");
        }