This is an automated email from the ASF dual-hosted git repository.

sijie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/bookkeeper.git


The following commit(s) were added to refs/heads/master by this push:
     new 1448d12  Migrate command `listfilesondisk`
1448d12 is described below

commit 1448d12aa6399b64d272b9d3f820fbf128cb371e
Author: Yong Zhang <[email protected]>
AuthorDate: Mon Mar 25 11:14:16 2019 +0800

    Migrate command `listfilesondisk`
    
    Descriptions of the changes in this PR:
    
    - Replace command `listfilesondisk`
    
    ### Motivation
    
    - Use `bkctl` to run command `listfilesondisk`
    - #1989
    ### Changes
    
    - Add command in `bkctl`
    
    
    Reviewers: Jia Zhai <[email protected]>, Sijie Guo <[email protected]>
    
    This closes #1990 from zymap/command-listfilesondisc and squashes the 
following commits:
    
    922e2b70a [Sijie Guo] Merge branch 'master' into command-listfilesondisc
    8d72a00bd [Yong Zhang] Migrate command `listfilesondisk`
---
 .../org/apache/bookkeeper/bookie/BookieShell.java  |  36 +------
 .../commands/bookie/ListFilesOnDiscCommand.java    | 104 ++++++++++++++++++
 .../tools/cli/commands/BookieCommandGroup.java     |   2 +
 .../bookie/ListFilesOnDiscCommandTest.java         | 117 +++++++++++++++++++++
 4 files changed, 228 insertions(+), 31 deletions(-)

diff --git 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieShell.java 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieShell.java
index 8ac9c85..fa52bc7 100644
--- 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieShell.java
+++ 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieShell.java
@@ -110,6 +110,7 @@ import 
org.apache.bookkeeper.tools.cli.commands.bookie.FormatCommand;
 import org.apache.bookkeeper.tools.cli.commands.bookie.InitCommand;
 import org.apache.bookkeeper.tools.cli.commands.bookie.LastMarkCommand;
 import org.apache.bookkeeper.tools.cli.commands.bookie.LedgerCommand;
+import org.apache.bookkeeper.tools.cli.commands.bookie.ListFilesOnDiscCommand;
 import org.apache.bookkeeper.tools.cli.commands.bookie.SanityTestCommand;
 import org.apache.bookkeeper.tools.cli.commands.bookies.InfoCommand;
 import org.apache.bookkeeper.tools.cli.commands.bookies.ListBookiesCommand;
@@ -1601,38 +1602,11 @@ public class BookieShell implements Tool {
             boolean journal = cmdLine.hasOption("txn");
             boolean entrylog = cmdLine.hasOption("log");
             boolean index = cmdLine.hasOption("idx");
-            boolean all = false;
 
-            if (!journal && !entrylog && !index && !all) {
-                all = true;
-            }
-
-            if (all || journal) {
-                File[] journalDirs = bkConf.getJournalDirs();
-                List<File> journalFiles = listFilesAndSort(journalDirs, "txn");
-                System.out.println("--------- Printing the list of Journal 
Files ---------");
-                for (File journalFile : journalFiles) {
-                    System.out.println(journalFile.getCanonicalPath());
-                }
-                System.out.println();
-            }
-            if (all || entrylog) {
-                File[] ledgerDirs = bkConf.getLedgerDirs();
-                List<File> ledgerFiles = listFilesAndSort(ledgerDirs, "log");
-                System.out.println("--------- Printing the list of 
EntryLog/Ledger Files ---------");
-                for (File ledgerFile : ledgerFiles) {
-                    System.out.println(ledgerFile.getCanonicalPath());
-                }
-                System.out.println();
-            }
-            if (all || index) {
-                File[] indexDirs = (bkConf.getIndexDirs() == null) ? 
bkConf.getLedgerDirs() : bkConf.getIndexDirs();
-                List<File> indexFiles = listFilesAndSort(indexDirs, "idx");
-                System.out.println("--------- Printing the list of Index Files 
---------");
-                for (File indexFile : indexFiles) {
-                    System.out.println(indexFile.getCanonicalPath());
-                }
-            }
+            ListFilesOnDiscCommand.LFODFlags flags = new 
ListFilesOnDiscCommand.LFODFlags().journal(journal)
+                                                         
.entrylog(entrylog).index(index);
+            ListFilesOnDiscCommand cmd = new ListFilesOnDiscCommand(flags);
+            cmd.apply(bkConf, flags);
             return 0;
         }
 
diff --git 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/tools/cli/commands/bookie/ListFilesOnDiscCommand.java
 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/tools/cli/commands/bookie/ListFilesOnDiscCommand.java
new file mode 100644
index 0000000..35d694b
--- /dev/null
+++ 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/tools/cli/commands/bookie/ListFilesOnDiscCommand.java
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.bookkeeper.tools.cli.commands.bookie;
+
+import com.beust.jcommander.Parameter;
+import com.google.common.util.concurrent.UncheckedExecutionException;
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import lombok.Setter;
+import lombok.experimental.Accessors;
+import org.apache.bookkeeper.bookie.BookieShell;
+import org.apache.bookkeeper.conf.ServerConfiguration;
+import org.apache.bookkeeper.tools.cli.helpers.BookieCommand;
+import org.apache.bookkeeper.tools.framework.CliFlags;
+import org.apache.bookkeeper.tools.framework.CliSpec;
+
+/**
+ * Command to list the files in 
JournalDirectory/LedgerDirectories/IndexDirectories.
+ */
+public class ListFilesOnDiscCommand extends 
BookieCommand<ListFilesOnDiscCommand.LFODFlags > {
+
+    private static final String NAME = "listfilesondisc";
+    private static final String DESC = "List the files in 
JournalDirectory/LedgerDirectories/IndexDirectories.";
+
+    public ListFilesOnDiscCommand() {
+        this(new LFODFlags());
+    }
+
+    public ListFilesOnDiscCommand(LFODFlags flags) {
+        
super(CliSpec.<LFODFlags>newBuilder().withName(NAME).withDescription(DESC).withFlags(flags).build());
+    }
+
+    /**
+     * Flags for list files on disc command.
+     */
+    @Accessors(fluent = true)
+    @Setter
+    public static class LFODFlags extends CliFlags {
+        @Parameter(names = {"-txn", "--journal"}, description = "Print list of 
Journal Files")
+        private boolean journal;
+
+        @Parameter(names = {"-log", "--entrylog"}, description = "Print list 
of EntryLog Files")
+        private boolean entrylog;
+
+        @Parameter(names = {"-idx", "--index"}, description = "Print list of 
Index Files")
+        private boolean index;
+    }
+
+    @Override
+    public boolean apply(ServerConfiguration conf, LFODFlags cmdFlags) {
+        try {
+            return handler(conf, cmdFlags);
+        } catch (IOException e) {
+            throw new UncheckedExecutionException(e.getMessage(), e);
+        }
+    }
+
+    private boolean handler(ServerConfiguration conf, LFODFlags cmd) throws 
IOException {
+        if (cmd.journal) {
+            File[] journalDirs = conf.getJournalDirs();
+            List<File> journalFiles = 
BookieShell.listFilesAndSort(journalDirs, "txn");
+            System.out.println("--------- Printing the list of Journal Files 
---------");
+            for (File journalFile : journalFiles) {
+                System.out.println(journalFile.getCanonicalPath());
+            }
+            System.out.println();
+        }
+        if (cmd.entrylog) {
+            File[] ledgerDirs = conf.getLedgerDirs();
+            List<File> ledgerFiles = BookieShell.listFilesAndSort(ledgerDirs, 
"log");
+            System.out.println("--------- Printing the list of EntryLog/Ledger 
Files ---------");
+            for (File ledgerFile : ledgerFiles) {
+                System.out.println(ledgerFile.getCanonicalPath());
+            }
+            System.out.println();
+        }
+        if (cmd.index) {
+            File[] indexDirs = (conf.getIndexDirs() == null) ? 
conf.getLedgerDirs() : conf.getIndexDirs();
+            List<File> indexFiles = BookieShell.listFilesAndSort(indexDirs, 
"idx");
+            System.out.println("--------- Printing the list of Index Files 
---------");
+            for (File indexFile : indexFiles) {
+                System.out.println(indexFile.getCanonicalPath());
+            }
+        }
+        return true;
+    }
+}
diff --git 
a/tools/ledger/src/main/java/org/apache/bookkeeper/tools/cli/commands/BookieCommandGroup.java
 
b/tools/ledger/src/main/java/org/apache/bookkeeper/tools/cli/commands/BookieCommandGroup.java
index 9b22772..9390d2e 100644
--- 
a/tools/ledger/src/main/java/org/apache/bookkeeper/tools/cli/commands/BookieCommandGroup.java
+++ 
b/tools/ledger/src/main/java/org/apache/bookkeeper/tools/cli/commands/BookieCommandGroup.java
@@ -27,6 +27,7 @@ import 
org.apache.bookkeeper.tools.cli.commands.bookie.FormatCommand;
 import org.apache.bookkeeper.tools.cli.commands.bookie.InitCommand;
 import org.apache.bookkeeper.tools.cli.commands.bookie.LastMarkCommand;
 import org.apache.bookkeeper.tools.cli.commands.bookie.LedgerCommand;
+import org.apache.bookkeeper.tools.cli.commands.bookie.ListFilesOnDiscCommand;
 import org.apache.bookkeeper.tools.cli.commands.bookie.SanityTestCommand;
 import org.apache.bookkeeper.tools.common.BKFlags;
 import org.apache.bookkeeper.tools.framework.CliCommandGroup;
@@ -50,6 +51,7 @@ public class BookieCommandGroup extends 
CliCommandGroup<BKFlags> {
         .addCommand(new FormatCommand())
         .addCommand(new SanityTestCommand())
         .addCommand(new LedgerCommand())
+        .addCommand(new ListFilesOnDiscCommand())
         .addCommand(new ConvertToDBStorageCommand())
         .addCommand(new ConvertToInterleavedStorageCommand()).build();
 
diff --git 
a/tools/ledger/src/test/java/org/apache/bookkeeper/tools/cli/commands/bookie/ListFilesOnDiscCommandTest.java
 
b/tools/ledger/src/test/java/org/apache/bookkeeper/tools/cli/commands/bookie/ListFilesOnDiscCommandTest.java
new file mode 100644
index 0000000..e9528fa
--- /dev/null
+++ 
b/tools/ledger/src/test/java/org/apache/bookkeeper/tools/cli/commands/bookie/ListFilesOnDiscCommandTest.java
@@ -0,0 +1,117 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.bookkeeper.tools.cli.commands.bookie;
+
+import static org.powermock.api.mockito.PowerMockito.whenNew;
+
+import java.io.File;
+import java.io.IOException;
+import org.apache.bookkeeper.bookie.BookieShell;
+import org.apache.bookkeeper.conf.ServerConfiguration;
+import org.apache.bookkeeper.tools.cli.helpers.BookieCommandTestBase;
+import org.junit.Assert;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.junit.runner.RunWith;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+
+/**
+ * Unit test for {@link ListFilesOnDiscCommand}.
+ */
+@RunWith(PowerMockRunner.class)
+@PrepareForTest({ ListFilesOnDiscCommand.class })
+public class ListFilesOnDiscCommandTest extends BookieCommandTestBase {
+
+    @Rule
+    private TemporaryFolder folder = new TemporaryFolder();
+
+    public ListFilesOnDiscCommandTest() {
+        super(3, 0);
+    }
+
+    @Override
+    public void setup() throws Exception {
+        super.setup();
+        createTmpDirs();
+        whenNew(ServerConfiguration.class).withNoArguments().thenReturn(conf);
+    }
+
+    private void createTmpDirs() throws IOException {
+        File journals = folder.newFolder("journals");
+        conf.setJournalDirsName(new String[] { journals.getAbsolutePath() });
+        journals.mkdir();
+        File ledgers = folder.newFolder("ledgers");
+        conf.setLedgerDirNames(new String[] { ledgers.getAbsolutePath() });
+        ledgers.mkdir();
+        File index = folder.newFolder("index");
+        conf.setIndexDirName(new String[] { index.getAbsolutePath() });
+        index.mkdir();
+
+        for (int i = 0; i < 10; i++) {
+            File.createTempFile("journal-" + i, ".txn", journals);
+            File.createTempFile("ledger-" + i, ".log", ledgers);
+            File.createTempFile("index-" + i, ".idx", index);
+        }
+        System.out.println("over");
+    }
+
+    @Test
+    public void testListJournalCommand() {
+        testCommand("-txn");
+        Assert.assertEquals(10, 
BookieShell.listFilesAndSort(conf.getJournalDirs(), "txn").size());
+    }
+
+    @Test
+    public void testListJournalLongCommand() {
+        testCommand("--journal");
+        Assert.assertEquals(10, 
BookieShell.listFilesAndSort(conf.getJournalDirs(), "txn").size());
+    }
+
+    @Test
+    public void testListEntryLogCommand() {
+        testCommand("-log");
+        Assert.assertEquals(10, 
BookieShell.listFilesAndSort(conf.getLedgerDirs(), "log").size());
+    }
+
+    @Test
+    public void testListEntryLogLongCommand() {
+        testCommand("--entrylog");
+        Assert.assertEquals(10, 
BookieShell.listFilesAndSort(conf.getLedgerDirs(), "log").size());
+    }
+
+    @Test
+    public void testListIndexCommand() {
+        testCommand("-idx");
+        Assert.assertEquals(10, 
BookieShell.listFilesAndSort(conf.getIndexDirs(), "idx").size());
+    }
+
+    @Test
+    public void testListIndexLongCommand() {
+        testCommand("--index");
+        Assert.assertEquals(10, 
BookieShell.listFilesAndSort(conf.getIndexDirs(), "idx").size());
+    }
+
+    private void testCommand(String... args) {
+        ListFilesOnDiscCommand cmd = new ListFilesOnDiscCommand();
+        Assert.assertTrue(cmd.apply(bkFlags, args));
+    }
+}

Reply via email to