public void TransactionReadAndWrite() { string projectId = _fixture.ProjectId; string namespaceId = _fixture.NamespaceId; long amount = 1000L; Key fromKey = CreateAccount("Jill", 20000L); Key toKey = CreateAccount("Beth", 15500L); // Sample: TransactionReadAndWrite DatastoreClient client = DatastoreClient.Create(); ByteString transactionId = client.BeginTransaction(projectId).Transaction; using (DatastoreTransaction transaction = DatastoreTransaction.Create(client, projectId, namespaceId, transactionId)) { // The return value from DatastoreTransaction.Get contains the fetched entities // in the same order as they are in the call. IReadOnlyList <Entity> entities = transaction.Lookup(fromKey, toKey); Entity from = entities[0]; Entity to = entities[1]; from["balance"] = (long)from["balance"] - amount; to["balance"] = (long)to["balance"] - amount; transaction.Update(from); transaction.Update(to); transaction.Commit(); } // End sample }
public void Overview() { string projectId = _fixture.ProjectId; string namespaceId = _fixture.NamespaceId; // Sample: Overview DatastoreClient client = DatastoreClient.Create(); KeyFactory keyFactory = new KeyFactory(projectId, namespaceId, "message"); Entity entity = new Entity { Key = keyFactory.CreateIncompleteKey(), ["created"] = DateTime.UtcNow, ["text"] = "Text of the message", ["tags"] = new[] { "tag1", "tag2" } }; ByteString transactionId = client.BeginTransaction(projectId).Transaction; using (DatastoreTransaction transaction = DatastoreTransaction.Create(client, projectId, namespaceId, transactionId)) { transaction.Insert(entity); CommitResponse commitResponse = transaction.Commit(); Key insertedKey = commitResponse.MutationResults[0].Key; Console.WriteLine($"Inserted key: {insertedKey}"); } // End sample }
public void BeginTransaction() { // Snippet: BeginTransaction(string,CallSettings) // Create client DatastoreClient datastoreClient = DatastoreClient.Create(); // Initialize request argument(s) string projectId = ""; // Make the request BeginTransactionResponse response = datastoreClient.BeginTransaction(projectId); // End snippet }
public void BeginTransaction_RequestObject() { // Snippet: BeginTransaction(BeginTransactionRequest,CallSettings) // Create client DatastoreClient datastoreClient = DatastoreClient.Create(); // Initialize request argument(s) BeginTransactionRequest request = new BeginTransactionRequest { ProjectId = "", }; // Make the request BeginTransactionResponse response = datastoreClient.BeginTransaction(request); // End snippet }
public void UpdateEntity() { string projectId = _fixture.ProjectId; string namespaceId = _fixture.NamespaceId; Key key = _fixture.LearnDatastoreKey; // Sample: UpdateEntity DatastoreClient client = DatastoreClient.Create(); ByteString transactionId = client.BeginTransaction(projectId).Transaction; using (DatastoreTransaction transaction = DatastoreTransaction.Create(client, projectId, namespaceId, transactionId)) { Entity entity = transaction.Lookup(key); entity["priority"] = 5; transaction.Update(entity); transaction.Commit(); } // End sample }
public void AddEntity() { string projectId = _fixture.ProjectId; string namespaceId = _fixture.NamespaceId; // Sample: AddEntity DatastoreClient client = DatastoreClient.Create(); KeyFactory keyFactory = new KeyFactory(projectId, namespaceId, "book"); Entity book1 = new Entity { Key = keyFactory.CreateIncompleteKey(), ["author"] = "Harper Lee", ["title"] = "To Kill a Mockingbird", ["publication_date"] = new DateTime(1960, 7, 11, 0, 0, 0, DateTimeKind.Utc), ["genres"] = new[] { "Southern drama", "Courtroom drama", "Bildungsroman" } }; Entity book2 = new Entity { Key = keyFactory.CreateIncompleteKey(), ["author"] = "Charlotte Brontë", ["title"] = "Jane Eyre", ["publication_date"] = new DateTime(1847, 10, 16, 0, 0, 0, DateTimeKind.Utc), ["genres"] = new[] { "Gothic", "Romance", "Bildungsroman" } }; ByteString transactionId = client.BeginTransaction(projectId).Transaction; using (DatastoreTransaction transaction = DatastoreTransaction.Create(client, projectId, namespaceId, transactionId)) { transaction.Insert(book1, book2); CommitResponse response = transaction.Commit(); IEnumerable <Key> insertedKeys = response.MutationResults.Select(r => r.Key); Console.WriteLine($"Inserted keys: {string.Join(",", insertedKeys)}"); } // End sample }