swh.perfecthash package#

Submodules:

Module contents:

class swh.perfecthash.Shard(path: str)[source]#

Bases: object

Low level management for files indexed with a perfect hash table.

This class allows creating a Read Shard by adding key/object pairs and looking up the content of an object when given the key.

Initialize with an existing Read Shard.

Parameters:

path – path to an existing Read Shard file or device

static key_len()[source]#
create(objects_count: int) Shard[source]#

Wipe out the content of the Read Shard. It must be followed by object_count calls to the write method otherwise the content of the Read Shard will be inconsistent. When all objects are inserted, the Read Shard must be made persistent by calling the save method.

Parameters:

objects_count – number of objects in the Read Shard.

Returns:

self.

load() Shard[source]#

Open the Read Shard file in read-only mode.

Returns:

self.

save() int[source]#

Create the perfect hash table the lookup method relies on to find the content of the objects.

It must be called after create and write otherwise the content of the Read Shard will be inconsistent.

Returns:

0 on success, -1 on error.

lookup(key: Key) HashObject[source]#

Fetch the object matching the key in the Read Shard.

Fetching an object is O(1): one lookup in the index to obtain the offset of the object in the Read Shard and one read to get the payload.

Parameters:

key – the key associated with the object to retrieve.

Returns:

the object as bytes.

write(key: Key, object: HashObject) int[source]#

Add the key/object pair to the Read Shard.

The create method must have been called prior to calling the write method.

Parameters:
  • key – the unique key associated with the object.

  • object – the object

Returns:

0 on success, -1 on error.