Hybrid ICN (hICN) plugin  v21.06-rc0-4-g18fa668
content_store.h
1 /*
2  * Copyright (c) 2017-2019 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #pragma once
17 
18 #include <hicn/transport/utils/spinlock.h>
19 
20 #include <mutex>
21 
22 namespace transport {
23 
24 namespace core {
25 class Name;
26 class ContentObject;
27 class Interest;
28 } // namespace core
29 
30 } // namespace transport
31 
32 namespace utils {
33 
34 using Name = transport::core::Name;
35 using ContentObject = transport::core::ContentObject;
36 using Interest = transport::core::Interest;
37 
38 typedef std::pair<std::shared_ptr<ContentObject>,
39  std::chrono::steady_clock::time_point>
40  ObjectTimeEntry;
41 typedef std::pair<ObjectTimeEntry,
42  std::list<std::reference_wrapper<const Name>>::iterator>
43  ContentStoreEntry;
44 typedef std::list<std::reference_wrapper<const Name>> FIFOList;
45 typedef std::unordered_map<Name, ContentStoreEntry> ContentStoreHashTable;
46 
47 class ContentStore {
48  public:
49  explicit ContentStore(std::size_t max_packets = (1 << 16));
50 
51  ~ContentStore();
52 
53  void insert(const std::shared_ptr<ContentObject> &content_object);
54 
55  std::shared_ptr<ContentObject> find(const Name &name);
56 
57  void erase(const Name &exact_name);
58 
59  void setLimit(size_t max_packets);
60 
61  size_t getLimit() const;
62 
63  size_t size() const;
64 
65  void printContent();
66 
67  private:
68  ContentStoreHashTable content_store_hash_table_;
69  FIFOList fifo_list_;
70  std::shared_ptr<ContentObject> empty_reference_;
71  // Must be atomic
72  std::atomic_size_t max_content_store_size_;
73  mutable utils::SpinLock cs_mutex_;
74 };
75 
76 } // end namespace utils
transport::core::Name
Definition: name.h:45
utils::ContentStore
Definition: content_store.h:47
transport::core::ContentObject
Definition: content_object.h:29
transport::core::Interest
Definition: interest.h:30
transport
Definition: forwarder_config.h:32
utils::SpinLock
Definition: spinlock.h:22