Skip to content

Commit

Permalink
Remove namespace prefix (#333)
Browse files Browse the repository at this point in the history
  • Loading branch information
brfrn169 committed Oct 31, 2021
1 parent 2aed41a commit b7a6e02
Show file tree
Hide file tree
Showing 38 changed files with 204 additions and 492 deletions.
3 changes: 0 additions & 3 deletions conf/database.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ scalar.db.password=cassandra
# Storage implementation. "cassandra" or "cosmos" or "dynamo" or "jdbc" or "grpc" can be set. Default storage is "cassandra".
#scalar.db.storage=cassandra

# Namespace prefix. The default is empty.
#scalar.db.namespace_prefix=

# The type of the transaction manager. "consensus-commit" or "jdbc" or "grpc" can be set. The default is "consensus-commit"
#scalar.db.transaction_manager=consensus-commit

Expand Down
1 change: 0 additions & 1 deletion core/src/main/java/com/scalar/db/api/Delete.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ public boolean equals(Object o) {
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("namespacePrefix", forNamespacePrefix())
.add("namespace", forNamespace())
.add("table", forTable())
.add("partitionKey", getPartitionKey())
Expand Down
1 change: 0 additions & 1 deletion core/src/main/java/com/scalar/db/api/Get.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ public boolean equals(Object o) {
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("namespacePrefix", forNamespacePrefix())
.add("namespace", forNamespace())
.add("table", forTable())
.add("partitionKey", getPartitionKey())
Expand Down
1 change: 0 additions & 1 deletion core/src/main/java/com/scalar/db/api/Mutation.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ public int hashCode() {
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("namespacePrefix", forNamespacePrefix())
.add("namespace", forNamespace())
.add("table", forTable())
.add("partitionKey", getPartitionKey())
Expand Down
75 changes: 2 additions & 73 deletions core/src/main/java/com/scalar/db/api/Operation.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,18 @@ public abstract class Operation {

private final Key partitionKey;
private final Optional<Key> clusteringKey;
private Optional<String> namespacePrefix;
private Optional<String> namespace;
private Optional<String> tableName;
private Consistency consistency;

public Operation(Key partitionKey, Key clusteringKey) {
this.partitionKey = checkNotNull(partitionKey);
this.clusteringKey = Optional.ofNullable(clusteringKey);
namespacePrefix = Optional.empty();
namespace = Optional.empty();
tableName = Optional.empty();
consistency = Consistency.SEQUENTIAL;
}

/**
* Returns the namespace prefix for this operation
*
* @return an {@code Optional} with the returned namespace prefix
*/
@Nonnull
public Optional<String> forNamespacePrefix() {
return namespacePrefix;
}

/**
* Returns the namespace for this operation
*
Expand All @@ -57,24 +45,6 @@ public Optional<String> forNamespace() {
return namespace;
}

/**
* Returns the namespace with the prefix for this operation
*
* @return an {@code Optional} with the returned namespace with the prefix
*/
@Nonnull
public Optional<String> forFullNamespace() {
if (!namespace.isPresent()) {
LOGGER.warn("namespace is not set. So, you might get an unexpected return value.");
}

if (namespace.isPresent() && namespacePrefix.isPresent()) {
return Optional.of(namespacePrefix.get() + namespace.get());
} else {
return namespace;
}
}

/**
* Returns the table name for this operation
*
Expand All @@ -92,47 +62,11 @@ public Optional<String> forTable() {
*/
@Nonnull
public Optional<String> forFullTableName() {
return forFullTableName(true);
}

/**
* Returns the full table name with the unprefixed namespace for this operation
*
* @return an {@code Optional} with the returned the full table name
*/
@Nonnull
public Optional<String> forUnprefixedFullTableName() {
return forFullTableName(false);
}

@Nonnull
private Optional<String> forFullTableName(boolean withPrefix) {
if (!namespace.isPresent() || !tableName.isPresent()) {
LOGGER.warn("namespace or table name isn't specified");
return Optional.empty();
}

StringBuilder builder = new StringBuilder();
if (withPrefix && namespacePrefix.isPresent()) {
builder.append(namespacePrefix.get());
}
builder.append(namespace.get());
builder.append(".");
builder.append(tableName.get());

return Optional.of(builder.toString());
}

/**
* Sets the specified target namespace prefix for this operation We don't have to set it
* explicitly since the prefix will be set by Scalar DB.
*
* @param prefix target namespace prefix for this operation
* @return this object
*/
public Operation forNamespacePrefix(String prefix) {
this.namespacePrefix = Optional.ofNullable(prefix);
return this;
return Optional.of(namespace.get() + "." + tableName.get());
}

/**
Expand Down Expand Up @@ -225,10 +159,6 @@ public boolean equals(Object o) {
clusteringKey.orElse(null),
other.clusteringKey.orElse(null),
Ordering.natural().nullsFirst())
.compare(
namespacePrefix.orElse(null),
other.namespacePrefix.orElse(null),
Ordering.natural().nullsFirst())
.compare(
namespace.orElse(null),
other.namespace.orElse(null),
Expand All @@ -244,8 +174,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return Objects.hash(
partitionKey, clusteringKey, namespacePrefix, namespace, tableName, consistency);
return Objects.hash(partitionKey, clusteringKey, namespace, tableName, consistency);
}

/**
Expand Down
1 change: 0 additions & 1 deletion core/src/main/java/com/scalar/db/api/Put.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ public int hashCode() {
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("namespacePrefix", forNamespacePrefix())
.add("namespace", forNamespace())
.add("table", forTable())
.add("partitionKey", getPartitionKey())
Expand Down
1 change: 0 additions & 1 deletion core/src/main/java/com/scalar/db/api/Selection.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ public int hashCode() {
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("namespacePrefix", forNamespacePrefix())
.add("namespace", forNamespace())
.add("table", forTable())
.add("partitionKey", getPartitionKey())
Expand Down
13 changes: 0 additions & 13 deletions core/src/main/java/com/scalar/db/config/DatabaseConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public class DatabaseConfig {
private Optional<String> password;
private Class<? extends DistributedStorage> storageClass;
private Class<? extends DistributedStorageAdmin> adminClass;
private Optional<String> namespacePrefix;
private Class<? extends DistributedTransactionManager> transactionManagerClass;
private Class<? extends TwoPhaseCommitTransactionManager> twoPhaseCommitTransactionManagerClass;
private Isolation isolation = Isolation.SNAPSHOT;
Expand All @@ -59,7 +58,6 @@ public class DatabaseConfig {
public static final String USERNAME = PREFIX + "username";
public static final String PASSWORD = PREFIX + "password";
public static final String STORAGE = PREFIX + "storage";
public static final String NAMESPACE_PREFIX = PREFIX + "namespace_prefix";
public static final String TRANSACTION_MANAGER = PREFIX + "transaction_manager";
public static final String ISOLATION_LEVEL = PREFIX + "isolation_level";
public static final String TABLE_METADATA_CACHE_EXPIRATION_TIME_SECS =
Expand Down Expand Up @@ -131,16 +129,9 @@ protected void load() {
}
username = Optional.ofNullable(props.getProperty(USERNAME));
password = Optional.ofNullable(props.getProperty(PASSWORD));

if (Strings.isNullOrEmpty(props.getProperty(NAMESPACE_PREFIX))) {
namespacePrefix = Optional.empty();
} else {
namespacePrefix = Optional.of(props.getProperty(NAMESPACE_PREFIX) + "_");
}
} else {
username = Optional.empty();
password = Optional.empty();
namespacePrefix = Optional.empty();
}

transactionManagerClass = ConsensusCommitManager.class;
Expand Down Expand Up @@ -222,10 +213,6 @@ public Class<? extends DistributedStorageAdmin> getAdminClass() {
return twoPhaseCommitTransactionManagerClass;
}

public Optional<String> getNamespacePrefix() {
return namespacePrefix;
}

public Class<? extends DistributedTransactionManager> getTransactionManagerClass() {
return transactionManagerClass;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import com.scalar.db.api.TwoPhaseCommitTransactionManager;
import com.scalar.db.config.DatabaseConfig;

/** A factory class to instantiate {@link DistributedTransactionManager} */
/**
* A factory class to instantiate {@link DistributedTransactionManager} and {@link
* TwoPhaseCommitTransactionManager}
*/
public class TransactionFactory {
private final Injector injector;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public class Cassandra implements DistributedStorage {
private final ClusterManager clusterManager;
private final TableMetadataManager metadataManager;
private final OperationChecker operationChecker;
private final Optional<String> namespacePrefix;
private Optional<String> namespace;
private Optional<String> tableName;

Expand All @@ -63,7 +62,6 @@ public Cassandra(DatabaseConfig config) {
metadataManager = new TableMetadataManager(new CassandraAdmin(clusterManager, config), config);
operationChecker = new OperationChecker(metadataManager);

namespacePrefix = config.getNamespacePrefix();
namespace = Optional.empty();
tableName = Optional.empty();
}
Expand Down Expand Up @@ -98,7 +96,7 @@ public Optional<String> getTable() {
@Nonnull
public Optional<Result> get(Get get) throws ExecutionException {
LOGGER.debug("executing get operation with " + get);
Utility.setTargetToIfNot(get, namespacePrefix, namespace, tableName);
Utility.setTargetToIfNot(get, namespace, tableName);
operationChecker.check(get);
TableMetadata metadata = metadataManager.getTableMetadata(get);
Utility.addProjectionsForKeys(get, metadata);
Expand All @@ -119,7 +117,7 @@ public Optional<Result> get(Get get) throws ExecutionException {
@Nonnull
public Scanner scan(Scan scan) throws ExecutionException {
LOGGER.debug("executing scan operation with " + scan);
Utility.setTargetToIfNot(scan, namespacePrefix, namespace, tableName);
Utility.setTargetToIfNot(scan, namespace, tableName);
operationChecker.check(scan);
TableMetadata metadata = metadataManager.getTableMetadata(scan);
Utility.addProjectionsForKeys(scan, metadata);
Expand All @@ -132,7 +130,7 @@ public Scanner scan(Scan scan) throws ExecutionException {
@Override
public void put(Put put) throws ExecutionException {
LOGGER.debug("executing put operation with " + put);
Utility.setTargetToIfNot(put, namespacePrefix, namespace, tableName);
Utility.setTargetToIfNot(put, namespace, tableName);
operationChecker.check(put);
handlers.get(put).handle(put);
}
Expand All @@ -146,7 +144,7 @@ public void put(List<Put> puts) throws ExecutionException {
@Override
public void delete(Delete delete) throws ExecutionException {
LOGGER.debug("executing delete operation with " + delete);
Utility.setTargetToIfNot(delete, namespacePrefix, namespace, tableName);
Utility.setTargetToIfNot(delete, namespace, tableName);
operationChecker.check(delete);
handlers.delete().handle(delete);
}
Expand All @@ -171,7 +169,7 @@ public void mutate(List<? extends Mutation> mutations) throws ExecutionException
return;
}

Utility.setTargetToIfNot(mutations, namespacePrefix, namespace, tableName);
Utility.setTargetToIfNot(mutations, namespace, tableName);
operationChecker.check(mutations);
for (Mutation mutation : mutations) {
operationChecker.check(mutation);
Expand Down
Loading

0 comments on commit b7a6e02

Please sign in to comment.