View
192
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Buffering
Citation preview
� Storing collection of records.
� Reading collection of records.
� Writing collection of records.
� Buffering Algorithm.
Dr. Hussien M.
Sharaf2
� Company info class only represents single record.
� A collection of records needs to be stored into a dynamic array.
� The collection of records can be encapsulated into a class that maps to a file.
Dr. Hussien M.
Sharaf3
Company1
Company2
…..
Company N
Dr. Hussien M.
Sharaf4
CompanyInfoCollection
StructureRecord 3 Record 4Record 1 Record 2210 250 180 190
1. Store collection of records in a dynamic array
2. Store file full path name.
3. Store and manage the indexing of records.
Dr. Hussien M.
Sharaf5
CompanyInfoCollection
FunctionalitiesRecord 3 Record 4Record 1 Record 2210 250 180 190
1. Responsible for opening and closing of the file stream.
2. Calling read or write of companyInfo without knowing the details of read/write single line.
3. Manage the buffering of records while reading or writing.
4. Count the number of records in a file.
Dr. Hussien M.
Sharaf6
Algorithm#1 Writing collection of
recordsRecord 3 Record 4Record 1 Record 2210 250 180 190
� While (len(Buffer) < bufferingSize)
� {
� companyInfo[i].writeIntoStream( stringStream)
� stringStream<<recordDelimiter
� i++
� }
� Stringstream>>Buffer
� FileStream.write(Buffer)
Dr. Hussien M.
Sharaf7
Algorithm#2 Reading collection
of records
Record 3 Record 4Record 1 Record 2210 250 180 190
� FileStream.read(Buffer, BufferingSize+1)
� Stringstream<<Buffer;
� While (! Stringstream.EOF)
� {
� companyInfo[i].ReadFromStream(Stringstream)
� i++
� }
1. Read x MB into memory buffer (i.e. char array).
2. Make sure to stop reading at the end of a record. How can this be done?
3. Put the buffered data into a string stream.
Dr. Hussien M.
Sharaf8
4. Pass the string stream to an instance of companyInfo which extracts fields.
5. Add the new companyInfo to the array of records.
6. Manage the index by adding an entry with the correct offset.
7. Repeat steps 1-6 until EOF
Dr. Hussien M.
Sharaf9
Dr. Hussien M.
Sharaf10
� Store the byte offset of each record into a list stored in another file.
Dr. Hussien M.
Sharaf11
Remember Indexing
� Problems:
1. Two files are used: one for the data and another for
CompanyName offset
IBM 0Record1
\n
\n
Google 211Record2 \n
Microsoft 462Record3 \n
ITE 643Record4 \n
Dr. Hussien M.
Sharaf12
class CompanyInfoCollection
{
private:
vector<CompanyInfo> Records;
RecordIndex recordIndexes;
public:
//read delimited fields
bool WriteDelimRecords(ostream& ,char);
//read delimited fields
bool readDelimRecords(istream&);
};
Dr. Hussien M.
Sharaf13
Read/write from
streamBuffer
Single companyInfo
RecordsCollection
File stream
Read/write from
streamBuffer
Single companyInfo
Records Indexes
Record 2211 Record 3462
�Continue using the CompanyInfo program:
1) Where would you put the following functionalities:a. Read the multiple records into a buffer.b. Split a single record into fields.c. Append a single record into a collection of
records.d. Search for a record given a company
name.
Dr. Hussien M.
Sharaf14
�Continue using the CompanyInfo program:
2) Implement Algorithm#1 into the CompanyInfo program.
3) Implement Algorithm#2 into the CompanyInfo program.
Dr. Hussien M.
Sharaf15