Skip to content

Commit

Permalink
[Remote Store] Add remote store utils useful to name metadata files (o…
Browse files Browse the repository at this point in the history
…pensearch-project#8412)

Signed-off-by: Sachin Kale <[email protected]>
  • Loading branch information
sachinpkale authored Jul 4, 2023
1 parent 2c5a22d commit a3b515a
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
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;
}
}
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)));
}
}
}

0 comments on commit a3b515a

Please sign in to comment.