Untitled

 avatar
unknown
plain_text
2 months ago
7.4 kB
9
Indexable
import java.io.IOException;

/**
 * The database implementation for this project. We have two hash tables and
 * graph.
 *
 * @author Leguejou Awunganyi
 * @author Ishita Punna
 * @version 2026-02-08
 */
public class GraphDB implements GPInterface {

    /**
     * Artist hash table.
     */
    private Hash artistTable;

    /**
     * Song hash table.
     */
    private Hash songTable;

    /**
     * Relationship graph.
     */
    private Graph graph;

    /**
     * True once the database has been initialized.
     */
    private boolean initialized;

    // ----------------------------------------------------------
    /**
     * Create a new GraphDB object. But don't set anything -- that gets done by
     * "create"
     */
    public GraphDB() {
        initialized = false;
    }

    /**
     * Create a brave new World.
     *
     * @param inHash Initial size for hash tables
     * @return Error messages if appropriate
     */
    public String create(int inHash) {
        if (inHash <= 0) {
            return "Initial hash table size must be positive";
        }
        if (initialized) {
            return "Database already exists";
        }
        artistTable = new Hash("artist", inHash);
        songTable = new Hash("song", inHash);
        graph = new Graph(inHash);
        initialized = true;
        return null;
    }

    /**
     * Re-initialize the database
     * 
     * @return true on successful clear of database
     */
    public boolean clear() {
        if (!initialized) {
            return false;
        }
        artistTable = null;
        songTable = null;
        graph = null;
        initialized = false;
        return true;
    }

    // ----------------------------------------------------------
    /**
     * Insert to the hash table
     *
     * @param artistString Artist string to insert
     * @param songString   Song string to insert
     * @return Status message as appropriate
     * @throws IOException
     */
    public String insert(String artistString, String songString) throws IOException {
        if (!initialized) {
            return "Database not initialized";
        }
        if (isBlank(artistString) || isBlank(songString)) {
            return "Input strings cannot be null or empty";
        }

        GraphNode artistNode = artistTable.find(artistString);
        GraphNode songNode = songTable.find(songString);

        if (artistNode != null && songNode != null && graph.hasEdge(artistNode, songNode)) {
            return "|" + artistString + "<SEP>" + songString + "| duplicates a record already in the database\r\n";
        }

        StringBuilder builder = new StringBuilder();

        if (artistNode == null) {
            Graph.AddNodeResult artistAdd = graph.addNode(artistString, true);
            artistNode = artistAdd.node;
            Hash.InsertResult artistInsert = artistTable.insert(artistString, artistNode);
            if (artistInsert.resized) {
                builder.append("artist hash table size doubled\r\n");
            }
            builder.append("|").append(artistString).append("| is added to the Artist database\r\n");
        }

        if (songNode == null) {
            Graph.AddNodeResult songAdd = graph.addNode(songString, false);
            songNode = songAdd.node;
            Hash.InsertResult songInsert = songTable.insert(songString, songNode);
            if (songAdd.resized) {
                builder.append("Graph size doubled to ").append(graph.capacity()).append("\r\n");
            }
            if (songInsert.resized) {
                builder.append("song hash table size doubled\r\n");
            }
            builder.append("|").append(songString).append("| is added to the Song database\r\n");
        }

        if (!graph.hasEdge(artistNode, songNode)) {
            graph.addEdge(artistNode, songNode);
        }
        return builder.toString();
    }

    // ----------------------------------------------------------
    /**
     * Remove from the hash table
     *
     * @param type       The table to be removed from
     * @param nameString The string to be removed from the table
     * @return Status message as appropriate
     * @throws IOException
     */
    public String remove(String type, String nameString) throws IOException {
        if (!initialized) {
            return "Database not initialized";
        }
        if (isBlank(type) || isBlank(nameString)) {
            return "Input strings cannot be null or empty";
        }

        if (equalsIgnoreCase(type, "artist")) {
            GraphNode node = artistTable.remove(nameString);
            if (node == null) {
                return "|" + nameString + "| does not exist in the Artist database\r\n";
            }
            graph.removeNode(node);
            return "|" + nameString + "| is removed from the Artist database\r\n";
        }
        if (equalsIgnoreCase(type, "song")) {
            GraphNode node = songTable.remove(nameString);
            if (node == null) {
                return "|" + nameString + "| does not exist in the Song database\r\n";
            }
            graph.removeNode(node);
            return "|" + nameString + "| is removed from the Song database\r\n";
        }
        return "Bad type value |" + type + "| on remove";
    }

    // ----------------------------------------------------------
    /**
     * Print out the hash table contents
     *
     * @param type Controls what object is being printed
     * @return Status message as appropriate
     * @throws IOException
     */
    public String print(String type) throws IOException {
        if (!initialized) {
            return "Database not initialized";
        }
        if (isBlank(type)) {
            return "Input strings cannot be null or empty";
        }
        if (equalsIgnoreCase(type, "artist")) {
            return artistTable.print();
        }
        if (equalsIgnoreCase(type, "song")) {
            return songTable.print();
        }
        return "Bad print parameter";
    }

    // ----------------------------------------------------------
    /**
     * Print out the graph information
     *
     * @return The string that was printed
     */
    public String printgraph() {
        if (!initialized) {
            return "Database not initialized";
        }
        Graph.Stats stats = graph.stats();
        return "There are " + stats.components + " connected components\r\n" + "The largest connected component has "
                + stats.largestSize + " elements\r\n" + "The diameter of the largest component is " + stats.diameter
                + "\r\n";
    }

    /**
     * Check if a string is null or empty.
     *
     * @param value string to test
     * @return true when null or empty
     */
    private boolean isBlank(String value) {
        return value == null || value.length() == 0;
    }

    /**
     * Compare two strings ignoring case.
     *
     * @param left  first string
     * @param right second string
     * @return true when the strings match ignoring case
     */
    private boolean equalsIgnoreCase(String left, String right) {
        return left.equalsIgnoreCase(right);
    }
}
Editor is loading...
Leave a Comment