---
title: "文件数据hash保存"
author: "jjw"
date: "2010-03-15T22:32:54Z"
tags: []
categories: ["编程语言"]
slug: "hash"
draft: false
lastmod: "2010-03-15T22:32:54Z"
id: 616477
url: "https://jjw.iteye.com/blog/616477"
---

假设数据块的大小是相差不大的，key占用8字节，data所占用的字节数量比如固定为1k，则文件格式如下：
_ _key _ _|_ _ _ _ _ _data _ _ _ _ _ _|
_ _key _ _|_ _ _ _ _ _data _ _ _ _ _ _|
_ _key _ _|_ _ _ _ _ _data _ _ _ _ _ _|
_ _key _ _|_ _ _ _ _ _data _ _ _ _ _ _|
_ _key _ _|_ _ _ _ _ _data _ _ _ _ _ _|
_ _key _ _|_ _ _ _ _ _data _ _ _ _ _ _|

如果数据块的大小相差非常大，则不利于文件块的分配，容易空间浪费，所以做一下索引,offset作为data区的偏移量，key还是8字节，offset也是8字节
_ _key _ _|_ _ offset_ _|
_ _key _ _|_ _ offset_ _|
_ _key _ _|_ _ offset_ _|
_ _key _ _|_ _ offset_ _|

数据区，data则可以随便保存，只是每个data同文件开始的偏移保存在offset里
_ _ _data _ |_ _data __ _ _|_ _data _ _ _ _ _|_ _data __ _ _ _ _ _ _|