void TFtrGenSparseNumeric::Split( const TStr& Str, int& Id, TStr& Val) const { if (!Str.IsChIn(':')) { TExcept::Throw("Wrong sparse numeric '" + Str + "'!"); } //split TStr IdStr; Str.SplitOnCh(IdStr, ':', Val); // parse id if (!IdStr.IsInt(Id)) { TExcept::Throw("Wrong sparse numeric '" + Str + "'!"); } }
int TSAppSrvFun::GetFldInt(const TStrKdV& FldNmValPrV, const TStr& FldNm, const int& DefInt) { if (!IsFldNm(FldNmValPrV, FldNm)) { return DefInt; } TStr IntStr = GetFldVal(FldNmValPrV, FldNm, ""); EAssertR(IntStr.IsInt(), "Parameter '" + FldNm + "' not a number"); return IntStr.GetInt(); }
int TSAppSrvFun::GetFldInt(const TStrKdV& FldNmValPrV, const TStr& FldNm) { EAssertR(IsFldNm(FldNmValPrV, FldNm), "Missing parameter '" + FldNm + "'"); TStr IntStr = GetFldVal(FldNmValPrV, FldNm, ""); EAssertR(IntStr.IsInt(), "Parameter '" + FldNm + "' not a number"); return IntStr.GetInt(); }
void TWebTxtBsSrv::OnHttpRq(const int& SockId, const PHttpRq& HttpRq){ // request parameters TStr RqContTypeStr=THttp::TextHtmlFldVal; PUrlEnv UrlEnv; TStr QueryStr; TStr EQueryStr; TStr HitSetStr; TStr AcceptStr; // prepare & extract search-environment if (HttpRq->IsOk()){ // prepare search-environment PUrl Url=HttpRq->GetUrl(); UrlEnv=HttpRq->GetUrlEnv(); // if empty search-environment and url-path is not empty if (UrlEnv->Empty()&& (Url->GetPathSegs()>0)&&(!Url->GetPathSeg(0).Empty())){ // get document name TStr DocNm=Url->GetPathSeg(Url->GetPathSegs()-1); if (WebTxtBs->GetTxtBs()->IsDoc(DocNm)){ // document exists in text-base TStr DocStr=WebTxtBs->GetTxtBs()->GetDocStr(DocNm); PSIn HttpBodySIn=TMIn::New(DocStr); PHttpResp HttpResp= THttpResp::New(THttp::OkStatusCd, RqContTypeStr, false, HttpBodySIn); SendHttpResp(SockId, HttpResp); } else { // ordinary http request TWebSrv::OnHttpRq(SockId, HttpRq); } // end if no search request return; } // extract fields from search-environment QueryStr=UrlEnv->GetVal(QueryUrlFldNm).GetTrunc(); EQueryStr=THtmlLx::GetEscapedStr(QueryStr); HitSetStr=UrlEnv->GetVal(HitSetUrlFldNm).GetTrunc(); AcceptStr=UrlEnv->GetVal(AcceptUrlFldNm).GetTrunc(); if (AcceptStr.Empty()){RqContTypeStr=GetRqContType(HttpRq);} else {RqContTypeStr=AcceptStr;} } // hit-set int HitSetN=1; HitSetStr.IsInt(true, 1, TInt::Mx, HitSetN); int HitSetDocs=GetVarVal(RqContTypeStr, "HitSetDocs").GetInt(); int StrHitSets=GetVarVal(RqContTypeStr, "StrHitSets").GetInt(); // output buffer TChA OutChA(10000); // header TStr HdTpl=GetTplVal(RqContTypeStr, "Header"); HdTpl.ChangeStrAll(QueryMacro, EQueryStr); OutChA+=HdTpl; // html body if (HttpRq->IsOk()){ if (!QueryStr.Empty()){ // execute query PTxtBsRes TxtBsRes=WebTxtBs->Search(QueryStr); TStr EWixExpStr=THtmlLx::GetEscapedStr(TxtBsRes->GetWixExpStr()); // log string TChA QueryInfoChA; QueryInfoChA+="Query: "+QueryStr; //QueryInfoChA+=" ["+GetPeerNm(SockId)+"]"; QueryInfoChA+=" ["+TSecTm::GetCurTm().GetStr()+"]"; TNotify::OnNotify(Notify, ntInfo, QueryInfoChA); SLog->PutStr(QueryInfoChA); SLog->PutLn(); SLog->Flush(); // query-results processing if (TxtBsRes->IsOk()){ // result header TStr ResultHdTpl=GetTplVal(RqContTypeStr, "ResultHd"); ResultHdTpl.ChangeStrAll(QueryMacro, EWixExpStr); ResultHdTpl.ChangeStrAll(HitsMacro, TInt::GetStr(TxtBsRes->GetDocs())); OutChA+=ResultHdTpl; // result records int MnDocN; int MxDocN; TxtBsRes->GetHitSetMnMxDocN(HitSetN, HitSetDocs, MnDocN, MxDocN); for (int DocN=MnDocN; DocN<=MxDocN; DocN++){ // get result document data int MxDocTitleLen=GetVarVal(RqContTypeStr, "MxDocTitleLen").GetInt(); int MxDocCtxLen=GetVarVal(RqContTypeStr, "MxDocCtxLen").GetInt(); TStr DocNm; TStr DocTitleStr; TStr DocStr; TStr DocCtxStr; TxtBsRes->GetDocInfo(DocN, MxDocTitleLen, MxDocCtxLen, DocNm, DocTitleStr, DocStr, DocCtxStr); if (DocTitleStr.Empty()){DocTitleStr=DocNm;} // result record TStr ResultRecTpl=GetTplVal(RqContTypeStr, "ResultRec"); ResultRecTpl.ChangeStrAll(HitNumMacro, TInt::GetStr(DocN+1)); ResultRecTpl.ChangeStrAll(DocAddrMacro, DocNm); ResultRecTpl.ChangeStrAll(DocTitleMacro, DocTitleStr); ResultRecTpl.ChangeStrAll(DocCtxMacro, DocCtxStr); OutChA+=ResultRecTpl; } // result footer TStr ResultFtTpl=GetTplVal(RqContTypeStr, "ResultFt"); OutChA+=ResultFtTpl; // hit-set AddHitSetChA(TxtBsRes, RqContTypeStr, HitSetN, HitSetDocs, StrHitSets, UrlEnv, OutChA); } else { // bad query TStr BadQueryTpl=GetTplVal(RqContTypeStr, "BadQuery"); BadQueryTpl.ChangeStrAll(QueryMacro, EWixExpStr); OutChA+=BadQueryTpl; } } } else { // bad http-request TStr BadHttpRqTpl=GetTplVal(RqContTypeStr, "BadHttpRq"); OutChA+=BadHttpRqTpl; } // footer TStr FtTpl=GetTplVal(RqContTypeStr, "Footer"); FtTpl.ChangeStrAll(QueryMacro, EQueryStr); OutChA+=FtTpl; // construct & send response PSIn HttpBodySIn=TMIn::New(OutChA); PHttpResp HttpResp= THttpResp::New(THttp::OkStatusCd, RqContTypeStr, false, HttpBodySIn); SendHttpResp(SockId, HttpResp); }
// load from allactors.zip that was prepared by Brad Malin in 2005 PImdbNet TImdbNet::LoadTxt(const TStr& ActorFNm) { PImdbNet Net = TImdbNet::New(); TStrV ColV; char line [2024]; int NLines=0, DupEdge=0, Year, Position, ActorNId, MovieNId; TIntH ActorNIdH; THash<TIntPr, TInt> MovieNIdH; FILE *F = fopen(ActorFNm.CStr(), "rt"); fgets(line, 2024, F); while (! feof(F)) { memset(line, 0, 2024); fgets(line, 2024, F); if (strlen(line) == 0) break; TStr(line).SplitOnAllCh('|', ColV, false); IAssert(ColV.Len() == 7); const int NameStrId = Net->AddStr(ColV[0].GetTrunc().GetLc()+" "+ColV[1].GetTrunc().GetLc()); const int MovieStrId = Net->AddStr(ColV[2].GetTrunc().GetLc()); TStr YearStr = ColV[3].GetTrunc(); if (YearStr.Len() > 4) YearStr = YearStr.GetSubStr(0, 3); Year = 1; YearStr.IsInt(Year); const TMovieTy MovieTy = TImdbNet::GetMovieTy(ColV[4]); Position = TInt::Mx; ColV[5].GetTrunc().IsInt(Position); IAssert(ColV[6].GetTrunc()[0] == 'M' || ColV[6].GetTrunc()[0]=='F'); const bool IsMale = ColV[6].GetTrunc()[0] == 'M'; // create nodes if (ActorNIdH.IsKey(NameStrId)) { ActorNId = ActorNIdH.GetDat(NameStrId); } else { ActorNId = Net->AddNode(-1, TImdbNode(NameStrId, Year, Position, IsMale)); ActorNIdH.AddDat(NameStrId, ActorNId); } if (MovieNIdH.IsKey(TIntPr(MovieStrId, Year))) { MovieNId = MovieNIdH.GetDat(TIntPr(MovieStrId, Year)); } else { MovieNId = Net->AddNode(-1, TImdbNode(NameStrId, Year, MovieTy)); MovieNIdH.AddDat(TIntPr(MovieStrId, Year), MovieNId); } if (! Net->IsEdge(ActorNId, MovieNId)) { Net->AddEdge(ActorNId, MovieNId); } else { DupEdge++; } if (++NLines % 100000 == 0) printf("\r %dk ", NLines/1000); } fclose(F); printf("duplicate edges: %d\n", DupEdge); printf("nodes: %d\n", Net->GetNodes()); printf("edges: %d\n", Net->GetEdges()); printf("actors: %d\n", ActorNIdH.Len()); printf("movies: %d\n", MovieNIdH.Len()); // set the actor year to the year of his first movie int NUpdates=0; for (TNet::TNodeI NI = Net->BegNI(); NI < Net->EndNI(); NI++) { if (NI().IsActor()) { int MinYear = NI().GetYear(); for (int e = 0; e < NI.GetOutDeg(); e++) { const TImdbNode& NodeDat = Net->GetNDat(NI.GetOutNId(e)); if (NodeDat.IsMovie()) MinYear = TMath::Mn(MinYear, NodeDat.GetYear()); } if (NI().Year != MinYear) NUpdates++; NI().Year = MinYear; } } printf("updated actor times: %d\n", NUpdates); return Net; }