Commits

Ali Gholami Rudi  committed 8562e48

Removed sqlite and shelve objectdbs

  • Participants
  • Parent commits 5d7f7f7

Comments (0)

Files changed (6)

File rope/base/default_config.py

     # You can extend python path:
     #prefs.add('python_path', '~/python/')
 
-    # This option tells rope how to hold and save object information.
-    # Possible values are:
-    #
-    # * 'memory': It holds all information in
-    #   memory.  So it is the fastest and the least memory efficient.
-    #   Its biggest problem is that the data is not saved and
-    #   the information is lost when you open a project in future.
-    #   You probably never want to use this (it is used in unit
-    #   tests), but if you decide not to have rope folder (see ~/.rope
-    #   file) this db is used.
-    #
-    # * 'persisted_memory': Exactly like 'memory' but the information is
-    #   saved for future sessions.  The problem with this approach is
-    #   that it might take lots of memory (this is not an issue for
-    #   small to medium-sized projects).
-    #
-    # * 'shelve': It stores data in `shelve` files.  This solves
-    #   both the memory efficiency and the persistency problems.  But
-    #   `shelve` is known to cause misterious problems in rare
-    #   conditions.
-    #
-    # * 'sqlite': It uses `sqlite3` module which is available in
-    #   Python distributions starting from ``2.5``.  It is like
-    #   'shelve' but probably more reliable.  But it is much less CPU
-    #   efficient.
-    #
-    # 'persisted_memory' is the best most of the time.  If your
-    # project is very large, you might consider 'shelve' or the
-    # slower 'sqlite'.
-    prefs['objectdb_type'] = 'persisted_memory'
+    # Should rope save object information or not.
+    prefs['save_objectdb'] = True
     prefs['compress_objectdb'] = False
 
     # Shows whether to save history across sessions.  Defaults to

File rope/base/oi/objectinfo.py

 import sys
+import warnings
 
 import rope.base.project
 from rope.base import exceptions
-from rope.base.oi import objectdb, memorydb, shelvedb, transform
+from rope.base.oi import objectdb, memorydb, transform
 
 
 class ObjectInfoManager(object):
             self._init_validation()
 
     def _init_objectdb(self):
-        preferred = self.project.get_prefs().get('objectdb_type', 'memory')
+        dbtype = self.project.get_prefs().get('objectdb_type', None)
+        if dbtype is not None:
+            warnings.warn(
+                '"objectdb_type" project config is deprecated;\n'
+                'Use "save_objectdb" instead in your project '
+                'config file.\n(".ropeproject/config.py" by default)\n',
+                DeprecationWarning)
+            save = dbtype != 'memory'
+        else:
+            save = self.project.get_prefs().get('save_objectdb', False)
         self.validation = TextualValidation(self.to_pyobject)
-        if preferred == 'memory' or self.project.ropefolder is None:
-            db = memorydb.MemoryDB(self.project)
-        elif preferred == 'sqlite' and sys.version_info >= (2, 5, 0):
-            import rope.base.oi.sqlitedb
-            db = rope.base.oi.sqlitedb.SqliteDB(self.project)
-        elif preferred == 'shelve':
-            db = shelvedb.ShelveDB(self.project)
-        elif True or preferred == 'persisted_memory':
+        if save and self.project.ropefolder is not None:
             db = memorydb.MemoryDB(self.project, persist=True)
+        else:
+            db = memorydb.MemoryDB(self.project, persist=False)
         self.objectdb = objectdb.ObjectDB(db, self.validation)
 
     def _init_validation(self):

File rope/base/oi/shelvedb.py

-import os
-import random
-import shelve
-
-from rope.base.oi import objectdb, memorydb
-
-
-class ShelveDB(objectdb.FileDict):
-
-    def __init__(self, project):
-        self.project = project
-        self._root = None
-        self._index = None
-        self.cache = {}
-        self.random = random.Random()
-        self.files = self
-
-    def keys(self):
-        return self.index.keys()
-
-    def __contains__(self, key):
-        return key in self.index
-
-    def __getitem__(self, key):
-        return memorydb.FileInfo(self._get_file_dict(key))
-
-    def create(self, key):
-        self._get_file_dict(key, readonly=False)
-
-    def __delitem__(self, key):
-        self._remove_file(key)
-
-    def _get_root(self):
-        if self._root is None:
-            self._root = self._get_resource(self.project.ropefolder,
-                                            'objectdb', is_folder=True)
-        return self._root
-
-    def _get_index(self):
-        if self._index is None:
-            index_file = self.project.get_file(self.root.path + '/index.shelve')
-            self._index = shelve.open(index_file.real_path, writeback=True)
-        return self._index
-
-    root = property(_get_root)
-    index = property(_get_index)
-
-    def _get_resource(self, parent, name, is_folder=False):
-        if parent.has_child(name):
-            return parent.get_child(name)
-        else:
-            if is_folder:
-                return parent.create_folder(name)
-            else:
-                return parent.create_file(name)
-
-    def _get_name_for_path(self, path):
-        base_name = os.path.basename(path)
-        def to_hex(i):
-            return hex(i).replace('0x', '', 1).replace('L', '')
-        hashed = to_hex(hash(path))
-        if not hashed.startswith('-'):
-            hashed = '-' + hashed
-        second_list = list(to_hex(id(path)) +
-                           to_hex(self.random.randint(0, 255)))
-        self.random.shuffle(second_list)
-        shuffled = ''.join(second_list)
-        return base_name + hashed + shuffled + '.shelve'
-
-    def _get_file_dict(self, path, readonly=True):
-        if path not in self.cache:
-            resource = self._get_file_resource(path)
-            if readonly and not resource.exists():
-                return
-            self.cache[path] = shelve.open(resource.real_path, writeback=True)
-        return self.cache[path]
-
-    def _get_file_resource(self, path):
-        if path not in self.index:
-            self.index[path] = self._add_file_for_path(path)
-        name = self.index[path]
-        return self.project.get_file(self.root.path + '/' + name)
-
-    def _add_file_for_path(self, path):
-        while True:
-            new_name = self._get_name_for_path(path)
-            new_resource = self.project.get_file(self.root.path
-                                                 + '/' + new_name)
-            if not new_resource.exists():
-                break
-        return new_name
-
-    def sync(self):
-        self.index.close()
-        for file_dict in self.cache.values():
-            file_dict.close()
-        self._index = None
-        self.cache.clear()
-
-    def rename(self, file, newfile):
-        if file not in self.index:
-            return
-        self.index[newfile] = self.index[file]
-        self._remove_file(file, on_disk=False)
-
-    def _remove_file(self, file, on_disk=True):
-        if file not in self.index:
-            return
-        if file in self.cache:
-            self.cache[file].close()
-            del self.cache[file]
-        mapping = self.index[file]
-        del self.index[file]
-        if on_disk:
-            self.root.get_child(mapping).remove()
-
-
-def is_available():
-    try:
-        import bsddb
-    except ImportError:
-        try:
-            import dbhash
-        except ImportError:
-            try:
-                import gdbm
-            except ImportError:
-                return False
-    return True

File rope/base/oi/sqlitedb.py

-import cPickle as pickle
-import UserDict
-import threading
-
-from rope.base.oi import objectdb
-
-try:
-    import sqlite3
-except ImportError:
-    pass
-
-
-class SqliteDB(objectdb.FileDict):
-
-    def __init__(self, project):
-        self.project = project
-        self.all_connections = []
-        self.main_thread = threading.currentThread()
-        self._connection = None
-        self._cursor = None
-        if not self._get_db_file().exists():
-            self.create_tables()
-        self.files_table = Table(self, 'files')
-        self.scopes_table = Table(self, 'scopes')
-        self.callinfos_table = Table(self, 'callinfos')
-        self.pernames_table = Table(self, 'pernames')
-        self.files = FilesTable(self)
-
-    def _get_cursor(self):
-        if self._connection is None:
-            db = self._get_db_file()
-            self._connection = sqlite3.connect(db.real_path)
-            self._cursor = self._connection.cursor()
-        return self._cursor
-
-    def execute(self, command, args=[]):
-        if self.main_thread == threading.currentThread():
-            result = list(self.cursor.execute(command, args).fetchall())
-            self._connection.commit()
-            return result
-        else:
-            db = self._get_db_file()
-            connection = sqlite3.connect(db.real_path)
-            cursor = connection.cursor()
-            result = list(cursor.execute(command, args))
-            connection.commit()
-            cursor.close()
-            connection.close()
-            return result
-
-    def _get_db_file(self):
-        return self.project.get_file(self.project.ropefolder.path +
-                                     '/objectdb.sqlite3')
-
-    cursor = property(_get_cursor)
-
-    def create_tables(self):
-        files_table = 'CREATE TABLE files(' \
-                      'file_id INTEGER NOT NULL,' \
-                      'path VARCHAR(255),' \
-                      'PRIMARY KEY (file_id))'
-
-        scopes_table = 'CREATE TABLE scopes(' \
-                       'scope_id INTEGER NOT NULL,' \
-                       'file_id INTEGER,' \
-                       'key VARCHAR(255),' \
-                       'PRIMARY KEY (scope_id),' \
-                       'FOREIGN KEY (file_id) REFERENCES files (file_id))'
-
-        callinfo_table = 'CREATE TABLE callinfos(' \
-                         'scope_id INTEGER,' \
-                         'args BLOB,' \
-                         'returned BLOB,' \
-                         'FOREIGN KEY (scope_id) REFERENCES scopes (scope_id))'
-
-        pername_table = 'CREATE TABLE pernames(' \
-                        'scope_id INTEGER,' \
-                        'name VARCHAR(255),' \
-                        'value BLOB,' \
-                        'FOREIGN KEY (scope_id) REFERENCES scopes ( scope_id ))'
-
-        self.execute(files_table)
-        self.execute(scopes_table)
-        self.execute(callinfo_table)
-        self.execute(pername_table)
-
-    def sync(self):
-        if self._connection is not None:
-            self._connection.commit()
-            self._cursor.close()
-            self._connection.close()
-            self._cursor = None
-            self._connection = None
-
-
-class FilesTable(objectdb.FileDict):
-
-    def __init__(self, db):
-        self.db = db
-        self.table = db.files_table
-
-    def keys(self):
-        return [item[0] for item in self.table.select('path')]
-
-    def __getitem__(self, path):
-        file_id = self.table.select('file_id', path=path)[0][0]
-        return _FileInfo(self.db, file_id)
-
-    def create(self, path):
-        self.table.insert(path=path)
-
-    def __contains__(self, path):
-        return self.table.contains(path=path)
-
-    def rename(self, path, newpath):
-        self.table.update({'path': newpath}, path=path)
-
-    def __delitem__(self, path):
-        file_id = self.table.select('file_id', path=path)[0][0]
-        keys = self.db.scopes_table.select('key', file_id=file_id)
-        file_info = _FileInfo(self.db, file_id)
-        for key in keys:
-            del file_info[key[0]]
-        self.table.delete(path=path)
-
-
-class _FileInfo(objectdb.FileInfo):
-
-    def __init__(self, db, file_id):
-        self.db = db
-        self.file_id = file_id
-        self.table = db.scopes_table
-
-    def create_scope(self, key):
-        self.table.insert(file_id=self.file_id, key=key)
-
-    def keys(self):
-        return [item[0] for item in self.table.select(
-                'key', file_id=self.file_id)]
-
-    def __contains__(self, key):
-        return self.table.contains(file_id=self.file_id, key=key)
-
-    def __getitem__(self, key):
-        scope_id = self.table.select(
-            'scope_id', file_id=self.file_id, key=key)[0][0]
-        return _ScopeInfo(self.db, scope_id)
-
-    def __delitem__(self, key):
-        scope_id = self.table.select('scope_id', key=key)[0][0]
-        self.db.callinfos_table.delete(scope_id=scope_id)
-        self.db.pernames_table.delete(scope_id=scope_id)
-        self.table.delete(key=key)
-
-
-class _ScopeInfo(objectdb.ScopeInfo):
-
-    def __init__(self, db, scope_id):
-        self.db = db
-        self.scope_id = scope_id
-        self.callinfo = db.callinfos_table
-        self.pername = db.pernames_table
-
-    def get_per_name(self, name):
-        result = self.pername.select('value', scope_id=self.scope_id,
-                                     name=name)
-        if result:
-            return pickle.loads(str(result[0][0]))
-        return None
-
-    def save_per_name(self, name, value):
-        value = buffer(pickle.dumps(value))
-        self.pername.update({'value': value},
-                            scope_id=self.scope_id, name=name)
-
-    def get_returned(self, parameters):
-        parameters = buffer(pickle.dumps(parameters))
-        result = self.callinfo.select('returned', scope_id=self.scope_id,
-                                      args=parameters)
-        if result:
-            return pickle.loads(str(result[0][0]))
-        return None
-
-    def get_call_infos(self):
-        result = self.callinfo.select('args, returned', scope_id=self.scope_id)
-        for args, returned in result:
-            yield objectdb.CallInfo(pickle.loads(str(args)),
-                                    pickle.loads(str(returned)))
-
-    def add_call(self, parameters, returned):
-        parameters = buffer(pickle.dumps(parameters))
-        returned = buffer(pickle.dumps(returned))
-        self.callinfo.update({'returned': returned},
-                             args=parameters, scope_id=self.scope_id)
-
-
-class Table(object):
-
-    def __init__(self, db, name):
-        self.db = db
-        self.name = name
-
-    def select(self, what='*', **kwds):
-        where = self._get_where(kwds)
-        command = 'SELECT %s FROM %s %s' % (what, self.name, where)
-        return self.db.execute(command, kwds.values())
-
-    def _get_where(self, kwds):
-        conditions = []
-        for key in kwds:
-            conditions.append('%s = ?' % key)
-        if conditions:
-            return 'WHERE ' + ' AND '.join(conditions)
-        return ''
-
-    def contains(self, **kwds):
-        return len(self.select(**kwds)) > 0
-
-    def insert(self, **kwds):
-        if self.contains(**kwds):
-            self.delete(**kwds)
-        names = []
-        for name in kwds:
-            names.append(name)
-        values = []
-        for value in kwds.values():
-            values.append(value)
-        command = 'INSERT INTO %s (%s) VALUES (%s)' % \
-                  (self.name, ', '.join(names),
-                   ', '.join(['?'] * len(values)))
-        self.db.execute(command, values)
-
-    def delete(self, **kwds):
-        where = self._get_where(kwds)
-        command = 'DELETE FROM %s %s' % (self.name, where)
-        return self.db.execute(command, kwds.values())
-
-    def update(self, sets, **kwds):
-        if not self.contains(**kwds):
-            new_kwds = dict(kwds)
-            new_kwds.update(sets)
-            self.insert(**new_kwds)
-            return
-        where = self._get_where(kwds)
-        values = []
-        commands = []
-        for key, value in sets.items():
-            commands.append('%s = ?' % key)
-            values.append(value)
-        values.extend(kwds.values())
-        command = 'UPDATE %s SET %s %s' % (self.name,
-                                           ', '.join(commands), where)
-        return self.db.execute(command, values)
-
-
-def is_available():
-    return 'sqlite3' in globals()

File ropetest/objectdbtest.py

 import unittest
 
-from rope.base.oi import objectdb, memorydb, shelvedb, sqlitedb
+from rope.base.oi import objectdb, memorydb
 from ropetest import testutils
 
 
         validation = _MockValidation()
         self.dbs = [
             objectdb.ObjectDB(memorydb.MemoryDB(self.project), validation)]
-        if shelvedb.is_available():
-            self.dbs.append(objectdb.ObjectDB(shelvedb.ShelveDB(self.project),
-                                              validation))
-        if sqlitedb.is_available():
-            self.dbs.append(objectdb.ObjectDB(sqlitedb.SqliteDB(self.project),
-                                              validation))
 
     def tearDown(self):
         for db in self.dbs:

File ropetest/testutils.py

         if os.name == 'posix' and os.path.isdir('/dev/shm'):
             root = '/dev/shm/' + root
     # Using these prefs for faster tests
-    prefs = {'objectdb_type': 'memory', 'save_history': False,
+    prefs = {'save_objectdb': False, 'save_history': False,
              'validate_objectdb': False, 'automatic_soi': False}
     prefs.update(kwds)
     remove_recursively(root)