fsort.c revision 1.39 1 1.39 dsl /* $NetBSD: fsort.c,v 1.39 2009/08/22 10:53:28 dsl Exp $ */
2 1.26 jdolecek
3 1.26 jdolecek /*-
4 1.26 jdolecek * Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
5 1.26 jdolecek * All rights reserved.
6 1.26 jdolecek *
7 1.26 jdolecek * This code is derived from software contributed to The NetBSD Foundation
8 1.26 jdolecek * by Ben Harris and Jaromir Dolecek.
9 1.26 jdolecek *
10 1.26 jdolecek * Redistribution and use in source and binary forms, with or without
11 1.26 jdolecek * modification, are permitted provided that the following conditions
12 1.26 jdolecek * are met:
13 1.26 jdolecek * 1. Redistributions of source code must retain the above copyright
14 1.26 jdolecek * notice, this list of conditions and the following disclaimer.
15 1.26 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
16 1.26 jdolecek * notice, this list of conditions and the following disclaimer in the
17 1.26 jdolecek * documentation and/or other materials provided with the distribution.
18 1.26 jdolecek *
19 1.26 jdolecek * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.26 jdolecek * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.26 jdolecek * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.26 jdolecek * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.26 jdolecek * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.26 jdolecek * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.26 jdolecek * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.26 jdolecek * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.26 jdolecek * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.26 jdolecek * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.26 jdolecek * POSSIBILITY OF SUCH DAMAGE.
30 1.26 jdolecek */
31 1.2 bjh21
32 1.1 bjh21 /*-
33 1.1 bjh21 * Copyright (c) 1993
34 1.1 bjh21 * The Regents of the University of California. All rights reserved.
35 1.1 bjh21 *
36 1.1 bjh21 * This code is derived from software contributed to Berkeley by
37 1.1 bjh21 * Peter McIlroy.
38 1.1 bjh21 *
39 1.1 bjh21 * Redistribution and use in source and binary forms, with or without
40 1.1 bjh21 * modification, are permitted provided that the following conditions
41 1.1 bjh21 * are met:
42 1.1 bjh21 * 1. Redistributions of source code must retain the above copyright
43 1.1 bjh21 * notice, this list of conditions and the following disclaimer.
44 1.1 bjh21 * 2. Redistributions in binary form must reproduce the above copyright
45 1.1 bjh21 * notice, this list of conditions and the following disclaimer in the
46 1.1 bjh21 * documentation and/or other materials provided with the distribution.
47 1.25 agc * 3. Neither the name of the University nor the names of its contributors
48 1.1 bjh21 * may be used to endorse or promote products derived from this software
49 1.1 bjh21 * without specific prior written permission.
50 1.1 bjh21 *
51 1.1 bjh21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 1.1 bjh21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 1.1 bjh21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 1.1 bjh21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 1.1 bjh21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 1.1 bjh21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 1.1 bjh21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 1.1 bjh21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 1.1 bjh21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 1.1 bjh21 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 1.1 bjh21 * SUCH DAMAGE.
62 1.1 bjh21 */
63 1.1 bjh21
64 1.1 bjh21 /*
65 1.37 dsl * Read in a block of records (until 'enough').
66 1.37 dsl * sort, write to temp file.
67 1.37 dsl * Merge sort temp files into output file
68 1.37 dsl * Small files miss out the temp file stage.
69 1.37 dsl * Large files might get multiple merges.
70 1.16 jdolecek */
71 1.1 bjh21 #include "sort.h"
72 1.1 bjh21 #include "fsort.h"
73 1.1 bjh21
74 1.2 bjh21 #ifndef lint
75 1.39 dsl __RCSID("$NetBSD: fsort.c,v 1.39 2009/08/22 10:53:28 dsl Exp $");
76 1.2 bjh21 __SCCSID("@(#)fsort.c 8.1 (Berkeley) 6/6/93");
77 1.2 bjh21 #endif /* not lint */
78 1.2 bjh21
79 1.1 bjh21 #include <stdlib.h>
80 1.1 bjh21 #include <string.h>
81 1.1 bjh21
82 1.13 jdolecek static const u_char **keylist = 0;
83 1.34 dsl u_char *buffer = 0;
84 1.18 jdolecek size_t bufsize = DEFBUFSIZE;
85 1.1 bjh21
86 1.23 jdolecek struct tempfile fstack[MAXFCT];
87 1.38 dsl
88 1.13 jdolecek #define SALIGN(n) ((n+sizeof(length_t)-1) & ~(sizeof(length_t)-1))
89 1.8 jdolecek
90 1.1 bjh21 void
91 1.38 dsl fsort(struct filelist *filelist, int nfiles, FILE *outfp, struct field *ftbl)
92 1.1 bjh21 {
93 1.38 dsl const u_char **keypos, **keyp;
94 1.30 jdolecek u_char *bufend;
95 1.38 dsl int mfct = 0;
96 1.37 dsl int c, nelem;
97 1.8 jdolecek get_func_t get;
98 1.4 jdolecek struct recheader *crec;
99 1.27 itojun u_char *nbuffer;
100 1.39 dsl FILE *fp;
101 1.1 bjh21
102 1.1 bjh21 if (!buffer) {
103 1.5 jdolecek buffer = malloc(bufsize);
104 1.1 bjh21 keylist = malloc(MAXNUM * sizeof(u_char *));
105 1.12 itojun memset(keylist, 0, MAXNUM * sizeof(u_char *));
106 1.1 bjh21 }
107 1.5 jdolecek bufend = buffer + bufsize;
108 1.38 dsl
109 1.37 dsl if (SINGL_FLD)
110 1.38 dsl /* Key and data are one! */
111 1.37 dsl get = makeline;
112 1.37 dsl else
113 1.38 dsl /* Key (merged key fields) added before data */
114 1.37 dsl get = makekey;
115 1.37 dsl
116 1.38 dsl /* Loop through reads of chunk of input files that get sorted
117 1.38 dsl * and then merged together. */
118 1.38 dsl for (;;) {
119 1.37 dsl keypos = keylist;
120 1.37 dsl nelem = 0;
121 1.37 dsl crec = (RECHEADER *) buffer;
122 1.37 dsl
123 1.38 dsl /* Loop reading records */
124 1.38 dsl for (;;) {
125 1.38 dsl c = get(-1, 0, filelist, nfiles, crec, bufend, ftbl);
126 1.38 dsl /* 'c' is 0, EOF or BUFFEND */
127 1.38 dsl if (c == 0) {
128 1.38 dsl /* Save start of key in input buffer */
129 1.38 dsl *keypos++ = crec->data;
130 1.38 dsl if (++nelem == MAXNUM) {
131 1.38 dsl c = BUFFEND;
132 1.38 dsl break;
133 1.38 dsl }
134 1.38 dsl crec = (RECHEADER *)((char *) crec +
135 1.38 dsl SALIGN(crec->length) + REC_DATA_OFFSET);
136 1.38 dsl continue;
137 1.38 dsl }
138 1.38 dsl if (c == EOF)
139 1.38 dsl break;
140 1.38 dsl if (nelem >= MAXNUM || bufsize >= MAXBUFSIZE)
141 1.38 dsl /* Need to sort and save this lot of data */
142 1.37 dsl break;
143 1.13 jdolecek
144 1.38 dsl /* c == BUFFEND, and we can process more data */
145 1.38 dsl /* Allocate a larger buffer for this lot of data */
146 1.38 dsl bufsize *= 2;
147 1.38 dsl nbuffer = realloc(buffer, bufsize);
148 1.37 dsl if (!nbuffer) {
149 1.38 dsl err(2, "failed to realloc buffer to %zu bytes",
150 1.38 dsl bufsize);
151 1.5 jdolecek }
152 1.19 jdolecek
153 1.37 dsl /* patch up keylist[] */
154 1.37 dsl for (keyp = &keypos[-1]; keyp >= keylist; keyp--)
155 1.38 dsl *keyp = nbuffer + (*keyp - buffer);
156 1.19 jdolecek
157 1.38 dsl crec = (RECHEADER *) (nbuffer + ((u_char *)crec - buffer));
158 1.38 dsl buffer = nbuffer;
159 1.38 dsl bufend = buffer + bufsize;
160 1.1 bjh21 }
161 1.37 dsl
162 1.38 dsl /* Sort this set of records */
163 1.39 dsl if (SINGL_FLD) {
164 1.39 dsl if (radix_sort(keylist, nelem, ftbl[0].weights, REC_D))
165 1.39 dsl err(2, "single field radix_sort");
166 1.39 dsl } else {
167 1.39 dsl if (radix_sort(keylist, nelem, unweighted, 0))
168 1.39 dsl err(2, "unweighted radix_sort");
169 1.39 dsl }
170 1.38 dsl
171 1.38 dsl if (c == EOF && mfct == 0) {
172 1.38 dsl /* all the data is (sorted) in the buffer */
173 1.39 dsl append(keylist, nelem, outfp,
174 1.39 dsl DEBUG('k') ? putkeydump : putline, ftbl->weights);
175 1.38 dsl break;
176 1.1 bjh21 }
177 1.37 dsl
178 1.38 dsl /* Save current data to a temporary file for a later merge */
179 1.39 dsl fp = ftmp();
180 1.39 dsl fstack[mfct].fp = fp;
181 1.39 dsl append(keylist, nelem, fp, putrec, NULL);
182 1.37 dsl mfct++;
183 1.8 jdolecek
184 1.38 dsl if (c == EOF) {
185 1.38 dsl /* merge to output file */
186 1.39 dsl fmerge(0, filelist, mfct, geteasy, outfp,
187 1.39 dsl DEBUG('k') ? putkeydump : putline, ftbl);
188 1.38 dsl break;
189 1.1 bjh21 }
190 1.19 jdolecek
191 1.38 dsl if (mfct == MERGE_FNUM) {
192 1.38 dsl /* Merge the files we have */
193 1.39 dsl fp = ftmp();
194 1.38 dsl fmerge(0, filelist, mfct, geteasy, fp, putrec, ftbl);
195 1.38 dsl mfct = 1;
196 1.38 dsl fstack[0].fp = fp;
197 1.38 dsl }
198 1.37 dsl }
199 1.1 bjh21
200 1.37 dsl free(keylist);
201 1.37 dsl keylist = NULL;
202 1.37 dsl free(buffer);
203 1.37 dsl buffer = NULL;
204 1.1 bjh21 }
205