/// <summary> /// Try to get the row at the given index from the buffer or cache /// </summary> /// <param name="cursor"></param> /// <returns></returns> public bool TryToGetNextRow( VoArrayListCursor cursor) { object[] vo = null; int ri = cursor.Position + 1; AssertMx.IsTrue(ri >= 0, "Invalid row index:" + ri); if (ri >= TotalRowCount) { if (DebugCaching) { DebugLog.Message(string.Format("Row {0} not in cache or buffer (cacheSize: {1}, bufferSize: {2})", ri, RowsWrittenToCache, RowBuffer.Count)); } return(false); // not in collection } vo = GetRow(ri); cursor.CurrentRow = vo; cursor.Position = ri; return(true); }
/// <summary> /// Indexer into pages /// </summary> /// <param name="cpi"></param> /// <returns></returns> public ResultsPage this[int cpi] { get { AssertMx.IsTrue(cpi >= 0 && cpi < Pages.Count); return(Pages[cpi]); } set { AssertMx.IsTrue(cpi >= 0 && cpi < Pages.Count); Pages[cpi] = value; } }
/// <summary> /// Get/set a single element in the ItemArray object array by col name /// </summary> /// <param name="columnName"></param> /// <returns></returns> public object this[string columnName] { get { int index = Table.Columns.IndexOf(columnName); AssertMx.IsTrue(index >= 0, "Column not found: " + columnName); return(this[index]); } set { int index = Table.Columns.IndexOf(columnName); AssertMx.IsTrue(index >= 0, "Column not found: " + columnName); this[index] = value; } }
/// <summary> /// Read in all of the rows that have been cached /// </summary> /// <returns></returns> public List <object[]> ReadCachedRows() { AssertMx.IsTrue(File.Exists(CacheFilePath), "CacheFilePath doesn't exist"); FileStream fs = File.Open(CacheFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); BinaryReader br = new BinaryReader(fs); List <object[]> cachedRows = new List <object[]>(); // in-memory buffer list of rows for (int ri = 0; ri < RowsWrittenToCache; ri++) { object[] vo = VoArray.ReadBinaryVoArray(br, VoaLength); cachedRows.Add(vo); } br.Close(); // close reader and the underlying stream return(cachedRows); }
/// <summary> /// ExecuteSearch /// </summary> /// <param name="queryMol"></param> public List <StructSearchMatch> ExecuteSearch( IAtomContainer queryMol) { AssertMx.IsTrue(FingerprintType == FingerprintType.MACCS || FingerprintType == FingerprintType.Circular, "Invalid FingerprintType: " + FingerprintType); QueryMol = queryMol; BitSetFingerprint fp = // generate a fingerprint CdkMol.BuildBitSetFingerprintForLargestFragment(queryMol, FingerprintType); QueryFpCardinality = fp.cardinality(); QueryFpLongArray = fp.asBitSet().toLongArray(); MatchList = new List <StructSearchMatch>(); ThreadException = null; foreach (string databaseName in FingerprintDbMx.Databases) // loop on all databases { int srcId = -1; if (Lex.Contains(databaseName, "corp")) { if (!GetCorpSim) { continue; } srcId = StructSearchMatch.CorpDbId; } else if (Lex.Contains(databaseName, "chembl")) { if (!GetChemblSim) { continue; } srcId = StructSearchMatch.ChemblDbId; } if (Debug) { DebugLog.Message("Starting sim search on " + databaseName + " database"); } FpDao = new FingerprintDao(databaseName, FingerprintType); if (!FpDao.DataFilesExist()) { continue; // no files for this database } FileStreamReaders = FpDao.OpenReaders(); FileMatchLists = new List <StructSearchMatch> [FileStreamReaders.Length]; for (int i1 = 0; i1 < FileMatchLists.Length; i1++) { FileMatchLists[i1] = new List <StructSearchMatch>(); } DateTime t0 = DateTime.Now; if (UseMultipleThreads) { ExecuteMultiThreadSearch(); } else { ExecuteSingleThreadSearch(); } double et = TimeOfDay.Delta(ref t0); FpDao.CloseReaders(); List <StructSearchMatch> matchList = MergeIndividualFileMatchLists(); if (KeysToExclude != null || SearchKeySubset != null) // filter by any allowed/disallowed keys { List <StructSearchMatch> matchList2 = new List <StructSearchMatch>(); foreach (StructSearchMatch m0 in matchList) { if (KeysToExclude != null && KeysToExclude.Contains(m0.SrcCid)) { continue; } if (SearchKeySubset != null && !SearchKeySubset.Contains(m0.SrcCid)) { continue; } matchList2.Add(m0); } matchList = matchList2; } matchList.Sort(StructSearchMatch.CompareByMatchQuality); //int removeCount = matchList.Count - MaxHits; // limit to maxhits per database //if (removeCount > 0) // matchList.RemoveRange(MaxHits, removeCount); //foreach (StructSearchMatch ssm0 in matchList) // if (ssm0.SrcId != srcId) ssm0.SrcId = srcId; // debug MatchList.AddRange(matchList); double et2 = TimeOfDay.Delta(ref t0); string msg = string.Format("Search complete (" + databaseName + ").Time : {0:0.00} ", et) + string.Format("{0} Hits: ", FileMatchLists[0].Count); if (Debug) { DebugLog.Message(msg); } for (int hi = 0; hi < 5 && hi < FileMatchLists[0].Count; hi++) { StructSearchMatch sm = FileMatchLists[0][hi]; msg += sm.SrcCid + string.Format(" = {0:0.00}, ", sm.MatchScore); } } // database loop if (ThreadException != null) { throw new Exception(ThreadException.Message, ThreadException); } MatchList.Sort( // sort by decreasing sim value delegate(StructSearchMatch p1, StructSearchMatch p2) { return(p2.MatchScore.CompareTo(p1.MatchScore)); }); if (MaxHits > 0 && MatchList.Count > MaxHits) // remove hits beyond maximum if defined { MatchList.RemoveRange(MaxHits, MatchList.Count - MaxHits); } //ShowProgress(msg); //Thread.Sleep(10000000); return(MatchList); }
/// <summary> /// GetFpFileName /// </summary> /// <param name="fi"></param> /// <returns></returns> public string GetFpFileName(int fi) { AssertMx.IsTrue(FingerprintType == FingerprintType.MACCS || FingerprintType == FingerprintType.Circular); return(ServicesDirs.BinaryDataDir + @"\Fingerprints\" + DatabaseName + FingerprintFileNameToken + string.Format(@"Fingerprints{0:00}", fi)); }
/// <summary> /// Run the query /// </summary> /// <param name="browseExistingResults">If true browse existing results</param> /// <returns></returns> public string RunQuery3( ResultsFormat rf, bool saveHitlist, bool browseExistingResults) { Query modifiedQuery; QueryTable qt; QueryColumn qc; ResultsTable rt; ResultsField rfld; MetaTable mt; MetaColumn mc; string txt, msg; DialogResult dr; bool success; CellGraphic cg; Lex lex = new Lex(); string tempfile, tok, command, unrecognizedCommand, response; int ti, gi, rc, i1, i2; // Begin execution if (rf == null) { throw new Exception("QueryExec.Run - Null ResultsFormat"); } if (ResultsFormatter == null) { throw new Exception("QueryExec.Run - Null ResultsFormatter"); } if (rf.Segments == null) { throw new Exception("QueryExec.Run - Null ResultsFormat.Segments"); } if (Query == null) { throw new Exception("QueryExec.Run - Null Rf.Query"); } if (Query.Tables == null || Query.Tables.Count <= 0) { throw new QueryException("QueryExec.Run - No Query Tables"); } QueryManager qm = QueryManager; ReturnMsg = ""; //bool useExistingQueryEngine = Rf.ParentQe != null; //bool useExistingDataTable = Query.BrowseExistingResultsWhenOpened && Query.SerializeResults && // qm.DataTable != null && qm.DataTable.Rows.Count > 0; try { //if (Math.Sqrt(4) == 2) throw new Exception("test"); // debug if (!browseExistingResults) // normal open of search { Progress.Show("Analyzing query..."); // put up a status message to the user as soon as possible to let them know something is happening... dr = ValidateQuery(Query); if (dr == DialogResult.Cancel) { return(""); } WriteCurrentListToServerIfNeeded(Query); if (rf.OutputDestination == OutputDest.WinForms) // update access stats if grid { UpdateTableUsageStatistics(Query); } Query.ResultsDataTable = null; // be sure to get new results qm = BuildQueryManager(Query, rf); Query.ResetViewStates(); // reset state of views for proper operation if (Rf.ParentQe == null) // open search unless using existing query engine { if (!ExecuteSearch(saveHitlist)) // returns false if cancelled by user { Progress.Hide(); return(""); } } if ((ResultsKeys == null || ResultsKeys.Count == 0) && // nothing for search !Query.Preview && !MqlUtil.SingleStepExecution(Query) && qm.DataTable.Rows.Count == 0 && Query.RetrievesDataFromQueryEngine) { // if (!Rf.PopupDisplay) Progress.Hide(); if (qm.StatusBarManager != null) { qm.StatusBarManager.DisplayStatusMessage(""); } // if (QueryEngine.Cancelled) return ""; // cancelled by user msg = "No data have been found that matches your query."; if (ResultsFormat.PopupOutputFormContext && !ResultsFormat.SuppressNoDataMessage) { MessageBoxMx.Show(msg, "Search Result", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return("Command EditQuery"); // return to edit query menu } else { return(msg); } } //if (ResultsFormat.PopupDisplay) // SessionManager.DisplayStatusMessage("Retrieving data..."); //else Progress.Show("Retrieving data...", UmlautMobius.Value, true, "Cancelling Retrieval..."); //Progress.Show("Retrieving data...", UmlautMobius.String, true, "Cancelling Retrieval..."); Progress.Hide(); // hide progress - "Retrieving data..." message now appears as bottom line of grid if (ResultsFormat.Grid) { if (ResultsFormat.SessionOutputFormContext) // if normal main session form grid display, set browse mode & view state { Query.ResetViewStates(); // reset view state for all views QbUtil.SetMode(QueryMode.Browse, Query); if (ResultsFormat.Query.LogicType == QueryLogicType.And) // log grid query by logic type { UsageDao.LogEvent("QueryGridAnd", ""); } else if (ResultsFormat.Query.LogicType == QueryLogicType.Or) { UsageDao.LogEvent("QueryGridOr", ""); } else if (ResultsFormat.Query.LogicType == QueryLogicType.Complex) { UsageDao.LogEvent("QueryGridComplex", ""); } } else if (ResultsFormat.PopupOutputFormContext) // create popup window & configure { PopupResults.Show(qm); //MoleculeGridPanel.ConfigureAndShow(qm, null); } else if (ResultsFormat.ToolOutputFormContext) { ContainerControl cc; QueryResultsControl qrc = ResultsFormat.OutputContainerControl as QueryResultsControl; AssertMx.IsTrue(qrc != null, "ResultsFormat.OutputContainerControl must be a QueryResultsControl"); if (!WindowsHelper.FindContainerControl(qrc, typeof(ToolResultsContainer), out cc)) { throw new Exception("ToolResultsContainer not found"); } ToolResultsContainer trc = cc as ToolResultsContainer; trc.SetupQueryResultsControlForResultsDisplay(qm); } else { throw new Exception("Invalid OutputformContext: " + ResultsFormat.OutputFormContext); } } } else // reentering display switch to browse tab { QbUtil.SetMode(QueryMode.Browse, Query); } response = ResultsFormatter.BeginFormatting(browseExistingResults); // format the data if (ResultsFormat.SessionOutputFormContext) // normal display { if (MqlUtil.SingleStepExecution(Query)) { // be sure hit count display is up to date if (ResultsKeys != null) { if (qm.StatusBarManager != null) { qm.StatusBarManager.DisplayCurrentCount(); } } } if (saveHitlist) { CidList hitList = new CidList(ResultsKeys); rc = CidListCommand.WriteCurrentList(hitList); SessionManager.DisplayCurrentCount(); } } return(response); } // end of surrounding try catch (Exception ex) { Progress.Hide(); if (ex is UserQueryException) // exception that can occur from user error { throw new UserQueryException(ex.Message, ex); } else { msg = DebugLog.FormatExceptionMessage(ex); if (!Lex.Contains(msg, "QueryLogged:")) // exception & query { QueryEngine.LogExceptionAndSerializedQuery(msg, Query); } else { ServicesLog.Message(msg); // just log exception } throw new Exception(ex.Message, ex); // pass it up } } }
/// <summary> /// Try to get the row at the given index from the buffer or cache /// </summary> /// <param name="ri"></param> /// <returns></returns> /// public object[] GetRow( int ri) { object[] vo = null; if (ri < 0 || ri >= TotalRowCount) { string msg = string.Format("Row {0} not in cache or buffer (cacheSize: {1}, bufferSize: {2})", ri, RowsWrittenToCache, RowBuffer.Count); throw new InvalidDataException(msg); } // See if in buffer if (ri >= RowsWrittenToCache) { vo = (object[])RowBuffer[ri - RowsWrittenToCache]; if (DebugCaching) { DebugLog.Message(string.Format("Row: {0} in Buffer (cacheSize: {1}, bufferSize: {2})", ri, RowsWrittenToCache, RowBuffer.Count)); } return(vo); } // Get from cache if (ri < CacheReaderPosition + 1) // if already read past this then close current cursor { CloseCacheReader(); } if (CacheReader == null) // need to open reader? { AssertMx.IsNotNull(CacheWriter, "CacheWriter"); AssertMx.IsTrue(File.Exists(CacheFilePath), "CacheFilePath doesn't exist"); FileStream fs = File.Open(CacheFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); CacheReader = new BinaryReader(fs); CacheReaderPosition = -1; if (DebugCaching) { DebugLog.Message("Opening cache reader"); } } while (CacheReaderPosition + 1 <= ri) // read rows until we get the one we want { vo = VoArray.ReadBinaryVoArray(CacheReader, VoaLength); CacheReaderPosition++; } CacheReaderPosition = ri; if (DebugCaching) { DebugLog.Message(string.Format("Row: {0} in Cache (cacheSize: {1}, bufferSize: {2})", ri, RowsWrittenToCache, RowBuffer.Count)); } return(vo); }
/// <summary> /// Get the next value for the sequence (MySQL) /// </summary> /// <param name="seqName"></param> /// <returns></returns> public static long NextValLongMySQL( string seqName) { string sql; long nextVal; SequenceDao seqDao = Lookup(seqName); Queue <long> seqQueue = seqDao.Queue; if (seqQueue.Count > 0) { nextVal = seqQueue.Dequeue(); return(nextVal); } int count = (seqDao.CacheSize > 0 ? seqDao.CacheSize : 1); int t0 = TimeOfDay.Milliseconds(); DbCommandMx seqCmd = new DbCommandMx(); seqCmd.MxConn = DbConnectionMx.GetConnection("MySql_Mobius"); // "MySql_Mobius_Sequences" sql = String.Format( @"update mbs_owner.mbs_sequences set value = last_insert_id(value) + {0} where name = '{1}'" , count, seqName.ToUpper()); seqCmd.PrepareUsingDefinedConnection(sql); int updCount = seqCmd.ExecuteNonReader(); if (updCount <= 0) { throw new Exception("Error updating sequence (may not exist): " + seqName); } sql = "select last_insert_id()"; // gets value before update above seqCmd.PrepareUsingDefinedConnection(sql, null); DbDataReader rdr = seqCmd.ExecuteReader(); bool readOk = rdr.Read(); AssertMx.IsTrue(readOk, "readOk"); long value = rdr.GetInt64(0); rdr.Close(); seqCmd.CloseReader(); nextVal = value + 1; // return this one now long v2 = value + 2; // next value long vn = value + count; // last value for (long vi = v2; vi <= vn; vi++) { seqQueue.Enqueue(vi); } t0 = TimeOfDay.Milliseconds() - t0; // DebugLog.Message("Read sequence, set size = " + seqQueue.Count.ToString() + ", Time(ms) = " + t0.ToString()); return(nextVal); }