Wednesday, 28 November 2012

FUNDAMENTAL FILE STRUCTURE CONCEPTS


General

persistent

Retained after execution of the program which created it.


  • When we build file structures, we are making it possible to make data persistent. That is, one program can store data from memory to a file, and terminate. Later, another program can retrieve the data from the file, and process it in memory.
  • In this chapter, we look at file structures which can be used to organize the data within the file, and at the algorithms which can be used to store and retrieve the data sequentially.                                


Field and Record Organization

 

Data Representation in Memory

record

A subdivision of a file, containing data related to a single entity.

field

A subdivision of a record containing a single attribute of the entity which the record describes.

stream of bytes

A file which is regarded as being without structure beyond separation 
into a sequential set of bytes. 
 
 
 
  • Within a program, data is temporarily stored in variables.
  • Individual values can be aggregated into structures, which can be treated as a single variable with parts.
  • In C++, classes are typically used as as an aggregate structure.
  • C++ Person class (version 0.1):
    class Person {
      public:
        char FirstName [11];
        char LastName[11];
        char Address [21];
        char City [21];
        char State [3];
        char ZIP [5];
    };
    
  • With this class declaration, variables can be declared to be of type Person.  The individual fields within a Person can be referred to as the name of the variable and the name of the field, separated by a period (.).
  • C++ Program:
    #include 
    
    class Person {
      public:
        char FirstName [11];
        char LastName[11];
        char Address [31];
        char City [21];
        char State [3];
        char ZIP [5];
    };
    
    void Display (Person);
    
    int main () {
      Person Clerk;
      Person Customer;
      
      strcpy (Clerk.FirstName, "Fred");
      strcpy (Clerk.LastName, "Flintstone");
      strcpy (Clerk.Address, "4444 Granite Place");
      strcpy (Clerk.City, "Rockville");
      strcpy (Clerk.State, "MD");
      strcpy (Clerk.ZIP, "00001");
      
      strcpy (Customer.FirstName, "Lily");
      strcpy (Customer.LastName, "Munster");
      strcpy (Customer.Address, "1313 Mockingbird Lane");
      strcpy (Customer.City, "Hollywood");
      strcpy (Customer.State, "CA");
      strcpy (Customer.ZIP, "90210");
      
      Display (Clerk);
      Display (Customer);
    }
    
    void Display (Person Someone) {
      cout << Someone.FirstName << Someone.LastName
           << Someone.Address << Someone.City
           << Someone.State << Someone.ZIP;
    }
    
  • In memory, each Person will appear as an aggregate, with the individual values being parts of the aggregate:
    Person
    Clerk
    FirstNameLastName AddressCity StateZIP
    FredFlintstone 4444 Granite PlaceRockville MD0001
  • The output of this program will be:
    FredFlintstone4444 Granite PlaceRockvilleMD00001LilyMunster1313 Mockingbird LaneHollywoodCA90210
  • Obviously, this output could be improved.  It is marginally readable by people, and it would be difficult to program a computer to read and correctly interpret this output. 


Delineation of Records in a File

 

Fixed Length Records

A record which is predetermined to be the same length as the other records in the file.

 
  • Record 1 Record 2 Record 3 Record 4 Record 5
  • The file is divided into records of equal size.
  • All records within a file have the same size.
  • Different files can have different length records.
  • Programs which access the file must know the record length.
  • Offset, or position, of the nth record of a file can be calculated.
  • There is no external overhead for record separation.
  • There may be internal fragmentation (unused space within records.)
  • There will be no external fragmentation (unused space outside of records) except for deleted records.
  • Individual records can always be updated in place.
  • Algorithms for Accessing Fixed Length Records
  • Code for Accessing Fixed Length Records
  • Code for Accessing String Records
  • Example (80 byte records):
       0  66 69 72 73 74 20 6C 69 6E 65  0  0  1  0  0  0  first line......
      10   0  0  0  0  0  0  0  0 FF FF FF FF  0  0  0  0  ................
      20  68 FB 12  0 DC E0 40  0 3C BA 42  0 78 FB 12  0  h.....@.<.B.x...
      30  CD E3 40  0 3C BA 42  0  8 BB 42  0 E4 FB 12  0  ..@.<.B...B.....
      40  3C 18 41  0 C4 FB 12  0  2  0  0  0 FC 3A 7C  0  <.A..........:|.
      50  73 65 63 6F 6E 64 20 6C 69 6E 65  0  1  0  0  0  second line.....
      60   0  0  0  0  0  0  0  0 FF FF FF FF  0  0  0  0  ................
      70  68 FB 12  0 DC E0 40  0 3C BA 42  0 78 FB 12  0  h.....@.<.B.x...
      80  CD E3 40  0 3C BA 42  0  8 BB 42  0 E4 FB 12  0  ..@.<.B...B.....
      90  3C 18 41  0 C4 FB 12  0  2  0  0  0 FC 3A 7C  0  <.A..........:|.
    
  • Advantage: the offset of each record can be calculated from its record number.  This makes direct access possible.
  • Advantage: there is no space overhead.
  • Disadvantage: there will probably be internal fragmentation (unusable space within records.)
 
 

Delimited Variable Length Records

 

variable length record

A record which can differ in length from the other records of the file.

delimited record

A variable length record which is terminated by a special character or sequence of characters.

delimiter

A special character or group of characters stored after a field or record, which indicates the end of the preceding unit.

     

  • Record 1 # Record 2 # Record 3 # Record 4 # Record 5 #
  • The records within a file are followed by a delimiting byte or series of bytes.
  • The delimiter cannot occur within the records.
  • Records within a file can have different sizes.
  • Different files can have different length records.
  • Programs which access the file must know the delimiter.
  • Offset, or position, of the nth record of a file cannot be calculated.
  • There is external overhead for record separation equal to the size of the delimiter per record.
  • There should be no internal fragmentation (unused space within records.)
  • There may be no external fragmentation (unused space outside of records) after file updating.
  • Individual records cannot always be updated in place.
  • Algorithms for Accessing Delimited Variable Length Records
  • Code for Accessing Delimited Variable Length Records
  • Code for Accessing Variable Length Line Records
  • Example (Delimiter = ASCII 30 (IE) = RS character:
       0  66 69 72 73 74 20 6C 69 6E 65 1E 73 65 63 6F 6E  first line.secon
      10  64 20 6C 69 6E 65 1E                             d line.         
    
  • Example (Delimiter = '\n'):
       0  46 69 72 73 74 20 28 31 73 74 29 20 4C 69 6E 65  First (1st) Line
      10   D  A 53 65 63 6F 6E 64 20 28 32 6E 64 29 20 6C  ..Second (2nd) l
      20  69 6E 65  D  A                                   ine..           
    
  • Disadvantage: the offset of each record cannot be calculated from its record number.  This makes direct access impossible.
  • Advantage: there is space overhead for the length prefix.
  • Advantage: there will probably be no internal fragmentation (unusable space within records.) 

   

    Delineation of Fields in a Record

 

  Fixed Length Fields

  • Field 1 Field 2 Field 3 Field 4 Field 5
  • Each record is divided into fields of correspondingly equal size.
  • Different fields within a record have different sizes.
  • Different records can have different length fields.
  • Programs which access the record must know the field lengths.
  • There is no external overhead for field separation.
  • There may be internal fragmentation (unused space within fields.)
 
   

  Delimited Variable Length Fields

  

  • Field 1 ! Field 2 ! Field 3 ! Field 4 ! Field 5 !
  • The fields within a record are followed by a delimiting byte or series of bytes.
  • Fields within a record can have different sizes.
  • Different records can have different length fields.
  • Programs which access the record must know the delimiter.
  • The delimiter cannot occur within the data.
  • If used with delimited records, the field delimiter must be different from the record delimiter.
  • There is external overhead for field separation equal to the size of the delimiter per field.
  • There should be no internal fragmentation (unused
 
  

   Length Prefixed Variable Length Fields

  

  • 12 Field 1 4 Field 2 10 Field 3 8 Field 4 7 Field 5
  • The fields within a record are prefixed by a length byte or bytes.
  • Fields within a record can have different sizes.
  • Different records can have different length fields.
  • Programs which access the record must know the size and format of the length prefix.
  • There is external overhead for field separation equal to the size of the length prefix per field.
  • There should be no internal fragmentation (unused space within fields.)
  

    

MAGNETIC DISKS

 

magnetic disk


A disk read and written by electromagnetic means

hard disk


A magnetic disk with a rigid substrate

floppy disk


A magnetic disk with a flexible substrate

fixed disk


A disk drive with non-removable media. 

cylinder


The set of tracks of a disk drive which can be accessed without changing the position of the access arm. 

track


The (circular) area on a disk platter which can be accessed without moving the access arm of the drive. 

sector


A fixed size physical data block on a disk drive. 

seek


To move to a specified location in a file.

block


A physical data record, separated on the medium from other blocks by inter-block gaps.

interblock gap


An area between data blocks which contains no data and which separates the blocks. 

access time


The total time required to store or retrieve data.

seek time


The time required for the head of a disk drive to be positioned to a designated cylinder. 

rotational delay


The time required for a designated sector to rotate to the head of a disk drive. 

transfer time


The time required to transfer the data from a sector, once the transfer has begun.  
 
 
 
 

Magnetic Technology

 

  • Data is stored on a magnetic disk by controlling the direction in which small areas of the disk surface are magnetized.
  • Data is stored on a disk serially, that is, one bit at a time. 
  • Data is recorded on magnetic disks as a series of magnitized areas on the surface of the disk:
    Magnetic pits and lands
  • The data is read by a head with a coil which is sensitive to changes in magnetism on the data track as the disk rotates. 
    Magnetic mechanism
  • The heads are attached to a common shuttle, which moves them in and out, to different redial positions, together:
    Magnetic track
  • The data is blocked into physical sectors, which are arranged in many concentric circles called tracks.  For hard drives, the sectors are all the same physical size, and the number of sectors varies with the circumference of the track
    Magnetic track
  • For floppy disks, there are the same number of sectors in each track, and the physical size of a sectors varies with the circumference of the track:
    Magnetic track
  • Track and cylinder locations are determined by the physical geometry of the drive. 
    Magnetic track
  • Track and cylinder numbers begin with 0. 
  • Tracks are often referred to as heads. 
    Magnetic track
  • The sectors on each track are numbered from 1 up:
    Magnetic track
  • The block size of magnetic disks is almost always 512 bytes.:
  • The blocks, or sectors, are separated by interblock gaps. 
  • Fixed disks are always hard disks. 
  • Removable disks are usually floppy disks. 
  • Accessing a sector on the drive requires three stems:
    1. Seeking: Moving the head to the right cylinder.
    2. Rotation: WAaiting for the right sector to reach the head.
    3. Transfer: WAaiting for the sector to pass under the head, reading or writing the data.
  • Seek time is affected by the size of the drive, the number of cylinders in the drive, and the mechanical responsiveness of the access arm.
  • Average seek time is approximately the time to move across 1/3 of the cylinders.
  • Rotational delay is also referred to as latency.
  • Rotational delay is inversely proportional to the rotational speed of the drive.
  • Average rotational delay is the time for the disk to rotate 180°.
  • Actual transfer time may be limited by the disk interface.
  • Transfer is inversely proportional to the rotational speed of the drive.
  • Transfer time is inversely proportional to the physical length of a sector.
  • Transfer time is roughly inversely proportional to the number of sectors per track.
  • Data is always read or written in complete blocks. 

 

 
 
 
 
 
 

SECONDARY STORAGE AND SYSTEM SOFTWARE



Disks 

 

  • Data is stored on a magnetic disk by controlling the direction in which small areas of the disk surface are magnetized.
  • Data is stored on a disk serially, that is, one bit at a time. 

 Organization of Disks

 

 

fixed disk

A disk drive with non-removable media.

block

A physical data record, separated on the medium from other blocks by inter-block gaps.

sector

A fixed size physical data block on a disk drive.

interblock gap

An area between data blocks which contains no data and which separates the blocks.

cylinder

The set of tracks of a disk drive which can be accessed without changing the position of the access arm.

track

The (circular) area on a disk platter which can be accessed without moving the access arm of the drive. 
  • Disk Drive Physical Structure
  • Data is recorded on each surface of the disk in sectors, which are located in concentric circles.
  • The sectors are separated by gaps which contain no data.
  • On PC disk drives, each sector contains 512 bytes of data.
  • Track and cylinder locations are determined by the physical geometry of the drive.
  • Track and cylinder numbers begin with 0.
  • Tracks are often referred to as heads.
 

 Organizing Tracks by Sector

  • Sector locations are determined by the electronics of the drive, and are identified by recorded address marks.
  • Sector numbers begin with 1.
  • Today, logically adjacent sectors are typically physically adjacent.

 Estimating Capacities and Space Needs

  • bytes/track = sectors/track * bytes/sector
  • bytes/cylinder = tracks/cylinder * bytes/track
  • bytes/drive = cylinders/drive * bytes/cylinder
  • bytes/drive = cylinders/drive * tracks/cylinder * sectors/track * bytes/sector

 File allocation

  cluster

A group of sectors handled as a unit of file allocation.

   extent

A physical section of a file occupying adjacent clusters.

   fragmentation
  

                 unused space with in a file.
                 


  

The Cost of a Disk Access
 
 

direct access device

A data storage device which supports direct access.

direct access

Accessing data from a file by record position with the file, without accessing intervening records.

access time

The total time required to store or retrieve data.

transfer time

The time required to transfer the data from a sector, once the transfer has begun.

seek time

The time required for the head of a disk drive to be positioned to a designated cylinder.

rotational delay

The time required for a designated sector to rotate to the head of a disk drive.

 

  • Access time of a disk is related to physical movement of the disk parts.
  • Disk access time has three components: seek time, rotational delay, and transfer time.
  • Seek time is affected by the size of the drive, the number of cylinders in the drive, and the mechanical responsiveness of the access arm.
  • Average seek time is approximately the time to move across 1/3 of the cylinders.
  • Rotational delay is also referred to as latency.
  • Rotational delay is inversely proportional to the rotational speed of the drive.
  • Average rotational delay is the time for the disk to rotate 180°.
  • Transfer is inversely proportional to the rotational speed of the drive.
  • Transfer time is inversely proportional to the physical length of a sector.
  • Transfer time is roughly inversely proportional to the number of sectors per track.
  • Actual transfer time may be limited by the disk interface.

 

Effect of Block Size

  • Fragmentation waste increases as cluster size increases.
  • Average access time decreases as cluster size increases.

Disk as a bottleneck

 

striping

The distribution of single files to two or more physical disk drives.

Redundant Array of Inexpensive Disks

An array of multiple disk drives which appears as a single drive to the system.

RAM disk

A virtual disk drive which actually exists in main memory.

solid state disk

A solid state memory array with an interface which responds as a disk drive.

cache

Solid state memory used to buffer and store data temporarily.

  • Several techniques have been developed to improve disk access time.
  • Striping allows disk transfers to be made in parallel.
  • There are 6 versions, or levels, of RAID technology.
  • RAID-0 uses striping.
  • RAID-0 improves access time, but does not provide redundancy.
  • RAID-1 uses mirroring, in which two drives are written with the same data.
  • RAID-1 provides complete redundancy.  If one drive fails, the other provides data backup.
  • RAID-1 improves read access time, but slows write access time.
  • RAM disks appear to programs as fast disk drives.
  • RAM disks are volatile.
  • Solid state disks appear to computer systems as fast disk drives.
  • Solid state disks are used on high performance data base systems.
  • Caching improves average access time.
  • Disk caching can occur at three levels: in the computer main memory, in the disk controller, and in the disk drive.
  • Windows operating systems use main memory caching.
  • Disk controller caching requires special hardware.
  • Most disk drives now contain caching memory.
  • With caching, writes are typically reported as complete when the data is in the cache. 
  • The physical write is delayed until later.
  • With caching, reads typically read more data than is requested, storing the unrequested data in the cache. 
  • If a read can be satisfied from data already in the cache, no additional physical read is needed.
  • Read caching works on average because of program locality. 

File System Organization

 

File Allocation Table

A table on a disk volume containing chained lists of the physical locations of all files on the volume.

index node

A data structure, associated with a file, which describes the file.

Storage as a Hierarchy

 

 Journey of a Byte

 

      

    Introduction to CD-ROM

  • A single disc can hold more than 600 megabytes of data (~ 400 books of the textbook’s size)
  • CD-ROM is read only. i.e., it is a publishing medium rather than a data storage and retrieval like magnetic disks.
  • CD-ROM Strengths: High storage capacity, inexpensive price, durability.
  • CD-ROM Weaknesses: extremely slow seek performance (between 1/2 a second to a second) ==> Intelligent File Structures are critical.
     

    Physical Organization of CD-ROM

    •  CD-ROM is a descendent of CD Audios. i.e., listening to music is sequential and does not require fast random access to data. 

    • Reading Pits and Lands: CD-ROMs are stamped from a glass master disk which has a coating that is changed by the laser beam. When the coating is developed, the areas hit by the laser beam turn into pits along the track followed by the beam. The smooth unchanged areas between the pits are called 


    CD-ROM Strengths & Weaknesses

    • Seek Performance: very bad
       
  • Data Transfer Rate: Not Terrible/Not Great

  • Storage Capacity: Great

    • Benefit: enables us to build indexes and other support structures that can help overcome some of the limitations associated with CD-ROM’s poor performance.
  • Read-Only Access: There can’t be any changes ==> File organization can be optimized.

  • No need for interaction with the user (which requires a quick response)
  •  etc..


 








 

 

INTRODUCTION

 

File

A data structure in a file system which maps to names to file system objects such as files or other directories.
File Structure
A pattern for arranging data in a file.
Algorithm
A finite set of well-defined rules for the solution of a problem in a finite number of steps.
Data Structure
         A pattern for organizing data in a program.
 

 File Access

 

  • volatile storage
Storage which loses its contents when power is removed.
  • non-volatile storage
Storage which retains its contents when power is removed.
  • persistent data
Information which is retained after execution of the program which created it.
Goals for Design of File Structures and Algorithms
 
  • Minimize the number of disk accesses.
  • If possible, transfer all information needed in one access.
  • Group related information physically so it can be accessed together.

Problems and Concerns

  • File data is frequently dynamic - that is, it changes from time to time.
  • Designing file structures for changes adds complexity.
  • Typical file sizes are growing.
  • Solutions which work for small files may be inadequate for large files.
  • File structures, algorithms, and data structures must work together.