/// <summary> /// Write result set to a binary file for later retrieval /// </summary> public void WriteBinaryResultsFile(string fileName) { int id = Query.UserObject.Id; if (id <= 0) { throw new Exception("UserObject id not defined, Query not saved"); } BinaryWriter bw = BinaryFile.OpenWriter(fileName + ".new"); // open ".new" file string sq = Query.Serialize(); // serialize & write the query bw.Write(sq); int voArrayLen = DataTableMx.Columns.Count - KeyValueVoPos; // don't write values before the key bw.Write(voArrayLen); // write the array length for (int dri = 0; dri < DataTableMx.Rows.Count; dri++) { DataRowMx dr = DataTableMx.Rows[dri]; VoArray.WriteBinaryVoArray(dr.ItemArrayRef, bw, KeyValueVoPos); } bw.Close(); FileUtil.BackupAndReplaceFile(fileName, fileName + ".bak", fileName + ".new"); return; }
public void BackupAndReplaceFingerprintFiles() { for (int fi = 0; fi < FingerprintDao.FingerprintFileCount; fi++) { string fBase = GetFpFileName(fi); FileUtil.BackupAndReplaceFile(fBase + ".bin", fBase + ".bak", fBase + ".new"); } }
/// <summary> /// Utility method to write out a metatable stats file for a broker /// </summary> /// <param name="stats"></param> /// <param name="arg"></param> /// <param name="fileName"></param> /// <returns></returns> public static int WriteMetaTableStats( Dictionary <string, MetaTableStats> stats, string singleMtName, string fileName) { string path = fileName + ".new"; StreamWriter sw = new StreamWriter(path); if (!Lex.IsNullOrEmpty(singleMtName)) // if mtName specified read & write all data but this metatable { path = fileName + ".txt"; StreamReader sr = new StreamReader(path); // open existing file while (true) { string rec = sr.ReadLine(); if (rec == null) { break; } if (rec.StartsWith(singleMtName + "\t")) { continue; // skip existing recs } sw.WriteLine(rec); // write all other recs } sr.Close(); } foreach (string mtName0 in stats.Keys) { MetaTableStats mts = stats[mtName0]; string rec = mtName0 + "\t" + mts.RowCount.ToString() + "\t" + mts.UpdateDateTime.ToShortDateString(); sw.WriteLine(rec); } sw.Close(); FileUtil.BackupAndReplaceFile(fileName + ".txt", fileName + ".bak", fileName + ".new"); return(stats.Count); }
/// <summary> /// Merge existing .bin files with new records /// 1. Copy .bin files to .mrg files filtering out cids that have been updated /// 2. Append new records to .mrg files /// 3. Rename .bin files to .bak files and .mrg files to new .bin files /// </summary> static void MergeRecordsIntoFiles( List <FingerprintRec> fpRecList) { string date1, date2; Progress.Show("Merging existing and new files..."); // Copy existing bin file entries filtering out cids that were updated FpDao.OpenReaders("bin"); // open existing .bin files for input FpDao.OpenWriters("mrg", FileMode.Create); // open new merge files for output HashSet <string> fpRecHash = new HashSet <string>(); // build a hash of cids that shouldn't be copied foreach (FingerprintRec fpr in fpRecList) { fpRecHash.Add(fpr.Cid); } int replacementCnt = 0; for (int fi = 0; fi < FingerprintDao.FingerprintFileCount; fi++) // copy each file { BinaryWriter bw = FpDao.BinaryWriters[fi]; while (true) { FingerprintRec fpr = FpDao.ReadFingerprintRec(fi); if (fpr == null) { break; } if (fpRecHash.Contains(fpr.Cid)) { //Log("Removing " + fpr.Cid); // debug replacementCnt++; continue; // skip if this cid is was updated in the incoming list } FpDao.WriteFingerprintRec(bw, fpr); } } FpDao.CloseReaders(); // Append the new records to the merge files foreach (FingerprintRec fpr in fpRecList) // write out buffered recs { FpDao.WriteFingerprintRec(fpr); } FpDao.CloseWriters(); // Backup old files and activate new files for (int fi = 0; fi < FingerprintDao.FingerprintFileCount; fi++) // check that we can backup all bin files { string fileName = FpDao.GetFpFileName(fi) + ".bin"; bool backupOk = FileUtil.CanRename(fileName); if (!backupOk) { throw new Exception("Unable to rename file: " + fileName + ", aborting update"); } } for (int fi = 0; fi < FingerprintDao.FingerprintFileCount; fi++) { string fileName = FpDao.GetFpFileName(fi); bool backupOk = FileUtil.BackupAndReplaceFile(fileName + ".bin", fileName + ".bak", fileName + ".mrg"); if (!backupOk) { throw new Exception("Error replacing file: " + fileName + ".bin"); } } if (ByCheckpoint) { string checkPointDate = String.Format("{0:dd-MMM-yyyy HHmmss}", MoleculeDateTime); // format last new/updated date time in a format that works with Oracle FpDao.WriteFingerPrintSimMxDataUpdateCheckpointDate(checkPointDate); // update checkpoint } Progress.Show("Merging complete..."); int cnt = fpRecList.Count; string cid1 = fpRecList[0].Cid; string cid2 = fpRecList[cnt - 1].Cid; if (CidUpdateDateDict.ContainsKey(cid1)) { date1 = CidUpdateDateDict[cid1].ToString(); } else { date1 = "Missing"; } if (CidUpdateDateDict.ContainsKey(cid2)) { date2 = CidUpdateDateDict[cid1].ToString(); } else { date2 = "Missing"; } Log("Records stored: " + cnt + ", Adds: " + (cnt - replacementCnt) + ", Replacements: " + replacementCnt + ", Date range: " + date1 + " - " + date2 + ", CIDs: First = " + cid1 + ", Last = " + cid2); return; }