Methods Summary |
---|
private synchronized java.lang.Object | action(java.lang.String s, byte[] data, int action)Go to the index of the record specified by s and perform
an action. Either an update, search or deletion. This method is for
code compaction as the process for updating, searching and
deleting varies only slightly.
if ((action != 1) && (recordIDs.size() == 0)) {
throw new RecordStoreException();
}
Enumeration IDs = recordIDs.elements();
while (IDs.hasMoreElements()) {
int index = ((Integer) IDs.nextElement()).intValue();
try {
if (rc.compare(database.getRecord(index), s.getBytes())
== RecordComparator.EQUIVALENT) {
switch (action) {
case 0:
database.deleteRecord(index);
recordIDs.removeElement(new Integer(index));
return null;
case 1:
return new String(database.getRecord(index));
case 2:
database.setRecord(index, data, 0, data.length);
return null;
default:
break;
}
}
} catch (InvalidRecordIDException iri) {
throw new RecordStoreException(iri.getMessage());
}
}
return null;
|
public synchronized void | add(java.lang.String record)Add the record to the database
Add the recordID to our vector
Update the database's last ID counter
if (database.getNumRecords() != 0) {
database.addRecord(record.getBytes(), 0, record.getBytes().length);
recordIDs.addElement(new Integer(++lastID));
database.setRecord(1, (String.valueOf(lastID)).getBytes(),
0, (String.valueOf(lastID)).length());
} else {
recordIDs.addElement(new Integer(++lastID));
database.addRecord((String.valueOf(lastID)).getBytes(),
0, (String.valueOf(lastID)).length());
try {
database.addRecord(record.getBytes(), 0,
record.getBytes().length);
} catch (RecordStoreException rs) {
recordIDs.removeElement(new Integer(lastID--));
database.setRecord(1, (String.valueOf(lastID)).getBytes(),
0, (String.valueOf(lastID)).length());
throw rs;
}
}
|
public void | cleanUp(java.lang.String fileName)Remove the database from persistant storage
RecordStore.deleteRecordStore(fileName);
open(fileName);
|
public void | close()Close the database and remove it from persistant
storage if it is empty
if (database.getNumRecords() == 0) {
String fileName = database.getName();
database.closeRecordStore();
database.deleteRecordStore(fileName);
} else {
database.closeRecordStore();
}
|
public synchronized void | delete(java.lang.String s)Delete the record from the database and remove that recordID from the
vector of used recordIDs
action(s, null, 0);
|
public int | getNumRecords()Return the number of records in the database
return database.getNumRecords();
|
public void | open(java.lang.String fileName)Initializes the database and if it's not a new database, loads the
recordID of the last record out of the first position in the
RecordStore . We have stored it there when we closed the
database, then checks each ID from 1 to lastID to see if they exist in
the database and then add the IDs that exist to the recordIDs
Vector
database = RecordStore.openRecordStore(fileName, true);
recordIDs = new Vector();
try {
if (database.getNumRecords() != 0) {
try {
lastID =
Integer.valueOf(
new String(database.getRecord(1))).intValue();
for (int i = 1; i <= lastID; i++) {
try {
database.getRecord(i);
recordIDs.addElement(new Integer(i));
} catch (RecordStoreException rs) {}
}
} catch (InvalidRecordIDException iri) {
throw new RecordStoreException(iri.getMessage());
}
}
} catch (RecordStoreNotOpenException rsno) {
throw new RecordStoreException(rsno.getMessage());
}
|
public synchronized java.lang.String | search(java.lang.String s)Find and return a record
return (String) action(s, null, 1);
|
public synchronized void | update(java.lang.String s, byte[] data)Update the record with the name s with the data
in the byte[] array
action(s, data, 2);
|