// 检索 // 本函数每次调用前,最好调用一次 TryInitialize() // parameters: // strQuery Search() 专用的检索式。注意,不是一个检索词那么简单 // dbnames 一般是从 targetInfo.DbNames 里面获得。或者从中选择一个数据库名用在这里 // strPreferredRecordSyntax 一般是从 targetInfo.PreferredRecordSyntax 获得即可 // result.Value: // -1 error // 0 fail // 1 succeed // result.ResultCount: // 命中结果集内记录条数 (当 result.Value 为 1 时) public async Task <SearchResult> Search( string strQuery, Encoding queryTermEncoding, string[] dbnames, string strPreferredRecordSyntax, string strResultSetName) { BerTree tree = new BerTree(); SEARCH_REQUEST struSearch_request = new SEARCH_REQUEST { m_dbnames = dbnames }; Debug.Assert(struSearch_request.m_dbnames.Length != 0, ""); struSearch_request.m_strReferenceId = this._currentRefID; struSearch_request.m_lSmallSetUpperBound = 0; struSearch_request.m_lLargeSetLowerBound = 1; struSearch_request.m_lMediumSetPresentNumber = 0; struSearch_request.m_nReplaceIndicator = 1; struSearch_request.m_strResultSetName = strResultSetName; // "default"; struSearch_request.m_strSmallSetElementSetNames = ""; struSearch_request.m_strMediumSetElementSetNames = ""; struSearch_request.m_strPreferredRecordSyntax = strPreferredRecordSyntax; // ZTargetControl.GetLeftValue(this.TargetInfo.PreferredRecordSyntax); // BerTree.MARC_SYNTAX; struSearch_request.m_strQuery = strQuery; struSearch_request.m_nQuery_type = 1; struSearch_request.m_queryTermEncoding = queryTermEncoding; // m_search_response.m_lErrorCode = 0; byte[] baPackage = null; try { // 这里可能抛出异常 tree.SearchRequest(struSearch_request, out baPackage); } catch (Exception ex) { return(new SearchResult { Value = -1, ErrorInfo = "CBERTree::SearchRequest() Exception: " + ex.Message }); } if (this._channel.Connected == false) { this.CloseConnection(); return(new SearchResult { Value = -1, ErrorInfo = "socket尚未连接或者已经被关闭" }); } #if DUMPTOFILE string strBinFile = this.MainForm.DataDir + "\\searchrequest.bin"; File.Delete(strBinFile); DumpPackage(strBinFile, baPackage); string strLogFile = this.MainForm.DataDir + "\\searchrequest.txt"; File.Delete(strLogFile); tree.m_RootNode.DumpToFile(strLogFile); #endif #if NO nRet = CheckConnect( out strError); if (nRet == -1) { return(-1); } #endif BerTree tree1 = new BerTree(); { RecvResult result = await this._channel.SendAndRecv( baPackage); if (result.Value == -1) { return(new SearchResult(result)); } #if NO #if DEBUG if (nRet == 0) { Debug.Assert(strError == "", ""); } #endif #endif #if DUPMTOFILE DeleteFile("searchresponse.bin"); DumpPackage("searchresponse.bin", (char *)baOutPackage.GetData(), baOutPackage.GetSize()); #endif tree1.m_RootNode.BuildPartTree(result.Package, 0, result.Package.Length, out int nTotalLen); } SEARCH_RESPONSE search_response = new SEARCH_RESPONSE(); int nRet = BerTree.GetInfo_SearchResponse(tree1.GetAPDuRoot(), ref search_response, true, out string strError); if (nRet == -1) { return new SearchResult { Value = -1, ErrorInfo = strError } } ; #if DUMPTOFILE DeleteFile("SearchResponse.txt"); tree1.m_RootNode.DumpDebugInfoToFile("SearchResponse.txt"); #endif { SearchResult result = new SearchResult(); result.ResultCount = (int)search_response.m_lResultCount; if (search_response.m_nSearchStatus != 0) // 不一定是1 { result.Value = 1; return(result); } result.ErrorInfo = "Search Fail: diagRecords:\r\n" + search_response.m_diagRecords.GetMessage(); result.Value = 0; // search fail return(result); } }
// 解释包 void menuItem_explainContent_Click(object sender, EventArgs e) { TreeNode node = this.treeView_ber.SelectedNode; if (node == null) { MessageBox.Show(this, "尚未选定节点"); return; } BerNode bernode = (BerNode)node.Tag; if (TreeNodeLevel(node) != 1) { MessageBox.Show(this, "必须是Ber包根节点"); return; } BerNode root = bernode; int nRet = 0; string strError = ""; string strDebugInfo = "OK"; if (root.m_uTag == BerTree.z3950_initRequest) { // 观察Initial请求包 nRet = BerTree.GetInfo_InitRequest( root, out strDebugInfo, out strError); } else if (root.m_uTag == BerTree.z3950_searchRequest) { nRet = BerTree.GetInfo_SearchRequest( root, out strDebugInfo, out strError); } else if (root.m_uTag == BerTree.z3950_presentRequest) { nRet = BerTree.GetInfo_PresentRequest( root, out strDebugInfo, out strError); } else if (root.m_uTag == BerTree.z3950_initResponse) { nRet = BerTree.GetInfo_InitResponse(root, out strDebugInfo, out strError); } else if (root.m_uTag == BerTree.z3950_searchResponse) { nRet = BerTree.GetInfo_SearchResponse(root, out strDebugInfo, out strError); } else if (root.m_uTag == BerTree.z3950_presentResponse) { RecordCollection records = null; SEARCH_RESPONSE search_response = new SEARCH_RESPONSE(); nRet = BerTree.GetInfo_PresentResponse(root, ref search_response, out records, true, out strError); } if (nRet == -1) { MessageBox.Show(this, strError); } else { MessageBox.Show(this, strDebugInfo); } }