Changeset View
Standalone View
swh/perfecthash/__init__.py
from _hash_cffi import lib | |||||||||||||||||||||||||
ardumont: copyright headers to match other swh modules? | |||||||||||||||||||||||||
Done Inline ActionsGood point, done! dachary: Good point, done! | |||||||||||||||||||||||||
class Shard: | |||||||||||||||||||||||||
def __init__(self, path): | |||||||||||||||||||||||||
self.shard = lib.shard_init(path.encode('utf-8')) | |||||||||||||||||||||||||
def __del__(self): | |||||||||||||||||||||||||
lib.shard_destroy(self.shard) | |||||||||||||||||||||||||
def create(self): | |||||||||||||||||||||||||
lib.shard_create(self.shard) | |||||||||||||||||||||||||
return self | |||||||||||||||||||||||||
def load(self): | |||||||||||||||||||||||||
lib.shard_load(self.shard) | |||||||||||||||||||||||||
return self | |||||||||||||||||||||||||
Done Inline Actions
Let's add some types. ardumont: Let's add some types. | |||||||||||||||||||||||||
def save(self): | |||||||||||||||||||||||||
lib.shard_save(self.shard) | |||||||||||||||||||||||||
def lookup(self, key): | |||||||||||||||||||||||||
object_pointer = ffi.new("char**") | |||||||||||||||||||||||||
object_size_pointer = ffi.new("size_t*") | |||||||||||||||||||||||||
lib.shard_lookup(self.shard, key, object, object_size) | |||||||||||||||||||||||||
return ffi.buffer(object_pointer[0], object_size_pointer[0]) | |||||||||||||||||||||||||
def write(self, key, object): | |||||||||||||||||||||||||
lib.shard_object_write(self.shard, key, object, len(object)) | |||||||||||||||||||||||||
Done Inline Actions
fyi, we now are documenting directly the types within the code and no longer in the docstring. ardumont: fyi, we now are documenting directly the types within the code and no longer in the docstring. | |||||||||||||||||||||||||
Done Inline Actionsindent is off by 2 chars almost everywhere. ardumont: indent is off by 2 chars almost everywhere.
We normalize on a 4 chars indent below "Args:" and… | |||||||||||||||||||||||||
Done Inline Actions
Let's add some types around this, then define at the top-level module something like: from typing import NewType Key = NewType("Key", bytes) HashObjects = NewType("HashObject", bytes) I believe that improves the reading significantly. ardumont: Let's add some types around this, then define at the top-level module something like:
```
from… | |||||||||||||||||||||||||
Done Inline Actions
ardumont: | |||||||||||||||||||||||||
Done Inline ActionsSomething hit me, can't the lookup fail? What would be the return if that was the case None? ardumont: Something hit me, can't the lookup fail?
What would be the return if that was the case None? | |||||||||||||||||||||||||
Done Inline ActionsI overlooked this question, sorry. The lookup cannot fail: if given a key that does not exist it will return a random object. Which is ok because the global index ensures the Read Shard is only queried with objects that are known to exist in the hash map. dachary: I overlooked this question, sorry. The lookup cannot fail: if given a key that does not exist… | |||||||||||||||||||||||||
Done Inline Actionsyou did not overlook it, it's a new one! If anything, that's me who overlooked that code last time ;) Great! "Can't happen!" ardumont: you did not overlook it, it's a new one!
If anything, that's me who overlooked that code last… | |||||||||||||||||||||||||
Done Inline Actions
From the docstring ;) ardumont: From the docstring ;) | |||||||||||||||||||||||||
Done Inline Actions
From the docstring. ardumont: From the docstring. |
copyright headers to match other swh modules?