Repository: incubator-distributedlog Updated Branches: refs/heads/master 06ab82162 -> 945c14a99
DL-174: added getParent method to Utils to replace usage of File.getParent which changes / to \ on Windows â¦ich changes / to \ in Windows. Author: adamtracymartin <atmar...@yahoo.com> Reviewers: Sijie Guo <si...@apache.org> Closes #104 from adamtracymartin/DL-174 Project: http://git-wip-us.apache.org/repos/asf/incubator-distributedlog/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-distributedlog/commit/945c14a9 Tree: http://git-wip-us.apache.org/repos/asf/incubator-distributedlog/tree/945c14a9 Diff: http://git-wip-us.apache.org/repos/asf/incubator-distributedlog/diff/945c14a9 Branch: refs/heads/master Commit: 945c14a99bdf8267f325e671600907ec6cff53d6 Parents: 06ab821 Author: adamtracymartin <atmar...@yahoo.com> Authored: Wed Apr 12 21:38:32 2017 -0700 Committer: Sijie Guo <si...@apache.org> Committed: Wed Apr 12 21:38:32 2017 -0700 ---------------------------------------------------------------------- .../impl/metadata/ZKLogStreamMetadataStore.java | 4 +-- .../org/apache/distributedlog/util/Utils.java | 31 ++++++++++++++++++++ .../apache/distributedlog/util/TestUtils.java | 28 ++++++++++++++++++ 3 files changed, 61 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-distributedlog/blob/945c14a9/distributedlog-core/src/main/java/org/apache/distributedlog/impl/metadata/ZKLogStreamMetadataStore.java ---------------------------------------------------------------------- diff --git a/distributedlog-core/src/main/java/org/apache/distributedlog/impl/metadata/ZKLogStreamMetadataStore.java b/distributedlog-core/src/main/java/org/apache/distributedlog/impl/metadata/ZKLogStreamMetadataStore.java index e6891c7..30f9dd4 100644 --- a/distributedlog-core/src/main/java/org/apache/distributedlog/impl/metadata/ZKLogStreamMetadataStore.java +++ b/distributedlog-core/src/main/java/org/apache/distributedlog/impl/metadata/ZKLogStreamMetadataStore.java @@ -335,7 +335,7 @@ public class ZKLogStreamMetadataStore implements LogStreamMetadataStore { // Note re. persistent lock state initialization: the read lock persistent state (path) is // initialized here but only used in the read handler. The reason is its more convenient and // less error prone to manage all stream structure in one place. - final String logRootParentPath = new File(logRootPath).getParent(); + final String logRootParentPath = Utils.getParent(logRootPath); final String logSegmentsPath = logRootPath + LOGSEGMENTS_PATH; final String maxTxIdPath = logRootPath + MAX_TXID_PATH; final String lockPath = logRootPath + LOCK_PATH; @@ -383,7 +383,7 @@ public class ZKLogStreamMetadataStore implements LogStreamMetadataStore { if (pathExists(metadatas.get(MetadataIndex.LOG_ROOT_PARENT))) { pathsToCreate.add(null); } else { - String logRootParentPath = new File(logRootPath).getParent(); + String logRootParentPath = Utils.getParent(logRootPath); pathsToCreate.add(DistributedLogConstants.EMPTY_BYTES); zkOps.add(Op.create(logRootParentPath, DistributedLogConstants.EMPTY_BYTES, acl, createMode)); } http://git-wip-us.apache.org/repos/asf/incubator-distributedlog/blob/945c14a9/distributedlog-core/src/main/java/org/apache/distributedlog/util/Utils.java ---------------------------------------------------------------------- diff --git a/distributedlog-core/src/main/java/org/apache/distributedlog/util/Utils.java b/distributedlog-core/src/main/java/org/apache/distributedlog/util/Utils.java index 17eb8e3..7a09eeb 100644 --- a/distributedlog-core/src/main/java/org/apache/distributedlog/util/Utils.java +++ b/distributedlog-core/src/main/java/org/apache/distributedlog/util/Utils.java @@ -604,4 +604,35 @@ public class Utils { executorService).map(VoidFunctions.LIST_TO_VOID_FUNC); } + /** + * Gets the parent of a path. + * + * @param path + * path to get the parent of + * @return parent of the path or null if no parent exists. + */ + public static String getParent(final String path) { + if (path == null) { + return null; + } + if (path.length() < 2) { + return null; + } + int firstIndex = path.indexOf("/"); + if (firstIndex == -1) { + return null; + } + int lastIndex = path.lastIndexOf("/"); + if (lastIndex == path.length() - 1) { + lastIndex = path.substring(0, path.length() - 1).lastIndexOf("/"); + } + if (lastIndex == -1) { + return null; + } + if (lastIndex == 0) { + return "/"; + } + return path.substring(0, lastIndex); + } + } http://git-wip-us.apache.org/repos/asf/incubator-distributedlog/blob/945c14a9/distributedlog-core/src/test/java/org/apache/distributedlog/util/TestUtils.java ---------------------------------------------------------------------- diff --git a/distributedlog-core/src/test/java/org/apache/distributedlog/util/TestUtils.java b/distributedlog-core/src/test/java/org/apache/distributedlog/util/TestUtils.java index 3f1689b..a9db6e0 100644 --- a/distributedlog-core/src/test/java/org/apache/distributedlog/util/TestUtils.java +++ b/distributedlog-core/src/test/java/org/apache/distributedlog/util/TestUtils.java @@ -122,4 +122,32 @@ public class TestUtils extends ZooKeeperClusterTestCase { assertEquals("Version should be zero", 0, ((ZkVersion) data.getVersion()).getZnodeVersion()); } + + @Test(timeout = 60000) + public void testGetParent() throws Exception { + String path1 = null; + assertNull("parent of a null path is null", Utils.getParent(path1)); + + String path2 = ""; + assertNull("parent of an empty string is null", Utils.getParent(path2)); + + String path3 = "abcdef"; + assertNull("parent of a string with no / is null", Utils.getParent(path3)); + + String path4 = "/test/test2"; + assertEquals("parent of a /test/test2 is /test", "/test", Utils.getParent(path4)); + + String path5 = "/test/test2/"; + assertEquals("parent of a " + path5 + " is /test", "/test", Utils.getParent(path5)); + + String path6 = "/test"; + assertEquals("parent of " + path6 + " is /", "/", Utils.getParent(path6)); + + String path7 = "//"; + assertEquals("parent of " + path7 + " is /", "/", Utils.getParent(path7)); + + String path8 = "/"; + assertNull("parent of " + path8 + " is null", Utils.getParent(path8)); + } + }