Rev 2651: Ensure that we have Tree.get_file_lines() and get_file_text() available. in http://bzr.arbash-meinel.com/branches/bzr/0.19-dev/faster_knit_extract

John Arbash Meinel john at arbash-meinel.com
Tue Jul 24 01:13:49 BST 2007


At http://bzr.arbash-meinel.com/branches/bzr/0.19-dev/faster_knit_extract

------------------------------------------------------------
revno: 2651
revision-id: john at arbash-meinel.com-20070724001312-86g4pqhi2jswl78d
parent: john at arbash-meinel.com-20070723232806-n0hcfp5c9zbyehe2
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: faster_knit_extract
timestamp: Mon 2007-07-23 19:13:12 -0500
message:
  Ensure that we have Tree.get_file_lines() and get_file_text() available.
  This allows us to have specific implementations based on what ends up being the
  most efficient.
  Callers can ask for what they really want, and implementations can try
  and make that call fast.
  It helps a little, it also allows TreeTransform.*_by_entry() to not have
  knits extract into lines, to combine into a single string, to wrap with
  a StringIO(), just to call readlines() when we are done.
added:
  bzrlib/tests/tree_implementations/test_get_file.py test_get_file.py-20070724000545-9wa6h3kznj0no4y2-1
modified:
  bzrlib/revisiontree.py         revisiontree.py-20060724012533-bg8xyryhxd0o0i0h-1
  bzrlib/tests/tree_implementations/__init__.py __init__.py-20060717075546-420s7b0bj9hzeowi-2
  bzrlib/transform.py            transform.py-20060105172343-dd99e54394d91687
  bzrlib/tree.py                 tree.py-20050309040759-9d5f2496be663e77
  bzrlib/workingtree.py          workingtree.py-20050511021032-29b6ec0a681e02e3
-------------- next part --------------
=== added file 'bzrlib/tests/tree_implementations/test_get_file.py'
--- a/bzrlib/tests/tree_implementations/test_get_file.py	1970-01-01 00:00:00 +0000
+++ b/bzrlib/tests/tree_implementations/test_get_file.py	2007-07-24 00:13:12 +0000
@@ -0,0 +1,72 @@
+# Copyright (C) 2006, 2007 Canonical Ltd
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+"""Tests for the get_file* functions for Tree implementations."""
+
+from bzrlib import (
+    errors,
+    tests,
+    )
+from bzrlib.tests import TestSkipped
+from bzrlib.tests.tree_implementations import TestCaseWithTree
+
+
+class TestGetFile(TestCaseWithTree):
+
+    def test_file_has_read(self):
+        work_tree = self.make_branch_and_tree('wt')
+        content = 'file contents\nand more contents\n'
+        self.build_tree_contents([('wt/file', content)])
+        work_tree.add(['file'], ['test-file-id'])
+        tree = self._convert_tree(work_tree)
+        tree.lock_read()
+        self.addCleanup(tree.unlock)
+        f = tree.get_file('test-file-id')
+        self.assertEqual(content, f.read())
+
+    def test_get_file_text(self):
+        work_tree = self.make_branch_and_tree('wt')
+        content = 'file contents\nand more contents\n'
+        self.build_tree_contents([('wt/file', content)])
+        work_tree.add(['file'], ['test-file-id'])
+        tree = self._convert_tree(work_tree)
+        tree.lock_read()
+        self.addCleanup(tree.unlock)
+        self.assertEqual(content, tree.get_file_text('test-file-id'))
+
+    def test_file_has_readlines(self):
+        work_tree = self.make_branch_and_tree('wt')
+        content = 'file contents\nand more contents\n'
+        self.build_tree_contents([('wt/file', content)])
+        work_tree.add(['file'], ['test-file-id'])
+        tree = self._convert_tree(work_tree)
+        tree.lock_read()
+        self.addCleanup(tree.unlock)
+        f = tree.get_file('test-file-id')
+        self.assertEqual(['file contents\n', 'and more contents\n'],
+                         f.readlines())
+
+    def test_get_file_lines(self):
+        work_tree = self.make_branch_and_tree('wt')
+        content = 'file contents\nand more contents\n'
+        self.build_tree_contents([('wt/file', content)])
+        work_tree.add(['file'], ['test-file-id'])
+        tree = self._convert_tree(work_tree)
+        tree.lock_read()
+        self.addCleanup(tree.unlock)
+        self.assertEqual(['file contents\n', 'and more contents\n'],
+                         tree.get_file_lines('test-file-id'))
+

=== modified file 'bzrlib/revisiontree.py'
--- a/bzrlib/revisiontree.py	2007-07-17 20:04:13 +0000
+++ b/bzrlib/revisiontree.py	2007-07-24 00:13:12 +0000
@@ -79,7 +79,9 @@
 
     def get_file_text(self, file_id):
         file_id = osutils.safe_file_id(file_id)
-        return ''.join(self.get_file_lines(file_id))
+        ie = self._inventory[file_id]
+        weave = self._get_weave(file_id)
+        return weave.get_text(ie.revision)
 
     def get_file(self, file_id):
         file_id = osutils.safe_file_id(file_id)

=== modified file 'bzrlib/tests/tree_implementations/__init__.py'
--- a/bzrlib/tests/tree_implementations/__init__.py	2007-06-28 07:15:28 +0000
+++ b/bzrlib/tests/tree_implementations/__init__.py	2007-07-24 00:13:12 +0000
@@ -323,6 +323,7 @@
 def test_suite():
     result = TestSuite()
     test_tree_implementations = [
+        'bzrlib.tests.tree_implementations.test_get_file',
         'bzrlib.tests.tree_implementations.test_get_file_mtime',
         'bzrlib.tests.tree_implementations.test_get_symlink_target',
         'bzrlib.tests.tree_implementations.test_inv',

=== modified file 'bzrlib/transform.py'
--- a/bzrlib/transform.py	2007-07-20 05:06:32 +0000
+++ b/bzrlib/transform.py	2007-07-24 00:13:12 +0000
@@ -1376,7 +1376,7 @@
     name = entry.name
     kind = entry.kind
     if kind == 'file':
-        contents = tree.get_file(entry.file_id).readlines()
+        contents = tree.get_file_lines(entry.file_id)
         executable = tree.is_executable(entry.file_id)
         return tt.new_file(name, parent_id, contents, entry.file_id, 
                            executable)
@@ -1395,7 +1395,7 @@
     """Create new file contents according to an inventory entry."""
     if entry.kind == "file":
         if lines is None:
-            lines = tree.get_file(entry.file_id).readlines()
+            lines = tree.get_file_lines(entry.file_id)
         tt.create_file(lines, trans_id, mode_id=mode_id)
     elif entry.kind == "symlink":
         tt.create_symlink(tree.get_symlink_target(entry.file_id), trans_id)

=== modified file 'bzrlib/tree.py'
--- a/bzrlib/tree.py	2007-07-19 15:44:17 +0000
+++ b/bzrlib/tree.py	2007-07-24 00:13:12 +0000
@@ -213,6 +213,14 @@
         """Return a file object for the file file_id in the tree."""
         raise NotImplementedError(self.get_file)
 
+    def get_file_lines(self, file_id):
+        """Return the text lines for the file file_id in the tree."""
+        raise NotImplementedError(self.get_file_lines)
+
+    def get_file_text(self, file_id):
+        """Return a string with the text for the file file_id in the tree."""
+        raise NotImplementedError(self.get_file_lines)
+
     def get_file_mtime(self, file_id, path=None):
         """Return the modification time for a file.
 
@@ -223,7 +231,7 @@
         raise NotImplementedError(self.get_file_mtime)
 
     def get_file_by_path(self, path):
-        return self.get_file(self._inventory.path2id(path))
+        return self.get_file(self.path2id(path))
 
     def get_symlink_target(self, file_id):
         """Get the target for a given file_id.

=== modified file 'bzrlib/workingtree.py'
--- a/bzrlib/workingtree.py	2007-07-17 18:28:49 +0000
+++ b/bzrlib/workingtree.py	2007-07-24 00:13:12 +0000
@@ -452,9 +452,19 @@
         file_id = osutils.safe_file_id(file_id)
         return self.get_file_byname(self.id2path(file_id))
 
+    def get_file_lines(self, file_id):
+        f = self.get_file(file_id)
+        try:
+            return f.readlines()
+        finally:
+            f.close()
+
     def get_file_text(self, file_id):
-        file_id = osutils.safe_file_id(file_id)
-        return self.get_file(file_id).read()
+        f = self.get_file(file_id)
+        try:
+            return f.read()
+        finally:
+            f.close()
 
     def get_file_byname(self, filename):
         return file(self.abspath(filename), 'rb')



More information about the bazaar-commits mailing list