CS计算机代考程序代写 scheme data structure cache 2021/4/28 Tuple Representation

2021/4/28 Tuple Representation
Tuple Representation
Tuples
Records vs Tuples Converting Records to Tuples Operations on Records Operations on Tuples Fixed-length Records Variable-length Records Data Types
Field Descriptors
>>
COMP9315 21T1 ♢ Tuple Representation ♢ [0/13]
cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/tuples/slides.html
1/15

2021/4/28 Tuple Representation
❖ Tuples
Each page contains a collection of tuples
What do tuples contain? How are they structured internally?
COMP9315 21T1 ♢ Tuple Representation ♢ [1/13]
∧ >>
cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/tuples/slides.html
2/15

2021/4/28 Tuple Representation
❖ Records vs Tuples
A table is dened by a schema, e.g.
create table Employee (
id integer primary key,
name varchar(20) not null,
job varchar(10),
dept smallint references Dept(id)
);
where a schema is a collection of attributes (name,type,constraints)
Reminder: schema information (meta-data) is also stored, in the DB catalog
COMP9315 21T1 ♢ Tuple Representation ♢ [2/13]
<< ∧ >>
cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/tuples/slides.html
3/15

2021/4/28 Tuple Representation
<< ∧ >>
❖ Records vs Tuples (cont)
Tuple = collection of attribute values based on a schema, e.g.
Record = sequence of bytes, containing data for one tuple, e.g.
Bytes need to be interpreted relative to schema to get tuple
COMP9315 21T1 ♢ Tuple Representation ♢ [3/13]
cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/tuples/slides.html
4/15

2021/4/28 Tuple Representation
❖ Converting Records to Tuples A Record is an array of bytes (byte[])
representing the data values from a typed Tuple stored on disk (persistent) or in a memory buffer
A Tuple is a collection of named,typed values (cf. C struct)
to manipulate the values, need an “interpretable” structure stored in working memory, and temporary
<< ∧ >>
COMP9315 21T1 ♢ Tuple Representation ♢ [4/13]
cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/tuples/slides.html
5/15

2021/4/28 Tuple Representation
<< ∧ >>
❖ Converting Records to Tuples (cont) Information on how to interpret bytes in a record …
may be contained in schema data in DBMS catalog
may be stored in the page directory
may be stored in the record (in a record header)
may be stored partly in the record and partly in the schema
For variable-length records, some formatting info … must be stored in the record or in the page directory
at the least, need to know how many bytes in each varlen value
COMP9315 21T1 ♢ Tuple Representation ♢ [5/13]
cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/tuples/slides.html
6/15

2021/4/28 Tuple Representation
<< ∧ >>
❖ Operations on Records
Common operation on records … access record via RecordId:
Record get_record(Relation rel, RecordId rid) {
(pid,tid) = rid;
Page buf = get_page(rel, pid);
return get_bytes(rel, buf, tid);
}
Cannot use a Record directly; need a Tuple:
Relation rel = … // relation schema Record rec = get_record(rel, rid) Tuple t = mkTuple(rel, rec)
Once we have a Tuple, we can access individual attributes/elds COMP9315 21T1 ♢ Tuple Representation ♢ [6/13]
cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/tuples/slides.html
7/15

2021/4/28 Tuple Representation
❖ Operations on Tuples
Once we have a record, we need to interpret it as a tuple …
Tuple t = mkTuple(rel, rec)
convert record to tuple data structure for relation rel
Once we have a tuple, we want to examines its contents …
Typ getTypField(Tuple t, int i)
extract the i’th eld from a Tuple as a value of type Typ E.g. int x = getIntField(t,1), char *s = getStrField(t,2)
COMP9315 21T1 ♢ Tuple Representation ♢ [7/13]
<< ∧ >>
cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/tuples/slides.html
8/15

2021/4/28 Tuple Representation
<< ∧ >>
❖ Fixed-length Records
A possible encoding scheme for xed-length records:
record format (length + offsets) stored in catalog data values stored in xed-size slots in data pages
Since record format is frequently used at query time, cache in memory.
COMP9315 21T1 ♢ Tuple Representation ♢ [8/13]
cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/tuples/slides.html
9/15

2021/4/28 Tuple Representation
<< ∧ >>
❖ Variable-length Records
Possible encoding schemes for variable-length records:
Prex each eld by length
Terminate elds by delimiter
Array of offsets
COMP9315 21T1 ♢ Tuple Representation ♢ [9/13]
cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/tuples/slides.html
10/15

2021/4/28 Tuple Representation
❖ Data Types
DBMSs typically dene a xed set of base types, e.g. DATE, FLOAT, INTEGER, NUMBER(n), VARCHAR(n), …
This determines implementation-level data types for eld values:
<< ∧ >>
DATE
FLOAT INTEGER NUMBER(n) VARCHAR(n)
time_t float,double int,long int[] (?) char[]
PostgreSQL allows new base types to be added
COMP9315 21T1 ♢ Tuple Representation ♢ [10/13]
cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/tuples/slides.html
11/15

2021/4/28 Tuple Representation
❖ Field Descriptors
A Tuple could be implemented as
a list of eld descriptors for a record instance (where a FieldDesc gives (offset,length,type) information)
along with a reference to the Record data
typedef struct {
ushort nfields; // number of fields/attrs ushort data_off; // offset in struct for data FieldDesc fields[]; // field descriptions
Record data; // pointer to record in buffer
} Tuple;
Fields are derived from relation descriptor + record instance data.
COMP9315 21T1 ♢ Tuple Representation ♢ [11/13]
<< ∧ >>
cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/tuples/slides.html
12/15

2021/4/28 Tuple Representation
❖ Field Descriptors (cont) Tuple data could be
a pointer to bytes stored elsewhere in memory
<< ∧ >>
COMP9315 21T1 ♢ Tuple Representation ♢ [12/13]
cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/tuples/slides.html
13/15

2021/4/28 Tuple Representation
❖ Field Descriptors (cont) Or, tuple data could be …
appendedtoTuple struct (usedwidelyinPostgreSQL)
<< ∧ COMP9315 21T1 ♢ Tuple Representation ♢ [13/13] cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/tuples/slides.html 14/15 2021/4/28 Tuple Representation Produced: 27 Feb 2021 cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/tuples/slides.html 15/15