next up previous
Next: Appending a record Up: Module irpr_kappa(irpr_kappa.kl1) Previous: Searching for records

Reading records

The procedure of reading records is as follows.

As a programing technique, how to deal with large amount of data using the reading control of primitive command read_record/5 is important. For the details, see the part of read_record_loop/5 shown below.

read_record1(normal, TableName, ReadSeq, ValidF, VLS,NVLS,Status)-IFP:-
    % Argument "ReadSeq" is the reading order. Instantiate it by  
    %  "set(Set, _)" to read the entities of the "Set".
    IFP <= create_format(TableName, (*), IFormat, Status1),
    % Designating the whole table by "(*)", gets the internal format
    % "IFormat"
    IFP <= get_table_info(TableName, [schema(Schema0)], Status2),
    % Gets the schema information
    Schema0 = Schema,
    lib:error_check([Status1,Status2],irpr_kappa:read_record1,Status3),
    read_record2(Status3,TableName,ReadSeq,IFormat,ValidF,Schema,VLS,NVLS,
        Status)-IFP.
otherwise.
read_record1(PreStatus, _TableName, _ReadSeq, _ValidF, VLS,NVLS,Status)-IFP :-
    NVLS = VLS, Status = PreStatus.

read_record2(normal,TableName,ReadSeq,IFormat,ValidF,Schema,VLS,NVLS,Status)
    -IFP :-
    IFP <= read_record(TableName, IFormat, ReadSeq, RRVLS, Status),
    ValidFL = [valid_flag(ValidF)],
    read_record_loop(VLS, Schema, ValidFL, RRVLS, NVLS).
otherwise.
read_record2(PreStatus,_TableName,_ReadSeq,_IFormat,_ValidF,_Schema,VLS,NVLS,
    Status)-IFP :-
    NVLS = VLS, Status = PreStatus.

%%%%
% read_record_loop/5
read_record_loop([VLB|VLS], Schema, ValidFL, RRVLS, NVLS) :-
    RRVLS = [RRVLB|RRVLS1],
    % The fourth argument of primitive command read_record/5 is 
    % instantiated here.
    read_record_loop1(RRVLB, VLB, VLS, Schema, ValidFL, RRVLS1, NVLS).
read_record_loop([], _Schema, _ValidFL, RRVLS, NVLS) :-
    RRVLS = [], NVLS = [].

After the first argument of read_record_loop/5 is instantiated as a list cell, "RRVLS" is instantiated. The instantiation of the first argument as a "[VLB VLS]" is done at the receiver side of command status "ComStatus" mentioned in the previous section. The concerning predicates are irpr_service:output_vls/5 and output_vls_loop/8, where after all the contents of the car(VLB) have been output, the cdr(VLS) is instantiated as a list cell.

This is how records are read on demand. The reason is to deal with large amount of data. If "RRVLS" is instantiated in advance, all the entities of concerning records are read at the same time, thereby main memory shortage can happen in case of enormous data.

%%%%
read_record_loop1(RRVLB, VLB, VLS, Schema, ValidFL,RRVLS,NVLS) :- list(RRVLB) |
    unnest_record_vlb(RRVLB, UNVLB, Schema, ValidFL)+UNVLB+VLB,
    % "VLB" will be unified with "UNVLB" which is totally unnested
    % "RRVLB".
    read_record_loop(VLS, Schema, ValidFL, RRVLS, NVLS).
read_record_loop1(end, VLB, VLS, _Schema, _ValidFL, RRVLS, NVLS) :-
    NVLS = [VLB|VLS], RRVLS = [].

%%%%
% The result of read_record/5 is unnested one record by one.
unnest_record_vlb([{_,VL}|RRVLB], UNVLB, Schema, ValidFL)-VLB :-
    unnest_record(Schema,VL)+UnVL+ValidFL,
    % "VL" is unnested to "UnVL", at the end of which "ValidFL" is 
    % appended.
    UNVLB = [UnVL|UNVLB1],
    unnest_record_vlb(RRVLB, UNVLB1, Schema, ValidFL)-VLB.
unnest_record_vlb([], UNVLB, _Schema, _ValidFL)-VLB :-
    UNVLB = [].