From ea5ed0ceb3ac8d245fd5b350009754bec35d161d Mon Sep 17 00:00:00 2001
From: Donnie Pinkston <donnie@cms.caltech.edu>
Date: Mon, 25 Feb 2019 09:20:49 -0800
Subject: [PATCH] Complete implementation of DUMP INDEX command

The DUMP INDEX command now generates a proper execution plan, and can
therefore dump indexes now.
---
 .../caltech/nanodb/commands/DumpCommand.java  | 40 +++++++++----------
 .../nanodb/commands/DumpIndexCommand.java     | 21 +++++++++-
 2 files changed, 38 insertions(+), 23 deletions(-)

diff --git a/src/main/java/edu/caltech/nanodb/commands/DumpCommand.java b/src/main/java/edu/caltech/nanodb/commands/DumpCommand.java
index d08a692..e0d49c3 100644
--- a/src/main/java/edu/caltech/nanodb/commands/DumpCommand.java
+++ b/src/main/java/edu/caltech/nanodb/commands/DumpCommand.java
@@ -1,6 +1,7 @@
 package edu.caltech.nanodb.commands;
 
 
+import java.io.FileNotFoundException;
 import java.io.PrintStream;
 
 import edu.caltech.nanodb.plannodes.PlanNode;
@@ -121,31 +122,28 @@ public abstract class DumpCommand extends Command {
 
     @Override
     public void execute(NanoDBServer server) throws ExecutionException {
-
-        try {
-            // Figure out where the dumped data should go.
-            PrintStream dumpOut = out;
-            if (fileName != null)
+        // Figure out where the dumped data should go.
+        PrintStream dumpOut = out;
+        if (fileName != null) {
+            try {
                 dumpOut = new PrintStream(fileName);
+            }
+            catch (FileNotFoundException e) {
+                throw new ExecutionException(e);
+            }
+        }
 
-            // Dump the table.
-            PlanNode dumpPlan = prepareDumpPlan(server);
-            TupleExporter exporter = new TupleExporter(dumpOut);
-            EvalStats stats = QueryEvaluator.executePlan(dumpPlan, exporter);
+        // Dump the table.
+        PlanNode dumpPlan = prepareDumpPlan(server);
+        TupleExporter exporter = new TupleExporter(dumpOut);
+        EvalStats stats = QueryEvaluator.executePlan(dumpPlan, exporter);
 
-            if (fileName != null)
-                dumpOut.close();
+        if (fileName != null)
+            dumpOut.close();
 
-            // Print out the evaluation statistics.
-            out.printf("Dumped %d rows in %f sec.%n",
-                stats.getRowsProduced(), stats.getElapsedTimeSecs());
-        }
-        catch (ExecutionException e) {
-            throw e;
-        }
-        catch (Exception e) {
-            throw new ExecutionException(e);
-        }
+        // Print out the evaluation statistics.
+        out.printf("Dumped %d rows in %f sec.%n",
+            stats.getRowsProduced(), stats.getElapsedTimeSecs());
     }
 
 
diff --git a/src/main/java/edu/caltech/nanodb/commands/DumpIndexCommand.java b/src/main/java/edu/caltech/nanodb/commands/DumpIndexCommand.java
index 80e430b..908221d 100644
--- a/src/main/java/edu/caltech/nanodb/commands/DumpIndexCommand.java
+++ b/src/main/java/edu/caltech/nanodb/commands/DumpIndexCommand.java
@@ -1,8 +1,13 @@
 package edu.caltech.nanodb.commands;
 
 
+import edu.caltech.nanodb.indexes.IndexInfo;
+import edu.caltech.nanodb.indexes.IndexManager;
+import edu.caltech.nanodb.plannodes.FileScanNode;
 import edu.caltech.nanodb.plannodes.PlanNode;
+import edu.caltech.nanodb.relations.TableInfo;
 import edu.caltech.nanodb.server.NanoDBServer;
+import edu.caltech.nanodb.storage.TableManager;
 
 
 /**
@@ -60,8 +65,20 @@ public class DumpIndexCommand extends DumpCommand {
 
     @Override
     protected PlanNode prepareDumpPlan(NanoDBServer server) {
-        // TODO:  Index scan!
-        return null;
+        // Open the table so we can open the index.
+        TableManager tblMgr = server.getStorageManager().getTableManager();
+        TableInfo tblInfo = tblMgr.openTable(tableName);
+
+        // Open the index.
+        IndexManager idxMgr = server.getStorageManager().getIndexManager();
+        IndexInfo idxInfo = idxMgr.openIndex(tblInfo, indexName);
+
+        // Make a file-scan plan-node over the index.
+        PlanNode plan = new FileScanNode(idxInfo, null);
+        plan.prepare();
+
+        // Done!
+        return plan;
     }
 
 
-- 
GitLab