|
| 1 | +/* |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.datastax.driver.core.schemabuilder; |
| 17 | + |
| 18 | +import com.google.common.base.Optional; |
| 19 | + |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +/** |
| 23 | + * The keyspace options used in CREATE KEYSPACE or ALTER KEYSPACE statements. |
| 24 | + */ |
| 25 | +public class KeyspaceOptions extends SchemaStatement { |
| 26 | + |
| 27 | + private Optional<Map<String, Object>> replication = Optional.absent(); |
| 28 | + private Optional<Boolean> durableWrites = Optional.absent(); |
| 29 | + |
| 30 | + private final String command; |
| 31 | + private final String keyspaceName; |
| 32 | + |
| 33 | + public KeyspaceOptions(String command, String keyspaceName) { |
| 34 | + validateNotEmpty(keyspaceName, "Keyspace name"); |
| 35 | + validateNotKeyWord(keyspaceName, |
| 36 | + String.format("The keyspace name '%s' is not allowed because it is a reserved keyword", keyspaceName)); |
| 37 | + |
| 38 | + this.command = command; |
| 39 | + this.keyspaceName = keyspaceName; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Define the replication options for the statement. |
| 44 | + * |
| 45 | + * @param replication replication properties map |
| 46 | + * @return this {@code KeyspaceOptions} object |
| 47 | + */ |
| 48 | + public KeyspaceOptions replication(Map<String, Object> replication) { |
| 49 | + validateReplicationOptions(replication); |
| 50 | + this.replication = Optional.fromNullable(replication); |
| 51 | + return this; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Define the durable writes option for the statement. If set to false, |
| 56 | + * data written to the keyspace will bypass the commit log. |
| 57 | + * |
| 58 | + * @param durableWrites durable write option |
| 59 | + * @return this {@code KeyspaceOptions} object |
| 60 | + */ |
| 61 | + public KeyspaceOptions durableWrites(Boolean durableWrites) { |
| 62 | + this.durableWrites = Optional.fromNullable(durableWrites); |
| 63 | + return this; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + String buildInternal() { |
| 68 | + StringBuilder builtStatement = new StringBuilder(STATEMENT_START); |
| 69 | + builtStatement.append(command); |
| 70 | + builtStatement.append(" "); |
| 71 | + builtStatement.append(keyspaceName); |
| 72 | + builtStatement.append("\n\tWITH\n\t\t"); |
| 73 | + |
| 74 | + boolean putSeparator = false; |
| 75 | + if (replication.isPresent()) { |
| 76 | + |
| 77 | + builtStatement.append("REPLICATION = {"); |
| 78 | + |
| 79 | + int l = replication.get().entrySet().size(); |
| 80 | + for (Map.Entry<String, Object> e : replication.get().entrySet()) { |
| 81 | + builtStatement.append("'") |
| 82 | + .append(e.getKey()) |
| 83 | + .append("'") |
| 84 | + .append(": "); |
| 85 | + |
| 86 | + if (e.getValue() instanceof String) { |
| 87 | + builtStatement.append("'") |
| 88 | + .append(e.getValue()) |
| 89 | + .append("'"); |
| 90 | + } else { |
| 91 | + builtStatement.append(e.getValue()); |
| 92 | + } |
| 93 | + |
| 94 | + if (--l > 0) { |
| 95 | + builtStatement.append(", "); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + builtStatement.append('}'); |
| 100 | + builtStatement.append("\n\t\t"); |
| 101 | + putSeparator = true; |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | + if (durableWrites.isPresent()) { |
| 106 | + if (putSeparator) { |
| 107 | + builtStatement.append("AND "); |
| 108 | + } |
| 109 | + |
| 110 | + builtStatement.append("DURABLE_WRITES = " + durableWrites.get().toString()); |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + return builtStatement.toString(); |
| 115 | + } |
| 116 | + |
| 117 | + static void validateReplicationOptions(Map<String, Object> replicationOptions) { |
| 118 | + if (replicationOptions != null && !replicationOptions.containsKey("class")) { |
| 119 | + throw new IllegalArgumentException("Replication Strategy 'class' should be provided"); |
| 120 | + } |
| 121 | + |
| 122 | + if (replicationOptions != null && !(replicationOptions.get("class") instanceof String)) { |
| 123 | + throw new IllegalArgumentException("Replication Strategy should be of type String"); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments