proxygen
HeaderTable.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree. An additional grant
7  * of patent rights can be found in the PATENTS file in the same directory.
8  *
9  */
10 #pragma once
11 
12 #include <list>
14 #include <string>
15 #include <unordered_map>
16 #include <unordered_set>
17 #include <vector>
18 
19 namespace proxygen {
20 
26 class HeaderTable {
27  public:
28  using names_map = std::unordered_map<HPACKHeaderName, std::list<uint32_t>>;
29 
30  explicit HeaderTable(uint32_t capacityVal) {
31  init(capacityVal);
32  }
33 
34  virtual ~HeaderTable() {}
35  HeaderTable(const HeaderTable&) = delete;
36  HeaderTable& operator=(const HeaderTable&) = delete;
37 
43  virtual bool add(HPACKHeader header);
44 
50  uint32_t getIndex(const HPACKHeader& header) const;
51 
57  const HPACKHeader& getHeader(uint32_t index) const;
58 
62  bool isValid(uint32_t index) const;
63 
67  bool hasName(const HPACKHeaderName& headerName);
68 
72  const names_map& names() const {
73  return names_;
74  }
75 
81  uint32_t nameIndex(const HPACKHeaderName& headerName) const;
82 
86  uint32_t capacity() const {
87  return capacity_;
88  }
89 
94  uint32_t getMaxTableLength(uint32_t capacityVal);
95 
100  virtual bool setCapacity(uint32_t capacity);
101 
105  uint32_t size() const {
106  return size_;
107  }
108 
112  uint32_t bytes() const {
113  return bytes_;
114  }
115 
119  size_t length() const {
120  return table_.size();
121  }
122 
123  bool operator==(const HeaderTable& other) const;
124 
129  uint32_t internalIndex);
130 
131  static uint32_t toInternal(uint32_t head, uint32_t length,
132  uint32_t externalIndex);
133 
134  protected:
138  void init(uint32_t capacityVal);
139 
140  /*
141  * Increase table length to newLength
142  */
143  virtual void increaseTableLengthTo(uint32_t newLength);
144 
145  virtual void resizeTable(uint32_t newLength);
146 
147  virtual void updateResizedTable(uint32_t oldTail, uint32_t oldLength,
148  uint32_t newLength);
149 
155  virtual uint32_t removeLast();
156 
160  void reset();
161 
165  virtual uint32_t evict(uint32_t needed, uint32_t desiredCapacity);
166 
170  uint32_t next(uint32_t i) const;
171 
175  uint32_t tail() const;
176 
180  uint32_t toExternal(uint32_t internalIndex) const;
181 
185  uint32_t toInternal(uint32_t externalIndex) const;
186 
188  uint32_t bytes_{0}; // size in bytes of the current entries
189  std::vector<HPACKHeader> table_;
190 
191  uint32_t size_{0}; // how many entries we have in the table
192  uint32_t head_{0}; // points to the first element of the ring
193 
195 
196  private:
197  /*
198  * Shared implementation for getIndex and nameIndex
199  */
200  uint32_t getIndexImpl(const HPACKHeaderName& header,
201  const folly::fbstring& value,
202  bool nameOnly) const;
203 };
204 
205 std::ostream& operator<<(std::ostream& os, const HeaderTable& table);
206 
207 }
uint32_t capacity() const
Definition: HeaderTable.h:86
virtual uint32_t evict(uint32_t needed, uint32_t desiredCapacity)
std::ostream & operator<<(std::ostream &os, const HeaderTable &table)
bool operator==(const HeaderTable &other) const
bool hasName(const HPACKHeaderName &headerName)
Definition: HeaderTable.cpp:84
uint32_t getMaxTableLength(uint32_t capacityVal)
Definition: HeaderTable.cpp:98
const HPACKHeader & getHeader(uint32_t index) const
Definition: HeaderTable.cpp:93
uint32_t bytes() const
Definition: HeaderTable.h:112
bool isValid(uint32_t index) const
HeaderTable & operator=(const HeaderTable &)=delete
uint32_t next(uint32_t i) const
static uint32_t toExternal(uint32_t head, uint32_t length, uint32_t internalIndex)
uint32_t getIndexImpl(const HPACKHeaderName &header, const folly::fbstring &value, bool nameOnly) const
Definition: HeaderTable.cpp:67
virtual void increaseTableLengthTo(uint32_t newLength)
static uint32_t toInternal(uint32_t head, uint32_t length, uint32_t externalIndex)
size_t length() const
Definition: HeaderTable.h:119
static const char *const value
Definition: Conv.cpp:50
virtual ~HeaderTable()
Definition: HeaderTable.h:34
uint32_t nameIndex(const HPACKHeaderName &headerName) const
Definition: HeaderTable.cpp:88
const names_map & names() const
Definition: HeaderTable.h:72
virtual void updateResizedTable(uint32_t oldTail, uint32_t oldLength, uint32_t newLength)
virtual uint32_t removeLast()
void init(uint32_t capacityVal)
Definition: HeaderTable.cpp:20
virtual void resizeTable(uint32_t newLength)
std::unordered_map< HPACKHeaderName, std::list< uint32_t >> names_map
Definition: HeaderTable.h:28
std::vector< HPACKHeader > table_
Definition: HeaderTable.h:189
virtual bool setCapacity(uint32_t capacity)
uint32_t size() const
Definition: HeaderTable.h:105
uint32_t tail() const
uint32_t getIndex(const HPACKHeader &header) const
Definition: HeaderTable.cpp:63
HeaderTable(uint32_t capacityVal)
Definition: HeaderTable.h:30
virtual bool add(HPACKHeader header)
Definition: HeaderTable.cpp:33