-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
86 additions
and
81 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
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,21 @@ | ||
(ns ziggurat.middleware.stream-joins | ||
(:require [ziggurat.middleware.default :as dmw])) | ||
|
||
(defn- deserialize-stream-joins-message | ||
"This function takes in the message(proto Byte Array) and the proto-class and deserializes the proto ByteArray into a | ||
Clojure PersistentHashMap. | ||
Temporary logic for migration of services to Ziggurat V3.0 | ||
If the message is of type map, the function just returns the map as it is. In older versions of Ziggurat (< 3.0) we stored | ||
the messages in deserialized formats in RabbitMQ and those messages can be processed by this function. So we have this logic here." | ||
[message proto-class topic-entity-name] | ||
(reduce | ||
(fn [[k1 v1] [k2 v2]] | ||
{k1 (dmw/deserialize-message v1 (if (vector? proto-class) (first proto-class) proto-class) topic-entity-name) | ||
k2 (dmw/deserialize-message v2 (if (vector? proto-class) (second proto-class) proto-class) topic-entity-name)}) | ||
message)) | ||
|
||
(defn protobuf->hash | ||
"This is a middleware function that takes in a message (Proto ByteArray or PersistentHashMap) and calls the handler-fn with the deserialized PersistentHashMap" | ||
[handler-fn proto-class topic-entity-name] | ||
(fn [message] | ||
(handler-fn (deserialize-stream-joins-message message proto-class topic-entity-name)))) |
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
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,43 @@ | ||
(ns ziggurat.middleware.stream-joins-test | ||
(:require [clojure.test :refer [deftest is join-fixtures testing use-fixtures]] | ||
[protobuf.core :as proto] | ||
[ziggurat.fixtures :as fix] | ||
[ziggurat.middleware.stream-joins :as sjmw]) | ||
(:import [flatland.protobuf.test Example$Photo])) | ||
|
||
(use-fixtures :once (join-fixtures [fix/mount-only-config | ||
fix/silence-logging])) | ||
|
||
(deftest common-protobuf->hash-test | ||
(testing "deserialize a message from a stream join" | ||
(let [handler-fn-called? (atom false) | ||
left-message {:id 123 | ||
:path "/path/to/left"} | ||
right-message {:id 456 | ||
:path "/path/to/right"} | ||
proto-class Example$Photo | ||
topic-entity-name "test" | ||
left-proto-message (proto/->bytes (proto/create Example$Photo left-message)) | ||
right-proto-message (proto/->bytes (proto/create Example$Photo right-message)) | ||
handler-fn (fn [{:keys [left right]}] | ||
(if (and (= left left-message) | ||
(= right right-message)) | ||
(reset! handler-fn-called? true)))] | ||
((sjmw/protobuf->hash handler-fn proto-class topic-entity-name) {:left left-proto-message :right right-proto-message}) | ||
(is (true? @handler-fn-called?)))) | ||
(testing "deserialize a message from a stream join using 2 proto classes" | ||
(let [handler-fn-called? (atom false) | ||
left-message {:id 123 | ||
:path "/path/to/left"} | ||
right-message {:id 456 | ||
:path "/path/to/right"} | ||
proto-class Example$Photo | ||
topic-entity-name "test" | ||
left-proto-message (proto/->bytes (proto/create Example$Photo left-message)) | ||
right-proto-message (proto/->bytes (proto/create Example$Photo right-message)) | ||
handler-fn (fn [{:keys [left right]}] | ||
(if (and (= left left-message) | ||
(= right right-message)) | ||
(reset! handler-fn-called? true)))] | ||
((sjmw/protobuf->hash handler-fn [proto-class proto-class] topic-entity-name) {:left left-proto-message :right right-proto-message}) | ||
(is (true? @handler-fn-called?))))) |
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