Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove prefix from indexes of DynamoDB #161

Merged
merged 4 commits into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/main/java/com/scalar/db/api/Operation.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,28 @@ public Optional<String> forTable() {
*/
@Nonnull
public Optional<String> forFullTableName() {
return forFullTableName(true);
}

/**
* Returns the full table name without the prefix with the namespace for this operation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Returns the full table name without the prefix with the namespace for this operation
* Returns the full table name without the unprefixed namespace for this operation

*
* @return an {@code Optional} with the returned the full table name
*/
@Nonnull
public Optional<String> forFullTableNameWithoutPrefix() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public Optional<String> forFullTableNameWithoutPrefix() {
public Optional<String> forUnprefixedFullTableName() {

This sounds a bit better to me.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Fixed.

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 (namespacePrefix.isPresent()) {
if (withPrefix && namespacePrefix.isPresent()) {
builder.append(namespacePrefix.get());
}
builder.append(namespace.get());
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/com/scalar/db/storage/dynamo/DynamoOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,20 @@ public String getTableName() {

@Nonnull
public String getIndexName(String clusteringKey) {
return getTableName() + "." + INDEX_NAME_PREFIX + "." + clusteringKey;
return operation.forFullTableNameWithoutPrefix().get()
+ "."
+ INDEX_NAME_PREFIX
+ "."
+ clusteringKey;
}

@Nonnull
public String getGlobalIndexName(String column) {
return getTableName() + "." + GLOBAL_INDEX_NAME_PREFIX + "." + column;
return operation.forFullTableNameWithoutPrefix().get()
+ "."
+ GLOBAL_INDEX_NAME_PREFIX
+ "."
+ column;
}

@Nonnull
Expand Down
4 changes: 2 additions & 2 deletions tools/scalar-schema/src/scalar_schema/common.clj
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@
(if prefix
(map (fn [table-schema]
(assoc table-schema
:database
(str prefix \_ (:database table-schema))))
:prefix prefix
:database (str prefix \_ (:database table-schema))))
schema)
schema))

Expand Down
6 changes: 3 additions & 3 deletions tools/scalar-schema/src/scalar_schema/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
"The number of replicas. This options is ignored when Cosmos DB and DynamoDB."
:default 1 :parse-fn #(Integer/parseInt %)]
["-n" "--network-strategy NETWORK_STRATEGY"
"The network topology strategy. SimpleStrategy or NetworkTopologyStrategy. This options is ignored when Cosmos DB and DynamoDB."]
"The network topology strategy. SimpleStrategy or NetworkTopologyStrategy. This option is ignored when Cosmos DB and DynamoDB."]
["-D" "--delete-all" "All database will be deleted, if this is enabled."]
[nil "--region REGION" "Region where the tool creates tables for DynamoDB"]
[nil "--prefix NAMESPACE_PREFIX" "Namespace prefix. The prefix is added to all the namespaces."]
[nil "--no-scaling" "Disable auto-scaling"]
[nil "--no-backup" "Disable continuous backup"]
[nil "--no-scaling" "Disable auto-scaling for Cosmos DB and DynamoDB. This option is ignored when Cassandra"]
[nil "--no-backup" "Disable continuous backup for DynamoDB. This option is ignored when Cosmos DB and Cassandra."]
[nil "--help"]])

(defn -main [& args]
Expand Down
14 changes: 11 additions & 3 deletions tools/scalar-schema/src/scalar_schema/dynamo/common.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns scalar-schema.dynamo.common
(:require [scalar-schema.common :as common])
(:require [clojure.string :as str]
[scalar-schema.common :as common])
(:import (software.amazon.awssdk.auth.credentials AwsBasicCredentials
StaticCredentialsProvider)))

Expand All @@ -15,10 +16,17 @@
[{:keys [database table]}]
(common/get-fullname database table))

(defn- get-table-name-without-prefix
[{:keys [prefix] :as schema}]
(str/replace-first (get-table-name schema)
(java.util.regex.Pattern/compile (str prefix \_)) ""))

(defn get-index-name
[schema key-name]
(str (get-table-name schema) \. INDEX_NAME_PREFIX \. key-name))
(str (get-table-name-without-prefix schema)
\. INDEX_NAME_PREFIX \. key-name))

(defn get-global-index-name
[schema key-name]
(str (get-table-name schema) \. GLOBAL_INDEX_NAME_PREFIX \. key-name))
(str (get-table-name-without-prefix schema)
\. GLOBAL_INDEX_NAME_PREFIX \. key-name))