aboutsummaryrefslogtreecommitdiffstats
path: root/metis/ccnx/forwarder/metis/processor/metis_MatchingRulesTable.c
blob: f1de22322bc6bf099e425f7eee8d0620f6140cef (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
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */



#include <config.h>
#include <stdio.h>

#include <parc/algol/parc_Memory.h>
#include <parc/algol/parc_Hash.h>

#include <ccnx/forwarder/metis/processor/metis_HashTableFunction.h>
#include <ccnx/forwarder/metis/processor/metis_MatchingRulesTable.h>
#include <LongBow/runtime.h>

struct metis_matching_rules_table {
    // we maintain three hash tables indexed by the different ways
    // one could ask for something.  THis means a content object needs
    // to do three lookups.  We can optimize this later.

    PARCHashCodeTable *tableByName;
    PARCHashCodeTable *tableByNameAndKeyId;
    PARCHashCodeTable *tableByNameAndObjectHash;

    PARCHashCodeTable_Destroyer dataDestroyer;
};

static PARCHashCodeTable *metisMatchingRulesTable_GetTableForMessage(const MetisMatchingRulesTable *pit, const MetisMessage *interestMessage);

// ======================================================================

MetisMatchingRulesTable *
metisMatchingRulesTable_Create(PARCHashCodeTable_Destroyer dataDestroyer)
{
    size_t initialSize = 65535;

    MetisMatchingRulesTable *table = parcMemory_AllocateAndClear(sizeof(MetisMatchingRulesTable));
    assertNotNull(table, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(MetisMatchingRulesTable));
    table->dataDestroyer = dataDestroyer;

    // There is not a Key destroyer because we use the message from the MetisPitEntry as the key

    table->tableByName = parcHashCodeTable_Create_Size(metisHashTableFunction_MessageNameEquals,
                                                       metisHashTableFunction_MessageNameHashCode,
                                                       NULL,
                                                       dataDestroyer,
                                                       initialSize);

    table->tableByNameAndKeyId = parcHashCodeTable_Create_Size(metisHashTableFunction_MessageNameAndKeyIdEquals,
                                                               metisHashTableFunction_MessageNameAndKeyIdHashCode,
                                                               NULL,
                                                               dataDestroyer,
                                                               initialSize);

    table->tableByNameAndObjectHash = parcHashCodeTable_Create_Size(metisHashTableFunction_MessageNameAndObjectHashEquals,
                                                                    metisHashTableFunction_MessageNameAndObjectHashHashCode,
                                                                    NULL,
                                                                    dataDestroyer,
                                                                    initialSize);
    return table;
}

void
metisMatchingRulesTable_Destroy(MetisMatchingRulesTable **tablePtr)
{
    assertNotNull(tablePtr, "Parameter must be non-null double pointer");
    assertNotNull(*tablePtr, "Parameter must dereference to non-null pointer");

    MetisMatchingRulesTable *table = *tablePtr;

    parcHashCodeTable_Destroy(&table->tableByNameAndObjectHash);
    parcHashCodeTable_Destroy(&table->tableByNameAndKeyId);
    parcHashCodeTable_Destroy(&table->tableByName);

    parcMemory_Deallocate((void **) &table);
    *tablePtr = NULL;
}

void *
metisMatchingRulesTable_Get(const MetisMatchingRulesTable *rulesTable, const MetisMessage *message)
{
    assertNotNull(rulesTable, "Parameter rulesTable must be non-null");
    assertNotNull(message, "Parameter message must be non-null");

    PARCHashCodeTable *hashTable = metisMatchingRulesTable_GetTableForMessage(rulesTable, message);
    return parcHashCodeTable_Get(hashTable, message);
}

PARCArrayList *
metisMatchingRulesTable_GetUnion(const MetisMatchingRulesTable *table, const MetisMessage *message)
{
    // we can have at most 3 results, so create with that capacity
    PARCArrayList *list = parcArrayList_Create_Capacity(NULL, NULL, 3);

    void *dataByName = parcHashCodeTable_Get(table->tableByName, message);
    if (dataByName) {
        parcArrayList_Add(list, dataByName);
    }

    if (metisMessage_HasKeyId(message)) {
        void *dataByNameAndKeyId = parcHashCodeTable_Get(table->tableByNameAndKeyId, message);
        if (dataByNameAndKeyId) {
            parcArrayList_Add(list, dataByNameAndKeyId);
        }
    }

    if (metisMessage_HasContentObjectHash(message)) {
        void *dataByNameAndObjectHash = parcHashCodeTable_Get(table->tableByNameAndObjectHash, message);
        if (dataByNameAndObjectHash) {
            parcArrayList_Add(list, dataByNameAndObjectHash);
        }
    }

    return list;
}

void
metisMatchingRulesTable_RemoveFromBest(MetisMatchingRulesTable *rulesTable, const MetisMessage *message)
{
    assertNotNull(rulesTable, "Parameter rulesTable must be non-null");
    assertNotNull(message, "Parameter message must be non-null");

    PARCHashCodeTable *hashTable = metisMatchingRulesTable_GetTableForMessage(rulesTable, message);
    parcHashCodeTable_Del(hashTable, message);
}

void
metisMatchingRulesTable_RemoveFromAll(MetisMatchingRulesTable *rulesTable, const MetisMessage *message)
{
    assertNotNull(rulesTable, "Parameter rulesTable must be non-null");
    assertNotNull(message, "Parameter message must be non-null");

    parcHashCodeTable_Del(rulesTable->tableByName, message);

    // not all messages have a keyid any more
    if (metisMessage_HasKeyId(message)) {
        parcHashCodeTable_Del(rulesTable->tableByNameAndKeyId, message);
    }

    if (metisMessage_HasContentObjectHash(message)) {
        parcHashCodeTable_Del(rulesTable->tableByNameAndObjectHash, message);
    }
}

bool
metisMatchingRulesTable_AddToBestTable(MetisMatchingRulesTable *rulesTable, MetisMessage *key, void *data)
{
    assertNotNull(rulesTable, "Parameter rulesTable must be non-null");
    assertNotNull(key, "Parameter key must be non-null");
    assertNotNull(data, "Parameter data must be non-null");

    PARCHashCodeTable *hashTable = metisMatchingRulesTable_GetTableForMessage(rulesTable, key);

    bool success = parcHashCodeTable_Add(hashTable, key, data);

    return success;
}

void
metisMatchingRulesTable_AddToAllTables(MetisMatchingRulesTable *rulesTable, MetisMessage *key, void *data)
{
    assertNotNull(rulesTable, "Parameter rulesTable must be non-null");
    assertNotNull(key, "Parameter key must be non-null");
    assertNotNull(data, "Parameter data must be non-null");

    parcHashCodeTable_Add(rulesTable->tableByName, key, data);

    // not all messages have a keyid any more
    if (metisMessage_HasKeyId(key)) {
        parcHashCodeTable_Add(rulesTable->tableByNameAndKeyId, key, data);
    }

    parcHashCodeTable_Add(rulesTable->tableByNameAndObjectHash, key, data);
}

// ========================================================================================

static PARCHashCodeTable *
metisMatchingRulesTable_GetTableForMessage(const MetisMatchingRulesTable *pit, const MetisMessage *interestMessage)
{
    PARCHashCodeTable *table;
    if (metisMessage_HasContentObjectHash(interestMessage)) {
        table = pit->tableByNameAndObjectHash;
    } else if (metisMessage_HasKeyId(interestMessage)) {
        table = pit->tableByNameAndKeyId;
    } else {
        table = pit->tableByName;
    }

    return table;
}