summaryrefslogtreecommitdiff
path: root/src/postprocess/merge_entries.c
blob: 1dd7d50c57f2c87f4f93acba448e7f716c1952bc (plain)
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
#include "mystdlib.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <assert.h>

extern int halfsiphash(const uint8_t *in, const size_t inlen, const uint8_t *k, uint8_t *out, const size_t outlen);

enum { COLUMNS = 15 };
typedef struct {
    long rows;
    long outoff;
    long flag;
    int  year;
} entry_t;
typedef struct {
    char    *ptr;
    size_t   size;
    uint64_t data; /* might store precision or normalized verweise+zusaetze */
} outvec_t;
static outvec_t * g_out_array;

const char *g_year_map[] = {
"1992_Q2", "1995_Q0", "1996_Q0", "1996_Q1", "1997_Q1", "1997_Q3", "1998_Q1", "1998_Q3", "1999_Q1", "1999_Q3", "2000_Q1", "2000_Q3", "2001_Q1", "2001_Q2", "2001_Q3", "2001_Q4", "2002_Q1",
"2002_Q3", "2003_Q1", "2003_Q3", "2004_Q1", "2004_Q3", "2005_Q1", "2005_Q3", "2006_Q1", "2006_Q3", "2007_Q1", "2007_Q3", "2008_Q1", "2008_Q3", "2009_Q1", "2009_Q3", "2010_Q1", "2010_Q3",
"2011_Q1", "2011_Q3", "2012_Q1", "2012_Q3", "2013_Q1", "2013_Q3", "2014_Q1", "2014_Q3", "2015_Q1", "2015_Q3", "2016_Q1", "2016_Q3", "2017_Q1", "2017_Q3", "2018_Q1", "2018_Q3", "2019_Q1",
0
};

static int year_to_offset(const char *year) {
  const char **y = g_year_map;
  int off = 0;
  while (*y) {
    if (!memcmp(year, *y, 7)) return off;
    ++off; ++y;
  }
  return -1;
}

static uint64_t string_to_hash(const char *start, const char *end) {
  const uint64_t k = 0xdc082e4772c897e7LL;
  uint64_t hash = 1LL, acc = 0LL;
  while (start < end) {
    const char *tokend = memchr(start, ' ', end - start);
    const char *tokend2 = memchr(start, '.', end - start);
    const char *compare_end = tokend;

    if (tokend2 && (!tokend || (tokend > tokend2))) {
      tokend = 0;
      compare_end = tokend2 + 1;
    }
    if (!tokend && !tokend2)
      compare_end = end;

    if (compare_end != start) {
      halfsiphash((const uint8_t*)start, (const size_t)(compare_end - start), (const uint8_t*)&k, (uint8_t*)&acc, sizeof(acc));
      hash *= acc;
      //printf("HASH %" PRIX64 " %" PRIX64 ":%" PRIX64 " TOKEN(%zd): %.*s\n", hash, acc, k, tokend - start, (int)(tokend - start), start);
    }
    start = compare_end;
    if (tokend) start++; // for space, we only compared up to the char
  }
  return hash;
}

static int
STRCMP_n (const char *p1, const char *p2)
{
  const unsigned char *s1 = (const unsigned char *) p1;
  const unsigned char *s2 = (const unsigned char *) p2;
  unsigned char c1, c2;
  do
    {
      c1 = (unsigned char) *s1++;
      c2 = (unsigned char) *s2++;
      if (c1 == 10)
        return c1 - c2;
    }
  while (c1 == c2);
  return c1 - c2;
}

static int compare_entries(entry_t*a, entry_t*b) {
  int col, row, nprec_a, nprec_b, a_is_newer;
  outvec_t *oa = g_out_array + a->outoff;
  outvec_t *ob = g_out_array + b->outoff;

  /* Multi line entries never match single line entries */
  if (a->rows != b->rows)
    return -1;

  /* Check all columns except year, flag and coords for equality */
  for (row=0; row <= a->rows; ++row)
    for (col=2; col<COLUMNS-1; ++col) {
      int off = COLUMNS*row+col;
      if (col == 5) continue;
      if (col == 8) {
        if (oa[off].data != ob[off].data)
          return -1;
        else
          continue;
      }
      if (oa[off].size != ob[off].size || memcmp(oa[off].ptr, ob[off].ptr, oa[off].size))
        return -1;
    }

  /* we haven't returned, so they're identical enough
     we always want to keep the newest coordinates,
     unless they are not present or from 2006_Q3 */
  a_is_newer = memcmp(oa[0].ptr,ob[0].ptr,oa[0].size) > 0;
  nprec_a = memcmp(oa[0].ptr,"2006_Q3",7) ? 2 : 1;
  nprec_b = memcmp(ob[0].ptr,"2006_Q3",7) ? 2 : 1;

  for (row=0; row <= a->rows; ++row) {
    int present_a = oa[row * COLUMNS + 14].size != 1;
    int present_b = ob[row * COLUMNS + 14].size != 1;

    if (!present_a) continue;

    /* If the current entry's coords were copied, use the stored precision */
    if (oa[row * COLUMNS + 14].data > 0)
      nprec_a = oa[row * COLUMNS + 14].data;

    if (!present_b || (nprec_a > nprec_b ) || ( a_is_newer && (nprec_a >= nprec_b))) {
      ob[row * COLUMNS + 14].ptr  = oa[row * COLUMNS + 14].ptr;
      ob[row * COLUMNS + 14].size = oa[row * COLUMNS + 14].size;
      ob[row * COLUMNS + 14].data = nprec_a;
    }
  }
  return 0;
}

static int sort_me(const void *f_a, const void *f_b) {
  entry_t *e_a = (entry_t *)f_a;
  entry_t *e_b = (entry_t *)f_b;

  outvec_t *oa = g_out_array + e_a->outoff;
  outvec_t *ob = g_out_array + e_b->outoff;

  int res, row;

  if (e_a->rows != e_b->rows)
    return e_a->rows - e_b->rows;

  for (row = 0; row <= e_a->rows; ++row) {
    outvec_t *oa_row = oa + row * COLUMNS;
    outvec_t *ob_row = ob + row * COLUMNS;

    if ((res = STRCMP_n(oa_row[ 2].ptr, ob_row[ 2].ptr))) return res; /* PLZ */
    if ((res = STRCMP_n(oa_row[ 9].ptr, ob_row[ 9].ptr))) return res; /* Ort */
    if ((res = STRCMP_n(oa_row[10].ptr, ob_row[10].ptr))) return res; /* Vorwahl */
    if ((res = STRCMP_n(oa_row[11].ptr, ob_row[11].ptr))) return res; /* Rufnummer */
    if ((res = STRCMP_n(oa_row[ 6].ptr, ob_row[ 6].ptr))) return res; /* Strasse */
    if ((res = STRCMP_n(oa_row[ 7].ptr, ob_row[ 7].ptr))) return res; /* Hausnummer */
    if ((res = STRCMP_n(oa_row[ 3].ptr, ob_row[ 3].ptr))) return res; /* Nachname */
    if ((res = STRCMP_n(oa_row[ 4].ptr, ob_row[ 4].ptr))) return res; /* Vorname */
    if (oa_row[8].data != ob_row[8].data ) return oa_row[8].data - ob_row[8].data;
  }

  return STRCMP_n(oa[0].ptr, ob[0].ptr);
}

static void do_escape_string(char * s, size_t len) {
  size_t i;

  putchar('"');
  for (i=0; i<len; ++i) {
    switch(s[i]) {
    case '\\':
      putchar('\\');
    case '"':
      putchar('\\');
    case ',':
      putchar('\\');
    default:
      break;
    }
    putchar(s[i]);
  }
  putchar('"');
}

static void escape_string(char * s, size_t len) {
  size_t i;
  if (len == 4 && ( ( s[0] | 0x20 ) == 'n' ) && ( ( s[1] | 0x20 ) == 'u' ) && ( ( s[2] | 0x20 ) == 'l' ) && ( ( s[3] | 0x20 ) == 'l' ) )
    return do_escape_string(s, len);
  for (i=0; i<len; ++i)
    switch(s[i]) {
      case '{': case '\\': case '}': case '"': case ',': return do_escape_string(s, len);
    }
  fwrite(s, len, 1, stdout);
}

int main(int argc, char **args) {
  MAP tbuch;
  char *ptr;
  entry_t * sort_array;
  int outoff = 0, lines = COLUMNS;
  unsigned long current = 0, i, flag;
  uint64_t year_list = 0, revflag_list = 0, bizflag_list = 0;

  if (argc != 2) exit(1);
  tbuch = map_file(args[1], 1);

  /* Estimate upper bound for amount of lines */
  for (i=0; i<tbuch->size; ++i)
    if (tbuch->addr[i] == 10)
      ++lines;

  g_out_array = (outvec_t*)malloc(lines * sizeof(outvec_t));
  sort_array = (entry_t*)malloc(lines * sizeof(entry_t));

  ptr = (char*)tbuch->addr;

  while (ptr < (char*)tbuch->addr + tbuch->size) {
    int c, year;

    /* Look for field terminator */
    for (c=0; c<COLUMNS; ++c) {
      char * end = strchr(ptr, 10);
      if (c==0)
        year = year_to_offset(ptr);
      if (c==1)
        flag = strtoul(ptr, 0, 16);
      if (c==5)
         g_out_array[outoff+8].data = string_to_hash(ptr, end);
      g_out_array[outoff+c].ptr = ptr;
      g_out_array[outoff+c].size = end - ptr;
      if (c==8)
        g_out_array[outoff+c].data *= string_to_hash(ptr, end);
      else
        g_out_array[outoff+c].data = 0;
      ptr = end + 1;
    }

    if (flag&2) {
      assert(current>0);
      sort_array[current-1].rows++;
    } else {
      sort_array[current].rows = 0;
      sort_array[current].outoff = outoff;
      sort_array[current].flag = flag;
      sort_array[current].year = year;
      current++;
    }
    outoff += COLUMNS;
  }

  /* Sort the whole thing */
  qsort(sort_array, current, sizeof(entry_t), sort_me);

  for (i=0; i<current; ++i) {
    year_list |= 1LL << sort_array[i].year;
    if (sort_array[i].flag & 0x80 ) bizflag_list |= 1LL << sort_array[i].year;
    if (sort_array[i].flag & 0x40 ) revflag_list |= 1LL << sort_array[i].year;

    if ((i == current - 1) || compare_entries(sort_array+i, sort_array+i+1)) {
      printf("%" PRIu64 "\t%" PRIu64 "\t%" PRIu64 "\t", year_list, bizflag_list, revflag_list);
      for (int c=2; c<COLUMNS; ++c) {
        int j, started = 0, skipped = 0;
        for (j=0; j<=sort_array[i].rows; ++j) {
          outvec_t * out = g_out_array + sort_array[i].outoff + j * COLUMNS;
          if (!out[c].size || *(out[c].ptr) == 9)
            skipped++;
          else {
            if (!started++)
              putchar('{');
            else
              putchar(',');
            for (int x=0; x<skipped; ++x) fputs("null,", stdout);
            if (c != COLUMNS-1)
              escape_string(out[c].ptr, out[c].size);
            else {
              char coords[64], *tab;
              memcpy(coords, out[c].ptr, out[c].size);
              tab = memchr(coords, 9, out[c].size);
              if (tab) *tab = ' ';
              fwrite(coords, out[c].size, 1, stdout);
            }
            skipped = 0;
          }
          ++out;
        }
        if (started) putchar('}');
        if (c<COLUMNS-1) putchar(9);
      }
      putchar(10);

      year_list = 0;
      bizflag_list = 0;
      revflag_list = 0;
    }
  }
}