vedang / clj_fdb

A thin Clojure wrapper for the Java API for FoundationDB.
https://vedang.github.io/clj_fdb/
Eclipse Public License 1.0
25 stars 8 forks source link

Implement watch #9

Closed tirkarthi closed 6 years ago

tirkarthi commented 6 years ago

Is your feature request related to a problem? Please describe.

Implement watch API that can be used to track the changes made to a key.

Describe the solution you'd like

Watch API is available in the Java client that can be used to track the changes made to the key with callbacks.

Additional context

I have tried an initial implementation of watch that works as below :

  (let [fd    (select-api-version 510)
       key    "foo"
       value  "bar"]
  (with-open [db (open fd)]
    (tr! db
         (clear-key tr key)
         (watch tr key #(println "key is set"))
         (set-val tr key value)
         (watch tr key #(println "key is changed to 1"))
         (set-val tr key value) ;; Doesn't trigger watch
         (set-val tr key "1")
         (watch tr key #(println "cleared key"))
         (clear-key tr key))))

  key is set
  key is changed to 1
  cleared key

Implementation

https://github.com/tirkarthi/clj-foundationdb/blob/37f39adca4069070fab2a4630b76b4fab22ead93/src/clj_foundationdb/core.clj#L176

Basic test with callback and future

https://github.com/tirkarthi/clj-foundationdb/blob/37f39adca4069070fab2a4630b76b4fab22ead93/test/clj_foundationdb/core_test.clj#L305

Java docs reference

https://apple.github.io/foundationdb/javadoc/com/apple/foundationdb/Transaction.html#watch-byte:A-

Python docs reference with example

https://apple.github.io/foundationdb/developer-guide.html#watches

Feedback welcome.

tirkarthi commented 6 years ago

I did an initial implementation of this. But it seems the watch callback is executed only once for the change since I was thinking about pub/sub like use cases.

vedang commented 6 years ago

Hi @tirkarthi :

While I appreciate the value of providing a callback based API in the library, I am not sure what the shape of this API should be.

In the meantime, would be be open to submitting a PR which implements a simple wrapper over the watch function in the src/clj_fdb/transaction.clj file? Let me know, and I will pick this up otherwise.

vedang commented 6 years ago

I did an initial implementation of this. But it seems the watch callback is executed only once for the change since I was thinking about pub/sub like use cases.

Yes, this is why I want to understand the possibilities before putting helper fns in the library.

tirkarthi commented 6 years ago

Thanks @vedang for the review. I proposed it for API parity and initially it seemed to be a useful addition. But once I implemented it I couldn't fit a good use case for it. Maybe I am missing something about the use case since I am still a beginner with respect to FoundationDB.

Unfortunately, I will not be able to pick this up due to lack of time. I am closing this in favour of #11

Thanks