예제 #1
0
        static public bool SaveChain(List <long> chunkList, int chunkListIdx, int LastChunkId, byte[] data, int offset, ChunkAndChainManager chunkAndChainManager)
        {
            ChunkCB chunkCB;

            byte[] buf;
            bool   presentChunks     = false;
            int    savedChunkListIdx = chunkListIdx;
            int    idx;
            Chain  chain4Lookup;

            if (chunkAndChainManager.chainId4Lookup == 0)
            {
                GetChainAsByteArray(new long[0], out buf);
                chunkAndChainManager.chainId4Lookup = fileManager.AddUpdateChainFile(buf, chunkAndChainManager.chainId4Lookup);
//                LogUtility.LogUtility.LogFile("****created new chain " + Convert.ToString(chainId4Lookup), ModuleLogLevel);
                ChainsCreated++;
            }
            else
            {
//                LogUtility.LogUtility.LogFile("****updating chain " + Convert.ToString(chainId4Lookup), ModuleLogLevel);
                ChainsUpdated++;
            }

            for (; chunkListIdx <= LastChunkId; chunkListIdx++)
            {
#if false
                if (chunkMap.TryGetValue(chunkId, out chunkCB))
#else
                if (!GetChunkCB(chunkList[chunkListIdx], out chunkCB))
#endif
                {
                    chunkCB      = new ChunkCB();
                    chunkCB.sha1 = PackChunking.chunkToSha1(chunkList[chunkListIdx]);
                    chunkCB.size = (uint)PackChunking.chunkToLen(chunkList[chunkListIdx]);
                    fileManager.AddNewChunk(data, (uint)offset, chunkCB.size, chunkList[chunkListIdx], out chunkCB.fo);
#if false
                    {
                        byte[] buff2log = new byte[chunkCB.size];
                        for (int i = 0; i < chunkCB.size; i++)
                        {
                            buff2log[i] = data[i + offset];
                        }
                        LogUtility.LogUtility.LogBinary("_stored", buff2log);
                    }
#endif
                    chunkCB.hint           = PackChunking.GetChunkHint(data, (uint)offset, chunkCB.size);
                    chunkCB.chains         = new long[1];
                    chunkCB.chains[0]      = chunkAndChainManager.chainId4Lookup;
                    chunkCB.ChainsListSize = 1;
//                    LogUtility.LogUtility.LogFile("****creating new chunk CB " + Convert.ToString(chunkCB.size) + " " + Convert.ToString(chunkList[chunkListIdx]) + " len " + Convert.ToString(PackChunking.chunkToLen(chunkList[chunkListIdx])) + " hint " + Convert.ToString(chunkCB.hint) + " sha1 " + Convert.ToString(chunkCB.sha1), ModuleLogLevel);
                    ChunksCreated++;
                }
                else if (!ChainIsPresentInChunkCB(chunkCB, chunkAndChainManager.chainId4Lookup))
                {
                    presentChunks = true;
                    long[] newChainList = new long[chunkCB.chains.Length + 1];
                    chunkCB.chains.CopyTo(newChainList, 0);
                    newChainList[chunkCB.chains.Length] = chunkAndChainManager.chainId4Lookup;
                    chunkCB.chains = newChainList;
                    chunkCB.ChainsListSize++;
//                    LogUtility.LogUtility.LogFile("****updating existing chunk CB " + Convert.ToString(chunkCB.size) + " " + Convert.ToString(chunkList[chunkListIdx]) + " hint " + Convert.ToString(chunkCB.hint) + " sha1 " + Convert.ToString(chunkCB.sha1), ModuleLogLevel);
                    ChainAdded2ExistingChunk++;
                }
                else
                {
//                    LogUtility.LogUtility.LogFile("****chain exists in chunk CB " + Convert.ToString(chunkList[chunkListIdx]) + " " + Convert.ToString(PackChunking.chunkToLen(chunkList[chunkListIdx])) + " chain " + Convert.ToString(chainId4Lookup) + " hint " + Convert.ToString(chunkCB.hint) + " sha1 " + Convert.ToString(chunkCB.sha1), ModuleLogLevel);
                    offset += (int)chunkCB.size;
                    ChainExistInChunkChainsList++;
                    continue;
                }
                offset += (int)chunkCB.size;
                GetChunkCBAsByteArray(chunkCB, out buf);
                fileManager.AddUpdateChunkCtrlFile(chunkList[chunkListIdx], buf, 0, (uint)buf.Length);
            }
            if (!GetChain(chunkAndChainManager.chainId4Lookup, out chain4Lookup))
            {
                LogUtility.LogUtility.LogFile("****cannot get chain!!! " + Convert.ToString(chunkAndChainManager.chainId4Lookup), ModuleLogLevel);
                CannotGetChain++;
                return(presentChunks);
            }
//            LogUtility.LogUtility.LogFile("prepare chain, now " + Convert.ToString(chain4Lookup.NumberOfEntries) + " ( " + Convert.ToString(chain4Lookup.chunkIds.Length) + ")" + " will be added " + Convert.ToString(chunkList.Count-savedChunkListIdx), ModuleLogLevel);
            long[] newChunkList = new long[chain4Lookup.NumberOfEntries + ((LastChunkId + 1) - savedChunkListIdx)];
            chain4Lookup.chunkIds.CopyTo(newChunkList, 0);
            idx = (int)chain4Lookup.NumberOfEntries;
            for (chunkListIdx = savedChunkListIdx; chunkListIdx <= LastChunkId; chunkListIdx++)
            {
#if false
                if (ChunkIsPresentInChain(chain4Lookup, chunkList[chunkListIdx]))
                {
                    LogUtility.LogUtility.LogFile("SANITY CHECK: chunk " + Convert.ToString(chunkList[chunkListIdx]) + " len " + Convert.ToString(PackChunking.chunkToLen(chunkList[chunkListIdx])) + " is already in chain", ModuleLogLevel);
                    return(presentChunks);
                }
#endif
                newChunkList[idx++] = chunkList[chunkListIdx];
            }
            chain4Lookup.chunkIds        = newChunkList;
            chain4Lookup.NumberOfEntries = (uint)newChunkList.Length;
            GetChainAsByteArray(chain4Lookup.chunkIds, out buf);
            //save on disk
            chunkAndChainManager.chainId4Lookup = fileManager.AddUpdateChainFile(buf, chunkAndChainManager.chainId4Lookup);
            return(presentChunks);
        }
예제 #2
0
        void MatchChain(List <ChunkMetaData> predMsg)
        {
            try
            {
                uint matchLen         = 0;
                uint matchChunkCount  = 0;
                uint firstSenderIdx   = 0;
                uint firstReceiverIdx = 0;
                int  senderChunkIdx   = 0;
                int  receiverChunkIdx = 0;
                bool match            = false;
                int  offset           = 0;
                int  savedOffset      = 0;

                while ((senderChunkIdx < m_SenderChunkList.Count) && (receiverChunkIdx < predMsg.Count))
                {
                    byte senderHint  = PackChunking.GetChunkHint(m_data, (uint)offset, (uint)PackChunking.chunkToLen(m_SenderChunkList[senderChunkIdx]));
                    long senderChunk = PackChunking.chunkCode(0, PackChunking.chunkToLen(m_SenderChunkList[senderChunkIdx]));
                    switch (match)
                    {
                    case false:
                        LogUtility.LogUtility.LogFile(Convert.ToString(m_Id) + " in non-match " + Convert.ToString(PackChunking.chunkToLen(m_SenderChunkList[senderChunkIdx])), ModuleLogLevel);
                        receiverChunkIdx = (int)FindFirstMatchingChunk(predMsg, senderChunk, senderHint);
                        if (receiverChunkIdx != predMsg.Count)
                        {
                            LogUtility.LogUtility.LogFile(Convert.ToString(m_Id) + " match " + Convert.ToString(PackChunking.chunkToLen(predMsg[(int)receiverChunkIdx].chunk)), ModuleLogLevel);
                            match            = true;
                            firstReceiverIdx = (uint)receiverChunkIdx;
                            firstSenderIdx   = (uint)senderChunkIdx;
                            matchLen         = (uint)PackChunking.chunkToLen(m_SenderChunkList[senderChunkIdx]);
                            matchChunkCount  = 1;
                            savedOffset      = offset;
                            receiverChunkIdx++;
                        }
                        else
                        {
                            receiverChunkIdx = 0;
                        }
                        break;

                    case true:
                        LogUtility.LogUtility.LogFile(Convert.ToString(m_Id) + "in match " + Convert.ToString(PackChunking.chunkToLen(m_SenderChunkList[senderChunkIdx])), ModuleLogLevel);
                        if ((senderChunk != PackChunking.chunkCode(0, PackChunking.chunkToLen(predMsg[receiverChunkIdx].chunk))) ||
                            (senderHint != predMsg[receiverChunkIdx].hint))
                        {
                            match = false;
                            LogUtility.LogUtility.LogFile(Convert.ToString(m_Id) + "stopped. matching sha1 ", ModuleLogLevel);
                            OnEndOfMatch(predMsg, matchLen, matchChunkCount, firstSenderIdx, firstReceiverIdx, savedOffset);
                            if (matchLen >= (m_data.Length / 3))
                            {
                                //return;
                            }
                        }
                        else
                        {
                            matchLen += (uint)PackChunking.chunkToLen(m_SenderChunkList[senderChunkIdx]);
                            matchChunkCount++;
                            receiverChunkIdx++;
                            if (senderChunkIdx == (m_SenderChunkList.Count - 1))
                            {
                                LogUtility.LogUtility.LogFile(Convert.ToString(m_Id) + "stopped (end). matching sha1 ", ModuleLogLevel);
                                OnEndOfMatch(predMsg, matchLen, matchChunkCount, firstSenderIdx, firstReceiverIdx, savedOffset);
                                if (matchLen >= (m_data.Length / 3))
                                {
                                    //return;
                                }
                            }
                        }
                        break;
                    }
                    //LogUtility.LogUtility.LogFile("Sender's offset " + Convert.ToString(offset), ModuleLogLevel);
                    offset += (int)PackChunking.chunkToLen(m_SenderChunkList[senderChunkIdx]);
                    senderChunkIdx++;
                }
            }
            catch (Exception exc)
            {
                LogUtility.LogUtility.LogFile("EXCEPTION: " + exc.Message + " " + exc.StackTrace + " " + exc.InnerException, LogUtility.LogLevels.LEVEL_LOG_HIGH);
            }
        }