1782{
1783 GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
1784 GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
1785 OffsetNumber i,
1786 maxoff;
1788 BOX2DF *leftBox, *rightBox;
1789 int dim,
1790 commonEntriesCount;
1792 *intervalsUpper;
1794 int nentries;
1795
1796 POSTGIS_DEBUG(3, "[GIST] 'picksplit' entered");
1797
1799
1800 maxoff = entryvec->n - 1;
1801 nentries = context.
entriesCount = maxoff - FirstOffsetNumber + 1;
1802
1803
1806
1807
1808
1809
1810 for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
1811 {
1812 BOX2DF *box = (BOX2DF *)DatumGetPointer(entryvec->vector[i].key);
1813 if (i == FirstOffsetNumber)
1815 else
1817 }
1818
1821
1822
1823
1824
1825 context.
first =
true;
1826 for (dim = 0; dim < 2; dim++)
1827 {
1828 float leftUpper,
1829 rightLower;
1830 int i1,
1831 i2;
1832
1833
1834 for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
1835 {
1836 BOX2DF *box = (BOX2DF *)DatumGetPointer(entryvec->vector[i].key);
1837 if (dim == 0)
1838 {
1839 intervalsLower[i - FirstOffsetNumber].
lower = box->xmin;
1840 intervalsLower[i - FirstOffsetNumber].
upper = box->xmax;
1841 }
1842 else
1843 {
1844 intervalsLower[i - FirstOffsetNumber].
lower = box->ymin;
1845 intervalsLower[i - FirstOffsetNumber].
upper = box->ymax;
1846 }
1847 }
1848
1849
1850
1851
1852
1853 memcpy(intervalsUpper, intervalsLower,
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894 i1 = 0;
1895 i2 = 0;
1896 rightLower = intervalsLower[i1].
lower;
1897 leftUpper = intervalsUpper[i2].
lower;
1898 while (true)
1899 {
1900
1901
1902
1903 while (i1 < nentries && (rightLower == intervalsLower[i1].lower ||
1904 isnan(intervalsLower[i1].lower)))
1905 {
1906 leftUpper = Max(leftUpper, intervalsLower[i1].upper);
1907 i1++;
1908 }
1909 if (i1 >= nentries)
1910 break;
1911 rightLower = intervalsLower[i1].
lower;
1912
1913
1914
1915
1916
1917 while (i2 < nentries && intervalsUpper[i2].upper <= leftUpper)
1918 i2++;
1919
1920
1921
1922
1924 }
1925
1926
1927
1928
1929
1930 i1 = nentries - 1;
1931 i2 = nentries - 1;
1932 rightLower = intervalsLower[i1].
upper;
1933 leftUpper = intervalsUpper[i2].
upper;
1934 while (true)
1935 {
1936
1937
1938
1939 while (i2 >= 0 && (leftUpper == intervalsUpper[i2].upper ||
1940 isnan(intervalsUpper[i2].upper)))
1941 {
1942 rightLower = Min(rightLower, intervalsUpper[i2].lower);
1943 i2--;
1944 }
1945 if (i2 < 0)
1946 break;
1947 leftUpper = intervalsUpper[i2].
upper;
1948
1949
1950
1951
1952
1953 while (i1 >= 0 && intervalsLower[i1].lower >= rightLower)
1954 i1--;
1955
1956
1957
1958
1960 rightLower, i1 + 1, leftUpper, i2 + 1);
1961 }
1962 }
1963
1964
1965
1966
1968 {
1969 POSTGIS_DEBUG(4, "no acceptable splits, trivial split");
1971 PG_RETURN_POINTER(v);
1972 }
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982 POSTGIS_DEBUGF(4,
"split direction: %d", context.
dim);
1983
1984
1985 v->spl_left = (OffsetNumber *) palloc(nentries * sizeof(OffsetNumber));
1986 v->spl_right = (OffsetNumber *) palloc(nentries * sizeof(OffsetNumber));
1987 v->spl_nleft = 0;
1988 v->spl_nright = 0;
1989
1990
1991 leftBox = palloc0(sizeof(BOX2DF));
1992 rightBox = palloc0(sizeof(BOX2DF));
1993
1994
1995
1996
1997
1998 commonEntriesCount = 0;
2000
2001
2002#define PLACE_LEFT(box, off) \
2003 do { \
2004 if (v->spl_nleft > 0) \
2005 adjustBox(leftBox, box); \
2006 else \
2007 *leftBox = *(box); \
2008 v->spl_left[v->spl_nleft++] = off; \
2009 } while(0)
2010
2011#define PLACE_RIGHT(box, off) \
2012 do { \
2013 if (v->spl_nright > 0) \
2014 adjustBox(rightBox, box); \
2015 else \
2016 *rightBox = *(box); \
2017 v->spl_right[v->spl_nright++] = off; \
2018 } while(0)
2019
2020
2021
2022
2023
2024 for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
2025 {
2026 float lower,
2027 upper;
2028
2029
2030
2031
2032 BOX2DF *box = (BOX2DF *)DatumGetPointer(entryvec->vector[i].key);
2033 if (context.
dim == 0)
2034 {
2035 lower = box->xmin;
2036 upper = box->xmax;
2037 }
2038 else
2039 {
2040 lower = box->ymin;
2041 upper = box->ymax;
2042 }
2043
2044 if (upper <= context.
leftUpper || isnan(upper))
2045 {
2046
2047 if (lower >= context.
rightLower || isnan(lower))
2048 {
2049
2050 commonEntries[commonEntriesCount++].
index = i;
2051 }
2052 else
2053 {
2054
2056 }
2057 }
2058 else
2059 {
2060
2061
2062
2063
2064
2066
2067
2069 }
2070 }
2071
2074
2075
2076
2077
2078 if (commonEntriesCount > 0)
2079 {
2080
2082
2083
2086
2087 Assert(m >= 1);
2088
2089 for (i = 0; i < (OffsetNumber)commonEntriesCount; i++)
2090 {
2091 BOX2DF *box = (BOX2DF *)DatumGetPointer(entryvec->vector[commonEntries[i].index].key);
2092
2093 if (v->spl_nright > 0 && v->spl_nleft > 0)
2094 {
2095
2096 commonEntries[i].
delta =
2098 }
2099 else
2100 {
2101
2103 }
2104 }
2105
2106
2108
2109
2110 for (i = 0; i < (OffsetNumber)commonEntriesCount; i++)
2111 {
2112 bool place_right = true;
2113 BOX2DF *box = (BOX2DF *)DatumGetPointer(entryvec->vector[commonEntries[i].index].key);
2114
2115
2118
2119
2120 if (v->spl_nright == 0 && v->spl_nleft > 0)
2121 place_right = true;
2122 else if (v->spl_nleft == 0 && v->spl_nright > 0)
2123 place_right = false;
2124
2125 else if (v->spl_nleft + (commonEntriesCount - i) <= m)
2126 place_right = false;
2127 else if (v->spl_nright + (commonEntriesCount - i) <= m)
2128 place_right = true;
2129
2130 else if (left_penalty < right_penalty)
2131 place_right = false;
2132 else if (right_penalty < left_penalty)
2133 place_right = true;
2134
2135 else if (v->spl_nleft < v->spl_nright)
2136 place_right = false;
2137 else
2138 place_right = true;
2139
2140 if (place_right)
2142 else
2144 }
2145 }
2146 v->spl_ldatum = PointerGetDatum(leftBox);
2147 v->spl_rdatum = PointerGetDatum(rightBox);
2148
2149 POSTGIS_DEBUG(4, "[GIST] 'picksplit' completed");
2150
2151 PG_RETURN_POINTER(v);
2152}
static void fallbackSplit(GistEntryVector *entryvec, GIST_SPLITVEC *v)
static char * box2df_to_string(const BOX2DF *a)
#define INDEX_TUPLES_PER_PAGE
static int interval_cmp_upper(const void *i1, const void *i2)
#define PLACE_RIGHT(box, off)
static float box2df_penalty_single(const BOX2DF *box)
static void g_box_consider_split(ConsiderSplitContext *context, int dimNum, float rightLower, int minLeftCount, float leftUpper, int maxLeftCount)
static int interval_cmp_lower(const void *i1, const void *i2)
static int common_entry_cmp(const void *i1, const void *i2)
#define PLACE_LEFT(box, off)
static float box2df_penalty(const BOX2DF *b1, const BOX2DF *b2)
static void adjustBox(BOX2DF *b, BOX2DF *addon)