Skip to content

Commit

Permalink
code commenting
Browse files Browse the repository at this point in the history
  • Loading branch information
U-C-S committed Jul 23, 2021
1 parent 71a6695 commit 8ad1b9d
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 36 deletions.
85 changes: 56 additions & 29 deletions src/cli.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,43 @@
import java.util.Scanner;

import constants.*;
import minidb.xmlParser.DatabaseFile;
import minidb.xmlParser.RegistryFile;
import constants.*;

/*
To do
- Comment the code
- Table Layout for the data.
- use threads
- usage of threads
*/

/**
* Javadoc comments. All are static methods because we are not creating any
* object of this class.
*
* @author Chanakya
*/
public class cli {

/**
* The Registry File is a XML file which contains the information about the all
* the databases created. It acts as a pointer to the Database File. So, We
* instantly load the registry file.
*/
static RegistryFile registry;

/**
* This attribute is for storing the DatabaseFile instance. Which is assigned
* when the user calls the command "use". if the user does not call the command
* "use" then the We show a error message.
*/
static DatabaseFile CurrentDb;

public static void main(String[] args) {
System.out.println(constants.HEADING);
registry = new RegistryFile(constants.DATA_XML_PATH);
print(constants.HEADING);

registry = new RegistryFile(constants.DATA_XML_PATH);
Scanner input = new Scanner(System.in);

while (true) {
System.out.print(constants.CMD_PREFIX);

Expand All @@ -34,28 +52,30 @@ public static void main(String[] args) {
long endTime = System.nanoTime();

long exeTime = (endTime - startTime) / 1000000;
System.out.println("\nExecution Time: " + exeTime + "ms");
print("\nExecution Time: " + exeTime + "ms");
}

input.close();
}

private static void cliInputs(String input) {
String[] inputCmds = input.split(" ");
String[] cmdArgs = input.split(" ");

switch (inputCmds[0]) {
switch (cmdArgs[0]) {
case "new": {
registry.createNewDatabase(inputCmds[1]);
registry.createNewDatabase(cmdArgs[1]);
break;
}

case "use": {
String path = registry.getDatabasePath(inputCmds[1], false);
String path = registry.getDatabasePath(cmdArgs[1], false);

if (path != null) {
CurrentDb = new DatabaseFile(path);
CurrentDb.EditMode();
System.out.println("Successfully loaded Database named: " + inputCmds[1]);
print("Successfully loaded Database named: " + cmdArgs[1]);
} else {
System.out.println("Database not found");
print("Database not found");
}
break;
}
Expand All @@ -66,94 +86,98 @@ private static void cliInputs(String input) {
}

case "help;": {
System.out.println(constants.HELP_COMMANDS);
print(constants.HELP_COMMANDS);
break;
}

case "info": {
// For querying the meta info of the database
// TODO
}

case "schema": {
if (CurrentDb != null) {
String xy = inputCmds[1];
String xy = cmdArgs[1];

if (xy.equals("show")) {
System.out.println(CurrentDb.getSchema());
print(CurrentDb.getSchema());
} else {
String[] schemaVals = xy.split(",");
if (schemaVals.length > 1) {
CurrentDb.createSchema(xy);
} else {
System.out.println("There should be atleast 2 columns of data");
print("There should be atleast 2 columns of data");
}
}

} else {
System.out.println(errors.NO_DATABASE_SELECTED);
print(errors.NO_DATABASE_SELECTED);
}
break;
}

case "add": {
if (CurrentDb != null) {
CurrentDb.addData(inputCmds[1]);
CurrentDb.addData(cmdArgs[1]);
} else {
System.out.println(errors.NO_DATABASE_SELECTED);
print(errors.NO_DATABASE_SELECTED);
}

break;
}

case "read": {
if (CurrentDb != null) {
if (inputCmds.length == 1) {
if (cmdArgs.length == 1) {
CurrentDb.readData();
} else {
CurrentDb.readData(inputCmds[1]);
CurrentDb.readData(cmdArgs[1]);
}
} else {
System.out.println(errors.NO_DATABASE_SELECTED);
print(errors.NO_DATABASE_SELECTED);
}

break;
}

case "drop": {
registry.deleteDatabase(inputCmds[1]);
registry.deleteDatabase(cmdArgs[1]);
break;
}

case "update": {
// TODO
if (CurrentDb != null) {
}
break;
}

case "delete": {
if (CurrentDb != null) {
CurrentDb.deleteData(inputCmds[1]);
CurrentDb.deleteData(cmdArgs[1]);
} else {
System.out.println(errors.NO_DATABASE_SELECTED);
print(errors.NO_DATABASE_SELECTED);
}
break;
}

default: {
System.out.println("UNKNOWN COMMAND: " + inputCmds[0] + "\nType `help;` for commands list");
print("UNKNOWN COMMAND: " + cmdArgs[0] + "\nType `help;` for commands list");
break;
}
}
}

private static void print(String x) {
System.out.println(x);
}
}

// Commands that are available
// ✅ read
// ✅ read id=2
// read id=8..99

// update name='cow' where id=2

// ✅ add 04,cow,25

// ✅ delete id=2
Expand All @@ -163,5 +187,8 @@ private static void cliInputs(String input) {
// schema update id, name,age
// ✅ schema show

// Future
// The hardest one:
// update name='cow' where id=2

// Future:
// - import/export databases cmds
5 changes: 5 additions & 0 deletions src/constants/constants.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package constants;

/**
* A Global store for all the constants used in the application.
* In future, we can let the user a create configuration file to modify the constants.
*/
public class constants {

public static String VERSION = "v0.3.1";

static String ROOT_PATH = ".";
Expand Down
4 changes: 2 additions & 2 deletions src/minidb/xmlParser/DatabaseFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public class DatabaseFile extends XMLFiles {
// prefix `X` to avoid namespace conflict
private static String TAG_STORAGE = "Xstorage";
private static String TAG_META = "Xmeta";
private static String TAG_META = "Xmeta"; // incomplete feature
private static String TAG_DATA = "Xdata";

private Element metaElem;
Expand Down Expand Up @@ -64,6 +64,7 @@ public void addData(String value) {
storageElem.appendChild(newDataElem);

this.updateFile();

} else {
print("The data does not follow the declared schema: " + this.getSchema());
}
Expand Down Expand Up @@ -120,5 +121,4 @@ public void deleteData(String id) {
e.printStackTrace();
}
}

}
3 changes: 1 addition & 2 deletions src/minidb/xmlParser/RegistryFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.Objects;
import javax.xml.parsers.*;
import org.w3c.dom.*;

import constants.constants;

/**
Expand All @@ -19,7 +18,7 @@ public RegistryFile(String path) {
super(path);
}

protected void createFile() {
void createFile() {
Element rootElem = doc.createElement("root");
Element emptyDb = this.addDbEntry("empty", "true");

Expand Down
8 changes: 5 additions & 3 deletions src/minidb/xmlParser/XMLFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.*;
import org.xml.sax.SAXException;

import constants.constants;

/**
* A abstract class with specfic set of methods to avoid repitition of code.
* @param path
*/
public abstract class XMLFiles {
protected File xmlFile;
protected Document doc;
Expand All @@ -34,7 +36,7 @@ private void load(boolean NoFile) throws ParserConfigurationException, SAXExcept
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
if (NoFile) {
doc = docBuilder.newDocument();
createFile();
createFile(); // abstract method to create the file
} else {
doc = docBuilder.parse(xmlFile);
;
Expand Down

0 comments on commit 8ad1b9d

Please sign in to comment.