|
2 | 2 |
|
3 | 3 | import com.scalar.db.api.ConditionalExpression;
|
4 | 4 | import com.scalar.db.api.Delete;
|
| 5 | +import com.scalar.db.api.Get; |
5 | 6 | import com.scalar.db.api.Mutation;
|
| 7 | +import com.scalar.db.api.Operation; |
6 | 8 | import com.scalar.db.api.Put;
|
| 9 | +import com.scalar.db.api.Scan; |
7 | 10 | import com.scalar.db.api.TableMetadata;
|
8 | 11 | import com.scalar.db.common.TableMetadataManager;
|
9 | 12 | import com.scalar.db.common.checker.OperationChecker;
|
10 | 13 | import com.scalar.db.common.error.CoreError;
|
11 | 14 | import com.scalar.db.config.DatabaseConfig;
|
12 | 15 | import com.scalar.db.exception.storage.ExecutionException;
|
| 16 | +import com.scalar.db.io.BigIntColumn; |
| 17 | +import com.scalar.db.io.BlobColumn; |
| 18 | +import com.scalar.db.io.BooleanColumn; |
| 19 | +import com.scalar.db.io.ColumnVisitor; |
13 | 20 | import com.scalar.db.io.DataType;
|
| 21 | +import com.scalar.db.io.DoubleColumn; |
| 22 | +import com.scalar.db.io.FloatColumn; |
| 23 | +import com.scalar.db.io.IntColumn; |
| 24 | +import com.scalar.db.io.TextColumn; |
14 | 25 |
|
15 | 26 | public class CosmosOperationChecker extends OperationChecker {
|
| 27 | + |
| 28 | + private static final char[] ILLEGAL_CHARACTERS_IN_PRIMARY_KEY = { |
| 29 | + // Colons are not allowed in primary-key columns due to the `ConcatenationVisitor` limitation. |
| 30 | + ':', |
| 31 | + |
| 32 | + // The following characters are not allowed in primary-key columns because they are restricted |
| 33 | + // and cannot be used in the `Id` property of a Cosmos DB document. For more information, see: |
| 34 | + // https://learn.microsoft.com/en-us/dotnet/api/microsoft.azure.cosmos.databaseproperties.id?view=azure-dotnet#remarks |
| 35 | + '/', |
| 36 | + '\\', |
| 37 | + '#', |
| 38 | + '?' |
| 39 | + }; |
| 40 | + |
| 41 | + private static final ColumnVisitor PRIMARY_KEY_COLUMN_CHECKER = |
| 42 | + new ColumnVisitor() { |
| 43 | + @Override |
| 44 | + public void visit(BooleanColumn column) {} |
| 45 | + |
| 46 | + @Override |
| 47 | + public void visit(IntColumn column) {} |
| 48 | + |
| 49 | + @Override |
| 50 | + public void visit(BigIntColumn column) {} |
| 51 | + |
| 52 | + @Override |
| 53 | + public void visit(FloatColumn column) {} |
| 54 | + |
| 55 | + @Override |
| 56 | + public void visit(DoubleColumn column) {} |
| 57 | + |
| 58 | + @Override |
| 59 | + public void visit(TextColumn column) { |
| 60 | + String value = column.getTextValue(); |
| 61 | + assert value != null; |
| 62 | + |
| 63 | + for (char illegalCharacter : ILLEGAL_CHARACTERS_IN_PRIMARY_KEY) { |
| 64 | + if (value.indexOf(illegalCharacter) != -1) { |
| 65 | + throw new IllegalArgumentException( |
| 66 | + CoreError.COSMOS_PRIMARY_KEY_CONTAINS_ILLEGAL_CHARACTER.buildMessage( |
| 67 | + column.getName(), value)); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void visit(BlobColumn column) {} |
| 74 | + }; |
| 75 | + |
16 | 76 | public CosmosOperationChecker(
|
17 | 77 | DatabaseConfig databaseConfig, TableMetadataManager metadataManager) {
|
18 | 78 | super(databaseConfig, metadataManager);
|
19 | 79 | }
|
20 | 80 |
|
| 81 | + @Override |
| 82 | + public void check(Get get) throws ExecutionException { |
| 83 | + super.check(get); |
| 84 | + checkPrimaryKey(get); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void check(Scan scan) throws ExecutionException { |
| 89 | + super.check(scan); |
| 90 | + checkPrimaryKey(scan); |
| 91 | + scan.getStartClusteringKey() |
| 92 | + .ifPresent( |
| 93 | + c -> c.getColumns().forEach(column -> column.accept(PRIMARY_KEY_COLUMN_CHECKER))); |
| 94 | + scan.getEndClusteringKey() |
| 95 | + .ifPresent( |
| 96 | + c -> c.getColumns().forEach(column -> column.accept(PRIMARY_KEY_COLUMN_CHECKER))); |
| 97 | + } |
| 98 | + |
21 | 99 | @Override
|
22 | 100 | public void check(Put put) throws ExecutionException {
|
23 | 101 | super.check(put);
|
| 102 | + checkPrimaryKey(put); |
| 103 | + |
24 | 104 | TableMetadata metadata = getTableMetadata(put);
|
25 | 105 | checkCondition(put, metadata);
|
26 | 106 | }
|
27 | 107 |
|
28 | 108 | @Override
|
29 | 109 | public void check(Delete delete) throws ExecutionException {
|
30 | 110 | super.check(delete);
|
| 111 | + checkPrimaryKey(delete); |
| 112 | + |
31 | 113 | TableMetadata metadata = getTableMetadata(delete);
|
32 | 114 | checkCondition(delete, metadata);
|
33 | 115 | }
|
34 | 116 |
|
| 117 | + private void checkPrimaryKey(Operation operation) { |
| 118 | + operation |
| 119 | + .getPartitionKey() |
| 120 | + .getColumns() |
| 121 | + .forEach(column -> column.accept(PRIMARY_KEY_COLUMN_CHECKER)); |
| 122 | + operation |
| 123 | + .getClusteringKey() |
| 124 | + .ifPresent( |
| 125 | + c -> c.getColumns().forEach(column -> column.accept(PRIMARY_KEY_COLUMN_CHECKER))); |
| 126 | + } |
| 127 | + |
35 | 128 | private void checkCondition(Mutation mutation, TableMetadata metadata) {
|
36 | 129 | if (!mutation.getCondition().isPresent()) {
|
37 | 130 | return;
|
|
0 commit comments