Skip to content

Errata for dirmirror/dirauth storage backed

While developing the HTTP backend for the future tor-dirserver crate, I stumbled upon a few shortcomings in the design of the storage model, described in the corresponding doc/dev document.

No central content addressable storage

Our design really embraces the concept of a content addressable storage. However, when you want to lookup a sha256 hash, you will also need to know the appropriate table name (or do some brute forcing which is rather ugly).

For the HTTP backend, this is sort of problematic, because right now, I am designing it in a callback based fashion, that is, as follows:

async fn cb(pool: Pool<Manager>, requ: Request<Incoming>) -> Result<Response<Vec<(TableName, Sha256)>>, Box<dyn StdError> {
    // Return table name and sha256 as strings
}

I believe that design is rather inelegant because the cool part of a true content addressable storage is that having the hash is all you need to access the relevant data.

My proposed solution would be to have one big table called store with the fields rowid, content_sha256, content. All remaining tables would then only serve as headers or meta information about that single hash, which can then be obtained elsewhere.

Re-think the cache for avoiding data duplication

So far, our common idea was to use a RwLock<WeakValueHashMap<Sha256, Weak<String>> to model the cache. Unfortunately, hyper really wants us to use Bytes which is a type that implements zero-cost clone and all the nice implementations required for use with hyper. Bytes does not use an Arc internally though, or at least not one that is publicly exposed, making it impossible for us to obtain a Weak reference, therefore we are basically unable to use this in a weak table.

My proposed solution would be to do type Cache = Arc<RwLock<Sha256, Bytes>> with a periodic task acquiring a write lock and removing all entries where Bytes::is_unique yields true. This is O(n) but I don't think it will be an issue here.

...

I will keep this updated in case I encounter other things.