-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathciifl.c
2621 lines (2526 loc) · 73.8 KB
/
ciifl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-------------------------------------------------------------------------
ciifl.c
Faz o load do arquivo invertido (.ifp) e do dicionario (.n0* e .l0*)
-------------------------------------------------------------------------*/
/* 19-02-95 : Correcao do load de arquivos com + de 32k postings
O primeiro segmento estava com o numero total de postings
corretos. Porem os apontadores dos segmentos seguintes
estavam incorretos. Algumas mudancas feitas(types) nao
implicavam no erro.
*/
#define TRACE_HEADER 0
#include <stdio.h>
#include <string.h>
#include "cisis.h"
#include "ciupi.h"
#if CICPP
#include "cirec.hpp" /* new RECSTRU */
#endif /* CICPP */
#if !CICPP
char *ifllk_gidbnp=NULL; /* dbn.par */
#endif /* CICPP */
#if CICPP
#include <ciifl.hpp>
#include <cidbx.hpp>
#include <citrm.hpp>
#include <cirun.hpp>
#ifdef USE_ERROR_SYS
#include <ui_win.hpp>
#include <errorsys.hpp>
extern MY_ERROR_SYSTEM * errsys;
extern UIW_WINDOW *wprogress;
extern MESS_SYSTEM *mess;
void ISIS_OemToAnsi(char *in, char *out);
#include <textdb.hpp>
#define USE_INFO_SYS
#endif
#endif /* CICPP */
#if !CICPP
int ifl_balan=1; /* Faz balanceamento da arvore */
#endif /* CICPP */
#define AOT 1
#define TSTBALANCE DEBIFUPD
#define TRACELOAD DEBIFUPD
#define RESUMOLOAD DEBIFUPD
#define TRCBUFF DEBIFUPD
#define MAXPSTS MAXIDXPST
#define BLK(x) x->ifpblk
/* Esta estrutura guarda o ultimo no de cada nivel
Foi definida em cima do maximo valor que sao os
nos da arvore do tipo 2.
Poderia ser feita uma alocacao dinamica
*/
#if !CICPP
typedef struct tree_nodes {
int top;
N2STRU idx_node[MAX_TREE_LEVEL];
} TREE_NODES;
#endif /* CICPP */
/* -----------------------------------------------------------------------*/
/* Prototypes */
/* -----------------------------------------------------------------------*/
#if !CICPP
#if ANSI
/*y*/ static void lifp_init_counters(void);
static void lifp_init_leaf(L0STRU *lp,int treecase);
static void lifp_init_node(N0STRU *n0p,int treecase);
static BOOLEAN lifp_insert_leaf(DBXSTRU *dbxp,char *key,char *p_b_key,
PUNT *p_b_punt, int treecase,INFX info1,
INFO info2);
static BOOLEAN lifp_insert_node (DBXSTRU *dbxp,N0STRU *n0p,int treecase,
int level,
int isroot,UCHR *b_key,PUNT b_punt,
UCHR *p_key,PUNT *p_punt);
static void lifp_create_root(INVMAP *invp,N0STRU *n0p,PUNT esq,char *key,
PUNT dir,int treecase,BOOLEAN first);
#else
/*y*/ static void lifp_init_counters();
static void lifp_init_leaf();
static void lifp_init_node();
static BOOLEAN lifp_insert_leaf();
static BOOLEAN lifp_insert_node ();
static void lifp_create_root();
#endif /* ANSI */
#endif /* CICPP */
#if !CICPP
#if ANSI
static void determina_nova_qtda(int ordem,int a,int b,int *n_a,int *n_b);
static void init_invp_roots_nodes_leaves(INVMAP *invp);
static void balance_nodes(DBXSTRU *dbxp,INVMAP *invp,int treecase,
int level,PUNT idxpenultimo,PUNT idxultimo,N0STRU *np);
static void balance_btree(DBXSTRU *dbxp,INVMAP *invp,
int level,PUNT punt,int treecase);
static void balance_leaves(DBXSTRU *dbxp,INVMAP *invp,int treecase,
PUNT idxpenultimo,PUNT idxultimo,N0STRU *np);
static void lifp_store_btree(UCHR *key,INFX blk,INFO off,int treecase);
static IFPBUFFER *lifp_init(INVMAP *invp); /* antes de LKXONLY: (void) */
static void lifp_close_tree(void);
#if !LIND
static void lifp_grava_ifp(void);
static void lifp_storepst(void);
static void lifp_init_buff(IFPBUFFER *p,INFO blk);
static void write_buffers(BOOLEAN keep_head,BOOLEAN reestrut_buff);
static void get_room_for (int qt,BOOLEAN opt_keep_head);
static void lifp_upd_pst_header(IFPSTRU *p,INFO offset,INFO nxtb,INFO nxtp,
INFO totp,INFO segp,INFO segc);
/*static void lifp_upd_first_rec(IFPAVAILPOS *pn,INFO next_blk,UWORD idxpst); */
static void lifp_upd_first_rec(IFPAVAILPOS *pn,INFO next_blk,INFO next_post);
/*19-02-95*/
static void lifp_init_ifprec(IFPSTRU *p,INFO blk);
#endif /* !LIND */
#else
static void determina_nova_qtda();
static void init_invp_roots_nodes_leaves();
static void balance_nodes();
static void balance_btree();
static void balance_leaves();
static void lifp_store_btree();
static IFPBUFFER *lifp_init();
static void lifp_close_tree();
#if !LIND
static void lifp_grava_ifp();
static void lifp_storepst();
static void lifp_init_buff();
static void write_buffers();
static void get_room_for ();
static void lifp_upd_pst_header();
static void lifp_upd_first_rec();
static void lifp_init_ifprec();
#endif /* !LIND */
#endif /* ANSI */
#endif /* CICPP */
#if !CICPP
#if ANSI
static int lifp_trans_key(UCHR *keyp, int len, int *treecase);
#else
static int lifp_trans_key();
#endif /* ANSI */
#endif /* CICPP */
/* -----------------------------------------------------------------------*/
#if !CICPP
static TREE_NODES load_idx[2];
static PUNT last_root; /* Guarda a raiz atual antes de criar uma nova raiz
*/
static L0STRU *l0p;
static L1STRU *l1p, l1node;
static L2STRU *l2p, l2node;
static L1IDXE init_leaf_el1;
static L2IDXE init_leaf_el2;
static N1IDXE init_node_el1;
static N2IDXE init_node_el2;
static LONGX print_step=0L;
#if TRACELOAD
UCHR tkey[LE2+1], *ptkey; /* AOT 23/10/93 */
int ti;
#endif
static POSTSTRU pst,pst_ant;
static UCHR *keyp;
static LONGX nlido;
static PUNT p_b_punt,
key_punt;
static INVMAP *invp;
static DBXSTRU *dbxp;
static N0STRU *n0p;
static UWORD isroot,i;
static UCHR *p_b_key,
a_p_b_key[LE2+1];
static BOOLEAN promoted;
static int topo,
level,
keysize,
treecase,
treecase_ant;
static UCHR key_ant[LE2+1],
key[LE2+1];
static IFPBUFFER *ifpbuff=NULL; /* AOT - 16/09/93 */
#if !LIND
static INFO key_blk,
key_pos;
/* static int tag,occ,cnt; */ /* AOT - 26/10/93 */
static IFPAVAILPOS pn;
static IFPSTRU *pf,
*ph,
*pb;
static INFO next_blk,
next_pos;
static INFO totp,
nxtb,
nxtp,
segp,
segc;
static INFO off_f,
off_h,
npst;
static int gbuf;
static IFPSTRU current_seghead,
first_seghead;
static IFPHEAD *currenthead,
*firsthead;
static BOOLEAN flag_first_seghead,
flag_current_seghead;
static UWORD idxpst;
#endif /* !LIND */
#if LIND
#include "ciiflh.c"
#endif /* LIND */
#if LKXONLY
static int lkxonly; /* AOT - 14/05/98 */
#endif
/*25/07/97*/
/* Variaveis para guardar a localizacao ds headers de um segmento */
/* 128/(5+2) */
#define IFLmaxhd IFPmaxhd*MAXIFPMEM + 1 /* headers em memoria */
#if CNV_PCBINUM
static int hdn=-1; /* numero headers de um ifp */
static INFO hdblk[IFLmaxhd];
static INFO hdoff[IFLmaxhd];
#endif
/* Descricao de Variaveis e Atualizacao do IFP
pn - pointer para o primeiro registro do arquivo que contem o apontador
para o proximo (blk,off) disponiveis.
ph - pointer para o registro onde esta o head do segmento que devera ser
atualizado
pb - pointer para o bloco onde estao sendo gravados os postings
pf - pointer para o primeiro bloco onde e' gravado o total de postings
quando uma chave possui mais MAXPSTSSEG e deve ser gravada em dois
segmentos diferentes
ifpbuff - Buffer de MAXIFPMEM blocos com objetivo de acelerar a atualiacao
Nesse buffer devem ser mantidos blocos sequenciais " de disco"
de forma em unico acesso gravar variso blocos
off_h e off_f - guardam a posicao do header do posting dentro de ph e pf
respectivamente
Algoritmo:
Em determinado momento os postings de uma chave podem ser encaixados em
uma das seguintes condicoes:
1) Todos os postings cabem em um pedaco do ifpbuff. Neste caso temos:
ph - aponta para um bloco do ifpbuff.
A varivavel pf nao dever ser considerada.
2) Embora a quantidade de postings seja pequena e menor que MAXPSTSSEG
nao cabe nas posicoes disponiveis do ifpbuff. Neste caso devemos:
a) Gravar ifpbuff;
b) copiar o bloco onde esta o header do segmento (pb) para uma
variavel temporaria e apontar ph para essa area. Notar que o
embora o header seja gravado seus valores estarao incorretos.
ph=endereco de header_temp;
copiar: pb para ph;
off_h = pb.off;
c) Indicar que agora ph nao aponta para o buffer sequencial
flag_current_seghead=true;
No final o header devera ser regravado com os valores corretos.
3) O numero de postings da chave ultrapassa MAXPSTSSEG. Neste caso
sera gerado mais de um segmento.
O primeiro segmento devera ser mantido em memoria porque e' nele
que e gravado a quantidade correta de postings do segmento
Entao havera necessidade de dois apontadores:
pf - aponta para o primeiro segmento
ph - aponta para o segmento corrente.
No memomento que o numero de postings ultrapassa MAXPSTSSEG deve-se
a) Copiar ph para pf
b) flag_first_seghead =true
c)
/*-------------------------------------------------------------------------*/
/* Este algoritmo determina qual a quantidade de chaves que os nos
mais a direita devem ter. Foi feito para que o load ficasse
igual ao do ISIS . Acho que nao deveria deste jeito, porque nao
separa igualmente a quantidade de chaves.
Inicialmente o algoritmo de load, fazia com que, para todos os niveis,
os dois nos mais a direita do nivel ficassem desbalanceados. Era possivel
termos o penultimo com 10 ocorrencias e o ultimo com uma ocorrencia.
Apos o balanceamento dos nos mais a direita de cada nivel fica da
seguinte maneira ( a e b sao os dois ultimos nos)
a+b nova qta a nova qta b
11 5 6
12 5 7
13 5 8
14 5 9
15 5 10
16 8 8
17 8 9
18 8 10
19 9 10
20 10 10
*/
#endif /* CICPP */
#if CICPP
void CIIFL :: determina_nova_qtda(int ordem,
int a,
int b,
int *n_a,
int *n_b)
#else /*CICPP*/
static void determina_nova_qtda(ordem,a,b,n_a,n_b)
int ordem;
int a;
int b;
int *n_a;
int *n_b;
#endif /*CICPP*/
{
int x,y,soma;
soma=a+b;
x=ordem; /* Caso 1 : a=5; b=6,7,8,9,10 */
y=soma-x;
if (y>2*ordem) {
x=(3*ordem+1)/2; /* Caso 2: a=8; b:8,9,10 */
y=soma-x;
if (y>2*ordem) { /* Caso 3: a=9; b=9,10 ou
Caso 4: a=10; b=10
*/
x=soma/2;
y=soma-x;
}
}
*n_a=x;
*n_b=y;
}
/* #include "leaves.c" */
/* ------------------------------------------------------------------------*/
#if CICPP
void CIIFL :: balance_leaves(DBXSTRU *dbxp,
INVMAP *invp,
int treecase,
PUNT idxesq,
PUNT idxdir,
N0STRU *np)
#else /*CICPP*/
static void balance_leaves(dbxp,invp,treecase,idxesq,idxdir,np)
DBXSTRU *dbxp;
INVMAP *invp;
int treecase;
PUNT idxesq;
PUNT idxdir;
N0STRU *np;
#endif /*CICPP*/
{
UCHR *lbufp;
N1STRU *ptmp_pai1;
N2STRU *ptmp_pai2;
L0STRU *l0p,*pesq0,*pdir0;
L1STRU *pesq1,esq1,
*pdir1,dir1,
*ptmp_esq1,tmp_esq1,
*ptmp_dir1,tmp_dir1,
trml1buf;
L2STRU *pesq2,esq2,
*pdir2,dir2,
*ptmp_esq2,tmp_esq2,
*ptmp_dir2,tmp_dir2,
trml2buf;
PUNT punt;
int n_esq,n_dir,ock,id,nn;
keysize=vlex[treecase];
if (treecase == 0)
lbufp= (UCHR *)(&trml1buf);
else
lbufp= (UCHR *)(&trml2buf);
if (idxesq==0) return;
if (idxdir==0) return;
punt= -idxesq;
if (punt<0) {
fatal(" balance_leaves/punt >0");
}
l0p=(L0STRU *)leafread(lbufp,invp,treecase,punt,0);
if (treecase==0) {
ptmp_esq1= &tmp_esq1;
pesq1= &esq1;
memmove((UCHR *)pesq1,(UCHR *)l0p,sizeof(L1STRU ));
} else {
ptmp_esq2= &tmp_esq2;
pesq2= &esq2;
memmove((UCHR *)pesq2,(UCHR *)l0p,sizeof(L2STRU ));
}
punt= -idxdir;
if (punt<0) {
fatal(" balance_leaves/punt >0");
}
l0p=(L0STRU *)leafread(lbufp,invp,treecase,punt,0);
if (treecase==0) {
pdir1= &dir1;
memmove((UCHR *)pdir1,(UCHR *)l0p,sizeof(L1STRU ));
} else {
pdir2= &dir2;
memmove((UCHR *)pdir2,(UCHR *)l0p,sizeof(L2STRU ));
}
if (treecase==0) {
ptmp_pai1=(N1STRU *)np ;
#if TSTBALANCE
printf("\n Antes de balancear ");
upif_print_node ((N0STRU *)ptmp_pai1);
upif_print_leaf ((L0STRU *)pesq1);
upif_print_leaf ((L0STRU *)pdir1);
#endif
determina_nova_qtda(ORDF,pesq1->ock,pdir1->ock,&n_esq,&n_dir);
ptmp_esq1= &tmp_esq1;
ptmp_dir1= &tmp_dir1;
lifp_init_leaf((L0STRU *)ptmp_esq1,treecase);
lifp_init_leaf((L0STRU *)ptmp_dir1,treecase);
}else {
ptmp_pai2=(N2STRU *)np ;
#if TSTBALANCE
printf("\n Antes de balancear ");
upif_print_node ((N0STRU *)ptmp_pai2);
upif_print_leaf ((L0STRU *)pesq2);
upif_print_leaf ((L0STRU *)pdir2);
#endif
determina_nova_qtda(ORDF,pesq2->ock,pdir2->ock,&n_esq,&n_dir);
ptmp_esq2= &tmp_esq2;
ptmp_dir2= &tmp_dir2;
lifp_init_leaf((L0STRU *)ptmp_esq2,treecase);
lifp_init_leaf((L0STRU *)ptmp_dir2,treecase);
}
/* Da forma que o algoritmo de load funciona, a celula da esquerda
contera sempre o numero maximo de chaves . Eles deverao ser
divididos entre a esquerda e a direita levando em conta o criterio
(furado!!!) do ISIS.
*/
/* Atualiza headers.
Copia "n_esq" nos da esquerda para temporaria
*/
if (treecase == 0) {
ock=pesq1->ock;
ptmp_esq1->ock=n_esq;
ptmp_esq1->pos=pesq1->pos;
ptmp_esq1->ps= pesq1->ps;
#if LIND
ptmp_esq1->psb= pesq1->psb;
#endif /* LIND */
ptmp_esq1->it= pesq1->it;
ptmp_dir1->ock=n_dir;
ptmp_dir1->pos=pdir1->pos;
ptmp_dir1->ps= pdir1->ps;
#if LIND
ptmp_dir1->psb= pdir1->psb;
#endif /* LIND */
ptmp_dir1->it= pdir1->it;
}else {
ock=pesq2->ock;
ptmp_esq2->ock=n_esq;
ptmp_esq2->pos=pesq2->pos;
ptmp_esq2->ps= pesq2->ps;
#if LIND
ptmp_esq2->psb= pesq2->psb;
#endif /* LIND */
ptmp_esq2->it= pesq2->it;
ptmp_dir2->ock=n_dir;
ptmp_dir2->pos=pdir2->pos;
ptmp_dir2->ps= pdir2->ps;
#if LIND
ptmp_dir2->psb= pdir2->psb;
#endif /* LIND */
ptmp_dir2->it= pdir2->it;
}
for (nn=0;nn<n_esq;nn++){
if (treecase==0) {
ptmp_esq1->idx[nn] = pesq1->idx[nn];
}else {
ptmp_esq2->idx[nn] = pesq2->idx[nn];
}
}
/* O resto das chaves sao copiados para o no da direita */
id=0;
for (;nn<ock;nn++){
if (treecase==0) {
ptmp_dir1->idx[id]=pesq1->idx[nn];
}else {
ptmp_dir2->idx[id]=pesq2->idx[nn];
}
id++;
}
/* Acrescenta todas as chaves da direita */
if (treecase == 0)
ock=pdir1->ock;
else
ock=pdir2->ock;
for (nn=0; nn<ock;nn++){
if (treecase==0) {
ptmp_dir1->idx[id]=pdir1->idx[nn];
} else {
ptmp_dir2->idx[id]=pdir2->idx[nn];
}
id++;
}
/* A primeira chave da direita deve ser promovida para o pai
Deve substituir a chave mais a direita do pai
*/
if (treecase == 0){
id=ptmp_pai1->ock-1;/* Ultimo elemento */
memmove(ptmp_pai1->idx[id].key,ptmp_dir1->idx[0].key,keysize);
/* O punt nao precisa mudar. E o mesmo */
}
else {
id=ptmp_pai2->ock-1;/* Ultimo elemento */
memmove(ptmp_pai2->idx[id].key,ptmp_dir2->idx[0].key,keysize);
}
if (treecase==0) {
n0p=(N0STRU *)ptmp_pai1;
pesq0=(L0STRU *)ptmp_esq1;
pdir0=(L0STRU *)ptmp_dir1;
}else {
n0p=(N0STRU *)ptmp_pai2;
pesq0=(L0STRU *)ptmp_esq2;
pdir0=(L0STRU *)ptmp_dir2;
}
/* Como a arvore ja foi gravada correntamente, os valores de level e
isroot nao tem mais sentido. Estao sendo passados somente para
poder usar as rotinas ja prontas
*/
isroot=FALSE;
#if TSTBALANCE
printf("\n Depois de balancear ");
upif_print_node (n0p);
upif_print_leaf (pesq0);
upif_print_leaf (pdir0);
#endif
nodewrit(dbxp,n0p,invp->cn[treecase].liv,isroot);
leafwrit(dbxp,pesq0);
leafwrit(dbxp,pdir0);
}
/* ------------------------------------------------------------------------*/
#if CICPP
void CIIFL :: balance_nodes(DBXSTRU *dbxp,
INVMAP *invp,
int treecase,
int level,
PUNT idxpenultimo,
PUNT idxultimo,
N0STRU *np)
#else /*CICPP*/
static void balance_nodes(dbxp,invp,treecase,level,idxpenultimo,idxultimo,np)
DBXSTRU *dbxp;
INVMAP *invp;
int treecase;
int level;
PUNT idxpenultimo;
PUNT idxultimo;
N0STRU *np;
#endif /*CICPP*/
{
static N0STRU *n0p,*pesq0,*pdir0;
static N1STRU tmp_esq1,tmp_dir1,
*ptmp_pai1,*ptmp_esq1,*ptmp_dir1,
*pesq1,*pdir1,esq1,dir1;
static N2STRU tmp_esq2,tmp_dir2,
*ptmp_pai2,*ptmp_esq2,*ptmp_dir2,
*pesq2,*pdir2,esq2,dir2;
static UCHR *char_pesq,
*char_pdir;
int n_esq,n_dir,ock,id,nn;
keysize=vlex[treecase];
if (treecase==0) {
pesq1= &esq1;
pdir1= &dir1;
char_pesq=(UCHR *)noderead(invp,treecase,level,idxpenultimo);
memmove((UCHR *)pesq1,char_pesq,sizeof(N1STRU));
char_pdir=(UCHR *)noderead(invp,treecase,level,idxultimo);
memmove((UCHR *)pdir1,char_pdir,sizeof(N1STRU));
}else {
pesq2= &esq2;
pdir2= &dir2;
char_pesq=(UCHR *)noderead(invp,treecase,level,idxpenultimo);
memmove((UCHR *)pesq2,char_pesq,sizeof(N2STRU));
char_pdir=(UCHR *)noderead(invp,treecase,level,idxultimo);
memmove((UCHR *)pdir2,char_pdir,sizeof(N2STRU));
}
if (treecase==0) {
ptmp_pai1=(N1STRU *)np ;
#if TSTBALANCE
printf("\n Antes de balancear ");
upif_print_node ((N0STRU *)ptmp_pai1);
upif_print_node ((N0STRU *)pesq1);
upif_print_node ((N0STRU *)pdir1);
#endif
determina_nova_qtda(ORDN,pesq1->ock,pdir1->ock,&n_esq,&n_dir);
ptmp_esq1= &tmp_esq1;
ptmp_dir1= &tmp_dir1;
lifp_init_node((N0STRU *)ptmp_esq1,treecase);
lifp_init_node((N0STRU *)ptmp_dir1,treecase);
}else {
ptmp_pai2=(N2STRU *)np ;
#if TSTBALANCE
printf("\n Antes de balancear ");
upif_print_node ((N0STRU *)ptmp_pai2);
upif_print_node ((N0STRU *)pesq2);
upif_print_node ((N0STRU *)pdir2);
#endif
determina_nova_qtda(ORDN,pesq2->ock,pdir2->ock,&n_esq,&n_dir);
ptmp_esq2= &tmp_esq2;
ptmp_dir2= &tmp_dir2;
lifp_init_node((N0STRU *)ptmp_esq2,treecase);
lifp_init_node((N0STRU *)ptmp_dir2,treecase);
}
/* Da forma que o algoritmo de load funciona, a celula da esquerda
contera sempre o numero maximo de chaves . Eles deverao ser
divididos entre a esquerda e a direita levando em conta o criterio
(furado!!!) do ISIS.
*/
/* Atualiza headers.
Copia "n_esq: nos da esquerda para temporaria*/
if (treecase == 0) {
ock=pesq1->ock;
ptmp_esq1->ock=n_esq;
ptmp_esq1->pos=pesq1->pos;
ptmp_esq1->it=pesq1->it;
ptmp_dir1->ock=n_dir;
ptmp_dir1->pos=pdir1->pos;
ptmp_dir1->it=pdir1->it;
}else {
ock=pesq2->ock;
ptmp_esq2->ock=n_esq;
ptmp_esq2->pos=pesq2->pos;
ptmp_esq2->it=pesq2->it;
ptmp_dir2->ock=n_dir;
ptmp_dir2->pos=pdir2->pos;
ptmp_dir2->it=pdir2->it;
}
for (nn=0;nn<n_esq;nn++){
if (treecase==0) {
ptmp_esq1->idx[nn] = pesq1->idx[nn];
}else {
ptmp_esq2->idx[nn] = pesq2->idx[nn];
}
}
/* O resto das chaves sao copiados para o no da direita*/
id=0;
for (;nn<ock;nn++){
if (treecase==0) {
ptmp_dir1->idx[id]=pesq1->idx[nn];
}else {
ptmp_dir2->idx[id]=pesq2->idx[nn];
}
id++;
}
/* Acrescenta todas as chaves da direita */
if (treecase == 0)
ock=pdir1->ock;
else
ock=pdir2->ock;
for (nn=0; nn<ock;nn++){
if (treecase==0) {
ptmp_dir1->idx[id]=pdir1->idx[nn];
} else {
ptmp_dir2->idx[id]=pdir2->idx[nn];
}
id++;
}
/* A primeira chave da direita deve ser promovida para o pai
Deve substituir a chave mais a direita do pai
*/
if (treecase == 0){
id=ptmp_pai1->ock-1;/* Ultimo elemento */
memmove(ptmp_pai1->idx[id].key,ptmp_dir1->idx[0].key,keysize);
/* O punt nao precisa mudar. E o mesmo */
}
else {
id=ptmp_pai2->ock-1;/* Ultimo elemento */
memmove(ptmp_pai2->idx[id].key,ptmp_dir2->idx[0].key,keysize);
}
if (treecase==0) {
n0p=(N0STRU *)ptmp_pai1;
pesq0=(N0STRU *)ptmp_esq1;
pdir0=(N0STRU *)ptmp_dir1;
}else {
n0p=(N0STRU *)ptmp_pai2;
pesq0=(N0STRU *)ptmp_esq2;
pdir0=(N0STRU *)ptmp_dir2;
}
/* Como a arvore ja foi gravada correntamente, os valores de level e
isroot nao tem mais sentido. Estao sendo passados somente para
poder usar as rotinas ja prontas
*/
isroot=FALSE;
#if TSTBALANCE
printf("\n Apos de balancear ");
upif_print_node (n0p);
upif_print_node (pesq0);
upif_print_node (pdir0);
#endif
nodewrit(dbxp,n0p,invp->cn[treecase].liv,isroot);
nodewrit(dbxp,pesq0,invp->cn[treecase].liv,isroot);
nodewrit(dbxp,pdir0,invp->cn[treecase].liv,isroot);
}
/* -----------------------------------------------------------------------*/
#if CICPP
void CIIFL :: balance_btree(DBXSTRU *dbxp,
INVMAP *invp,
int level,
PUNT punt,
int treecase)
#else /*CICPP*/
static void balance_btree(dbxp,invp,level,punt,treecase)
DBXSTRU *dbxp;
INVMAP *invp;
int level;
PUNT punt;
int treecase;
#endif /*CICPP*/
{
UCHR *np;
static N2STRU node2,area_node2;
static N1STRU node1,area_node1;
N2STRU *n2p, *keep_node2;
N1STRU *n1p, *keep_node1;
N0STRU *n0local;
int ock;
PUNT idxultimo,idxpenultimo;
keep_node1= &area_node1;
keep_node2= &area_node2;
if (punt==0) return;
if (punt>0) {
idxultimo=1; /* so para inicializar while */
while (idxultimo > 0) {
np=(UCHR *)noderead(invp,treecase,level,punt);
if (treecase==0){
memmove((UCHR *)&node1,np,sizeof(N1STRU));
np=(UCHR *)&node1;
}
else {
memmove((UCHR *)&node2,np,sizeof(N2STRU));
np=(UCHR *)&node2;
}
n1p=(N1STRU *)np;
n2p=(N2STRU *)np;
n0p=(N0STRU *)np;
ock=n0p->ock;
if(ock <= 1) return; /* nada a reestruturar */
if (treecase == 0) {
idxpenultimo=n1p->idx[ock-2].punt;
idxultimo=n1p->idx[ock-1].punt;
memmove(keep_node1,np,sizeof(N1STRU));
n0local=(N0STRU *)keep_node1;
} else {
idxpenultimo=n2p->idx[ock-2].punt;
idxultimo=n2p->idx[ock-1].punt;
memmove(keep_node2,np,sizeof(N2STRU));
n0local=(N0STRU *)keep_node2;
}
if (idxpenultimo < 0) { /* aponta para folha */
balance_leaves(dbxp,invp,treecase,
idxpenultimo,idxultimo, n0local);
} else { /* aponta para no */
balance_nodes(dbxp,invp,treecase,level,idxpenultimo,idxultimo,
n0local);
punt=idxultimo;
}
}
}/* WHILE*/
}
#if !LIND
/*------------------------------------------------------------------------*/
#if CICPP
void CIIFL :: lifp_upd_first_rec(IFPAVAILPOS *pn,
INFO next_blk,
INFO next_pos)
#else /*CICPP*/
static void lifp_upd_first_rec(pn,next_blk,next_pos)
IFPAVAILPOS *pn;
INFO next_blk;
INFO next_pos;
#endif /*CICPP*/
{
pn->ifpblk=1;
pn->nxtb=next_blk;
pn->nxtp=next_pos;
}
/*------------------------------------------------------------------------*/
#if CICPP
void CIIFL :: lifp_init_ifprec(IFPSTRU *p,
INFO blk)
#else /*CICPP*/
static void lifp_init_ifprec(p,blk)
IFPSTRU *p;
INFO blk;
#endif /*CICPP*/
{ int i;
p->ifpblk=blk;
for (i=0;i<IFMAXTIV;i++){
p->ifprec[i]=0;
}
}
/*------------------------------------------------------------------------*/
#if CICPP
void CIIFL :: lifp_init_buff(IFPBUFFER *q,
INFO blk)
#else /*CICPP*/
static void lifp_init_buff(q,blk)
IFPBUFFER *q;
INFO blk;
#endif /*CICPP*/
{ int i;
IFPSTRU *p;
for (i=0;i<MAXIFPMEM;i++){
p=(IFPSTRU *)&q->buff[i];
lifp_init_ifprec(p,blk);
blk++;
}
}
#endif /* !LIND */
/*------------------------------------------------------------------------*/
#if CICPP
IFPBUFFER *CIIFL ::lifp_init(INVMAP *invp)
#else /*CICPP*/
static IFPBUFFER *lifp_init(invp)
INVMAP *invp;
#endif /*CICPP*/
{
IFPBUFFER *ifpb;
#if !LIND
next_blk=(INFO )1;
next_pos=(INFO )2;
#endif /* !LIND */
#if CICPP
try { ifpb = (IFPBUFFER *) new char [sizeof(IFPBUFFER)]; }
catch (BAD_ALLOC) { ifpb = (IFPBUFFER *)ALLONULL; }
#else /* CICPP */
ifpb = (IFPBUFFER *)ALLOC(sizeof(IFPBUFFER));
#endif /* CICPP */
if (ifpb == (IFPBUFFER *)ALLONULL) {
#if CICPP
return(NULL);
#else /* CICPP */
fatal("ALLOC/lifp_init");
#endif /* CICPP */
}
#if !LIND
pn.ifpblk=1;
#if LKXONLY
if (lkxonly & 2) {
postread((UCHR *)ifpb,invp,1L,0);
next_blk=((IFPCTRL *)ifpb)->ifprec1;
next_pos=((IFPCTRL *)ifpb)->ifprec2;
pn.ifpblk=next_blk;
}
#endif
pn.nxtb=next_blk;
pn.nxtp=next_pos;
lifp_init_buff(ifpb,next_blk);
flag_first_seghead=FALSE;
flag_current_seghead=FALSE;
#endif /* !LIND */
key_ant[0]='\0';
treecase_ant=0; /* Sindo, esta' ok para LKXONLY? Thanks, AOT */
#if !LIND
pf=(IFPSTRU *)&first_seghead;
#endif /* !LIND */
key[0]='\0';
#if !LIND
totp=0;
nxtb=0;
nxtp=0;
segp=0;
segc=0;
idxpst=next_pos;
gbuf=0;
ph= &ifpb->buff[gbuf];
off_h=idxpst;
pb=(IFPSTRU *)&ifpb->buff[gbuf];
pb->ifprec[0]=next_blk;
pb->ifprec[1]=next_pos;
memset((char *)&pst_ant,0x00,sizeof(POSTSTRU));
#endif /* !LIND */
return(ifpb);
}
#if !LIND
/*-----------------------------------------------------------------------*/
#if CICPP
void CIIFL :: write_buffers(BOOLEAN keep_head,
BOOLEAN reestrut_buff)
#else /*CICPP*/
static void write_buffers( keep_head,reestrut_buff)
BOOLEAN keep_head;
BOOLEAN reestrut_buff;
#endif /*CICPP*/
{
IFPSTRU tmp;
#if CNV_PCBINUM
ifpwrit(dbxp,(char *)ifpbuff,(LONGX )sizeof(IFPBUFFER),hdblk,hdoff,hdn);
ifp_init_hd(&hdn);
#else /* CNV_PCBINUM */
ifpwrit(dbxp,(char *)ifpbuff,(LONGX )sizeof(IFPBUFFER));
#endif /* CNV_PCBINUM */
if (keep_head==TRUE && flag_current_seghead==FALSE) {
memmove((UCHR *)¤t_seghead,(UCHR *)ph,sizeof(IFPSTRU));
ph= ¤t_seghead;
/* O header corrente foi convertido para formato UNIX. Como
vai continuar em memoria, precisa voltar para DOS
*/
#if TRACE_HEADER
printf("Convertendo para dos blk=%"_LD_" off=%"_LD_" \n",current_seghead.ifpblk,off_h);
#endif
flag_current_seghead=TRUE;
}
if (reestrut_buff==TRUE ){
memmove((UCHR *)&tmp,(UCHR *)&ifpbuff->buff[gbuf],sizeof(IFPSTRU));
next_blk=tmp.ifpblk;
lifp_init_buff(ifpbuff,next_blk);
memmove((UCHR *)&ifpbuff->buff[0],(UCHR *)&tmp,sizeof(IFPSTRU));
gbuf=0;
pb=(IFPSTRU *)&ifpbuff->buff[gbuf];
}
if (keep_head==TRUE)gbuf=0;
}
/*-----------------------------------------------------------------------*/
#if CICPP
void CIIFL :: get_room_for (int qt,
BOOLEAN opt_keep_head)
#else /*CICPP*/
static void get_room_for (qt,opt_keep_head)
int qt;
BOOLEAN opt_keep_head;
#endif /*CICPP*/
{
if (idxpst+qt-1>MAXPSTS+1 ) {
if (BUFFER_FULL) {
write_buffers(opt_keep_head,TRUE);
gbuf++;
}else {
gbuf++;
}
next_blk++;
idxpst=0;
pb=(IFPSTRU *)&ifpbuff->buff[gbuf];
}
}
/*-----------------------------------------------------------------------*/
#if CICPP
void CIIFL :: lifp_upd_pst_header(IFPSTRU *p,
INFO offset,
INFO nxtb,
INFO nxtp,
INFO totp,
INFO segp,
INFO segc)
#else /*CICPP*/
static void lifp_upd_pst_header(p, offset,nxtb,nxtp,totp,segp,segc)
IFPSTRU *p;
INFO offset;
INFO nxtb,nxtp,totp,segp,segc;
#endif /*CICPP*/
{
IFPHEAD *head;
head=(IFPHEAD *)&p->ifprec[offset];
head->ifpnxtb=nxtb;
head->ifpnxtp=nxtp;
head->ifptotp=totp;
head->ifpsegp=segp;
head->ifpsegc=segc;
#if CNV_PCBINUM
ifp_ins_new_hd(p->ifpblk, offset,hdblk,hdoff, IFLmaxhd,&hdn);
#endif
}
/*-----------------------------------------------------------------------*/
#if CICPP
void CIIFL :: lifp_init_counters()
#else /*CICPP*/
static void lifp_init_counters()
#endif /*CICPP*/
{
nxtb=0;
nxtp=0;
segc=0;
segp=0;
totp=0;
npst=0;
}
#endif /* !LIND */
/*------------------------------------------------------------------------*/
#if CICPP
void CIIFL :: init_invp_roots_nodes_leaves(INVMAP *invp)
#else /*CICPP*/
static void init_invp_roots_nodes_leaves(invp)
INVMAP *invp;
#endif /*CICPP*/
{
L0STRU *l0p;
N0STRU *n0p;
char keytmp[1];/* Somente para chamar create_root */
int treecase;
PUNT esq,dir;
keytmp[0]='\0';
/* Inicializa elementos "brancos" para os dois tipos de nos e
folhas
*/
memset(init_leaf_el1.key,' ',LE1);
init_leaf_el1.info1=(INFO )0;