<commit_before>#include <xcopy.h>
#include <intercept.h>

static route_table_t  *table = NULL;

/* initiate router table */
int
router_init()
{
    table = (route_table_t *) malloc(sizeof(route_table_t));

    if (table == NULL) {
        tc_log_info(LOG_ERR, 0, "malloc route table error");
        return TC_ERROR;
    }

    memset(table, 0, sizeof(route_table_t));
    
    return TC_OK;
}

inline uint32_t
get_route_key(uint32_t ip, uint16_t port)
{
    uint32_t value = port;

    value = (value << 16) + ip + port;

    return value;
}

static inline void router_update_adjust(route_slot_t *slot, int child) 
{
    int          parent;
    route_item_t tmp;

    if (child < 1) {
        return;
    }

    parent = (child - 1) / 2;
    tmp = slot->items[parent];
    slot->items[parent] = slot->items[child];
    slot->items[child] = tmp;

    return;
}


static void router_add_adjust(route_slot_t *slot, int key, int fd) 
{
    int          i, max, depth;
    route_item_t item, tmp;

    max = slot->num;
    if (max > 0) {
        item = slot->items[0];
        if (max == 1) {
            slot->items[1] = item;
        }
    }

    slot->items[0].key = key;
    slot->items[0].fd = fd;
    slot->items[0].timestamp = tc_current_time_sec;

    depth = 1;
    for (i = 1; i < max; i = (i << 1) + 1) {
        if (slot->items[i].timestamp > slot->items[i + 1].timestamp) {
            ++i;
        }

        /* TODO needs to be optimized */ 
        tmp = slot->items[i];
        slot->items[i] = item;
        item = tmp;
        depth++;
    }
    
    if (depth < ROUTE_ARRAY_DEPTH) {
        slot->items[max] = item;
    }

    if (slot->num < ROUTE_ARRAY_SIZE) {
        slot->num++;
    }
}

/* add item to the router table */
void
router_add(uint32_t ip, uint16_t port, int fd)
{
    int           i, max, existed, index, remainder;
    uint32_t      key;
    route_slot_t *slot;

    key = get_route_key(ip, port);

    index = (key & ROUTE_KEY_HIGH_MASK) >> ROUTE_KEY_SHIFT;
    remainder = key & ROUTE_KEY_LOW_MASK;

    table->cache[index].key = remainder; 
    table->cache[index].fd  = (uint16_t) fd; 

    slot = table->slots + index;

    existed = 0;
    max = ROUTE_ARRAY_SIZE;
    if (slot->num < ROUTE_ARRAY_SIZE) {
        max = slot->num;
    }

    for (i = 0; i < max; i++) {
        if (slot->items[i].key == remainder) {
            slot->items[i].fd = fd;
            slot->items[i].timestamp = tc_current_time_sec;
            existed = 1;
            break;
        }
    }

    if (!existed) {
        router_add_adjust(slot, remainder, fd);
    } else {
        router_update_adjust(slot, i);
    }

    delay_table_send(get_key(ip, port), fd);

}

int
router_get(uint32_t key)
{
    int           i, fd = 0, index, remainder;
    route_slot_t *slot;

    index = (key & ROUTE_KEY_HIGH_MASK) >> ROUTE_KEY_SHIFT;
    remainder = key & ROUTE_KEY_LOW_MASK;

    if (table->cache[index].key == remainder) {
        table->hit++;
        return (int) table->cache[index].fd;
    }

    slot = table->slots + index;
    for (i = 0; i < ROUTE_ARRAY_SIZE; i++) {
        if (slot->items[i].key == remainder) {
            table->missed++;
            fd = (int) slot->items[i].fd;
            router_update_adjust(slot, i);
            break;
        }
    }<commit_msg>add debug log<commit_after>#include <xcopy.h>
#include <intercept.h>

static route_table_t  *table = NULL;

/* initiate router table */
int
router_init()
{
    table = (route_table_t *) malloc(sizeof(route_table_t));

    if (table == NULL) {
        tc_log_info(LOG_ERR, 0, "malloc route table error");
        return TC_ERROR;
    }

    memset(table, 0, sizeof(route_table_t));
    
    return TC_OK;
}

inline uint32_t
get_route_key(uint32_t ip, uint16_t port)
{
    uint32_t value = port;

    value = (value << 16) + ip + port;

    return value;
}

static inline void router_update_adjust(route_slot_t *slot, int child) 
{
    int          parent;
    route_item_t tmp;

    if (child < 1) {
        return;
    }

    parent = (child - 1) / 2;
    tmp = slot->items[parent];
    slot->items[parent] = slot->items[child];
    slot->items[child] = tmp;

    return;
}


static void router_add_adjust(route_slot_t *slot, int key, int fd) 
{
    int          i, max, depth;
    route_item_t item, tmp;

    max = slot->num;
    if (max > 0) {
        item = slot->items[0];
        if (max == 1) {
            slot->items[1] = item;
        }
    }

    slot->items[0].key = key;
    slot->items[0].fd = fd;
    slot->items[0].timestamp = tc_current_time_sec;

    depth = 1;
    for (i = 1; i < max; i = (i << 1) + 1) {
        if (slot->items[i].timestamp > slot->items[i + 1].timestamp) {
            ++i;
        }

        /* TODO needs to be optimized */ 
        tmp = slot->items[i];
        slot->items[i] = item;
        item = tmp;
        depth++;
    }
    
    if (depth < ROUTE_ARRAY_DEPTH) {
        slot->items[max] = item;
    }

    if (slot->num < ROUTE_ARRAY_SIZE) {
        slot->num++;
    }
}

/* add item to the router table */
void
router_add(uint32_t ip, uint16_t port, int fd)
{
    int           i, max, existed, index, remainder;
    uint32_t      key;
    route_slot_t *slot;

    key = get_route_key(ip, port);

    index = (key & ROUTE_KEY_HIGH_MASK) >> ROUTE_KEY_SHIFT;
    remainder = key & ROUTE_KEY_LOW_MASK;

    table->cache[index].key = remainder; 
    table->cache[index].fd  = (uint16_t) fd; 

    slot = table->slots + index;

    existed = 0;
    max = ROUTE_ARRAY_SIZE;
    if (slot->num < ROUTE_ARRAY_SIZE) {
        max = slot->num;
    }

    for (i = 0; i < max; i++) {
        if (slot->items[i].key == remainder) {
            slot->items[i].fd = fd;
            slot->items[i].timestamp = tc_current_time_sec;
            existed = 1;
            break;
        }

#if 1
        if (slot->items[i].timestamp == 0) {
            tc_log_info(LOG_WARN, 0, "visit 0 in add");
        }
#endif
    }

    if (!existed) {
        router_add_adjust(slot, remainder, fd);
    } else {
        router_update_adjust(slot, i);
    }

    delay_table_send(get_key(ip, port), fd);

}

int
router_get(uint32_t key)
{
    int           i, fd = 0, index, remainder;
    route_slot_t *slot;

    index = (key & ROUTE_KEY_HIGH_MASK) >> ROUTE_KEY_SHIFT;
    remainder = key & ROUTE_KEY_LOW_MASK;

    if (table->cache[index].key == remainder) {
        table->hit++;
        return (int) table->cache[index].fd;
    }

    slot = table->slots + index;
    for (i = 0; i < ROUTE_ARRAY_SIZE; i++) {
        if (slot->items[i].key == remainder) {
            table->missed++;
            fd = (int) slot->items[i].fd;
            router_update_adjust(slot, i);
            break;
        }
#if 1
        if (slot->items[i].timestamp == 0) {
            tc_log_info(LOG_WARN, 0, "visit 0 in get");
        }
#endif
 
    }
{'fraction_non_alphanumeric': 0.1428779705496428, 'fraction_numerical': 0.013413033969966468, 'mean_word_length': 2.030035335689046, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1181404', 'n_tokens_mistral': 2584, 'n_tokens_neox': 2470, 'n_words': 556}