2012-03-17 20 views
5

c: Açık, onlarca binlerce *.foobar dosya var. Her türlü yerdeydiler (diğer bir ifadeyle). Bu dosyalar yaklaşık 1 - 64 kb boyutunda ve düz metinlerdir.En hızlı/en güvenli dosya bulma/ayrıştırma?

Bu .foobar dosyalarını güçlü bir şekilde türeten bir class Foobar(string fileContents) sahibim.

Yüklemem, Foobar nesnelerinin bir dizisi olarak temsil edilen numaralı *.foobar dosyalarının tümünü içerir. Bunu yapmanın en hızlı yolu nedir?

Aşağıdaki ilk yaklaşımdan daha iyi bir yolun (şüphesiz) olup olmadığını ve bu yaklaşımın herhangi bir potansiyel problemi varsa (örn. I/O eşzamanlılık sorunları istisnalar atıyor mu?) Olup olmadığını öğrenmek istiyorum:

var files = Directory.EnumerateFiles 
       (rootPath, "*.foobar", SearchOption.AllDirectories); 

Foobar[] foobars = 
(
    from filePath in files.AsParallel() 
    let contents = File.ReadAllText(filePath) 
    select new Foobar(contents) 
) 
.ToArray(); 
+8

İşlemi paralel yapmak büyük bir olasılıkla satın almıyor; Fiziksel diskteki dosyaların aranması zorunlu olarak bir G/Ç bağlı işlemidir. –

+0

Aptal soru: dosya gerçekten gerekli disk I/O mu arıyor? Bir diskin dosya sistemi yapısının, işletim sistemi çekirdeği tarafından bellekte önbelleğe alındığını ve yapı yalnızca diskteki içerikten ayrı olduğu için gerektiğinde güncelleştirildiğini düşünürdüm. Yok hayır? – user979672

+0

Eğer arama G/Ç bağlıysa, '.Parallel()' ın alacağı tek şey 'yeni Foobar() 'işlemini (zaman alabilen; daha sonra dev bir dizeden ayrıştırmak zorunda kalıyor) iş parçacığıdır. Doğru? Her yeni Foobar() için yeni konuların eğrilmesinin maliyeti, yeni Foobar() nesnelerinin tek bir iş parçacığı içinde seri olarak oluşturulmasından daha pahalıdır. – user979672

cevap

8

izin hataları (veya diğer hatalar) görünüşte onun parça numaralandırma ölü durdurabilir olduğundan, bu gibi kendi listeleyicisi şey uygulamak isteyebilirsiniz:

class SafeFileEnumerator : IEnumerable<string> 
{ 
    private string root; 
    private string pattern; 
    private IList<Exception> errors; 
    public SafeFileEnumerator(string root, string pattern) 
    { 
    this.root = root; 
    this.pattern = pattern; 
    this.errors = new List<Exception>(); 
    } 

    public SafeFileEnumerator(string root, string pattern, IList<Exception> errors) 
    { 
    this.root = root; 
    this.pattern = pattern; 
    this.errors = errors; 
    } 

    public Exception[] Errors() 
    { 
    return errors.ToArray(); 
    } 
    class Enumerator : IEnumerator<string> 
    { 
    IEnumerator<string> fileEnumerator; 
    IEnumerator<string> directoryEnumerator; 
    string root; 
    string pattern; 
    IList<Exception> errors; 

    public Enumerator(string root, string pattern, IList<Exception> errors) 
    { 
     this.root = root; 
     this.pattern = pattern; 
     this.errors = errors; 
     fileEnumerator = System.IO.Directory.EnumerateFiles(root, pattern).GetEnumerator(); 
     directoryEnumerator = System.IO.Directory.EnumerateDirectories(root).GetEnumerator(); 
    } 
    public string Current 
    { 
     get 
     { 
      if (fileEnumerator == null) throw new ObjectDisposedException("FileEnumerator"); 
      return fileEnumerator.Current; 
     } 
    } 

    public void Dispose() 
    { 
     if (fileEnumerator != null) 
      fileEnumerator.Dispose(); 
     fileEnumerator = null; 
     if (directoryEnumerator != null) 
      directoryEnumerator.Dispose(); 
     directoryEnumerator = null; 
    } 

    object System.Collections.IEnumerator.Current 
    { 
     get { return Current; } 
    } 

    public bool MoveNext() 
    { 
     if ((fileEnumerator != null) && (fileEnumerator.MoveNext())) 
      return true; 
     while ((directoryEnumerator != null) && (directoryEnumerator.MoveNext())) 
     { 
      if (fileEnumerator != null) 
       fileEnumerator.Dispose(); 
      try 
      { 
       fileEnumerator = new SafeFileEnumerator(directoryEnumerator.Current, pattern, errors).GetEnumerator(); 
      } 
      catch (Exception ex) 
      { 
       errors.Add(ex); 
       continue; 
      } 
      if (fileEnumerator.MoveNext()) 
       return true; 
     } 
     if (fileEnumerator != null) 
      fileEnumerator.Dispose(); 
     fileEnumerator = null; 
     if (directoryEnumerator != null) 
      directoryEnumerator.Dispose(); 
     directoryEnumerator = null; 
     return false; 
    } 

    public void Reset() 
    { 
     Dispose(); 
     fileEnumerator = System.IO.Directory.EnumerateFiles(root, pattern).GetEnumerator(); 
     directoryEnumerator = System.IO.Directory.EnumerateDirectories(root).GetEnumerator(); 
    } 
    } 
    public IEnumerator<string> GetEnumerator() 
    { 
    return new Enumerator(root, pattern, errors); 
    } 

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 
    { 
    return GetEnumerator(); 
    } 
} 
4

Büyük çalışmalarını burada bir uzantısıdır FileSystemInfo dönmek için kodunuza Dize yolları yerine. Bazı küçük değişiklikler, SearchOption'a eklenmesi (native .net'in sahip olduğu gibi) ve ilk dizinde hata yakalaması, kök klasörün erişim reddedilmesi durumunda gerçekleşir. Orijinal kayıt için tekrar teşekkürler!

public class SafeFileEnumerator : IEnumerable<FileSystemInfo> 
{ 
    /// <summary> 
    /// Starting directory to search from 
    /// </summary> 
    private DirectoryInfo root; 

    /// <summary> 
    /// Filter pattern 
    /// </summary> 
    private string pattern; 

    /// <summary> 
    /// Indicator if search is recursive or not 
    /// </summary> 
    private SearchOption searchOption; 

    /// <summary> 
    /// Any errors captured 
    /// </summary> 
    private IList<Exception> errors; 

    /// <summary> 
    /// Create an Enumerator that will scan the file system, skipping directories where access is denied 
    /// </summary> 
    /// <param name="root">Starting Directory</param> 
    /// <param name="pattern">Filter pattern</param> 
    /// <param name="option">Recursive or not</param> 
    public SafeFileEnumerator(string root, string pattern, SearchOption option) 
     : this(new DirectoryInfo(root), pattern, option) 
    {} 

    /// <summary> 
    /// Create an Enumerator that will scan the file system, skipping directories where access is denied 
    /// </summary> 
    /// <param name="root">Starting Directory</param> 
    /// <param name="pattern">Filter pattern</param> 
    /// <param name="option">Recursive or not</param> 
    public SafeFileEnumerator(DirectoryInfo root, string pattern, SearchOption option) 
     : this(root, pattern, option, new List<Exception>()) 
    {} 

    // Internal constructor for recursive itterator 
    private SafeFileEnumerator(DirectoryInfo root, string pattern, SearchOption option, IList<Exception> errors) 
    { 
     if (root == null || !root.Exists) 
     { 
      throw new ArgumentException("Root directory is not set or does not exist.", "root"); 
     } 
     this.root = root; 
     this.searchOption = option; 
     this.pattern = String.IsNullOrEmpty(pattern) 
      ? "*" 
      : pattern; 
     this.errors = errors; 
    } 

    /// <summary> 
    /// Errors captured while parsing the file system. 
    /// </summary> 
    public Exception[] Errors 
    { 
     get 
     { 
      return errors.ToArray(); 
     } 
    } 

    /// <summary> 
    /// Helper class to enumerate the file system. 
    /// </summary> 
    private class Enumerator : IEnumerator<FileSystemInfo> 
    { 
     // Core enumerator that we will be walking though 
     private IEnumerator<FileSystemInfo> fileEnumerator; 
     // Directory enumerator to capture access errors 
     private IEnumerator<DirectoryInfo> directoryEnumerator; 

     private DirectoryInfo root; 
     private string pattern; 
     private SearchOption searchOption; 
     private IList<Exception> errors; 

     public Enumerator(DirectoryInfo root, string pattern, SearchOption option, IList<Exception> errors) 
     { 
      this.root = root; 
      this.pattern = pattern; 
      this.errors = errors; 
      this.searchOption = option; 

      Reset(); 
     } 

     /// <summary> 
     /// Current item the primary itterator is pointing to 
     /// </summary> 
     public FileSystemInfo Current 
     { 
      get 
      { 
       //if (fileEnumerator == null) throw new ObjectDisposedException("FileEnumerator"); 
       return fileEnumerator.Current as FileSystemInfo; 
      } 
     } 

     object System.Collections.IEnumerator.Current 
     { 
      get { return Current; } 
     } 

     public void Dispose() 
     { 
      Dispose(true, true); 
     } 

     private void Dispose(bool file, bool dir) 
     { 
      if (file) 
      { 
       if (fileEnumerator != null) 
        fileEnumerator.Dispose(); 

       fileEnumerator = null; 
      } 

      if (dir) 
      { 
       if (directoryEnumerator != null) 
        directoryEnumerator.Dispose(); 

       directoryEnumerator = null; 
      } 
     } 

     public bool MoveNext() 
     { 
      // Enumerate the files in the current folder 
      if ((fileEnumerator != null) && (fileEnumerator.MoveNext())) 
       return true; 

      // Don't go recursive... 
      if (searchOption == SearchOption.TopDirectoryOnly) { return false; } 

      while ((directoryEnumerator != null) && (directoryEnumerator.MoveNext())) 
      { 
       Dispose(true, false); 

       try 
       { 
        fileEnumerator = new SafeFileEnumerator(
         directoryEnumerator.Current, 
         pattern, 
         SearchOption.AllDirectories, 
         errors 
         ).GetEnumerator(); 
       } 
       catch (Exception ex) 
       { 
        errors.Add(ex); 
        continue; 
       } 

       // Open up the current folder file enumerator 
       if (fileEnumerator.MoveNext()) 
        return true; 
      } 

      Dispose(true, true); 

      return false; 
     } 

     public void Reset() 
     { 
      Dispose(true,true); 

      // Safely get the enumerators, including in the case where the root is not accessable 
      if (root != null) 
      { 
       try 
       { 
        fileEnumerator = root.GetFileSystemInfos(pattern, SearchOption.TopDirectoryOnly).AsEnumerable<FileSystemInfo>().GetEnumerator(); 
       } 
       catch (Exception ex) 
       { 
        errors.Add(ex); 
        fileEnumerator = null; 
       } 

       try 
       { 
        directoryEnumerator = root.GetDirectories(pattern, SearchOption.TopDirectoryOnly).AsEnumerable<DirectoryInfo>().GetEnumerator(); 
       } 
       catch (Exception ex) 
       { 
        errors.Add(ex); 
        directoryEnumerator = null; 
       } 
      } 
     } 
    } 
    public IEnumerator<FileSystemInfo> GetEnumerator() 
    { 
     return new Enumerator(root, pattern, searchOption, errors); 
    } 

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 
    { 
     return GetEnumerator(); 
    } 
} 
İlgili konular