1
0
Fork
You've already forked rusdis
1

WIP: Add default implementation for the database #1

Draft
karreiro wants to merge 1 commit from karreiro/rusdis:add-default-impl into master
pull from: karreiro/rusdis:add-default-impl
merge into: era:master
era:master
First-time contributor
Copy link

I'd like to hear some early thoughts about the default_database.rs.

In order to support the first implementation, I've introduced some changes in the src/database.rs:

  • Personally, I believe it's a bit more cohesive to keep the key/value as Strings (but I'm fine with bringing the old implementation if that's too expensive)
  • I've changed the Options to Option<Options>, wdyt?

Also, the changes in the Cargo.toml files are temporary ;) (I'm using them for development purposes only).

:)

I'd like to hear some early thoughts about the `default_database.rs`. In order to support the first implementation, I've introduced some changes in the `src/database.rs`: - Personally, I believe it's a bit more cohesive to keep the `key/value` as `String`s (but I'm fine with bringing the old implementation if that's too expensive) - I've changed the `Options` to `Option<Options>`, wdyt? Also, the changes in the `Cargo.toml` files are temporary ;) (I'm using them for development purposes only). :)
era left a comment
Copy link

Thank you :), fell free to change any signature you want, they are all placeholders for now

Thank you :), fell free to change any signature you want, they are all placeholders for now
@ -0,0 +23,4 @@
implKeyValueDBforDefaultDatabase{
fn get(&mutself,key: String)-> Option<String>{
ifletSome(i)=self.lru_cache.iter().position(|r|r.eq(&key)){
Owner
Copy link

Thinking out loud (take it with a bit of salt, there is probably someway better to do what I'm saying here):

It would be nice to use the HashMap to find if the key is in the cache, so we can do it in O(1) instead of O(n). For that you must be able to locate the entry in the lru_cache with the information provided by the index object. One way to do that would be to store the position of the item in the index object. But everytime we move an object from position x to 1 we would have to update the x elements in the index as well with their new position.

Another way would be to point to the node itself from the index. Since the node contains prev & next, we could easily swap it to the front without having to update x elements.

Sooner or later we may need to introduce a TTL configuration for the cache. If that's the case, I think there is an interesting third option:

  1. Create an object Bucket:
struct Bucket{used_at: Timestamp// milliseconds precision
values: Map<Key,Value>}
  1. transform lru_cache in a MaxHeap or MinHeap which elements are Bucket and they are sorted by used_at.
  2. Modify the index to store the used_at timestamp.
  3. When getting the key:
self.index.get(key).map(|timestamp|self.lru_cache.get(timestamp).values.get(key))//omiting the process of updating the timestamp

Dropping items from the cache: you will need to get the oldest timestamp in the queue and drop one entry in the values. If values are empty you can remove the timestamp from lru_cache as well. We are sacrificing some correctness here, if there are more than one value in the given timestamp we may not be respecting exactly the order they were used, but the error here should be so marginal that I don't think we care.

PS: We could also just use a LinkedHashMap and merge lru_cache and index in the same object, which would be the simplest solution.

Thinking out loud (take it with a bit of salt, there is probably someway better to do what I'm saying here): It would be nice to use the HashMap to find if the key is in the cache, so we can do it in O(1) instead of O(n). For that you must be able to locate the entry in the lru_cache with the information provided by the index object. One way to do that would be to store the position of the item in the index object. But everytime we move an object from position x to 1 we would have to update the x elements in the index as well with their new position. Another way would be to point to the node itself from the index. Since the node contains prev & next, we could easily swap it to the front without having to update x elements. Sooner or later we may need to introduce a TTL configuration for the cache. If that's the case, I think there is an interesting third option: 1) Create an object Bucket: ```rust struct Bucket { used_at: Timestamp // milliseconds precision values: Map<Key, Value> } ``` 1) transform lru_cache in a MaxHeap or MinHeap which elements are Bucket and they are sorted by used_at. 2) Modify the index to store the used_at timestamp. 3) When getting the key: ```rust self.index.get(key).map(|timestamp| self.lru_cache.get(timestamp).values.get(key) ) //omiting the process of updating the timestamp ``` Dropping items from the cache: you will need to get the oldest timestamp in the queue and drop one entry in the `values`. If values are empty you can remove the timestamp from lru_cache as well. We are sacrificing some correctness here, if there are more than one value in the given timestamp we may not be respecting exactly the order they were used, but the error here should be so marginal that I don't think we care. PS: We could also just use a LinkedHashMap and merge lru_cache and index in the same object, which would be the simplest solution.
Owner
Copy link

PS2: The third option is O(logn)

PS2: The third option is O(logn)
This pull request is marked as a work in progress.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u add-default-impl:karreiro-add-default-impl
git switch karreiro-add-default-impl

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch master
git merge --no-ff karreiro-add-default-impl
git switch karreiro-add-default-impl
git rebase master
git switch master
git merge --ff-only karreiro-add-default-impl
git switch karreiro-add-default-impl
git rebase master
git switch master
git merge --no-ff karreiro-add-default-impl
git switch master
git merge --squash karreiro-add-default-impl
git switch master
git merge --ff-only karreiro-add-default-impl
git switch master
git merge karreiro-add-default-impl
git push origin master
Sign in to join this conversation.
No reviewers
era
Labels
Clear labels
No items
No labels
Milestone
Clear milestone
No items
No milestone
Projects
Clear projects
No items
No project
Assignees
Clear assignees
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
era/rusdis!1
Reference in a new issue
era/rusdis
No description provided.
Delete branch "karreiro/rusdis:add-default-impl"

Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?