-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_tree.c
More file actions
131 lines (118 loc) · 2.66 KB
/
Copy pathtest_tree.c
File metadata and controls
131 lines (118 loc) · 2.66 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tree.h"
#include <mysql/mysql.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#if 0
#define SQL_HOST "localhost"
#define SQL_USER "root"
#define SQL_PASS "fantasmadelux"
#define SQL_DB "newsrt"
int fetch_data(tree_t *tree)
{
MYSQL *sql;
MYSQL_RES *res;
MYSQL_ROW row;
if(!(sql = mysql_init(NULL))) {
perror("mysql_init");
return -1;
}
if(!mysql_real_connect(sql, SQL_HOST, SQL_USER, SQL_PASS, SQL_DB, 0,NULL,0)) {
mysql_close(sql);
perror("mysql_real_connect");
return -1;
}
if(mysql_query(sql, "SELECT url FROM queue LIMIT 500000")) {
perror("mysql_query");
return -1;
}
if(!(res = mysql_store_result(sql))) {
perror("mysql_store_result");
return -1;
}
while((row = mysql_fetch_row(res))) {
if(row[0])
tree_add(tree, strdup(row[0]));
}
mysql_free_result(res);
mysql_close(sql);
return 0;
}
#endif
unsigned int min_len = -1;
void del_str(void *str) {
free(str);
}
int cmp_str(void *s1, void *s2) {
return strcmp((char *) s1+min_len, (char *) s2+min_len);
}
int save_url(void *url, void *ctx) {
int *fd = ctx;
int n = 0;
if(!url) return -1;
n = write(*fd, url, strlen((char*)url));
n+=write(*fd, "\n", 1);
return n;
}
int save_urls(tree_t *tree) {
void *ctx;
int fd = open("tree.db", O_WRONLY|O_TRUNC|O_CREAT, 0666);
if(!fd) {
perror("open");
return -1;
}
ctx = &fd;
tree_foreach(tree, save_url, 0, ctx);
close(fd);
return 0;
}
#define min(a, b) a < b ? a : b
int load_urls(char *file, tree_t *tree)
{
FILE *fp;
char buf[4096];
int i = 0;
printf("Loading data from file %s ", file);
fflush(stdout);
if(!(fp = fopen(file, "r"))) {
perror("fopen");
return -1;
}
while(fgets(buf, sizeof(buf), fp)) {
if(i % 10000 == 0) write(0, ".", 1);
unsigned int len = strlen(buf);
buf[len-1] = 0;
min_len = min(min_len, len);
tree_add(tree, strdup(buf));
i++;
}
printf(" OK\n");
fclose(fp);
return 0;
}
int main()
{
tree_t *urls = tree_open(cmp_str, del_str);
/*if(fetch_data(urls) < 0) {
fprintf(stderr, "cannot fetch data\n");
exit(1);
}*/
int min_len;
min_len = load_urls("tree.db", urls);
printf("min len is: %d\n", min_len);
char *str = "http://metro.co.uk/2016/08/24/rapists-dad-threatened-to-kill-witness-with-rounders-bat-6087084/";
printf("%s exists? %s\n", str, tree_find(urls, str)==0 ? "yes":"no");
if(tree_find(urls, str) == 0) {
tree_del(urls, str);
}
printf("%s exists? %s\n", str, tree_find(urls, str)==0 ? "yes":"no");
printf("tree height is: %d\n", urls->root->height);
printf("root balance factor: %i", tree_node_bf(urls->root->rgt));
//save_urls(urls);
tree_close(urls);
return 0;
}