Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Simulate the external hashing structure for a company to store the clients’ info

ID: 3636230 • Letter: S

Question

Simulate the external hashing structure for a company to store the clients’ information by using Java RandomAccessFile class.

Assumptions:
• The data structure is implemented by an in-memory hash table and an external file. Assume the file consists of n disc blocks and each block contains r records maximally.
• The number of total records is not more than (n * r ) / 2.
• each record is defined as follows:
It consists of one string name and one string id. The id is a key which uniquely represents a record. The mapping integer of an id is in the range of 1 to 100. (the digits given to an id is 4 for extending purpose)
The read operation reads the record from a file
The write operation writes the record into the file
• The in-memory hash table contains the index of each block in the external file.
• The probing is linear in the external blocks. The linear probing here means a sequential search record by record within a block.

Detail guides:

1. Implement an Index Hash Table by a class named IndexHashTable. Each element of the table contains a block number in the range of 0 to n-1. Each block number is an index of the block in the external file. For example, if one block size is 300 bytes and the index is 6, the offset for the block 6 should be 6*300=1800 byte in the file. In the other word, the block 6 starts from 1800 byte in the file.

2. The block numbers should be created by random numbers. The range of the random numbers is 0 to n-1. The block numbers should be unique in the table. The IndexHashTable should be filled before storing any records into the external storage (the file).

3. The operations in the hash table are insert, delete, and search. The class must define a noData record as a class field which can be used for clearing the storage and representing the deleted elements in the external file.
• The possible steps for insert (operation analysis sample):
1) For a given record (input by user), use the id of the record to be the original key. Note that the key is an integer converted from the string id.
2) Apply the hash function to the key to obtain an index of the Index Hash Table.
3) Use the index to retrieve the block index in the table.
4) Calculate the start address of the block in the file: index * blockSize. The blockSize is recSize * r.
5) Use the address obtained in 4) to locate the position of the block in the file (use seek() method in RandomFile.java, the sample code given by the instructor.)
6) Use the linear probe approach to insert the record in the block. If the number of probes reaches r and the available space is not found in the block, the operation fails. The error message should be displayed to the user and the insertion should be terminated. (This situation indicates that the hash function itself has a poor performance.)
7) The insertion should guarantee no redundant record in the block (thus in the entire file)
• The signature of the insert method should be:
public int insert (RandomAccessFile file, Record rec)
It returns 0 for successful, -1 for block full and 1 for redundancy
• Start from step 5), the operations of the random access file will be involved.
• The students should figure out the algorithms for delete and search.

4. The classes provided by instructor:
1) Record class
This class defines a record required in the problem, includes methods to write the record into a specified random access file and read a record from the file.
Class field:
name – It is String type and limited to 16 chars
id – It is String type and limited to 4 chars
[Note: Each char in Unicode is two bytes. When you search in the object of RandomAccessFile, counting offset for each record should double the size of the chars in a record.]
Constructor:
public record (String name, String id) –
create a new record
Public Methods:
public void read (RandomAccessFile file) –
to retrieve the name and id from the file at the current file pointer position
public void write (RandomAccessFile file) –
to write name and id in the file at the current position
public String getId( ) –
to return the id of a record
public String getName( ) –
to return the name in a record
public String toString( ) – to display the name and id in the record
2) RandomFile class
This is a demo program. It shows how to create a random access file which works as an external disk file, how to use the write and read operations defined in Record class, how to locate a block by seek() in the file, and how to close the file. It is an application class containing main(). You can use it as the base skeleton of your application class.

5. Write an application class named ExternalHashing to supply a menu for users to use the hash table to store the records. The menu should include the Insert, Remove, Search and Quit operations. You should also display a title to this system.

6. Your program should be designed to accept any values for n and r. To test your program, use n = 11 (It is the size of the in-memory hash table and also the number of blocks in the file) and r = 10 (the maximum number of records in a block)

Classes provided by the instructor:

import java.util.*;
import java.io.*;

public class Record
{
private String name;
private String id;
private int NAMELENGTH=16;
private int IDLENGTH=4;

public Record()
{
name = " ";
id = " ";
}

public Record(String newName, String newId)
{
name = newName;
id = newId;
}

//Read a 'name' and the related 'id' from the current position of
//the given file
public void read(RandomAccessFile file) throws IOException
{
name = readString(file, NAMELENGTH);
id = readString(file, IDLENGTH);
}

//Write the value of 'this' object to the given file
public void write(RandomAccessFile file) throws IOException
{
writeStr(file, name, NAMELENGTH);
writeStr(file, id, IDLENGTH);
}

public String getId()
{
return id;
}

public String getName()
{
return name;
}

//Help method: read a string with length of strLength from the file
// Return the string to the caller
private String readString (RandomAccessFile file, int strLength) throws IOException
{
char[] chs = new char[strLength];

for (int i = 0; i < chs.length; i++)
{
chs[i] = file.readChar();
}

String theStr = new String(chs);

return theStr;
}

//Help method: write a string with length of strLength to the file
private void writeStr( RandomAccessFile file, String str, int strLength ) throws IOException
{
StringBuffer buffer = null;

if ( str != null )
buffer = new StringBuffer( str );
else
buffer = new StringBuffer( strLength );

buffer.setLength( strLength );
file.writeChars( buffer.toString() );

} // end method writeName

public String toString()
{
return " Name: " + name + " ID: " + id;
}
}


import java.io.*;
import java.io.RandomAccessFile;

public class RandomFile
{
public static void main( String args[] )
{

Record rec = new Record("Instructor", "0002");
Record rec1 = new Record();
RandomAccessFile f = null;

try
{
f = new RandomAccessFile("newfile.txt", "rw");
f.seek(0);
rec.write(f);
}
catch (IOException e)
{
System.out.println("cannot write " + e);
}

try
{
if (f != null)
{
f.seek(0);
rec1.read(f);
f.close();
}
}
catch (EOFException ee)
{
System.out.println("end of file");
}
catch (IOException e)
{
System.out.println("cannot read " + e);
}

System.out.println(rec1);
} // end main
} // end class RandomFile

Explanation / Answer

The program below demonstrates this with a generic string drawing applet. The applet parameter "Message" is the string to be drawn. import java.applet.*; import java.awt.*; public class DrawStringApplet extends Applet { private String defaultMessage = "Hello!"; public void paint(Graphics g) { String inputFromPage = this.getParameter("Message"); if (inputFromPage == null) inputFromPage = defaultMessage; g.drawString(inputFromPage, 50, 25); } }