registry/datanet-search-engine-backend/yjs/SearchEngine.yjs
PhiTer9426 641d6cda65 init
2022-05-03 21:07:23 +08:00

411 lines
14 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import "DAC.yjs"
@Permission("RocksDB")
//onCreate时初始化rocksdb
oracle SearchEngine {
function onCreate(){
Global.message = "";
Global.events = [];
Global.allSearchCount=0;//搜索次数
Global.allIndexCount=0;//检索次数
Global.allUpdateCount=0;//更新次数
Global.allInvokeCount=0//调用次数
Global.allRepoCount=0;//Repo数量
//SearchEngine监管的Repo数据
Global.allUserCount=0;//用户数量
Global.AllDoCount=0;//Do数量
//SearchEngine监管的Do数据
initDAC(requester);
Global.se= org.bdware.datanet.searchengine.SearchEngine.createObj("SearchEngineDoMetaIndex");
}
//onRecover初始化索引
function onRecover(){
// Global.se= org.bdware.datanet.searchengine.SearchEngine.createObj("SearchEngineDoMetaIndex");
// Global.message = "";
// Global.events = [];
// Global.allSearchCount=0;//搜索次数
// Global.allIndexCount=0;//检索次数
// Global.allUpdateCount=0;//更新次数
// Global.allInvokeCount=0//调用次数
// Global.allRepoCount=0;//Repo数量
// //SearchEngine监管的Repo数据
// Global.allUserCount=0;//用户数量
// Global.AllDoCount=0;//Do数量
// initDAC(requester);
}
function convertArgToJson(arg){
if (typeof(arg)=='string') return JSON.parse(arg);
return arg;
}
function updateRepoHandler(e) {
var ret = "receive event, topic: " + e.topic + " content: " + e.content;
var params=JSON.parse(e.content);
//print("update"+e.content);
//print(params.contractID);
var res=executeContract(params.contractID+"","getMetaByOffset",e.content);
print("update res "+res.result);
jo=JSON.parse(res.result);
//print("zzz"+JSON.stringify(jo));
//var jo=res.result;
for(var i=0;i<jo.length;i++){
jo[i].doId=params.doId+"/"+jo[i].key;
print("do:"+JSON.stringify(jo));
addDo(JSON.stringify(jo[i]));
}
Global.allUpdateCount++;
}
export function testevent(){
var res0=executeContract("DBRepo","testOffset","");
print("se"+JSON.stringify(res0));
//print(res0.result.data);
var res=executeContract("DBRepo","getMetaByOffset","{\"count\":1,\"offset\":0}");
print("se"+JSON.stringify(res));
jo=res.result;
print("zzz"+JSON.stringify(jo));
for(var i=0;i<jo.length;i++){
jo[i].doId="x.x.x/"+jo[i].key;
//print("zzz"+JSON.stringify(jo[i]));
addDo(JSON.stringify(jo[i]));
}
}
//{"name":"SearchEngine","pubKey":"04da01345770b7e09d4774bf6c0395399b18814aa0b7b158f64b634b8f3d628d9964af6523835225af11e467271f4969d67bf90b32eaa09f517c79b2d1f9b8a926","signature":"804f86b71650e747fef0eb1e822156325aee14f96e848547feeeab6850e34f8b27853ce4aded3a063292e90a349753667caffd944ab45dd8be6076930aaf79c3"}
export function getLRS(arg){
//res=executeContract("Router","getLRS",arg);
//获取到的res为一个对象或者"failed"
//info=JSON.parse(res).result.result;
// Global.rocksdb.put("info",info);
//return res;
//模拟
mockres="{\"date\": \"2016-07-04\",\"name\": \"搜索引擎\",\"doId\": \"86.139.38\",\"address\": \"39.104.209.15\",\"status\": \"已审核\",\"protocol\": \"IRP\",\"pubKey\": \"04da01345770b7e09d4774bf6c0395399b18814aa0b7b158f64b634b8f3d628d9964af6523835225af11e467271f4969d67bf90b32eaa09f517c79b2d1f9b8a926\"}"
info=JSON.parse(mockres);
SearchEnginedb = RocksDBUtil.loadDB("./SearchEngineDBdir/","false");
SearchEnginedb.put("info",JSON.stringify(info));
SearchEnginedb.close();
//return JSON.parse(res).result.result;
return "success";
}
export function getinfo(arg){
//如果空返回null
SearchEnginedb = RocksDBUtil.loadDB("./SearchEngineDBdir/","false");
info=SearchEnginedb.get("info");
SearchEnginedb.close();
return info;
}
export function setinfo(arg){
SearchEnginedb = RocksDBUtil.loadDB("./SearchEngineDBdir/","false");
SearchEnginedb.put("info",arg);
SearchEnginedb.close();
return "success";
}
//{"doId":"aibd.govdata.tj/DBRepo","repoName":"DB类型Repo","contractID":"DBRepo"}
//{"doId":"aibd.govdata.tj/RepoDemo","repoName":"repoDemo","contractID":"repoDemo"}
//添加repo
//1.向对应repo注册
//2.getCount
//3.getMetaByOffset
//4.订阅这个Repo的事件事件名称是RepoID
@Description("示例参数一个JsonObj: {\"doId\":\"aibd.govdata.tj/DBRepo\",\"repoName\":\"DB类型Repo\",\"contractID\":\"DBRepo\"}")
export function addRepo(arg){
arg=convertArgToJson(arg);
//index和不index有啥区别
if(arg.enableIndex==true){
Global.allIndexCount++;
updateEvent=executeContract(arg.contractID+"","getEvent","").result;
//print(event);
//订阅事件
YancloudUtil.subscribe(updateEvent, updateRepoHandler);
Global.events.push(updateEvent);
//向repo注册
var regist={
name:"端节点搜索引擎",contractID:"SearchEngine"};
executeContract(arg.contractID+"","register",JSON.stringify(regist));
//getCount
count=executeContract(arg.contractID+"","getCount","").result;
doCountDB = RocksDBUtil.loadDB("./SearchEngineDoCount/","false");
doCountDB.put(arg.doId,count);
doCountDB.close();
//print(count);
//getMetaByOffset
var param={
};
param.count=count/1;
param.offset=0;
//print(JSON.stringify(param));
///print(arg.contractID);
var ret=executeContract(arg.contractID+"","getMetaByOffset",JSON.stringify(param));
//print(JSON.stringify(ret));
var dolist = ret.result;
for(var i=0;i<dolist.length;i++){
//print(dolist[i].key);
//print(dolist[i].value);
jo=JSON.parse(dolist[i].value);
jo.doId=arg.doId+"/"+dolist[i].key;
//jo.doId=dolist[i].key;
print("do:"+JSON.stringify(jo));
addDo(JSON.stringify(jo));
}
Global.allUpdateCount++;
Global.allRepoCount++;
repodb = RocksDBUtil.loadDB("./SearchEngineRepoList/","false");
repodb.put(arg.doId,JSON.stringify(arg));
repodb.close();
}
return {
"msg":"success"};
}
export function testoffset(arg){
arg=JSON.parse(arg);
count=executeContract(arg.contractID+"","getCount","").result;
//print(count);
var param={
};
param.count=1/1;
param.offset=0;
//print(JSON.stringify(param));
//print(arg.contractID);
var ret=executeContract("DBRepo","getMetaByOffset","{\"count\":1,\"offset\":0}");
//print(JSON.stringify(ret));
}
//{"doId":"86.139.28/d/d","owner":"xxx"}
export function testAddDo(arg){
var jo=convertArgToJson(arg);
if(jo.prefix==undefined){
jo.prefix=jo.doId.substr(0,jo.doId.lastIndexOf('/'));
//print(jo.prefix);
}
Global.allUpdateCount++;
Global.allDoCount++;
dodb=RocksDBUtil.loadDB("./SearchEngineDoList/","false");
//print(jo.doId);
dodb.put(jo.doId,JSON.stringify(jo));
dodb.close();
// print(JSON.stringify(jo));
return Global.se.update(jo);
}
//{"doId":"86.139.28","owner":"xxx"}
export function addDo(arg){
print(arg);
jo=convertArgToJson(arg);
Global.allUpdateCount++;
Global.allDoCount++;
dodb=RocksDBUtil.loadDB("./SearchEngineDoList/","false");
//print(jo.doId);
dodb.put(jo.doId,arg);
dodb.close();
print(JSON.stringify(jo));
return Global.se.update(jo);
}
//86.139.28/abc
@Description("示例参数: \"doi\"")
export function getByDoid(arg){
Global.allSearchCount++;
dodb=RocksDBUtil.loadDB("./SearchEngineDoList/","false");
ret=dodb.get(arg);
dodb.close();
return ret;
}
//{"type":"doid","keyword":"86.139.28/abc"}
//{"type":"owner","keyword":"04f21"}
export function searchDo(arg){
Global.allSearchCount++;
var jo=JSON.parse(arg)
if(jo.count==undefined) jo.count=10;
if(jo.offset==undefined) jo.offset=0;
return Global.se.search(jo);
}
//{"type":"doid","keyword":"86.139.28/abc","count":10,"offset":"0"}
// export function searchByOffset(arg){
// Global.searchCount++;
// arg=JSON.parse(arg)
// return Global.se.search(arg.type,arg.keyword,arg.count,arg.offset);
// }
//{"type":"creatTime","keyword":"creatTime","count":10,"offset":"0","from":"1141363811328","to":"1941363811328"}
export function searchByRange(arg){
Global.allSearchCount++;
arg=JSON.parse(arg)
return Global.se.searchByRange(arg);
}
//多个标签的搜索
//params是一个数组对应若干个key和val
//执行对value的模糊搜索
//count和offset可缺省
//{"params":[{"type":"info","keyword":"说明"},{"type":"doid","keyword":"aibd"}],"count":"10","offset":"0"}
export function searchByMultiTag(arg){
Global.allSearchCount++;
var arg=JSON.parse(arg);
return Global.se.searchByMultiTag(arg);
}
//模糊搜索
// {"keyword":"balabala","count":10,"offset":0}
// export function searchByFuzzy(arg){
// Global.searchCount++;
// arg=JSON.parse(arg)
// return Global.se.search(arg.type,arg.keyword,arg.count,arg.offset);
// }
//"86.139.28/1"
//只支持根据doid删除
@Description("示例参数: \"abc\"")
export function deleteDo(arg){
Global.allUpdateCount++;
Global.allDoCount--;
dodb=RocksDBUtil.loadDB("./SearchEngineDoList/","false");
//这里要删除!!!!
dodb.close();
return Global.se.delete(arg);
}
@Description("遍历KV库无需参数")
export function iterRepo(arg){
repodb=RocksDBUtil.loadDB("./SearchEngineRepoList/","false");
var iter = repodb.newIterator();
var obj = undefined;
var ret = [];
var count=0;
for (iter.seekToFirst();(obj=repodb.getNext(iter))!=undefined;){
ret.push(obj);
count++;
}
repodb.close();
Global.allRepoCount=count;
return {
"data":ret,"total":count};
}
//测试用,待删除
export function iterDo(arg){
Global.allSearchCount++;
dodb=RocksDBUtil.loadDB("./SearchEngineDoList/","false");
var iter = dodb.newIterator();
var obj = undefined;
var ret = [];
var count=0;
for (iter.seekToFirst();(obj=dodb.getNext(iter))!=undefined;){
ret.push(obj);
count++;
}
Global.allDoCount=count;
dodb.close();
return JSON.stringify(ret)
}
//{"doId":"xxx","count":1,"offset":0}
export function getDoList(arg){
Global.allSearchCount++;
arg =convertArgToJson(arg);
if(arg.count==undefined) arg.count=10;
if(arg.offset==undefined) arg.offset=0;
if(arg.doId==undefined){
dodb=RocksDBUtil.loadDB("./SearchEngineDoList/","false");
var iter = dodb.newIterator();
var obj = undefined;
var ret = [];
var i=0;
for (iter.seekToFirst();i<arg.offset&&(obj= dodb.getNext(iter))!=undefined;i++){;};
i=0;
for (;(obj= dodb.getNext(iter))!=undefined&&i<arg.count+arg.offset;i++){
value=convertArgToJson(obj.value);
ret.push(value);
}
return {data: ret, total: ret.length};
}
else{
dodb=RocksDBUtil.loadDB("./SearchEngineDoList/","false");
var iter = dodb.newIterator();
var obj = undefined;
var ret = [];
var i=0;
for (iter.seekToFirst();i<arg.offset&&(obj= dodb.getNext(iter))!=undefined;i++){;};
i=0;
for (;(obj= dodb.getNext(iter))!=undefined&&i<arg.count+arg.offset;i++){
value=convertArgToJson(obj.value);
if(value.prefix==arg.doId){
ret.push(value);
}
}
return {data: ret, total: ret.length};
}
}
export function hello(arg){
return "hello World"+requester;
//return org.bdware.datanet.searchEngine.searchEngine.test();
}
export function testMock(arg){
return 0;
}
export function getAllRepoCount(arg) {
return {"data":Global.allRepoCount};
}
export function getAllUserCount(arg) {
return {"data":Global.allUserCount};
}
export function getAllSearchCount(arg) {
return {"data":Global.allSearchCount};
}
export function getAllUpdateCount(arg) {
return {"data":Global.allUpdateCount};
}
export function getAllDoCount(arg) {
return {"data":Global.allDoCount};
}
export function getAllIndexCount(arg) {
return {"data":Global.allIndexCount};
}
//ControlProxy使用的接口
//参数doid的模糊匹配
//bdware.txte/Repo1/do.newele.data
//输入:"Repo"
//"data":[{
// "doId":"bdware.txte/Repo1","count":10}
// ,{
// "doId":"bdware.txte/Repo2","count":12}
// ]};
export function getDoCount(arg) {
Global.searchCount++;
doCountDB = RocksDBUtil.loadDB("./SearchEngineDoCount/","false");
var iter = doCountDB.newIterator();
var obj = undefined;
var ret = [];
var count=0;
for (iter.seekToFirst();(obj=doCountDB.getNext(iter))!=undefined;){
//print(JSON.stringify(ret));
var matchs_array=obj.key.match(arg);
if(matchs_array==null) continue;
else if(matchs_array.length>0) ret.push({
"doId":obj.key,"count":obj.value/1}
);
}
return {
"data":ret};
}
export function testIndex(arg) {
return Global.se.test();
}
//全量同步
//{"doId":"aibd.govdata.tj/RepoDemo","repoName":"repoDemo","contractID":"repoDemo"}
export function fullSync(arg) {
//getCount
arg=JSON.parse(arg);
count=executeContract(arg.contractID+"","getCount","").result;
doCountDB = RocksDBUtil.loadDB("./SearchEngineDoCount/","false");
doCountDB.put(arg.doId,count);
doCountDB.close();
//print(count);
//getMetaByOffset
var param={
};
param.count=count/1;
param.offset=0;
var ret=executeContract(arg.contractID+"","getMetaByOffset",JSON.stringify(param));
var dolist = JSON.parse(ret.result);
for(var i=0;i<dolist.length;i++){
// print(dolist[i].key);
// print(dolist[i].value);
jo=JSON.parse(dolist[i].value);
jo.doId=arg.doId+"/"+dolist[i].key;
// print(JSON.stringify(jo));
addDo(JSON.stringify(jo));
}
}
}