Untitled

Anonymous
plain_text
02/12/2026 5:47 PM
7.8 KB
16
Indexable
import java.io.IOException;

/**
 * Database implementation for the songs project.
 * Maintains two hash tables (artists and songs) and a memory manager.
 *
 * @author Leguejou Awunganyi
 * @author Ishita Punna
 * @version 2026-02-08
 */
public class SongsDB implements Songs {

    /**
     * Hash table for artist names.
     */
    private Hash artist;

    /**
     * Hash table for song titles.
     */
    private Hash song;

    /**
     * Shared memory manager where all string bytes are stored.
     */
    private MemManager mem;

    /**
     * True once the database has been created.
     */
    private boolean init = false;

    /**
     * Initial hash table size used when recreating the database.
     */
    private int initHashSize;

    /**
     * Initial memory manager size used when recreating the database.
     */
    private int initMemSize;

    /**
     * Create a new SongsDB object.
     * Actual initialization is done by {@link #create(int, int)}.
     */
    public SongsDB() {
        // Nothing to initialize yet.
    }

    /**
     * Initialize or re‑initialize the database.
     *
     * @param inHash   initial size for both hash tables
     * @param inMemMan initial size for the memory manager
     * @return error message if parameters 
     * are invalid, or empty string on success
     */
    public String create(int inHash, int inMemMan) {
        if (inHash <= 0) {
            return "Initial hash table size must be positive";
        }

        if (inMemMan <= 0) {
            return "Initial memory manager size must be positive";
        }

        // Require memory size to be a power of two for buddy allocation.
        if ((inMemMan & (inMemMan - 1)) != 0) {
            return "Initial memory manager size must be a power of 2";
        }

        // Save initial sizes for clear().
        initHashSize = inHash;
        initMemSize = inMemMan;

        mem = new MemManager(inMemMan);

        // Create artist and song hash tables backed by the same memory manager.
        artist = new Hash(inHash, mem);
        song = new Hash(inHash, mem);

        init = true;
        return "";
    }

    /**
     * Re‑initialize the database to its original sizes.
     *
     * @return true if the database was cleared, false if it was not initialized
     */
    public boolean clear() {
        if (!init) {
            return false;
        }

        mem = new MemManager(initMemSize);
        artist = new Hash(initHashSize, mem);
        song = new Hash(initHashSize, mem);

        return true;
    }

    /**
     * Insert an artist–song pair into the database.
     *
     * @param artistStr artist string to insert
     * @param songStr   song string to insert
     * @return status message describing what happened
     */
    public String insert(String artistStr, String songStr) {
        // Database must be created first.
        if (!init) {
            return "Database not initialized";
        }

        // Check for null or empty input strings.
        if (artistStr == null || songStr == null
            || artistStr.isEmpty() || songStr.isEmpty()) {
            return "Input strings cannot be null or empty";
        }

        StringBuilder sb = new StringBuilder();

        // Insert artist first so messages appear in correct order.
        String artistMsg = artist.insert(artistStr, "Artist");

        // Capture any memory expansion from the artist insert.
        String expanded = mem.getExpandMessage();
        if (!expanded.isEmpty()) {
            sb.append(expanded);
        }

        sb.append(artistMsg)
            .append("\r\n");

        // Insert song.
        String songMsg = song.insert(songStr, "Song");

        // Capture any memory expansion from the song insert.
        expanded = mem.getExpandMessage();
        if (!expanded.isEmpty()) {
            sb.append(expanded);
        }

        // Append song message; no trailing newline.
        sb.append(songMsg);

        return sb.toString();
    }

    /**
     * Remove an entry from one of the hash tables.
     *
     * @param type which table to remove from ("artist" or "song")
     * @param name string to remove from the chosen table
     * @return status message describing the result
     * @throws IOException not used here but required by interface
     */
    public String remove(String type, String name) throws IOException {
        if (!init) {
            return "Database not initialized";
        }

        if (type == null || name == null
            || type.isEmpty() || name.isEmpty()) {
            return "Input strings cannot be null or empty";
        }

        if (type.equalsIgnoreCase("artist")) {
            return artist.remove(name, "Artist");
        }

        if (type.equalsIgnoreCase("song")) {
            return song.remove(name, "Song");
        }

        return "Bad type value |" + type + "| on remove";
    }

    /**
     * Print information from the database.
     *
     * @param type controls what object is printed:
     *             "artist", "song", or "blocks"
     * @return the formatted string that was printed
     * @throws IOException not used here but required by interface
     */
    public String print(String type) throws IOException {
        if (!init) {
            return "Database not initialized";
        }

        if (type == null || type.isEmpty()) {
            return "Input strings cannot be null or empty";
        }

        if (type.equalsIgnoreCase("artist")) {
            return artist.print("artist");
        }

        if (type.equalsIgnoreCase("song")) {
            return song.print("song");
        }

        if (type.equalsIgnoreCase("blocks")) {
            return mem.printBlocks();
        }

        return "Bad print parameter";
    }
}

Editor is loading...
Leave a Comment