/// <summary> Constructs with defaults: false/RaisesException </summary> public BulkInsertOptions() { _replaceContents = false; _commitOnCompletion = true; _inputIsSorted = false; _duplicateHandling = DuplicateHandling.RaisesException; }
private bool ImportFile(string filename, TGIN tgin, bool useName, bool rename, bool compress, DuplicateHandling dups, bool select) { IResourceKey rk = (TGIBlock)tgin; string resName = tgin.ResName; bool nmOK = true; MemoryStream ms = new MemoryStream(); BinaryWriter w = new BinaryWriter(ms); BinaryReader r = new BinaryReader(new FileStream(filename, FileMode.Open, FileAccess.Read)); w.Write(r.ReadBytes((int)r.BaseStream.Length)); r.Close(); w.Flush(); if (useName && !string.IsNullOrEmpty(resName)) { nmOK = this.browserWidget1.ResourceName(rk.Instance, resName, true, rename); } IResourceIndexEntry rie = this.NewResource(rk, ms, dups, compress); if (rie != null) { this.browserWidget1.Add(rie, select); } return(nmOK); }
/// <summary> Constructs an ordered enumeration from an unordered enumeration </summary> public OrderedEnumeration(IComparer <T> comparer, IEnumerable <T> unordered, ISerializer <T> serializer, int memoryLimit) { _enumerated = false; _comparer = comparer; _unordered = unordered; _serializer = serializer; _memoryLimit = Check.InRange(memoryLimit, 1, LimitMax); _duplicateHandling = DuplicateHandling.None; }
public OrderedEnumerator( IEnumerable <T> enumerator, IComparer <T> comparer, DuplicateHandling duplicateHandling) { _ordered = enumerator; _enumerator = null; _comparer = comparer; _duplicateHandling = duplicateHandling; _isFirst = true; }
public async Task <ShipmentResponse> Carrier(DuplicateHandling duplicateHandling, ShipmentRequest shipment) { var connection = GetConnection("create", Method.POST, shipment); int duplicateHanlde = (int)duplicateHandling; connection.Request.AddQueryParameter("duplicateHandling", duplicateHanlde.ToString()); var body = JsonConvert.SerializeObject(shipment); var restResponse = await connection.Client.ExecuteAsync(connection.Request); if (!restResponse.IsSuccessful) { throw new ApplicationException($"Server response: {restResponse.Content} ; failed to carrier shipments: {body}"); } var result = JsonConvert.DeserializeObject <ShipmentResponse>(restResponse.Content); return(result); }
private void ImportStream(MyDataFormat data, bool useName, bool rename, bool compress, DuplicateHandling dups, bool select) { if (useName && !string.IsNullOrEmpty(data.tgin.ResName)) { this.browserWidget1.ResourceName(data.tgin.ResInstance, data.tgin.ResName, true, rename); } IResourceIndexEntry rie = this.NewResource((TGIBlock)data.tgin, new MemoryStream(data.data), dups, compress); if (rie != null) { this.browserWidget1.Add(rie, select); } }
public List <ShipmentResponse> CarrierBatch(DuplicateHandling duplicateHandling, ShipmentRequest[] shipments) { try { var result = new List <ShipmentResponse>(); Parallel.ForEach(shipments, new ParallelOptions { MaxDegreeOfParallelism = _maxDegreeOfParallelism }, async(shipment) => { result.Add(await Carrier(duplicateHandling, shipment)); }); return(result); } catch (Exception ex) { throw new ApplicationException(ex.ToString()); } }
public EnumWithDuplicateKeyHandling(IEnumerable <T> items, IComparer <T> comparer, DuplicateHandling duplicateHandling) { _items = items; _comparer = comparer; _duplicateHandling = duplicateHandling; }
/// <summary> /// Wraps an existing enumeration of Key/value pairs with an assertion about ascending order and handling /// for duplicate keys. /// </summary> public static IEnumerable <T> WithDuplicateHandling( IEnumerable <T> items, IComparer <T> comparer, DuplicateHandling duplicateHandling) { return(new EnumWithDuplicateKeyHandling(items, comparer, duplicateHandling)); }
/// <summary> /// Merges n-number of ordered enumerations based on the comparer provided. /// </summary> public static IEnumerable <T> Merge(IComparer <T> comparer, DuplicateHandling duplicateHandling, params IEnumerable <T>[] enums) { return(WithDuplicateHandling(Merge(comparer, enums), comparer, duplicateHandling)); }
private IResourceIndexEntry NewResource(IResourceKey rk, MemoryStream ms, DuplicateHandling dups, bool compress) { IResourceIndexEntry rie = CurrentPackage.Find(x => rk.Equals(x)); if (rie != null) { if (dups == DuplicateHandling.reject) return null; if (dups == DuplicateHandling.replace) CurrentPackage.DeleteResource(rie); } rie = CurrentPackage.AddResource(rk, ms, false);//we do NOT want to search again... if (rie == null) return null; rie.Compressed = (ushort)(compress ? 0x5A42 : 0); IsPackageDirty = true; return rie; }
private void importPackagesCommon(string[] packageList, string title, DuplicateHandling dups, bool compress, bool useNames = false, bool rename = false, List <uint> dupsList = null, AutoSaveState autoSaveState = AutoSaveState.Ask, IList <IResourceIndexEntry> selection = null ) { bool CPuseNames = controlPanel1.UseNames; DateTime now = DateTime.UtcNow; bool autoSave = false; if (autoSaveState == AutoSaveState.Ask) { switch (CopyableMessageBox.Show("Auto-save current package after each package imported?", title, CopyableMessageBoxButtons.YesNoCancel, CopyableMessageBoxIcon.Question)) { case 0: autoSave = true; break; case 2: return; } } else { autoSave = autoSaveState == AutoSaveState.Always; } try { browserWidget1.Visible = false; controlPanel1.UseNames = false; bool skipAll = false; foreach (string filename in packageList) { if (Filename != null && Filename.Length > 0 && Path.GetFullPath(Filename).Equals(Path.GetFullPath(filename))) { CopyableMessageBox.Show("Skipping current package.", importPackagesDialog.Title); continue; } lbProgress.Text = "Importing " + Path.GetFileNameWithoutExtension(filename) + "..."; Application.DoEvents(); IPackage imppkg = null; try { imppkg = Package.OpenPackage(0, filename); } catch (InvalidDataException ex) { if (skipAll) { continue; } int btn = CopyableMessageBox.Show(String.Format("Could not open package {0}.\n{1}", Path.GetFileName(filename), ex.Message), title, CopyableMessageBoxIcon.Error, new List <string>(new string[] { "Skip this", "Skip all", "Abort" }), 0, 0); if (btn == 0) { continue; } if (btn == 1) { skipAll = true; continue; } break; } try { List <Tuple <myDataFormat, DuplicateHandling> > limp = new List <Tuple <myDataFormat, DuplicateHandling> >(); List <IResourceIndexEntry> lrie = selection == null ? imppkg.GetResourceList : imppkg.FindAll(rie => selection.Any(tgt => ((AResourceKey)tgt).Equals(rie))); progressBar1.Value = 0; progressBar1.Maximum = lrie.Count; foreach (IResourceIndexEntry rie in lrie) { try { if (rie.ResourceType == 0x0166038C)//NMAP { if (useNames) { browserWidget1.MergeNamemap(s4pi.WrapperDealer.WrapperDealer.GetResource(0, imppkg, rie) as IDictionary <ulong, string>, true, rename); } } else { IResource res = s4pi.WrapperDealer.WrapperDealer.GetResource(0, imppkg, rie, true); myDataFormat impres = new myDataFormat() { tgin = rie as AResourceIndexEntry, data = res.AsBytes, }; // dups Replace | Reject | Allow // dupsList null | list of allowable dup types DuplicateHandling dupThis = dups == DuplicateHandling.allow ? dupsList == null || dupsList.Contains(rie.ResourceType) ? DuplicateHandling.allow : DuplicateHandling.replace : dups; limp.Add(Tuple.Create(impres, dupThis)); progressBar1.Value++; if (now.AddMilliseconds(100) < DateTime.UtcNow) { Application.DoEvents(); now = DateTime.UtcNow; } } } catch (Exception ex) { string rk = ""; if (rie != null) { rk = "(RK: " + rie + ")\n"; } else { rk = "(RK is null)\n"; } CopyableMessageBox.IssueException(ex, "Could not import all resources - aborting.\n" + rk, title); throw new IgnoredException(ex); } } progressBar1.Value = 0; IEnumerable <IResourceIndexEntry> rieList = limp .Select(x => NewResource((AResourceKey)x.Item1.tgin, new MemoryStream(x.Item1.data), x.Item2, compress)) .Where(x => x != null); browserWidget1.AddRange(rieList); } catch (IgnoredException) { break; }//just the thrown exception, stop looping catch (Exception ex) { CopyableMessageBox.IssueException(ex, "Could not import all resources - aborting.\n", title); break; } finally { imppkg.Dispose(); }//Package.ClosePackage(0, imppkg); } if (autoSave) { if (!fileSave()) { break; } } } } finally { lbProgress.Text = ""; progressBar1.Value = 0; progressBar1.Maximum = 0; controlPanel1.UseNames = CPuseNames; browserWidget1.Visible = true; ForceFocus.Focus(Application.OpenForms[0]); Application.DoEvents(); } }
void importStream(myDataFormat data, bool useName, bool rename, bool compress, DuplicateHandling dups, bool select) { if (useName && data.tgin.ResName != null && data.tgin.ResName.Length > 0) browserWidget1.ResourceName(data.tgin.ResInstance, data.tgin.ResName, true, rename); IResourceIndexEntry rie = NewResource((TGIBlock)data.tgin, new MemoryStream(data.data), dups, compress); if (rie != null) browserWidget1.Add(rie, select); }
bool importFile(string filename, TGIN tgin, bool useName, bool rename, bool compress, DuplicateHandling dups, bool select) { IResourceKey rk = (TGIBlock)tgin; string resName = tgin.ResName; bool nmOK = true; MemoryStream ms = new MemoryStream(); BinaryWriter w = new BinaryWriter(ms); BinaryReader r = new BinaryReader(new FileStream(filename, FileMode.Open, FileAccess.Read)); w.Write(r.ReadBytes((int)r.BaseStream.Length)); r.Close(); w.Flush(); if (useName && resName != null && resName.Length > 0) nmOK = browserWidget1.ResourceName(rk.Instance, resName, true, rename); IResourceIndexEntry rie = NewResource(rk, ms, dups, compress); if (rie != null) browserWidget1.Add(rie, select); return nmOK; }
private void importPackagesCommon(string[] packageList, string title, DuplicateHandling dups, bool compress, bool useNames = false, bool rename = false, List<uint> dupsList = null, AutoSaveState autoSaveState = AutoSaveState.Ask, IList<IResourceIndexEntry> selection = null ) { bool CPuseNames = controlPanel1.UseNames; DateTime now = DateTime.UtcNow; bool autoSave = false; if (autoSaveState == AutoSaveState.Ask) { switch (CopyableMessageBox.Show("Auto-save current package after each package imported?", title, CopyableMessageBoxButtons.YesNoCancel, CopyableMessageBoxIcon.Question)) { case 0: autoSave = true; break; case 2: return; } } else autoSave = autoSaveState == AutoSaveState.Always; try { browserWidget1.Visible = false; controlPanel1.UseNames = false; bool skipAll = false; foreach (string filename in packageList) { if (Filename != null && Filename.Length > 0 && Path.GetFullPath(Filename).Equals(Path.GetFullPath(filename))) { CopyableMessageBox.Show("Skipping current package.", importPackagesDialog.Title); continue; } lbProgress.Text = "Importing " + Path.GetFileNameWithoutExtension(filename) + "..."; Application.DoEvents(); IPackage imppkg = null; try { imppkg = Package.OpenPackage(0, filename); } catch (InvalidDataException ex) { if (skipAll) continue; int btn = CopyableMessageBox.Show(String.Format("Could not open package {0}.\n{1}", Path.GetFileName(filename), ex.Message), title, CopyableMessageBoxIcon.Error, new List<string>(new string[] { "Skip this", "Skip all", "Abort"}), 0, 0); if (btn == 0) continue; if (btn == 1) { skipAll = true; continue; } break; } try { List<Tuple<myDataFormat, DuplicateHandling>> limp = new List<Tuple<myDataFormat, DuplicateHandling>>(); List<IResourceIndexEntry> lrie = selection == null ? imppkg.GetResourceList : imppkg.FindAll(rie => selection.Any(tgt => ((AResourceKey)tgt).Equals(rie))); progressBar1.Value = 0; progressBar1.Maximum = lrie.Count; foreach (IResourceIndexEntry rie in lrie) { try { if (rie.ResourceType == 0x0166038C)//NMAP { if (useNames) browserWidget1.MergeNamemap(s4pi.WrapperDealer.WrapperDealer.GetResource(0, imppkg, rie) as IDictionary<ulong, string>, true, rename); } else { IResource res = s4pi.WrapperDealer.WrapperDealer.GetResource(0, imppkg, rie, true); myDataFormat impres = new myDataFormat() { tgin = rie as AResourceIndexEntry, data = res.AsBytes, }; // dups Replace | Reject | Allow // dupsList null | list of allowable dup types DuplicateHandling dupThis = dups == DuplicateHandling.allow ? dupsList == null || dupsList.Contains(rie.ResourceType) ? DuplicateHandling.allow : DuplicateHandling.replace : dups; limp.Add(Tuple.Create(impres, dupThis)); progressBar1.Value++; if (now.AddMilliseconds(100) < DateTime.UtcNow) { Application.DoEvents(); now = DateTime.UtcNow; } } } catch (Exception ex) { string rk = ""; if (rie != null) rk = "(RK: " + rie + ")\n"; else rk = "(RK is null)\n"; CopyableMessageBox.IssueException(ex, "Could not import all resources - aborting.\n" + rk, title); throw new IgnoredException(ex); } } progressBar1.Value = 0; IEnumerable<IResourceIndexEntry> rieList = limp .Select(x => NewResource((AResourceKey)x.Item1.tgin, new MemoryStream(x.Item1.data), x.Item2, compress)) .Where(x => x != null); browserWidget1.AddRange(rieList); } catch (IgnoredException) { break; }//just the thrown exception, stop looping catch (Exception ex) { CopyableMessageBox.IssueException(ex, "Could not import all resources - aborting.\n", title); break; } finally { imppkg.Dispose(); }//Package.ClosePackage(0, imppkg); } if (autoSave) if (!fileSave()) break; } } finally { lbProgress.Text = ""; progressBar1.Value = 0; progressBar1.Maximum = 0; controlPanel1.UseNames = CPuseNames; browserWidget1.Visible = true; ForceFocus.Focus(Application.OpenForms[0]); Application.DoEvents(); } }
/// <summary> /// Merges n-number of ordered enumerations based on the comparer provided. /// </summary> public static IEnumerable <KeyValuePair <TKey, TValue> > Merge(IComparer <TKey> comparer, DuplicateHandling duplicateHandling, params IEnumerable <KeyValuePair <TKey, TValue> >[] enums) { KeyValueComparer <TKey, TValue> kvcompare = new KeyValueComparer <TKey, TValue>(comparer); return(WithDuplicateHandling(Merge(kvcompare, enums), kvcompare, duplicateHandling)); }
/// <summary> /// Creates the zip file. /// </summary> protected override void ExecuteTask() { ZipOutputStream zOutstream = null; Log(Level.Info, "Zipping {0} files to '{1}'.", ZipFileSets.FileCount, ZipFile.FullName); try { if (!Directory.Exists(ZipFile.DirectoryName)) { Directory.CreateDirectory(ZipFile.DirectoryName); } // set encoding to use for filenames and comment ZipConstants.DefaultCodePage = Encoding.CodePage; zOutstream = new ZipOutputStream(ZipFile.Create()); // set compression level zOutstream.SetLevel(ZipLevel); // set password if (!String.IsNullOrEmpty(PassWord)) { zOutstream.Password = PassWord; } // set comment if (!String.IsNullOrEmpty(Comment)) { zOutstream.SetComment(Comment); } foreach (ZipFileSet fileset in ZipFileSets) { string basePath = fileset.BaseDirectory.FullName; if (Path.GetPathRoot(basePath) != basePath) { basePath = Path.GetDirectoryName(basePath + Path.DirectorySeparatorChar); } // add files to zip foreach (string file in fileset.FileNames) { // ensure file exists (in case "asis" was used) if (!File.Exists(file)) { throw new BuildException(string.Format(CultureInfo.InvariantCulture, "File '{0}' does not exist.", file), Location); } // the name of the zip entry string entryName; // determine name of the zip entry if (!Flatten && file.StartsWith(basePath)) { entryName = file.Substring(basePath.Length); if (entryName.Length > 0 && entryName[0] == Path.DirectorySeparatorChar) { entryName = entryName.Substring(1); } // remember that directory was added to zip file, so // that we won't add it again later string dir = Path.GetDirectoryName(file); if (_addedDirs[dir] == null) { _addedDirs[dir] = dir; } } else { // flatten directory structure entryName = Path.GetFileName(file); } // add prefix if specified if (fileset.Prefix != null) { entryName = fileset.Prefix + entryName; } // ensure directory separators are understood on linux if (Path.DirectorySeparatorChar == '\\') { entryName = entryName.Replace(@"\", "/"); } // perform duplicate checking if (_fileEntries.ContainsKey(entryName)) { switch (DuplicateHandling) { case DuplicateHandling.Add: break; case DuplicateHandling.Fail: throw new BuildException(string.Format( CultureInfo.InvariantCulture, "Duplicate file '{0}' was found.", entryName), Location.UnknownLocation); case DuplicateHandling.Preserve: // skip current entry continue; default: throw new BuildException(string.Format( CultureInfo.InvariantCulture, "Duplicate value '{0}' is not supported.", DuplicateHandling.ToString()), Location.UnknownLocation); } } // create zip entry ZipEntry entry = new ZipEntry(entryName); // store entry (to allow for duplicate checking) _fileEntries[entryName] = null; // set date/time stamp on zip entry if (Stamp != DateTime.MinValue) { entry.DateTime = Stamp; } else { entry.DateTime = File.GetLastWriteTime(file); } // write file content to stream in small chuncks using (FileStream fs = File.OpenRead(file)) { // set size for backward compatibility with older unzip entry.Size = fs.Length; Log(Level.Verbose, "Adding {0}.", entryName); // write file to zip file zOutstream.PutNextEntry(entry); byte[] buffer = new byte[50000]; while (true) { int bytesRead = fs.Read(buffer, 0, buffer.Length); if (bytesRead == 0) { break; } zOutstream.Write(buffer, 0, bytesRead); } } } // add (possibly empty) directories to zip if (IncludeEmptyDirs) { foreach (string directory in fileset.DirectoryNames) { // skip directories that were already added when the // files were added if (_addedDirs[directory] != null) { continue; } // skip directories that are not located beneath the base // directory if (!directory.StartsWith(basePath) || directory.Length <= basePath.Length) { continue; } // determine zip entry name string entryName = directory.Substring(basePath.Length + 1); // add prefix if specified if (fileset.Prefix != null) { entryName = fileset.Prefix + entryName; } // ensure directory separators are understood on linux if (Path.DirectorySeparatorChar == '\\') { entryName = entryName.Replace(@"\", "/"); } if (!entryName.EndsWith("/")) { // trailing directory signals to #ziplib that we're // dealing with directory entry entryName += "/"; } // create directory entry ZipEntry entry = new ZipEntry(entryName); // set size for backward compatibility with older unzip entry.Size = 0L; // write directory to zip file zOutstream.PutNextEntry(entry); } } } zOutstream.Close(); zOutstream.Finish(); } catch (Exception ex) { // close the zip output stream if (zOutstream != null) { zOutstream.Close(); } // delete the (possibly corrupt) zip file if (ZipFile.Exists) { ZipFile.Delete(); } throw new BuildException(string.Format(CultureInfo.InvariantCulture, "Zip file '{0}' could not be created.", ZipFile.FullName), Location, ex); } finally { CleanUp(); } }
void importStream(myDataFormat data, bool useName, bool rename, bool compress, DuplicateHandling dups, bool select) { if (useName && data.tgin.ResName != null && data.tgin.ResName.Length > 0) { browserWidget1.ResourceName(data.tgin.ResInstance, data.tgin.ResName, true, rename); } IResourceIndexEntry rie = NewResource((TGIBlock)data.tgin, new MemoryStream(data.data), dups, compress); if (rie != null) { browserWidget1.Add(rie, select); } }