Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultOptions = Options{ DirPath: os.TempDir(), SlotValueLength: 0, LoadFactor: 0.7, }
DefaultOptions is the default options.
Functions ¶
This section is empty.
Types ¶
type MatchKeyFunc ¶
MatchKeyFunc is used to determine whether the key of the slot matches the stored key. And you must supply the function to the Put/Get/Delete methods.
Why we need this function?
When we store the data in the hash table, we only store the hash value of the key, and the raw value. So when we get the data from hash table, even if the hash value of the key matches, that doesn't mean the key matches because of hash collision. So we need to provide a function to determine whether the key of the slot matches the stored key.
type Options ¶
type Options struct { // DirPath is the directory path to store the hash table files. DirPath string // SlotValueLength is the length of the value in each slot. // Your value lenght must be equal to the value length you set when creating the table. SlotValueLength uint32 // LoadFactor is the load factor of the hash table. // The load factor is the ratio of the number of elements in the hash table to the table size. // If the ratio is greater than the load factor, the hash table will be expanded automatically. // The default value is 0.7. LoadFactor float64 }
Options is used to create a new diskhash table.
type Table ¶
type Table struct {
// contains filtered or unexported fields
}
Table is a hash table that stores data on disk. It consists of two files, the primary file and the overflow file. Each file is divided into multiple buckets, each bucket contains multiple slots.
The overview design of the hash table is as the Linear Hashing algorithm. See more: https://en.wikipedia.org/wiki/Linear_hashing https://dsf.berkeley.edu/jmh/cs186/f02/lecs/lec18_2up.pdf
func Open ¶
Open opens a hash table. If the hash table does not exist, it will be created automatically. It will open the primary file, the overflow file and the meta file.
func (*Table) Delete ¶
func (t *Table) Delete(key []byte, matchKey MatchKeyFunc) error
Delete deletes the key from the hash table. the parameter matchKey is described in the MatchKeyFunc.
func (*Table) Get ¶
func (t *Table) Get(key []byte, matchKey MatchKeyFunc) error
Get gets the value of the key from the hash table. the parameter matchKey is described in the MatchKeyFunc.
func (*Table) Put ¶
func (t *Table) Put(key, value []byte, matchKey MatchKeyFunc) error
Put puts a new ke/value pair to the hash table. the parameter matchKey is described in the MatchKeyFunc.