190 likes | 327 Views
Record Access. Chapter 5. Sequential Access. Performance - what do we count? Number of comparisons? Number of low level reads O (n) run time. Sequential Access. When to use: Searching for pattern Small files Files rarely processed
E N D
Record Access Chapter 5
Sequential Access • Performance - what do we count? • Number of comparisons? • Number of low level reads • O(n) run time
Sequential Access • When to use: • Searching for pattern • Small files • Files rarely processed • Searches of a secondary field, where numerous matches expected
Sequential Access • Unix Tools • cat • wc • grep
Direct Access • Allows direct seeking of a record. • Record position can be referrer to by: • Record start byte offset from beginning of file • RRN - Relative Record Number • First - RRN 1 • Second - RRN 2 • Byte offset = n x r • n - RRN • r - record size
Direct Access int IOBuffer::DRead(istream &stream, int recref) { stream . seekg (recref, ios::beg); if (stream . tellg () != recref) return -1; return Read (stream); } int IOBuffer::DWrite(ostream &stream, int recref) const { stream . seekp (recref, ios::beg); if (stream . tellp () != recref) return -1; return Write (stream); }
Header Record • Useful to keep track of general information about a file • Record count • Record size • Header size • Information can be stored at beginning of file • This first record is a different structure
Header Record int IOBuffer::ReadHeader (istream & stream) { char str[headerSize+1]; stream . seekg (0, ios::beg); stream . read (str, headerSize); if (! stream . good ()) return -1; if (strncmp (str, headerStr, headerSize)==0) return headerSize; else return -1; } int IOBuffer::WriteHeader (ostream & stream) const { stream . seekp (0, ios::beg); stream . write (headerStr, headerSize); if (! stream . good ()) return -1; return headerSize; }
Encapsulating Record I/O Operations • The idea of a “buffer” can be hidden • Methods can be created to perform Read and Write operations on a given Object type. • The idea is to make object persistent. • Question: How do we provide support for different object types without creating a version of the class for each class? • Use a C++ Template • Provides parameterized function and class definitions
RecordFile Template • Suppose we want to create a version of Record file for two different record types. Person p; RecordFile pFile; pFile.Read(p); Course c ; RecordFile cFile; cFile.Read(c); • How?
RecordFile Template • Create a version of RecordFile which operates on Person file: RecordFile<Person> PersonFile(Buffer); • This uses Iobuffer Buffer, creating PersonFile for operating on file Person person; PersonFile.Create(“person.dat”.ios::in); PersonFile.Read(person); PersonFile.Append(person); PersonFile.Open(“person.dat”,ios::in);
RecordFile Template #include "buffile.h" #include "iobuffer.h" // template class to support direct read and write of records // The template parameter RecType must support the following // int Pack (BufferType &); pack record into buffer // int Unpack (BufferType &); unpack record from buffer template <class RecType> class RecordFile: public BufferFile {public: int Read (RecType & record, int recaddr = -1); int Write (const RecType & record, int recaddr = -1); int Append (const RecType & record, int recaddr = -1); RecordFile (IOBuffer & buffer): BufferFile (buffer) {} };
RecordFile Template // template method bodies template <class RecType> int RecordFile<RecType>::Read (RecType & record, int recaddr = -1) { int readAddr, result; readAddr = BufferFile::Read (recaddr); if (readAddr==-1) return -1; result = record . Unpack (Buffer); if (!result) return -1; return readAddr; }
RecordFile Template #include "buffile.h" template <class RecType> int RecordFile<RecType>::Write (const RecType & record, int recaddr = -1) { int result; result = record . Pack (Buffer); if (!result) return -1; return BufferFile::Write (recaddr); }
RecordFile Template template <class RecType> int RecordFile<RecType>::Append (const RecType & record, int recaddr = -1) { int result; result = record . Pack (Buffer); if (!result) return -1; return BufferFile::Append (); }
File Access and File Organization • Record Length Options (Organization) • Variable-length • Fixed Length • Access Methods (Access) • Sequential access • Direct access • How are these related?
Advanced File Structures • Self-Describing files • Data can be added to a file to describe the data and contents • This can add flexibility to the file structure • The file structure can evolve over time • Processing routines can be general purpose
Self-Describing files • Consider a header file which completely describes the structure of a file. • The header can contain: • The type of file • Size of header • The number of fields • The name of each field • The size of each field
Self-Describing files • Text Based header record IOBUFFERFIXED\065\06\0NAME\020\0ADDRESS\020\0 CITY\010\0STATE\02\0ZIPPHONE\08 • Binary Record Header