Untitled

Anonymous
plain_text
02/08/2026 6:27 PM
7.9 KB
15
Indexable
import java.io.IOException;

/**
 * The database implementation for this project.
 * We have two hash tables and a memory manager.
 *
 * @author Leguejou Awunganyi & Ishita Punna
 * @version <Put something here>
 */
public class SongsDB implements Songs
{
    
    private Hash artist; // hash table for artist names
    private Hash song; // ht fir song titles
    private MemManager mem; 
    // shared mem pool where all actual string bytes are stored
    private boolean init = false; 
    private int initHashSize;
    private int initMemSize;

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


    /**
     * Create a brave new World.
     *
     * @param inHash
     *            Initial size for hash tables
     * @param inMemMan
     *            Initial size for the memory manager
     * @return Error messages if appropriate
     */
    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";
        }

        if ((inMemMan & (inMemMan - 1)) != 0) {
            return "Initial memory manager size must be a power of 2";
            // ensure the memory size is a 
            // power of 2 (2^n), which is required for the Buddy Method
        }

        // save intital sizes
        initHashSize = inHash;
        initMemSize = inMemMan;

        mem = new MemManager(inMemMan);
        // create memory pool
        
        artist = new Hash(inHash, mem);
        // creates artist hash table and links it to mem.
        
        song = new Hash(inHash, mem);
         // creates song hash table and links it to mem.

        init = true;
        return ""; // database is ready to use
    }


    /**
     * Re-initialize the database
     * @return true on successful clear of database
     */
    public boolean clear() {
    	if (!init) {
    	return false;
    	}

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

    	return true;
    	}


    // ----------------------------------------------------------
    /**
     * Insert to the hash table
     *
     * @param artistStr
     *            Artist string to insert
     * @param songStr
     *            Song string to insert
     * @return Error message if appropriate
     * @throws IOException
     */
    public String insert(String artistStr, String songStr) {

        // DB must be created
        if (!init) {
            return "Database not initialized";
        }

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

        StringBuilder sb = new StringBuilder();
        
        String artistMsg = artist.insert(artistStr, "Artist"); 
        // puts artist string in artist hash table
        // the above is done before checking memory expansion to produce the proper order

        
        String expanded = mem.getExpandMessage();
        if (expanded.isEmpty() == false) {
        	sb.append(expanded);
        }
        
        sb.append(artistMsg).append("\r\n");
        
        String songMsg = song.insert(songStr, "Song");
        // puts song string in song hash table

        
        expanded = mem.getExpandMessage();
        
        if (expanded.isEmpty() == false) {
        	sb.append(expanded);
        }
        
        
        sb.append(songMsg);
        //completes the output after inserting the song
        
        /*sb.append(mem.getExpandMessage());
        sb.append(mem.getExpandMessage());
        sb.append(song.insert(songStr, "Song"));*/

        return sb.toString();
    }

    // ----------------------------------------------------------
    /**
     * Remove from the hash table
     *
     * @param type
     *            The table to be removed
     * @param name
     *            The string to be removed from the table
     * @return Error message if appropriate
     * @throws IOException
     */
    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 out the hash table contents
     *
     * @param type
     *            Controls what object is being printed
     * @return The string that was printed
     * @throws IOException
     */
    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