/****************************************************************************
  module      : vkb33
  author      : JuergenA
  responsible : UweH
  special area: KB_index_handling
  see also    :
  description :
*****************************************************************************/
 
.tt 1 $SAP$LiveCache$VKB33$
.tt 3 $UweH$KB_index_handling$2000-07-21$
 
.nf
 
 .nf
 
    ========== licence begin  GPL
    Copyright (c) 2000-2005 SAP AG
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
    ========== licence end
.fo
 
 
.fo
 
Module  : KB_index_handling
 
Define  :
 
        PROCEDURE
              k33build_new_index_id (
                    VAR table_treeid : tgg00_FileId;
                    indexNo          : tsp00_Int2;
                    VAR inv_id       : tgg00_FileId);
 
        PROCEDURE
              k33create_index (VAR m : tgg00_MessBlock);
 
        PROCEDURE
              k33diagnose_index (VAR m : tgg00_MessBlock);
 
.CM *-END-* define --------------------------------------
.sp;.cp 3
Use     :
 
        FROM
              Select_Help_Procedures : VGG04;
 
        PROCEDURE
              g04index_tree_build (
                    VAR file_id    : tgg00_FileId (*ptocConst*);
                    VAR index_tree : tgg00_FileId;
                    index_no       : tsp00_Int2);
 
      ------------------------------ 
 
        FROM
              KB_transaction : VKB53;
 
        PROCEDURE
              k53eot_excl_lock (
                    VAR t             : tgg00_TransContext;
                    VAR TreeId        : tgg00_FileId;
                    VAR k             : tgg00_Lkey;
                    forInsert         : boolean;
                    WantedMode        : tgg00_LockReqMode;
                    VAR GrantedMode   : tgg00_LockReqMode);
 
        PROCEDURE
              k53lock (VAR t         : tgg00_TransContext;
                    VAR lock_tabid   : tgg00_Surrogate;
                    VAR k            : tgg00_Lkey;
                    wanted_mode      : tgg00_LockReqMode;
                    wanted_state     : tgg00_LockReqState;
                    nowait           : boolean;
                    collision_test   : boolean;
                    VAR GrantedMode  : tgg00_LockReqMode);
 
      ------------------------------ 
 
        FROM
              KB_Logging : VKB560;
 
        PROCEDURE
              kb560StartSavepoint (VAR Trans : tgg00_TransContext;
                    MessType2 : tgg00_MessType2);
 
      ------------------------------ 
 
        FROM
              KB_CreateDropFile_interface : VKB641;
 
        PROCEDURE
              kb641CreateIndexFile (
                    VAR trans       : tgg00_TransContext;
                    VAR tableFileId : tgg00_FileId;
                    VAR indexFileId : tgg00_FileId;
                    indexId         : tsp00_Int2);
 
        PROCEDURE
              kb641CreateIndex (
                    VAR TransContext : tgg00_TransContext;
                    VAR PrimFileId   : tgg00_FileId;
                    VAR InvFileId    : tgg00_FileId;
                    VAR StackDesc    : tgg00_StackDesc;
                    VAR IndexCounter : tgg00_IndexCounter);
 
      ------------------------------ 
 
        FROM
              filesysteminterface_1 : VBD01;
 
        VAR
              b01niltree_id : tgg00_FileId;
 
        PROCEDURE
              b01destroy_file (VAR t : tgg00_TransContext;
                    VAR file_id      : tgg00_FileId);
 
        PROCEDURE
              b01filestate (VAR t : tgg00_TransContext;
                    VAR file_id   : tgg00_FileId);
 
        PROCEDURE
              b01tcreate_file (VAR t : tgg00_TransContext;
                    VAR act_tree_id  : tgg00_FileId);
 
      ------------------------------ 
 
        FROM
              filesysteminterface_3 : VBD03;
 
        PROCEDURE
              bd03DiagnoseIndex (
                    VAR t               : tgg00_TransContext;
                    VAR file_id         : tgg00_FileId;
                    VAR inv_id          : tgg00_FileId;
                    VAR temp_tree_id    : tgg00_FileId;
                    VAR b               : tgg00_Rec;
                    VAR prim_duplicates : tsp00_Int4;
                    VAR prim_missing    : tsp00_Int4);
 
        PROCEDURE
              bd03BuildIndex (
                    VAR trans         : tgg00_TransContext;
                    indexmode         : tgg00_IndexMode;
                    VAR file_id       : tgg00_FileId;
                    VAR inv_id        : tgg00_FileId;
                    VAR stack_desc    : tgg00_StackDesc;
                    indexNo           : tsp00_Int2;
                    VAR index_counter : tgg00_IndexCounter);
 
      ------------------------------ 
 
        FROM
              FileDir_Wrapper : vbd998;
 
        PROCEDURE
              bd998NewFileNo (
                    VAR file_no : tgg00_Surrogate);
 
      ------------------------------ 
 
        FROM
              RTE-Extension-20 : VSP20;
 
        PROCEDURE
              s20int4_to_buf (val    : tsp00_Int4;
                    VAR dest : tsp00_Buf;
                    di       : tsp00_Int4);
 
        FUNCTION
              s20buf_to_int4 (VAR str : tsp00_Buf; si : tsp00_Int4) : tsp00_Int4;
 
      ------------------------------ 
 
        FROM
              PUT-Conversions : VSP41;
 
        PROCEDURE
              s41p4int (VAR buf : tsp00_Buf;
                    pos         : tsp00_Int4;
                    source      : tsp00_Int4;
                    VAR res     : tsp00_NumError);
 
.CM *-END-* use -----------------------------------------
.sp;.cp 3
Synonym :
 
        FUNCTION
              s20int4_to_buf;
 
              tsp00_MoveObj tsp00_Buf
 
        FUNCTION
              s20buf_to_int4;
 
              tsp00_MoveObj tsp00_Buf
 
        PROCEDURE
              s41p4int;
 
              tsp00_MoveObj tsp00_Buf
 
.CM *-END-* synonym -------------------------------------
.sp;.cp 3
Author  : JuergenA
.sp
.cp 3
Created : 1979-12-14
.sp
.cp 3
.sp
.cp 3
Release :      Date : 2000-11-28
.sp
***********************************************************
.sp
.cp 9
.fo
.oc _/1
Specification:
.sp
Procedure K33CREATE_INDEX
.sp
Depending on the message types the following inv file handling is done:
.sp
M_CREATE_INDEX/MM_NIL
.br
A new inv file is created and filled. To avoid the sys exclusive lock
and to allow the read access of the primary table by other transactions
the table is locked in share mode and a virtual index key is locked
in row exclusive mode.
.sp
M_CREATE_INDEX/MM_PARSE
.br
The table is locked in sys exclusive mode and
a new inv file is created via K64CREATE_TREE.
.sp
M_DIAGNOSE/MM_INDEX
.br
The table is locked in share and the already exiting index
is checked.
.sp 2;.cp 4
Procedure KB33CHECK_INDEX
.sp
Checks the contents of the existing inv file and the result
(inv count, duplicate primary entries, missing primary entries)
to MB_PART1.
.sp 2;.cp 5
Procedure KB33NEW_INDEX
.sp
A log entry with the tableid and the inv stack entries is created and
forced to the log. Afterwards, the inv file is created and filled
via BD03CREATEINDEX. The numbers of inserted entries and the
number of undefined entries (not inserted) are assigned to MB_PART1.
.nf;.sp;.cp 4
create_index_logentry
+-------------------------------------+
| LG_CREATE_INDEX | inv stack entries |
+-------------------------------------+
.fo
.CM *-END-* specification -------------------------------
.sp 2
***********************************************************
.sp
.cp 7
.fo
.oc _/1
Description:
.sp
.CM *-END-* description ---------------------------------
.sp 2
***********************************************************
.sp
.cp 10
.nf
.oc _/1
Structure:
 
.CM *-END-* structure -----------------------------------
.sp 2
**********************************************************
.sp
.cp 35
.nf
.oc _/1
.CM -lll-
Code    :
 
 
CONST
      c_collision_test = true;
      c_nowait         = true;
 
 
(*------------------------------*) 
 
PROCEDURE
      k33create_index (VAR m : tgg00_MessBlock);
 
VAR
      indexNo        : tsp00_Int2;
      dummy_granted  : tgg00_LockReqMode;
      wanted_lock    : tgg00_LockReqMode;
      inv_id         : tgg00_FileId;
      index_lock_key : tgg00_Lkey;
      tableId        : tgg00_FileId;
 
BEGIN
m.mb_trns^.trError_gg00 := e_ok;
indexNo := ord(m.mb_qual^.mst_addr^[m.mb_qual^.mmult_pos].ecol_tab [1]);
tableId := m.mb_qual^.mtree;
k33build_new_index_id (tableId, indexNo, inv_id);
IF  m.mb_type = m_create_index
THEN
    BEGIN
    IF  m.mb_type2 = mm_parse
    THEN
        wanted_lock := lckSysExcl_egg00
    ELSE
        BEGIN
        index_lock_key.keyVal_gg00 [1] := chr (INDEX_LOCK_BYTE_KB00);
        index_lock_key.keyLen_gg00     := 1;
        wanted_lock                    := lckRowExcl_egg00
        END;
    (*ENDIF*) 
    k53eot_excl_lock (m.mb_trns^, tableId, index_lock_key, true, wanted_lock,dummy_granted)
    END;
(*ENDIF*) 
IF  (m.mb_trns^.trError_gg00 = e_ok) AND
    (m.mb_type2 <> mm_parse  )
THEN
    k53lock (m.mb_trns^, tableId.fileTabId_gg00, index_lock_key,
          lckTabShare_egg00, [ ], NOT c_nowait, NOT c_collision_test, dummy_granted);
(*ENDIF*) 
IF  (m.mb_trns^.trError_gg00 = e_ok)
THEN
    BEGIN
    b01filestate (m.mb_trns^, m.mb_qual^.mtree);
    IF  m.mb_trns^.trError_gg00 = e_file_read_only
    THEN
        m.mb_trns^.trError_gg00 := e_ok
    (*ENDIF*) 
    END;
(*ENDIF*) 
IF  m.mb_trns^.trError_gg00 = e_ok
THEN
    BEGIN
    IF  (m.mb_type  = m_create_index) AND
        (m.mb_type2 = mm_nil        )
    THEN
        kb33new_index (m, inv_id)
    ELSE
        IF  (m.mb_type  = m_create_index) AND
            (m.mb_type2 = mm_parse)
        THEN
            kb641CreateIndexFile (m.mb_trns^, m.mb_qual^.mtree, inv_id, indexNo)
        ELSE
            m.mb_trns^.trError_gg00 := e_not_implemented
        (*ENDIF*) 
    (*ENDIF*) 
    END
(*ENDIF*) 
END;
 
(*------------------------------*) 
 
PROCEDURE
      k33diagnose_index (VAR m : tgg00_MessBlock);
 
VAR
      dummy_granted  : tgg00_LockReqMode;
      inv_id         : tgg00_FileId;
      index_lock_key : tgg00_Lkey;
 
BEGIN
m.mb_trns^.trError_gg00 := e_ok;
g04index_tree_build (m.mb_qual^.mtree,
      inv_id,
      ord(m.mb_qual^.mst_addr^[m.mb_qual^.mmult_pos].ecol_tab [1]));
IF  (m.mb_trns^.trError_gg00 = e_ok) AND
    (m.mb_type2 <> mm_parse  )
THEN
    k53lock (m.mb_trns^, inv_id.fileTabId_gg00, index_lock_key,
          lckTabShare_egg00, [ ], NOT c_nowait, NOT c_collision_test, dummy_granted);
(*ENDIF*) 
IF  (m.mb_trns^.trError_gg00 = e_ok)
THEN
    BEGIN
    b01filestate (m.mb_trns^, m.mb_qual^.mtree);
    IF  m.mb_trns^.trError_gg00 = e_file_read_only
    THEN
        m.mb_trns^.trError_gg00 := e_ok
    (*ENDIF*) 
    END;
(*ENDIF*) 
IF  m.mb_trns^.trError_gg00 = e_ok
THEN
    IF  (m.mb_type  = m_diagnose) AND
        (m.mb_type2 = mm_index  )
    THEN
        kb33check_index (m, inv_id)
    ELSE
        m.mb_trns^.trError_gg00 := e_not_implemented
    (*ENDIF*) 
(*ENDIF*) 
END;
 
(*------------------------------*) 
 
PROCEDURE
      k33build_new_index_id (
            VAR table_treeid : tgg00_FileId;
            indexNo          : tsp00_Int2;
            VAR inv_id       : tgg00_FileId);
 
BEGIN
inv_id                    := table_treeid;
inv_id.fileLeafNodes_gg00 := cgg_nil_leafnodes;
inv_id.fileRoot_gg00      := NIL_PAGE_NO_GG00;
inv_id.fileRootCheck_gg00 := ROOT_CHECK_GG00; (* PTS 1105022 TS 2000-01-03 *)
inv_id.fileBdUse_gg00     := [ ];
inv_id.fileVersion_gg00.ci2_gg00 := cgg_dummy_file_version;
inv_id.fileType_gg00      := inv_id.fileType_gg00 - [ftsDynamic_egg00];
inv_id.fileTfn_gg00       := tfnMulti_egg00;
inv_id.fileTfnNo_gg00 [1] := chr (indexNo);
(* *)
bd998NewFileNo (inv_id.fileTabId_gg00);
(* *)
END;
 
(*------------------------------*) 
 
PROCEDURE
      kb33check_index (VAR m : tgg00_MessBlock;
            VAR inv_id       : tgg00_FileId);
 
VAR
      aux_error       : tgg00_BasisError;
      dummy_num_err   : tsp00_NumError;
      pos             : integer;
      indexNo         : tsp00_Int2;
      prim_duplicates : tsp00_Int4;
      prim_missing    : tsp00_Int4;
      temp_id         : tgg00_FileId;
      index_counter   : tgg00_IndexCounter;
 
BEGIN
inv_id.fileBdUse_gg00 := [ ];
m.mb_qual^.mst_addr   := m.mb_st;
m.mb_qual^.mst_max    := m.mb_st_max;
indexNo := ord(m.mb_qual^.mst_addr^[m.mb_qual^.mmult_pos].ecol_tab [1]);
WITH index_counter DO
    BEGIN
    idc_prim_keycount  := 0;
    idc_nullcount      := 0;
    idc_sec_keycount   := 0;
    idc_prim_leafnodes := 1;
    idc_sec_leafnodes  := 1
    END;
(*ENDWITH*) 
bd03BuildIndex (m.mb_trns^, test_index, m.mb_qual^.mtree,
      inv_id, m.mb_qual^.mstack_desc, indexNo, index_counter);
IF  m.mb_trns^.trError_gg00 = e_ok
THEN
    BEGIN
    temp_id                    := b01niltree_id;
    temp_id.fileType_gg00      := [ ftsTemp_egg00 ];
    temp_id.fileTfn_gg00       := tfnTemp_egg00;
    temp_id.fileTfnNo_gg00 [1] := chr(1);
    temp_id.fileSession_gg00   := m.mb_trns^.trSessionId_gg00;
    b01tcreate_file (m.mb_trns^, temp_id)
    END;
(*ENDIF*) 
IF  m.mb_trns^.trError_gg00 = e_ok
THEN
    BEGIN
    bd03DiagnoseIndex (m.mb_trns^, m.mb_qual^.mtree, inv_id,
          temp_id, m.mb_data^.mbp_rec, prim_duplicates, prim_missing);
    IF  m.mb_trns^.trError_gg00 = e_ok
    THEN
        BEGIN
        m.mb_type     := m_return_result;
        m.mb_type2    := mm_nil;
        m.mb_struct   := mbs_buf;
        m.mb_qual_len := 3 * RESNUM_MXSP00;
        m.mb_data_len := 0;
        pos           := 1;
        m.mb_qual^.buf [pos] := csp_defined_byte;
        s41p4int (m.mb_qual^.buf, pos+1,
              index_counter.idc_prim_keycount, dummy_num_err);
        pos := pos + RESNUM_MXSP00;
        m.mb_qual^.buf [pos] := csp_defined_byte;
        s41p4int (m.mb_qual^.buf, pos+1, prim_duplicates,
              dummy_num_err);
        pos := pos + RESNUM_MXSP00;
        m.mb_qual^.buf [pos] := csp_defined_byte;
        s41p4int (m.mb_qual^.buf, pos+1, prim_missing,
              dummy_num_err)
        END;
    (*ENDIF*) 
    aux_error               := m.mb_trns^.trError_gg00;
    m.mb_trns^.trError_gg00 := e_ok;
    b01destroy_file (m.mb_trns^, temp_id);
    IF  m.mb_trns^.trError_gg00 = e_ok
    THEN
        m.mb_trns^.trError_gg00 := aux_error
    (*ENDIF*) 
    END
(*ENDIF*) 
END;
 
(*------------------------------*) 
 
PROCEDURE
      kb33new_index (VAR m : tgg00_MessBlock;
            VAR inv_id     : tgg00_FileId);
 
VAR
      index_counter : tgg00_IndexCounter;
 
BEGIN
m.mb_trns^.trError_gg00 := e_ok;
inv_id.fileBdUse_gg00   := [ ];
m.mb_qual^.mst_addr     := m.mb_st;
m.mb_qual^.mst_max      := m.mb_st_max;
IF  m.mb_trns^.trError_gg00 = e_ok
THEN
    kb641CreateIndex (m.mb_trns^, m.mb_qual^.mtree,
          inv_id, m.mb_qual^.mstack_desc, index_counter);
(*ENDIF*) 
IF  m.mb_trns^.trError_gg00 = e_ok
THEN
    BEGIN
    WITH m.mb_data^.mbp_sample, index_counter DO
        BEGIN
        prim_key_cnt   := idc_prim_keycount;
        null_value_cnt := idc_nullcount;
        sec_key_cnt    := idc_sec_keycount;
        leafnodes      := idc_prim_leafnodes;
        indexnodes     := idc_sec_leafnodes
        END;
    (*ENDWITH*) 
    m.mb_type      := m_return_result;
    m.mb_type2     := mm_nil;
    m.mb_struct    := mbs_buf;
    m.mb_qual_len  := 0;
    m.mb_data_len  := sizeof (m.mb_data^.mbp_sample);
    IF  m.mb_data^.mbp_sample.prim_key_cnt > 0
    THEN
        kb560StartSavepoint (m.mb_trns^, mm_index);
    (*ENDIF*) 
    END
(*ENDIF*) 
END;
 
.CM *-END-* code ----------------------------------------
.SP 2 
***********************************************************
.PA 
