| 1944 | |
| 1945 | |
| 1946 | void Sort::orderAndSave(thread_db* tdbb) |
| 1947 | { |
| 1948 | /************************************** |
| 1949 | * |
| 1950 | * The memory full of record pointers has been sorted, but more |
| 1951 | * records remain, so the run will have to be written to scratch file. |
| 1952 | * If target run can be allocated in contiguous chunk of memory then |
| 1953 | * just memcpy records into it. Else call more expensive order() to |
| 1954 | * physically rearrange records in sort space and write its run into |
| 1955 | * scratch file as one big chunk |
| 1956 | * |
| 1957 | **************************************/ |
| 1958 | EngineCheckout cout(tdbb, FB_FUNCTION); |
| 1959 | |
| 1960 | run_control* run = m_runs; |
| 1961 | run->run_records = 0; |
| 1962 | |
| 1963 | sort_record** ptr = m_first_pointer + 1; // 1st ptr is low key |
| 1964 | // m_next_pointer points to the end of pointer memory or the beginning of records |
| 1965 | while (ptr < m_next_pointer) |
| 1966 | { |
| 1967 | // If the next pointer is null, it's record has been eliminated as a |
| 1968 | // duplicate. This is the only easy case. |
| 1969 | if (!(*ptr++)) |
| 1970 | continue; |
| 1971 | |
| 1972 | run->run_records++; |
| 1973 | } |
| 1974 | |
| 1975 | const ULONG key_length = (m_longs - SIZEOF_SR_BCKPTR_IN_LONGS) * sizeof(ULONG); |
| 1976 | run->run_size = run->run_records * key_length; |
| 1977 | run->run_seek = m_space->allocateSpace(run->run_size); |
| 1978 | |
| 1979 | UCHAR* mem = m_space->inMemory(run->run_seek, run->run_size); |
| 1980 | |
| 1981 | if (mem) |
| 1982 | { |
| 1983 | ptr = m_first_pointer + 1; |
| 1984 | while (ptr < m_next_pointer) |
| 1985 | { |
| 1986 | SR* record = (SR*) (*ptr++); |
| 1987 | |
| 1988 | if (!record) |
| 1989 | continue; |
| 1990 | |
| 1991 | // make record point back to the starting of SR struct. |
| 1992 | // as all m_*_pointer point to the key_id locations! |
| 1993 | record = (SR*) (((SORTP*)record) - SIZEOF_SR_BCKPTR_IN_LONGS); |
| 1994 | |
| 1995 | memcpy(mem, record->sr_sort_record.sort_record_key, key_length); |
| 1996 | mem += key_length; |
| 1997 | } |
| 1998 | } |
| 1999 | else |
| 2000 | { |
| 2001 | order(); |
| 2002 | writeBlock(m_space, run->run_seek, (UCHAR*) m_last_record, run->run_size); |
| 2003 | } |
nothing calls this directly
no test coverage detected