1 / 19

Record Access

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

jared
Télécharger la présentation

Record Access

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Record Access Chapter 5

  2. Sequential Access • Performance - what do we count? • Number of comparisons? • Number of low level reads • O(n) run time

  3. Sequential Access • When to use: • Searching for pattern • Small files • Files rarely processed • Searches of a secondary field, where numerous matches expected

  4. Sequential Access • Unix Tools • cat • wc • grep

  5. 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

  6. 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); }

  7. 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

  8. 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; }

  9. 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

  10. 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?

  11. 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);

  12. 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) {} };

  13. 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; }

  14. 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); }

  15. 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 (); }

  16. File Access and File Organization • Record Length Options (Organization) • Variable-length • Fixed Length • Access Methods (Access) • Sequential access • Direct access • How are these related?

  17. 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

  18. 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

  19. Self-Describing files • Text Based header record IOBUFFERFIXED\065\06\0NAME\020\0ADDRESS\020\0 CITY\010\0STATE\02\0ZIPPHONE\08 • Binary Record Header

More Related