예제 #1
0
        protected override IBlockStore GetBlockStore(BlockId blockId)
        {
            // If it's not found in the map,
            // Turn the block id into a filename,
            string      blockFname    = FormatFileName(blockId);
            string      blockFileName = Path.Combine(path, blockFname + ".mcd");
            IBlockStore blockStore;

            if (!File.Exists(blockFileName))
            {
                blockFileName = Path.Combine(path, blockFname);
                blockStore    = new FileBlockStore(blockId, blockFileName);
            }
            else
            {
                blockStore = new CompressedBlockStore(blockId, blockFileName);
            }

            return(blockStore);
        }
예제 #2
0
        protected override BlockContainer LoadBlock(long blockId)
        {
            // If it's not found in the map,
            // Turn the block id into a filename,
            String block_fname = blockId.ToString();
            string block_file_name = Path.Combine(path, block_fname + ".mcd");
            IBlockStore block_store;
            if (!File.Exists(block_file_name)) {
                block_file_name = Path.Combine(path, block_fname);
                // If this file doesn't exist,
                if (!File.Exists(block_file_name)) {
                    // We check if the block_id is less than maximum id. If it is we
                    // generate an exception indicating this block doesn't exist on this
                    // service. This means something screwed up, either the manager service
                    // was erroneously told the block was located on this service but it
                    // isn't, or the file was deleted by the user.
                    if (blockId < LastBlockId)
                        throw new BlockReadException("Block " + blockId + " not stored on service");
                }

                block_store = new FileBlockStore(blockId, block_file_name);
            } else {
                block_store = new CompressedBlockStore(blockId, block_file_name);
            }

            // Make the block container object,
            BlockContainer container = new BlockContainer(blockId, block_store);

            // Add the new container to the control list (used by the compression
            // thread).
            lock (compressionAddList) {
                compressionAddList.Add(container);
            }

            return container;
        }
예제 #3
0
        protected override IBlockStore GetBlockStore(BlockId blockId)
        {
            // If it's not found in the map,
            // Turn the block id into a filename,
            string blockFname = FormatFileName(blockId);
            string blockFileName = Path.Combine(path, blockFname + ".mcd");
            IBlockStore blockStore;
            if (!File.Exists(blockFileName)) {
                blockFileName = Path.Combine(path, blockFname);
                blockStore = new FileBlockStore(blockId, blockFileName);
            } else {
                blockStore = new CompressedBlockStore(blockId, blockFileName);
            }

            return blockStore;
        }
예제 #4
0
            private void Execute()
            {
                try {
                    lock (this) {
                        // Wait 2 seconds,
                        Monitor.Wait(2000);

                        // Any new block containers added, we need to process,
                        List <BlockContainer> newItems = new List <BlockContainer>();

                        while (true)
                        {
                            lock (service.compressionAddList) {
                                newItems.AddRange(service.compressionAddList);
                                service.compressionAddList.Clear();
                            }

                            // Sort the container list,
                            newItems.Sort();
                            newItems.Reverse();

                            for (int i = newItems.Count - 1; i >= 0; i--)
                            {
                                BlockContainer container = newItems[i];

                                // If it's already compressed, remove it from the list
                                if (container.IsCompressed)
                                {
                                    newItems.RemoveAt(i);
                                }
                                // Don't compress if written to less than 3 minutes ago,
                                // and we confirm it can be compressed,
                                else if (service.IsKnownStaticBlock(container))
                                {
                                    FileBlockStore mblockStore = (FileBlockStore)container.Store;
                                    string         sourcef     = mblockStore.FileName;
                                    string         destf       = Path.Combine(Path.GetDirectoryName(sourcef),
                                                                              Path.GetFileName(sourcef) + ".tempc");
                                    try {
                                        File.Delete(destf);
                                        service.Logger.Info(String.Format("Compressing block: {0}", container.Id));
                                        service.Logger.Info(String.Format("Current block size = {0}", sourcef.Length));

                                        // Compress the file,
                                        CompressedBlockStore.Compress(sourcef, destf);

                                        // Rename the file,
                                        string compressedf = Path.Combine(Path.GetDirectoryName(sourcef),
                                                                          Path.GetFileName(sourcef) + ".mcd");
                                        File.Move(destf, compressedf);

                                        // Switch the block container,
                                        container.ChangeStore(new CompressedBlockStore(container.Id, compressedf));

                                        service.Logger.Info(String.Format("Compression of block {0} finished.", container.Id));
                                        service.Logger.Info(String.Format("Compressed block size = {0}", compressedf.Length));
                                        // Wait a little bit and delete the original file,
                                        if (finished)
                                        {
                                            hasFinished = true;
                                            Monitor.PulseAll(this);
                                            return;
                                        }

                                        Monitor.Wait(this, 1000);

                                        // Delete the file after 5 minutes,
                                        new Timer(FileDelete, sourcef, 5 * 60 * 1000, Timeout.Infinite);

                                        // Remove it from the new_items list
                                        newItems.RemoveAt(i);
                                    } catch (IOException e) {
                                        service.Logger.Error("IO Error in compression thread", e);
                                    }
                                }

                                if (finished)
                                {
                                    hasFinished = true;
                                    Monitor.PulseAll(this);
                                    return;
                                }

                                Monitor.Wait(this, 200);
                            }


                            if (finished)
                            {
                                hasFinished = true;
                                Monitor.PulseAll(this);
                                return;
                            }

                            Monitor.Wait(this, 3000);
                        }
                    }
                } catch (ThreadInterruptedException) {
                    // ThreadInterruptedException causes the thread to end,
                }
            }