Skip to content

Commit 58ca736

Browse files
[Refactor] Strings methods other than MediaType (#9126) (#9128)
This refactors all of the remaining utility methods (except those that convert MediaType and XContentBuilder to String) from the :server to :libs:opensearch-core library. This commit is to keep the Strings refactor surface area lean in preparation for cloud native and serverless refactoring. (cherry picked from commit 1d28fac) Signed-off-by: Nicholas Walter Knize <nknize@apache.org> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent e59b21a commit 58ca736

File tree

12 files changed

+244
-245
lines changed

12 files changed

+244
-245
lines changed

libs/core/src/main/java/org/opensearch/core/common/Strings.java

Lines changed: 224 additions & 135 deletions
Large diffs are not rendered by default.

server/src/main/java/org/opensearch/action/admin/cluster/snapshots/create/CreateSnapshotRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
* <li>must not contain hash sign ('#')</li>
7676
* <li>must not start with underscore ('_')</li>
7777
* <li>must be lowercase</li>
78-
* <li>must not contain invalid file name characters {@link org.opensearch.common.Strings#INVALID_FILENAME_CHARS} </li>
78+
* <li>must not contain invalid file name characters {@link org.opensearch.core.common.Strings#INVALID_FILENAME_CHARS} </li>
7979
* </ul>
8080
*
8181
* @opensearch.internal

server/src/main/java/org/opensearch/cluster/metadata/MetadataCreateIndexService.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,8 @@ public boolean validateDotIndex(String index, @Nullable Boolean isHidden) {
258258
* Validate the name for an index or alias against some static rules.
259259
*/
260260
public static void validateIndexOrAliasName(String index, BiFunction<String, String, ? extends RuntimeException> exceptionCtor) {
261-
if (org.opensearch.common.Strings.validFileName(index) == false) {
262-
throw exceptionCtor.apply(
263-
index,
264-
"must not contain the following characters " + org.opensearch.common.Strings.INVALID_FILENAME_CHARS
265-
);
261+
if (Strings.validFileName(index) == false) {
262+
throw exceptionCtor.apply(index, "must not contain the following characters " + Strings.INVALID_FILENAME_CHARS);
266263
}
267264
if (index.isEmpty()) {
268265
throw exceptionCtor.apply(index, "must not be empty");

server/src/main/java/org/opensearch/cluster/metadata/MetadataIndexTemplateService.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,12 +1502,9 @@ private void validate(String name, @Nullable Settings settings, List<String> ind
15021502
if (indexPattern.startsWith("_")) {
15031503
validationErrors.add("index_pattern [" + indexPattern + "] must not start with '_'");
15041504
}
1505-
if (org.opensearch.common.Strings.validFileNameExcludingAstrix(indexPattern) == false) {
1505+
if (Strings.validFileNameExcludingAstrix(indexPattern) == false) {
15061506
validationErrors.add(
1507-
"index_pattern ["
1508-
+ indexPattern
1509-
+ "] must not contain the following characters "
1510-
+ org.opensearch.common.Strings.INVALID_FILENAME_CHARS
1507+
"index_pattern [" + indexPattern + "] must not contain the following characters " + Strings.INVALID_FILENAME_CHARS
15111508
);
15121509
}
15131510
}

server/src/main/java/org/opensearch/common/Strings.java

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232

3333
package org.opensearch.common;
3434

35-
import org.apache.lucene.util.BytesRefBuilder;
3635
import org.opensearch.ExceptionsHelper;
3736
import org.opensearch.OpenSearchException;
3837
import org.opensearch.core.common.bytes.BytesReference;
@@ -41,11 +40,6 @@
4140
import org.opensearch.core.xcontent.XContentBuilder;
4241

4342
import java.io.IOException;
44-
import java.util.Arrays;
45-
import java.util.Set;
46-
47-
import static java.util.Collections.unmodifiableSet;
48-
import static org.opensearch.common.util.set.Sets.newHashSet;
4943

5044
/**
5145
* String utility class.
@@ -56,90 +50,8 @@ public class Strings {
5650

5751
public static final String[] EMPTY_ARRAY = org.opensearch.core.common.Strings.EMPTY_ARRAY;
5852

59-
// ---------------------------------------------------------------------
60-
// General convenience methods for working with Strings
61-
// ---------------------------------------------------------------------
62-
63-
/**
64-
* Check that the given BytesReference is neither <code>null</code> nor of length 0
65-
* Note: Will return <code>true</code> for a BytesReference that purely consists of whitespace.
66-
*
67-
* @param bytesReference the BytesReference to check (may be <code>null</code>)
68-
* @return <code>true</code> if the BytesReference is not null and has length
69-
* @see org.opensearch.core.common.Strings#hasLength(CharSequence)
70-
*/
71-
public static boolean hasLength(BytesReference bytesReference) {
72-
return (bytesReference != null && bytesReference.length() > 0);
73-
}
74-
75-
/**
76-
* Test whether the given string matches the given substring
77-
* at the given index.
78-
*
79-
* @param str the original string (or StringBuilder)
80-
* @param index the index in the original string to start matching against
81-
* @param substring the substring to match at the given index
82-
*/
83-
public static boolean substringMatch(CharSequence str, int index, CharSequence substring) {
84-
for (int j = 0; j < substring.length(); j++) {
85-
int i = index + j;
86-
if (i >= str.length() || str.charAt(i) != substring.charAt(j)) {
87-
return false;
88-
}
89-
}
90-
return true;
91-
}
92-
93-
// ---------------------------------------------------------------------
94-
// Convenience methods for working with formatted Strings
95-
// ---------------------------------------------------------------------
96-
97-
/**
98-
* Quote the given String with single quotes.
99-
*
100-
* @param str the input String (e.g. "myString")
101-
* @return the quoted String (e.g. "'myString'"),
102-
* or <code>null</code> if the input was <code>null</code>
103-
*/
104-
public static String quote(String str) {
105-
return (str != null ? "'" + str + "'" : null);
106-
}
107-
108-
public static final Set<Character> INVALID_FILENAME_CHARS = unmodifiableSet(
109-
newHashSet('\\', '/', '*', '?', '"', '<', '>', '|', ' ', ',')
110-
);
111-
112-
public static boolean validFileName(String fileName) {
113-
for (int i = 0; i < fileName.length(); i++) {
114-
char c = fileName.charAt(i);
115-
if (INVALID_FILENAME_CHARS.contains(c)) {
116-
return false;
117-
}
118-
}
119-
return true;
120-
}
121-
122-
public static boolean validFileNameExcludingAstrix(String fileName) {
123-
for (int i = 0; i < fileName.length(); i++) {
124-
char c = fileName.charAt(i);
125-
if (c != '*' && INVALID_FILENAME_CHARS.contains(c)) {
126-
return false;
127-
}
128-
}
129-
return true;
130-
}
131-
13253
private Strings() {}
13354

134-
public static byte[] toUTF8Bytes(CharSequence charSequence) {
135-
return toUTF8Bytes(charSequence, new BytesRefBuilder());
136-
}
137-
138-
public static byte[] toUTF8Bytes(CharSequence charSequence, BytesRefBuilder spare) {
139-
spare.copyChars(charSequence);
140-
return Arrays.copyOf(spare.bytes(), spare.length());
141-
}
142-
14355
/**
14456
* Return a {@link String} that is the json representation of the provided {@link ToXContent}.
14557
* Wraps the output into an anonymous object if needed. The content is not pretty-printed

server/src/main/java/org/opensearch/common/settings/PropertyPlaceholder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
package org.opensearch.common.settings;
3434

35-
import org.opensearch.common.Strings;
35+
import org.opensearch.core.common.Strings;
3636

3737
import java.util.HashSet;
3838
import java.util.Objects;

server/src/main/java/org/opensearch/index/snapshots/blobstore/BlobStoreIndexShardSnapshot.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
import org.apache.lucene.util.Version;
3737
import org.opensearch.OpenSearchParseException;
3838
import org.opensearch.core.ParseField;
39-
import org.opensearch.common.Strings;
4039
import org.opensearch.common.lucene.Lucene;
40+
import org.opensearch.core.common.Strings;
4141
import org.opensearch.core.common.unit.ByteSizeValue;
4242
import org.opensearch.core.xcontent.ToXContent;
4343
import org.opensearch.core.xcontent.ToXContentFragment;

server/src/main/java/org/opensearch/repositories/RepositoriesService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
import org.opensearch.cluster.service.ClusterManagerTaskKeys;
5858
import org.opensearch.cluster.service.ClusterManagerTaskThrottler;
5959
import org.opensearch.cluster.service.ClusterService;
60-
import org.opensearch.common.Strings;
6160
import org.opensearch.common.lifecycle.AbstractLifecycleComponent;
6261
import org.opensearch.common.regex.Regex;
6362
import org.opensearch.common.settings.Setting;
@@ -66,6 +65,7 @@
6665
import org.opensearch.common.util.FeatureFlags;
6766
import org.opensearch.common.util.concurrent.ConcurrentCollections;
6867
import org.opensearch.common.util.io.IOUtils;
68+
import org.opensearch.core.common.Strings;
6969
import org.opensearch.repositories.blobstore.MeteredBlobStoreRepository;
7070
import org.opensearch.threadpool.ThreadPool;
7171
import org.opensearch.transport.TransportService;
@@ -599,7 +599,7 @@ private Repository createRepository(RepositoryMetadata repositoryMetadata, Map<S
599599
}
600600

601601
private static void validate(final String repositoryName) {
602-
if (org.opensearch.core.common.Strings.hasLength(repositoryName) == false) {
602+
if (Strings.hasLength(repositoryName) == false) {
603603
throw new RepositoryException(repositoryName, "cannot be empty");
604604
}
605605
if (repositoryName.contains("#")) {

server/src/main/java/org/opensearch/repositories/blobstore/BlobStoreRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@
6767
import org.opensearch.common.Nullable;
6868
import org.opensearch.common.Numbers;
6969
import org.opensearch.common.SetOnce;
70-
import org.opensearch.common.Strings;
7170
import org.opensearch.common.UUIDs;
7271
import org.opensearch.common.blobstore.BlobContainer;
7372
import org.opensearch.common.blobstore.BlobMetadata;
7473
import org.opensearch.common.blobstore.BlobPath;
7574
import org.opensearch.common.blobstore.BlobStore;
7675
import org.opensearch.common.blobstore.DeleteResult;
7776
import org.opensearch.common.blobstore.fs.FsBlobContainer;
77+
import org.opensearch.core.common.Strings;
7878
import org.opensearch.core.common.bytes.BytesArray;
7979
import org.opensearch.core.common.bytes.BytesReference;
8080
import org.opensearch.common.collect.Tuple;

server/src/main/java/org/opensearch/snapshots/SnapshotsService.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@
8080
import org.opensearch.cluster.service.ClusterService;
8181
import org.opensearch.common.Nullable;
8282
import org.opensearch.common.Priority;
83-
import org.opensearch.common.Strings;
8483
import org.opensearch.common.UUIDs;
8584
import org.opensearch.common.collect.Tuple;
8685
import org.opensearch.common.lifecycle.AbstractLifecycleComponent;
@@ -89,6 +88,7 @@
8988
import org.opensearch.common.settings.Settings;
9089
import org.opensearch.common.unit.TimeValue;
9190
import org.opensearch.common.util.concurrent.AbstractRunnable;
91+
import org.opensearch.core.common.Strings;
9292
import org.opensearch.core.common.io.stream.StreamInput;
9393
import org.opensearch.core.index.Index;
9494
import org.opensearch.core.index.shard.ShardId;
@@ -655,7 +655,7 @@ public ClusterState execute(ClusterState currentState) {
655655
"No indices in the source snapshot ["
656656
+ sourceSnapshotId
657657
+ "] matched requested pattern ["
658-
+ org.opensearch.core.common.Strings.arrayToCommaDelimitedString(request.indices())
658+
+ Strings.arrayToCommaDelimitedString(request.indices())
659659
+ "]"
660660
);
661661
}
@@ -1001,7 +1001,7 @@ private static void validate(String repositoryName, String snapshotName, Cluster
10011001
}
10021002

10031003
private static void validate(final String repositoryName, final String snapshotName) {
1004-
if (org.opensearch.core.common.Strings.hasLength(snapshotName) == false) {
1004+
if (Strings.hasLength(snapshotName) == false) {
10051005
throw new InvalidSnapshotNameException(repositoryName, snapshotName, "cannot be empty");
10061006
}
10071007
if (snapshotName.contains(" ")) {
@@ -2246,7 +2246,7 @@ public void deleteSnapshots(final DeleteSnapshotRequest request, final ActionLis
22462246
logger.info(
22472247
() -> new ParameterizedMessage(
22482248
"deleting snapshots [{}] from repository [{}]",
2249-
org.opensearch.core.common.Strings.arrayToCommaDelimitedString(snapshotNames),
2249+
Strings.arrayToCommaDelimitedString(snapshotNames),
22502250
repoName
22512251
)
22522252
);

server/src/test/java/org/opensearch/cluster/metadata/MetadataCreateIndexServiceTests.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,11 @@ public void testValidateIndexName() throws Exception {
680680
false,
681681
new AwarenessReplicaBalance(Settings.EMPTY, clusterService.getClusterSettings())
682682
);
683-
validateIndexName(checkerService, "index?name", "must not contain the following characters " + Strings.INVALID_FILENAME_CHARS);
683+
validateIndexName(
684+
checkerService,
685+
"index?name",
686+
"must not contain the following characters " + org.opensearch.core.common.Strings.INVALID_FILENAME_CHARS
687+
);
684688

685689
validateIndexName(checkerService, "index#name", "must not contain '#'");
686690

server/src/test/java/org/opensearch/repositories/RepositoriesServiceTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@
4747
import org.opensearch.cluster.node.DiscoveryNode;
4848
import org.opensearch.cluster.service.ClusterApplierService;
4949
import org.opensearch.cluster.service.ClusterService;
50-
import org.opensearch.common.Strings;
5150
import org.opensearch.common.UUIDs;
5251
import org.opensearch.common.blobstore.BlobPath;
5352
import org.opensearch.common.blobstore.BlobStore;
5453
import org.opensearch.common.lifecycle.Lifecycle;
5554
import org.opensearch.common.lifecycle.LifecycleListener;
5655
import org.opensearch.common.settings.Settings;
56+
import org.opensearch.core.common.Strings;
5757
import org.opensearch.core.xcontent.NamedXContentRegistry;
5858
import org.opensearch.index.mapper.MapperService;
5959
import org.opensearch.core.index.shard.ShardId;

0 commit comments

Comments
 (0)