forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Remote Store] Add remote store utils useful to name metadata files (o…
…pensearch-project#8412) Signed-off-by: Sachin Kale <kalsac@amazon.com>
- Loading branch information
1 parent
f14ed89
commit 92d1df2
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
server/src/main/java/org/opensearch/index/remote/RemoteStoreUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.index.remote; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* Utils for remote store | ||
* | ||
* @opensearch.internal | ||
*/ | ||
public class RemoteStoreUtils { | ||
public static final int LONG_MAX_LENGTH = String.valueOf(Long.MAX_VALUE).length(); | ||
|
||
/** | ||
* This method subtracts given numbers from Long.MAX_VALUE and returns a string representation of the result. | ||
* The resultant string is guaranteed to be of the same length that of Long.MAX_VALUE. If shorter, we add left padding | ||
* of 0s to the string. | ||
* @param num number to get the inverted long string for | ||
* @return String value of Long.MAX_VALUE - num | ||
*/ | ||
public static String invertLong(long num) { | ||
if (num < 0) { | ||
throw new IllegalArgumentException("Negative long values are not allowed"); | ||
} | ||
String invertedLong = String.valueOf(Long.MAX_VALUE - num); | ||
char[] characterArray = new char[LONG_MAX_LENGTH - invertedLong.length()]; | ||
Arrays.fill(characterArray, '0'); | ||
|
||
return new String(characterArray) + invertedLong; | ||
} | ||
|
||
/** | ||
* This method converts the given string into long and subtracts it from Long.MAX_VALUE | ||
* @param str long in string format to be inverted | ||
* @return long value of the invert result | ||
*/ | ||
public static long invertLong(String str) { | ||
long num = Long.parseLong(str); | ||
if (num < 0) { | ||
throw new IllegalArgumentException("Strings representing negative long values are not allowed"); | ||
} | ||
return Long.MAX_VALUE - num; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
server/src/test/java/org/opensearch/index/remote/RemoteStoreUtilsTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.index.remote; | ||
|
||
import org.opensearch.test.OpenSearchTestCase; | ||
|
||
public class RemoteStoreUtilsTests extends OpenSearchTestCase { | ||
|
||
public void testInvertToStrInvalid() { | ||
assertThrows(IllegalArgumentException.class, () -> RemoteStoreUtils.invertLong(-1)); | ||
} | ||
|
||
public void testInvertToStrValid() { | ||
assertEquals("9223372036854774573", RemoteStoreUtils.invertLong(1234)); | ||
assertEquals("0000000000000001234", RemoteStoreUtils.invertLong(9223372036854774573L)); | ||
} | ||
|
||
public void testInvertToLongInvalid() { | ||
assertThrows(IllegalArgumentException.class, () -> RemoteStoreUtils.invertLong("-5")); | ||
} | ||
|
||
public void testInvertToLongValid() { | ||
assertEquals(1234, RemoteStoreUtils.invertLong("9223372036854774573")); | ||
assertEquals(9223372036854774573L, RemoteStoreUtils.invertLong("0000000000000001234")); | ||
} | ||
|
||
public void testinvert() { | ||
assertEquals(0, RemoteStoreUtils.invertLong(RemoteStoreUtils.invertLong(0))); | ||
assertEquals(Long.MAX_VALUE, RemoteStoreUtils.invertLong(RemoteStoreUtils.invertLong(Long.MAX_VALUE))); | ||
for (int i = 0; i < 10; i++) { | ||
long num = randomLongBetween(1, Long.MAX_VALUE); | ||
assertEquals(num, RemoteStoreUtils.invertLong(RemoteStoreUtils.invertLong(num))); | ||
} | ||
} | ||
} |