february 1 & 31 files organizing files for performance

36
February 1 & 3 1 Files Organizing Files for Performance

Upload: paxton-wimp

Post on 14-Dec-2015

227 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: February 1 & 31 Files Organizing Files for Performance

February 1 & 3 1

Files

Organizing Files for Performance

Page 2: February 1 & 31 Files Organizing Files for Performance

2

Overview

• In this lecture, we continue to focus on file organization, but with a different motivation.

• This time we look at ways to organize or re-organize files in order to improve performance.

Page 3: February 1 & 31 Files Organizing Files for Performance

3

Outline

• We will be looking at four different issues:

– Data Compression: how to make files smaller

– Reclaiming space in files that have undergone deletions and updates

– Sorting Files in order to support binary searching ==> Internal Sorting

– A better Sorting Method: KeySorting

Page 4: February 1 & 31 Files Organizing Files for Performance

4

Data Compression I: An Overview

• Question: Why do we want to make files smaller?

• Answer: – To use less storage, i.e., saving costs– To transmit these files faster, decreasing access

time or using the same access time, but with a lower and cheaper bandwidth

– To process the file sequentially faster.

Page 5: February 1 & 31 Files Organizing Files for Performance

5

Data Compression II: Suppressing Repeating Sequences ==> Redundancy Compression

• When the data is represented in a Sparse array, we can use a type of compression called: run-length encoding.

• Procedure:– Read through the array in sequence except where the same

value occurs more than once in succession.– When the same value occurs more than once, substitute the

following 3 bytes in order:• The special run-length code indicator• The values that is repeated; and• The number of times that the value is repeated.

– See next slide

Page 6: February 1 & 31 Files Organizing Files for Performance

6

50 51 52 52 52 52 52 53 54 54 54 54 54 54 54 55 52 52 53 53 53 54

The encoded sequence is:50 51 ff 52 05 53 ff 54 07 55 ff 52 02 ff 53 03 54

• No guarantee that space will be saved!!!

Page 7: February 1 & 31 Files Organizing Files for Performance

7

Data Compression III: Assigning Variable-Length Code

• Principle: Assign short codes to the most frequent occurring values and long ones to the least frequent ones.

• The code-size cannot be fully optimized as one wants codes to occur in succession, without delimiters between them, and still be recognized.

• This is the principle used in the Morse Code• As well, it is used in Huffman Coding. ==> Used

for compression in Unix (see next slide).

Page 8: February 1 & 31 Files Organizing Files for Performance

8

• Variable length encoding– Letters with high frequency are encoded using

shorter symbols.– Letters with low frequency are encoded using

longer symbols.– Huffman code (for a set of seven letters):

• four bits per letter (minimum 3 bits).The string “abefd” is encoded as “1010000100100000”.

– Huffman codes are used in some UNIX systems for data compression

Page 9: February 1 & 31 Files Organizing Files for Performance

9

Data Compression IV: Irreversible Compression Techniques

• Irreversible Compression is based on the assumption that some information can be sacrificed. [Irreversible compression is also called Entropy Reduction].

• Example: Shrinking a raster image from 400-by-400 pixels to 100-by-100 pixels. The new image contains 1 pixel for every 16 pixels in the original image.

• There is usually no way to determine what the original pixels were from the one new pixel.

• In data files, irreversible compression is seldom used. However, it is used in image and speech processing.

Page 10: February 1 & 31 Files Organizing Files for Performance

10

Data Compression V: Compression in Unix I: Huffman Coding (pack and unpack)

• Suppose messages are made of letters a, b, c, d, and e, which appear with probabilities .12, .4, .15, .08, and .25, respectively.

• We wish to encode each character into a sequence of 0’s and 1’s so that no code for a character is the prefix for another.

• Answer (using Huffman’s algorithm given on the next slide): a=1111, b=0, c=110, d=1110, e=10.

Page 11: February 1 & 31 Files Organizing Files for Performance

11

Constructing Huffman Codes (A FOREST is a collection of TREES; each TREE has a root

and a weight)

While there is more than one TREE in the FOREST {• i= index of the TREE in FOREST with smallest weight;• j= index of the TREE in FOREST with 2nd smallest weight;• Create a new node with left child FOREST(i)--> root and

right child FOREST(j)--> root• Replace TREE i in FOREST by a tree whose root is the new

node and whose weight is FOREST(i)--> weight + FOREST(j)--> weight

• Delete TREE j from FOREST }

Page 12: February 1 & 31 Files Organizing Files for Performance

12

Reclaiming Space in Files I: Record Deletion and Storage Compaction

• Recognizing Deleted Records

• Reusing the space from the record ==> Storage Compaction.

• Storage Compaction: After deleted records have accumulated for some time, a special program is used to reconstruct the file with all the deleted approaches.

• Storage Compaction can be used with both fixed- and variable-length records.

Page 13: February 1 & 31 Files Organizing Files for Performance

13

Record Deletion (cont’d)

• Space reclamation:

– A simple solution is to copy the file by skipping the deleted records.

• Suitable for both fixed-length and variable-length records.

• After space reclamation

Ames|John|123 Maple|Stillwater|OK|74075|...

Brown|Martha|625 Kimbark|Des Moines|IA|50311|...– In place (not copying a file) space reclamation is more

complicated and time consuming.

Page 14: February 1 & 31 Files Organizing Files for Performance

14

• An naive approach:When inserting a new record,– searching the file record by record;– if a deleted record is found, insert the new record in the

place of the deleted record;– otherwise, insert the new record at the end of the file.

• Issues on reclaiming space quickly:– How to know immediately if there are empty slots in the

file?– How to jump to one of those slots, if they exist?

• Linking all deleted records together using a linked list:

pointerdeletedrecord

Headpointer

pointerdeletedrecord

deletedrecord

pointer-1

...

Page 15: February 1 & 31 Files Organizing Files for Performance

15

– Use the link list of the deleted records as a stack:

– Add (push) a recently deleted record of RRN 3 to the top of the stack:

– Remove a free space of RRN from the top of the stack for an inserted record:

2RRN

5Head

pointerRRN

2 -1

2RRN

5Head

pointerRRN

2 -15RRN

3

2RRN

5Head

pointerRRN

2 -1

Page 16: February 1 & 31 Files Organizing Files for Performance

16

– Use the link list of the deleted records as a stack:

– Add (push) a recently deleted record of RRN 3 to the top of the stack:

– Insert three new records to the space of the deleted records:

List head (first available record): 30 1 2 3 4 5 6

Edwards... Bates... *-1 *5 Masters... *2 Chavez...

List head (first available record): -10 1 2 3 4 5 6

Edwards... Bates... 3rd new rec. 1st new rec. Masters... 2nd new rec. Chavez...

List head (first available record): 50 1 2 3 4 5 6

Edwards... Bates... *-1 Wills... Masters... *2 Chavez...

Page 17: February 1 & 31 Files Organizing Files for Performance

• An available list to store the deleted variable-length records:

– How to link the deleted records together into a list?

– How to add newly deleted records to the available list?

– How to find and remove records from the available list when space is reclaimed?

• An available list of variable-length records

HEAD.FIRST_AVAILABLE: -1

40 Ames|John|123 Maple|Stillwater|OK|74075|64 Morrison|Sebastian|9035 South Hillcrest|Forest Village|OK|78420|45 Brown|Martha|625 Kimbark|Des Moines|IA|50311|

• Delete the second record:

HEAD.FIRST_AVAILABLE: 43

40 Ames|John|123 Maple|Stillwater|OK|74075|64 *|-1.............................................................................................|45 Brown|Martha|625 Kimbark|Des Moines|IA|50311|

Page 18: February 1 & 31 Files Organizing Files for Performance

18

• When inserting a new record, we need to search the available list for a deleted record with large enough record length:–The current available list:

–Insert a record of 55 bytes:

Size72

Size68 -1

Size38

Size47

Size68 -1New Link

Size38

Size47

Size72removed record:

Page 19: February 1 & 31 Files Organizing Files for Performance

19

Reclaiming Space in Files II: Deleting Fixed-Length Records for Reclaiming Space

Dynamically

• RECAPPING:• In some applications, it is necessary to reclaim space

immediately. • To do so, we can:

– Mark deleted records in some special ways– Find the space that deleted records once occupied so that we can

reuse that space when we add records.– Come up with a way to know immediately if there are empty

slots in the file and jump directly to them.• Solution: Use an avail linked list in the form of a stack.

Relative Record Numbers (RRNs) play the role of pointers.

Page 20: February 1 & 31 Files Organizing Files for Performance

20

Reclaiming Space in Files III: Deleting Variable-Length Records for Reclaiming

Space Dynamically

• Same ideas as for Fixed-Length Records, but a different implementation must be used.

• In particular, we must keep a byte count of each record and the links to the next records on the avail list cannot be the RRNs.

• As well, the data structure used for the avail list cannot be a stack since we have to make sure that when re-using a record it is of the right size.

Page 21: February 1 & 31 Files Organizing Files for Performance

21

Reclaiming Space in Files IV: Storage Fragmentation

• Wasted Space within a record is called internal Fragmentation.

• Variable-Length records do not suffer from internal fragmentation. However, external fragmentation is not avoided.

• 3 ways to deal with external fragmentation: – Storage Compaction– Coalescing the holes– Use a clever placement strategy

Page 22: February 1 & 31 Files Organizing Files for Performance

22

• First-fit placement strategy: NON-sorted - search the first available space which is large enough for the inserted record.–Least amount of work when we place a newly available

space on the list.• Best-fit placement strategy: (Sorted) search the smallest

available which is large enough for the inserted record.–Order the available list in ascending order by size, then use

the first-fit placement strategy.–After inserting the new record, the free area left over may be

too small to be useful. May cause serious external fragmentation.

–The small free slots are placed at the beginning of the available list. Make the search of the first-fit space increasingly long as time goes on.

Page 23: February 1 & 31 Files Organizing Files for Performance

23

• Worst-fit placement strategy: – (Sorted – descending) Order the available list in

descending order by size, then use first-fit placement strategy.• Always insert the new record to the first slot.

If the first slot is not large enough. The new record is inserted to the end of the file.

• Decrease the chance of external fragmentation.

Page 24: February 1 & 31 Files Organizing Files for Performance

24

Reclaiming Space in Files V: Placement Strategies II

• Some general remarks about placement strategies:– Placement strategies only apply to variable-length records– If space is lost due to internal fragmentation, the choice is

first fit and best fit. A worst fit strategy truly makes internal fragmentation worse.

– If the space is lost due to external fragmentation, one should give careful consideration to a worst-fit strategy.

Page 25: February 1 & 31 Files Organizing Files for Performance

25

Finding Things Quickly I: Overview I

• The cost of Seeking is very high.• This cost has to be taken into consideration when

determining a strategy for searching a file for a particular piece of information.

• The same question also arises with respect to sorting, which often is the first step to searching efficiently.

• Rather than simply trying to sort and search, we concentrate on doing so in a way that minimizes the number of seeks.

Page 26: February 1 & 31 Files Organizing Files for Performance

26

Finding things Quickly II: Overview II

• So far, the only way we have to retrieve or find records quickly is by using their RRN (in case the record is of fixed-length).

• Without a RRN or in the case of variable-length records, the only way, so far, to look for a record is by doing a sequential search. This is a very inefficient method.

• We are interested in more efficient ways to retrieve records based on their key-value.

Page 27: February 1 & 31 Files Organizing Files for Performance

27

Finding things Quickly III: Binary Search

• The binary search: Let’s assume that the file is sorted and that we are looking for record whose key is Kelly in a file of 1000 fixed-length records.

1 2 …. 500 1000

1: Johnson

750

2: Monroe

Next Comparison

Page 28: February 1 & 31 Files Organizing Files for Performance

28

Finding things Quickly IV: Binary Search versus Sequential Search

• When sequential search is used, doubling the number of records in the file doubles the number of comparisons required for sequential search.

• When binary search is used, doubling the number of records in the file only adds one more guess to our worst case.

• In order to use binary search, though, the file first has to be sorted. This can be very expensive.

Page 29: February 1 & 31 Files Organizing Files for Performance

29

Finding things Quickly V: Sorting a Disk File in Memory

• If the entire content of a file can be held in memory, then we can perform an internal sort. Sorting in memory is very efficient.

• However, if the file does not hold entirely in memory, any sorting algorithm will require a large number of seeks. Sorting would, thus, be extremely slow. Unfortunately, this is often the case, and solutions have to be found.

Page 30: February 1 & 31 Files Organizing Files for Performance

30

Finding things Quickly VI: The limitations of Binary Search and Internal Sorting

• Binary Search requires more than one or two accesses. Accessing a record using the RRN can be done with a single access ==> We would like to achieve RRN retrieval performance while keeping the advantage of key access.

• Keeping a file sorted is very expensive: in addition to searching for the right location for the insert, once this location is founds, we have to shift records to open up the space for insertion.

Page 31: February 1 & 31 Files Organizing Files for Performance

31

Finding things Quickly VII: KeySorting

• Overview: when sorting a file in memory, the only thing that really needs sorting are record keys.

• Internal Sorting only works on small files. ==> Keysorting

Page 32: February 1 & 31 Files Organizing Files for Performance

• Only load records keys into RAM.• A KEYNODES[ ] array has two fields: KEY and RRN. There is a

correspondence between KEYNODES[ ] and records in the actual file.

• Actual sorting process, simply sort the KEYNODES[ ] array according to the key field.

KEYNODES array RecordsKEY RRNHARRISON SUSAN 1 Harrison | Susan | 387 Eastern ...KELLOG BILL 2 Kellog | Bill | 17 Maple ...HARRIS MARGARET 3 Harris | Margaret | 4343 West ...... ... ...BELL ROBERT k Bell | Robert | 8912 Hill ...

KEYNODES array RecordsKEY RRNBELL ROBERT k Harrison | Susan | 387 Eastern ...HARRIS MARGARET 3 Kellog | Bill | 17 Maple ...HARRISON SUSAN 1 Harris | Margaret | 4343 West ...... ... ...KELLOG BILL 2 Bell | Robert | 8912 Hill ...

Page 33: February 1 & 31 Files Organizing Files for Performance

33

• Keysort algorithms work like internal sort, but with 2 important differences:– Rather than read an entire record into a memory

array, we simply read each record into a temporary buffer, extract the key and then discard.

– If we want to write the records in sorted order, we have to read them a second time.

Page 34: February 1 & 31 Files Organizing Files for Performance

34

Finding things Quickly VIII: Limitation of the KeySort Method

• Writing the records in sorted order requires as many random seeks as there are records.

• Since writing is interspersed with reading, writing also requires as many seeks as there are records.

• Solution: Why bother to write the file of records in key order: simply write back the sorted index.

Page 35: February 1 & 31 Files Organizing Files for Performance

35

KEYNODES array RecordsKEY RRNBELL ROBERT k Harrison | Susan | 387 Eastern ...HARRIS MARGARET 3 Kellog | Bill | 17 Maple ...HARRISON SUSAN 1 Harris | Margaret | 4343 West ...... ... ...KELLOG BILL 2 Bell | Robert | 8912 Hill ...

Page 36: February 1 & 31 Files Organizing Files for Performance

36