2012-03-17 14 views
5

c: पर, मेरे पास हजारों *.foobar फ़ाइलें हैं। वे सभी प्रकार के स्थानों में हैं (यानी subdirs)। ये फ़ाइलें लगभग 1 - 64 केबी आकार में हैं, और सादे टेक्स्ट।सबसे तेज़/सुरक्षित फ़ाइल ढूंढना/पार्सिंग?

मेरे पास class Foobar(string fileContents) है जो दृढ़ता से इन .foobar फ़ाइलों को टाइप करता है।

मेरी चुनौती *.foobarc: पर फ़ाइलों की एक सूची प्राप्त होती है, जो Foobar ऑब्जेक्ट्स की एक सरणी के रूप में दर्शाती है। ऐसा करने का सबसे तेज़ तरीका क्या है?

मुझे यह जानने में दिलचस्पी है कि मेरे पहले दृष्टिकोण की तुलना में कोई बेहतर तरीका है (निस्संदेह), और यदि मेरे इस दृष्टिकोण में कोई संभावित समस्याएं हैं (उदाहरण के लिए I/O समवर्ती मुद्दे अपवाद फेंकने वाले मुद्दे हैं?):

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

समानांतर में ऑपरेशन करना शायद आपको अधिक खरीद नहीं रहा है; भौतिक डिस्क पर फ़ाइलों की खोज करना आवश्यक रूप से एक I/O बाध्य ऑपरेशन है। –

+0

बेवकूफ सवाल: फ़ाइल खोज वास्तव में डिस्क I/O की आवश्यकता है? मुझे लगता है कि डिस्क की फाइल सिस्टम संरचना ऑपरेटिंग सिस्टम कर्नेल द्वारा कैश की गई है और केवल आवश्यकतानुसार अपडेट की गई है, क्योंकि संरचना डिस्क पर सामग्री से अलग है। नहीं? – user979672

+0

यदि खोज I/O बाध्य है, तो एकमात्र चीज '.Parallel()' खरीदती है 'नया Foobar()' ऑपरेशन थ्रेडिंग (जो समय ले सकता है; इसे एक विशाल स्ट्रिंग के माध्यम से पार्स करना होगा)। सही बात? मुझे आश्चर्य है कि प्रत्येक 'नए Foobar() 'के लिए नए धागे कताई की लागत सिर्फ एक ही धागे में' नई Foobar()' वस्तुओं को क्रमशः बनाने से अधिक महंगा है। – user979672

उत्तर

8

क्योंकि अनुमति त्रुटियों (या अन्य त्रुटियों) जाहिरा तौर पर ने रास्ते में ही शुमार मृत बंद कर सकते हैं, तो आप इस तरह अपने खुद के प्रगणक कुछ लागू करना चाहते हो सकता है:

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

महान काम है, यहाँ एक विस्तार है FileSystemInfo वापस करने के लिए अपने कोड पर स्ट्रिंग पथ के बजाय है। लाइन में कुछ मामूली परिवर्तन, जैसे सर्चऑप्शन (जैसे मूल .net में) में जोड़ना, और प्रारंभिक निर्देशिका पर फंसने में त्रुटि रूट फ़ोल्डर पहुंच से वंचित हो जाती है। मूल पोस्टिंग के लिए फिर से धन्यवाद!

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(); 
    } 
} 
संबंधित मुद्दे