private void WriteCount(SequentialFileWriter indexFile, int count)
        {
            byte[] buffer = new byte[4];

            buffer[0] = (byte)(count);
            buffer[1] = (byte)(count >> 8);
            buffer[2] = (byte)(count >> 16);
            buffer[3] = (byte)(count >> 24);

            indexFile.Write(buffer, 0, buffer.Length);
        }
        private void OpenFiles()
        {
            itemData  = new SequentialFileWriter(World.ItemDataPath, metrics);
            itemIndex = new SequentialFileWriter(World.ItemIndexPath, metrics);

            mobileData  = new SequentialFileWriter(World.MobileDataPath, metrics);
            mobileIndex = new SequentialFileWriter(World.MobileIndexPath, metrics);

            guildData  = new SequentialFileWriter(World.GuildDataPath, metrics);
            guildIndex = new SequentialFileWriter(World.GuildIndexPath, metrics);

            WriteCount(itemIndex, World.Items.Count);
            WriteCount(mobileIndex, World.Mobiles.Count);
            WriteCount(guildIndex, BaseGuild.List.Count);
        }
Пример #3
0
        public int CommitTo(SequentialFileWriter dataFile, SequentialFileWriter indexFile, int typeCode, int serial)
        {
            Flush();

            byte[] buffer = stream.GetBuffer();
            int    length = (int)stream.Length;

            long position = dataFile.Position;

            dataFile.Write(buffer, 0, length);

            if (indexBuffer == null)
            {
                indexBuffer = new byte[20];
            }

            indexBuffer[0] = (byte)(typeCode);
            indexBuffer[1] = (byte)(typeCode >> 8);
            indexBuffer[2] = (byte)(typeCode >> 16);
            indexBuffer[3] = (byte)(typeCode >> 24);

            indexBuffer[4] = (byte)(serial);
            indexBuffer[5] = (byte)(serial >> 8);
            indexBuffer[6] = (byte)(serial >> 16);
            indexBuffer[7] = (byte)(serial >> 24);

            indexBuffer[8]  = (byte)(position);
            indexBuffer[9]  = (byte)(position >> 8);
            indexBuffer[10] = (byte)(position >> 16);
            indexBuffer[11] = (byte)(position >> 24);
            indexBuffer[12] = (byte)(position >> 32);
            indexBuffer[13] = (byte)(position >> 40);
            indexBuffer[14] = (byte)(position >> 48);
            indexBuffer[15] = (byte)(position >> 56);

            indexBuffer[16] = (byte)(length);
            indexBuffer[17] = (byte)(length >> 8);
            indexBuffer[18] = (byte)(length >> 16);
            indexBuffer[19] = (byte)(length >> 24);

            indexFile.Write(indexBuffer, 0, indexBuffer.Length);

            stream.SetLength(0);

            return(length);
        }
        private Task StartCommitTask(BlockingCollection <QueuedMemoryWriter> threadWriter, SequentialFileWriter data, SequentialFileWriter index)
        {
            Task commitTask = Task.Factory.StartNew(() =>
            {
                while (!(threadWriter.IsCompleted))
                {
                    QueuedMemoryWriter writer;

                    try
                    {
                        writer = threadWriter.Take();
                    }
                    catch (InvalidOperationException)
                    {
                        //Per MSDN, it's fine if we're here, successful completion of adding can rarely put us into this state.
                        break;
                    }

                    writer.CommitTo(data, index);
                }
            });

            return(commitTask);
        }
        public void CommitTo(SequentialFileWriter dataFile, SequentialFileWriter indexFile)
        {
            this.Flush();

            int memLength = (int)_memStream.Position;

            if (memLength > 0)
            {
                byte[] memBuffer = _memStream.GetBuffer();

                long actualPosition = dataFile.Position;

                dataFile.Write(memBuffer, 0, memLength);    //The buffer contains the data from many items.

                //Console.WriteLine("Writing {0} bytes starting at {1}, with {2} things", memLength, actualPosition, _orderedIndexInfo.Count);

                byte[] indexBuffer = new byte[20];

                //int indexWritten = _orderedIndexInfo.Count * indexBuffer.Length;
                //int totalWritten = memLength + indexWritten

                for (int i = 0; i < _orderedIndexInfo.Count; i++)
                {
                    IndexInfo info = _orderedIndexInfo[i];

                    int typeCode = info.typeCode;
                    int serial   = info.serial;
                    int length   = info.size;


                    indexBuffer[0] = (byte)(info.typeCode);
                    indexBuffer[1] = (byte)(info.typeCode >> 8);
                    indexBuffer[2] = (byte)(info.typeCode >> 16);
                    indexBuffer[3] = (byte)(info.typeCode >> 24);

                    indexBuffer[4] = (byte)(info.serial);
                    indexBuffer[5] = (byte)(info.serial >> 8);
                    indexBuffer[6] = (byte)(info.serial >> 16);
                    indexBuffer[7] = (byte)(info.serial >> 24);

                    indexBuffer[8]  = (byte)(actualPosition);
                    indexBuffer[9]  = (byte)(actualPosition >> 8);
                    indexBuffer[10] = (byte)(actualPosition >> 16);
                    indexBuffer[11] = (byte)(actualPosition >> 24);
                    indexBuffer[12] = (byte)(actualPosition >> 32);
                    indexBuffer[13] = (byte)(actualPosition >> 40);
                    indexBuffer[14] = (byte)(actualPosition >> 48);
                    indexBuffer[15] = (byte)(actualPosition >> 56);

                    indexBuffer[16] = (byte)(info.size);
                    indexBuffer[17] = (byte)(info.size >> 8);
                    indexBuffer[18] = (byte)(info.size >> 16);
                    indexBuffer[19] = (byte)(info.size >> 24);

                    indexFile.Write(indexBuffer, 0, indexBuffer.Length);

                    actualPosition += info.size;
                }
            }

            this.Close();   //We're done with this writer.
        }