aboutsummaryrefslogtreecommitdiff
path: root/src/single-threaded.c
blob: 6f2b1598f0045c4f989647b45c407c88b2673641 (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
// single-threaded.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>

#include "string-view.h"
#include "scanner.h"
#include "utils.h"
#include "card.h"
#include "user.h"
#include "transaction.h"
#include "transaction-heap.h"
#include "stb_ds.h"

unsigned int hash (register const char *str, register size_t len);
extern const char *wordlist[];

void
read_filtered_cards_and_users (const char *filename,
                               struct card **cards, struct user **users)
{
  MEASURE_SCOPE ();

  ASSERT (filename);
  ASSERT (cards);
  ASSERT (users);

  int fd = open (filename, O_RDONLY);
  ASSERT_MSG (fd != -1, "open %s: %s", filename, strerror (errno));
  struct string_view sv = mmap_whole (fd);

  struct scanner sc;
  scanner__init (&sc, sv);

  // skip header: id, client_id, card_brand, card_type, card_number,
  // expires, cvv, has_chip, num_cards_issued, credit_limit,
  // acct_open_date, year_pin_last_changed, card_on_dark_web
  scanner__skip_over (&sc, '\n');
  while (!scanner__reached_end (&sc)) {
    struct card card;
    struct user user;

    card.id = scanner__get_uint64 (&sc);

    scanner__skip_n (&sc, 1);

    user.id = scanner__get_uint64 (&sc);

    scanner__skip_over (&sc, '$');

    uint64_t credit_limit = scanner__get_uint64 (&sc);
    scanner__skip_over (&sc, '\n');

    if (credit_limit < 12000)
      continue;

    hmputs (*cards, card);

    if (hmgetp_null (*users, user.key) == NULL)
      hmputs (*users, user);
  }

  munmap ((char *) sv.begin, sv.end - sv.begin);
  close (fd);
}

void
read_user_per_capita_incomes (const char *filename, struct user *users)
{
  MEASURE_SCOPE ();

  ASSERT (filename);
  ASSERT (users);

  int fd = open (filename, O_RDONLY);
  ASSERT_MSG (fd != -1, "open %s: %s", filename, strerror (errno));
  struct string_view sv = mmap_whole (fd);

  struct scanner sc;
  scanner__init (&sc, sv);

  // skip header: id, current_age, retirement_age, birth_year,
  // birth_month, gender, address, latitude, longitude,
  // per_capita_income, yearly_income, total_debt, credit_score,
  // num_credit_cards
  scanner__skip_over (&sc, '\n');

  while (!scanner__reached_end (&sc)) {
    client_id_t id = scanner__get_uint64 (&sc);
    struct user *user = hmgetp_null (users, id);
    if (user == NULL) {
      scanner__skip_over (&sc, '\n');
      continue;
    }

    scanner__skip_over (&sc, '$');

    user->normalizer = 0.12/scanner__get_uint64 (&sc);

    scanner__skip_over (&sc, '\n');
  }

  munmap ((char *) sv.begin, sv.end - sv.begin);
  close (fd);
}

void
read_filtered_and_reduced_transactions (const char *filename,
                                        struct transaction **transactions,
                                        struct card *cards)
{
  MEASURE_SCOPE ();

  ASSERT (filename);
  ASSERT (transactions);
  ASSERT (cards);

  int fd = open (filename, O_RDONLY);
  ASSERT_MSG (fd != -1, "open %s: %s", filename, strerror (errno));
  struct string_view sv = mmap_whole (fd);

  struct scanner sc;
  scanner__init (&sc, sv);

  // skip header: id, date, client_id, card_id, amount, use_chip,
  // merchant_id, merchant_city, merchant_state, zip, mcc, errors
  scanner__skip_over (&sc, '\n');
  while (!scanner__reached_end (&sc)) {
    union transaction_key transaction_key;

    scanner__skip_over (&sc, ',');

    transaction_key.year_month  = scanner__get_uint64 (&sc) << 4;
    scanner__skip_n (&sc, 1);
    transaction_key.year_month |= scanner__get_uint64 (&sc);

    scanner__skip_over (&sc, ',');

    transaction_key.client_id = scanner__get_uint64 (&sc);

    scanner__skip_n (&sc, 1);

    card_id_t card_id = scanner__get_uint64 (&sc);
    if (hmgetp_null (cards, card_id) == NULL) {
      scanner__skip_over (&sc, '\n');
      continue;
    }

    scanner__skip_n (&sc, 2);

    int64_t amount = 100*scanner__get_int64 (&sc);
    scanner__skip_n (&sc, 1);
    int sign = amount >= 0 ? 1 : -1;
    amount += sign*scanner__get_uint64 (&sc);

    scanner__skip_n (&sc, 1);
    scanner__skip_over (&sc, ',');
    scanner__skip_over (&sc, ',');

    struct string_view sv = scanner__get_until (&sc, ',');
    transaction_key.merchant_city_id = hash (sv.begin, STRING_VIEW__LENGTH (sv));

    scanner__skip_over (&sc, '\n');

    struct transaction *transaction = hmgetp_null (*transactions,
                                                   transaction_key);
    if (transaction == NULL)
      hmput (*transactions, transaction_key, amount);
    else transaction->sum += amount;
  }

  munmap ((char *) sv.begin, sv.end - sv.begin);
  close (fd);
}

void
normalize_transaction_sums (struct transaction *transactions,
                            struct user *users, size_t count)
{
  MEASURE_SCOPE ("%lu transactions", count);

  ASSERT (transactions);
  ASSERT (users);

  for (size_t i = 0; i < count; ++i) {
    struct transaction *transaction = &transactions[i];
    float normalizer = hmgets (users, transaction->key.client_id).normalizer;
    transaction->normalized = transaction->sum*normalizer;
  }
}

int
compare_transactions (const void *lhs, const void *rhs)
{
  const struct transaction *lhs_transaction = lhs;
  const struct transaction *rhs_transaction = rhs;

  float lhs_normalized = lhs_transaction->normalized;
  float rhs_normalized = rhs_transaction->normalized;

  return (lhs_normalized < rhs_normalized) - (lhs_normalized > rhs_normalized);
}

void
extract_top_normalized_transaction_sums (struct transaction *transactions,
                                         size_t count, struct transaction_heap *heap)
{
  MEASURE_SCOPE ("(~10x faster then full sort)");

  ASSERT (transactions);
  ASSERT (heap);

  for (size_t i = 0; i < count; ++i)
    transaction_heap__push (heap, transactions[i]);

  qsort (heap->array, heap->count, sizeof (*heap->array), compare_transactions);
}

void
print_top_transactions (struct transaction_heap *heap)
{
  MEASURE_SCOPE ();

  printf (" i client_id normalized_spending year_month city\n");
  for (int i = 0; i < heap->count; ++i) {
    printf ("%2d      %4hu              %6.2f    %hu-%02hu %s\n", i,
            heap->array[i].key.client_id,
            heap->array[i].normalized,
            heap->array[i].key.year_month >> 4,
            heap->array[i].key.year_month & ((1 << 4) - 1),
            wordlist[heap->array[i].key.merchant_city_id]);
  }
}

int
main (void)
{
  MEASURE_SCOPE ();

  struct card        *cards        = NULL;
  struct user        *users        = NULL;
  struct transaction *transactions = NULL;
  struct transaction_heap top_transactions = {0};

  read_filtered_cards_and_users ("res/cards_data.csv", &cards, &users);
  read_filtered_and_reduced_transactions ("res/transactions_data.csv",
                                          &transactions, cards);
  size_t transaction_count = hmlenu (transactions);
  read_user_per_capita_incomes ("res/users_data.csv", users);
  normalize_transaction_sums (transactions, users, transaction_count);
  extract_top_normalized_transaction_sums (transactions, transaction_count,
                                           &top_transactions);
  printf ("Top %d out of %lu:\n", top_transactions.count, transaction_count);
  print_top_transactions (&top_transactions);

  return EXIT_SUCCESS;
}
// single-threaded.c ends here