Skip to content

Commit

Permalink
Remove prefix from indexes of DynamoDB (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
yito88 authored Feb 25, 2021
1 parent 2083797 commit 159622c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 11 deletions.
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 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 (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.forUnprefixedFullTableName().get()
+ "."
+ INDEX_NAME_PREFIX
+ "."
+ clusteringKey;
}

@Nonnull
public String getGlobalIndexName(String column) {
return getTableName() + "." + GLOBAL_INDEX_NAME_PREFIX + "." + column;
return operation.forUnprefixedFullTableName().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))

0 comments on commit 159622c

Please sign in to comment.