Develop a class
Data that the interface DB implemented. The class Data should provide methods to manipulate (read, write) binary files using the described data format. package data;The class has an additional Data
/ ** *
Rud Exercise 2011
* * * @ author data.DB.java
(c) 2011, Harald R. Haberstroh
* 03/01/2011 * / import
java.rmi.Remote;
import java.rmi.RemoteException;
/ ** * @ author Harald
R. Haberstroh (hh)
* * / public interface
DB extends Remote {
/ ** * Lock the
given data set. If the record is already locked, then
* wait ().
*
* * @ Param recNo
number to lock the record.
* @ return Block identification.
* @ throws RemoteException * @ throws
RecordNotFoundException
* invalid record number
* / public long
lock (int recNo) throws RemoteException, RecordNotFoundException;
/ ** *
lifting lock again. notify (). * * @ Param
recNo
* number of the locked record * @ param
LockCookie
* Block Identfikation.
* @ throws RemoteException * @ throws
RecordNotFoundException
* invalid record number
* @ throws SecurityException *
LockCookie invalid or not tempted
* / public void
unlock (int recNo, long LockCookie) throws RemoteException,
RecordNotFoundException, SecurityException;
/ ** *
put on the new record.
* * @ param record * New Record
* @ return number of the new record
* @ throws RemoteException
* / public int
create (Record record) throws RemoteException;
/ ** * delete
record. Lock is released, notify ();
* * @ param recNo
* Number of the record. * @ Param
LockCookie
* Block Identfikation.
* @ throws RemoteException
* @ throws RecordNotFoundException
* invalid record number
* @ throws SecurityException
* invalid LockCookie not lured
* /
public void delete (int recNo, long LockCookie) throws RemoteException,
RecordNotFoundException, SecurityException;
/ ** *
change record (overwrite with the given data set).
* * @ param record new record
* * @ param number * recNo
of the locked record * @ param
LockCookie
* Block Identfikation.
* @ throws RemoteException
* @ throws RecordNotFoundException
* invalid record number
* @ throws SecurityException
* invalid LockCookie not lured
* /
public void update (Record record, int recNo, long LockCookie)
throws RemoteException, RecordNotFoundException, SecurityException ;
/ ** *
search records.
* * @ param criteria
* String array of criteria, null matcht everything else
* String representation of the search criterion (corresponding to the data in
* Record).
* @ return array of record numbers
* @ throws RemoteException *
/
public int [] find (String [] criteria) throws RemoteException;
/ ** * read
record. * * @ Param
recNo
* number of data to be read set.
* @ return Record
* @ throws RemoteException * @ throws
RecordNotFoundException
* Record does not exist.
* / public Record
read (int recNo) throws RemoteException, RecordNotFoundException;}
Data Constructor (String filename) provide up to open the file filename (eg "machines.db" ) and for working with the interface DB defined "trusted". The constructor must take the necessary exceptions, if the file can be opened. The parameter String [] criteria the method find () contains the search criterion, where each field is assigned a string. Should therefore be sought only for a name (see below), must appear criteria [1] a surname (or part thereof). All other elements of the array must then be zero . If after several Criteria are selected, the corresponding fields must be assigned values. find () then provides an array of the indices of the matching records. The record number
recNo is always the index within the file! Deleted records are marked simply as deleted (see below) and must be used when creating new records first (only if no record exists is deleted, increases the file).
RecordNotFoundException is thrown whenever recNo on a deleted record has one or if the record does not exist. Solve the problem that only with the simple RecordNotFoundException end of the file can not be determined, as appropriate. Record must be suitably implemented. to records can be blocked (the class
Data can be used by multiple threads). This is the method lock () a (any) allocated to provide the record number (LockCookie or lock-identification) and block the sentence. The methods that require a lock must be verified on the basis of this LockCookie if the record is locked and perform the appropriate operation. If the record is not locked or invalid, the LockCookie so is a throw SecurityException . The method unlock () unlocks again. After a delete () you need (you can) make no unlock () . data format
The strings are encoded using the encoding ISO-8859-1 , meaning that each character is a byte-coded and there are only ASCII characters and the Western European extensions allowed. There are strings of fixed length (to be filled with blanks) and variable-length strings (such as C-strings are
'\\ 0' completed) with an upper limit. The file with the (test) data has the following format: The file begins with
a header :
4 bytes magic cookie, value 4242
4 offset in bytes to the actual data
are 2 bytes number of fields per record
each field is the following:
2 bytes length field name
n bytes Field name
2 bytes Length Field Description
n bytes Description field for the table view
1 byte data type: 'F' ... fix, 'V' ... variable, 'C' ... a character field length 2 bytes
After field descriptions, ie from the offset to find the actual records in accordance with the descriptions in the header: 2 bytes
Flag Deleted (deleted == 0 ... = 0 ... active!)
n bytes of data in accordance with the record description
test file machines.db
This file contains only the device (and its graphical coordinates):
| field name | field description | (maximum) length | type | comment |
| id | Device Id | 10 | string '\\ 0' terminated | eg "z23" |
| name | device name | 100 | String '\\ 0' terminated | device description, eg "loading robot Hall B" |
| x | x fixed | 5 | string length | eg "50" (front padded with spaces) |
| y | y | 5 | string fixed length | eg "200" |
Test Class Data
letter matching tests to test the class
Data . The first test is simply the file machines.db in readable form to the console. Further tests are to test the functionality of course, and the locking.
Downloads
- Interface DB.java
- test file machines.db
tax
you call the class project
- name -data eg 5AD-Meier-data and check it on at the CVS. Date to be announced. How's more
The data class is then later in a client / server system are used, where several graphical clients to edit a binary file. A claim to follow ...
0 comments:
Post a Comment