public void Query_ImplicitlyUsesPartition() { var db = DatastoreDb.Create(_fixture.ProjectId, _fixture.NamespaceId); var keyFactory = db.CreateKeyFactory("parent"); var parent = new Entity { Key = keyFactory.CreateIncompleteKey() }; var parentKey = db.Insert(parent); var child = new Entity { Key = parentKey.WithElement(new PathElement { Kind = "child" }), }; db.Insert(child); using (var transaction = db.BeginTransaction()) { var query = new Query("child") { Filter = Filter.HasAncestor(parentKey) }; var results = transaction.RunQueryLazily(query); Assert.Equal(1, results.Count()); } }
public void LazyStructuredQuery() { string projectId = _fixture.ProjectId; string namespaceId = _fixture.NamespaceId; // Snippet: RunQueryLazily(Query,*,*) DatastoreDb db = DatastoreDb.Create(projectId, namespaceId); Query query = new Query("book") { Filter = Filter.Equal("author", "Jane Austen") }; LazyDatastoreQuery results = db.RunQueryLazily(query); // LazyDatastoreQuery implements IEnumerable<Entity>, but you can // call AsResponses() to see the raw RPC responses, or // GetAllResults() to get all the results into memory, complete with // the end cursor and the reason for the query finishing. foreach (Entity entity in results) { Console.WriteLine(entity); } // End snippet // This will run the query again, admittedly... List <Entity> entities = results.ToList(); Assert.Equal(1, entities.Count); Entity book = entities[0]; Assert.Equal("Jane Austen", (string)book["author"]); Assert.Equal("Pride and Prejudice", (string)book["title"]); }
public void Upsert_ResultKeys() { var db = DatastoreDb.Create(_fixture.ProjectId, _fixture.NamespaceId); var keyFactory = db.CreateKeyFactory("upsert_test"); var insertedKey = db.Insert(new Entity { Key = keyFactory.CreateIncompleteKey(), ["description"] = "original" }); var revisedEntity = new Entity { Key = insertedKey, ["description"] = "changed" }; var newEntity1 = new Entity { Key = keyFactory.CreateKey("x"), ["description"] = "predefined_key" }; var newEntity2 = new Entity { Key = keyFactory.CreateIncompleteKey(), ["description"] = "incomplete_key" }; var keys = db.Upsert(revisedEntity, newEntity1, newEntity2); Assert.Null(keys[0]); // Update Assert.Null(keys[1]); // Insert with predefined key Assert.NotNull(keys[2]); // Insert with incomplete key var fetchedEntity = db.Lookup(keys[2]); Assert.Equal("incomplete_key", fetchedEntity["description"]); }
public void Lookup_NoPartition() { // Deliberately in the empty namespace, which won't be cleaned up automatically - hence the db.Delete call later. var db = DatastoreDb.Create(_fixture.ProjectId); var keyFactory = db.CreateKeyFactory("test"); var entity = new Entity { Key = keyFactory.CreateIncompleteKey(), ["foo"] = "bar" }; var insertedKey = db.Insert(entity); try { var lookupKey = new Key { Path = { insertedKey.Path } }; var result = db.Lookup(lookupKey); Assert.NotNull(result); Assert.Equal("bar", (string)entity["foo"]); } finally { db.Delete(insertedKey); } }
public void Dispose() { try { DatastoreDb datastoreDb = DatastoreDb.Create(ProjectId, Namespace); var deadEntities = datastoreDb.RunQuery(new Query(Kind)); datastoreDb.Delete(deadEntities.Entities); } catch (Exception) { // Do nothing, we delete on a best effort basis. } try { var storage = StorageClient.Create(); var storageObjects = storage.ListObjects(BucketName); foreach (var storageObject in storageObjects) { storage.DeleteObject(BucketName, storageObject.Name); } storage.DeleteBucket(BucketName); } catch (Exception) { // Do nothing, we delete on a best effort basis. } }
public static void Main(string[] args) { // Your Google Cloud Platform project ID string projectId = "ordrcoffee"; // Instantiates a client DatastoreDb db = DatastoreDb.Create(projectId); // The kind for the new entity string kind = "Task"; // The name/ID for the new entity string name = "sampletask1"; KeyFactory keyFactory = db.CreateKeyFactory(kind); // The Cloud Datastore key for the new entity Key key = keyFactory.CreateKey(name); var task = new Entity { Key = key, ["description"] = "Buy milk" }; using (DatastoreTransaction transaction = db.BeginTransaction()) { // Saves the task transaction.Upsert(task); transaction.Commit(); Console.WriteLine($"Saved {task.Key.Path[0].Name}: {(string) task["description"]}"); } }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } string projectId = Configuration["GoogleProjectId"]; if (new string[] { "your-project-id", "", null }.Contains(projectId)) { app.Run(async(context) => { await context.Response.WriteAsync(@"<html> <head><title>Error</title></head> <body><p>Set GoogleProjectId to your project id in appsettings.json. <p>See the README.md in the project directory for more information.</p> </body> </html>"); }); return; } // [START example] DatastoreDb datastore = DatastoreDb.Create(projectId); var visitKeyFactory = datastore.CreateKeyFactory("visit"); // [END example] app.Run(async(HttpContext context) => { // [START example] // Insert a visit into Datastore: Entity newVisit = new Entity(); newVisit.Key = visitKeyFactory.CreateIncompleteKey(); newVisit["time_stamp"] = DateTime.UtcNow; newVisit["ip_address"] = FormatAddress( context.Connection.RemoteIpAddress); await datastore.InsertAsync(newVisit); // Look up the last 10 visits. var results = await datastore.RunQueryAsync(new Query("visit") { Order = { { "time_stamp", PropertyOrder.Types.Direction.Descending } }, Limit = 10 }); await context.Response.WriteAsync(@"<html> <head><title>Visitor Log</title></head> <body>Last 10 visits:<br>"); foreach (Entity visit in results.Entities) { await context.Response.WriteAsync(string.Format("{0} {1}<br>", visit["time_stamp"].TimestampValue, visit["ip_address"].StringValue)); } await context.Response.WriteAsync(@"</body></html>"); // [END example] }); }
TaskList(string projectId) { // [START build_service] // Create an authorized Datastore service using Application Default Credentials. _db = DatastoreDb.Create(projectId); // Create a Key factory to construct keys associated with this project. _keyFactory = _db.CreateKeyFactory("Task"); // [END build_service] }
public T Get(int id) { DatastoreDb db = DatastoreDb.Create("fantasyfootball-204922", "default"); KeyFactory keyFactory = db.CreateKeyFactory("fpl_player"); var result = db.Lookup(keyFactory.CreateKey(id)); return(JsonConvert.DeserializeObject <T>(result["text"].StringValue)); }
public static void AddServicesDependencies(this IServiceCollection services, string projectId) { services.AddSingleton(DatastoreDb.Create(projectId)); services.AddTransient <IBaseRepository, BaseRepository>(); services.AddTransient <IExtensionRequestRepository, ExtensionRequestRepository>(); services.AddTransient <ICloudStorageService, GoogleCloudStorageService>(); services.AddTransient <IMailService, SendGridMailService>(); services.AddTransient <INotificationRepository, NotificationRepository>(); }
public void Init() { db = DatastoreDb.Create(projectId); if (ds != null) { Debug.LogError("Trying to initialize a Datastore instance when one already exists"); } ds = this; }
public void LazyGqlQuery() { string projectId = _fixture.ProjectId; string namespaceId = _fixture.NamespaceId; // Snippet: RunQueryLazily(GqlQuery,*) DatastoreDb db = DatastoreDb.Create(projectId, namespaceId); KeyFactory keyFactory = db.CreateKeyFactory("player"); // Prepare the data: a player with two game child entities Entity player = new Entity { Key = keyFactory.CreateIncompleteKey(), ["name"] = "Sophie" }; Key playerKey = db.Insert(player); Entity game1 = new Entity { Key = playerKey.WithElement(new PathElement { Kind = "game" }), ["score"] = 10, ["timestamp"] = new DateTime(2017, 2, 16, 8, 35, 0, DateTimeKind.Utc) }; Entity game2 = new Entity { Key = playerKey.WithElement(new PathElement { Kind = "game" }), ["score"] = 25, ["timestamp"] = new DateTime(2017, 3, 15, 10, 35, 0, DateTimeKind.Utc) }; db.Insert(game1, game2); // Perform a query within a transaction using (DatastoreTransaction transaction = db.BeginTransaction()) { // Any query executed in a transaction must at least have an ancestor filter. GqlQuery query = new GqlQuery { QueryString = "SELECT * FROM game WHERE __key__ HAS ANCESTOR @player", NamedBindings = { { "player", playerKey } } }; LazyDatastoreQuery results = db.RunQueryLazily(query); // LazyDatastoreQuery implements IEnumerable<Entity>, but you can // call AsResponses() to see the raw RPC responses, or // GetAllResults() to get all the results into memory, complete with // the end cursor and the reason for the query finishing. foreach (Entity entity in results) { Console.WriteLine(entity); } } // End snippet }
public void EagerStructuredQuery() { string projectId = _fixture.ProjectId; string namespaceId = _fixture.NamespaceId; // Snippet: RunQuery(Query,*) DatastoreDb db = DatastoreDb.Create(projectId, namespaceId); KeyFactory keyFactory = db.CreateKeyFactory("player"); // Prepare the data: a player with two game child entities Entity player = new Entity { Key = keyFactory.CreateIncompleteKey(), ["name"] = "Sophie" }; Key playerKey = db.Insert(player); Entity game1 = new Entity { Key = playerKey.WithElement(new PathElement { Kind = "game" }), ["score"] = 10, ["timestamp"] = new DateTime(2017, 2, 16, 8, 35, 0, DateTimeKind.Utc) }; Entity game2 = new Entity { Key = playerKey.WithElement(new PathElement { Kind = "game" }), ["score"] = 25, ["timestamp"] = new DateTime(2017, 3, 15, 10, 35, 0, DateTimeKind.Utc) }; db.Insert(game1, game2); // Perform a query within a transaction using (DatastoreTransaction transaction = db.BeginTransaction()) { // Any query executed in a transaction must at least have an ancestor filter. Query query = new Query("game") { Filter = Filter.HasAncestor(playerKey), Limit = 10 }; DatastoreQueryResults results = db.RunQuery(query); // RunQuery fetches all the results into memory in a single call. // Constrast this with RunQueryLazily, which merely prepares an enumerable // query. Always specify a limit when you use RunQuery, to avoid running // out of memory. foreach (Entity entity in results.Entities) { Console.WriteLine(entity); } } // End snippet }
public Datastore() { string datastoreMarketOddsKey = ConfigurationManager.AppSettings["datastoreMarketOddsKey"]; _marketOddsKeyFactory = DatastoreDb.CreateKeyFactory(datastoreMarketOddsKey); string datastoreFinishedtOddsKey = ConfigurationManager.AppSettings["datastoreFinishedOddsKey"]; _finishedOddsKeyFactory = DatastoreDb.CreateKeyFactory(datastoreFinishedtOddsKey); }
public DatastoreTest() { _projectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID"); _db = DatastoreDb.Create(_projectId, TestUtil.RandomName()); _keyFactory = _db.CreateKeyFactory("Task"); _sampleTask = new Entity() { Key = _keyFactory.CreateKey("sampleTask"), }; }
public DatastoreUserStore(DatastoreDb datastore, ILogger <DatastoreUserStore <U> > logger) { _datastore = datastore ?? throw new ArgumentNullException(nameof(datastore)); _userKeyFactory = new KeyFactory(_datastore.ProjectId, _datastore.NamespaceId, USER_KIND); _nnindexKeyFactory = new KeyFactory(_datastore.ProjectId, _datastore.NamespaceId, NORMALIZED_NAME_INDEX_KIND); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); }
private void upsert(DatastoreDb db, Entity entity) { GrpcEnvironment.SetLogger(new ConsoleLogger()); using (DatastoreTransaction transaction = db.BeginTransaction()) { transaction.Upsert(entity); transaction.Commit(); } }
private void SetKey(string id, string kind, DatastoreDb datastoreDb, Entity entity) { if (long.TryParse(id, out var idLong)) { entity.Key = datastoreDb.CreateKeyFactory(kind).CreateKey(idLong); return; } entity.Key = datastoreDb.CreateKeyFactory(kind).CreateKey(id); }
public Entity FromPoco(DatastoreDb datastoreDb, string kind, object poco) { var entity = new Entity(); SetKey(reflection.GetIdValue(poco), kind, datastoreDb, entity); AddProperties(entity, poco, new List <string>()); return(entity); }
public DatastoreCounterSingleton(DatastoreDb datastore, IOptions <DatastoreCounterOptions> options, ILogger <DatastoreCounter> logger, IManagedTracer tracer) { _datastore = datastore; _options = options; _logger = logger; _tracer = tracer; _counter = new DatastoreCounter(datastore, options, logger, tracer); _counterBirthday = DateTime.UtcNow; }
public void OnAddingEntity_WhenPassingKey_ValidateNamedKey() { _db = DatastoreDb.Create(_projectId, "ApiKeyDataStoreTest"); _kind = "ApiKey"; _keyFactory = _db.CreateKeyFactory(_kind); Key key = _keyFactory.CreateKey(_apiKey.Id); Assert.IsTrue(IsValidKey(key)); Assert.IsNotNull(key.Path.FirstOrDefault(x => x.Kind == _kind).Kind); }
public ActionResult Delete(long id) { var dbStore = DatastoreDb.Create("programming-for-the-cloud"); //getting an instance of the database var objectManager = dbStore.CreateKeyFactory("Username"); //creating or getting an instance of the user table Entity userToDelete = dbStore.Lookup(objectManager.CreateKey(id)); //get existing user dbStore.Delete(userToDelete); return(RedirectToAction("Index")); }
public void LookupEntity() { string projectId = _fixture.ProjectId; string namespaceId = _fixture.NamespaceId; Key key = _fixture.LearnDatastoreKey; // Sample: LookupEntity DatastoreDb db = DatastoreDb.Create(projectId, namespaceId); Entity entity = db.Lookup(key); // End sample }
// [END retry] public DatastoreTest() { _projectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID"); _db = DatastoreDb.Create(_projectId, "ghijklmnop"); _keyFactory = _db.CreateKeyFactory("Task"); _sampleTask = new Entity() { Key = _keyFactory.CreateKey("sampleTask"), }; ClearTasks(); }
public void AllocateId() { string projectId = _fixture.ProjectId; string namespaceId = _fixture.NamespaceId; // Snippet: AllocateId DatastoreDb db = DatastoreDb.Create(projectId, namespaceId); KeyFactory keyFactory = db.CreateKeyFactory("message"); Key key = db.AllocateId(keyFactory.CreateIncompleteKey()); // End snippet }
/// <summary> /// Instantiate with custom stack or through Depency Injection /// </summary> /// <param name="datastoreDb">The low-level datastore connection. Needed to generate keys.</param> /// <param name="reflector">Answers questions about a POCO</param> /// <param name="entityFactory">Creates entities from POCOs</param> /// <param name="pocoFactory">Creates POCOs from entities</param> public DatastoreOrm( DatastoreDb datastoreDb, DatastoreReflector reflector, IEntityFactory entityFactory, IPocoFactory pocoFactory ) { this.datastoreDb = datastoreDb; this.reflector = reflector; this.entityFactory = entityFactory; this.pocoFactory = pocoFactory; }
public EmulatedDatastoreDbProvider(IConfigurationService configurationService, IDataEntityObjectFactory dataEntityObjectFactory) { var host = configurationService.Get("DATASTOREDB_HOST"); var project = configurationService.Get("DATASTOREDB_PROJECT"); var port = Convert.ToInt32(configurationService.Get("DATASTOREDB_PORT")); var channel = new Channel(host, port, ChannelCredentials.Insecure); var client = DatastoreClient.Create(channel); _db = DatastoreDb.Create(project, "", client); _dataEntityObjectFactory = dataEntityObjectFactory; }
public async Task DeleteAsync() { var db = DatastoreDb.Create(_fixture.ProjectId, _fixture.NamespaceId); var keyFactory = db.CreateKeyFactory("update_test"); var insertedKey = await db.InsertAsync(new Entity { Key = keyFactory.CreateIncompleteKey(), ["description"] = "original" }); Assert.NotNull(await db.LookupAsync(insertedKey)); await db.DeleteAsync(insertedKey); Assert.Null(await db.LookupAsync(insertedKey)); }
public GcpDatastoreMqttIntentStore(IOptions <GcpDatastoreConfig> config, DataStoreKind kind, IMapper mapper) { _kind = kind; _mapper = mapper; _db = DatastoreDb.Create(config.Value.ProjectId, client: string.IsNullOrWhiteSpace(config.Value.KeyJson) ? null : new DatastoreClientBuilder { JsonCredentials = config.Value.KeyJson }.Build()); _keyFactory = _db.CreateKeyFactory(kind.MqttIntentKind); }
public void RunQuery_NoResults() { var db = DatastoreDb.Create(_fixture.ProjectId, _fixture.NamespaceId); var query = db.RunQueryLazily(new Query("absent")); // Each of the checks below will run the query again, as the query is only lazily // evaluated. Assert.Equal(0, query.Count()); var singleResponse = query.AsResponses().Single(); Assert.Equal(MoreResultsType.NoMoreResults, singleResponse.Batch.MoreResults); }
/// <summary> /// Create a new datastore-backed bookstore. /// </summary> /// <param name="projectId">Your Google Cloud project id</param> public DatastoreBookStore(string projectId) { _projectId = projectId; _db = DatastoreDb.Create(_projectId); }