def __init__(self,
                 app_id,
                 datastore_file,
                 require_indexes=False,
                 verbose=False,
                 service_name='datastore_v3',
                 trusted=False,
                 consistency_policy=None,
                 root_path=None,
                 use_atexit=True,
                 auto_id_policy=datastore_stub_util.SEQUENTIAL):
        """Constructor.

    Initializes the SQLite database if necessary.

    Args:
      app_id: string
      datastore_file: string, path to sqlite database. Use None to create an
          in-memory database.
      require_indexes: bool, default False. If True, composite indexes must
          exist in index.yaml for queries that need them.
      verbose: bool, default False. If True, logs all select statements.
      service_name: Service name expected for all calls.
      trusted: bool, default False. If True, this stub allows an app to access
          the data of another app.
      consistency_policy: The consistency policy to use or None to use the
        default. Consistency policies can be found in
        datastore_stub_util.*ConsistencyPolicy
      root_path: string, the root path of the app.
      use_atexit: bool, indicates if the stub should save itself atexit.
      auto_id_policy: enum, datastore_stub_util.SEQUENTIAL or .SCATTERED
    """
        datastore_stub_util.BaseDatastore.__init__(
            self, require_indexes, consistency_policy, use_atexit
            and datastore_file, auto_id_policy)
        apiproxy_stub.APIProxyStub.__init__(self, service_name)
        datastore_stub_util.DatastoreStub.__init__(self, weakref.proxy(self),
                                                   app_id, trusted, root_path)

        self.__datastore_file = datastore_file

        self.__verbose = verbose

        self.__id_map_sequential = {}
        self.__id_map_scattered = {}
        self.__id_counter_tables = {
            datastore_stub_util.SEQUENTIAL:
            ('IdSeq', self.__id_map_sequential),
            datastore_stub_util.SCATTERED:
            ('ScatteredIdCounters', self.__id_map_scattered),
        }
        self.__id_lock = threading.Lock()

        if self.__verbose:
            sql_conn = SQLiteConnectionWrapper
        else:
            sql_conn = sqlite3.Connection

        self.__connection = sqlite3.connect(self.__datastore_file
                                            or ':memory:',
                                            timeout=_MAX_TIMEOUT,
                                            check_same_thread=False,
                                            factory=sql_conn)

        self.__connection.text_factory = lambda x: unicode(
            x, 'utf-8', 'ignore')

        self.__connection_lock = threading.RLock()

        self.__namespaces = set()

        self.__query_history = {}

        self._RegisterPseudoKind(KindPseudoKind())
        self._RegisterPseudoKind(PropertyPseudoKind())
        self._RegisterPseudoKind(NamespacePseudoKind())
        self._RegisterPseudoKind(datastore_stub_util.EntityGroupPseudoKind())

        try:
            self.__Init()
        except sqlite3.DatabaseError, e:
            raise apiproxy_errors.ApplicationError(
                datastore_pb.Error.INTERNAL_ERROR,
                self.READ_ERROR_MSG % (self.__datastore_file, e))
Example #2
0
    def __init__(self,
                 app_id,
                 datastore_file,
                 history_file=None,
                 require_indexes=False,
                 service_name='datastore_v3',
                 trusted=False,
                 consistency_policy=None,
                 save_changes=True,
                 root_path=None,
                 use_atexit=True,
                 auto_id_policy=datastore_stub_util.SEQUENTIAL):
        """Constructor.

    Initializes and loads the datastore from the backing files, if they exist.

    Args:
      app_id: string
      datastore_file: string, stores all entities across sessions.  Use None
          not to use a file.
      history_file: DEPRECATED. No-op.
      require_indexes: bool, default False.  If True, composite indexes must
          exist in index.yaml for queries that need them.
      service_name: Service name expected for all calls.
      trusted: bool, default False.  If True, this stub allows an app to
        access the data of another app.
      consistency_policy: The consistency policy to use or None to use the
        default. Consistency policies can be found in
        datastore_stub_util.*ConsistencyPolicy
      save_changes: bool, default True. If this stub should modify
        datastore_file when entities are changed.
      root_path: string, the root path of the app.
      use_atexit: bool, indicates if the stub should save itself atexit.
      auto_id_policy: enum, datastore_stub_util.SEQUENTIAL or .SCATTERED
    """

        self.__datastore_file = datastore_file
        self.__save_changes = save_changes

        self.__entities_by_kind = collections.defaultdict(dict)
        self.__entities_by_group = collections.defaultdict(dict)
        self.__entities_lock = threading.Lock()

        self.__schema_cache = {}

        self.__id_counters = {
            datastore_stub_util.SEQUENTIAL: 1L,
            datastore_stub_util.SCATTERED: 1L
        }
        self.__id_lock = threading.Lock()

        self.__file_lock = threading.Lock()

        datastore_stub_util.BaseDatastore.__init__(
            self, require_indexes, consistency_policy, use_atexit
            and self.__IsSaveable(), auto_id_policy)
        apiproxy_stub.APIProxyStub.__init__(self, service_name)
        datastore_stub_util.DatastoreStub.__init__(self, weakref.proxy(self),
                                                   app_id, trusted, root_path)

        self._RegisterPseudoKind(KindPseudoKind())
        self._RegisterPseudoKind(PropertyPseudoKind())
        self._RegisterPseudoKind(NamespacePseudoKind())
        self._RegisterPseudoKind(datastore_stub_util.EntityGroupPseudoKind())

        self.Read()