forked from O2-Czech-Republic/proxima-platform
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[proxima-direct-core] O2-Czech-Republic#269 add TransactionValidator
- Loading branch information
Showing
5 changed files
with
381 additions
and
11 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
90 changes: 90 additions & 0 deletions
90
.../src/test/java/cz/o2/proxima/direct/transaction/TransactionUniqueConstraintValidator.java
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,90 @@ | ||
/* | ||
* Copyright 2017-2022 O2 Czech Republic, a.s. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package cz.o2.proxima.direct.transaction; | ||
|
||
import cz.o2.proxima.direct.core.DirectDataOperator; | ||
import cz.o2.proxima.direct.randomaccess.KeyValue; | ||
import cz.o2.proxima.direct.randomaccess.RandomAccessReader; | ||
import cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter.Transaction; | ||
import cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter.TransactionPreconditionFailedException; | ||
import cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter.TransactionRejectedException; | ||
import cz.o2.proxima.direct.transaction.TransactionalOnlineAttributeWriter.TransactionValidator; | ||
import cz.o2.proxima.repository.EntityAwareAttributeDescriptor.Regular; | ||
import cz.o2.proxima.repository.EntityDescriptor; | ||
import cz.o2.proxima.repository.Repository; | ||
import cz.o2.proxima.storage.StreamElement; | ||
import cz.o2.proxima.transaction.KeyAttributes; | ||
import cz.o2.proxima.util.Optionals; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public class TransactionUniqueConstraintValidator extends TransactionValidator { | ||
|
||
private DirectDataOperator op; | ||
private RandomAccessReader reader; | ||
private EntityDescriptor gateway; | ||
private Regular<Integer> intField; | ||
|
||
@Override | ||
public void setup( | ||
Repository repo, DirectDataOperator directDataOperator, Map<String, Object> cfg) { | ||
op = directDataOperator; | ||
gateway = repo.getEntity("gateway"); | ||
intField = Regular.of(gateway, gateway.getAttribute("intField")); | ||
} | ||
|
||
@Override | ||
public void validate(StreamElement element, Transaction transaction) | ||
throws TransactionPreconditionFailedException, TransactionRejectedException { | ||
|
||
if (element.getAttributeDescriptor().equals(intField)) { | ||
Optional<Integer> intValue = intField.valueOf(element); | ||
if (intValue.isPresent()) { | ||
List<String> keys = new ArrayList<>(); | ||
reader().listEntities(p -> keys.add(p.getSecond())); | ||
for (String k : keys) { | ||
Optional<KeyValue<Integer>> value = reader.get(k, intField); | ||
if (value.isPresent()) { | ||
transaction.update( | ||
Collections.singletonList(KeyAttributes.ofStreamElement(value.get()))); | ||
if (value.get().getParsedRequired().equals(intValue.get())) { | ||
throw new TransactionPreconditionFailedException( | ||
String.format( | ||
"Duplicate value %d, first key: %s, duplicate: %s", | ||
value.get().getParsedRequired(), k, element.getKey())); | ||
} | ||
} else { | ||
transaction.update( | ||
Collections.singletonList(KeyAttributes.ofMissingAttribute(gateway, k, intField))); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void close() throws Exception {} | ||
|
||
private RandomAccessReader reader() { | ||
if (reader == null) { | ||
reader = Optionals.get(op.getRandomAccess(intField)); | ||
} | ||
return reader; | ||
} | ||
} |
Oops, something went wrong.