aboutsummaryrefslogtreecommitdiffstats
path: root/hicn-light/src/hicn/core/content_store.c
blob: 74b6752246e20ced59afd86d09f87279b3607b61 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
 * Copyright (c) 2020 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.
 */

/**
 * \file content_store.c
 * \brief Implementation of hICN content_store
 */

#include <inttypes.h>
#include <hicn/base/pool.h>
#include <hicn/util/log.h>

//#include <hicn/content_store/lru.h>

#include "content_store.h"

extern const cs_ops_t cs_lru;

const cs_ops_t * const cs_vft[] = {
  [CS_TYPE_LRU] = &cs_lru,
};

// XXX TODO replace by a single packet cache
// XXX TODO per cs type entry data too !
// XXX TODO getting rid of logger and the need to acquire
// XXX TODO separate cs from vft, same with strategy

#define cs_entry_from_msgbuf(entry, msgbuf)                          \
do {                                                                            \
  (entry)->hasExpiryTimeTicks = msgbuf_HasContentExpiryTime(msgbuf);            \
  if ((entry)->hasExpiryTimeTicks)                                              \
    (entry)->expiryTimeTicks = msgbuf_GetContentExpiryTimeTicks(msgbuf);        \
} while(0)

/* This is only used as a hint for first allocation, as the table is resizeable */
#define DEFAULT_CS_SIZE 64

cs_t *
_cs_create(cs_type_t type, size_t init_size, size_t max_size)
{
    if (!CS_TYPE_VALID(type)) {
        ERROR("[cs_create] Invalid content store type");
        return NULL;
    }

    if (init_size == 0)
        init_size = DEFAULT_CS_SIZE;

    cs_t * cs = malloc(sizeof(cs_t));
    if (!cs)
        return NULL;

    cs->type = type;

    // XXX TODO an entry = data + metadata specific to each policy
    pool_init(cs->entries, init_size, max_size);

    // data
    // options
    // stats


    // index by name
    cs->index_by_name = kh_init(cs_name);

#if 0
    cs->index_by_expiry_time = NULL;
    if (!cs->index_by_expiry_time) {
        ERROR("Could not create index (expiry time)");
        goto ERR_INDEX_EXPIRY;
    }
#endif

    cs_vft[type]->initialize(cs);

    return cs;
#if 0
ERR_INDEX_EXPIRY:
    free(cs);
    // XXX

    return NULL;
#endif
}

void
cs_free(cs_t * cs)
{
    cs_vft[cs->type]->finalize(cs);

#if 0
    if (cs->index_by_expiry_time)
        ; //listTimeOrdered_Release(&(store->indexByExpirationTime));
#endif
}

void cs_clear(cs_t * cs)
{
    // XXX TODO
}

off_t
cs_match(cs_t * cs, off_t msgbuf_id, uint64_t now)
{
    assert(cs);

    const msgbuf_pool_t * msgbuf_pool = cs_get_msgbuf_pool(cs);
    const msgbuf_t * msgbuf = msgbuf_pool_at(msgbuf_pool, msgbuf_id);

    assert(msgbuf);
    assert(msgbuf_get_type(msgbuf) == MSGBUF_TYPE_INTEREST);

    /* Lookup entry by name */
    khiter_t k = kh_get_cs_name(cs->index_by_name, msgbuf_get_name(msgbuf));
    if (k == kh_end(cs->index_by_name))
        return INVALID_MSGBUF_ID;
    cs_entry_t * entry = cs->entries + kh_val(cs->index_by_name, k);
    assert(entry);

    /* Remove any expired entry */
    if (cs_entry_has_expiry_time(entry) &&
            cs_entry_get_expiry_time(entry) < now) {
        // the entry is expired, we can remove it
        cs_remove_entry(cs, entry);
        goto NOT_FOUND;
    }

    cs->stats.lru.countHits++;

#if 0 // XXX
    contentStoreEntry_MoveToHead(entry);
#endif

    DEBUG("CS %p LRU match %p (hits %" PRIu64 ", misses %" PRIu64 ")",
           cs, msgbuf, cs->stats.lru.countHits, cs->stats.lru.countMisses);
    return cs_entry_get_msgbuf_id(entry);

NOT_FOUND:
    cs->stats.lru.countMisses++;

    DEBUG("ContentStoreLRU %p missed msgbuf %p (hits %" PRIu64 ", misses %" PRIu64 ")",
            cs, msgbuf, cs->stats.lru.countHits, cs->stats.lru.countMisses);
    return INVALID_MSGBUF_ID;
}

// XXX temp
// XXX pool member pointer might change, not the ID.
#define msgbuf_acquire(x) (x)

cs_entry_t *
cs_add(cs_t * cs, off_t msgbuf_id, uint64_t now)
{
    assert(cs);
    assert(msgbuf_id_is_valid(msgbuf_id));

#if DEBUG
    forwarder_t * forwarder = cs_get_forwarder(cs);
    msgbuf_t * msgbuf = msgbuf_pool_at(msgbuf_pool, msgbuf_id);
    assert(msgbuf_get_type(msgbuf) == MSGBUF_TYPE_DATA);
#endif

#if 0
    // entry exists ?
    cs_entry_t *dataEntry = parcHashCodeTable_Get(data->storageByName, content);
    if(dataEntry)
        _cs_lru_purge_entry(data, dataEntry);
#endif

#if 0
    // check expiration
    uint64_t expiryTimeTicks = contentStoreEntry_MaxExpiryTime;
    if (message_HasContentExpiryTime(content))
        expiryTimeTicks = message_GetContentExpiryTimeTicks(content);

    // Don't add anything that's already expired or has exceeded RCT.
    if (now >= expiryTimeTicks)
        return false;
#endif

#if 0
    // evict
    if (data->objectCount >= data->objectCapacity)
        // Store is full. Need to make room.
        _evictByStorePolicy(data, now);
#endif

    cs_entry_t * entry = NULL;
    off_t entry_id = pool_get(cs->entries, entry);
    if (!entry)
        goto ERR_ENTRY;

    *entry = (cs_entry_t) {
        .msgbuf_id = msgbuf_id,
        .hasExpiryTimeTicks = false, // XXX
        .expiryTimeTicks = 0, // XXX
    };

    // update indices

    // update policy index
    /* eg. LRU: add new the entry at the head of the LRU */
    if (!cs_vft[cs->type]->add_entry(cs, entry_id))
        goto ERR_VFT;

#if 0
    // update expiry time index
    if (cs_entry_has_expiry_time(entry)) {
    }
#endif

#if 0
    // stats
    data->objectCount++;
    data->stats.countAdds++;
#endif

    return entry;

ERR_VFT:
    pool_put(cs->entries, entry);
ERR_ENTRY:
    return NULL;
}

int
cs_remove_entry(cs_t * cs, cs_entry_t * entry)
{
    assert(cs);
    assert(entry);

    if (cs_entry_has_expiry_time(entry))
        ; // XXX TODO listTimeOrdered_Remove(store->indexByExpirationTime, entryToPurge);

    off_t msgbuf_id = cs_entry_get_msgbuf_id(entry);

    const msgbuf_pool_t * msgbuf_pool = cs_get_msgbuf_pool(cs);
    const msgbuf_t * msgbuf = msgbuf_pool_at(msgbuf_pool, msgbuf_id);

    khiter_t k = kh_get_cs_name(cs->index_by_name, msgbuf_get_name(msgbuf));
    if (k != kh_end(cs->index_by_name))
        kh_del(cs_name, cs->index_by_name, k);

    // This will take care of LRU entry for instance
    cs_vft[cs->type]->remove_entry(cs, entry);

    //store->objectCount--;
    pool_put(cs->entries, entry);

    return 0;
}
//
// XXX TODO what is the difference between purge and remove ?
bool
cs_remove(cs_t * cs, msgbuf_t * msgbuf)
{
    assert(cs);
    assert(msgbuf);
    assert(msgbuf_get_type(msgbuf) == MSGBUF_TYPE_DATA);

    /* Lookup entry by name */
    khiter_t k = kh_get_cs_name(cs->index_by_name, msgbuf_get_name(msgbuf));
    if (k == kh_end(cs->index_by_name))
        return false;

    cs_entry_t * entry = cs->entries + kh_val(cs->index_by_name, k);
    assert(entry);

    cs_remove_entry(cs, entry);
    return true;
}