-
Notifications
You must be signed in to change notification settings - Fork 0
/
services.js
155 lines (144 loc) · 3.65 KB
/
services.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
const Entry = require("./models/Entry");
const createRBTree = require("functional-red-black-tree");
var table = [];
var cache = [];
var mapcapacity = 16;
var size = 0;
var rehashCount = 0;
function showTable() {
return cache;
}
function hash(key) {
let ind = 0;
for (let i = 0; i < key.length; i++) {
ind += key.charCodeAt(i);
}
ind = ind % mapcapacity;
return ind;
}
function rehash() {
rehashCount++;
mapcapacity = 2 * mapcapacity;
let temp = table;
table = [];
size = 0;
temp.forEach((node) => {
const keys = node.keys;
const values = node.values;
for (let i = 0; i < keys.length; i++) {
insertion(keys[i], values[i]);
}
// while(node!=null){
// insertion(node.key,node.value);
// node=node.next;
// }
});
cache = table;
rehashCount--;
}
function sleep(s) {
return new Promise((resolve) => {
setTimeout(resolve, s);
});
}
async function insertion(key, value) {
if (size == mapcapacity * 8) {
if(rehashCount==0) {
rehash();
} else{
// delay the request
await sleep(5000);
}
}
let index = hash(key);
// var newnode = new Entry(key,value,null);
if (cache[index] == null) {
var rbTree = createRBTree();
cache[index] = rbTree.insert(key, value);
table[index] = rbTree.insert(key, value);
size++;
} else {
let currentTree = cache[index];
if (currentTree.get(key) == null) {
currentTree = currentTree.insert(key, value);
size++;
} else {
var iter = currentTree.find(key);
currentTree = iter.update(value);
cache[index] = currentTree;
table[index] = currentTree;
return `${key} was already present, Value updated!`;
}
cache[index] = currentTree;
table[index] = currentTree;
}
// if(table[index] == null){
// table[index] = newnode;
// size++;
// } else {
// let previousNode = null;
// let currentNode = table[index];
// while(currentNode!=null){
// if(currentNode.key == key){
// currentNode.value = value;
// return `${key} was already present, Value updated!`;
// }
// previousNode = currentNode;
// currentNode = currentNode.next;
// }
// if(previousNode != null && currentNode == null){
// size++;
// previousNode.next = newnode;
// }
// }
console.log(`${index} ${size}`);
return `${key} - ${value} inserted successfully`;
}
function getvalueofkey(key) {
const index = hash(key);
let listHead = cache[index];
if (listHead) {
return listHead.get(key);
// while(listHead!=null){
// if(listHead.key == key){
// return listHead.value;
// }
// listHead = listHead.next;
// }
}
return null;
}
function deletekeyvaluepair(key) {
const index = hash(key);
let previousNode = null;
let listHead = cache[index];
if (listHead) {
if (listHead.get(key)) {
cache[index] = listHead.remove(key);
table[index] = listHead.remove(key);
return true;
}
// while(listHead!=null){
// if(listHead.key == key){
// if(previousNode == null){
// listHead = listHead.next;
// table[index] = listHead;
// return true;
// } else{
// previousNode.next = listHead.next;
// return true;
// }
// }
// previousNode = listHead;
// listHead = listHead.next;
// }
}
return false;
}
module.exports = {
showTable,
hash,
insertion,
deletekeyvaluepair,
getvalueofkey,
};