Esempio n. 1
0
 def test_b(self):
     filename = tempfile.mktemp()
     s = FileStorage2(filename)
     connection = Connection(s)
     root = connection.get_root()
     root['a'] = Persistent()
     root['a'].b = 1
     connection.commit()
     root['b'] = Persistent()
     connection.commit()
     root['b'].a = root['a']
     root['a'].b = 2
     connection.commit()
     root['a'].b = 3
     connection.commit()
     s.close()
     hc = HistoryConnection(filename)
     a = hc.get_root()['a']
     assert len(hc.get_storage().index.history) == 6
     hc.previous_instance(a)
     assert a.b == 2
     hc.previous_instance(a)
     assert a.b == 1
     hc.previous_instance(a)
     assert not hasattr(a, '__dict__')
     assert hc.get_root().keys() == []
     hc.get_storage().fp.close()
     os.unlink(filename)
Esempio n. 2
0
 def test_check_touch_every_reference(self):
     connection = Connection(self._get_storage())
     root = connection.get_root()
     root['a'] = Persistent()
     root['b'] = Persistent()
     from schevo.store.persistent_list import PersistentList
     root['b'].c = PersistentList()
     connection.commit()
     touch_every_reference(connection, 'PersistentList')
     assert root['b']._p_is_unsaved()
     assert root['b'].c._p_is_unsaved()
     assert not root._p_is_unsaved()
Esempio n. 3
0
 def open(self):
     """Open the underlying storage based on initial arguments."""
     if not self.is_open:
         try:
             self.storage = FileStorage(self.database, fp=self.fp)
         except RuntimeError:
             raise DatabaseFileLocked()
         self.conn = Connection(self.storage, cache_size=self.cache_size)
         self.is_open = True
Esempio n. 4
0
 def test_check_more(self):
     storage = TempFileStorage()
     connection = Connection(storage)
     root=connection.get_root()
     assert root._p_is_ghost()
     root['a'] = 1
     assert root._p_is_unsaved()
     del root['a']
     connection.abort()
     assert root._p_is_ghost()
     try:
         root['a']
         assert 0
     except KeyError: pass
     root._p_set_status_saved()
     assert root._p_is_saved()
     root._p_set_status_unsaved()
     assert root._p_is_unsaved()
     root._p_set_status_ghost()
     assert root._p_is_ghost()
     root._p_set_status_unsaved()
Esempio n. 5
0
 def test_a(self):
     filename = tempfile.mktemp()
     s = FileStorage2(filename)
     connection = Connection(s)
     root = connection.get_root()
     root['a'] = Persistent()
     root['a'].b = 1
     connection.commit()
     root['a'].b = 2
     connection.commit()
     s.close()
     hc = HistoryConnection(filename)
     a = hc.get_root()['a']
     assert len(hc.get_storage().index.history) == 4
     assert a.b == 2
     hc.previous()
     assert a.b == 1
     hc.next()
     assert a.b == 2
     hc.previous()
     assert a.b == 1
     hc.previous()
     assert a._p_is_ghost()
     assert not hasattr(a, '__dict__')
     assert isinstance(a, Persistent)
     assert raises(KeyError, getattr, a, 'b')
     assert hc.get(a._p_oid) is a
     hc.next()
     assert a.b == 1
     hc.get_storage().fp.close()
     os.unlink(filename)
Esempio n. 6
0
class TestDurus(object):

    def setUp(self):
        self.connection = Connection(TempFileStorage())

    def tearDown(self):
        del self.connection

    def test_a(self):
        bt = self.connection.get_root()['bt'] = BTree()
        t = bt.root.minimum_degree
        assert self.connection.get_cache_count() == 1
        for x in range(2 * t - 1):
            bt.add(x)
        self.connection.commit()
        assert self.connection.get_cache_count() == 3
        bt.add(2 * t - 1)
        self.connection.commit()
        assert self.connection.get_cache_count() == 5
Esempio n. 7
0
 def test_check_storage_tools(self):
     connection = Connection(self._get_storage())
     root = connection.get_root()
     root['a'] = Persistent()
     root['b'] = Persistent()
     connection.commit()
     index = get_reference_index(connection.get_storage())
     assert index == {p64(1): [p64(0)], p64(2): [p64(0)]}
     census = get_census(connection.get_storage())
     assert census == {'PersistentDict':1, 'PersistentTester':2}
     references = list(gen_referring_oid_record(connection.get_storage(),
                                                p64(1)))
     assert references == [(p64(0), connection.get_storage().load(p64(0)))]
     class Fake(object):
         pass
     s = Fake()
     s.__class__ = Storage
     assert raises(RuntimeError, s.__init__)
     assert raises(NotImplementedError, s.load, None)
     assert raises(NotImplementedError, s.begin)
     assert raises(NotImplementedError, s.store, None, None)
     assert raises(NotImplementedError, s.end)
     assert raises(NotImplementedError, s.sync)
     assert raises(NotImplementedError, s.gen_oid_record)
Esempio n. 8
0
class SchevoStoreBackend(object):

    DEFAULT_CACHE_SIZE = 100000

    description = 'Built-in backend, based on Durus 3.4'
    backend_args_help = """
    Use "schevostore:///:memory:" for an in-memory database.

    cache_size=%(DEFAULT_CACHE_SIZE)i (int)
        Set the size of the in-memory object cache to SIZE, which is an
        integer specifying the maximum number of objects to keep in the
        cache.

    fp=None (file-like object)
        Optional file object to use instead of an actual file in the
        filesystem.
    """ % locals()

    __test__ = False

    BTree = BTree
    PDict = PersistentDict
    PList = PersistentList

    TestMethods_CreatesDatabase = TestMethods_CreatesDatabase
    TestMethods_CreatesSchema = TestMethods_CreatesSchema
    TestMethods_EvolvesSchemata = TestMethods_EvolvesSchemata

    def __init__(self,
                 database,
                 fp=None,
                 cache_size=DEFAULT_CACHE_SIZE,
                 ):
        self.database = database
        if database == ':memory:' and fp is None:
            fp = StringIO()
        self.fp = fp
        self.cache_size = cache_size
        self.is_open = False
        self.open()

    @classmethod
    def usable_by_backend(cls, filename):
        """Return (`True`, *additional backend args*) if the named
        file is usable by this backend, or `False` if not."""
        # Get first 128 bytes of file.
        f = open(filename, 'rb')
        try:
            try:
                header = f.read(128)
            except IOError:
                if sys.platform == 'win32':
                    raise DatabaseFileLocked()
                else:
                    raise
        finally:
            f.close()
        # Look for Durus file storage signature and schevo.store
        # module signature.
        if header[:5] == 'DFS20':
            if 'schevo.store.persistent_dict' in header:
                return (True, {})
        return False

    @property
    def has_db(self):
        """Return `True` if the backend contains a Schevo database."""
        return self.get_root().has_key('SCHEVO')

    def close(self):
        """Close the underlying storage (and the connection if
        needed)."""
        self.storage.close()
        self.is_open = False

    def get_root(self):
        """Return the backend's `root` object."""
        return self.conn.get_root()

    def commit(self):
        """Commit the current transaction."""
        self.conn.commit()

    def open(self):
        """Open the underlying storage based on initial arguments."""
        if not self.is_open:
            try:
                self.storage = FileStorage(self.database, fp=self.fp)
            except RuntimeError:
                raise DatabaseFileLocked()
            self.conn = Connection(self.storage, cache_size=self.cache_size)
            self.is_open = True

    def pack(self):
        """Pack the underlying storage."""
        self.conn.pack()

    def rollback(self):
        """Abort the current transaction."""
        self.conn.abort()
Esempio n. 9
0
 def __init__(self, filename):
     Connection.__init__(self, HistoryFileStorage(filename))
Esempio n. 10
0
 def setUp(self):
     self.connection = Connection(TempFileStorage())
Esempio n. 11
0
class SchevoStoreBackend(object):

    description = 'Built-in backend, based on Durus 3.4'
    backend_args_help = """
    cache_size=SIZE
        Set the size of the in-memory object cache to SIZE, which is an
        integer specifying the maximum number of objects to keep in the
        cache.
    """

    __test__ = False

    BTree = BTree
    PDict = PersistentDict
    PList = PersistentList

    TestMethods_CreatesDatabase = TestMethods_CreatesDatabase
    TestMethods_CreatesSchema = TestMethods_CreatesSchema
    TestMethods_EvolvesSchemata = TestMethods_EvolvesSchemata

    def __init__(self, filename, fp=None, cache_size=100000):
        """Create a new `SchevoStoreBackend` instance.

        - `filename`: Name of file to open with this backend. If
          `None`, the backend will expect that you passed in a value
          for `fp` instead.
        - `fp`: (optional) File-like object to use instead of an
          actual file in the filesystem.
        - `cache_size`: Maximum number of objects to keep in the
          in-memory object cache.
        """
        self._filename = filename
        self._fp = fp
        self._cache_size = cache_size
        self._is_open = False
        self.open()

    @classmethod
    def args_from_string(cls, s):
        """Return a dictionary of keyword arguments based on a string given
        to a command-line tool."""
        kw = {}
        if s is not None:
            for arg in (p.strip() for p in s.split(',')):
                name, value = (p2.strip() for p2 in arg.split('='))
                if name == 'cache_size':
                    kw[name] = int(value)
                else:
                    raise KeyError(
                        '%s is not a valid name for backend args' % name)
        return kw

    @classmethod
    def usable_by_backend(cls, filename):
        """Return (`True`, *additional backend args*) if the named
        file is usable by this backend, or `False` if not."""
        # Get first 128 bytes of file.
        f = open(filename, 'rb')
        try:
            try:
                header = f.read(128)
            except IOError:
                if sys.platform == 'win32':
                    raise DatabaseFileLocked()
                else:
                    raise
        finally:
            f.close()
        # Look for Durus file storage signature and schevo.store
        # module signature.
        if header[:5] == 'DFS20':
            if 'schevo.store.persistent_dict' in header:
                return (True, {})
        return False

    @property
    def has_db(self):
        """Return `True` if the backend contains a Schevo database."""
        return self.get_root().has_key('SCHEVO')

    def close(self):
        """Close the underlying storage (and the connection if
        needed)."""
        self.storage.close()
        self._is_open = False

    def get_root(self):
        """Return the backend's `root` object."""
        return self.conn.get_root()

    def commit(self):
        """Commit the current transaction."""
        self.conn.commit()

    def open(self):
        """Open the underlying storage based on initial arguments."""
        if not self._is_open:
            try:
                self.storage = FileStorage(self._filename, fp=self._fp)
            except RuntimeError:
                raise DatabaseFileLocked()
            self.conn = Connection(self.storage, cache_size=self._cache_size)
            self._is_open = True

    def pack(self):
        """Pack the underlying storage."""
        self.conn.pack()

    def rollback(self):
        """Abort the current transaction."""
        self.conn.abort()