11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

282 Chap. 8 File Processing <strong>and</strong> External Sortingbecome important. Note however th<strong>at</strong> the in-memory copy time will always be farless than the time required to write the contents of a buffer to disk. For applic<strong>at</strong>ionswhere disk I/O is the bottleneck for the program, even the time to copy lots ofinform<strong>at</strong>ion between the buffer pool user <strong>and</strong> the buffer might be inconsequential.Another advantage to buffer passing is the reduction in unnecessary read oper<strong>at</strong>ionsfor d<strong>at</strong>a th<strong>at</strong> will be overwritten anyway.You should note th<strong>at</strong> these implement<strong>at</strong>ions for the buffer pool ADT do not usegenerics. Instead, the space parameter <strong>and</strong> the buffer pointer are declared to bebyte[]. When a class uses a generic, th<strong>at</strong> means th<strong>at</strong> the record type is arbitrary,but th<strong>at</strong> the class knows wh<strong>at</strong> the record type is. In contrast, using byte[] for thespace means th<strong>at</strong> not only is the record type arbitrary, but also the buffer pool doesnot even know wh<strong>at</strong> the user’s record type is. In fact, a given buffer pool mighthave many users who store many types of records.In a buffer pool, the user decides where a given record will be stored but hasno control over the precise mechanism by which d<strong>at</strong>a are transferred to the backingstorage. This is in contrast to the memory manager described in Section 12.3 inwhich the user passes a record to the manager <strong>and</strong> has no control <strong>at</strong> all over wherethe record is stored.8.4 The Programmer’s View of FilesThe Java programmer’s logical view of a r<strong>and</strong>om access file is a single streamof bytes. Interaction with a file can be viewed as a communic<strong>at</strong>ions channel forissuing one of three instructions: read bytes from the current position in the file,write bytes to the current position in the file, <strong>and</strong> move the current position withinthe file. You do not normally see how the bytes are stored in sectors, clusters, <strong>and</strong>so forth. The mapping from logical to physical addresses is done by the file system,<strong>and</strong> sector-level buffering is done autom<strong>at</strong>ically by the disk controller.When processing records in a disk file, the order of access can have a gre<strong>at</strong>effect on I/O time. A r<strong>and</strong>om access procedure processes records in an orderindependent of their logical order within the file. Sequential access processesrecords in order of their logical appearance within the file. Sequential processingrequires less seek time if the physical layout of the disk file m<strong>at</strong>ches its logicallayout, as would be expected if the file were cre<strong>at</strong>ed on a disk with a high percentageof free space.Java provides several mechanisms for manipul<strong>at</strong>ing disk files. One of the mostcommonly used is the R<strong>and</strong>omAccessFile class. The following methods canbe used to manipul<strong>at</strong>e inform<strong>at</strong>ion in the file.• R<strong>and</strong>omAccessFile(String name, String mode): Class constructor,opens a disk file for processing.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!