コード例 #1
0
 public void KeyIn54BitCheckGetNotFound()
 {
     try
     {
         var    hashTable = new KeyIn54BitCASHashTable(arrayLength, contentLength);
         byte[] output;
         Assert.AreEqual(hashTable.TryGet(2097151, 262143, 16383, out output), -2);
         Assert.IsNull(output);
     }
     catch (Exception ex)
     {
         if (log.IsFatalEnabled)
         {
             log.Fatal(ex.Message + "\t" + ex.StackTrace);
         }
     }
 }
コード例 #2
0
        public void KeyIn54BitCheckUpdateBringDeletedBack()
        {
            KeyIn54BitCASHashTable hashTable = null;

            try
            {
                hashTable = new KeyIn54BitCASHashTable(arrayLength, contentLength);
            }
            catch (Exception ex)
            {
                if (log.IsFatalEnabled)
                {
                    log.Fatal(ex.Message + "\t" + ex.StackTrace);
                }
            }

            var result = Utility.GetData().Result;

            int SBPCounter   = 0;
            var SBPHashTable = new ConcurrentDictionary <string, int>();
            int SBPId        = 0;

            foreach (var record in result.EmptyIfNull())
            {
                if (!SBPHashTable.ContainsKey(record.sbp))
                {
                    SBPId = Interlocked.Increment(ref SBPCounter);
                    SBPHashTable.TryAdd(record.sbp, SBPId);
                }
                else
                {
                    SBPId = SBPHashTable[record.sbp];
                }
                try
                {
                    Assert.AreEqual(hashTable.TrySet(record.linkId, record.clcId, SBPId, record.url), 0);
                }
                catch (Exception ex)
                {
                    if (log.IsFatalEnabled)
                    {
                        log.Fatal(ex.Message + "\t" + ex.StackTrace);
                    }
                }
            }

            string message = string.Empty;

            byte[] output;

            foreach (var record in result.EmptyIfNull())
            {
                SBPId = SBPHashTable[record.sbp];
                try
                {
                    Assert.AreEqual(hashTable.TryDelete(record.linkId, record.clcId, SBPId), 0);
                }
                catch (Exception ex)
                {
                    if (log.IsFatalEnabled)
                    {
                        log.Fatal(ex.Message + "\t" + ex.StackTrace);
                    }
                }
                output = null;
                try
                {
                    Assert.AreEqual(hashTable.TryGet(record.linkId, record.clcId, SBPId, out output), -3);
                }
                catch (Exception ex)
                {
                    if (log.IsFatalEnabled)
                    {
                        log.Fatal(ex.Message + "\t" + ex.StackTrace);
                    }
                }
                Assert.IsNull(output);
            }

            var u8 = Encoding.UTF8;

            //Update all the records again.
            foreach (var record in result.EmptyIfNull())
            {
                SBPId = SBPHashTable[record.sbp];
                try
                {
                    Assert.AreEqual(hashTable.TrySet(record.linkId, record.clcId, SBPId, record.url), 1);
                }
                catch (Exception ex)
                {
                    if (log.IsFatalEnabled)
                    {
                        log.Fatal(ex.Message + "\t" + ex.StackTrace);
                    }
                }
            }

            //We should get all the URL again.
            output = null;
            foreach (var record in result.EmptyIfNull())
            {
                SBPId = SBPHashTable[record.sbp];
                try
                {
                    Assert.AreEqual(hashTable.TryGet(record.linkId, record.clcId, SBPId, out output), 0);
                }
                catch (Exception ex)
                {
                    if (log.IsFatalEnabled)
                    {
                        log.Fatal(ex.Message + "\t" + ex.StackTrace);
                    }
                }
                Assert.AreEqual(u8.GetString(record.url), u8.GetString(output));
            }
        }
コード例 #3
0
        public void KeyIn54BitCheckUpdate()
        {
            KeyIn54BitCASHashTable hashTable = null;

            try
            {
                hashTable = new KeyIn54BitCASHashTable(arrayLength, contentLength);
            }
            catch (Exception ex)
            {
                if (log.IsFatalEnabled)
                {
                    log.Fatal(ex.Message + "\t" + ex.StackTrace);
                }
            }
            var result = Utility.GetDataArrayList().Result;

            int SBPCounter   = 0;
            var SBPHashTable = new ConcurrentDictionary <string, int>();
            int SBPId        = 0;

            for (var i = 0; i < result.Count; i++)
            {
                if (!SBPHashTable.ContainsKey(((Record)result[i]).sbp))
                {
                    SBPId = Interlocked.Increment(ref SBPCounter);
                    SBPHashTable.TryAdd(((Record)result[i]).sbp, SBPId);
                }
                else
                {
                    SBPId = SBPHashTable[((Record)result[i]).sbp];
                }
                try
                {
                    Assert.AreEqual(hashTable.TrySet(((Record)result[i]).linkId, ((Record)result[i]).clcId, SBPId, ((Record)result[i]).url), 0);
                }
                catch (Exception ex)
                {
                    if (log.IsFatalEnabled)
                    {
                        log.Fatal(ex.Message + "\t" + ex.StackTrace);
                    }
                }
            }

            var temp = new byte[hashTable.ContentLength];

            for (int i = 0; i < hashTable.ContentLength; i++)
            {
                temp[i] = 0;
            }

            for (var i = 0; i < result.Count; i++)
            {
                for (var j = 0; j < ((Record)result[i]).url.Length; j++)
                {
                    ((Record)result[i]).url[j] = 0xFF;
                }
            }

            var u8 = Encoding.UTF8;

            byte[] output = null;

            for (var i = 0; i < result.Count; i++)
            {
                SBPId = SBPHashTable[((Record)result[i]).sbp];
                try
                {
                    Assert.AreEqual(hashTable.TryGet(((Record)result[i]).linkId, ((Record)result[i]).clcId, SBPId, out output), 0);
                }
                catch (Exception ex)
                {
                    if (log.IsFatalEnabled)
                    {
                        log.Fatal(ex.Message + "\t" + ex.StackTrace);
                    }
                }
                if (((Record)result[i]).url.Length == 0)
                {
                    Assert.AreNotEqual("haha", u8.GetString(output));
                }
                else
                {
                    Assert.AreNotEqual(u8.GetString(((Record)result[i]).url), u8.GetString(output));
                }
                output = null;
            }

            for (var i = 0; i < result.Count; i++)
            {
                SBPId = SBPHashTable[((Record)result[i]).sbp];
                try
                {
                    Assert.AreEqual(hashTable.TrySet(((Record)result[i]).linkId, ((Record)result[i]).clcId, SBPId, ((Record)result[i]).url), 1);
                }
                catch (Exception ex)
                {
                    if (log.IsFatalEnabled)
                    {
                        log.Fatal(ex.Message + "\t" + ex.StackTrace);
                    }
                }
            }

            for (var i = 0; i < result.Count; i++)
            {
                SBPId = SBPHashTable[((Record)result[i]).sbp];
                try
                {
                    Assert.AreEqual(hashTable.TryGet(((Record)result[i]).linkId, ((Record)result[i]).clcId, SBPId, out output), 0);
                }
                catch (Exception ex)
                {
                    if (log.IsFatalEnabled)
                    {
                        log.Fatal(ex.Message + "\t" + ex.StackTrace);
                    }
                }
                Assert.AreEqual(u8.GetString(((Record)result[i]).url), u8.GetString(output));
                output = null;
            }
        }
コード例 #4
0
        public void KeyIn54BitCheckDeleteAgain()
        {
            var stopWatch = new Stopwatch();
            KeyIn54BitCASHashTable hashTable = null;

            try
            {
                hashTable = new KeyIn54BitCASHashTable(arrayLength, contentLength);
            }
            catch (Exception ex)
            {
                if (log.IsFatalEnabled)
                {
                    log.Fatal(ex.Message + "\t" + ex.StackTrace);
                }
            }
            var result = Utility.GetData().Result;

            int SBPCounter   = 0;
            var SBPHashTable = new ConcurrentDictionary <string, int>();
            int SBPId        = 0;

            foreach (var record in result.EmptyIfNull())
            {
                if (!SBPHashTable.ContainsKey(record.sbp))
                {
                    SBPId = Interlocked.Increment(ref SBPCounter);
                    SBPHashTable.TryAdd(record.sbp, SBPId);
                }
                else
                {
                    SBPId = SBPHashTable[record.sbp];
                }
                try
                {
                    Assert.AreEqual(hashTable.TrySet(record.linkId, record.clcId, SBPId, record.url), 0);
                }
                catch (Exception ex)
                {
                    if (log.IsFatalEnabled)
                    {
                        log.Fatal(ex.Message + "\t" + ex.StackTrace);
                    }
                }
            }

            byte[] output;

            foreach (var record in result.EmptyIfNull())
            {
                SBPId = SBPHashTable[record.sbp];
                try
                {
                    Assert.AreEqual(hashTable.TryDelete(record.linkId, record.clcId, SBPId), 0);
                }
                catch (Exception ex)
                {
                    if (log.IsFatalEnabled)
                    {
                        log.Fatal(ex.Message + "\t" + ex.StackTrace);
                    }
                }
                output = null;

                try
                {
                    Assert.AreEqual(hashTable.TryGet(record.linkId, record.clcId, SBPId, out output), -3);
                }
                catch (Exception ex)
                {
                    if (log.IsFatalEnabled)
                    {
                        log.Fatal(ex.Message + "\t" + ex.StackTrace);
                    }
                }
                Assert.IsNull(output);
            }

            output = null;
            //Delete all the records again and nothing should happens.
            foreach (var record in result.EmptyIfNull())
            {
                SBPId = SBPHashTable[record.sbp];
                try
                {
                    Assert.AreEqual(hashTable.TryDelete(record.linkId, record.clcId, SBPId), -3);
                }
                catch (Exception ex)
                {
                    if (log.IsFatalEnabled)
                    {
                        log.Fatal(ex.Message + "\t" + ex.StackTrace);
                    }
                }

                output = null;
                try
                {
                    Assert.AreEqual(hashTable.TryGet(record.linkId, record.clcId, SBPId, out output), -3);
                }
                catch (Exception ex)
                {
                    if (log.IsFatalEnabled)
                    {
                        log.Fatal(ex.Message + "\t" + ex.StackTrace);
                    }
                }
                Assert.IsNull(output);
            }
        }
コード例 #5
0
        public void KeyIn54BitCheckAddTotalSync()
        {
            var stopWatch = new Stopwatch();
            KeyIn54BitCASHashTable hashTable = null;

            try
            {
                hashTable = new KeyIn54BitCASHashTable(arrayLength, contentLength);
            }
            catch (Exception ex)
            {
                if (log.IsFatalEnabled)
                {
                    log.Fatal(ex.Message + "\t" + ex.StackTrace);
                }
            }
            var ts = stopWatch.Elapsed;

            Console.WriteLine("Create hash table" + "\t" + ts.ToString());

            stopWatch.Restart();
            var result = Utility.GetData().Result;

            stopWatch.Stop();
            Console.WriteLine("Get " + result.Count + " data from database" + "\t" + stopWatch.Elapsed.ToString());

            int SBPCounter   = 0;
            var SBPHashTable = new ConcurrentDictionary <string, int>();
            int SBPId        = 0;

            stopWatch.Restart();
            foreach (var record in result.EmptyIfNull())
            {
                if (!SBPHashTable.ContainsKey(record.sbp))
                {
                    SBPId = Interlocked.Increment(ref SBPCounter);
                    SBPHashTable.TryAdd(record.sbp, SBPId);
                }
                else
                {
                    SBPId = SBPHashTable[record.sbp];
                }
                try
                {
                    Assert.AreEqual(hashTable.TrySet(record.linkId, record.clcId, SBPId, record.url), 0);
                }
                catch (Exception ex)
                {
                    if (log.IsFatalEnabled)
                    {
                        log.Fatal(ex.Message + "\t" + ex.StackTrace);
                    }
                }
            }
            stopWatch.Stop();
            Console.WriteLine("Add all records" + "\t" + stopWatch.Elapsed.ToString());

            var u8 = Encoding.UTF8;

            byte[] output = null;

            foreach (var record in result)
            {
                SBPId = SBPHashTable[record.sbp];
                try
                {
                    Assert.AreEqual(hashTable.TryGet(record.linkId, record.clcId, SBPId, out output), 0);
                }
                catch (Exception ex)
                {
                    if (log.IsFatalEnabled)
                    {
                        log.Fatal(ex.Message + "\t" + ex.StackTrace);
                    }
                }
                Assert.AreEqual(u8.GetString(record.url), u8.GetString(output));
                output = null;
            }
        }
コード例 #6
0
        public void KeyIn54BitCheckGetDeleted()
        {
            KeyIn54BitCASHashTable hashTable = null;

            try
            {
                hashTable = new KeyIn54BitCASHashTable(arrayLength, contentLength);
            }
            catch (Exception ex)
            {
                if (log.IsFatalEnabled)
                {
                    log.Fatal(ex.Message + "\t" + ex.StackTrace);
                }
            }
            var result = Utility.GetData().Result;

            int SBPCounter   = 0;
            var SBPHashTable = new ConcurrentDictionary <string, int>();
            int SBPId        = 0;
            int i            = 0;

            long linkIdTemp = 0;
            long clcIdTemp  = 0;
            long SBPIdTemp  = 0;

            foreach (var record in result.EmptyIfNull())
            {
                if (!SBPHashTable.ContainsKey(record.sbp))
                {
                    SBPId = Interlocked.Increment(ref SBPCounter);
                    SBPHashTable.TryAdd(record.sbp, SBPId);
                }
                else
                {
                    SBPId = SBPHashTable[record.sbp];
                }

                try
                {
                    Assert.AreEqual(hashTable.TrySet(record.linkId, record.clcId, SBPId, record.url), 0);
                }
                catch (Exception ex)
                {
                    if (log.IsFatalEnabled)
                    {
                        log.Fatal(ex.Message + "\t" + ex.StackTrace);
                    }
                }

                if (i++ == 1)
                {
                    linkIdTemp = record.linkId;
                    clcIdTemp  = record.clcId;
                    SBPIdTemp  = SBPId;
                }
            }

            try
            {
                Assert.AreEqual(hashTable.TryDelete(linkIdTemp, clcIdTemp, SBPIdTemp), 0);
                byte[] output = null;
                Assert.AreEqual(hashTable.TryGet(linkIdTemp, clcIdTemp, SBPIdTemp, out output), -3);
                Assert.IsNull(output);
            }
            catch (Exception ex)
            {
                if (log.IsFatalEnabled)
                {
                    log.Fatal(ex.Message + "\t" + ex.StackTrace);
                }
            }
        }
コード例 #7
0
        public void KeyIn54BitCheckGetInvlidInput()
        {
            KeyIn54BitCASHashTable hashTable = null;

            try
            {
                hashTable = new KeyIn54BitCASHashTable(arrayLength, contentLength);
            }
            catch (Exception ex)
            {
                if (log.IsFatalEnabled)
                {
                    log.Fatal(ex.Message + "\t" + ex.StackTrace);
                }
            }

            byte[] output;

            try
            {
                string message = null;
                try { hashTable.TryGet(0, 1, 1, out output); }
                catch (ArgumentOutOfRangeException e)
                { message = e.Message; }
                Assert.IsTrue(message.Contains("linkId"));

                message = null;
                try { hashTable.TryGet(4194304, 1, 1, out output); }
                catch (ArgumentOutOfRangeException e)
                { message = e.Message; }
                Assert.IsTrue(message.Contains("linkId"));

                message = null;
                try { hashTable.TryGet(1, 0, 1, out output); }
                catch (ArgumentOutOfRangeException e)
                { message = e.Message; }
                Assert.IsTrue(message.Contains("clcId"));

                message = null;
                try { hashTable.TryGet(1, 262144, 1, out output); }
                catch (ArgumentOutOfRangeException e)
                { message = e.Message; }
                Assert.IsTrue(message.Contains("clcId"));

                message = null;
                try { hashTable.TryGet(1, 1, 0, out output); }
                catch (ArgumentOutOfRangeException e)
                { message = e.Message; }
                Assert.IsTrue(message.Contains("sbp"));

                message = null;
                try { hashTable.TryGet(1, 1, 16384, out output); }
                catch (ArgumentOutOfRangeException e)
                { message = e.Message; }
                Assert.IsTrue(message.Contains("sbp"));
            }
            catch (Exception ex)
            {
                if (log.IsFatalEnabled)
                {
                    log.Fatal(ex.Message + "\t" + ex.StackTrace);
                }
            }
        }