1/* 2 * Copyright (C) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16importScripts("trace_streamer_builtin.js", "TempSql.js"); 17self.onerror = function (error: any) { 18} 19let Module: any = null; 20 21function initWASM() { 22 return new Promise((resolve, reject) => { 23 // @ts-ignore 24 let wasm = trace_streamer_builtin_wasm 25 Module = wasm({ 26 locateFile: (s: any) => { 27 return s 28 }, 29 print: (line: any) => { 30 }, 31 printErr: (line: any) => { 32 }, 33 onRuntimeInitialized: () => { 34 resolve("ok"); 35 }, 36 onAbort: () => { 37 reject("on abort"); 38 } 39 }); 40 }) 41} 42 43const REQ_BUF_SIZE = 32 * 1024 * 1024; 44self.onmessage = async (e: MessageEvent) => { 45 if (e.data.action === "open") { 46 await initWASM(); 47 // @ts-ignore 48 self.postMessage({id: e.data.id, action: "open", ready: true,index:0}); 49 let uint8Array = new Uint8Array(e.data.buffer); 50 let p = Module._malloc(uint8Array.length); 51 Module.HEAPU8.set(uint8Array, p); 52 let r1 = Module._TraceStreamerParseData(p, uint8Array.length); 53 let r2 = Module._TraceStreamerParseDataOver(); 54 if(r1 == -1){ 55 // @ts-ignore 56 self.postMessage({id: e.data.id, action: "open", init: false,msg:"parse data error"}); 57 return ; 58 } 59 // @ts-ignore 60 temp_init_sql_list.forEach((item, index) => { 61 let r = createView(item); 62 // @ts-ignore 63 self.postMessage({id: e.data.id, ready: true, index: index + 1}); 64 }); 65 // @ts-ignore 66 self.postMessage({id: e.data.id, action: "open", init: true,msg:"ok"}); 67 } else if (e.data.action === "exec") { 68 let arr = query(e.data.name, e.data.sql, e.data.params); 69 // @ts-ignore 70 self.postMessage({id: e.data.id, action: "exec", results: arr}); 71 } 72} 73 74function createView(sql: string) { 75 let enc = new TextEncoder(); 76 let dec = new TextDecoder(); 77 let sqlPtr = Module._malloc(sql.length); 78 Module.HEAPU8.set(enc.encode(sql), sqlPtr); 79 let res = Module._TraceStreamerSqlOperate(sqlPtr, sql.length); 80 return res; 81} 82 83function query(name: string, sql: string, params: any) { 84 if (params) { 85 Reflect.ownKeys(params).forEach((key: any) => { 86 if(typeof params[key] ==="string"){ 87 sql = sql.replace(new RegExp(`\\${key}`, "g"), `'${params[key]}'`); 88 }else{ 89 sql = sql.replace(new RegExp(`\\${key}`, "g"), params[key]); 90 } 91 }); 92 } 93 let arr: Array<any> = [] 94 let enc = new TextEncoder(); 95 let dec = new TextDecoder(); 96 let sqlPtr = Module._malloc(sql.length); 97 let outPtr = Module._malloc(REQ_BUF_SIZE); 98 Module.HEAPU8.set(enc.encode(sql), sqlPtr); 99 let a = new Date().getTime(); 100 let res = Module._TraceStreamerSqlQuery(sqlPtr, sql.length, outPtr, REQ_BUF_SIZE); 101 let out = Module.HEAPU8.subarray(outPtr, outPtr + res); 102 let str = dec.decode(out); 103 Module._free(sqlPtr); 104 Module._free(outPtr); 105 str = str.substring(str.indexOf("\n") + 1); 106 let parse = JSON.parse(str); 107 let columns = parse.columns; 108 let values = parse.values; 109 for (let i = 0; i < values.length; i++) { 110 let obj: any = {} 111 for (let j = 0; j < columns.length; j++) { 112 obj[columns[j]] = values[i][j] 113 } 114 arr.push(obj) 115 } 116 return arr 117}