| 1 | /*------------------------------------------------------------------------- |
|---|
| 2 | ciifuh.c |
|---|
| 3 | SVD 10-03-93 |
|---|
| 4 | Rotinas para atualizar o dicionario (btree) |
|---|
| 5 | |
|---|
| 6 | -------------------------------------------------------------------------*/ |
|---|
| 7 | |
|---|
| 8 | /*------------------------------------------------------------------------- |
|---|
| 9 | Alteracao: SVD 26-11-94 |
|---|
| 10 | Problema: Quando havia necessidade de se fazer um split de uma folha |
|---|
| 11 | ou um no', o algoritmo que constroi uma folha(no') temporaria |
|---|
| 12 | com ORD+1 elementos estava incorreto. Se o elemento a ser |
|---|
| 13 | inserido fosse menor que o elemento[0] da folha(no), inseria |
|---|
| 14 | lixo. |
|---|
| 15 | ---------------------------------------------------------------------------*/ |
|---|
| 16 | |
|---|
| 17 | #define GERA_KEYS (0 || DEBIFUPD) |
|---|
| 18 | #define ONLY_INSERTION 0 |
|---|
| 19 | #if TRACEUPIF |
|---|
| 20 | #if !CICPP |
|---|
| 21 | static UCHR tkey[LE2+1]; /* AOT 11/09/2000 */ |
|---|
| 22 | #endif /* CICPP */ |
|---|
| 23 | #endif |
|---|
| 24 | |
|---|
| 25 | #define TRCBTREE 1 |
|---|
| 26 | |
|---|
| 27 | #if !CICPP |
|---|
| 28 | static TREE_PATH lfpath; |
|---|
| 29 | static PUNT puntlf ; |
|---|
| 30 | #endif /* CICPP */ |
|---|
| 31 | |
|---|
| 32 | /*-------------------------------------------------------------------------- |
|---|
| 33 | upif_find_key_node |
|---|
| 34 | .Retorna em 'pos' a posicao em que deve ser inserida a chave 'key' com |
|---|
| 35 | tamanho 'keysize' em um vetor '*p' com 'ock' elementos compostos |
|---|
| 36 | cada um com tamanho 'itemsize'. |
|---|
| 37 | .Se a chave existe a funcao retorna 'TRUE' e em 'pos' a posicao que esta |
|---|
| 38 | .Caso contrario a funcao retorna 'FALSE' e em 'pos' a posicao onde |
|---|
| 39 | devera ser inserido. |
|---|
| 40 | .Supoe-se que o vetor esta classificado em ordem crescente da chave. |
|---|
| 41 | --------------------------------------------------------------------------*/ |
|---|
| 42 | |
|---|
| 43 | #if CICPP |
|---|
| 44 | BOOLEAN CIIFU :: upif_find_key_node(N0STRU *np, |
|---|
| 45 | UCHR *key, |
|---|
| 46 | int treecase, |
|---|
| 47 | int *pos) |
|---|
| 48 | #else /*CICPP*/ |
|---|
| 49 | BOOLEAN upif_find_key_node(np,key,treecase,pos) |
|---|
| 50 | N0STRU *np; |
|---|
| 51 | UCHR *key; |
|---|
| 52 | int treecase; |
|---|
| 53 | int *pos; |
|---|
| 54 | #endif /*CICPP*/ |
|---|
| 55 | { |
|---|
| 56 | BOOLEAN existe; |
|---|
| 57 | N1STRU *n1p; |
|---|
| 58 | N2STRU *n2p; |
|---|
| 59 | int n,cmp,keysize,ock; |
|---|
| 60 | |
|---|
| 61 | n1p=(N1STRU *)np; |
|---|
| 62 | n2p=(N2STRU *)np; |
|---|
| 63 | ock=np->ock; |
|---|
| 64 | keysize=vlex[treecase]; |
|---|
| 65 | existe=FALSE; |
|---|
| 66 | if (treecase == 0) { |
|---|
| 67 | for (n=ock-1; n>=0; n--) { |
|---|
| 68 | *pos=n; |
|---|
| 69 | cmp= strncmp((CONST char *)key,(CONST char *)n1p->idx[n].key,keysize); |
|---|
| 70 | if (cmp==0) { |
|---|
| 71 | existe=TRUE; |
|---|
| 72 | break; |
|---|
| 73 | } |
|---|
| 74 | else |
|---|
| 75 | { if (cmp > 0) { |
|---|
| 76 | existe=FALSE; /* Not found */ |
|---|
| 77 | *pos= *pos+1; |
|---|
| 78 | break; |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | } /*for*/ |
|---|
| 82 | }/*treecase*/ |
|---|
| 83 | else { |
|---|
| 84 | for (n=ock-1; n>=0; n--) { |
|---|
| 85 | *pos=n; |
|---|
| 86 | cmp= strncmp((CONST char *)key,(CONST char *)n2p->idx[n].key,keysize); |
|---|
| 87 | if (cmp==0) { |
|---|
| 88 | existe=TRUE; |
|---|
| 89 | break; |
|---|
| 90 | } |
|---|
| 91 | else |
|---|
| 92 | { if (cmp > 0) { |
|---|
| 93 | existe=FALSE; /* Not found */ |
|---|
| 94 | *pos= *pos+1; |
|---|
| 95 | break; |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | } /*for*/ |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | return(existe); |
|---|
| 102 | } |
|---|
| 103 | /*-------------------------------------------------------------------------- |
|---|
| 104 | upif_find_key_leaf |
|---|
| 105 | .Retorna em 'pos' a posicao em que deve ser inserida a chave 'key' com |
|---|
| 106 | tamanho 'keysize' em um vetor '*p' com 'ock' elementos compostos |
|---|
| 107 | cada um com tamanho 'itemsize'. |
|---|
| 108 | .Se a chave existe a funcao retorna 'TRUE' e em 'pos' a posicao que esta |
|---|
| 109 | .Caso contrario a funcao retorna 'FALSE' e em 'pos' a posicao onde |
|---|
| 110 | devera ser inserido. |
|---|
| 111 | .Supoe-se que o vetor esta classificado em ordem crescente da chave. |
|---|
| 112 | --------------------------------------------------------------------------*/ |
|---|
| 113 | |
|---|
| 114 | #if CICPP |
|---|
| 115 | BOOLEAN CIIFU :: upif_find_key_leaf(L0STRU *lp, |
|---|
| 116 | UCHR *key, |
|---|
| 117 | int treecase, |
|---|
| 118 | int *pos) |
|---|
| 119 | #else /*CICPP*/ |
|---|
| 120 | BOOLEAN upif_find_key_leaf(lp,key,treecase,pos) |
|---|
| 121 | L0STRU *lp; |
|---|
| 122 | UCHR *key; |
|---|
| 123 | int treecase; |
|---|
| 124 | int *pos; |
|---|
| 125 | #endif /*CICPP*/ |
|---|
| 126 | { |
|---|
| 127 | BOOLEAN existe; |
|---|
| 128 | L1STRU *l1p; |
|---|
| 129 | L2STRU *l2p; |
|---|
| 130 | int n,cmp,keysize,ock; |
|---|
| 131 | |
|---|
| 132 | l1p=(L1STRU *)lp; |
|---|
| 133 | l2p=(L2STRU *)lp; |
|---|
| 134 | ock=lp->ock; |
|---|
| 135 | keysize=vlex[treecase]; |
|---|
| 136 | existe=FALSE; |
|---|
| 137 | if (treecase == 0) { |
|---|
| 138 | for (n=ock-1; n>=0; n--) { |
|---|
| 139 | *pos=n; |
|---|
| 140 | cmp= strncmp((CONST char *)key,(CONST char *)l1p->idx[n].key,keysize); |
|---|
| 141 | if (cmp==0) { |
|---|
| 142 | existe=TRUE; |
|---|
| 143 | break; |
|---|
| 144 | } |
|---|
| 145 | else |
|---|
| 146 | { if (cmp > 0) { |
|---|
| 147 | existe=FALSE; /* Not found */ |
|---|
| 148 | *pos= *pos+1; |
|---|
| 149 | break; |
|---|
| 150 | } |
|---|
| 151 | } |
|---|
| 152 | } /*for*/ |
|---|
| 153 | }/*treecase*/ |
|---|
| 154 | else { |
|---|
| 155 | for (n=ock-1; n>=0; n--) { |
|---|
| 156 | *pos=n; |
|---|
| 157 | cmp= strncmp((CONST char *)key,(CONST char *)l2p->idx[n].key,keysize); |
|---|
| 158 | if (cmp==0) { |
|---|
| 159 | existe=TRUE; |
|---|
| 160 | break; |
|---|
| 161 | } |
|---|
| 162 | else |
|---|
| 163 | { if (cmp > 0) { |
|---|
| 164 | existe=FALSE; /* Not found */ |
|---|
| 165 | *pos= *pos+1; |
|---|
| 166 | break; |
|---|
| 167 | } |
|---|
| 168 | } |
|---|
| 169 | } /*for*/ |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | return(existe); |
|---|
| 173 | } |
|---|
| 174 | /* ----------------------------------------------------------------------- |
|---|
| 175 | .Criar uma nova raiz |
|---|
| 176 | .Se first=true cria a raiz com apenas um elemento igual a branco |
|---|
| 177 | (Isto corresponde a inicializar o arquivo invertido). |
|---|
| 178 | .Se first=false cria uma nova raiz que contem oprimeiro elemento branco |
|---|
| 179 | e o segundo elemento a chave promovida de elementos de um nivel mais baixo |
|---|
| 180 | -------------------------------------------------------------------------*/ |
|---|
| 181 | |
|---|
| 182 | #if CICPP |
|---|
| 183 | PUNT CIIFU :: upif_create_root(DBXSTRU *dbxp, |
|---|
| 184 | PUNT esq, |
|---|
| 185 | UCHR *key, |
|---|
| 186 | PUNT dir, |
|---|
| 187 | int treecase, |
|---|
| 188 | BOOLEAN first) |
|---|
| 189 | #else /*CICPP*/ |
|---|
| 190 | PUNT upif_create_root(dbxp,esq,key,dir,treecase,first) |
|---|
| 191 | DBXSTRU *dbxp; |
|---|
| 192 | PUNT esq; |
|---|
| 193 | UCHR *key; |
|---|
| 194 | PUNT dir; |
|---|
| 195 | int treecase; |
|---|
| 196 | BOOLEAN first; |
|---|
| 197 | #endif /*CICPP*/ |
|---|
| 198 | { |
|---|
| 199 | N1STRU *n1p,nw_node1; |
|---|
| 200 | N2STRU *n2p,nw_node2; |
|---|
| 201 | N0STRU *n0p; |
|---|
| 202 | |
|---|
| 203 | N1IDXE item_no1; |
|---|
| 204 | N2IDXE item_no2; |
|---|
| 205 | |
|---|
| 206 | INVMAP *invp; |
|---|
| 207 | |
|---|
| 208 | UWORD level; |
|---|
| 209 | BOOLEAN isroot; |
|---|
| 210 | UCHR tmp[LE2+1]; |
|---|
| 211 | PUNT nodenumber; |
|---|
| 212 | int |
|---|
| 213 | keysize,i,ind; |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | invp=DBXifmap; |
|---|
| 218 | keysize=vlex[treecase]; |
|---|
| 219 | /*Branqueia para pior caso */ |
|---|
| 220 | upif_branqueia((UCHR *)tmp,LE2); |
|---|
| 221 | nodenumber=invp->cn[treecase].nmaxpos+1; |
|---|
| 222 | if (treecase==0) { |
|---|
| 223 | n0p=(N0STRU *)&nw_node1; |
|---|
| 224 | n1p=(N1STRU *)&nw_node1; |
|---|
| 225 | memcpy(item_no1.key,tmp,keysize); |
|---|
| 226 | item_no1.punt=esq; |
|---|
| 227 | } |
|---|
| 228 | else { |
|---|
| 229 | n0p=(N0STRU *)&nw_node2; |
|---|
| 230 | n2p=(N2STRU *)&nw_node2; |
|---|
| 231 | memcpy(item_no2.key,tmp,keysize); |
|---|
| 232 | item_no2.punt=esq; |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | level=invp->cn[treecase].liv+1; |
|---|
| 236 | n0p->it =treecase+1; |
|---|
| 237 | n0p->pos = nodenumber; |
|---|
| 238 | n0p->ock =2; |
|---|
| 239 | if (first==TRUE)n0p->ock=1; |
|---|
| 240 | |
|---|
| 241 | ind=0; |
|---|
| 242 | if (treecase == 0) { |
|---|
| 243 | n1p->idx[ind]=item_no1; |
|---|
| 244 | } |
|---|
| 245 | else { |
|---|
| 246 | n2p->idx[ind]=item_no2; |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | if (first==FALSE) { |
|---|
| 250 | ind++; |
|---|
| 251 | if (treecase == 0) { |
|---|
| 252 | memcpy(n1p->idx[ind].key,key,keysize); |
|---|
| 253 | n1p->idx[ind].punt=dir; |
|---|
| 254 | } |
|---|
| 255 | else { |
|---|
| 256 | memcpy(n2p->idx[ind].key,key,keysize); |
|---|
| 257 | n2p->idx[ind].punt=dir; |
|---|
| 258 | } |
|---|
| 259 | } |
|---|
| 260 | isroot=TRUE; |
|---|
| 261 | upif_branqueia(tmp,keysize); |
|---|
| 262 | if (treecase == 0) { |
|---|
| 263 | memcpy(item_no1.key,tmp,keysize); |
|---|
| 264 | item_no1.punt=(INFO )0; |
|---|
| 265 | } |
|---|
| 266 | else { |
|---|
| 267 | memcpy(item_no2.key,tmp,keysize); |
|---|
| 268 | item_no2.punt=(INFO )0; |
|---|
| 269 | } |
|---|
| 270 | for (i=ind+1;i<=TWORDN-1;i++){ |
|---|
| 271 | if (treecase == 0) |
|---|
| 272 | n1p->idx[i]=item_no1; |
|---|
| 273 | else |
|---|
| 274 | n2p->idx[i]=item_no2; |
|---|
| 275 | } |
|---|
| 276 | #if TRACEUPIF |
|---|
| 277 | upif_print_node(n0p); |
|---|
| 278 | #endif |
|---|
| 279 | |
|---|
| 280 | nodewrit(dbxp,(N0STRU *)n0p,level,isroot); |
|---|
| 281 | /* retorna a nova raiz */ |
|---|
| 282 | return (nodenumber); |
|---|
| 283 | } |
|---|
| 284 | /*------------------------------------------------------------------------- |
|---|
| 285 | upif_insert_index: |
|---|
| 286 | .Insere a chave "b_key" na pagina de indice apontada por "b_punt". |
|---|
| 287 | (Chave e apontador que vieram de "b_otton". |
|---|
| 288 | .Se a pagina esta cheia, cria uma nova pagina do indice, divide os |
|---|
| 289 | elementos entre as duas paginas. Na divisao a pagina velha fica com ORDN |
|---|
| 290 | itens e nova com ORDN+1. O primeiro item da nova pagina e' escolhido |
|---|
| 291 | para ser inserido em um nivel superior. Esta operacao e' chamada de |
|---|
| 292 | "promover" uma chave. |
|---|
| 293 | .Se houver promocao a funcao retorna TRUE e a chave em "p_key" e seu |
|---|
| 294 | apontador em "p_punt". O registro CNT deve ser atualizado e regravado. |
|---|
| 295 | As paginas apontadas por "punt" e "b_punt" tambem tem que ser atualizadas |
|---|
| 296 | e regravadas. |
|---|
| 297 | .Caso contrario a funcao retorna FALSE; neste caso "p_key" e "p_punt" |
|---|
| 298 | ficam indefinidos. Somente a pagina apontada por "punt" deve ser |
|---|
| 299 | atualizada e regravada. |
|---|
| 300 | /*-------------------------------------------------------------------------*/ |
|---|
| 301 | |
|---|
| 302 | #if CICPP |
|---|
| 303 | BOOLEAN CIIFU :: upif_insert_index (DBXSTRU *dbxp, |
|---|
| 304 | PUNT punt, |
|---|
| 305 | int treecase, |
|---|
| 306 | int level, |
|---|
| 307 | int isroot, |
|---|
| 308 | UCHR *b_key, |
|---|
| 309 | PUNT b_punt, |
|---|
| 310 | UCHR *p_key, |
|---|
| 311 | PUNT *p_punt) |
|---|
| 312 | #else /*CICPP*/ |
|---|
| 313 | BOOLEAN upif_insert_index (dbxp,punt,treecase,level,isroot, |
|---|
| 314 | b_key,b_punt,p_key,p_punt) |
|---|
| 315 | DBXSTRU *dbxp; |
|---|
| 316 | PUNT punt; |
|---|
| 317 | int treecase; |
|---|
| 318 | int level; |
|---|
| 319 | int isroot; |
|---|
| 320 | UCHR *b_key; |
|---|
| 321 | PUNT b_punt; |
|---|
| 322 | UCHR *p_key; |
|---|
| 323 | PUNT *p_punt; |
|---|
| 324 | #endif /*CICPP*/ |
|---|
| 325 | { |
|---|
| 326 | INVMAP *invp; |
|---|
| 327 | UCHR *np; |
|---|
| 328 | static N2STRU node; /* pior caso*/ |
|---|
| 329 | UWORD ock; |
|---|
| 330 | UCHR tmp[LE2+1]; |
|---|
| 331 | BOOLEAN found; |
|---|
| 332 | PUNT nodenumber; |
|---|
| 333 | int pos, keysize,i,cmp; |
|---|
| 334 | int ileft,ifrom,iright,inew; |
|---|
| 335 | N0STRU *n0p,*n0pnw; |
|---|
| 336 | static N1STRU *n1p, n1nw, *n1pnw; |
|---|
| 337 | static N2STRU *n2p, n2nw, *n2pnw; |
|---|
| 338 | static N1STRUW n1w, *n1wp; |
|---|
| 339 | static N2STRUW n2w, *n2wp; |
|---|
| 340 | N1IDXE no1; |
|---|
| 341 | N2IDXE no2; |
|---|
| 342 | #if TRACEUPIF |
|---|
| 343 | N0STRU *n0wp; |
|---|
| 344 | #endif |
|---|
| 345 | |
|---|
| 346 | invp=DBXifmap; |
|---|
| 347 | keysize=vlex[treecase]; |
|---|
| 348 | /* Inicializa um no do ttipo 1 ou tipo 2 */ |
|---|
| 349 | if (treecase == 0){ |
|---|
| 350 | memcpy(no1.key,b_key,keysize); |
|---|
| 351 | no1.punt=b_punt; |
|---|
| 352 | } |
|---|
| 353 | else { |
|---|
| 354 | memcpy(no2.key,b_key,keysize); |
|---|
| 355 | no2.punt=b_punt; |
|---|
| 356 | } |
|---|
| 357 | |
|---|
| 358 | np=(UCHR *)noderead(invp,treecase,level,punt); |
|---|
| 359 | memcpy((UCHR *)&node,np,sizeof(N2STRU)); /* pega maior caso*/ |
|---|
| 360 | np=(UCHR *)&node; |
|---|
| 361 | n0p=(N0STRU *)np; |
|---|
| 362 | n1p=(N1STRU *)np; |
|---|
| 363 | n2p=(N2STRU *)np; |
|---|
| 364 | n1wp= &n1w; |
|---|
| 365 | n2wp= &n2w; |
|---|
| 366 | ock = n0p->ock; |
|---|
| 367 | |
|---|
| 368 | found=upif_find_key_node(n0p,b_key,treecase,&pos); |
|---|
| 369 | if (found) /* Erro de concepcao!!! */ |
|---|
| 370 | fatal("ciifuh/upif_insert_key/EC/tp=0"); |
|---|
| 371 | |
|---|
| 372 | |
|---|
| 373 | if (ock < TWORDN) { /* Cabe na pagina */ |
|---|
| 374 | if (treecase == 0) { |
|---|
| 375 | for (i=ock;i>pos;i--){ /* Abre espaco para novo item */ |
|---|
| 376 | n1p->idx[i] = n1p->idx[i-1]; |
|---|
| 377 | } |
|---|
| 378 | n1p->idx[pos] = no1; /* Insere o novo no */ |
|---|
| 379 | }else { |
|---|
| 380 | for (i=ock;i>pos;i--){ /* Abre espaco para novo item */ |
|---|
| 381 | n2p->idx[i] = n2p->idx[i-1]; |
|---|
| 382 | } |
|---|
| 383 | n2p->idx[pos] = no2; |
|---|
| 384 | } |
|---|
| 385 | n0p->ock++; /* Atualiza ocorrencias */ |
|---|
| 386 | #if TRACEUPIF |
|---|
| 387 | printf("\n<insert_index> Apos insercao "); |
|---|
| 388 | upif_print_node(n0p); |
|---|
| 389 | #endif |
|---|
| 390 | nodewrit(dbxp,n0p,level,isroot); |
|---|
| 391 | return (FALSE); /* Nao precisou fazer split */ |
|---|
| 392 | } |
|---|
| 393 | |
|---|
| 394 | /* Nao cabe na pagina. Precisa fazer Split |
|---|
| 395 | E criada uma pagina (wk) com TWORDN+1 elementos, inserindo elemento na |
|---|
| 396 | posicao correta. A copia e feita do fim para o comeco. |
|---|
| 397 | Se key < Original entao copia original, muda origem e destino |
|---|
| 398 | Se Key > Original entao copia key e muda somente destino e transforma |
|---|
| 399 | key em '\0'; |
|---|
| 400 | */ |
|---|
| 401 | /* Proxima posicao livre de idx */ |
|---|
| 402 | nodenumber= invp->cn[treecase].nmaxpos+1; |
|---|
| 403 | memcpy((UCHR *)tmp,b_key,keysize); |
|---|
| 404 | tmp[keysize]='\0'; |
|---|
| 405 | /* Copia o no para area de trabalho para inserir mais um elemento. |
|---|
| 406 | Processa do Fim para Comeco |
|---|
| 407 | */ |
|---|
| 408 | ifrom=ock-1; |
|---|
| 409 | if (treecase == 0) { |
|---|
| 410 | #if TRACEUPIF |
|---|
| 411 | n0wp = (N0STRU *)n1wp; |
|---|
| 412 | #endif |
|---|
| 413 | /* SVD 26-11-94 for (i=ock;i>=0;i--) { */ |
|---|
| 414 | i=ock; /* SVD 26-11-94 */ |
|---|
| 415 | for (;ifrom>=0;) { |
|---|
| 416 | cmp = strncmp((CONST char *)n1p->idx[ifrom].key,(CONST char *)tmp,keysize); |
|---|
| 417 | if ( cmp>0 ) { |
|---|
| 418 | n1wp->idx[i]=n1p->idx[ifrom]; |
|---|
| 419 | ifrom--; |
|---|
| 420 | i--; /* SVD 26-11-94 */ |
|---|
| 421 | }else |
|---|
| 422 | if ( cmp<0 ) { |
|---|
| 423 | n1wp->idx[i]=no1; |
|---|
| 424 | i--; /* SVD 26-11-94 */ |
|---|
| 425 | tmp[0]='\0'; /* forca para copiar resto do vetor */ |
|---|
| 426 | }else |
|---|
| 427 | if ( cmp==0 ) { |
|---|
| 428 | /* Lembrar que o primeiro elemento da arvore contem uma |
|---|
| 429 | chave "branco". Neste caso nao pode ser considerada |
|---|
| 430 | como erro. |
|---|
| 431 | */ |
|---|
| 432 | if (i>0 ) /*Termo duplicado- erro de concepcao */ |
|---|
| 433 | fatal("ciifuh/upif_insert_index/EC/tp=1"); |
|---|
| 434 | n1wp->idx[i]=n1p->idx[ifrom]; |
|---|
| 435 | i--; /* SVD 26-11-94 */ |
|---|
| 436 | } |
|---|
| 437 | }/*for*/ |
|---|
| 438 | /* SVD 26-11-94 |
|---|
| 439 | Verifica se a insercao ainda nao foi feita,ou seja , deve ser |
|---|
| 440 | inserida na primeira posicao |
|---|
| 441 | */ |
|---|
| 442 | if (tmp[0]!='\0'){ |
|---|
| 443 | n1wp->idx[0]=no1; |
|---|
| 444 | tmp[0]='\0'; |
|---|
| 445 | } |
|---|
| 446 | }/*treecase */ |
|---|
| 447 | else { |
|---|
| 448 | #if TRACEUPIF |
|---|
| 449 | n0wp = (N0STRU *)n2wp; |
|---|
| 450 | #endif |
|---|
| 451 | /* SVD 26-11-94 for (i=ock;i>=0;i--) { */ |
|---|
| 452 | i=ock; /* SVD 26-11-94 */ |
|---|
| 453 | for (;ifrom>=0;) { |
|---|
| 454 | cmp = strncmp((CONST char *)n2p->idx[ifrom].key,(CONST char *)tmp,keysize); |
|---|
| 455 | if ( cmp>0 ) { |
|---|
| 456 | n2wp->idx[i]=n2p->idx[ifrom]; |
|---|
| 457 | ifrom--; |
|---|
| 458 | i--; /* SVD 26-11-94 */ |
|---|
| 459 | }else |
|---|
| 460 | if ( cmp<0 ) { |
|---|
| 461 | n2wp->idx[i]=no2; |
|---|
| 462 | i--; /* SVD 26-11-94 */ |
|---|
| 463 | tmp[0]='\0'; /* forca para copiar todo vetor */ |
|---|
| 464 | }else |
|---|
| 465 | if ( cmp==0 ) { |
|---|
| 466 | /* Lembrar que o primeiro elemento da arvore contem uma |
|---|
| 467 | chave "branco". Neste caso nao pode ser considerada |
|---|
| 468 | como erro. |
|---|
| 469 | */ |
|---|
| 470 | if (i>0 )/*Termo duplicado- erro de concepcao */ |
|---|
| 471 | fatal("ciifuh/Upif_insert_index/EC/tp=2"); |
|---|
| 472 | n2wp->idx[i]=n2p->idx[ifrom]; |
|---|
| 473 | i--; /* SVD 26-11-94 */ |
|---|
| 474 | } |
|---|
| 475 | }/*for*/ |
|---|
| 476 | /* SVD 26-11-94 |
|---|
| 477 | Verifica se a insercao ainda nao foi feita,ou seja , deve ser |
|---|
| 478 | inserida na primeira posicao |
|---|
| 479 | */ |
|---|
| 480 | if (tmp[0]!='\0'){ |
|---|
| 481 | n2wp->idx[0]=no2; |
|---|
| 482 | tmp[0]='\0'; |
|---|
| 483 | } |
|---|
| 484 | } |
|---|
| 485 | |
|---|
| 486 | ock++; |
|---|
| 487 | #if TRACEUPIF |
|---|
| 488 | n0wp->ock=ock; |
|---|
| 489 | n0wp->it=treecase+1; |
|---|
| 490 | n0wp->pos=nodenumber; /* numero de elementos em wk */ |
|---|
| 491 | printf("\n<insert_node> Apos insercao em wk"); |
|---|
| 492 | upif_print_node((N0STRU *)n0wp); |
|---|
| 493 | #endif |
|---|
| 494 | |
|---|
| 495 | /* Metade esquerda */ |
|---|
| 496 | /* A primeira metade do vetor de trabalho volta para o no' original |
|---|
| 497 | A segunda metade + o novo item inserido vai para o no' novo |
|---|
| 498 | Lembrar que os indices comecam de zero ! |
|---|
| 499 | */ |
|---|
| 500 | n1pnw= &n1nw; |
|---|
| 501 | n2pnw= &n2nw; |
|---|
| 502 | |
|---|
| 503 | iright = ORDN; /* Metade + 1 */ |
|---|
| 504 | inew = 0; |
|---|
| 505 | for (ileft=0; ileft < ORDN; ileft++) { |
|---|
| 506 | if (treecase == 0 ) { |
|---|
| 507 | n1p->idx[ileft] = n1wp->idx[ileft]; |
|---|
| 508 | n1pnw->idx[inew] =n1wp->idx[iright]; |
|---|
| 509 | }else { |
|---|
| 510 | n2p->idx[ileft] = n2wp->idx[ileft]; |
|---|
| 511 | n2pnw->idx[inew] =n2wp->idx[iright]; |
|---|
| 512 | } |
|---|
| 513 | iright++; |
|---|
| 514 | inew++; |
|---|
| 515 | } |
|---|
| 516 | /* Copia o ultimo elemento */ |
|---|
| 517 | if (treecase == 0) { |
|---|
| 518 | n0p = (N0STRU *)n1p; |
|---|
| 519 | n0pnw =(N0STRU *)n1pnw; |
|---|
| 520 | n1pnw->idx[inew] = n1wp->idx[iright]; |
|---|
| 521 | }else { |
|---|
| 522 | n0p = (N0STRU *)n2p; |
|---|
| 523 | n0pnw =(N0STRU *)n2pnw; |
|---|
| 524 | n2pnw->idx[inew] = n2wp->idx[iright]; |
|---|
| 525 | } |
|---|
| 526 | inew++; |
|---|
| 527 | iright++; |
|---|
| 528 | n0p->ock=ORDN; |
|---|
| 529 | |
|---|
| 530 | /* Completa informacoes da nova pagina */ |
|---|
| 531 | n0pnw->it=n0p->it; /* Mesmo tipo que a original */ |
|---|
| 532 | n0pnw->pos=nodenumber; /* Proxima folha */ |
|---|
| 533 | n0pnw->ock=ORDN+1; /* O item a ser promovido fica |
|---|
| 534 | armazenado na 1. posicao da nova arvore |
|---|
| 535 | */ |
|---|
| 536 | upif_branqueia((UCHR *)tmp,LE2); |
|---|
| 537 | if (treecase == 0) { |
|---|
| 538 | memcpy(no1.key,tmp,keysize); |
|---|
| 539 | no1.punt=(INFO )0; |
|---|
| 540 | }else { |
|---|
| 541 | memcpy(no2.key,tmp,keysize); |
|---|
| 542 | no2.punt=(INFO )0; |
|---|
| 543 | } |
|---|
| 544 | |
|---|
| 545 | /* Branqueia itens disponiveis da pagina velha */ |
|---|
| 546 | for (i=ORDN+1-1;i<=TWORDN-1;i++){ |
|---|
| 547 | if (treecase == 0 ) { |
|---|
| 548 | n1p->idx[i]=no1; |
|---|
| 549 | }else { |
|---|
| 550 | n2p->idx[i]=no2; |
|---|
| 551 | } |
|---|
| 552 | } |
|---|
| 553 | for (i=ORDN+2-1; i<=TWORDN-1;i++){ /* Indice comeca de zero */ |
|---|
| 554 | if (treecase == 0) { |
|---|
| 555 | n1pnw->idx[i]=no1; |
|---|
| 556 | }else { |
|---|
| 557 | n2pnw->idx[i]=no2; |
|---|
| 558 | } |
|---|
| 559 | } |
|---|
| 560 | /* Grava as novas paginas */ |
|---|
| 561 | nodewrit(dbxp,n0p,level,isroot); |
|---|
| 562 | nodewrit(dbxp,n0pnw,level,isroot); |
|---|
| 563 | #if TRACEUPIF |
|---|
| 564 | printf("\n<insert_node> Apos insercao. Velha= "); |
|---|
| 565 | upif_print_node(n0p); |
|---|
| 566 | printf("\n<insert_node> Apos insercao.Nova= "); |
|---|
| 567 | upif_print_node(n0pnw); |
|---|
| 568 | #endif |
|---|
| 569 | |
|---|
| 570 | /* Retorna o primeiro item da nova pagina ser promovido */ |
|---|
| 571 | if (treecase == 0) { |
|---|
| 572 | memcpy(p_key,n1pnw->idx[0].key,keysize); |
|---|
| 573 | }else { |
|---|
| 574 | memcpy(p_key,n2pnw->idx[0].key,keysize); |
|---|
| 575 | } |
|---|
| 576 | |
|---|
| 577 | *p_punt=nodenumber; /* numero da folha definida ??*/ |
|---|
| 578 | return(TRUE); |
|---|
| 579 | } |
|---|
| 580 | /*------------------------------------------------------------------------- |
|---|
| 581 | upif_branqueia |
|---|
| 582 | .Transforma um vetor em um string com "size"brancos |
|---|
| 583 | /*-------------------------------------------------------------------------*/ |
|---|
| 584 | #if CICPP |
|---|
| 585 | void CIIFU :: upif_branqueia(UCHR *str, |
|---|
| 586 | int size) |
|---|
| 587 | #else /*CICPP*/ |
|---|
| 588 | void upif_branqueia(str,size) |
|---|
| 589 | UCHR *str; |
|---|
| 590 | int size; |
|---|
| 591 | #endif /*CICPP*/ |
|---|
| 592 | { |
|---|
| 593 | #if 0 |
|---|
| 594 | int i; |
|---|
| 595 | for (i=0; i<size; i++) str[i]=' '; |
|---|
| 596 | #else |
|---|
| 597 | memset(str,(int)' ',(size_t)size); |
|---|
| 598 | #endif |
|---|
| 599 | str[size]='\0'; |
|---|
| 600 | } |
|---|
| 601 | |
|---|
| 602 | /* -----------------------------------------------------------------------*/ |
|---|
| 603 | |
|---|
| 604 | #if CICPP |
|---|
| 605 | BOOLEAN CIIFU :: upif_insert_leaf(DBXSTRU *dbxp, |
|---|
| 606 | L0STRU *lp, |
|---|
| 607 | UCHR *key, |
|---|
| 608 | int treecase, |
|---|
| 609 | UCHR *p_b_key, |
|---|
| 610 | PUNT *p_b_punt, |
|---|
| 611 | int pos, |
|---|
| 612 | INFO TSTblk, |
|---|
| 613 | INFO TSToff) |
|---|
| 614 | #else /*CICPP*/ |
|---|
| 615 | BOOLEAN upif_insert_leaf(dbxp,lp,key,treecase,p_b_key,p_b_punt, |
|---|
| 616 | pos,TSTblk,TSToff) |
|---|
| 617 | DBXSTRU *dbxp; |
|---|
| 618 | L0STRU *lp; |
|---|
| 619 | UCHR *key; |
|---|
| 620 | int treecase; |
|---|
| 621 | UCHR *p_b_key; |
|---|
| 622 | PUNT *p_b_punt; |
|---|
| 623 | int pos; |
|---|
| 624 | INFO TSTblk; |
|---|
| 625 | INFO TSToff; |
|---|
| 626 | #endif /*CICPP*/ |
|---|
| 627 | { |
|---|
| 628 | INVMAP *invp; |
|---|
| 629 | L0STRU *l0p,*l0pnw; |
|---|
| 630 | static L1STRU *l1p, l1nw, *l1pnw; |
|---|
| 631 | static L2STRU *l2p, l2nw, *l2pnw; |
|---|
| 632 | static L1STRUW l1w, *l1wp; |
|---|
| 633 | static L2STRUW l2w, *l2wp; |
|---|
| 634 | int ifrom,iright,inew,ileft; |
|---|
| 635 | |
|---|
| 636 | #if TRACEUPIF |
|---|
| 637 | UCHR *pnw_item; |
|---|
| 638 | L0STRU *l0wp; |
|---|
| 639 | #endif |
|---|
| 640 | |
|---|
| 641 | PUNT leafnumber; |
|---|
| 642 | int i,ock,keysize,cmp; |
|---|
| 643 | UCHR tmp[LE2+1]; |
|---|
| 644 | L1IDXE leaf_el1; |
|---|
| 645 | L2IDXE leaf_el2; |
|---|
| 646 | |
|---|
| 647 | invp=DBXifmap; |
|---|
| 648 | keysize=vlex[treecase]; |
|---|
| 649 | |
|---|
| 650 | l1p=(L1STRU *)lp; |
|---|
| 651 | l2p=(L2STRU *)lp; |
|---|
| 652 | l2wp = &l2w; |
|---|
| 653 | l1wp = &l1w; |
|---|
| 654 | if (treecase == 0) { |
|---|
| 655 | memcpy(leaf_el1.key,key,keysize); |
|---|
| 656 | leaf_el1.info1=TSTblk; |
|---|
| 657 | leaf_el1.info2=TSToff; |
|---|
| 658 | #if LIND /* 55 */ |
|---|
| 659 | leaf_el1.info3info4.info3=0L; |
|---|
| 660 | #endif /* LIND 55 */ |
|---|
| 661 | #if TRACEUPIF |
|---|
| 662 | pnw_item=(UCHR *)&leaf_el1; |
|---|
| 663 | #endif |
|---|
| 664 | } |
|---|
| 665 | else { |
|---|
| 666 | memcpy(leaf_el2.key,key,keysize); |
|---|
| 667 | leaf_el2.info1=TSTblk; |
|---|
| 668 | leaf_el2.info2=TSToff; |
|---|
| 669 | #if LIND /* 56 */ |
|---|
| 670 | leaf_el2.info3info4.info3=0L; |
|---|
| 671 | #endif /* LIND 56 */ |
|---|
| 672 | #if TRACEUPIF |
|---|
| 673 | pnw_item=(UCHR *)&leaf_el2; |
|---|
| 674 | #endif |
|---|
| 675 | } |
|---|
| 676 | #if TRACEUPIF |
|---|
| 677 | upif_print_lfitem(treecase,(UCHR *)pnw_item); |
|---|
| 678 | #endif |
|---|
| 679 | |
|---|
| 680 | |
|---|
| 681 | /* Deve ser aberto espaco para inserir o elemento na posicao apontada por |
|---|
| 682 | 'pos' . Varre o vetor do fim para comeco, dando um shift em cada elemento |
|---|
| 683 | ate chegar em pos. |
|---|
| 684 | */ |
|---|
| 685 | ock=lp->ock; |
|---|
| 686 | if (ock<TWORDF){ /* Cabe na pagina */ |
|---|
| 687 | if (treecase == 0) { |
|---|
| 688 | for (i=ock;i>pos;i--){ /* Abre espaco para novo item */ |
|---|
| 689 | l1p->idx[i] = l1p->idx[i-1]; |
|---|
| 690 | } |
|---|
| 691 | l1p->idx[pos] = leaf_el1; |
|---|
| 692 | } |
|---|
| 693 | else { |
|---|
| 694 | for (i=ock;i>pos;i--){ /* Abre espaco para novo item */ |
|---|
| 695 | l2p->idx[i] = l2p->idx[i-1]; |
|---|
| 696 | } |
|---|
| 697 | l2p->idx[pos] = leaf_el2; |
|---|
| 698 | } |
|---|
| 699 | lp->ock++; /* Atualiza ocorrencias */ |
|---|
| 700 | #if TRACEUPIF |
|---|
| 701 | printf("\n<insert_leaf> Apos insercao "); |
|---|
| 702 | upif_print_leaf(lp); |
|---|
| 703 | #endif |
|---|
| 704 | leafwrit(dbxp,(L0STRU *)lp); |
|---|
| 705 | return (FALSE); /* Nao precisou fazer split */ |
|---|
| 706 | } |
|---|
| 707 | |
|---|
| 708 | /* Nao cabe na pagina. Precisa fazer Split |
|---|
| 709 | E criada um pagina (wk) com TWORDF+1 elementos, inserindo elemento na |
|---|
| 710 | posicao correta. A copia e feita do fim para o comeco. |
|---|
| 711 | Se key < Original entao copia original, muda origem e destino |
|---|
| 712 | Se Key > Original entao copia key e muda somente destino e transforma |
|---|
| 713 | key em '\0'; |
|---|
| 714 | */ |
|---|
| 715 | |
|---|
| 716 | leafnumber=invp->cn[treecase].fmaxpos+1; |
|---|
| 717 | memcpy((UCHR *)tmp,key,keysize); /* para nao estragar chave original */ |
|---|
| 718 | tmp[keysize]='\0'; |
|---|
| 719 | /* A copia devera ser feita para todo vetor original (ock vezes). |
|---|
| 720 | Para o vetor novo devera ser feita ock+1 vezes. |
|---|
| 721 | Compara-se a chave com cada elemento do vetor. Se o elemento |
|---|
| 722 | do vetor |
|---|
| 723 | Cuidado : indice comeca de 0 |
|---|
| 724 | */ |
|---|
| 725 | ifrom=ock-1; |
|---|
| 726 | if (treecase == 0) { |
|---|
| 727 | #if TRACEUPIF |
|---|
| 728 | l0wp = (L0STRU *)l1wp; |
|---|
| 729 | #endif |
|---|
| 730 | /* SVD 26-11-94 for (i=ock;i>=0;i--) { */ |
|---|
| 731 | i=ock; /* SVD 26-11-94 */ |
|---|
| 732 | for (;ifrom>=0;) { |
|---|
| 733 | cmp = strncmp((CONST char *)l1p->idx[ifrom].key,(CONST char *)tmp,keysize); /*11*/ |
|---|
| 734 | if ( cmp>0 ) { |
|---|
| 735 | l1wp->idx[i]=l1p->idx[ifrom]; |
|---|
| 736 | ifrom--; |
|---|
| 737 | i--; /* SVD 26-11-94 */ |
|---|
| 738 | } |
|---|
| 739 | else |
|---|
| 740 | if ( cmp<0 ) { |
|---|
| 741 | l1wp->idx[i]=leaf_el1; |
|---|
| 742 | i--; /* SVD 26-11-94 */ |
|---|
| 743 | tmp[0]='\0'; |
|---|
| 744 | } |
|---|
| 745 | else |
|---|
| 746 | if ( cmp==0 ) { /* Ja existe termo - erro de logica */ |
|---|
| 747 | fatal("ciifuh/upif_insert_leaf_it1/EL"); |
|---|
| 748 | } |
|---|
| 749 | }/*for*/ |
|---|
| 750 | /* SVD 26-11-94 |
|---|
| 751 | Verifica se a insercao ainda nao foi feita,ou seja , deve ser |
|---|
| 752 | inserida na primeira posicao |
|---|
| 753 | */ |
|---|
| 754 | if (tmp[0]!='\0'){ |
|---|
| 755 | l1wp->idx[0]=leaf_el1; |
|---|
| 756 | tmp[0]='\0'; |
|---|
| 757 | } |
|---|
| 758 | } |
|---|
| 759 | else { |
|---|
| 760 | #if TRACEUPIF |
|---|
| 761 | l0wp = (L0STRU *)l2wp; |
|---|
| 762 | #endif |
|---|
| 763 | /* SVD 26-11-94 for (i=ock;i>=0;i--) { */ |
|---|
| 764 | i=ock; /* SVD 26-11-94 */ |
|---|
| 765 | for (;ifrom>=0;) { |
|---|
| 766 | cmp = strncmp((CONST char *)l2p->idx[ifrom].key,(CONST char *)tmp,keysize); |
|---|
| 767 | if ( cmp>0 ) { |
|---|
| 768 | l2wp->idx[i]=l2p->idx[ifrom]; |
|---|
| 769 | ifrom--; |
|---|
| 770 | i--; /* SVD 26-11-94 */ |
|---|
| 771 | }else |
|---|
| 772 | if ( cmp<0 ) { |
|---|
| 773 | l2wp->idx[i]=leaf_el2; |
|---|
| 774 | i--; /* SVD 26-11-94 */ |
|---|
| 775 | tmp[0]='\0'; |
|---|
| 776 | }else |
|---|
| 777 | if ( cmp==0 ) { /* Ja existe termo - erro de logica */ |
|---|
| 778 | fatal("ciifuh/upif_insert_leaf_it2/EL"); |
|---|
| 779 | } |
|---|
| 780 | }/*for*/ |
|---|
| 781 | /* SVD 26-11-94 |
|---|
| 782 | Verifica se a insercao ainda nao foi feita,ou seja , deve ser |
|---|
| 783 | inserida na primeira posicao |
|---|
| 784 | */ |
|---|
| 785 | if (tmp[0]!='\0'){ |
|---|
| 786 | l2wp->idx[0]=leaf_el2; |
|---|
| 787 | tmp[0]='\0'; |
|---|
| 788 | } |
|---|
| 789 | } |
|---|
| 790 | |
|---|
| 791 | ock++; |
|---|
| 792 | #if TRACEUPIF |
|---|
| 793 | /* teste retirar depois */ |
|---|
| 794 | l0wp->ock=ock; |
|---|
| 795 | l0wp->it=treecase+1; |
|---|
| 796 | l0wp->pos=leafnumber; /* numero de elementos em wk */ |
|---|
| 797 | printf("\n<insert_leaf> Apos insercao em wk"); |
|---|
| 798 | upif_print_leaf((L0STRU *)l0wp); |
|---|
| 799 | #endif |
|---|
| 800 | |
|---|
| 801 | /* Metade esquerda */ |
|---|
| 802 | /* A primeira metade do vetor de trabalho volta para a folha original |
|---|
| 803 | A segunda metade + o novo item inserido vai para a folha nova |
|---|
| 804 | Lembrar que os indices comecam de zero ! |
|---|
| 805 | */ |
|---|
| 806 | l1pnw= &l1nw; |
|---|
| 807 | l2pnw= &l2nw; |
|---|
| 808 | ileft = 0; |
|---|
| 809 | iright = ORDF; /* Metade +1 */ |
|---|
| 810 | inew = 0; |
|---|
| 811 | for (ileft=0; ileft < ORDF; ileft++) { |
|---|
| 812 | if (treecase == 0 ) { |
|---|
| 813 | l1p->idx[ileft] = l1wp->idx[ileft]; |
|---|
| 814 | l1pnw->idx[inew] =l1wp->idx[iright]; |
|---|
| 815 | } |
|---|
| 816 | else { |
|---|
| 817 | l2p->idx[ileft] = l2wp->idx[ileft]; |
|---|
| 818 | l2pnw->idx[inew] =l2wp->idx[iright]; |
|---|
| 819 | } |
|---|
| 820 | iright++; |
|---|
| 821 | inew++; |
|---|
| 822 | } |
|---|
| 823 | if (treecase == 0) { |
|---|
| 824 | l0p = (L0STRU *)l1p; |
|---|
| 825 | l0pnw =(L0STRU *)l1pnw; |
|---|
| 826 | l1pnw->idx[inew] = l1wp->idx[iright]; |
|---|
| 827 | } |
|---|
| 828 | else { |
|---|
| 829 | l0p = (L0STRU *)l2p; |
|---|
| 830 | l0pnw =(L0STRU *)l2pnw; |
|---|
| 831 | l2pnw->idx[inew] = l2wp->idx[iright]; |
|---|
| 832 | } |
|---|
| 833 | inew++; |
|---|
| 834 | iright++; |
|---|
| 835 | l0p->ock=ORDF; |
|---|
| 836 | |
|---|
| 837 | /* Completa informacoes da nova pagina */ |
|---|
| 838 | l0pnw->it=l0p->it; /* Mesmo tipo que a original */ |
|---|
| 839 | l0pnw->pos=leafnumber; /* Proxima folha */ |
|---|
| 840 | l0pnw->ock=ORDF+1; /* O item a ser promovido fica |
|---|
| 841 | armazenado na 1. posicao da |
|---|
| 842 | nova arvore */ |
|---|
| 843 | l0pnw->ps=l0p->ps; /* Acerta apontadores */ |
|---|
| 844 | l0p->ps=leafnumber; |
|---|
| 845 | |
|---|
| 846 | upif_branqueia((UCHR *)tmp,LE2); |
|---|
| 847 | if (treecase ==0) { |
|---|
| 848 | strncpy((char *)leaf_el1.key,(CONST char *)tmp,keysize); |
|---|
| 849 | leaf_el1.info1=(INFO )0; |
|---|
| 850 | leaf_el1.info2=(INFO )0; |
|---|
| 851 | #if LIND /* 53 */ |
|---|
| 852 | leaf_el1.info2=1L; |
|---|
| 853 | leaf_el1.info3info4.info3=(INFO)0; |
|---|
| 854 | #endif /* LIND 53 */ |
|---|
| 855 | } |
|---|
| 856 | else { |
|---|
| 857 | strncpy((char *)leaf_el2.key,(CONST char *)tmp,keysize); |
|---|
| 858 | leaf_el2.info1=(INFO )0; |
|---|
| 859 | leaf_el2.info2=(INFO )0; |
|---|
| 860 | #if LIND /* 54 */ |
|---|
| 861 | leaf_el2.info2=1L; |
|---|
| 862 | leaf_el2.info3info4.info3=(INFO)0; |
|---|
| 863 | #endif /* LIND 54 */ |
|---|
| 864 | } |
|---|
| 865 | /* Branqueia itens disponiveis da pagina velha */ |
|---|
| 866 | for (i=ORDF+1-1;i<=TWORDF-1;i++){ /* indice comeca de zero*/ |
|---|
| 867 | if (treecase == 0 ) { |
|---|
| 868 | l1p->idx[i]=leaf_el1; |
|---|
| 869 | } |
|---|
| 870 | else { |
|---|
| 871 | l2p->idx[i]=leaf_el2; |
|---|
| 872 | } |
|---|
| 873 | } |
|---|
| 874 | for (i=ORDF+2-1; i<=TWORDF-1;i++){ /* Indice comeca de zero */ |
|---|
| 875 | if (treecase == 0) { |
|---|
| 876 | l1pnw->idx[i]=leaf_el1; |
|---|
| 877 | } |
|---|
| 878 | else { |
|---|
| 879 | l2pnw->idx[i]=leaf_el2; |
|---|
| 880 | } |
|---|
| 881 | } |
|---|
| 882 | |
|---|
| 883 | /* Grava as novas paginas */ |
|---|
| 884 | leafwrit(dbxp,l0pnw); |
|---|
| 885 | leafwrit(dbxp,l0p); |
|---|
| 886 | |
|---|
| 887 | #if TRACEUPIF |
|---|
| 888 | printf("\n<insert_leaf> Apos insercao em velha "); |
|---|
| 889 | upif_print_leaf(l0p); |
|---|
| 890 | #endif |
|---|
| 891 | |
|---|
| 892 | #if TRACEUPIF |
|---|
| 893 | printf("\n<insert_leaf> Apos insercao na nova "); |
|---|
| 894 | upif_print_leaf(l0pnw); |
|---|
| 895 | #endif |
|---|
| 896 | |
|---|
| 897 | /* Retorna o primeiro item da nova pagina ser promovido */ |
|---|
| 898 | if (treecase == 0) { |
|---|
| 899 | memcpy(p_b_key,l1pnw->idx[0].key,keysize); |
|---|
| 900 | } |
|---|
| 901 | else { |
|---|
| 902 | memcpy(p_b_key,l2pnw->idx[0].key,keysize); |
|---|
| 903 | } |
|---|
| 904 | *p_b_punt= -leafnumber; /* numero da nova folha inserida */ |
|---|
| 905 | return(TRUE); |
|---|
| 906 | } |
|---|
| 907 | |
|---|
| 908 | /*------------------------------------------------------------------------- |
|---|
| 909 | upif_init_index: |
|---|
| 910 | /*-------------------------------------------------------------------------*/ |
|---|
| 911 | |
|---|
| 912 | #if CICPP |
|---|
| 913 | PUNT CIIFU :: upif_init_index(DBXSTRU *dbxp, |
|---|
| 914 | int treecase, |
|---|
| 915 | UCHR *key, |
|---|
| 916 | INFO TSTblk, |
|---|
| 917 | INFO TSToff) |
|---|
| 918 | #else /*CICPP*/ |
|---|
| 919 | PUNT upif_init_index(dbxp,treecase,key,TSTblk,TSToff) |
|---|
| 920 | DBXSTRU *dbxp; |
|---|
| 921 | int treecase; |
|---|
| 922 | UCHR *key; |
|---|
| 923 | INFO TSTblk; |
|---|
| 924 | INFO TSToff; |
|---|
| 925 | #endif /*CICPP*/ |
|---|
| 926 | { |
|---|
| 927 | INVMAP *invp; |
|---|
| 928 | L0STRU *l0p; |
|---|
| 929 | L1STRU nw_leaf1; |
|---|
| 930 | L2STRU nw_leaf2; |
|---|
| 931 | |
|---|
| 932 | PUNT leafnumber,root,puntdir; |
|---|
| 933 | int keysize,i,ind; |
|---|
| 934 | UCHR tmp[LE2+1]; |
|---|
| 935 | |
|---|
| 936 | invp=DBXifmap; |
|---|
| 937 | keysize=vlex[treecase]; |
|---|
| 938 | |
|---|
| 939 | |
|---|
| 940 | if (treecase == 0) { |
|---|
| 941 | memcpy(nw_leaf1.idx[0].key,key,keysize); |
|---|
| 942 | nw_leaf1.idx[0].info1=TSTblk; |
|---|
| 943 | nw_leaf1.idx[0].info2=TSToff; |
|---|
| 944 | #if LIND /* 57 */ |
|---|
| 945 | nw_leaf1.idx[0].info3info4.info3=(INFO)0; |
|---|
| 946 | #endif /* LIND 57 */ |
|---|
| 947 | l0p=(L0STRU *)&nw_leaf1; |
|---|
| 948 | } |
|---|
| 949 | else { |
|---|
| 950 | memcpy(nw_leaf2.idx[0].key,key,keysize); |
|---|
| 951 | nw_leaf2.idx[0].info1=TSTblk; |
|---|
| 952 | nw_leaf2.idx[0].info2=TSToff; |
|---|
| 953 | #if LIND /* 58 */ |
|---|
| 954 | nw_leaf2.idx[0].info3info4.info3=(INFO)0; |
|---|
| 955 | #endif /* LIND 58 */ |
|---|
| 956 | l0p=(L0STRU *)&nw_leaf2; |
|---|
| 957 | } |
|---|
| 958 | ind=0; |
|---|
| 959 | leafnumber=invp->cn[treecase].fmaxpos+1; |
|---|
| 960 | /* Completa informacoes da nova pagina */ |
|---|
| 961 | l0p->it=treecase+1; |
|---|
| 962 | l0p->pos=leafnumber; |
|---|
| 963 | l0p->ock=1; |
|---|
| 964 | l0p->ps=0; |
|---|
| 965 | |
|---|
| 966 | /* Branqueia itens disponiveis na folha velha */ |
|---|
| 967 | upif_branqueia(tmp,keysize); |
|---|
| 968 | for (i=ind+1;i<TWORDF;i++){ /*Atencao: Indices de vetor 0..twordf-1*/ |
|---|
| 969 | if (treecase == 0) { |
|---|
| 970 | memcpy(nw_leaf1.idx[i].key,tmp,keysize); |
|---|
| 971 | nw_leaf1.idx[i].info1=(INFO )0; |
|---|
| 972 | nw_leaf1.idx[i].info2=(INFO )0; |
|---|
| 973 | #if LIND /* 59 */ |
|---|
| 974 | nw_leaf1.idx[i].info2=1L; |
|---|
| 975 | nw_leaf1.idx[i].info3info4.info3=(INFO)0; |
|---|
| 976 | #endif /* LIND 59 */ |
|---|
| 977 | } |
|---|
| 978 | else { |
|---|
| 979 | memcpy(nw_leaf2.idx[i].key,tmp,keysize); |
|---|
| 980 | nw_leaf2.idx[i].info1=(INFO )0; |
|---|
| 981 | nw_leaf2.idx[i].info2=(INFO )0; |
|---|
| 982 | #if LIND /* 59 */ |
|---|
| 983 | nw_leaf2.idx[i].info2=1L; |
|---|
| 984 | nw_leaf2.idx[i].info3info4.info3=(INFO)0; |
|---|
| 985 | #endif /* LIND 59 */ |
|---|
| 986 | } |
|---|
| 987 | } |
|---|
| 988 | |
|---|
| 989 | leafwrit(dbxp,l0p); |
|---|
| 990 | |
|---|
| 991 | #if TRACEUPIF |
|---|
| 992 | printf("\n<insert_leaf> Apos insercao na nova "); |
|---|
| 993 | upif_print_leaf((L0STRU *)l0p); |
|---|
| 994 | #endif |
|---|
| 995 | |
|---|
| 996 | puntdir=(PUNT )0; |
|---|
| 997 | root =upif_create_root(dbxp, -leafnumber ,key, |
|---|
| 998 | puntdir,treecase,ISFIRST); |
|---|
| 999 | /* Forca a gravar a raiz, poque no UPDATE so grava no fim*/ |
|---|
| 1000 | /* cntwrit(dbxp); *Ja grava quando grava no e esta fazendo update no ifp*/ |
|---|
| 1001 | return (root); |
|---|
| 1002 | } |
|---|
| 1003 | |
|---|
| 1004 | |
|---|
| 1005 | |
|---|
| 1006 | /* -----------------------------------------------------------------------*/ |
|---|
| 1007 | #if CICPP |
|---|
| 1008 | PUNT CIIFU :: upif_search_btree(INVMAP *invp, |
|---|
| 1009 | int treecase, |
|---|
| 1010 | UCHR *key) |
|---|
| 1011 | #else /*CICPP*/ |
|---|
| 1012 | PUNT upif_search_btree(invp,treecase,key) |
|---|
| 1013 | INVMAP *invp; |
|---|
| 1014 | int treecase; |
|---|
| 1015 | UCHR *key; |
|---|
| 1016 | #endif /*CICPP*/ |
|---|
| 1017 | { |
|---|
| 1018 | PUNT punt; |
|---|
| 1019 | int mx_liv; |
|---|
| 1020 | int pos,level; |
|---|
| 1021 | UCHR *np; |
|---|
| 1022 | N2STRU node; |
|---|
| 1023 | BOOLEAN found; |
|---|
| 1024 | N0STRU *n0p; |
|---|
| 1025 | N1STRU *n1p; |
|---|
| 1026 | N2STRU *n2p; |
|---|
| 1027 | |
|---|
| 1028 | lfpath.top=0; |
|---|
| 1029 | punt = invp->cn[treecase].posrx; |
|---|
| 1030 | mx_liv=invp->cn[treecase].liv; |
|---|
| 1031 | |
|---|
| 1032 | if (mx_liv >=MAX_TREE_LEVEL) /* Aumentar a definicao do array Path */ |
|---|
| 1033 | fatal("ciifuh/upif_search_btree/MAX_TREE_LEVEL"); |
|---|
| 1034 | |
|---|
| 1035 | /* Comeca de zero porque considera a raiz como um no normal */ |
|---|
| 1036 | |
|---|
| 1037 | for (level=0;level<=mx_liv;level++){ |
|---|
| 1038 | lfpath.stck[level]=punt; |
|---|
| 1039 | np = (UCHR *)noderead(invp,treecase,level,punt); |
|---|
| 1040 | memcpy((UCHR *)&node,np,sizeof(N2STRU)); |
|---|
| 1041 | np = (UCHR *)&node; |
|---|
| 1042 | n0p = (N0STRU *)np; |
|---|
| 1043 | n1p = (N1STRU *)np; |
|---|
| 1044 | n2p = (N2STRU *)np; |
|---|
| 1045 | found=upif_find_key_node(n0p,key,treecase,&pos); |
|---|
| 1046 | /* para escolher o proximo no deve-se levar em conta que |
|---|
| 1047 | pos aponta para a posicao onde encontrou ou onde esta a chave |
|---|
| 1048 | ou onde devera ser inserida |
|---|
| 1049 | found=true -> desvia para punt(pos); |
|---|
| 1050 | found=FALSE-> desvia para punt(pos-1); |
|---|
| 1051 | found=false e pos=0 (acho que por construcao nao deveria |
|---|
| 1052 | acontecer) |
|---|
| 1053 | */ |
|---|
| 1054 | if (found==FALSE && pos==0) /* Acho que nao deveria acontecer */ |
|---|
| 1055 | fatal("ciifuh/upif_search_btree/AQNDA"); |
|---|
| 1056 | if (found==FALSE) pos--; |
|---|
| 1057 | |
|---|
| 1058 | if (treecase) |
|---|
| 1059 | punt=n2p->idx[pos].punt; |
|---|
| 1060 | else |
|---|
| 1061 | punt=n1p->idx[pos].punt; |
|---|
| 1062 | } |
|---|
| 1063 | /* punt=0 e mx_liv= -1 => arvore vazia */ |
|---|
| 1064 | /* O ultimo apontador tem que ser para uma folha */ |
|---|
| 1065 | lfpath.top=mx_liv; |
|---|
| 1066 | if (punt > 0) fatal("ciifuh/upif_search_btree/punt>=0"); |
|---|
| 1067 | return(-punt); |
|---|
| 1068 | } |
|---|
| 1069 | /*-------------------------------------------------------------------------- |
|---|
| 1070 | upif_fnd_pif |
|---|
| 1071 | .Retorna o pointer para o arquivo invertido onde estao os posts da chave |
|---|
| 1072 | . A atualizacao e feita de acordo com a seguinte tabela: |
|---|
| 1073 | Puntlf Found Pstflag Oper Acoes |
|---|
| 1074 | -------|----------------------------------------------------------------- |
|---|
| 1075 | | | | I Return |
|---|
| 1076 | | | Dic |-------------------------------------- |
|---|
| 1077 | | | | D Return |
|---|
| 1078 | | |-------------------------------------------------------- |
|---|
| 1079 | | | | I pega(blk,off) da arvore |
|---|
| 1080 | | TRUE | IFP | atualiza invertido |
|---|
| 1081 | | ----------------------------------------------- |
|---|
| 1082 | | | | D pega(blk,off) da arvore |
|---|
| 1083 | | | | atualiza invertido |
|---|
| 1084 | EXISTE |----------------------------------------------------------------- |
|---|
| 1085 | | | | I blk=1;off=2; |
|---|
| 1086 | | | | Atualiza arvore |
|---|
| 1087 | | |Dic |-------------------------------------- |
|---|
| 1088 | | | | D Link not found |
|---|
| 1089 | | |------------------------------------------------ |
|---|
| 1090 | | | | I cria new pst(&blk,&off) |
|---|
| 1091 | | FALSE | IFP | atualiza arvore |
|---|
| 1092 | | | --------------------------------------- |
|---|
| 1093 | | | | D Link not found |
|---|
| 1094 | | | | |
|---|
| 1095 | ----------------|-------------------------------------------------------- |
|---|
| 1096 | -------|----------------------------------------------------------------- |
|---|
| 1097 | | | | I blk=1;off=2; |
|---|
| 1098 | | | | Atualiza arvore-Cria raiz |
|---|
| 1099 | | |Dic |--------------------------------------- |
|---|
| 1100 | | | | D Link not found |
|---|
| 1101 | NAO | |------------------------------------------------- |
|---|
| 1102 | Existe | | | I cria new pst(&blk,&off) |
|---|
| 1103 | | FALSE | IFP | cria arvore |
|---|
| 1104 | | | ---------------------------------------- |
|---|
| 1105 | | | | D Link not found |
|---|
| 1106 | | | | |
|---|
| 1107 | |----------------------------------------------------------------- |
|---|
| 1108 | --------------------------------------------------------------------------*/ |
|---|
| 1109 | |
|---|
| 1110 | #if CICPP |
|---|
| 1111 | void CIIFU :: upif_fnd_pifp(DBXSTRU *dbxp, |
|---|
| 1112 | UCHR *key, |
|---|
| 1113 | int treecase, |
|---|
| 1114 | POSTSTRU *pst, |
|---|
| 1115 | UCHR oper, |
|---|
| 1116 | int pstflag) |
|---|
| 1117 | #else /*CICPP*/ |
|---|
| 1118 | void upif_fnd_pifp(dbxp,key,treecase,pst,oper,pstflag) |
|---|
| 1119 | DBXSTRU *dbxp; |
|---|
| 1120 | UCHR *key; |
|---|
| 1121 | int treecase; |
|---|
| 1122 | POSTSTRU *pst; |
|---|
| 1123 | UCHR oper; |
|---|
| 1124 | int pstflag; |
|---|
| 1125 | #endif /*CICPP*/ |
|---|
| 1126 | { |
|---|
| 1127 | INVMAP *invp; |
|---|
| 1128 | PUNT p_b_punt,root,node,key_punt; |
|---|
| 1129 | L0STRU *lp; |
|---|
| 1130 | |
|---|
| 1131 | L1STRU *l1p; |
|---|
| 1132 | L2STRU *l2p; |
|---|
| 1133 | |
|---|
| 1134 | UWORD isroot; |
|---|
| 1135 | UCHR *p_b_key,a_p_b_key[LE2+1]; |
|---|
| 1136 | UCHR *lbufp; |
|---|
| 1137 | BOOLEAN promoted; |
|---|
| 1138 | BOOLEAN found; |
|---|
| 1139 | int topo,level; |
|---|
| 1140 | int pos, |
|---|
| 1141 | keysize; |
|---|
| 1142 | INFO xblk,xoff; |
|---|
| 1143 | static L2STRU xxbufp; |
|---|
| 1144 | |
|---|
| 1145 | /* ==================== xupd =======================> */ |
|---|
| 1146 | #if LINDLUX /* 15x */ |
|---|
| 1147 | LONGX *luxinfo1p,*luxinfo2p,*luxinfo3p; |
|---|
| 1148 | char *luxkeyp; |
|---|
| 1149 | LONGX luxmfn; |
|---|
| 1150 | LONGX luxyaddr; |
|---|
| 1151 | int luxycase,luxn; |
|---|
| 1152 | unsigned char luxmask; |
|---|
| 1153 | #endif /* LINDLUX 15x */ |
|---|
| 1154 | /* <==================== xupd ======================= */ |
|---|
| 1155 | |
|---|
| 1156 | invp=DBXifmap; |
|---|
| 1157 | |
|---|
| 1158 | keysize=vlex[treecase]; |
|---|
| 1159 | isroot=0; |
|---|
| 1160 | root=invp->cn[treecase].posrx; |
|---|
| 1161 | p_b_key=(UCHR *)a_p_b_key; |
|---|
| 1162 | lbufp=(UCHR *)&xxbufp; |
|---|
| 1163 | |
|---|
| 1164 | memset(lbufp,0x00,sizeof(L2STRU)); |
|---|
| 1165 | |
|---|
| 1166 | #if TRACEUPIF |
|---|
| 1167 | upif_chgtostr(key,keysize,tkey); |
|---|
| 1168 | printf("\nfnd key=%s treecase=%d",tkey,treecase); |
|---|
| 1169 | #endif |
|---|
| 1170 | |
|---|
| 1171 | /* Pesquisa na arvore qual a folha onde deve ficar o posting */ |
|---|
| 1172 | puntlf=upif_search_btree( invp ,treecase,(UCHR *)key); |
|---|
| 1173 | |
|---|
| 1174 | /* ==================== xupd =======================> */ |
|---|
| 1175 | #if LINDLUX /* 15x */ |
|---|
| 1176 | if (invp->lvxiflind) { /* iflind Step#2 */ |
|---|
| 1177 | if (!invp->lvxpages) { |
|---|
| 1178 | invp->lvxpages=invp->lvxmaxmfn/8+1; |
|---|
| 1179 | if (invp->lvxpages > LVXPAGES) fatal("svdifupd/lvx/LVXPAGES"); |
|---|
| 1180 | for (luxx=0; luxx < invp->lvxpages; luxx++ ) { |
|---|
| 1181 | invp->lvxvpagp[luxx]=(char *)ALLOC((ALLOPARM)LVXIYPBS); |
|---|
| 1182 | if (invp->lvxvpagp[luxx] == (char *)ALLONULL) |
|---|
| 1183 | fatal("svdifupd/lvx/ALLOC"); |
|---|
| 1184 | #if 0 |
|---|
| 1185 | printf("+++ luxx=%ld coreleft=%ld\n",luxx,CORELEFT()); |
|---|
| 1186 | #endif |
|---|
| 1187 | memset(invp->lvxvpagp[luxx],0x00,(size_t)LVXIYPBS); |
|---|
| 1188 | } |
|---|
| 1189 | } |
|---|
| 1190 | if (puntlf != 0) { /* Achou a folha */ |
|---|
| 1191 | lp=leafread(NULL,invp,treecase,puntlf,0); /* lp=invp->luxvpagp[][] */ |
|---|
| 1192 | found=upif_find_key_leaf((L0STRU *)lp, key,treecase, &pos); |
|---|
| 1193 | if (found==TRUE) { |
|---|
| 1194 | if (pstflag==IFUPDICT) fatal("svdifupd/lvx/pstflag"); |
|---|
| 1195 | if (treecase==0) { |
|---|
| 1196 | l1p=(L1STRU *)lp; |
|---|
| 1197 | luxinfo1p = &(l1p->idx[pos].info1); |
|---|
| 1198 | luxinfo2p = &(l1p->idx[pos].info2); |
|---|
| 1199 | luxinfo3p = &(l1p->idx[pos].info3info4.info3); |
|---|
| 1200 | luxkeyp = l1p->idx[pos].key; |
|---|
| 1201 | } else { |
|---|
| 1202 | l2p=(L2STRU *)lp; |
|---|
| 1203 | luxinfo1p = &(l2p->idx[pos].info1); |
|---|
| 1204 | luxinfo2p = &(l2p->idx[pos].info2); |
|---|
| 1205 | luxinfo3p = &(l2p->idx[pos].info3info4.info3); |
|---|
| 1206 | luxkeyp = l2p->idx[pos].key; |
|---|
| 1207 | } |
|---|
| 1208 | /* upif_process_posting(pst,xblk,xoff,oper); */ |
|---|
| 1209 | luxp=(char *)pst; |
|---|
| 1210 | luxmfn=100*(LONGX)luxp[0]+10*(LONGX)luxp[1]+(LONGX)luxp[2]; |
|---|
| 1211 | if (luxmfn < 1 || luxmfn > invp->lvxmaxmfn) { |
|---|
| 1212 | fprintf(stderr,"*** key=%s mfn=%ld maxmfn=%ld", |
|---|
| 1213 | luxkeyp,luxmfn,invp->lvxmaxmfn); |
|---|
| 1214 | fatal("svdifupd/lvx/offset"); |
|---|
| 1215 | } |
|---|
| 1216 | luxyaddr = *luxinfo1p; |
|---|
| 1217 | if (*luxinfo2p <= 0) fatal("svdifupd/lvx/docsp"); |
|---|
| 1218 | if (*luxinfo3p < 0) { |
|---|
| 1219 | luxycase=BITSTRING; |
|---|
| 1220 | luxyaddr+=luxmfn/8; /* bp= &areabits[mfn/8]; */ |
|---|
| 1221 | } |
|---|
| 1222 | else |
|---|
| 1223 | if (*luxinfo3p == 0) { |
|---|
| 1224 | luxycase=MFNSTRING; |
|---|
| 1225 | luxyaddr-=(*luxinfo2p)*PMFNSIZ; /* (*luxinfo2p)*PMFNSIZ */ |
|---|
| 1226 | } |
|---|
| 1227 | else fatal("svdifupd/lvx/case"); |
|---|
| 1228 | #if 0 /* TRPTRACE */ |
|---|
| 1229 | printf("+++ %ld key=%s info1=%ld info2=%ld info3=%ld \n", |
|---|
| 1230 | luxmfn,keyp,*luxinfo1p,*luxinfo2p,*luxinfo3p); |
|---|
| 1231 | #endif |
|---|
| 1232 | luxx=luxyaddr/LVXIYPBS; |
|---|
| 1233 | if (luxx >= invp->lvxpages) fatal("svdifupd/lvx/pages"); |
|---|
| 1234 | luxp=invp->lvxvpagp[luxx]; |
|---|
| 1235 | luxx=luxyaddr%LVXIYPBS; |
|---|
| 1236 | luxp+=luxx; |
|---|
| 1237 | switch (luxycase) { |
|---|
| 1238 | case BITSTRING: |
|---|
| 1239 | /* bp = &areabits[ luxmfn / 8]; */ |
|---|
| 1240 | luxmask = (unsigned char)bitmask [ luxmfn & BY8RMASK ]; |
|---|
| 1241 | if ((unsigned char)(*luxp) & luxmask) { |
|---|
| 1242 | if (invp->lvxfplog) |
|---|
| 1243 | fprintf(invp->lvxfplog, |
|---|
| 1244 | "duplicated posting|%8ld|%s\n", |
|---|
| 1245 | luxmfn,keyp); |
|---|
| 1246 | } |
|---|
| 1247 | else { |
|---|
| 1248 | *luxp = (unsigned char)(*luxp) | luxmask; |
|---|
| 1249 | (*luxinfo2p)--; /* one more doc unflagged */ |
|---|
| 1250 | } |
|---|
| 1251 | break; |
|---|
| 1252 | case MFNSTRING: |
|---|
| 1253 | if (luxx+PMFNSIZ < LVXIYPBS) memcpy(luxp,(char *)pst,PMFNSIZ); |
|---|
| 1254 | else { |
|---|
| 1255 | memcpy(luxp,(char *)pst,(luxn=PMFNSIZ-(LVXIYPBS-luxx))); |
|---|
| 1256 | luxyaddr+=luxn; |
|---|
| 1257 | luxx=luxyaddr/LVXIYPBS; |
|---|
| 1258 | if (luxx >= invp->lvxpages) fatal("svdifupd/lvx/pages"); |
|---|
| 1259 | luxp=invp->lvxvpagp[luxx]; |
|---|
| 1260 | luxx=luxyaddr%LVXIYPBS; |
|---|
| 1261 | luxp+=luxx; |
|---|
| 1262 | memcpy(luxp,((char *)pst)+luxn,PMFNSIZ-luxn); |
|---|
| 1263 | } |
|---|
| 1264 | (*luxinfo2p)--; /* one more doc unflagged */ |
|---|
| 1265 | (*luxinfo1p)+=PMFNSIZ; /* one more doc stored */ |
|---|
| 1266 | break; |
|---|
| 1267 | } /* end of switch luxycase */ |
|---|
| 1268 | } |
|---|
| 1269 | else fatal("svdifupd/lvx/found"); |
|---|
| 1270 | } |
|---|
| 1271 | else fatal("svdifupd/lvx/puntlf"); |
|---|
| 1272 | } |
|---|
| 1273 | #endif /* LINDLUX 15x */ |
|---|
| 1274 | /* <==================== xupd ======================= */ |
|---|
| 1275 | |
|---|
| 1276 | if (puntlf != 0) { /* Achou a folha */ |
|---|
| 1277 | lp=leafread(lbufp,invp,treecase,puntlf,0); |
|---|
| 1278 | #if TRACEUPIF2 |
|---|
| 1279 | printf("\n<fnd> Vai pesquisar na folha "); |
|---|
| 1280 | upif_print_leaf((L0STRU*)lp); |
|---|
| 1281 | #endif |
|---|
| 1282 | found=upif_find_key_leaf((L0STRU *)lp, key,treecase, &pos); |
|---|
| 1283 | } |
|---|
| 1284 | |
|---|
| 1285 | if ( (puntlf !=0) && (found==TRUE)) { |
|---|
| 1286 | #if !LIND /* 15 */ |
|---|
| 1287 | if (pstflag==IFUPDICT) return; |
|---|
| 1288 | if (treecase==0) { |
|---|
| 1289 | l1p=(L1STRU *)lp; |
|---|
| 1290 | xblk=l1p->idx[pos].info1; |
|---|
| 1291 | xoff=l1p->idx[pos].info2; |
|---|
| 1292 | }else { |
|---|
| 1293 | l2p=(L2STRU *)lp; |
|---|
| 1294 | xblk=l2p->idx[pos].info1; |
|---|
| 1295 | xoff=l2p->idx[pos].info2; |
|---|
| 1296 | } |
|---|
| 1297 | upif_process_posting(pst,xblk,xoff,oper); |
|---|
| 1298 | #else /* !LIND 15 */ |
|---|
| 1299 | if (pstflag!=IFUPDICT && pstflag!=IFUPISIS) fatal("ciifuh/lind"); |
|---|
| 1300 | if (treecase==0) { |
|---|
| 1301 | l1p=(L1STRU *)lp; |
|---|
| 1302 | l1p->idx[pos].info2++; |
|---|
| 1303 | leafwrit(dbxp,(L0STRU *)l1p); |
|---|
| 1304 | }else { |
|---|
| 1305 | l2p=(L2STRU *)lp; |
|---|
| 1306 | l2p->idx[pos].info2++; |
|---|
| 1307 | leafwrit(dbxp,(L0STRU *)l2p); |
|---|
| 1308 | } |
|---|
| 1309 | #endif /* !LIND 15 */ |
|---|
| 1310 | #if TRACEUPIF |
|---|
| 1311 | printf("\nfnd Encontrou chave folha=%ld pos=%d ",lp->pos,pos); |
|---|
| 1312 | #endif |
|---|
| 1313 | } |
|---|
| 1314 | |
|---|
| 1315 | if ((puntlf !=0 ) && (found==FALSE)) { |
|---|
| 1316 | |
|---|
| 1317 | if (oper == DELETION){ |
|---|
| 1318 | upif_print_msg(pst,(UCHR *)lnk_not_found_msg); |
|---|
| 1319 | return; |
|---|
| 1320 | } |
|---|
| 1321 | |
|---|
| 1322 | #if !LIND /* 13 */ |
|---|
| 1323 | if (pstflag == IFUPDICT) { |
|---|
| 1324 | xblk=1L; |
|---|
| 1325 | xoff=2L; |
|---|
| 1326 | }else { |
|---|
| 1327 | upif_process_new_key(pst,&xblk,&xoff); |
|---|
| 1328 | } |
|---|
| 1329 | |
|---|
| 1330 | #else /* !LIND 13 */ |
|---|
| 1331 | xblk=0L; |
|---|
| 1332 | xoff=1L; |
|---|
| 1333 | #endif /* !LIND 13 */ |
|---|
| 1334 | |
|---|
| 1335 | promoted=upif_insert_leaf( |
|---|
| 1336 | dbxp,(L0STRU *)lp,key,treecase,p_b_key,&p_b_punt, |
|---|
| 1337 | pos,xblk,xoff); |
|---|
| 1338 | |
|---|
| 1339 | topo=lfpath.top; |
|---|
| 1340 | /* se houve necessidade de criar um novo no, |
|---|
| 1341 | faz insercao em niveis superiores */ |
|---|
| 1342 | for (level=topo; level>=0 && promoted==TRUE; level--){ |
|---|
| 1343 | memcpy(key,p_b_key,keysize); |
|---|
| 1344 | key_punt=p_b_punt; |
|---|
| 1345 | node=lfpath.stck[level]; |
|---|
| 1346 | #if TRACEUPIF |
|---|
| 1347 | upif_chgtostr(p_b_key,keysize,tkey); /* AOT 11/09/2000 */ |
|---|
| 1348 | printf("\nfnd Vai promover para node=%ld keypunt=%ld key=%s", |
|---|
| 1349 | node,key_punt,tkey); |
|---|
| 1350 | #endif |
|---|
| 1351 | isroot=(root==node)?TRUE:FALSE; |
|---|
| 1352 | promoted=upif_insert_index(dbxp,node,treecase,level,isroot, |
|---|
| 1353 | key,key_punt, |
|---|
| 1354 | p_b_key,&p_b_punt); |
|---|
| 1355 | } /* for */ |
|---|
| 1356 | |
|---|
| 1357 | /* Se houve promocao ate a raiz cria uma nova raiz */ |
|---|
| 1358 | if (promoted==TRUE){ |
|---|
| 1359 | memcpy(key,p_b_key,keysize); |
|---|
| 1360 | root=upif_create_root(dbxp,node,key,p_b_punt,treecase,NOFIRST); |
|---|
| 1361 | } |
|---|
| 1362 | } |
|---|
| 1363 | |
|---|
| 1364 | if (puntlf == 0) { |
|---|
| 1365 | if (oper == DELETION) { |
|---|
| 1366 | upif_print_msg(pst,(UCHR *)lnk_not_found_msg); |
|---|
| 1367 | return; |
|---|
| 1368 | } |
|---|
| 1369 | |
|---|
| 1370 | #if !LIND /* 12 */ |
|---|
| 1371 | if (pstflag == IFUPDICT) { |
|---|
| 1372 | xblk=1L; |
|---|
| 1373 | xoff=2L; |
|---|
| 1374 | }else { |
|---|
| 1375 | upif_process_new_key(pst,&xblk,&xoff); |
|---|
| 1376 | } |
|---|
| 1377 | #else /* !LIND 12 */ |
|---|
| 1378 | xblk=0L; xoff=1L; |
|---|
| 1379 | #endif /* !LIND 12 */ |
|---|
| 1380 | |
|---|
| 1381 | root=upif_init_index(dbxp,treecase,key,xblk,xoff); |
|---|
| 1382 | } |
|---|
| 1383 | } |
|---|