-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer copy 2.js
208 lines (181 loc) · 5.98 KB
/
renderer copy 2.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// No Node.js APIs are available in this process because
// `nodeIntegration` is turned off. Use `preload.js` to
// selectively enable features needed in the rendering
// process.
var Mock = window.require('mockjs');
var nodejieba = window.require('nodejieba');
var result = nodejieba.cut('一个南京市长江大桥');
console.log('~~~~~~~', result);
const si = require('search-index')
let searchDB
si({
name: 'ELECTRON-FULLTEXT-SEARCH-DB'
}).then((result) => {
searchDB = result;
})
// 大约是 1369.73 MB
const ignoreChars = " \t\r\n~!@#$%^&*()_+-=【】、{}|;':\",。、《》?αβγδεζηθικλμνξοπρστυφχψωΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ。,、;:?!…—·ˉ¨‘’“”々~‖∶"'`|〃〔〕〈〉《》「」『』.〖〗【】()[]{}ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ⒈⒉⒊⒋⒌⒍⒎⒏⒐⒑⒒⒓⒔⒕⒖⒗⒘⒙⒚⒛㈠㈡㈢㈣㈤㈥㈦㈧㈨㈩①②③④⑤⑥⑦⑧⑨⑩⑴⑵⑶⑷⑸⑹⑺⑻⑼⑽⑾⑿⒀⒁⒂⒃⒄⒅⒆⒇≈≡≠=≤≥<>≮≯∷±+-×÷/∫∮∝∞∧∨∑∏∪∩∈∵∴⊥∥∠⌒⊙≌∽√§№☆★○●◎◇◆□℃‰€■△▲※→←↑↓〓¤°#&@\︿_ ̄―♂♀┌┍┎┐┑┒┓─┄┈├┝┞┟┠┡┢┣│┆┊┬┭┮┯┰┱┲┳┼┽┾┿╀╁╂╃└┕┖┗┘┙┚┛━┅┉┤┥┦┧┨┩┪┫┃┇┋┴┵┶┷┸┹┺┻╋╊╉╈╇╆╅╄";
// 这样一条 msg,大概是 2.11857421875 kb,那么 200mb 的 db 乐观估计是十万条。
var obj = {
cc: true,
"flow|1": [
"out",
"in",
],
"from|1": [
"cjhz1",
"wangsitu1",
"cs1",
"cs2",
"cs3",
"cs4"
],
"fromClientType|1": [
"Web",
"Android",
"IOS"
],
"fromDeviceId|1": [
"2fb0d8c26d874a4790a92719a186bea0",
"3ab0ddc2rtjk4a4790a92719o98jbAa1",
"4ab0ddc2rtjk4a4790a92719o98jbAa1",
],
"fromNick|1": [
"cjhz1",
"wangsitu1",
"cs1",
"cs2",
"cs3",
"cs4"
],
"idClient": /\w{32}/,
"idServer": /\d{12}/,
isHistoryable: true,
isLocal: false,
isOfflinable: true,
isPushable: true,
isReplyMsg: true,
isRoamingable: true,
isSyncable: true,
isUnreadable: true,
needMsgReceipt: false,
needPushNick: true,
resend: false,
scene: "p2p",
"sessionId": /p2p-cs\d{3}/,
status: "success",
"target|1": [
"cjhz1",
"wangsitu1",
"cs1",
"cs2",
"cs3",
"cs4"
],
text: Mock.Random.cparagraph(2, 10),
"time|1600000000000-1700000000000": 1,
"to|1": [
"cjhz1",
"wangsitu1",
"cs1",
"cs2",
"cs3",
"cs4"
],
type: "text",
"userUpdateTime|1600000000000-1700000000000": 1
}
var request = window.indexedDB.open('nim-cjhz2');
var db;
request.onerror = function (event) {
console.log('数据库打开报错', event);
};
request.onsuccess = function (event) {
db = request.result;
console.log('数据库打开成功,拥有三个方法:writeData、readByKeyword、readByPrimary、printUseSize');
};
request.onupgradeneeded = function (event) {
db = event.target.result;
var objectStore;
console.log('onupgradeneeded:', db.objectStoreNames);
if (!db.objectStoreNames.contains('msg1')) {
var objectStore = db.createObjectStore(
'msg1',
// { keyPath: 'id', autoIncrement: true }
{ keyPath: 'idClient' }
);
objectStore.createIndex('idx_idClient', 'idClient', { unique: true });
// objectStore.createIndex('idx_fulltext', 'terms', { multiEntry: true });
}
}
function readByPrimary(id) {
var transaction = db.transaction(['msg1']);
var objectStore = transaction.objectStore('msg1');
var request = objectStore.get(id);
console.time('readByPrimary last')
request.onerror = function(event) {
console.log('事务失败');
};
request.onsuccess = function(event) {
console.timeEnd('readByPrimary last')
if (request.result) {
console.log('GET: ', request.result);
} else {
console.log('未获得数据记录');
}
};
}
async function readByKeyword(text, limit) {
// var transaction = db.transaction(['msg1']);
// var objectStore = transaction.objectStore('msg1');
// var index = objectStore.index('idx_fulltext');
console.time('readByKeyword last')
// FullText.search(index, text, 'ch', ids => {
// console.log('query:', text, 'results:', ids);
// console.timeEnd('readByKeyword last')
// }, limit)
// searchDB.INDEX.STORE.clear() 清除所有
var searchParams = nodejieba.cut(text).filter(word => !ignoreChars.includes(word))
await searchDB.QUERY({
SEARCH: searchParams
}).then(console.log)
console.timeEnd('readByKeyword last')
}
function writeData(num = 100) {
var transaction = db.transaction(['msg1'], 'readwrite');
var objectStore = transaction.objectStore('msg1');
console.log('写事务开始:');
let tempTime = new Date().getTime()
let fts = []
for (let i = 0; i < num; i++) {
let temp = Mock.mock(obj)
let text = Mock.Random.cparagraph(2, 10);
fts.push({
_id: temp.idClient,
idx: nodejieba.cut(text).filter(word => !ignoreChars.includes(word))
})
objectStore.add({
...temp,
text: text
// terms: FullText.tokenize(temp.text, 'ch').filter(word => !ignoreChars.includes(word))
// terms: nodejieba.cut(temp.text).filter(word => !ignoreChars.includes(word))
});
}
searchDB.PUT(fts).then(() => {
console.log('search-index save success, last: ', new Date().getTime() - tempTime)
});
// do add 100 times
transaction.oncomplete = function (event) {
console.log('transaction success, writeData last: ', new Date().getTime() - tempTime);
};
transaction.onerror = function (event) {
console.log('transaction error: ' + transaction.error);
};
}
async function printUseSize() {
var obj = await navigator.storage.estimate();
var size = (obj.usageDetails.indexedDB / 1024 / 1024).toFixed(2);
console.log(`${size} MB`)
}