-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use yyyyMMddHHmmssSSS formatted datetime instead of UUID for index id (…
…#696) * Use yyyyMMddHHmmssSSS formatted datetime instead of UUID for index id * Add back test, spotlessApply * Revert changes to RestoreIncrementalCommand * Change setDateTime parameter to setId * Reverted change in test * Update grpc-gateway * Always use UTC time for index id
- Loading branch information
Showing
10 changed files
with
143 additions
and
33 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
41 changes: 41 additions & 0 deletions
41
src/main/java/com/yelp/nrtsearch/server/utils/IndexIdUtil.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 @@ | ||
/* | ||
* Copyright 2024 Yelp Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.yelp.nrtsearch.server.utils; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public class IndexIdUtil { | ||
|
||
private static final DateTimeFormatter INDEX_ID_FORMATTER = | ||
DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"); | ||
|
||
/** Generate a unique index id based on the current time formatted as yyyyMMddHHmmssSSS. */ | ||
public static String generateIndexId() { | ||
return INDEX_ID_FORMATTER.format(LocalDateTime.now(ZoneId.of("UTC"))); | ||
} | ||
|
||
/** Check if the given string is a valid index id. */ | ||
public static boolean isIndexId(String indexId) { | ||
try { | ||
INDEX_ID_FORMATTER.parse(indexId); | ||
return true; | ||
} catch (Exception e) { | ||
return false; | ||
} | ||
} | ||
} |
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
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
53 changes: 53 additions & 0 deletions
53
src/test/java/com/yelp/nrtsearch/server/utils/IndexIdUtilTest.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,53 @@ | ||
/* | ||
* Copyright 2024 Yelp Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.yelp.nrtsearch.server.utils; | ||
|
||
import static org.junit.Assert.*; | ||
import static org.mockito.Mockito.mockStatic; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
import org.junit.Test; | ||
import org.mockito.MockedStatic; | ||
|
||
public class IndexIdUtilTest { | ||
|
||
@Test | ||
public void testGenerateIndexId() { | ||
LocalDateTime mockTime = LocalDateTime.of(2024, 8, 20, 12, 34, 56, 789000000); | ||
try (MockedStatic<LocalDateTime> mockLocalDateTime = mockStatic(LocalDateTime.class)) { | ||
mockLocalDateTime.when(() -> LocalDateTime.now(ZoneId.of("UTC"))).thenReturn(mockTime); | ||
String indexId = IndexIdUtil.generateIndexId(); | ||
assertEquals("20240820123456789", indexId); | ||
} | ||
} | ||
|
||
@Test | ||
public void testIsIndexId() { | ||
assertTrue(IndexIdUtil.isIndexId("20240820123456789")); | ||
assertTrue(IndexIdUtil.isIndexId("19701010000000000")); | ||
assertTrue(IndexIdUtil.isIndexId("20391229233759999")); | ||
|
||
assertFalse(IndexIdUtil.isIndexId("20241329233759999")); | ||
assertFalse(IndexIdUtil.isIndexId("20241232233759999")); | ||
assertFalse(IndexIdUtil.isIndexId("20241231243759999")); | ||
assertFalse(IndexIdUtil.isIndexId("09d9c9e4-483e-4a90-9c4F-D342c8da1210")); | ||
assertFalse(IndexIdUtil.isIndexId("09d9c9e4-483e-4a90-D342c8da1210")); | ||
assertFalse(IndexIdUtil.isIndexId("other_file")); | ||
assertFalse(IndexIdUtil.isIndexId("_3.cfs")); | ||
assertFalse(IndexIdUtil.isIndexId("segments")); | ||
} | ||
} |
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