@@ -52,11 +52,13 @@ class MongoDbEventPublicationRepository implements EventPublicationRepository {
52
52
private static final String ID = "id" ;
53
53
private static final String LISTENER_ID = "listenerId" ;
54
54
private static final String PUBLICATION_DATE = "publicationDate" ;
55
-
56
55
private static final Sort DEFAULT_SORT = Sort .by (PUBLICATION_DATE ).ascending ();
57
56
57
+ static final String ARCHIVE_COLLECTION = "event_publication_archive" ;
58
+
58
59
private final MongoTemplate mongoTemplate ;
59
60
private final CompletionMode completionMode ;
61
+ private final String collection , archiveCollection ;
60
62
61
63
/**
62
64
* Creates a new {@link MongoDbEventPublicationRepository} for the given {@link MongoTemplate}.
@@ -71,6 +73,8 @@ public MongoDbEventPublicationRepository(MongoTemplate mongoTemplate, Completion
71
73
72
74
this .mongoTemplate = mongoTemplate ;
73
75
this .completionMode = completionMode ;
76
+ this .collection = "event_publication" ;
77
+ this .archiveCollection = completionMode == CompletionMode .ARCHIVE ? ARCHIVE_COLLECTION : collection ;
74
78
}
75
79
76
80
/*
@@ -80,7 +84,7 @@ public MongoDbEventPublicationRepository(MongoTemplate mongoTemplate, Completion
80
84
@ Override
81
85
public TargetEventPublication create (TargetEventPublication publication ) {
82
86
83
- mongoTemplate .save (domainToDocument (publication ));
87
+ mongoTemplate .save (domainToDocument (publication ), collection );
84
88
85
89
return publication ;
86
90
}
@@ -93,16 +97,20 @@ public TargetEventPublication create(TargetEventPublication publication) {
93
97
public void markCompleted (Object event , PublicationTargetIdentifier identifier , Instant completionDate ) {
94
98
95
99
var query = byEventAndListenerId (event , identifier );
100
+ var update = Update .update (COMPLETION_DATE , completionDate );
96
101
97
102
if (completionMode == CompletionMode .DELETE ) {
98
103
99
- mongoTemplate .remove (query , MongoDbEventPublication .class );
104
+ mongoTemplate .remove (query , MongoDbEventPublication .class , collection );
100
105
101
- } else {
106
+ } else if ( completionMode == CompletionMode . ARCHIVE ) {
102
107
103
- var update = Update .update (COMPLETION_DATE , completionDate );
108
+ mongoTemplate .findAndModify (query , update , MongoDbEventPublication .class , collection );
109
+ var completedEvent = mongoTemplate .findAndRemove (query , MongoDbEventPublication .class , collection );
110
+ mongoTemplate .save (completedEvent , archiveCollection );
111
+ } else {
104
112
105
- mongoTemplate .findAndModify (query , update , MongoDbEventPublication .class );
113
+ mongoTemplate .findAndModify (query , update , MongoDbEventPublication .class , collection );
106
114
}
107
115
}
108
116
@@ -113,17 +121,21 @@ public void markCompleted(Object event, PublicationTargetIdentifier identifier,
113
121
@ Override
114
122
public void markCompleted (UUID identifier , Instant completionDate ) {
115
123
116
- var criateria = query (where (ID ).is (identifier ));
124
+ var criteria = query (where (ID ).is (identifier ));
125
+ var update = Update .update (COMPLETION_DATE , completionDate );
117
126
118
127
if (completionMode == CompletionMode .DELETE ) {
119
128
120
- mongoTemplate .remove (criateria , MongoDbEventPublication .class );
129
+ mongoTemplate .remove (criteria , MongoDbEventPublication .class , collection );
121
130
122
- } else {
131
+ } else if ( completionMode == CompletionMode . ARCHIVE ) {
123
132
124
- var update = Update .update (COMPLETION_DATE , completionDate );
133
+ mongoTemplate .findAndModify (criteria , update , MongoDbEventPublication .class , collection );
134
+ var completedEvent = mongoTemplate .findAndRemove (criteria , MongoDbEventPublication .class , collection );
135
+ mongoTemplate .save (completedEvent , archiveCollection );
125
136
126
- mongoTemplate .findAndModify (criateria , update , MongoDbEventPublication .class );
137
+ } else {
138
+ mongoTemplate .findAndModify (criteria , update , MongoDbEventPublication .class , collection );
127
139
}
128
140
}
129
141
@@ -168,7 +180,7 @@ public Optional<TargetEventPublication> findIncompletePublicationsByEventAndTarg
168
180
*/
169
181
@ Override
170
182
public List <TargetEventPublication > findCompletedPublications () {
171
- return readMapped (defaultQuery (where (COMPLETION_DATE ).ne (null )));
183
+ return readMapped (defaultQuery (where (COMPLETION_DATE ).ne (null )), archiveCollection );
172
184
}
173
185
174
186
/*
@@ -177,7 +189,9 @@ public List<TargetEventPublication> findCompletedPublications() {
177
189
*/
178
190
@ Override
179
191
public void deletePublications (List <UUID > identifiers ) {
180
- mongoTemplate .remove (query (where (ID ).in (identifiers )), MongoDbEventPublication .class );
192
+
193
+ mongoTemplate .remove (query (where (ID ).in (identifiers )), MongoDbEventPublication .class , collection );
194
+ mongoTemplate .remove (query (where (ID ).in (identifiers )), MongoDbEventPublication .class , archiveCollection );
181
195
}
182
196
183
197
/*
@@ -186,7 +200,7 @@ public void deletePublications(List<UUID> identifiers) {
186
200
*/
187
201
@ Override
188
202
public void deleteCompletedPublications () {
189
- mongoTemplate .remove (query (where (COMPLETION_DATE ).ne (null )), MongoDbEventPublication .class );
203
+ mongoTemplate .remove (query (where (COMPLETION_DATE ).ne (null )), MongoDbEventPublication .class , archiveCollection );
190
204
}
191
205
192
206
/*
@@ -198,16 +212,22 @@ public void deleteCompletedPublicationsBefore(Instant instant) {
198
212
199
213
Assert .notNull (instant , "Instant must not be null!" );
200
214
201
- mongoTemplate .remove (query (where (COMPLETION_DATE ).lt (instant )), MongoDbEventPublication .class );
215
+ mongoTemplate .remove (query (where (COMPLETION_DATE ).lt (instant )), MongoDbEventPublication .class , archiveCollection );
202
216
}
203
217
204
218
private List <TargetEventPublication > readMapped (Query query ) {
219
+ return readMapped (query , collection );
220
+ }
221
+
222
+ private List <TargetEventPublication > readMapped (Query query , String collection ) {
205
223
206
224
return mongoTemplate .query (MongoDbEventPublication .class )
225
+ .inCollection (collection )
207
226
.matching (query )
208
227
.stream ()
209
228
.map (MongoDbEventPublicationRepository ::documentToDomain )
210
229
.toList ();
230
+
211
231
}
212
232
213
233
private Query byEventAndListenerId (Object event , PublicationTargetIdentifier identifier ) {
0 commit comments