-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfix-cache.js
157 lines (142 loc) · 5.46 KB
/
fix-cache.js
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
const { cacheDB, putItems } = require('./base.js');
class FixCache{
#fileCache; // the file cache of probable bug files
#cacheSize; // cache size
#fixKeywords; // fix key words
constructor(cacheSize, fixKeywords){
this.#fileCache = cacheDB;
this.cacheSize = cacheSize;
this.#fixKeywords = fixKeywords;
};
/* initializes the file cache
*/
async initCache(repoID, files){
var newCache = [];
files.forEach(file =>{
newCache.Push({
"repo": repoID,
"file": file,
"last_hit": Date.now(),
"number_of_hits": 1,
});
});
await this.#fileCache.putItems(newCache);
return Promise.resolve(null);
}
/* gets the current file cache
returns objects with {"file_name": number of hits}
*/
async getCurrentCache(repoID){
const currentCache = this.#fileCache.fetch({"repo": repoID}, this.#cacheSize);
var cacheFiles = {};
for await(const cacheItem of currentCache){
for (const item of cacheItem){
console.log("cache item", item);
cacheFiles[item.file] = item.number_of_hits;
}
}
return Promise.resolve(cacheFiles);
}
/*
updates cache with new files
uses Least Recently Used (LRU) replacement method
*/
async updateCache(repoID, files){
if (files.length === 0) {
return Promise.resolve(null);
}
// get current cache items from the database
const asyncCurrentCache = this.#fileCache.fetch({"repo": repoID}, this.#cacheSize);
var currentCache = [];
for await (const values of asyncCurrentCache){
values.forEach(value => {
currentCache.push(value);
});
}
// if cache is empty
if (currentCache.length === 0){
let newCache = {};
files.forEach(file => {
if (newCache[file]){
newCache[file]["last_hit"] = Date.now();
newCache[file]["number_of_hits"] +=1;
} else{
newCache[file] = {
"repo": repoID,
"file": file,
"last_hit": Date.now(),
"number_of_hits":1,
}
}
// early stop if number of files is greater than cache size
// TODO: better way of replacing files in this scneario
// paper does not discuss a replacement policy in this scenario
if (Object.keys(newCache).length === 25){
return;
}
});
try {
await putItems(this.#fileCache, Object.values(newCache));
return Promise.resolve(null);
} catch(err){
return Promise.reject(err);
}
}
// sort cache by decreasing order by date of last hit
currentCache.sort((a, b) => {
new Date(a.last_hit) > new Date(b.last_hit) ? 1 : -1;
});
// lookup table for improving complexity
var currentCacheLookupTable = {};
currentCache.forEach(cacheItem => {
currentCacheLookupTable[`${cacheItem.file}`] = cacheItem
})
// least recently used file index
// init as last element as currentCache is sorted in decreasing order by date
var leastHitIndex = currentCache.length-1;
files.forEach(file => {
// if already in cache, update cache item
if (currentCacheLookupTable[file]){
currentCacheLookupTable[file].number_of_hits+=1;
currentCacheLookupTable[file].last_hit = Date.now();
} else{
// add new file to cache
currentCacheLookupTable[file] = {
"repo": repoID,
"file": file,
"last_hit": Date.now(),
"number_of_hits": 1,
}
// delete least recently hit file
// if number of cache items is already greater than cache size
if (leastHitIndex > 0 && Object.keys(currentCacheLookupTable).length > this.#cacheSize){
const leastRecentlyHit = currentCache[leastHitIndex];
delete currentCacheLookupTable[leastRecentlyHit];
leastHitIndex--;
// early stop if there are no more files to replace but cache is already full
// TODO: think of a better way to handle this
// paper does not show a replacement policy for this scenario
if (leastHitIndex === 0 && Object.keys(currentCacheLookupTable).length === this.#cacheSize){
return;
}
}
}
})
// update the cache
await putItems(this.#fileCache, Object.values(currentCacheLookupTable));
return Promise.resolve(null);
}
isFixMessage(message, excludeMerge){
// exclude merge commits
if (excludeMerge && message.includes("Merge pull request")){
return false;
}
for (const keyword of this.#fixKeywords){
if (message.toLowerCase().includes(keyword)){
return true;
}
}
return false;
}
}
module.exports = { FixCache };