11
11
*
12
12
*
13
13
* IDENTIFICATION
14
- * $PostgreSQL: pgsql/src/bin/pg_dump/common.c,v 1.97 2007/08/21 01:11:20 tgl Exp $
14
+ * $PostgreSQL: pgsql/src/bin/pg_dump/common.c,v 1.98 2007/09/23 23:39:36 tgl Exp $
15
15
*
16
16
*-------------------------------------------------------------------------
17
17
*/
@@ -41,7 +41,11 @@ static int numCatalogIds = 0;
41
41
42
42
/*
43
43
* These variables are static to avoid the notational cruft of having to pass
44
- * them into findTableByOid() and friends.
44
+ * them into findTableByOid() and friends. For each of these arrays, we
45
+ * build a sorted-by-OID index array immediately after it's built, and then
46
+ * we use binary search in findTableByOid() and friends. (qsort'ing the base
47
+ * arrays themselves would be simpler, but it doesn't work because pg_dump.c
48
+ * may have already established pointers between items.)
45
49
*/
46
50
static TableInfo * tblinfo ;
47
51
static TypeInfo * typinfo ;
@@ -51,12 +55,18 @@ static int numTables;
51
55
static int numTypes ;
52
56
static int numFuncs ;
53
57
static int numOperators ;
58
+ static DumpableObject * * tblinfoindex ;
59
+ static DumpableObject * * typinfoindex ;
60
+ static DumpableObject * * funinfoindex ;
61
+ static DumpableObject * * oprinfoindex ;
54
62
55
63
56
64
static void flagInhTables (TableInfo * tbinfo , int numTables ,
57
65
InhInfo * inhinfo , int numInherits );
58
66
static void flagInhAttrs (TableInfo * tbinfo , int numTables ,
59
67
InhInfo * inhinfo , int numInherits );
68
+ static DumpableObject * * buildIndexArray (void * objArray , int numObjs ,
69
+ Size objSize );
60
70
static int DOCatalogIdCompare (const void * p1 , const void * p2 );
61
71
static void findParentsByOid (TableInfo * self ,
62
72
InhInfo * inhinfo , int numInherits );
@@ -104,11 +114,13 @@ getSchemaData(int *numTablesPtr)
104
114
if (g_verbose )
105
115
write_msg (NULL , "reading user-defined functions\n" );
106
116
funinfo = getFuncs (& numFuncs );
117
+ funinfoindex = buildIndexArray (funinfo , numFuncs , sizeof (FuncInfo ));
107
118
108
119
/* this must be after getFuncs */
109
120
if (g_verbose )
110
121
write_msg (NULL , "reading user-defined types\n" );
111
122
typinfo = getTypes (& numTypes );
123
+ typinfoindex = buildIndexArray (typinfo , numTypes , sizeof (TypeInfo ));
112
124
113
125
/* this must be after getFuncs, too */
114
126
if (g_verbose )
@@ -122,6 +134,7 @@ getSchemaData(int *numTablesPtr)
122
134
if (g_verbose )
123
135
write_msg (NULL , "reading user-defined operators\n" );
124
136
oprinfo = getOperators (& numOperators );
137
+ oprinfoindex = buildIndexArray (oprinfo , numOperators , sizeof (OprInfo ));
125
138
126
139
if (g_verbose )
127
140
write_msg (NULL , "reading user-defined operator classes\n" );
@@ -154,6 +167,7 @@ getSchemaData(int *numTablesPtr)
154
167
if (g_verbose )
155
168
write_msg (NULL , "reading user-defined tables\n" );
156
169
tblinfo = getTables (& numTables );
170
+ tblinfoindex = buildIndexArray (tblinfo , numTables , sizeof (TableInfo ));
157
171
158
172
if (g_verbose )
159
173
write_msg (NULL , "reading table inheritance information\n" );
@@ -540,6 +554,70 @@ findObjectByCatalogId(CatalogId catalogId)
540
554
return NULL ;
541
555
}
542
556
557
+ /*
558
+ * Find a DumpableObject by OID, in a pre-sorted array of one type of object
559
+ *
560
+ * Returns NULL for unknown OID
561
+ */
562
+ static DumpableObject *
563
+ findObjectByOid (Oid oid , DumpableObject * * indexArray , int numObjs )
564
+ {
565
+ DumpableObject * * low ;
566
+ DumpableObject * * high ;
567
+
568
+ /*
569
+ * This is the same as findObjectByCatalogId except we assume we need
570
+ * not look at table OID because the objects are all the same type.
571
+ *
572
+ * We could use bsearch() here, but the notational cruft of calling
573
+ * bsearch is nearly as bad as doing it ourselves; and the generalized
574
+ * bsearch function is noticeably slower as well.
575
+ */
576
+ if (numObjs <= 0 )
577
+ return NULL ;
578
+ low = indexArray ;
579
+ high = indexArray + (numObjs - 1 );
580
+ while (low <= high )
581
+ {
582
+ DumpableObject * * middle ;
583
+ int difference ;
584
+
585
+ middle = low + (high - low ) / 2 ;
586
+ difference = oidcmp ((* middle )-> catId .oid , oid );
587
+ if (difference == 0 )
588
+ return * middle ;
589
+ else if (difference < 0 )
590
+ low = middle + 1 ;
591
+ else
592
+ high = middle - 1 ;
593
+ }
594
+ return NULL ;
595
+ }
596
+
597
+ /*
598
+ * Build an index array of DumpableObject pointers, sorted by OID
599
+ */
600
+ static DumpableObject * *
601
+ buildIndexArray (void * objArray , int numObjs , Size objSize )
602
+ {
603
+ DumpableObject * * ptrs ;
604
+ int i ;
605
+
606
+ ptrs = (DumpableObject * * ) malloc (numObjs * sizeof (DumpableObject * ));
607
+ for (i = 0 ; i < numObjs ; i ++ )
608
+ ptrs [i ] = (DumpableObject * ) ((char * ) objArray + i * objSize );
609
+
610
+ /* We can use DOCatalogIdCompare to sort since its first key is OID */
611
+ if (numObjs > 1 )
612
+ qsort ((void * ) ptrs , numObjs , sizeof (DumpableObject * ),
613
+ DOCatalogIdCompare );
614
+
615
+ return ptrs ;
616
+ }
617
+
618
+ /*
619
+ * qsort comparator for pointers to DumpableObjects
620
+ */
543
621
static int
544
622
DOCatalogIdCompare (const void * p1 , const void * p2 )
545
623
{
@@ -630,80 +708,44 @@ removeObjectDependency(DumpableObject *dobj, DumpId refId)
630
708
* findTableByOid
631
709
* finds the entry (in tblinfo) of the table with the given oid
632
710
* returns NULL if not found
633
- *
634
- * NOTE: should hash this, but just do linear search for now
635
711
*/
636
712
TableInfo *
637
713
findTableByOid (Oid oid )
638
714
{
639
- int i ;
640
-
641
- for (i = 0 ; i < numTables ; i ++ )
642
- {
643
- if (tblinfo [i ].dobj .catId .oid == oid )
644
- return & tblinfo [i ];
645
- }
646
- return NULL ;
715
+ return (TableInfo * ) findObjectByOid (oid , tblinfoindex , numTables );
647
716
}
648
717
649
718
/*
650
719
* findTypeByOid
651
720
* finds the entry (in typinfo) of the type with the given oid
652
721
* returns NULL if not found
653
- *
654
- * NOTE: should hash this, but just do linear search for now
655
722
*/
656
723
TypeInfo *
657
724
findTypeByOid (Oid oid )
658
725
{
659
- int i ;
660
-
661
- for (i = 0 ; i < numTypes ; i ++ )
662
- {
663
- if (typinfo [i ].dobj .catId .oid == oid )
664
- return & typinfo [i ];
665
- }
666
- return NULL ;
726
+ return (TypeInfo * ) findObjectByOid (oid , typinfoindex , numTypes );
667
727
}
668
728
669
729
/*
670
730
* findFuncByOid
671
731
* finds the entry (in funinfo) of the function with the given oid
672
732
* returns NULL if not found
673
- *
674
- * NOTE: should hash this, but just do linear search for now
675
733
*/
676
734
FuncInfo *
677
735
findFuncByOid (Oid oid )
678
736
{
679
- int i ;
680
-
681
- for (i = 0 ; i < numFuncs ; i ++ )
682
- {
683
- if (funinfo [i ].dobj .catId .oid == oid )
684
- return & funinfo [i ];
685
- }
686
- return NULL ;
737
+ return (FuncInfo * ) findObjectByOid (oid , funinfoindex , numFuncs );
687
738
}
688
739
689
740
/*
690
741
* findOprByOid
691
742
* finds the entry (in oprinfo) of the operator with the given oid
692
743
* returns NULL if not found
693
- *
694
- * NOTE: should hash this, but just do linear search for now
695
744
*/
696
745
OprInfo *
697
746
findOprByOid (Oid oid )
698
747
{
699
- int i ;
700
-
701
- for (i = 0 ; i < numOperators ; i ++ )
702
- {
703
- if (oprinfo [i ].dobj .catId .oid == oid )
704
- return & oprinfo [i ];
705
- }
706
- return NULL ;
748
+ return (OprInfo * ) findObjectByOid (oid , oprinfoindex , numOperators );
707
749
}
708
750
709
751
0 commit comments