Changeset 1007:0fb499033581


Ignore:
Timestamp:
Dec 25, 2010, 4:43:58 PM (11 years ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
Message:
Added cache auto-resizing (see ticket #54).

Ticket URL: http://ftputil.sschwarzer.net/trac/ticket/54
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • ftp_stat.py

    r1005 r1007  
    417417        """
    418418        lines = self._host_dir(path)
     419        # `cache` is the "high-level" `StatCache` object whereas
     420        #  `cache._cache` is the "low-level" `LRUCache` object.
     421        cache = self._lstat_cache
     422        # Auto-resize cache if the cache up to now can't hold as many
     423        #  entries as there are in the directory `path`.
     424        if cache._enabled and len(lines) >= cache._cache.size:
     425            cache.resize(2 * len(lines))
     426        # Yield stat results from lines.
    419427        for line in lines:
    420428            if self._parser.ignores_line(line):
     
    478486                  "can't stat remote root directory")
    479487        dirname, basename = self._path.split(path)
    480 
    481488        # If even the directory doesn't exist and we don't want the
    482489        #  exception, treat it the same as if the path wasn't found in
  • ftputil.py

    r1001 r1007  
    5959import ftp_path
    6060import ftp_stat
     61import ftp_stat_cache
    6162import ftputil_version
    6263
  • test/test_real_ftp.py

    r999 r1007  
    1919from ftputil import ftp_error
    2020from ftputil import ftp_stat
     21from ftputil import ftp_stat_cache
    2122
    2223
     
    443444        host1.stat_cache.invalidate(absolute_path)
    444445        self.assertRaises(ftp_error.PermanentError, host1.stat, "_testfile_")
     446
     447    def test_cache_auto_resizing(self):
     448        """Test if the cache is resized appropriately."""
     449        host = self.host
     450        cache = host.stat_cache._cache
     451        # Make sure the cache size isn't adjusted towards smaller values.
     452        entries = host.listdir("walk_test")
     453        self.assertEqual(cache.size,
     454                         ftp_stat_cache.StatCache._DEFAULT_CACHE_SIZE)
     455        # Make the cache very small initially and see if it gets resized.
     456        cache.size = 2
     457        entries = host.listdir("walk_test")
     458        # Actually, the cache is going to be 18 because `listdir`
     459        #  implicitly calls `path.isdir` on the directory argument
     460        #  which in turn reads the parent directory of `walk_test`
     461        #  which happens to have 9 entries.
     462        self.assertEqual(cache.size, 18)
    445463
    446464
     
    796814    unittest.main()
    797815    import __main__
    798     #unittest.main(__main__, "TestUnicodePaths")
    799 
     816    #unittest.main(__main__, "TestStat.test_cache_auto_resizing")
     817
Note: See TracChangeset for help on using the changeset viewer.