コード例 #1
0
ファイル: Program.cs プロジェクト: nRafinia/GrpcCache
        private static void Test2()
        {
            // This sample uses class key and value types, which are not blittable (i.e., they
            // require a pointer to heap objects). Such datatypes include variable length types
            // such as strings. You can override the default key equality comparer in two ways;
            // (1) Make Key implement IFasterEqualityComparer<Key> interface
            // (2) Provide IFasterEqualityComparer<Key> instance as param to constructor
            // FASTER stores the actual objects in a separate object log.
            // Note that serializers are required for class types, see below.

            var dir = Path.GetTempPath() + "\\Faster";

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

            var log    = Devices.CreateLogDevice(Path.GetTempPath() + "hlog.log");
            var objlog = Devices.CreateLogDevice(Path.GetTempPath() + "hlog.obj.log");

            var h = new FasterKV
                    <MyKey, MyValue, MyInput, MyOutput, MyContext, MyFunctions>
                        (1L << 20, new MyFunctions(),
                        new LogSettings {
                LogDevice = log, ObjectLogDevice = objlog, MemorySizeBits = 29, CopyReadsToTail = true
                },
                        new CheckpointSettings()
                    {
                    CheckpointDir = ""
                },
                        new SerializerSettings <MyKey, MyValue> {
                keySerializer = () => new MyKeySerializer(), valueSerializer = () => new MyValueSerializer()
                }
                        );

            var context = default(MyContext);

            // Each thread calls StartSession to register itself with FASTER
            //var s = h.NewSession("Test");
            var g = Guid.Parse("6eb89566-8ef4-4280-8559-50be0cd3fbfe");

            h.Recover(g);
            var s = h.ResumeSession("Test", out var aa);

            MyKey key;

            key = new MyKey {
                Key = 23.ToString()
            };
            var      inp1    = default(MyInput);
            MyOutput g11     = new MyOutput();
            var      status1 = s.Read(ref key, ref inp1, ref g11, context, 0);


            for (int i = 0; i < 20000; i++)
            {
                key = new MyKey {
                    Key = i.ToString()
                };
                var value = new MyValue
                {
                    Value      = i,
                    ExpireDate = DateTime.Now.AddSeconds(new Random().Next(60))
                };
                s.Upsert(ref key, ref value, context, 0);

                // Each thread calls Refresh periodically for thread coordination
                if (i % 1024 == 0)
                {
                    s.Refresh();
                }
            }

            h.Log.Flush(true);

            //h.TakeFullCheckpoint(out var aa);

            key = new MyKey {
                Key = 23.ToString()
            };
            var      input  = default(MyInput);
            MyOutput g1     = new MyOutput();
            var      status = s.Read(ref key, ref input, ref g1, context, 0);

            if (status == Status.OK && g1.Value.Value.ToString() == key.Key)
            {
                Console.WriteLine("Success!");
            }
            else
            {
                Console.WriteLine("Error!");
            }

            MyOutput g2 = new MyOutput();

            key = new MyKey {
                Key = 46.ToString()
            };
            status = s.Read(ref key, ref input, ref g2, context, 0);

            if (status == Status.OK && g2.Value.Value.ToString() == key.Key)
            {
                Console.WriteLine("Success!");
            }
            else
            {
                Console.WriteLine("Error!");
            }

            var inp = new MyValue()
            {
                Value      = 1000,
                ExpireDate = DateTime.Now.AddMinutes(1)
            };

            status = s.Upsert(ref key, ref inp, context, 0);

            var outp = new MyOutput();

            status = s.Read(ref key, ref input, ref outp, context, 0);

            // Delete key, read to verify deletion
            var output = new MyOutput();

            s.Delete(ref key, context, 0);
            status = s.Read(ref key, ref input, ref output, context, 0);
            if (status == Status.NOTFOUND)
            {
                Console.WriteLine("Success!");
            }
            else
            {
                Console.WriteLine("Error!");
            }


            // Each thread ends session when done

            s.Dispose();

            // Dispose FASTER instance and log
            h.Dispose();
            log.Close();
            objlog.Close();

            Console.WriteLine("Press <ENTER> to end");
            Console.ReadLine();
        }
コード例 #2
0
ファイル: TestTreap.cs プロジェクト: taotao111/redblacktree
        static public void Main()
        {
            // create MyObjs containing key and string data
            MyObj obj1 = new MyObj(0003, "MyObj C");
            MyObj obj2 = new MyObj(0001, "MyObj A");
            MyObj obj3 = new MyObj(0002, "MyObj B");
            MyObj obj4 = new MyObj(0004, "MyObj D");
            MyObj obj5 = new MyObj(0005, "MyObj E");

            try
            {
                // format: Add(key, value)
                treap.Add(new MyKey(obj1.Key), obj1);
                Console.WriteLine("Added to Treap, key: " + obj1.ToString());
                treap.Add(new MyKey(obj2.Key), obj2);
                Console.WriteLine("Added to Treap, key: " + obj2.ToString());
                treap.Add(new MyKey(obj3.Key), obj3);
                Console.WriteLine("Added to Treap, key: " + obj3.ToString());
                treap.Add(new MyKey(obj4.Key), obj4);
                Console.WriteLine("Added to Treap, key: " + obj4.ToString());
                treap.Add(new MyKey(obj5.Key), obj5);
                Console.WriteLine("Added to Treap, key: " + obj5.ToString());
                Console.WriteLine(Environment.NewLine);

                DumpTreap(true);
                Console.WriteLine(Environment.NewLine);

                Console.WriteLine("- Treap Values -");
                TreapEnumerator t = treap.Values();
                while (t.MoveNext())
                {
                    Console.WriteLine(((MyObj)(t.Value)).Data);
                }
                Console.WriteLine(Environment.NewLine);

                Console.WriteLine("- Treap Keys -");
                TreapEnumerator k = treap.Keys();
                while (k.MoveNext())
                {
                    Console.WriteLine(k.Key);
                }
                Console.WriteLine(Environment.NewLine);

                DumpMinMaxValue();
                Console.WriteLine(Environment.NewLine);

                // test Remove
                MyKey tObjKey = (MyKey)treap.GetMinKey();
                MyObj tObj    = (MyObj)treap.GetData(tObjKey);
                Console.WriteLine("Remove Min Key: " + tObj.ToString());
                Console.WriteLine(Environment.NewLine);
                treap.Remove(tObjKey);
                DumpTreap(false);
                Console.WriteLine(Environment.NewLine);

                Console.WriteLine("Remove Max Value:" + treap.GetMaxValue().ToString());
                treap.RemoveMax();
                Console.WriteLine("Remove Min Value:" + treap.GetMinValue().ToString());
                treap.RemoveMin();
                Console.WriteLine(Environment.NewLine);

                DumpTreap(true);
                Console.WriteLine(Environment.NewLine);

                Console.WriteLine("Remove Min Key:" + ((MyKey)(treap.GetMinKey())).ToString());
                treap.RemoveMin();
                Console.WriteLine("Remove Max Key:" + ((MyKey)(treap.GetMaxKey())).ToString());
                treap.RemoveMax();

                Console.WriteLine(Environment.NewLine);
                DumpTreap(true);
                Console.WriteLine(Environment.NewLine);

                // add some more and clear the treap
                Console.WriteLine("- Adding MyKeyObjs - ");
                Console.WriteLine(Environment.NewLine);

                MyKeyObj myKeyObj1 = new MyKeyObj(0025, "MyKeyObj W");
                MyKeyObj myKeyObj2 = new MyKeyObj(0023, "MyKeyObj X");
                MyKeyObj myKeyObj3 = new MyKeyObj(0026, "MyKeyObj Y");
                MyKeyObj myKeyObj4 = new MyKeyObj(0024, "MyKeyObj Z");
                treap.Add(myKeyObj1.Key, myKeyObj1);
                Console.WriteLine("Added to Treap, key: " + myKeyObj1.ToString());
                treap.Add(myKeyObj2.Key, myKeyObj2);
                Console.WriteLine("Added to Treap, key: " + myKeyObj2.ToString());
                treap.Add(myKeyObj3.Key, myKeyObj3);
                Console.WriteLine("Added to Treap, key: " + myKeyObj3.ToString());
                treap.Add(myKeyObj4.Key, myKeyObj4);
                Console.WriteLine("Added to Treap, key: " + myKeyObj4.ToString());
                Console.WriteLine(Environment.NewLine);

                TraverseEnumerator();
                Console.WriteLine(Environment.NewLine);

                Console.WriteLine("- Clearing Treap -");
                Console.WriteLine(Environment.NewLine);
                treap.Clear();
                DumpTreap(true);
                Console.WriteLine(Environment.NewLine);

                Console.WriteLine("Press enter to terminate");
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine("Press enter to terminate");
                Console.ReadLine();
            }
        }