-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from AppsFlyer/listeners
refactor listeners
- Loading branch information
Showing
3 changed files
with
113 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
(ns aerospike-clj.aerospike-record | ||
(:require [aerospike-clj.utils :as utils]) | ||
(:import [com.aerospike.client Record])) | ||
|
||
(defrecord AerospikeRecord [payload ^Integer gen ^Integer ttl]) | ||
|
||
(defn record->map [^Record record] | ||
(and record | ||
(let [bins (into {} (.bins record)) ;; converting from java.util.HashMap to a Clojure map | ||
bin-names (keys bins) | ||
payload (if (utils/single-bin? bin-names) | ||
;; single bin record | ||
(utils/desanitize-bin-value (get bins "")) | ||
;; multiple-bin record | ||
(reduce-kv (fn [m k v] | ||
(assoc m k (utils/desanitize-bin-value v))) | ||
{} | ||
bins))] | ||
(->AerospikeRecord | ||
payload | ||
^Integer (.generation ^Record record) | ||
^Integer (.expiration ^Record record))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
(ns aerospike-clj.listeners | ||
(:require [promesa.core :as p] | ||
[aerospike-clj.aerospike-record :as record]) | ||
(:import [com.aerospike.client Key Record AerospikeException AerospikeException$QueryTerminated] | ||
[com.aerospike.client.listener RecordListener WriteListener DeleteListener | ||
ExistsListener BatchListListener RecordSequenceListener InfoListener ExistsArrayListener] | ||
[java.util List Map])) | ||
|
||
(deftype AsyncExistsListener [op-future] | ||
ExistsListener | ||
(^void onFailure [_this ^AerospikeException ex] | ||
(p/reject! op-future ex)) | ||
(^void onSuccess [_this ^Key _k ^boolean exists] | ||
(p/resolve! op-future exists))) | ||
|
||
(deftype AsyncDeleteListener [op-future] | ||
DeleteListener | ||
(^void onSuccess [_this ^Key _k ^boolean existed] | ||
(p/resolve! op-future existed)) | ||
(^void onFailure [_ ^AerospikeException ex] | ||
(p/reject! op-future ex))) | ||
|
||
(deftype AsyncWriteListener [op-future] | ||
WriteListener | ||
(^void onSuccess [_this ^Key _] | ||
(p/resolve! op-future true)) | ||
(^void onFailure [_this ^AerospikeException ex] | ||
(p/reject! op-future ex))) | ||
|
||
(deftype AsyncInfoListener [op-future] | ||
InfoListener | ||
(^void onSuccess [_this ^Map result-map] | ||
(p/resolve! op-future (into {} result-map))) | ||
(^void onFailure [_this ^AerospikeException ex] | ||
(p/reject! op-future ex))) | ||
|
||
(deftype AsyncRecordListener [op-future] | ||
RecordListener | ||
(^void onFailure [_this ^AerospikeException ex] | ||
(p/reject! op-future ex)) | ||
(^void onSuccess [_this ^Key _k ^Record record] | ||
(p/resolve! op-future record))) | ||
|
||
(deftype AsyncRecordSequenceListener [op-future callback] | ||
RecordSequenceListener | ||
(^void onRecord [_this ^Key k ^Record record] | ||
(when (= :abort-scan (callback (.userKey k) (record/record->map record))) | ||
(throw (AerospikeException$QueryTerminated.)))) | ||
(^void onSuccess [_this] | ||
(p/resolve! op-future true)) | ||
(^void onFailure [_this ^AerospikeException exception] | ||
(if (instance? AerospikeException$QueryTerminated exception) | ||
(p/resolve! op-future false) | ||
(p/reject! op-future exception)))) | ||
|
||
(deftype AsyncBatchListListener [op-future] | ||
BatchListListener | ||
(^void onFailure [_this ^AerospikeException ex] | ||
(p/reject! op-future ex)) | ||
(^void onSuccess [_this ^List records] | ||
(p/resolve! op-future records))) | ||
|
||
(deftype AsyncExistsArrayListener [op-future] | ||
ExistsArrayListener | ||
(^void onFailure [_this ^AerospikeException ex] | ||
(p/reject! op-future ex)) | ||
(^void onSuccess [_this ^"[Lcom.aerospike.client.Key;" _keys ^"[Z" exists] | ||
(p/resolve! op-future exists))) | ||
|