disk storage, basic file structures, and hashing disk storage devices disk storage devices files of...

28
Disk Storage, Basic File Disk Storage, Basic File Structures, and Hashing Structures, and Hashing Disk Storage Devices Disk Storage Devices Files of Records Files of Records Operations on Files Operations on Files Unordered Files Unordered Files Ordered Files Ordered Files Hashed Files Hashed Files

Upload: jeanette-hulsey

Post on 29-Mar-2015

234 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Disk Storage, Basic File Structures, and Hashing Disk Storage Devices Disk Storage Devices Files of Records Files of Records Operations on Files Operations

Disk Storage, Basic File Structures, Disk Storage, Basic File Structures, and Hashingand Hashing

Disk Storage DevicesDisk Storage DevicesFiles of RecordsFiles of RecordsOperations on FilesOperations on FilesUnordered FilesUnordered FilesOrdered FilesOrdered FilesHashed FilesHashed Files

Page 2: Disk Storage, Basic File Structures, and Hashing Disk Storage Devices Disk Storage Devices Files of Records Files of Records Operations on Files Operations

Chapter OutlineChapter Outline

Disk Storage DevicesDisk Storage DevicesFiles of RecordsFiles of RecordsOperations on FilesOperations on FilesUnordered FilesUnordered FilesOrdered FilesOrdered FilesHashed FilesHashed Files

Dynamic and Extendible Hashing Techniques Dynamic and Extendible Hashing Techniques RAID TechnologyRAID Technology

Page 3: Disk Storage, Basic File Structures, and Hashing Disk Storage Devices Disk Storage Devices Files of Records Files of Records Operations on Files Operations

Disk Storage DevicesDisk Storage Devices

Preferred secondary storage device for Preferred secondary storage device for high storage capacity and low cost.high storage capacity and low cost.

Data stored as magnetized areas on Data stored as magnetized areas on magnetic disk surfaces.magnetic disk surfaces.

A A diskdisk packpack contains several magnetic contains several magnetic disks connected to a rotating spindle.disks connected to a rotating spindle.

Disks are divided into concentric circular Disks are divided into concentric circular trackstracks on each disk on each disk surfacesurface..Track capacities vary typically from 4 to 50 Track capacities vary typically from 4 to 50

Kbytes or moreKbytes or more

Page 4: Disk Storage, Basic File Structures, and Hashing Disk Storage Devices Disk Storage Devices Files of Records Files of Records Operations on Files Operations

Disk Storage Devices (contd.)Disk Storage Devices (contd.) A track is divided into smaller A track is divided into smaller blocksblocks or or sectorssectors

because it usually contains a large amount of because it usually contains a large amount of information information

The division of a track into The division of a track into sectorssectors is hard-coded is hard-coded on the disk surface and cannot be changed.on the disk surface and cannot be changed. One type of sector organization calls a portion of a track One type of sector organization calls a portion of a track

that subtends a fixed angle at the center as a sector.that subtends a fixed angle at the center as a sector. A track is divided into A track is divided into blocksblocks..

The block size B is fixed for each system.The block size B is fixed for each system. Typical block sizes range from B=512 bytes to B=4096 bytes.Typical block sizes range from B=512 bytes to B=4096 bytes.

Whole blocks are transferred between disk and main Whole blocks are transferred between disk and main memory for processing.memory for processing.

Page 5: Disk Storage, Basic File Structures, and Hashing Disk Storage Devices Disk Storage Devices Files of Records Files of Records Operations on Files Operations

Disk Storage Devices (contd.)Disk Storage Devices (contd.)

Page 6: Disk Storage, Basic File Structures, and Hashing Disk Storage Devices Disk Storage Devices Files of Records Files of Records Operations on Files Operations

Disk Storage Devices (contd.)Disk Storage Devices (contd.) A A read-write headread-write head moves to the track that moves to the track that

contains the block to be transferred.contains the block to be transferred. Disk rotation moves the block under the read-write Disk rotation moves the block under the read-write

head for reading or writing.head for reading or writing. A physical disk block (hardware) address A physical disk block (hardware) address

consists of:consists of: a cylinder number (imaginary collection of tracks of a cylinder number (imaginary collection of tracks of

same radius from all recorded surfaces)same radius from all recorded surfaces) the track number or surface number (within the the track number or surface number (within the

cylinder)cylinder) and block number (within track).and block number (within track).

Reading or writing a disk block is time Reading or writing a disk block is time consuming because of the seek time s and consuming because of the seek time s and rotational delay (latency) rotational delay (latency) rdrd..

Double buffering can be used to speed up the Double buffering can be used to speed up the transfer of contiguous disk blocks.transfer of contiguous disk blocks.

Page 7: Disk Storage, Basic File Structures, and Hashing Disk Storage Devices Disk Storage Devices Files of Records Files of Records Operations on Files Operations

Disk Storage Devices (contd.)Disk Storage Devices (contd.)

Page 8: Disk Storage, Basic File Structures, and Hashing Disk Storage Devices Disk Storage Devices Files of Records Files of Records Operations on Files Operations

RecordsRecords

Fixed and variable length recordsFixed and variable length records Records contain fields which have values of Records contain fields which have values of

a particular typea particular typeE.g., amount, date, time, ageE.g., amount, date, time, age

Fields themselves may be fixed length or Fields themselves may be fixed length or variable lengthvariable length

Variable length fields can be mixed into one Variable length fields can be mixed into one record:record:Separator characters or length fields are Separator characters or length fields are

needed so that the record can be “parsed.” needed so that the record can be “parsed.”

Page 9: Disk Storage, Basic File Structures, and Hashing Disk Storage Devices Disk Storage Devices Files of Records Files of Records Operations on Files Operations

BlockingBlockingBlockingBlocking: :

Refers to storing a number of records in one Refers to storing a number of records in one block on the disk.block on the disk.

Blocking factor (Blocking factor (bfrbfr) refers to the number ) refers to the number of records per block. of records per block.

There may be empty space in a block if an There may be empty space in a block if an integral number of records do not fit in one integral number of records do not fit in one block.block.

Spanned RecordsSpanned Records::Refers to records that exceed the size of one Refers to records that exceed the size of one

or more blocks and hence span a number of or more blocks and hence span a number of blocks.blocks.

Page 10: Disk Storage, Basic File Structures, and Hashing Disk Storage Devices Disk Storage Devices Files of Records Files of Records Operations on Files Operations

Files of RecordsFiles of Records A A filefile is a is a sequencesequence of records, where each of records, where each

record is a collection of data values (or data record is a collection of data values (or data items).items).

A A file descriptorfile descriptor (or (or file headerfile header) includes ) includes information that describes the file, such as the information that describes the file, such as the field namesfield names and their and their data typesdata types, and the , and the addresses of the file blocks on disk.addresses of the file blocks on disk.

Records are stored on disk blocks. Records are stored on disk blocks. The The blocking factorblocking factor bfrbfr for a file is the for a file is the

(average) number of file records stored in a disk (average) number of file records stored in a disk block.block.

A file can have A file can have fixed-lengthfixed-length records or records or variable-lengthvariable-length records. records.

Page 11: Disk Storage, Basic File Structures, and Hashing Disk Storage Devices Disk Storage Devices Files of Records Files of Records Operations on Files Operations

Files of Records (contd.)Files of Records (contd.) File records can be File records can be unspannedunspanned or or spannedspanned

UnspannedUnspanned: no record can span two blocks: no record can span two blocks SpannedSpanned: a record can be stored in more than one block: a record can be stored in more than one block

The physical disk blocks that are allocated to hold the The physical disk blocks that are allocated to hold the records of a file can be records of a file can be contiguous, linked, or indexedcontiguous, linked, or indexed..

In a file of fixed-length records, all records have the In a file of fixed-length records, all records have the same format. Usually, unspanned blocking is used same format. Usually, unspanned blocking is used with such files.with such files.

Files of variable-length records require additional Files of variable-length records require additional information to be stored in each record, such as information to be stored in each record, such as separatorseparator characterscharacters and and field typesfield types.. Usually spanned blocking is used with such files. Usually spanned blocking is used with such files.

Page 12: Disk Storage, Basic File Structures, and Hashing Disk Storage Devices Disk Storage Devices Files of Records Files of Records Operations on Files Operations

Operation on FilesOperation on Files Typical file operations include:Typical file operations include:

OPENOPEN: Readies the file for access, and associates a pointer that will : Readies the file for access, and associates a pointer that will refer to a refer to a currentcurrent file record at each point in time. file record at each point in time.

FINDFIND: Searches for the first file record that satisfies a certain condition, : Searches for the first file record that satisfies a certain condition, and makes it the current file record.and makes it the current file record.

FINDNEXTFINDNEXT: Searches for the next file record (from the current record) : Searches for the next file record (from the current record) that satisfies a certain condition, and makes it the current file record.that satisfies a certain condition, and makes it the current file record.

READREAD: Reads the current file record into a program variable.: Reads the current file record into a program variable. INSERTINSERT: Inserts a new record into the file & makes it the current file : Inserts a new record into the file & makes it the current file

record. record. DELETEDELETE: Removes the current file record from the file, usually by : Removes the current file record from the file, usually by

marking the record to indicate that it is no longer valid.marking the record to indicate that it is no longer valid. MODIFYMODIFY: Changes the values of some fields of the current file record.: Changes the values of some fields of the current file record. CLOSECLOSE: Terminates access to the file.: Terminates access to the file. REORGANIZEREORGANIZE: Reorganizes the file records.: Reorganizes the file records.

For example, the records marked deleted are physically removed from the For example, the records marked deleted are physically removed from the file or a new organization of the file records is created.file or a new organization of the file records is created.

READ_ORDEREDREAD_ORDERED: Read the file blocks in order of a specific field of the : Read the file blocks in order of a specific field of the file. file.

Page 13: Disk Storage, Basic File Structures, and Hashing Disk Storage Devices Disk Storage Devices Files of Records Files of Records Operations on Files Operations

Unordered FilesUnordered Files Also called a Also called a heapheap or a or a pilepile file. file. New records are inserted at the end of the file.New records are inserted at the end of the file. A A linear searchlinear search through the file records is through the file records is

necessary to search for a record.necessary to search for a record.This requires reading and searching half the file This requires reading and searching half the file

blocks on the average, and is hence quite blocks on the average, and is hence quite expensive.expensive.

Record insertion is quite efficient.Record insertion is quite efficient. Reading the records in order of a particular field Reading the records in order of a particular field

requires sorting the file records.requires sorting the file records.

Page 14: Disk Storage, Basic File Structures, and Hashing Disk Storage Devices Disk Storage Devices Files of Records Files of Records Operations on Files Operations

Hashed Files (contd.)Hashed Files (contd.) There are numerous methods for collision resolution, There are numerous methods for collision resolution,

including the following:including the following: Open addressingOpen addressing: Proceeding from the occupied position : Proceeding from the occupied position

specified by the hash address, the program checks the specified by the hash address, the program checks the subsequent positions in order until an unused (empty) position subsequent positions in order until an unused (empty) position is found. is found.

ChainingChaining: For this method, various overflow locations are : For this method, various overflow locations are kept, usually by extending the array with a number of overflow kept, usually by extending the array with a number of overflow positions. In addition, a pointer field is added to each record positions. In addition, a pointer field is added to each record location. A collision is resolved by placing the new record in location. A collision is resolved by placing the new record in an unused overflow location and setting the pointer of the an unused overflow location and setting the pointer of the occupied hash address location to the address of that occupied hash address location to the address of that overflow location. overflow location.

Multiple hashingMultiple hashing: The program applies a second hash : The program applies a second hash function if the first results in a collision. If another collision function if the first results in a collision. If another collision results, the program uses open addressing or applies a third results, the program uses open addressing or applies a third hash function and then uses open addressing if necessary.hash function and then uses open addressing if necessary.

Page 15: Disk Storage, Basic File Structures, and Hashing Disk Storage Devices Disk Storage Devices Files of Records Files of Records Operations on Files Operations

Hashed Files (contd.)Hashed Files (contd.)

Page 16: Disk Storage, Basic File Structures, and Hashing Disk Storage Devices Disk Storage Devices Files of Records Files of Records Operations on Files Operations

Extendible HashingExtendible Hashing

Page 17: Disk Storage, Basic File Structures, and Hashing Disk Storage Devices Disk Storage Devices Files of Records Files of Records Operations on Files Operations

Chapter 14Chapter 14

Types of Single-level Ordered Types of Single-level Ordered IndexesIndexes

Primary IndexesPrimary IndexesClustering IndexesClustering IndexesSecondary IndexesSecondary Indexes

Multilevel IndexesMultilevel Indexes

Page 18: Disk Storage, Basic File Structures, and Hashing Disk Storage Devices Disk Storage Devices Files of Records Files of Records Operations on Files Operations

Indexes as Access PathsIndexes as Access Paths

A single-level index is an auxiliary file that A single-level index is an auxiliary file that makes it more efficient to search for a record makes it more efficient to search for a record in the data file.in the data file.

The index is usually specified on one field of The index is usually specified on one field of the file (although it could be specified on the file (although it could be specified on several fields)several fields)

One form of an index is a file of entries <One form of an index is a file of entries <field field value, pointer to record>value, pointer to record>, which is ordered , which is ordered by field valueby field value

The index is called an access path on the field.The index is called an access path on the field.

Page 19: Disk Storage, Basic File Structures, and Hashing Disk Storage Devices Disk Storage Devices Files of Records Files of Records Operations on Files Operations

Indexes as Access Paths Indexes as Access Paths (contd.)(contd.)

The index file usually occupies considerably less The index file usually occupies considerably less disk blocks than the data file because its entries disk blocks than the data file because its entries are much smallerare much smaller

A binary search on the index yields a pointer to the A binary search on the index yields a pointer to the file recordfile record

Indexes can also be characterized as dense or Indexes can also be characterized as dense or sparse sparse A A dense indexdense index has an index entry for every search key has an index entry for every search key

value (and hence every record) in the data file. value (and hence every record) in the data file. A A sparse (or nondense) indexsparse (or nondense) index, on the other hand, has , on the other hand, has

index entries for only some of the search values index entries for only some of the search values

Page 20: Disk Storage, Basic File Structures, and Hashing Disk Storage Devices Disk Storage Devices Files of Records Files of Records Operations on Files Operations

Indexes as Access Paths Indexes as Access Paths (contd.)(contd.)

Example: Given the following data file EMPLOYEE(NAME, SSN, Example: Given the following data file EMPLOYEE(NAME, SSN, ADDRESS, JOB, SAL, ... )ADDRESS, JOB, SAL, ... )

Suppose that:Suppose that: record size R=150 bytesrecord size R=150 bytes block size B=512 bytesblock size B=512 bytes r=30000 r=30000

recordsrecords Then, we get:Then, we get:

blocking factor Bfr= B div R= 512 div 150= 3 records/blockblocking factor Bfr= B div R= 512 div 150= 3 records/block number of file blocks b= (r/Bfr)= (30000/3)= 10000 blocksnumber of file blocks b= (r/Bfr)= (30000/3)= 10000 blocks

For an index on the SSN field, assume the field size VFor an index on the SSN field, assume the field size VSSNSSN=9 bytes, assume =9 bytes, assume the record pointer size Pthe record pointer size PRR=7 bytes. Then:=7 bytes. Then: index entry size Rindex entry size RII=(V=(VSSNSSN+ P+ PRR)=(9+7)=16 bytes)=(9+7)=16 bytes index blocking factor Bfrindex blocking factor BfrII= B div R= B div RII= 512 div 16= 32 entries/block= 512 div 16= 32 entries/block number of index blocks b= (r/ Bfrnumber of index blocks b= (r/ BfrII)= (30000/32)= 938 blocks)= (30000/32)= 938 blocks binary search needs logbinary search needs log22bI= logbI= log22938= 10 block accesses938= 10 block accesses This is compared to an average linear search cost of:This is compared to an average linear search cost of:

(b/2)= 30000/2= 15000 block accesses(b/2)= 30000/2= 15000 block accesses If the file records are ordered, the binary search cost would be:If the file records are ordered, the binary search cost would be:

loglog22b= logb= log2230000= 15 block accesses30000= 15 block accesses

Page 21: Disk Storage, Basic File Structures, and Hashing Disk Storage Devices Disk Storage Devices Files of Records Files of Records Operations on Files Operations

Types of Single-Level IndexesTypes of Single-Level Indexes

Primary IndexPrimary Index Defined on an ordered data fileDefined on an ordered data file The data file is ordered on a The data file is ordered on a key fieldkey field Includes one index entry Includes one index entry for each blockfor each block in the data in the data

file; the index entry has the key field value for the file; the index entry has the key field value for the first first recordrecord in the block, which is called the in the block, which is called the block anchorblock anchor

A similar scheme can use the A similar scheme can use the last recordlast record in a block. in a block. A primary index is a nondense (sparse) index, since it A primary index is a nondense (sparse) index, since it

includes an entry for each disk block of the data file includes an entry for each disk block of the data file and the keys of its anchor record rather than for every and the keys of its anchor record rather than for every search value.search value.

Page 22: Disk Storage, Basic File Structures, and Hashing Disk Storage Devices Disk Storage Devices Files of Records Files of Records Operations on Files Operations

Primary index on the ordering key Primary index on the ordering key fieldfield

Page 23: Disk Storage, Basic File Structures, and Hashing Disk Storage Devices Disk Storage Devices Files of Records Files of Records Operations on Files Operations

Types of Single-Level IndexesTypes of Single-Level Indexes

Clustering IndexClustering IndexDefined on an ordered data fileDefined on an ordered data fileThe data file is ordered on a The data file is ordered on a non-key fieldnon-key field unlike unlike

primary index, which requires that the ordering field of primary index, which requires that the ordering field of the data file have a distinct value for each recordthe data file have a distinct value for each record..

Includes one index entry Includes one index entry for each distinct valuefor each distinct value of the of the field; the index entry points to the first data block that field; the index entry points to the first data block that

contains records with that field valuecontains records with that field value..It is another example of It is another example of nondensenondense index where index where

Insertion and Deletion is relatively straightforward with Insertion and Deletion is relatively straightforward with a clustering indexa clustering index..

Page 24: Disk Storage, Basic File Structures, and Hashing Disk Storage Devices Disk Storage Devices Files of Records Files of Records Operations on Files Operations

A Clustering Index ExampleA Clustering Index Example

FIGURE 14.2FIGURE 14.2A clustering A clustering index on the index on the

DEPTNUMBEDEPTNUMBER ordering R ordering

non-key field non-key field of an of an

EMPLOYEE EMPLOYEE filefile..

Page 25: Disk Storage, Basic File Structures, and Hashing Disk Storage Devices Disk Storage Devices Files of Records Files of Records Operations on Files Operations

Another Clustering Index ExampleAnother Clustering Index Example

Page 26: Disk Storage, Basic File Structures, and Hashing Disk Storage Devices Disk Storage Devices Files of Records Files of Records Operations on Files Operations

Types of Single-Level IndexesTypes of Single-Level Indexes Secondary IndexSecondary Index

A secondary index provides a secondary means of A secondary index provides a secondary means of accessing a file for which some primary access already accessing a file for which some primary access already exists.exists.

The secondary index may be on a field which is a The secondary index may be on a field which is a candidate key and has a unique value in every record, candidate key and has a unique value in every record, or a non-key with duplicate values.or a non-key with duplicate values.

The index is an ordered file with two fields.The index is an ordered file with two fields. The first field is of the same data type as some The first field is of the same data type as some non-orderingnon-ordering

fieldfield of the data file that is an indexing field. of the data file that is an indexing field. The second field is either a The second field is either a blockblock pointer or a record pointer. pointer or a record pointer. There can be There can be manymany secondary indexes (and hence, indexing secondary indexes (and hence, indexing

fields) for the same file.fields) for the same file. Includes one entry Includes one entry for each recordfor each record in the data file; in the data file;

hence, it is a hence, it is a dense indexdense index

Page 27: Disk Storage, Basic File Structures, and Hashing Disk Storage Devices Disk Storage Devices Files of Records Files of Records Operations on Files Operations

Example of a Dense Secondary Example of a Dense Secondary IndexIndex

Page 28: Disk Storage, Basic File Structures, and Hashing Disk Storage Devices Disk Storage Devices Files of Records Files of Records Operations on Files Operations

An Example of a Secondary An Example of a Secondary IndexIndex