求boost序列化库对于磁盘空间不足的处理方案

zhouzhenyan 2013-03-10 06:35:51
使用boost序列化库实现数据对象的存储,如果在未完全写入的时候由于磁盘空间不足而导致出现异常,而此时的文件已经写入一部分,即造成了文件的不完整。请问有没有什么解决方案来处理这个问题?
...全文
312 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhouzhenyan 2013-03-13
  • 打赏
  • 举报
回复
引用 14 楼 ForestDB 的回复:
写到内存先,然后判断大小,看disk空间还够。
这个办法我也想过,但太费内存。
e_play 2013-03-12
  • 打赏
  • 举报
回复
先写log,后写库,数据库都是这么干的
ForestDB 2013-03-12
  • 打赏
  • 举报
回复
写到内存先,然后判断大小,看disk空间还够。
赵4老师 2013-03-12
  • 打赏
  • 举报
回复
特供楼主一个Windows下百试不爽的快速腾出磁盘空间的妙招: 用资源管理器全盘搜索文件大小至少比如10MB的文件,将搜索结果按从大到小排序,看前面的大文件,删除几个确定没有用处的文件。
赵4老师 2013-03-12
  • 打赏
  • 举报
回复
引用 10 楼 zhao4zhong2 的回复:
写入前先备份文件,成功则删除备份,失败则恢复备份,这才是成功之道! 以冗余保证数据安全,是赵老师一直以来苦口婆心的教导! 少年啊,要时刻以牢记赵老师的话而感到自豪!
弓虽丁页! 不要企图优雅的结束(因为这是不可能办到的) 而要在烂的不能再烂的摊子上也能重整河山!
mujiok2003 2013-03-11
  • 打赏
  • 举报
回复
引用 6 楼 zhouzhenyan 的回复:
引用 5 楼 mujiok2003 的回复:引用 4 楼 zhouzhenyan 的回复: 引用 3 楼 mujiok2003 的回复:原子操作:按整个对象序列化,而不是对象的字段逐个序列化。 要么全完成,要么删除部分写入的内容。 部分对象写入成功,部分不成功,那整个文件还是破坏了,还是不能在出错时恢复到写入之前的状态。要是有一种类似于数据库中事务处理的办法就好了。 ……
流对象都有缓冲,确保不把部分序列号的对象提交到物理文件就可以了。
zhouzhenyan 2013-03-11
  • 打赏
  • 举报
回复
引用 5 楼 mujiok2003 的回复:
引用 4 楼 zhouzhenyan 的回复: 引用 3 楼 mujiok2003 的回复:原子操作:按整个对象序列化,而不是对象的字段逐个序列化。 要么全完成,要么删除部分写入的内容。 部分对象写入成功,部分不成功,那整个文件还是破坏了,还是不能在出错时恢复到写入之前的状态。要是有一种类似于数据库中事务处理的办法就好了。 这个可以实现的呀。
“要么全完成,要么删除部分写入的内容”,这个怎么实现,发生异常时,原始的数据已经不再存在,如何恢复?
mujiok2003 2013-03-11
  • 打赏
  • 举报
回复
引用 4 楼 zhouzhenyan 的回复:
引用 3 楼 mujiok2003 的回复:原子操作:按整个对象序列化,而不是对象的字段逐个序列化。 要么全完成,要么删除部分写入的内容。 部分对象写入成功,部分不成功,那整个文件还是破坏了,还是不能在出错时恢复到写入之前的状态。要是有一种类似于数据库中事务处理的办法就好了。
这个可以实现的呀。
zhouzhenyan 2013-03-11
  • 打赏
  • 举报
回复
引用 3 楼 mujiok2003 的回复:
原子操作:按整个对象序列化,而不是对象的字段逐个序列化。 要么全完成,要么删除部分写入的内容。
部分对象写入成功,部分不成功,那整个文件还是破坏了,还是不能在出错时恢复到写入之前的状态。要是有一种类似于数据库中事务处理的办法就好了。
zhao4zhong2 2013-03-11
  • 打赏
  • 举报
回复
写入前先备份文件,成功则删除备份,失败则恢复备份,这才是成功之道! 以冗余保证数据安全,是赵老师一直以来苦口婆心的教导! 少年啊,要时刻以牢记赵老师的话而感到自豪!
zhouzhenyan 2013-03-11
  • 打赏
  • 举报
回复
引用 8 楼 zhao4zhong1 的回复:
写盘前判断磁盘剩余空间。 Linux: system("df >df.txt"); //然后读文件df.txt的内容。 Windows: GetDiskFreeSpace The GetDiskFreeSpace function retrieves information about the specified disk, including the amount of free s……
关键是写前无法判断要写入数据的总大小。还有,如果写入过程中不是磁盘满的异常,而是其他异常呢?要找一种当写入过程中发生异常时,能够恢复到写入前原始状态的方法。可能写入某个对象数据时出错了,那么前面那些对象都已经成功写入了,此时不是简单恢复到该出错对象没写入的状态,而是还要使得前面那些已经写入的对象也恢复到他们的原始状态。
赵4老师 2013-03-11
  • 打赏
  • 举报
回复
写盘前判断磁盘剩余空间。 Linux: system("df >df.txt"); //然后读文件df.txt的内容。 Windows: GetDiskFreeSpace The GetDiskFreeSpace function retrieves information about the specified disk, including the amount of free space on the disk. This function has been superseded by the GetDiskFreeSpaceEx function. New Win32-based applications should use GetDiskFreeSpaceEx. BOOL GetDiskFreeSpace( LPCTSTR lpRootPathName, // pointer to root path LPDWORD lpSectorsPerCluster, // pointer to sectors per cluster LPDWORD lpBytesPerSector, // pointer to bytes per sector LPDWORD lpNumberOfFreeClusters, // pointer to number of free clusters LPDWORD lpTotalNumberOfClusters // pointer to total number of clusters ); Parameters lpRootPathName Pointer to a null-terminated string that specifies the root directory of the disk to return information about. If lpRootPathName is NULL, the function uses the root of the current directory. If this parameter is a UNC name, you must follow it with an additional backslash. For example, you would specify \\MyServer\MyShare as \\MyServer\MyShare\. Windows 95: The initial release of Windows 95 does not support UNC paths for the lpszRootPathName parameter. To query the free disk space using a UNC path, temporarily map the UNC path to a drive letter, query the free disk space on the drive, then remove the temporary mapping. Windows 95 OSR2 and later: UNC paths are supported. lpSectorsPerCluster Pointer to a variable for the number of sectors per cluster. lpBytesPerSector Pointer to a variable for the number of bytes per sector. lpNumberOfFreeClusters Pointer to a variable for the total number of free clusters on the disk that are available to the user associated with the calling thread. Windows NT 5.0 and later: If per-user disk quotas are in use, this value may be less than the total number of free clusters on the disk. lpTotalNumberOfClusters Pointer to a variable for the total number of clusters on the disk that are available to the user associated with the calling thread. Windows NT 5.0 and later: If per-user disk quotas are in use, this value may be less than the total number of clusters on the disk. Return Values If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError. Remarks The GetDiskFreeSpaceEx function lets you avoid the arithmetic required by the GetDiskFreeSpace function. Windows 95: The GetDiskFreeSpace function returns incorrect values for volumes that are larger than 2 gigabytes. The function caps the values stored into *lpNumberOfFreeClusters and *lpTotalNumberOfClusters so as to never report volume sizes that are greater than 2 gigabytes. Even on volumes that are smaller than 2 gigabytes, the values stored into *lpSectorsPerCluster, *lpNumberOfFreeClusters, and *lpTotalNumberOfClusters values may be incorrect. That is because the operating system manipulates the values so that computations with them yield the correct volume size. Windows 95 OSR2 and later: The GetDiskFreeSpaceEx function is available on Windows 95 systems beginning with OEM Service Release 2 (OSR2). The GetDiskFreeSpaceEx function returns correct values for all volumes, including those that are greater than 2 gigabytes. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Unsupported. Header: Declared in winbase.h. Import Library: Use kernel32.lib. Unicode: Implemented as Unicode and ANSI versions on Windows NT. See Also File I/O Overview, File Functions, GetDiskFreeSpaceEx, GetDriveType
mujiok2003 2013-03-10
  • 打赏
  • 举报
回复
原子操作:按整个对象序列化,而不是对象的字段逐个序列化。 要么全完成,要么删除部分写入的内容。
zhouzhenyan 2013-03-10
  • 打赏
  • 举报
回复
引用 1 楼 taodm 的回复:
这恐怕不是boost库能解决的。 建议你是序列化到内存,然后由内存一起写文件。
这样能够避免这个问题吗?由内存一起写的时候可能还会遇到这样的问题,或者在写入的时候由于其他异常而导致写失败。而且这样内存开销会更大。
taodm 2013-03-10
  • 打赏
  • 举报
回复
这恐怕不是boost库能解决的。 建议你是序列化到内存,然后由内存一起写文件。

24,855

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧