从文件中间删除内存 [英] Remove memory from the middle of a file

查看:176
本文介绍了从文件中间删除内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二进制格式,如下所示:

I have a binary format which is build up like that:

magic number

name size blob
name size blob
name size blob
...

它是构建,以便轻松移动通过文件,并找到正确的条目。但我也想删除一个条目(我们称之为一个块,因为它是一个)。我想我可以使用std :: copy / memmove与一些iostream迭代器移动块后面的删除和复制他们超过要删除的块。但是,然后我有我删除的空间,充满了不可用的数据(我可以填充它的零或否)。我可能会收缩文件后。

it is build up to easy move through the file and find the right entry. But I would like also to remove an entry (let's call it a chunk as it is one). I guess I can use std::copy/memmove with some iostream iterators to move the chunks behind the one to delete and copy them over the chunk to delete. But then I have the space I deleted at the end filled with unusable data(I could fill it up with zeros or not). I likely would shrink the file afterwards.

我知道我可以读取整个数据,我想保存在一个缓冲区,并把它放入一个新的文件,但我不喜欢

I know I can read the whole data that I want to keep in a buffer and put it into a new file, but I dislike it to rewrite the whole file for deleting just one chunk.

有关删除文件中数据的最佳方式的任何想法吗?

Any ideas for the best way of removing data in a file?

推荐答案


我知道我可以读取我想要保存在缓冲区中的所有数据,

I know I can read the whole data that I want to keep in a buffer and put it into a new file, but I dislike it to rewrite the whole file for deleting just one chunk.

有关删除文件中数据的最佳方法的任何想法?

Any ideas for the best way of removing data in a file?

你不能拥有两个世界的最好的。如果你想保留空间,你需要一些东西来描述文件部分(让它称为分配表),每个文件部分由片段序列组成。

You can't have the best of both worlds. If you want to preserve space, you will need something to describe the file sections (lets call it an allocation table), with each file sections consisting of sequence of shards).

一个段将正常开始(一个分片),但是一旦解除分配,解除分配的段将作为新段的分片的一部分提供。现在可以选择在什么时间点,你愿意生活在分片(不连续)节(可能只有在你的文件达到一定的大小限制后)。

A section would start of normally (one shard), but as soon as it is de-allocated, the de-allocated section will be made available as part of a shard for a new section. One can now choose at what point in time you are willing to live with sharded (non-contiguous) sections (perhaps only after your file reaches a certain size limit).

分配表将每个部分描述为严重(链接列表)的分片(或一个分片,如果连续)。可以为分配表保留固定大小,也可以将其放在不同的文件中,或者将其分片,并赋予其重建自身的能力。

The allocation table describes each section as a serious (link list) of shards (or one shard, if contiguous). One could either preserve a fixed size for the allocation table, or have it in a different file, or shard it and give it the ability to reconstruct itself.

struct Section
{
  struct Shard
  {
    std::size_t baseAddr_;
    std::size_t size_;
  };
  std::string name_;
  std::size_t shardCount_;
  std::vector<Shard> shards_;

  istream& readFrom( std::istream& );
};

struct AllocTable
{
  std::size_t sectionCount_;
  std::vector<Section> sections_;
  std::size_t next_;


  istream& readFrom( std::istream& is, AllocTable* previous )
  {
    //Brief code... error handling left as your exercise
    is >> sectionCount_;

    sections_.resize( sectionCount_ );
    for( std::size_t i = 0; i < sectionCount_; ++i ) 
    {
      sections_[i].readFrom( is );
    }
    is >> next_; //Note - no error handling for brevity
    if( next_ != static_cast<std::size_t>(-1) )
    {
      is.seekg( next_ ); //Seek to next_ from file beginning
      AllocTable nextTable;
      nextTable.readFrom( is, this );
      sections_.insert( sections_.end(), 
        nextTable.sections_.begin(), table_.sections_.end() );
    }
    return is;
  }
};

...

这篇关于从文件中间删除内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆