aboutsummaryrefslogtreecommitdiff
path: root/src/single-threaded.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/single-threaded.c')
-rw-r--r--src/single-threaded.c262
1 files changed, 262 insertions, 0 deletions
diff --git a/src/single-threaded.c b/src/single-threaded.c
new file mode 100644
index 0000000..6f2b159
--- /dev/null
+++ b/src/single-threaded.c
@@ -0,0 +1,262 @@
1// single-threaded.c
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <errno.h>
6#include <unistd.h>
7#include <fcntl.h>
8#include <sys/mman.h>
9
10#include "string-view.h"
11#include "scanner.h"
12#include "utils.h"
13#include "card.h"
14#include "user.h"
15#include "transaction.h"
16#include "transaction-heap.h"
17#include "stb_ds.h"
18
19unsigned int hash (register const char *str, register size_t len);
20extern const char *wordlist[];
21
22void
23read_filtered_cards_and_users (const char *filename,
24 struct card **cards, struct user **users)
25{
26 MEASURE_SCOPE ();
27
28 ASSERT (filename);
29 ASSERT (cards);
30 ASSERT (users);
31
32 int fd = open (filename, O_RDONLY);
33 ASSERT_MSG (fd != -1, "open %s: %s", filename, strerror (errno));
34 struct string_view sv = mmap_whole (fd);
35
36 struct scanner sc;
37 scanner__init (&sc, sv);
38
39 // skip header: id, client_id, card_brand, card_type, card_number,
40 // expires, cvv, has_chip, num_cards_issued, credit_limit,
41 // acct_open_date, year_pin_last_changed, card_on_dark_web
42 scanner__skip_over (&sc, '\n');
43 while (!scanner__reached_end (&sc)) {
44 struct card card;
45 struct user user;
46
47 card.id = scanner__get_uint64 (&sc);
48
49 scanner__skip_n (&sc, 1);
50
51 user.id = scanner__get_uint64 (&sc);
52
53 scanner__skip_over (&sc, '$');
54
55 uint64_t credit_limit = scanner__get_uint64 (&sc);
56 scanner__skip_over (&sc, '\n');
57
58 if (credit_limit < 12000)
59 continue;
60
61 hmputs (*cards, card);
62
63 if (hmgetp_null (*users, user.key) == NULL)
64 hmputs (*users, user);
65 }
66
67 munmap ((char *) sv.begin, sv.end - sv.begin);
68 close (fd);
69}
70
71void
72read_user_per_capita_incomes (const char *filename, struct user *users)
73{
74 MEASURE_SCOPE ();
75
76 ASSERT (filename);
77 ASSERT (users);
78
79 int fd = open (filename, O_RDONLY);
80 ASSERT_MSG (fd != -1, "open %s: %s", filename, strerror (errno));
81 struct string_view sv = mmap_whole (fd);
82
83 struct scanner sc;
84 scanner__init (&sc, sv);
85
86 // skip header: id, current_age, retirement_age, birth_year,
87 // birth_month, gender, address, latitude, longitude,
88 // per_capita_income, yearly_income, total_debt, credit_score,
89 // num_credit_cards
90 scanner__skip_over (&sc, '\n');
91
92 while (!scanner__reached_end (&sc)) {
93 client_id_t id = scanner__get_uint64 (&sc);
94 struct user *user = hmgetp_null (users, id);
95 if (user == NULL) {
96 scanner__skip_over (&sc, '\n');
97 continue;
98 }
99
100 scanner__skip_over (&sc, '$');
101
102 user->normalizer = 0.12/scanner__get_uint64 (&sc);
103
104 scanner__skip_over (&sc, '\n');
105 }
106
107 munmap ((char *) sv.begin, sv.end - sv.begin);
108 close (fd);
109}
110
111void
112read_filtered_and_reduced_transactions (const char *filename,
113 struct transaction **transactions,
114 struct card *cards)
115{
116 MEASURE_SCOPE ();
117
118 ASSERT (filename);
119 ASSERT (transactions);
120 ASSERT (cards);
121
122 int fd = open (filename, O_RDONLY);
123 ASSERT_MSG (fd != -1, "open %s: %s", filename, strerror (errno));
124 struct string_view sv = mmap_whole (fd);
125
126 struct scanner sc;
127 scanner__init (&sc, sv);
128
129 // skip header: id, date, client_id, card_id, amount, use_chip,
130 // merchant_id, merchant_city, merchant_state, zip, mcc, errors
131 scanner__skip_over (&sc, '\n');
132 while (!scanner__reached_end (&sc)) {
133 union transaction_key transaction_key;
134
135 scanner__skip_over (&sc, ',');
136
137 transaction_key.year_month = scanner__get_uint64 (&sc) << 4;
138 scanner__skip_n (&sc, 1);
139 transaction_key.year_month |= scanner__get_uint64 (&sc);
140
141 scanner__skip_over (&sc, ',');
142
143 transaction_key.client_id = scanner__get_uint64 (&sc);
144
145 scanner__skip_n (&sc, 1);
146
147 card_id_t card_id = scanner__get_uint64 (&sc);
148 if (hmgetp_null (cards, card_id) == NULL) {
149 scanner__skip_over (&sc, '\n');
150 continue;
151 }
152
153 scanner__skip_n (&sc, 2);
154
155 int64_t amount = 100*scanner__get_int64 (&sc);
156 scanner__skip_n (&sc, 1);
157 int sign = amount >= 0 ? 1 : -1;
158 amount += sign*scanner__get_uint64 (&sc);
159
160 scanner__skip_n (&sc, 1);
161 scanner__skip_over (&sc, ',');
162 scanner__skip_over (&sc, ',');
163
164 struct string_view sv = scanner__get_until (&sc, ',');
165 transaction_key.merchant_city_id = hash (sv.begin, STRING_VIEW__LENGTH (sv));
166
167 scanner__skip_over (&sc, '\n');
168
169 struct transaction *transaction = hmgetp_null (*transactions,
170 transaction_key);
171 if (transaction == NULL)
172 hmput (*transactions, transaction_key, amount);
173 else transaction->sum += amount;
174 }
175
176 munmap ((char *) sv.begin, sv.end - sv.begin);
177 close (fd);
178}
179
180void
181normalize_transaction_sums (struct transaction *transactions,
182 struct user *users, size_t count)
183{
184 MEASURE_SCOPE ("%lu transactions", count);
185
186 ASSERT (transactions);
187 ASSERT (users);
188
189 for (size_t i = 0; i < count; ++i) {
190 struct transaction *transaction = &transactions[i];
191 float normalizer = hmgets (users, transaction->key.client_id).normalizer;
192 transaction->normalized = transaction->sum*normalizer;
193 }
194}
195
196int
197compare_transactions (const void *lhs, const void *rhs)
198{
199 const struct transaction *lhs_transaction = lhs;
200 const struct transaction *rhs_transaction = rhs;
201
202 float lhs_normalized = lhs_transaction->normalized;
203 float rhs_normalized = rhs_transaction->normalized;
204
205 return (lhs_normalized < rhs_normalized) - (lhs_normalized > rhs_normalized);
206}
207
208void
209extract_top_normalized_transaction_sums (struct transaction *transactions,
210 size_t count, struct transaction_heap *heap)
211{
212 MEASURE_SCOPE ("(~10x faster then full sort)");
213
214 ASSERT (transactions);
215 ASSERT (heap);
216
217 for (size_t i = 0; i < count; ++i)
218 transaction_heap__push (heap, transactions[i]);
219
220 qsort (heap->array, heap->count, sizeof (*heap->array), compare_transactions);
221}
222
223void
224print_top_transactions (struct transaction_heap *heap)
225{
226 MEASURE_SCOPE ();
227
228 printf (" i client_id normalized_spending year_month city\n");
229 for (int i = 0; i < heap->count; ++i) {
230 printf ("%2d %4hu %6.2f %hu-%02hu %s\n", i,
231 heap->array[i].key.client_id,
232 heap->array[i].normalized,
233 heap->array[i].key.year_month >> 4,
234 heap->array[i].key.year_month & ((1 << 4) - 1),
235 wordlist[heap->array[i].key.merchant_city_id]);
236 }
237}
238
239int
240main (void)
241{
242 MEASURE_SCOPE ();
243
244 struct card *cards = NULL;
245 struct user *users = NULL;
246 struct transaction *transactions = NULL;
247 struct transaction_heap top_transactions = {0};
248
249 read_filtered_cards_and_users ("res/cards_data.csv", &cards, &users);
250 read_filtered_and_reduced_transactions ("res/transactions_data.csv",
251 &transactions, cards);
252 size_t transaction_count = hmlenu (transactions);
253 read_user_per_capita_incomes ("res/users_data.csv", users);
254 normalize_transaction_sums (transactions, users, transaction_count);
255 extract_top_normalized_transaction_sums (transactions, transaction_count,
256 &top_transactions);
257 printf ("Top %d out of %lu:\n", top_transactions.count, transaction_count);
258 print_top_transactions (&top_transactions);
259
260 return EXIT_SUCCESS;
261}
262// single-threaded.c ends here