Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions datastore/google/cloud/datastore/_gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
import contextlib
import sys

from google.cloud.gapic.datastore.v1 import datastore_client
from google.api_core.gapic_v1 import client_info
from google.cloud.datastore_v1.gapic import datastore_client
from google.gax.errors import GaxError
from google.gax.grpc import exc_to_code
from google.gax.utils import metrics
Expand Down Expand Up @@ -231,4 +232,9 @@ def make_datastore_api(client):
channel = insecure_channel(host)

return GAPICDatastoreAPI(
channel=channel, lib_name='gccl', lib_version=__version__)
channel=channel,
client_info=client_info.ClientInfo(
client_library_version=__version__,
gapic_version=__version__,
),
)
2 changes: 1 addition & 1 deletion datastore/google/cloud/datastore/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from google.cloud import _http as connection_module
from google.cloud import exceptions
from google.cloud.proto.datastore.v1 import datastore_pb2 as _datastore_pb2
from google.cloud.datastore_v1.proto import datastore_pb2 as _datastore_pb2

from google.cloud.datastore import __version__

Expand Down
2 changes: 1 addition & 1 deletion datastore/google/cloud/datastore/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"""

from google.cloud.datastore import helpers
from google.cloud.proto.datastore.v1 import datastore_pb2 as _datastore_pb2
from google.cloud.datastore_v1.proto import datastore_pb2 as _datastore_pb2


class Batch(object):
Expand Down
2 changes: 1 addition & 1 deletion datastore/google/cloud/datastore/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import os

from google.cloud.proto.datastore.v1 import datastore_pb2 as _datastore_pb2
from google.cloud.datastore_v1.proto import datastore_pb2 as _datastore_pb2

from google.cloud._helpers import _LocalStack
from google.cloud._helpers import (
Expand Down
2 changes: 1 addition & 1 deletion datastore/google/cloud/datastore/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

from google.cloud._helpers import _datetime_to_pb_timestamp
from google.cloud._helpers import _pb_timestamp_to_datetime
from google.cloud.proto.datastore.v1 import entity_pb2 as _entity_pb2
from google.cloud.datastore_v1.proto import entity_pb2 as _entity_pb2
from google.cloud.datastore.entity import Entity
from google.cloud.datastore.key import Key

Expand Down
2 changes: 1 addition & 1 deletion datastore/google/cloud/datastore/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import copy
import six

from google.cloud.proto.datastore.v1 import entity_pb2 as _entity_pb2
from google.cloud.datastore_v1.proto import entity_pb2 as _entity_pb2

from google.cloud._helpers import _to_bytes
from google.cloud.datastore import _app_engine_key_pb2
Expand Down
6 changes: 3 additions & 3 deletions datastore/google/cloud/datastore/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
from google.api_core import page_iterator
from google.cloud._helpers import _ensure_tuple_or_list

from google.cloud.proto.datastore.v1 import datastore_pb2 as _datastore_pb2
from google.cloud.proto.datastore.v1 import entity_pb2 as _entity_pb2
from google.cloud.proto.datastore.v1 import query_pb2 as _query_pb2
from google.cloud.datastore_v1.proto import datastore_pb2 as _datastore_pb2
from google.cloud.datastore_v1.proto import entity_pb2 as _entity_pb2
from google.cloud.datastore_v1.proto import query_pb2 as _query_pb2
from google.cloud.datastore import helpers
from google.cloud.datastore.key import Key

Expand Down
83 changes: 82 additions & 1 deletion datastore/google/cloud/datastore/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,48 @@
from google.cloud.datastore.batch import Batch


class TransactionOptions(object):

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

"""Options for Transactions

TransactionOptions stores the mode to indicate whether it is ReadOnly or
ReadWrite. In the case of ReadWrite an optional parameter allows the
previous transaction id to be stored.

:type readonly: `boolean`

This comment was marked as spam.

This comment was marked as spam.

:param readonly: indicates whether the transaction is readonly (True) or
readwrite (False)

:type previous_transaction: `bytes`
:param previous_transaction: The transaction identifier of the transaction
being retried.

This comment was marked as spam.

This comment was marked as spam.

"""

class ReadWrite(object):

This comment was marked as spam.

This comment was marked as spam.

""" Indicates the transaction allow both reads and writes

:type previous_transaction: `bytes`
:param previous_transaction: The transaction identifier of the
transaction being retried.
"""

def __init__(self, previous_transaction):
self.previous_transaction = previous_transaction

class ReadOnly(object):
""" Indicates the transaction allow only reads """
pass

def __init__(self, readonly=False, previous_transaction=None):
if readonly and previous_transaction is not None:
raise ValueError("Previous Transaction ID can only be "
"supplied for readwrite")
if readonly:
self.mode = self.ReadOnly()
else:
self.mode = self.ReadWrite(previous_transaction)


class Transaction(Batch):
"""An abstraction representing datastore Transactions.

Expand Down Expand Up @@ -152,13 +194,19 @@ def Entity(*args, **kwargs):

:type client: :class:`google.cloud.datastore.client.Client`
:param client: the client used to connect to datastore.

:type options: `google.cloud.datastore.transaction.TransactionOptions`
:param options: the transaction options used for this transaction.
"""

_status = None

def __init__(self, client):
def __init__(self, client, options=None):
super(Transaction, self).__init__(client)
self._id = None
if options is None:
options = TransactionOptions()
self.options = options

@property
def id(self):
Expand Down Expand Up @@ -234,3 +282,36 @@ def commit(self):
finally:
# Clear our own ID in case this gets accidentally reused.
self._id = None

def put(self, entity):
"""Remember an entity's state to be saved during :meth:`commit`.

This comment was marked as spam.

This comment was marked as spam.


.. note::
Any existing properties for the entity will be replaced by those

This comment was marked as spam.

This comment was marked as spam.

currently set on this instance. Already-stored properties which do
not correspond to keys set on this instance will be removed from
the datastore.

.. note::
Property values which are "text" ('unicode' in Python2, 'str' in
Python3) map to 'string_value' in the datastore; values which are
"bytes" ('str' in Python2, 'bytes' in Python3) map to 'blob_value'.

When an entity has a partial key, calling :meth:`commit` sends it as
an ``insert`` mutation and the key is completed. On return,
the key for the ``entity`` passed in is updated to match the key ID
assigned by the server.

:type entity: :class:`google.cloud.datastore.entity.Entity`
:param entity: the entity to be saved.

:raises: :class:`RuntimeError` if the transaction
is marked ReadOnly
:class:`ValueError` if the batch is not in
progress, if entity has no key assigned, or if the key's
``project`` does not match ours.
"""
if isinstance(self.options.mode, self.options.ReadWrite):
super(Transaction, self).put(entity)
else:
raise RuntimeError("Transaction is read only")

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

30 changes: 30 additions & 0 deletions datastore/google/cloud/datastore_v1/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright 2017, Google LLC All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import

from google.cloud.datastore_v1 import types
from google.cloud.datastore_v1.gapic import datastore_client
from google.cloud.datastore_v1.gapic import enums


class DatastoreClient(datastore_client.DatastoreClient):
__doc__ = datastore_client.DatastoreClient.__doc__
enums = enums


__all__ = (
'enums',
'types',
'DatastoreClient', )
Empty file.
Loading