{"action":"created","comment":{"url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/pulls/comments/74208104","id":74208104,"diff_hunk":"@@ -0,0 +1,135 @@\n+package uk.gov.justice.services.event.buffer.core.repository.service;\n+\n+import org.slf4j.Logger;\n+import org.slf4j.LoggerFactory;\n+\n+import uk.gov.justice.services.event.buffer.api.EventBufferService;\n+import uk.gov.justice.services.event.buffer.core.repository.spliterator.EventBufferSpliterator;\n+import uk.gov.justice.services.event.buffer.core.repository.streambuffer.StreamBufferEvent;\n+import uk.gov.justice.services.event.buffer.core.repository.streambuffer.StreamBufferJdbcRepository;\n+import uk.gov.justice.services.event.buffer.core.repository.streamstatus.StreamStatus;\n+import uk.gov.justice.services.event.buffer.core.repository.streamstatus.StreamStatusJdbcRepository;\n+import uk.gov.justice.services.messaging.JsonEnvelope;\n+import uk.gov.justice.services.messaging.JsonObjectEnvelopeConverter;\n+import uk.gov.justice.services.messaging.logging.JsonEnvelopeLoggerHelper;\n+\n+import java.util.Iterator;\n+import java.util.Optional;\n+import java.util.UUID;\n+import java.util.stream.Stream;\n+import java.util.stream.StreamSupport;\n+\n+import javax.annotation.Priority;\n+import javax.enterprise.context.ApplicationScoped;\n+import javax.enterprise.inject.Alternative;\n+import javax.inject.Inject;\n+\n+import static java.lang.String.format;\n+import static uk.gov.justice.services.messaging.logging.JsonEnvelopeLoggerHelper.toEnvelopeTraceString;\n+\n+@ApplicationScoped\n+@Alternative\n+@Priority(2)\n+public class ConsecutiveEventBufferService implements EventBufferService {\n+\n+    private static final Logger LOGGER = LoggerFactory.getLogger(EventBufferService.class);\n+\n+    @Inject\n+    private StreamBufferJdbcRepository streamBufferRepository;\n+\n+    @Inject\n+    private StreamStatusJdbcRepository streamStatusRepository;\n+\n+    @Inject\n+    private JsonObjectEnvelopeConverter jsonObjectEnvelopeConverter;\n+\n+\n+    /**\n+     *\n+     * Takes an incoming event and returns a stream of json envelopes. If the event is not consecutive\n+     * according to the stream_status repository then an empty stream is returned and the incomingEvent\n+     * is added to the event-buffer-repository. If the incomingEvent is consecutive then it is returned\n+     * as a stream with any consecutive events from the buffer. If an event is the first to be processed\n+     * for that streamId then the version value must be 1 or the incomingEvent is added to the buffer\n+     * and an empty stream is returned.\n+     *\n+     * @param incomingEvent\n+     * @return streamOfConsecutiveEvents\n+     */\n+    @Override\n+    public Stream<JsonEnvelope> currentOrderedEventsWith(final JsonEnvelope incomingEvent) {\n+\n+        LOGGER.trace(format(\"Message buffering for message: %s\", toEnvelopeTraceString(incomingEvent)));\n+\n+        final UUID streamId = incomingEvent.metadata().streamId().get();\n+        final Long incomingEventVersion = incomingEvent.metadata().version().get();\n+\n+        final Optional<StreamStatus> currentStatus = streamStatusRepository.findByStreamId(streamId);\n+        if (currentStatus.isPresent()) {\n+            final Long currentVersion = currentStatus.get().getVersion();\n+            if (incomingEventObsolete(incomingEventVersion, currentVersion)) {\n+                LOGGER.trace(format(\"Message : %s is an obsolete version\", toEnvelopeTraceString(incomingEvent)));\n+                return Stream.empty();\n+\n+            } else if (incomingEventNotInOrder(incomingEventVersion, currentVersion)) {\n+                LOGGER.trace(format(\"Message : %s is not consecutive, adding to buffer\", toEnvelopeTraceString(incomingEvent)));\n+                return addToBufferAndReturnEmptyStream(incomingEvent, streamId, incomingEventVersion);\n+\n+            } else {\n+                LOGGER.trace(format(\"Message : %s version is valid sending stream to dispatcher\", toEnvelopeTraceString(incomingEvent)));\n+                return streamOfIncomingAndBufferedEvents(incomingEvent, streamId, incomingEventVersion);\n+\n+            }\n+        } else {\n+            if(incomingEventVersion == 1) {","path":"event-buffer/event-buffer-core/src/main/java/uk/gov/justice/services/event/buffer/core/repository/service/ConsecutiveEventBufferService.java","position":84,"original_position":84,"commit_id":"225cb07a2655cef7dd3ac172c4e51074458df2f7","original_commit_id":"225cb07a2655cef7dd3ac172c4e51074458df2f7","user":{"login":"jaceko","id":5623398,"avatar_url":"https://avatars.githubusercontent.com/u/5623398?v=3","gravatar_id":"","url":"https://api.github.com/users/jaceko","html_url":"https://github.com/jaceko","followers_url":"https://api.github.com/users/jaceko/followers","following_url":"https://api.github.com/users/jaceko/following{/other_user}","gists_url":"https://api.github.com/users/jaceko/gists{/gist_id}","starred_url":"https://api.github.com/users/jaceko/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jaceko/subscriptions","organizations_url":"https://api.github.com/users/jaceko/orgs","repos_url":"https://api.github.com/users/jaceko/repos","events_url":"https://api.github.com/users/jaceko/events{/privacy}","received_events_url":"https://api.github.com/users/jaceko/received_events","type":"User","site_admin":false},"body":"is INITAIL_VERSION shorter than 1?","created_at":"2016-08-10T08:55:00Z","updated_at":"2016-08-10T08:55:00Z","html_url":"https://github.com/CJSCommonPlatform/microservice_framework/pull/138#discussion_r74208104","pull_request_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/pulls/138","_links":{"self":{"href":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/pulls/comments/74208104"},"html":{"href":"https://github.com/CJSCommonPlatform/microservice_framework/pull/138#discussion_r74208104"},"pull_request":{"href":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/pulls/138"}}},"pull_request":{"url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/pulls/138","id":80640153,"html_url":"https://github.com/CJSCommonPlatform/microservice_framework/pull/138","diff_url":"https://github.com/CJSCommonPlatform/microservice_framework/pull/138.diff","patch_url":"https://github.com/CJSCommonPlatform/microservice_framework/pull/138.patch","issue_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/issues/138","number":138,"state":"open","locked":false,"title":"Add consecutive event ordering","user":{"login":"skiddykong","id":17156444,"avatar_url":"https://avatars.githubusercontent.com/u/17156444?v=3","gravatar_id":"","url":"https://api.github.com/users/skiddykong","html_url":"https://github.com/skiddykong","followers_url":"https://api.github.com/users/skiddykong/followers","following_url":"https://api.github.com/users/skiddykong/following{/other_user}","gists_url":"https://api.github.com/users/skiddykong/gists{/gist_id}","starred_url":"https://api.github.com/users/skiddykong/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/skiddykong/subscriptions","organizations_url":"https://api.github.com/users/skiddykong/orgs","repos_url":"https://api.github.com/users/skiddykong/repos","events_url":"https://api.github.com/users/skiddykong/events{/privacy}","received_events_url":"https://api.github.com/users/skiddykong/received_events","type":"User","site_admin":false},"body":"","created_at":"2016-08-09T15:19:29Z","updated_at":"2016-08-10T08:55:00Z","closed_at":null,"merged_at":null,"merge_commit_sha":"c9259abedb44df26e1a10e0689cacfc7b883e7f2","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/pulls/138/commits","review_comments_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/pulls/138/comments","review_comment_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/pulls/comments{/number}","comments_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/issues/138/comments","statuses_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/statuses/225cb07a2655cef7dd3ac172c4e51074458df2f7","head":{"label":"CJSCommonPlatform:message-ordering","ref":"message-ordering","sha":"225cb07a2655cef7dd3ac172c4e51074458df2f7","user":{"login":"CJSCommonPlatform","id":16131145,"avatar_url":"https://avatars.githubusercontent.com/u/16131145?v=3","gravatar_id":"","url":"https://api.github.com/users/CJSCommonPlatform","html_url":"https://github.com/CJSCommonPlatform","followers_url":"https://api.github.com/users/CJSCommonPlatform/followers","following_url":"https://api.github.com/users/CJSCommonPlatform/following{/other_user}","gists_url":"https://api.github.com/users/CJSCommonPlatform/gists{/gist_id}","starred_url":"https://api.github.com/users/CJSCommonPlatform/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CJSCommonPlatform/subscriptions","organizations_url":"https://api.github.com/users/CJSCommonPlatform/orgs","repos_url":"https://api.github.com/users/CJSCommonPlatform/repos","events_url":"https://api.github.com/users/CJSCommonPlatform/events{/privacy}","received_events_url":"https://api.github.com/users/CJSCommonPlatform/received_events","type":"Organization","site_admin":false},"repo":{"id":51292541,"name":"microservice_framework","full_name":"CJSCommonPlatform/microservice_framework","owner":{"login":"CJSCommonPlatform","id":16131145,"avatar_url":"https://avatars.githubusercontent.com/u/16131145?v=3","gravatar_id":"","url":"https://api.github.com/users/CJSCommonPlatform","html_url":"https://github.com/CJSCommonPlatform","followers_url":"https://api.github.com/users/CJSCommonPlatform/followers","following_url":"https://api.github.com/users/CJSCommonPlatform/following{/other_user}","gists_url":"https://api.github.com/users/CJSCommonPlatform/gists{/gist_id}","starred_url":"https://api.github.com/users/CJSCommonPlatform/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CJSCommonPlatform/subscriptions","organizations_url":"https://api.github.com/users/CJSCommonPlatform/orgs","repos_url":"https://api.github.com/users/CJSCommonPlatform/repos","events_url":"https://api.github.com/users/CJSCommonPlatform/events{/privacy}","received_events_url":"https://api.github.com/users/CJSCommonPlatform/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/CJSCommonPlatform/microservice_framework","description":"","fork":false,"url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework","forks_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/forks","keys_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/keys{/key_id}","collaborators_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/teams","hooks_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/hooks","issue_events_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/issues/events{/number}","events_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/events","assignees_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/assignees{/user}","branches_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/branches{/branch}","tags_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/tags","blobs_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/git/refs{/sha}","trees_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/git/trees{/sha}","statuses_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/statuses/{sha}","languages_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/languages","stargazers_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/stargazers","contributors_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/contributors","subscribers_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/subscribers","subscription_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/subscription","commits_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/commits{/sha}","git_commits_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/git/commits{/sha}","comments_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/comments{/number}","issue_comment_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/issues/comments{/number}","contents_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/contents/{+path}","compare_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/compare/{base}...{head}","merges_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/merges","archive_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/downloads","issues_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/issues{/number}","pulls_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/pulls{/number}","milestones_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/milestones{/number}","notifications_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/labels{/name}","releases_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/releases{/id}","deployments_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/deployments","created_at":"2016-02-08T10:51:31Z","updated_at":"2016-07-28T15:57:20Z","pushed_at":"2016-08-10T08:41:27Z","git_url":"git://github.com/CJSCommonPlatform/microservice_framework.git","ssh_url":"git@github.com:CJSCommonPlatform/microservice_framework.git","clone_url":"https://github.com/CJSCommonPlatform/microservice_framework.git","svn_url":"https://github.com/CJSCommonPlatform/microservice_framework","homepage":null,"size":2232,"stargazers_count":5,"watchers_count":5,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":4,"mirror_url":null,"open_issues_count":1,"forks":4,"open_issues":1,"watchers":5,"default_branch":"master"}},"base":{"label":"CJSCommonPlatform:master","ref":"master","sha":"f97e0c5a49f434570984eb3ce8abdfb28d143148","user":{"login":"CJSCommonPlatform","id":16131145,"avatar_url":"https://avatars.githubusercontent.com/u/16131145?v=3","gravatar_id":"","url":"https://api.github.com/users/CJSCommonPlatform","html_url":"https://github.com/CJSCommonPlatform","followers_url":"https://api.github.com/users/CJSCommonPlatform/followers","following_url":"https://api.github.com/users/CJSCommonPlatform/following{/other_user}","gists_url":"https://api.github.com/users/CJSCommonPlatform/gists{/gist_id}","starred_url":"https://api.github.com/users/CJSCommonPlatform/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CJSCommonPlatform/subscriptions","organizations_url":"https://api.github.com/users/CJSCommonPlatform/orgs","repos_url":"https://api.github.com/users/CJSCommonPlatform/repos","events_url":"https://api.github.com/users/CJSCommonPlatform/events{/privacy}","received_events_url":"https://api.github.com/users/CJSCommonPlatform/received_events","type":"Organization","site_admin":false},"repo":{"id":51292541,"name":"microservice_framework","full_name":"CJSCommonPlatform/microservice_framework","owner":{"login":"CJSCommonPlatform","id":16131145,"avatar_url":"https://avatars.githubusercontent.com/u/16131145?v=3","gravatar_id":"","url":"https://api.github.com/users/CJSCommonPlatform","html_url":"https://github.com/CJSCommonPlatform","followers_url":"https://api.github.com/users/CJSCommonPlatform/followers","following_url":"https://api.github.com/users/CJSCommonPlatform/following{/other_user}","gists_url":"https://api.github.com/users/CJSCommonPlatform/gists{/gist_id}","starred_url":"https://api.github.com/users/CJSCommonPlatform/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CJSCommonPlatform/subscriptions","organizations_url":"https://api.github.com/users/CJSCommonPlatform/orgs","repos_url":"https://api.github.com/users/CJSCommonPlatform/repos","events_url":"https://api.github.com/users/CJSCommonPlatform/events{/privacy}","received_events_url":"https://api.github.com/users/CJSCommonPlatform/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/CJSCommonPlatform/microservice_framework","description":"","fork":false,"url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework","forks_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/forks","keys_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/keys{/key_id}","collaborators_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/teams","hooks_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/hooks","issue_events_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/issues/events{/number}","events_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/events","assignees_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/assignees{/user}","branches_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/branches{/branch}","tags_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/tags","blobs_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/git/refs{/sha}","trees_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/git/trees{/sha}","statuses_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/statuses/{sha}","languages_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/languages","stargazers_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/stargazers","contributors_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/contributors","subscribers_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/subscribers","subscription_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/subscription","commits_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/commits{/sha}","git_commits_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/git/commits{/sha}","comments_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/comments{/number}","issue_comment_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/issues/comments{/number}","contents_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/contents/{+path}","compare_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/compare/{base}...{head}","merges_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/merges","archive_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/downloads","issues_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/issues{/number}","pulls_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/pulls{/number}","milestones_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/milestones{/number}","notifications_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/labels{/name}","releases_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/releases{/id}","deployments_url":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/deployments","created_at":"2016-02-08T10:51:31Z","updated_at":"2016-07-28T15:57:20Z","pushed_at":"2016-08-10T08:41:27Z","git_url":"git://github.com/CJSCommonPlatform/microservice_framework.git","ssh_url":"git@github.com:CJSCommonPlatform/microservice_framework.git","clone_url":"https://github.com/CJSCommonPlatform/microservice_framework.git","svn_url":"https://github.com/CJSCommonPlatform/microservice_framework","homepage":null,"size":2232,"stargazers_count":5,"watchers_count":5,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":4,"mirror_url":null,"open_issues_count":1,"forks":4,"open_issues":1,"watchers":5,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/pulls/138"},"html":{"href":"https://github.com/CJSCommonPlatform/microservice_framework/pull/138"},"issue":{"href":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/issues/138"},"comments":{"href":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/issues/138/comments"},"review_comments":{"href":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/pulls/138/comments"},"review_comment":{"href":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/pulls/138/commits"},"statuses":{"href":"https://api.github.com/repos/CJSCommonPlatform/microservice_framework/statuses/225cb07a2655cef7dd3ac172c4e51074458df2f7"}}}}
{"actor":{"display_login":"jaceko"}}