Skip to content

Commit

Permalink
feat: Multi-db support in Datastore.
Browse files Browse the repository at this point in the history
  • Loading branch information
anuragsrivstv committed Oct 20, 2023
1 parent 280c31d commit 168660f
Show file tree
Hide file tree
Showing 14 changed files with 196 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@ public void Lookup_NamespaceOnly()
Assert.Equal("bar", (string)entity["foo"]);
}

[Fact]
public void Lookup_DatabaseIdOnly()
{
var db = _fixture.CreateDatastoreDb();
var keyFactory = db.CreateKeyFactory("test");
var entity = new Entity
{
Key = keyFactory.CreateIncompleteKey(),
["foo"] = "bar"
};
var insertedKey = db.Insert(entity);

var lookupKey = new Key { PartitionId = new PartitionId { DatabaseId = _fixture.DatabaseId }, Path = { insertedKey.Path } };
var result = db.Lookup(lookupKey);
Assert.NotNull(result);
Assert.Equal("bar", (string) entity["foo"]);
}

[Fact]
public async Task Lookup_NoPartition()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 Google Inc. All Rights Reserved.
// Copyright 2016 Google Inc. 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.
Expand Down Expand Up @@ -32,11 +32,13 @@ public sealed class DatastoreFixture : CloudProjectFixtureBase, ICollectionFixtu
private static readonly TimeSpan RetryDelay = TimeSpan.FromSeconds(3);

public string NamespaceId { get; }
public PartitionId PartitionId => new PartitionId { ProjectId = ProjectId, NamespaceId = NamespaceId };
public string DatabaseId { get; }
public PartitionId PartitionId => new PartitionId { ProjectId = ProjectId, NamespaceId = NamespaceId, DatabaseId = DatabaseId };

public DatastoreFixture()
{
NamespaceId = IdGenerator.FromDateTime(prefix: "test-");
DatabaseId = IdGenerator.FromDateTime(prefix: "test-db-");
}

public override void Dispose()
Expand Down Expand Up @@ -88,7 +90,7 @@ public async Task RetryQueryAsync(Func<Task> check)
}
}

public DatastoreDb CreateDatastoreDb(string namespaceId = null)
public DatastoreDb CreateDatastoreDb(string namespaceId = null, string databaseId = null)
{
#if NETCOREAPP3_1
// On .NET Core 3.1 (but not .NET 6) Grpc.Net.Client needs an additional switch
Expand All @@ -99,10 +101,12 @@ public DatastoreDb CreateDatastoreDb(string namespaceId = null)
#endif

string effectiveNamespace = namespaceId ?? NamespaceId;
string effectiveDatabase = databaseId ?? DatabaseId;
var builder = new DatastoreDbBuilder
{
ProjectId = ProjectId,
NamespaceId = effectiveNamespace,
DatabaseId = effectiveDatabase,
EmulatorDetection = EmulatorDetection.EmulatorOrProduction
};
return builder.Build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ public void Lookup()
{
string projectId = _fixture.ProjectId;
string namespaceId = _fixture.NamespaceId;
string databaseId = _fixture.DatabaseId;

// Snippet: Lookup(*,*,*,*)
KeyFactory keyFactory = new KeyFactory(projectId, namespaceId, "book");
KeyFactory keyFactory = new KeyFactory(projectId, namespaceId, databaseId, "book");
Key key1 = keyFactory.CreateKey("pride_and_prejudice");
Key key2 = keyFactory.CreateKey("not_present");

Expand All @@ -56,16 +57,16 @@ public void Lookup()
// End snippet

Entity entity = response.Found[0].Entity;
Assert.Equal("Jane Austen", (string)entity["author"]);
Assert.Equal("Pride and Prejudice", (string)entity["title"]);
Assert.Equal("Jane Austen", (string) entity["author"]);
Assert.Equal("Pride and Prejudice", (string) entity["title"]);
}

[Fact]
public void RunQuery()
{
string projectId = _fixture.ProjectId;
PartitionId partitionId = _fixture.PartitionId;

// Snippet: RunQuery(RunQueryRequest,*)
DatastoreClient client = DatastoreClient.Create();

Expand Down Expand Up @@ -100,19 +101,20 @@ public void RunQuery()

Assert.Equal(1, response.Batch.EntityResults.Count);
Entity entity = response.Batch.EntityResults[0].Entity;
Assert.Equal("Jane Austen", (string)entity["author"]);
Assert.Equal("Pride and Prejudice", (string)entity["title"]);
Assert.Equal("Jane Austen", (string) entity["author"]);
Assert.Equal("Pride and Prejudice", (string) entity["title"]);
}

[Fact]
public void AddEntity()
{
string projectId = _fixture.ProjectId;
string namespaceId = _fixture.NamespaceId;
string databaseId = _fixture.DatabaseId;

// Sample: AddEntity
DatastoreClient client = DatastoreClient.Create();
KeyFactory keyFactory = new KeyFactory(projectId, namespaceId, "book");
KeyFactory keyFactory = new KeyFactory(projectId, namespaceId, databaseId, "book");
Entity book1 = new Entity
{
Key = keyFactory.CreateIncompleteKey(),
Expand All @@ -131,7 +133,7 @@ public void AddEntity()
};

ByteString transactionId = client.BeginTransaction(projectId).Transaction;
using (DatastoreTransaction transaction = DatastoreTransaction.Create(client, projectId, namespaceId, transactionId))
using (DatastoreTransaction transaction = DatastoreTransaction.Create(client, projectId, namespaceId, databaseId, transactionId))
{
transaction.Insert(book1, book2);
CommitResponse response = transaction.Commit();
Expand All @@ -146,10 +148,11 @@ public void AllocateIds()
{
string projectId = _fixture.ProjectId;
string namespaceId = _fixture.NamespaceId;
string databaseId = _fixture.DatabaseId;

// Snippet: AllocateIds(*,*,*)
DatastoreClient client = DatastoreClient.Create();
KeyFactory keyFactory = new KeyFactory(projectId, namespaceId, "message");
KeyFactory keyFactory = new KeyFactory(projectId, namespaceId, databaseId, "message");
AllocateIdsResponse response = client.AllocateIds(projectId,
new[] { keyFactory.CreateIncompleteKey(), keyFactory.CreateIncompleteKey() }
);
Expand Down Expand Up @@ -187,7 +190,8 @@ public void KindQuery()
{
string projectId = _fixture.ProjectId;
string namespaceId = _fixture.NamespaceId;
PartitionId partitionId = new PartitionId(projectId, namespaceId);
string databaseId = _fixture.DatabaseId;
PartitionId partitionId = new PartitionId(projectId, namespaceId, databaseId);

// Sample: KindQuery
DatastoreClient client = DatastoreClient.Create();
Expand All @@ -210,7 +214,8 @@ public void PropertyQuery()
{
string projectId = _fixture.ProjectId;
string namespaceId = _fixture.NamespaceId;
PartitionId partitionId = new PartitionId(projectId, namespaceId);
string databaseId = _fixture.DatabaseId;
PartitionId partitionId = new PartitionId(projectId, namespaceId, databaseId);

// Sample: PropertyQuery
DatastoreClient client = DatastoreClient.Create();
Expand Down Expand Up @@ -239,10 +244,11 @@ public void Overview()
{
string projectId = _fixture.ProjectId;
string namespaceId = _fixture.NamespaceId;
string databaseId = _fixture.DatabaseId;
// Sample: Overview
DatastoreClient client = DatastoreClient.Create();

KeyFactory keyFactory = new KeyFactory(projectId, namespaceId, "message");
KeyFactory keyFactory = new KeyFactory(projectId, namespaceId, databaseId, "message");
Entity entity = new Entity
{
Key = keyFactory.CreateIncompleteKey(),
Expand All @@ -251,7 +257,7 @@ public void Overview()
["tags"] = new[] { "tag1", "tag2" }
};
ByteString transactionId = client.BeginTransaction(projectId).Transaction;
using (DatastoreTransaction transaction = DatastoreTransaction.Create(client, projectId, namespaceId, transactionId))
using (DatastoreTransaction transaction = DatastoreTransaction.Create(client, projectId, namespaceId, databaseId, transactionId))
{
transaction.Insert(entity);
CommitResponse commitResponse = transaction.Commit();
Expand All @@ -268,9 +274,10 @@ public void CreateEntity()
{
string projectId = _fixture.ProjectId;
string namespaceId = _fixture.NamespaceId;
string databaseId = _fixture.DatabaseId;

// Sample: CreateEntity
KeyFactory keyFactory = new KeyFactory(projectId, namespaceId, "Task");
KeyFactory keyFactory = new KeyFactory(projectId, namespaceId, databaseId, "Task");
Entity entity = new Entity
{
Key = keyFactory.CreateIncompleteKey(),
Expand All @@ -288,9 +295,10 @@ public void InsertEntity()
{
string projectId = _fixture.ProjectId;
string namespaceId = _fixture.NamespaceId;
string databaseId = _fixture.DatabaseId;

// Sample: InsertEntity
KeyFactory keyFactory = new KeyFactory(projectId, namespaceId, "Task");
KeyFactory keyFactory = new KeyFactory(projectId, namespaceId, databaseId, "Task");
Entity entity = new Entity
{
Key = keyFactory.CreateIncompleteKey(),
Expand Down Expand Up @@ -328,12 +336,13 @@ public void UpdateEntity()
{
string projectId = _fixture.ProjectId;
string namespaceId = _fixture.NamespaceId;
string databaseId = _fixture.DatabaseId;
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))
using (DatastoreTransaction transaction = DatastoreTransaction.Create(client, projectId, namespaceId, databaseId, transactionId))
{
Entity entity = transaction.Lookup(key);
entity["priority"] = 5;
Expand All @@ -348,9 +357,10 @@ public void DeleteEntity()
{
string projectId = _fixture.ProjectId;
string namespaceId = _fixture.NamespaceId;
string databaseId = _fixture.DatabaseId;

// Copied from InsertEntity; we want to create a new one to delete.
KeyFactory keyFactory = new KeyFactory(projectId, namespaceId, "Task");
KeyFactory keyFactory = new KeyFactory(projectId, namespaceId, databaseId, "Task");
Entity entity = new Entity
{
Key = keyFactory.CreateIncompleteKey(),
Expand Down Expand Up @@ -379,9 +389,10 @@ public void AncestorPaths()
{
string projectId = _fixture.ProjectId;
string namespaceId = _fixture.NamespaceId;
string databaseId = _fixture.DatabaseId;

// Sample: AncestorPaths
KeyFactory keyFactory = new KeyFactory(projectId, namespaceId, "User");
KeyFactory keyFactory = new KeyFactory(projectId, namespaceId, databaseId, "User");
Key taskKey = keyFactory.CreateKey("alice").WithElement("Task", "sampleTask");

Key multiLevelKey = keyFactory
Expand Down Expand Up @@ -433,7 +444,7 @@ public void CompositeFilterQuery()
foreach (EntityResult result in response.Batch.EntityResults)
{
Entity entity = result.Entity;
Console.WriteLine((string)entity["description"]);
Console.WriteLine((string) entity["description"]);
}
// TODO: Results beyond this batch?
// End sample
Expand All @@ -444,9 +455,10 @@ public void KeyQuery()
{
string projectId = _fixture.ProjectId;
string namespaceId = _fixture.NamespaceId;
string databaseId = _fixture.DatabaseId;

// Sample: KeyQuery
KeyFactory keyFactory = new KeyFactory(projectId, namespaceId, "Task");
KeyFactory keyFactory = new KeyFactory(projectId, namespaceId, databaseId, "Task");
Query query = new Query("Task")
{
Filter = Filter.GreaterThan(DatastoreConstants.KeyProperty, keyFactory.CreateKey("someTask"))
Expand All @@ -459,9 +471,10 @@ public void AncestorQuery()
{
string projectId = _fixture.ProjectId;
string namespaceId = _fixture.NamespaceId;
string databaseId = _fixture.DatabaseId;

// Sample: AncestorQuery
KeyFactory keyFactory = new KeyFactory(projectId, namespaceId, "Task");
KeyFactory keyFactory = new KeyFactory(projectId, namespaceId, databaseId, "Task");
Query query = new Query("Task")
{
Filter = Filter.HasAncestor(keyFactory.CreateKey("someTask"))
Expand All @@ -474,9 +487,10 @@ public void KindlessQuery()
{
string projectId = _fixture.ProjectId;
string namespaceId = _fixture.NamespaceId;
string databaseId = _fixture.DatabaseId;

// Sample: KindlessQuery
KeyFactory keyFactory = new KeyFactory(projectId, namespaceId, "Task");
KeyFactory keyFactory = new KeyFactory(projectId, namespaceId, databaseId, "Task");
Key lastSeenKey = keyFactory.CreateKey(100L);
Query query = new Query
{
Expand Down Expand Up @@ -517,7 +531,7 @@ public void ProjectionQuery()
foreach (EntityResult result in response.Batch.EntityResults)
{
Entity entity = result.Entity;
Console.WriteLine($"{(int)entity["priority"]}: {(double?)entity["percentage_complete"]}");
Console.WriteLine($"{(int) entity["priority"]}: {(double?) entity["percentage_complete"]}");
}
// End sample
}
Expand Down Expand Up @@ -620,22 +634,23 @@ public void TransactionReadAndWrite()
{
string projectId = _fixture.ProjectId;
string namespaceId = _fixture.NamespaceId;
string databaseId = _fixture.DatabaseId;
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))
using (DatastoreTransaction transaction = DatastoreTransaction.Create(client, projectId, namespaceId, databaseId, 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;
from["balance"] = (long) from["balance"] - amount;
to["balance"] = (long) to["balance"] - amount;
transaction.Update(from);
transaction.Update(to);
transaction.Commit();
Expand All @@ -648,8 +663,9 @@ private Key CreateAccount(string name, long balance)
{
string projectId = _fixture.ProjectId;
string namespaceId = _fixture.NamespaceId;
string databaseId = _fixture.DatabaseId;
DatastoreClient client = DatastoreClient.Create();
KeyFactory factory = new KeyFactory(projectId, namespaceId, "Account");
KeyFactory factory = new KeyFactory(projectId, namespaceId, databaseId, "Account");
Entity entity = new Entity
{
Key = factory.CreateIncompleteKey(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public void Lookup()
{
string projectId = _fixture.ProjectId;
string namespaceId = _fixture.NamespaceId;
string databaseId = _fixture.DatabaseId;

// Snippet: Lookup(*)
DatastoreDb db = DatastoreDb.Create(projectId, namespaceId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 Google Inc. All Rights Reserved.
// Copyright 2016 Google Inc. 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.
Expand Down Expand Up @@ -26,7 +26,8 @@ namespace Google.Cloud.Datastore.V1.Snippets
public sealed class DatastoreSnippetFixture : CloudProjectFixtureBase, ICollectionFixture<DatastoreSnippetFixture>
{
public string NamespaceId { get; }
public PartitionId PartitionId => new PartitionId { ProjectId = ProjectId, NamespaceId = NamespaceId };
public string DatabaseId { get; }
public PartitionId PartitionId => new PartitionId { ProjectId = ProjectId, NamespaceId = NamespaceId, DatabaseId = DatabaseId };
public string BookKind = "book";
public string TaskKind = "Task";
private Key _prideAndPrejudiceKey;
Expand All @@ -37,14 +38,15 @@ public sealed class DatastoreSnippetFixture : CloudProjectFixtureBase, ICollecti
public DatastoreSnippetFixture()
{
NamespaceId = IdGenerator.FromDateTime(prefix: "test-");
DatabaseId = IdGenerator.FromDateTime(prefix: "test-db-");
AddSampleBooks();
AddSampleTasks();
}

private void AddSampleBooks()
{
var client = DatastoreClient.Create();
var keyFactory = new KeyFactory(ProjectId, NamespaceId, BookKind);
var keyFactory = new KeyFactory(ProjectId, NamespaceId, DatabaseId, BookKind);
var entity = new Entity
{
Key = keyFactory.CreateKey("pride_and_prejudice"),
Expand All @@ -59,7 +61,7 @@ private void AddSampleBooks()
private void AddSampleTasks()
{
var client = DatastoreClient.Create();
var keyFactory = new KeyFactory(ProjectId, NamespaceId, TaskKind);
var keyFactory = new KeyFactory(ProjectId, NamespaceId, DatabaseId, TaskKind);
var entity = new Entity
{
Key = keyFactory.CreateIncompleteKey(),
Expand Down
Loading

0 comments on commit 168660f

Please sign in to comment.