Ejemplo n.º 1
0
        static void testSingleWrites(Dictionary <string, byte[]> extractedFiles, PersistentContainerCompressType compressType = 0)
        {
            var totalLengths = extractedFiles.Sum(p => p.Value.Length);

            Console.WriteLine();
            Console.WriteLine("Single requests - write [Compress={0}]", compressType.ToString());
            Console.WriteLine("Page\tms\tMB\tMB/sec\tLost(%)");
            foreach (var pageSize in pageSizes)
            {
                var targetFileName = getFileNameByPageSize(pageSize);
                if (File.Exists(targetFileName))
                {
                    File.Delete(targetFileName);
                }

                var sw = Stopwatch.StartNew();
                using (var container = new PersistentContainer(targetFileName, new PersistentContainerSettings(pageSize, 0, compressType)))
                    foreach (var file in extractedFiles)
                    {
                        container.Put(file.Key, file.Value);
                    }

                var targetFileLength = new FileInfo(targetFileName).Length;

                sw.Stop();

                var lengthInMB = targetFileLength / 1048576.0;
                Console.WriteLine("{0,5}\t{1}\t{2:F1}\t{3:F1}\t{4}",
                                  pageSize, sw.ElapsedMilliseconds, lengthInMB,
                                  lengthInMB / (sw.ElapsedMilliseconds / 1000.0),
                                  compressType == PersistentContainerCompressType.None
                                      ? (((targetFileLength - totalLengths) / (double)targetFileLength) * 100).ToString("F2")
                                      : "-");
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            // var cc = new Symmetric(Aes.Create());
            // for (var i = 1; i < 2000; i++)
            // {
            //     var buff = Enumerable.Range(0, i).Select(p => (byte) (p & 0xFF)).ToArray();
            //     var b    = cc.Encrypt(buff);
            //     var z    = cc.Decrypt(b);
            //
            //     var b2 = cc.Encrypt(buff);
            //     var z2 = cc.Decrypt(b);
            // }

            var aes = Aes.Create();

            aes.GenerateKey();
            aes.GenerateIV();

            var fileName = Path.Combine(@"D:\test2.container");

            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            var text = string.Join(Environment.NewLine, Enumerable.Range(0, 15).Select(p => $"Hello, line #{p}, Текст, κείμενο, ਟੈਕਸਟ, random guid: {Guid.NewGuid()}"));

            const int maxItems = 50;

            using (var pc = new PersistentContainer(fileName, new PersistentContainerSettings(256).With(aes)))
            {
                foreach (var itemId in Enumerable.Range(0, maxItems))
                {
                    var path = (itemId / 10).ToString("D4");
                    pc.Put($"/{path}/item{itemId}", text);
                }
            }

            using (var pc = new PersistentContainer(fileName, new PersistentContainerSettings(256).With(aes)))
            {
                Console.WriteLine("File length: {0} bytes, entries: {1}", pc.Length, pc.Find().Length);
                Console.WriteLine();

                var mask    = "/004?/*";
                var entries = pc.Find(mask);
                Console.WriteLine("Found {0} items with mask: {1}, show first 10:", entries.Length, mask);
                foreach (var entry in entries.Take(10))
                {
                    var item = pc.GetString(entry.Name);
                    Console.WriteLine(entry);
                }
            }
        }
Ejemplo n.º 3
0
        static void testBatchWrites(Dictionary <string, byte[]> extractedFiles, int batchSize, PersistentContainerCompressType compressType = 0)
        {
            Console.WriteLine();
            Console.WriteLine("Batch requests [Compress={0}]: {1}", compressType.ToString(), batchSize);
            Console.WriteLine("Page        ms     MB/sec(w)    MB/sec(r)     Ratio");

            var items = makeBatches(extractedFiles, batchSize);

            foreach (var pageSize in pageSizes)
            {
                var targetFileName = getFileNameByPageSize(pageSize);
                if (File.Exists(targetFileName))
                {
                    File.Delete(targetFileName);
                }

                var sw = Stopwatch.StartNew();
                using (var container = new PersistentContainer(targetFileName, new PersistentContainerSettings(pageSize, 0, compressType)))
                    foreach (var item in items)
                    {
                        container.Put(item);
                    }

                var elapsedWrites = sw.ElapsedMilliseconds;

                var targetFileLength = new FileInfo(targetFileName).Length;

                sw.Restart();
                var rawLength        = 0;
                var compressedLength = 0;
                using (var container = new PersistentContainer(targetFileName, new PersistentContainerSettings(pageSize, 0, compressType)))
                {
                    foreach (var entry in container.Find())
                    {
                        compressedLength += entry.CompressedLength;
                        rawLength        += container.Get(entry.Name)?.Length ?? 0;
                    }
                }

                var elapsedRead = sw.ElapsedMilliseconds;
                sw.Stop();

                var lengthInMB    = targetFileLength / 1048576.0;
                var rawLengthInMB = rawLength / 1048576.0;
                Console.WriteLine("{0,5} {1,8}      {2,8:F1}     {3,8:F1} {4,8:F2}x",
                                  pageSize, sw.ElapsedMilliseconds,
                                  lengthInMB / (elapsedWrites / 1000.0),
                                  rawLengthInMB / (elapsedRead / 1000.0),
                                  rawLength / (double)compressedLength);
            }
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            var fileName = Path.Combine(Path.GetTempPath(), "test2.container");

            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            const int maxItems = 1000;

            using (var pc = new PersistentContainer(fileName, new PersistentContainerSettings(256)))
            {
                // write 0000/item0, 0000/item1, 0000/item2, ..., 0001/item10, 0001/item11, ..., 0002/item10, 0002/item21,
                foreach (var itemId in Enumerable.Range(0, maxItems))
                {
                    var path = (itemId / 10).ToString("D4");
                    pc.Put($"/{path}/item{itemId}", $"Hello, i'm item #{itemId}, some random guid: {Guid.NewGuid()}");
                }
            }

            using (var pc = new PersistentContainer(fileName, new PersistentContainerSettings(256)))
            {
                Console.WriteLine("File length: {0} bytes, entries: {1}", pc.Length, pc.Find().Length);
                Console.WriteLine();

                var mask1    = "/004?/*";
                var entries1 = pc.Find(mask1);
                Console.WriteLine("Found {0} items with mask: {1}, show first 10:", entries1.Length, mask1);
                foreach (var entry in entries1.Take(10))
                {
                    Console.WriteLine(entry);
                }
                Console.WriteLine();

                var mask2    = "/*/item?00";
                var entries2 = pc.Find(mask2);
                Console.WriteLine("Found {0} items with mask: {1}, show first 10:", entries2.Length, mask2);
                foreach (var entry in entries2.Take(10))
                {
                    Console.WriteLine(entry);
                }
                Console.WriteLine();
            }
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            var fileName = Path.Combine(Path.GetTempPath(), "test.container");

            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            using (var pc = new PersistentContainer(fileName, new PersistentContainerSettings(256)))
            {
                pc.Put("item1", "Hello");
                pc.Put("item2", "User");

                // overwrite item1:
                pc.Put("item1", "Bye!");

                pc.Append("item1", "See you later!");

                pc.Delete("item2");

                pc.Put("item3", "another item");
            }

            using (var pc = new PersistentContainer(fileName, new PersistentContainerSettings(256)))
            {
                var entries = pc.Find();
                foreach (var entry in entries)
                {
                    Console.WriteLine(entry);
                }

                Console.WriteLine();
                foreach (var entry in entries)
                {
                    Console.WriteLine("{0}: {1}", entry.Name, pc.GetString(entry.Name));
                }
            }
        }
Ejemplo n.º 6
0
        public override bool Fire(int b)
        {
            // todo: move this to the abstract object??
            if (_pcCache == null && PersistentContainer.IsLoaded)
            {
                try
                {
                    _pcCache = PersistentContainer.Instance.Clone();
                }
                catch
                {
                    Log.Out(Config.ModPrefix + " CACHE CLONE ERROR");
                }
            }

            if (ConnectionManager.Instance.ClientCount() > 0 && _pcCache != null)
            {
                using (List <ClientInfo> .Enumerator enumerator = ConnectionManager.Instance.GetClients().GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        string playerName = enumerator.Current.playerName;
                        string playerId   = enumerator.Current.playerId;

                        //Quests _currentQuests = new Quests();
                        //Quests _cacheQuests = new Quests();
                        try
                        {
                            Player pcurr  = PersistentContainer.Instance.Players[playerId, false];
                            Player pcache = _pcCache.Players[playerId, true]; // allow player to be created if it doesn't exist in cache
                            if (pcurr != null)
                            {
                                //_currentQuests = pcurr.Quests;
                            }
                            if (pcache != null)
                            {
                                //_cacheQuests = pcache.Quests;
                            }
                            //else
                            //{
                            //  // test: need to create player in cache if they have just joined for the first time after deleting the data file
                            //  try
                            //  {
                            //    _pcCache.Players[playerId, true].Quests.quests = pcurr.Quests.quests.Clone();
                            //    pcache =
                            //  } catch (Exception e)
                            //  {
                            //    Log.Out(Config.ModPrefix + " QuestMonitoring.Fire Exception couldn't update new player quests: " + e);
                            //  }
                            //}
                        }
                        catch (Exception e)
                        { Log.Out(Config.ModPrefix + " QuestMonitoring.Fire Exception getting quest lists: " + e); }

                        List <Quest> _changedQuests = new List <Quest>();
                        try
                        {
                            //foreach (Quest q in _currentQuests.quests)
                            //{
                            //  if (_cacheQuests != null)
                            //  {
                            //    Quest qq = _cacheQuests.quests.Find(x => x.Id == q.Id && x.CurrentState == q.CurrentState);
                            //    if (qq == null)
                            //    {
                            //      //not found in list, so must be new or status changed
                            //      _changedQuests.Add(q);
                            //    }
                            //    else
                            //    {
                            //      //found a match so remove 1 instance of quest
                            //      _cacheQuests.quests.RemoveAt(_cacheQuests.quests.LastIndexOf(qq));
                            //    }
                            //  }
                            //  else
                            //  {
                            //    _changedQuests.Add(q);
                            //  }
                            //}
                            // update cached quests to current quests
                            try
                            {
                                //_cacheQuests.quests = _currentQuests.quests.Clone();
                            }
                            catch (Exception e)
                            {
                                Log.Out(Config.ModPrefix + " QuestMonitoring.Fire Exception assigning current quests to cache: " + e);
                            }
                        }
                        catch (Exception e)
                        {
                            Log.Out(Config.ModPrefix + " QuestMonitoring.Fire Exception computing changed quests: " + e);
                            _changedQuests = new List <Quest>(); //if an error computing changes then don't return a list or it spams log files
                        }

                        try
                        {
                            foreach (Quest q in _changedQuests)
                            {
                                Log.Out(Config.ModPrefix + " " + playerName + " Quest Status Changed to " + q.CurrentState + ":" + q.ID + "");
                            }
                        }
                        catch (Exception e)
                        { Log.Out(Config.ModPrefix + " QuestMonitoring.Fire Exception: (changedquests.foreach) " + e); }
                    }
                }
            }

            return(true);
        }