Ejemplo n.º 1
0
def delete_multi(keys, connection=None, dataset_id=None):
    """Delete the keys in the Cloud Datastore.

    :type keys: list of :class:`gcloud.datastore.key.Key`
    :param keys: The keys to be deleted from the datastore.

    :type connection: :class:`gcloud.datastore.connection.Connection`
    :param connection: Optional connection used to connect to datastore.
                       If not passed, inferred from the environment.

    :type dataset_id: :class:`gcloud.datastore.connection.Connection`
    :param dataset_id: Optional. The dataset ID used to connect to datastore.
                       If not passed, inferred from the environment.

    :raises: EnvironmentError if ``connection`` or ``dataset_id`` not passed,
             and cannot be inferred from the environment.  ValueError if
             one or more keys has a dataset ID not matching the passed /
             inferred dataset ID.
    """
    if not keys:
        return

    connection = _require_connection(connection)
    dataset_id = _require_dataset_id(dataset_id, keys[0])

    # We allow partial keys to attempt a delete, the backend will fail.
    current = Batch.current()
    in_batch = current is not None
    if not in_batch:
        current = Batch(dataset_id=dataset_id, connection=connection)
    for key in keys:
        current.delete(key)
    if not in_batch:
        current.commit()
Ejemplo n.º 2
0
def _require_dataset_id(dataset_id=None, first_key=None):
    """Infer a dataset ID from the environment, if not passed explicitly.

    Order of precedence:

    - Passed `dataset_id` (if not None).
    - `dataset_id` of current batch / transaction (if current exists).
    - `dataset_id` of first key
    - `dataset_id` inferred from the environment (if `set_default_dataset_id`
      has been called).

    :type dataset_id: string
    :param dataset_id: Optional.

    :type first_key: :class:`gcloud.datastore.key.Key` or None
    :param first_key: Optional: first key being manipulated.

    :rtype: string
    :returns: A dataset ID based on the current environment.
    :raises: :class:`EnvironmentError` if ``dataset_id`` is ``None``,
             and cannot be inferred from the environment.
    """
    if dataset_id is not None:
        return dataset_id
    top = Batch.current()
    if top is not None:
        return top.dataset_id
    if first_key is not None:
        return first_key.dataset_id

    dataset_id = _implicit_environ.get_default_dataset_id()
    if dataset_id is None:
        raise EnvironmentError('Dataset ID could not be inferred.')
    return dataset_id
Ejemplo n.º 3
0
def _require_dataset_id(dataset_id=None, first_key=None):
    """Infer a dataset ID from the environment, if not passed explicitly.

    Order of precedence:

    - Passed `dataset_id` (if not None).
    - `dataset_id` of current batch / transaction (if current exists).
    - `dataset_id` of first key
    - `dataset_id` inferred from the environment (if `set_default_dataset_id`
      has been called).

    :type dataset_id: string
    :param dataset_id: Optional.

    :type first_key: :class:`gcloud.datastore.key.Key` or None
    :param first_key: Optional: first key being manipulated.

    :rtype: string
    :returns: A dataset ID based on the current environment.
    :raises: :class:`EnvironmentError` if ``dataset_id`` is ``None``,
             and cannot be inferred from the environment.
    """
    if dataset_id is not None:
        return dataset_id
    top = Batch.current()
    if top is not None:
        return top.dataset_id
    if first_key is not None:
        return first_key.dataset_id

    dataset_id = _implicit_environ.get_default_dataset_id()
    if dataset_id is None:
        raise EnvironmentError('Dataset ID could not be inferred.')
    return dataset_id
Ejemplo n.º 4
0
def delete(keys, connection=None, dataset_id=None):
    """Delete the keys in the Cloud Datastore.

    :type keys: list of :class:`gcloud.datastore.key.Key`
    :param keys: The keys to be deleted from the datastore.

    :type connection: :class:`gcloud.datastore.connection.Connection`
    :param connection: Optional connection used to connect to datastore.
                       If not passed, inferred from the environment.

    :type dataset_id: :class:`gcloud.datastore.connection.Connection`
    :param dataset_id: Optional. The dataset ID used to connect to datastore.
                       If not passed, inferred from the environment.

    :raises: EnvironmentError if ``connection`` or ``dataset_id`` not passed,
             and cannot be inferred from the environment.  ValueError if
             one or more keys has a dataset ID not matching the passed /
             inferred dataset ID.
    """
    if not keys:
        return

    connection = _require_connection(connection)
    dataset_id = _require_dataset_id(dataset_id, keys[0])

    # We allow partial keys to attempt a delete, the backend will fail.
    current = Batch.current()
    in_batch = current is not None
    if not in_batch:
        current = Batch(dataset_id=dataset_id, connection=connection)
    for key in keys:
        current.delete(key)
    if not in_batch:
        current.commit()
Ejemplo n.º 5
0
def put(entities, connection=None, dataset_id=None):
    """Save the entities in the Cloud Datastore.

    :type entities: list of :class:`gcloud.datastore.entity.Entity`
    :param entities: The entities to be saved to the datastore.

    :type connection: :class:`gcloud.datastore.connection.Connection`
    :param connection: Optional connection used to connect to datastore.
                       If not passed, inferred from the environment.

    :type dataset_id: :class:`gcloud.datastore.connection.Connection`
    :param dataset_id: Optional. The dataset ID used to connect to datastore.
                       If not passed, inferred from the environment.

    :raises: EnvironmentError if ``connection`` or ``dataset_id`` not passed,
             and cannot be inferred from the environment.  ValueError if
             one or more entities has a key with a dataset ID not matching
             the passed / inferred dataset ID.
    """
    if not entities:
        return

    connection = _require_connection(connection)
    dataset_id = _require_dataset_id(dataset_id, entities[0].key)

    current = Batch.current()
    in_batch = current is not None
    if not in_batch:
        current = Batch(dataset_id=dataset_id, connection=connection)
    for entity in entities:
        current.put(entity)
    if not in_batch:
        current.commit()
Ejemplo n.º 6
0
    def current():
        """Return the topmost transaction.

        .. note:: if the topmost element on the stack is not a transaction,
                  returns None.

        :rtype: :class:`gcloud.datastore.transaction.Transaction` or None
        """
        top = Batch.current()
        if isinstance(top, Transaction):
            return top
Ejemplo n.º 7
0
    def current():
        """Return the topmost transaction.

        .. note:: if the topmost element on the stack is not a transaction,
                  returns None.

        :rtype: :class:`gcloud.datastore.transaction.Transaction` or None
        """
        top = Batch.current()
        if isinstance(top, Transaction):
            return top
Ejemplo n.º 8
0
def _require_connection(connection=None):
    """Infer a connection from the environment, if not passed explicitly.

    :type connection: :class:`gcloud.datastore.connection.Connection`
    :param connection: Optional.

    :rtype: :class:`gcloud.datastore.connection.Connection`
    :returns: A connection based on the current environment.
    :raises: :class:`EnvironmentError` if ``connection`` is ``None``, and
             cannot be inferred from the environment.
    """
    if connection is None:
        top = Batch.current()
        if top is not None:
            connection = top.connection
        else:
            connection = _implicit_environ.get_default_connection()
            if connection is None:
                raise EnvironmentError('Connection could not be inferred.')
    return connection
Ejemplo n.º 9
0
def _require_connection(connection=None):
    """Infer a connection from the environment, if not passed explicitly.

    :type connection: :class:`gcloud.datastore.connection.Connection`
    :param connection: Optional.

    :rtype: :class:`gcloud.datastore.connection.Connection`
    :returns: A connection based on the current environment.
    :raises: :class:`EnvironmentError` if ``connection`` is ``None``, and
             cannot be inferred from the environment.
    """
    if connection is None:
        top = Batch.current()
        if top is not None:
            connection = top.connection
        else:
            connection = _implicit_environ.get_default_connection()
            if connection is None:
                raise EnvironmentError('Connection could not be inferred.')
    return connection
Ejemplo n.º 10
0
def delete(keys, connection=None):
    """Delete the keys in the Cloud Datastore.

    :type keys: list of :class:`gcloud.datastore.key.Key`
    :param keys: The keys to be deleted from the datastore.

    :type connection: :class:`gcloud.datastore.connection.Connection`
    :param connection: Optional connection used to connect to datastore.
    """
    if not keys:
        return

    connection = connection or _implicit_environ.CONNECTION

    # We allow partial keys to attempt a delete, the backend will fail.
    current = Batch.current()
    in_batch = current is not None
    if not in_batch:
        dataset_id = _get_dataset_id_from_keys(keys)
        current = Batch(dataset_id=dataset_id, connection=connection)
    for key in keys:
        current.delete(key)
    if not in_batch:
        current.commit()
Ejemplo n.º 11
0
def put(entities, connection=None):
    """Save the entities in the Cloud Datastore.

    :type entities: list of :class:`gcloud.datastore.entity.Entity`
    :param entities: The entities to be saved to the datastore.

    :type connection: :class:`gcloud.datastore.connection.Connection`
    :param connection: Optional connection used to connect to datastore.
    """
    if not entities:
        return

    connection = connection or _implicit_environ.CONNECTION

    current = Batch.current()
    in_batch = current is not None
    if not in_batch:
        keys = [entity.key for entity in entities]
        dataset_id = _get_dataset_id_from_keys(keys)
        current = Batch(dataset_id=dataset_id, connection=connection)
    for entity in entities:
        current.put(entity)
    if not in_batch:
        current.commit()