Use tree for peer_name search

This commit is contained in:
Vysheng 2014-01-31 20:03:40 +04:00
parent ec0d7274be
commit 33d25bab99
4 changed files with 46 additions and 13 deletions

View File

@ -224,6 +224,7 @@ void replay_log_event (void) {
tsnprintf (buf, 99, "user#%d", U->user_id);
U->print_name = create_print_name (U->id, "!", buf, 0, 0);
}
peer_insert_name ((void *)U);
}
};
break;
@ -280,6 +281,7 @@ void replay_log_event (void) {
tsnprintf (buf, 99, "user#%d", U->user_id);
U->print_name = create_print_name (U->id, "!", buf, 0, 0);
}
peer_insert_name ((void *)U);
}
rptr += 2;
};
@ -315,7 +317,9 @@ void replay_log_event (void) {
}
U->first_name = fetch_str_dup ();
U->last_name = fetch_str_dup ();
assert (!U->print_name);
U->print_name = create_print_name (U->id, U->first_name, U->last_name, 0, 0);
peer_insert_name ((void *)U);
U->access_hash = fetch_long ();
U->phone = fetch_str_dup ();
if (fetch_int ()) {
@ -452,6 +456,7 @@ void replay_log_event (void) {
U->user_id = *(rptr ++);
peer_t *Us = user_chat_get (MK_USER (U->user_id));
assert (!U->print_name);
if (Us) {
U->print_name = create_print_name (id, "!", Us->user.first_name, Us->user.last_name, 0);
} else {
@ -459,6 +464,7 @@ void replay_log_event (void) {
tsnprintf (buf, 99, "user#%d", U->user_id);
U->print_name = create_print_name (id, "!", buf, 0, 0);
}
peer_insert_name ((void *)U);
U->g_key = talloc (256);
U->nonce = talloc (256);
memcpy (U->g_key, rptr, 256);
@ -559,6 +565,7 @@ void replay_log_event (void) {
peer_t *Us = user_chat_get (MK_USER (P->encr_chat.user_id));
assert (Us);
P->print_name = create_print_name (P->id, "!", Us->user.first_name, Us->user.last_name, 0);
peer_insert_name (P);
memcpy (P->encr_chat.key, rptr, 256);
rptr += 64;
P->encr_chat.g_key = talloc (256);
@ -598,7 +605,9 @@ void replay_log_event (void) {
struct chat *C = &_C->chat;
C->flags = FLAG_CREATED | fetch_int ();
C->title = fetch_str_dup ();
assert (!C->print_title);
C->print_title = create_print_name (id, C->title, 0, 0, 0);
peer_insert_name ((void *)C);
C->users_num = fetch_int ();
C->date = fetch_int ();
C->version = fetch_int ();
@ -628,8 +637,12 @@ void replay_log_event (void) {
struct chat *C = &_C->chat;
if (C->title) { tfree_str (C->title); }
C->title = fetch_str_dup ();
if (C->print_title) { tfree_str (C->print_title); }
if (C->print_title) {
peer_delete_name ((void *)C);
tfree_str (C->print_title);
}
C->print_title = create_print_name (C->id, C->title, 0, 0, 0);
peer_insert_name ((void *)C);
#ifdef USE_LUA
lua_chat_update (C);
#endif
@ -1778,7 +1791,6 @@ void bl_do_create_message_service (int msg_id, int from_id, int to_type, int to_
out_ints (data, len);
add_log_event (packet_buffer, 4 * (packet_ptr - packet_buffer));
}
void bl_do_create_message_service_encr (long long msg_id, int from_id, int to_type, int to_id, int date, const int *data, int len) {
clear_packet ();
out_int (CODE_binlog_create_message_service_encr);

View File

@ -840,10 +840,14 @@ void work_update_binlog (void) {
struct user *U = &UC->user;
if (U->first_name) { tfree_str (U->first_name); }
if (U->last_name) { tfree_str (U->last_name); }
if (U->print_name) { tfree_str (U->print_name); }
if (U->print_name) {
peer_delete_name (UC);
tfree_str (U->print_name);
}
U->first_name = fetch_str_dup ();
U->last_name = fetch_str_dup ();
U->print_name = create_print_name (U->id, U->first_name, U->last_name, 0, 0);
peer_insert_name ((void *)U);
} else {
fetch_skip_str ();
fetch_skip_str ();

View File

@ -38,7 +38,9 @@
static int id_cmp (struct message *M1, struct message *M2);
#define peer_cmp(a,b) (cmp_peer_id (a->id, b->id))
#define peer_cmp_name(a,b) (strcmp (a->print_name, b->print_name))
DEFINE_TREE(peer,peer_t *,peer_cmp,0)
DEFINE_TREE(peer_by_name,peer_t *,peer_cmp_name,0)
DEFINE_TREE(message,struct message *,id_cmp,0)
@ -48,6 +50,7 @@ struct message message_list = {
};
struct tree_peer *peer_tree;
struct tree_peer_by_name *peer_by_name_tree;
struct tree_message *message_tree;
struct tree_message *message_unsent_tree;
@ -165,16 +168,8 @@ char *create_print_name (peer_id_t id, const char *a1, const char *a2, const cha
int fl = strlen (s);
int cc = 0;
while (1) {
int ok = 1;
int i;
for (i = 0; i < peer_num; i++) {
assert (Peers[i]);
if (cmp_peer_id (Peers[i]->id, id) && Peers[i]->print_name && !strcmp (Peers[i]->print_name, s)) {
ok = 0;
break;
}
}
if (ok) {
peer_t *P = peer_lookup_name (s);
if (!P || !cmp_peer_id (Peers[i]->id, id)) {
break;
}
cc ++;
@ -1980,3 +1975,22 @@ void __send_msg (struct message *M) {
void send_all_unsent (void ) {
tree_act_message (message_unsent_tree, __send_msg);
}
void peer_insert_name (peer_t *P) {
//if (!P->print_name || !strlen (P->print_name)) { return; }
//logprintf ("+%s\n", P->print_name);
peer_by_name_tree = tree_insert_peer_by_name (peer_by_name_tree, P, lrand48 ());
}
void peer_delete_name (peer_t *P) {
//if (!P->print_name || !strlen (P->print_name)) { return; }
//logprintf ("-%s\n", P->print_name);
peer_by_name_tree = tree_delete_peer_by_name (peer_by_name_tree, P);
}
peer_t *peer_lookup_name (const char *s) {
static peer_t P;
P.print_name = (void *)s;
peer_t *R = tree_lookup_peer_by_name (peer_by_name_tree, &P);
return R;
}

View File

@ -374,6 +374,9 @@ void message_add_peer (struct message *M);
void message_del_peer (struct message *M);
void free_message (struct message *M);
void message_del_use (struct message *M);
void peer_insert_name (peer_t *P);
void peer_delete_name (peer_t *P);
peer_t *peer_lookup_name (const char *s);
#define PEER_USER 1
#define PEER_CHAT 2