forked from PyPatamon/pipelinequery
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLRU_Cache_Batch.h
More file actions
141 lines (127 loc) · 2.55 KB
/
LRU_Cache_Batch.h
File metadata and controls
141 lines (127 loc) · 2.55 KB
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
#include <iostream>
#include<stdint.h>
#include <unordered_map>
#include <vector>
using namespace std;
vector<unsigned>listLength;
struct Node{
unsigned termid;
Node*prev, *next;
};
int64_t CACHE_SIZE = 1024 * 1024;
class LRUCache{
public:
LRUCache();
~LRUCache();
Node* Put(unsigned key);
Node* Get(unsigned key);
void cacheClear();
void print();
uint64_t hit_size;
uint64_t miss_size;
uint64_t hit_count;
uint64_t miss_count;
void attach(Node *node);
void detach(Node *node);
unordered_map<unsigned, Node*>hashmap_;
Node*head_, *tail_;
int64_t sumBytes;
};
LRUCache::LRUCache()
{
miss_size = 0; hit_size = 0;
miss_count = 0; hit_count = 0;
head_ = new Node;
tail_ = new Node;
head_->prev = NULL;
head_->next = tail_;
tail_->prev = head_;
tail_->next = NULL;
sumBytes = 0;
}
LRUCache::~LRUCache()
{
delete head_;
delete tail_;
}
Node* LRUCache::Put(unsigned key)
{
Node *node;
unsigned keylistlength = listLength[key];
if (keylistlength> CACHE_SIZE)
{
cout << "That block overflow!!" << endl;
return NULL;
}
node = tail_->prev;
while (sumBytes + keylistlength>CACHE_SIZE)
{
if (node == head_){ break; }
detach(node);
sumBytes -= listLength[node->termid];
hashmap_.erase(node->termid);
Node *tmp = node->prev;
delete node;
node = tmp;
}
node = new Node();
node->termid = key;
sumBytes += keylistlength;
attach(node);
hashmap_[key] = node;
return node;
}
Node* LRUCache::Get(unsigned key)
{
Node *node;
unordered_map<unsigned, Node* >::iterator it = hashmap_.find(key);
if (it != hashmap_.end())
{
node = it->second;
hit_count++;
detach(node);
attach(node);
}
else
{
miss_count++;
node = Put(key);
miss_size += listLength[key];
}
return node;
}
void LRUCache::attach(Node *node)
{
node->next = head_->next;
head_->next = node;
node->next->prev = node;
node->prev = head_;
}
void LRUCache::detach(Node *node)
{
node->prev->next = node->next;
node->next->prev = node->prev;
}
void LRUCache::print()
{
unordered_map<unsigned, Node* >::iterator iter;
int64_t mysumsize = 0;
for (iter = hashmap_.begin(); iter != hashmap_.end(); iter++)
{
mysumsize += listLength[iter->second->termid];
}
cout << "sumsize=" << mysumsize << endl;
}
void LRUCache::cacheClear()
{
Node *node = tail_->prev;
while (node != head_)
{
detach(node);
sumBytes -= listLength[node->termid];
hashmap_.erase(node->termid);
Node *tmp = node->prev;
delete node;
node = tmp;
}
}