private static void Main(string[] args) { // Initialise license and stores directory location SamplesConfiguration.Register(); //create a unique store name var storeName = "tweetbox_" + Guid.NewGuid().ToString(); //connection string to the BrightstarDB service string connectionString = string.Format(@"Type=embedded;storesDirectory={0};StoreName={1};", SamplesConfiguration.StoresDirectory, storeName); const int numUsers = 100; //Load the database with users, hashtags and tweets var context = new TweetBoxContext(connectionString); var timetaken = LoadTweetBoxContent(context, numUsers); const int numTweets = numUsers * 200; Console.WriteLine("Created, persisted and indexed {0} users, {1} tweets, {2} hashtags in {3} seconds.", numUsers, numTweets, _allHashTags.Count, (timetaken / 1000)); // Shutdown Brightstar processing threads. BrightstarService.Shutdown(); Console.WriteLine(); Console.WriteLine("Finished. Press the Return key to exit."); Console.ReadLine(); }
private static void Main(string[] args) { // Initialise license and stores directory location SamplesConfiguration.Register(); //create a unique store name var storeName = "changetracking_" + Guid.NewGuid(); //connection string to the BrightstarDB service var connectionString = String.Format(@"Type=embedded;storesDirectory={0};storeName={1}", SamplesConfiguration.StoresDirectory, storeName); // Create and modify an article to show that the tracking timestamps get updated var article = CreateArticle(connectionString); Console.WriteLine("Created new article: {0}", article); System.Threading.Thread.Sleep(1000); article = ModifyArticle(connectionString, article.Id); Console.WriteLine("Modified article: {0}", article); // Shutdown Brightstar processing threads. BrightstarService.Shutdown(); Console.WriteLine(); Console.WriteLine("Finished. Press the Return key to exit."); Console.ReadLine(); }
private static void Main() { // Initialise license and stores directory location SamplesConfiguration.Register(); //create a unique store name var storeName = "foaf_" + Guid.NewGuid(); //connection string to the BrightstarDB service var connectionString = String.Format(@"Type=embedded;storesDirectory={0};", SamplesConfiguration.StoresDirectory); //Load some RDF data into the store LoadRdfData(connectionString, storeName); //Connect to the store via the entity framework and loop through the entities PrintOutUsingEntityFramework(connectionString, storeName); // Shutdown Brightstar processing threads. BrightstarService.Shutdown(); Console.WriteLine(); Console.WriteLine("Finished. Press the Return key to exit."); Console.ReadLine(); }
private static void Main() { // Register the license we are using for BrightstarDB SamplesConfiguration.Register(); // Create a client - the connection string used is configured in the App.config file. var client = BrightstarService.GetClient("type=embedded;storesDirectory=" + SamplesConfiguration.StoresDirectory); // Create a test store and populate it with data using several transactions var storeName = CreateTestStore(client); Console.WriteLine("Initial commit points:"); List <ICommitPointInfo> commitPoints = ListCommitPoints(client, storeName); // Revert to the last-but-one commit point Console.WriteLine("Reverting store to commit point #1"); client.RevertToCommitPoint(storeName, commitPoints[1]); Console.WriteLine("Commit points after revert:"); ListCommitPoints(client, storeName); // Revert back to the commit point before the first revert Console.WriteLine("Re-reverting store"); client.RevertToCommitPoint(storeName, commitPoints[0]); Console.WriteLine("Commit points after re-revert:"); ListCommitPoints(client, storeName); Console.WriteLine("Coalescing store to single commit point"); var consolidateJob = client.ConsolidateStore(storeName); while (!(consolidateJob.JobCompletedOk || consolidateJob.JobCompletedWithErrors)) { System.Threading.Thread.Sleep(500); consolidateJob = client.GetJobInfo(storeName, consolidateJob.JobId); } if (consolidateJob.JobCompletedOk) { Console.WriteLine("Consolidate job completed. Listing commit points after consolidate:"); ListCommitPoints(client, storeName); } else { Console.WriteLine("Consolidate job failed."); } // Shutdown Brightstar processing threads BrightstarService.Shutdown(); Console.WriteLine(); Console.WriteLine("Finished. Press the Return key to exit."); Console.ReadLine(); }
private static void Main(string[] args) { // Initialise license and stores directory location SamplesConfiguration.Register(); //create a unique store name var storeName = "Films_" + Guid.NewGuid().ToString(); //connection string to the BrightstarDB service string connectionString = string.Format(@"Type=embedded;storesDirectory={0};StoreName={1};", SamplesConfiguration.StoresDirectory, storeName); // if the store does not exist it will be automatically // created when a context is created var ctx = new MyEntityContext(connectionString); // create some films var bladeRunner = ctx.Films.Create(); bladeRunner.Name = "BladeRunner"; var starWars = ctx.Films.Create(); starWars.Name = "Star Wars"; // create some actors and connect them to films var ford = ctx.Actors.Create(); ford.Name = "Harrison Ford"; ford.DateOfBirth = new DateTime(1942, 7, 13); ford.Films.Add(starWars); ford.Films.Add(bladeRunner); var hamill = ctx.Actors.Create(); hamill.Name = "Mark Hamill"; hamill.DateOfBirth = new DateTime(1951, 9, 25); hamill.Films.Add(starWars); // save the data ctx.SaveChanges(); // open a new context ctx = new MyEntityContext(connectionString); // find an actor via LINQ ford = ctx.Actors.FirstOrDefault(a => a.Name.Equals("Harrison Ford")); // get his films var films = ford.Films; // get star wars var sw = films.FirstOrDefault(f => f.Name.Equals("Star Wars")); // list actors in star wars foreach (var actor in sw.Actors) { var actorName = actor.Name; Console.WriteLine(actorName); } foreach (var actor in ctx.Actors.Where(a => a.Name.Equals("Mark Hamill"))) { Console.WriteLine(actor.Name + " born " + actor.DateOfBirth); } Console.WriteLine(); Console.WriteLine("Making changes to the store with optismistic locking enabled"); ctx = new MyEntityContext(connectionString, true); var newFilm = ctx.Films.Create(); ctx.SaveChanges(); var newFilmId = newFilm.Id; //use optimistic locking when creating a new context var ctx1 = new MyEntityContext(connectionString, true); var ctx2 = new MyEntityContext(connectionString, true); //create a film in the first context var film1 = ctx1.Films.FirstOrDefault(f => f.Id.Equals(newFilmId)); Console.WriteLine("First context has film with ID '{0}'", film1.Id); //create a film in the second context var film2 = ctx2.Films.FirstOrDefault(f => f.Id.Equals(newFilmId)); Console.WriteLine("Second context has film with ID '{0}'", film2.Id); //attempt to change the data from both contexts film1.Name = "Raiders of the Lost Ark"; film2.Name = "American Graffiti"; //save the data to the store try { Console.WriteLine("Attempting update from one context"); ctx1.SaveChanges(); Console.WriteLine("Successfully updated the film to '{0}' in the store", film1.Name); Console.WriteLine("Attempting a conflicting update from a different context - this should fail."); ctx2.SaveChanges(); } catch (TransactionPreconditionsFailedException) { Console.WriteLine( "Optimistic locking enabled: the conflicting update has not been processed as the underlying data has been modified."); } // Shutdown Brightstar processing threads. BrightstarService.Shutdown(); Console.WriteLine(); Console.WriteLine("Finished. Press the Return key to exit."); Console.ReadLine(); }
static void Main(string[] args) { SamplesConfiguration.Register(); // Create a new service context using a connection string. var context = BrightstarService.GetDataObjectContext(@"Type=embedded;storesDirectory=" + SamplesConfiguration.StoresDirectory); // create a new store string storeName = "DataObjectLayerSample_" + Guid.NewGuid(); var store = context.CreateStore(storeName); //In order to use simpler identities, we set up some namespace mappings to pass through when we open a store _namespaceMappings = new Dictionary <string, string>() { { "people", "http://example.org/people/" }, { "skills", "http://example.org/skills/" }, { "schema", "http://example.org/schema/" } }; //Open a Data Object Store passing through the namespace mappings store = context.OpenStore(storeName, _namespaceMappings); var skillType = store.MakeDataObject("schema:skill"); //use namespace mappings to create a number of skills var csharp = store.MakeDataObject("skills:csharp"); csharp.SetType(skillType); var html = store.MakeDataObject("skills:html"); html.SetType(skillType); var css = store.MakeDataObject("skills:css"); css.SetType(skillType); var javascript = store.MakeDataObject("skills:javascript"); javascript.SetType(skillType); //create a data objects for people var personType = store.MakeDataObject("schema:person"); var fred = store.MakeDataObject("people:fred"); fred.SetType(personType); var william = store.MakeDataObject("people:william"); william.SetType(personType); //create objects for property types for name and category var fullname = store.MakeDataObject("schema:person/fullName"); var skill = store.MakeDataObject("schema:person/skill"); //Set the name property fred.SetProperty(fullname, "Fred Evans"); //Add the skills fred.AddProperty(skill, csharp); fred.AddProperty(skill, html); fred.AddProperty(skill, css); //Set the name property william.SetProperty(fullname, "William Turner"); //Add the skills william.AddProperty(skill, html); william.AddProperty(skill, css); william.AddProperty(skill, javascript); //save the changes to the store store.SaveChanges(); Console.WriteLine("Added 2 data objects to store."); Console.WriteLine("Identity: {0}", fred.Identity); Console.WriteLine("Name: {0}", fred.GetPropertyValue(fullname)); Console.WriteLine("Identity: {0}", william.Identity); Console.WriteLine("Name: {0}", william.GetPropertyValue(fullname)); Console.WriteLine(); var employeeNumber = store.MakeDataObject("schema:person/employeeNumber"); var dateOfBirth = store.MakeDataObject("schema:person/dateOfBirth"); var salary = store.MakeDataObject("schema:person/salary"); //adding literal properties to a data object fred = store.GetDataObject("people:fred"); //the datatypes are auto detected Console.WriteLine("Adding literal data to the person data object"); fred.SetProperty(employeeNumber, 123); fred.SetProperty(dateOfBirth, DateTime.Now.AddYears(-30)); fred.SetProperty(salary, 18000.00); store.SaveChanges(); store = context.OpenStore(storeName, _namespaceMappings); fred = store.GetDataObject("people:fred"); Console.WriteLine("Name: {0}", fred.GetPropertyValue(fullname)); Console.WriteLine("Employee Number: {0}", fred.GetPropertyValue(employeeNumber)); Console.WriteLine("Date of Birth: {0}", fred.GetPropertyValue(dateOfBirth)); Console.WriteLine("Salary: {0:C}", fred.GetPropertyValue(salary)); Console.WriteLine(); // SPARQL query for all the categories connected to BrightstarDB const string getPersonSkillsQuery = "SELECT ?skill WHERE { <http://example.org/people/fred> <http://example.org/schema/person/skill> ?skill }"; Console.WriteLine("Executing SPARQL query to return Fred's skills:"); Console.WriteLine(getPersonSkillsQuery); var sparqlResult = store.ExecuteSparql(getPersonSkillsQuery); var result = sparqlResult.ResultSet; foreach (var sparqlResultRow in result) { var val = sparqlResultRow["skill"]; Console.WriteLine("Skill is " + val); } Console.WriteLine(); //connect to the store again to make sure cache is flushed store = context.OpenStore(storeName, _namespaceMappings); // SPARQL query to return URIs of all objects of the 'category' type const string skillsQuery = "SELECT ?skill WHERE {?skill a <http://example.org/schema/skill>}"; //Use the BindDataObjectsWithSparql Method to pull a collection of data objects from the store that match the SPARQL query supplied var allSkills = store.BindDataObjectsWithSparql(skillsQuery).ToList(); Console.WriteLine("Binding data objects with query for all skills in the store:"); Console.WriteLine(skillsQuery); foreach (var s in allSkills) { Console.WriteLine("Skill is " + s.Identity); } //Delete all categories found Console.WriteLine(); Console.WriteLine("Deleting all skill data objects"); foreach (var s in allSkills) { s.Delete(); } store.SaveChanges(); //connect to the store again to make sure cache is flushed store = context.OpenStore(storeName, _namespaceMappings); allSkills = store.BindDataObjectsWithSparql(skillsQuery).ToList(); var skillCount = allSkills.Count; Console.WriteLine("Skill count after delete is " + skillCount); // Shutdown Brightstar processing threads. BrightstarService.Shutdown(); Console.WriteLine(); Console.WriteLine("Finished. Press Return key to exit."); Console.ReadLine(); }
private static void Main(string[] args) { SamplesConfiguration.Register(); // Create a new service client using a connection string. var connectionString = String.Format(@"Type=embedded;storesDirectory={0};", SamplesConfiguration.StoresDirectory); var client = BrightstarService.GetClient(connectionString); // create a new store string storeName = "RdfGettingStarted_" + Guid.NewGuid(); client.CreateStore(storeName); // Define some NTriples data to insert into the store. // NTriple is a line based format. One good way to create data is to use // the StringBuilder class and AppendLine. var data = new StringBuilder(); // data about the BrightstarDB product; name, and categories. data.AppendLine( "<http://www.networkedplanet.com/products/brightstar> <http://www.networkedplanet.com/schemas/product/name> \"Brightstar DB\" ."); data.AppendLine( "<http://www.networkedplanet.com/products/brightstar> <http://www.networkedplanet.com/schemas/product/category> <http://www.networkedplanet.com/categories/nosql> ."); data.AppendLine( "<http://www.networkedplanet.com/products/brightstar> <http://www.networkedplanet.com/schemas/product/category> <http://www.networkedplanet.com/categories/.net> ."); data.AppendLine( "<http://www.networkedplanet.com/products/brightstar> <http://www.networkedplanet.com/schemas/product/category> <http://www.networkedplanet.com/categories/rdf> ."); // data about the Networked Planet Web3 product; name, and categories. data.AppendLine( "<http://www.networkedplanet.com/products/web3> <http://www.networkedplanet.com/schemas/product/name> \"Web3 Platform\" ."); data.AppendLine( "<http://www.networkedplanet.com/products/web3> <http://www.networkedplanet.com/schemas/product/category> <http://www.networkedplanet.com/categories/.net> ."); data.AppendLine( "<http://www.networkedplanet.com/products/web3> <http://www.networkedplanet.com/schemas/product/category> <http://www.networkedplanet.com/categories/rdf> ."); data.AppendLine( "<http://www.networkedplanet.com/products/web3> <http://www.networkedplanet.com/schemas/product/category> <http://www.networkedplanet.com/categories/topicmaps> ."); Console.WriteLine("Inserting RDF triples into store."); // execute a transaction to insert the data into the store client.ExecuteTransaction(storeName, null, null, data.ToString()); // SPARQL query for all the categories connected to BrightstarDB var query = "SELECT ?category WHERE { <http://www.networkedplanet.com/products/brightstar> <http://www.networkedplanet.com/schemas/product/category> ?category }"; // Create an XDocument from the SPARQL Result XML. // See http://www.w3.org/TR/rdf-sparql-XMLres/ for the XML format returned. var result = XDocument.Load(client.ExecuteQuery(storeName, query)); Console.WriteLine("Executing SPARQL query:"); Console.WriteLine(query); Console.WriteLine(); // Use BrightstarDB Extension methods to iterate the result rows // and pull out column values foreach (var sparqlResultRow in result.SparqlResultRows()) { var val = sparqlResultRow.GetColumnValue("category"); Console.WriteLine("Category is " + val); } Console.WriteLine(); Console.WriteLine(); // Deletion is done by matching patterns of triples to be deleted. // There is a special Resource that can be used as a wildcard. // The following example deletes all the category data about BrightstarDB. // Again we use the StringBuilder to create the delete pattern. var deletePatternsData = new StringBuilder(); deletePatternsData.AppendLine( "<http://www.networkedplanet.com/products/brightstar> <http://www.networkedplanet.com/schemas/product/category> <http://www.brightstardb.com/.well-known/model/wildcard> ."); Console.WriteLine("Executing SPARQL query for deletion against store."); Console.WriteLine(deletePatternsData); client.ExecuteTransaction(storeName, null, deletePatternsData.ToString(), null); // SPARQL query for all the categories connected to BrightstarDB query = "SELECT ?category WHERE { <http://www.networkedplanet.com/products/brightstar> <http://www.networkedplanet.com/schemas/product/category> ?category }"; result = XDocument.Load(client.ExecuteQuery(storeName, query)); var numberOfCategories = result.SparqlResultRows().Count(); Console.WriteLine("Category count after delete (should be 0) is " + numberOfCategories); Console.WriteLine(); Console.WriteLine(); // data about the BrightstarDB product with literals defined with XML Schema data types var literals = new StringBuilder(); literals.AppendLine( "<http://www.networkedplanet.com/products/brightstar> <http://www.networkedplanet.com/schemas/product/code> \"123\"^^<http://www.w3.org/2001/XMLSchema#integer> ."); literals.AppendLine( "<http://www.networkedplanet.com/products/brightstar> <http://www.networkedplanet.com/schemas/product/releaseDate> \"2011-11-11 12:00\"^^<http://www.w3.org/2001/XMLSchema#dateTime> ."); literals.AppendLine( "<http://www.networkedplanet.com/products/brightstar> <http://www.networkedplanet.com/schemas/product/cost> \"0.00\"^^<http://www.w3.org/2001/XMLSchema#decimal> ."); Console.WriteLine("Inserting RDF triples into store."); // execute a transaction to insert the data into the store client.ExecuteTransaction(storeName, null, null, literals.ToString()); const string queryAll = "SELECT ?o ?p ?l WHERE { ?o ?p ?l }"; // Create an XDocument from the SPARQL Result XML. // See http://www.w3.org/TR/rdf-sparql-XMLres/ for the XML format returned. var resultAll = XDocument.Load(client.ExecuteQuery(storeName, queryAll)); Console.WriteLine("Executing SPARQL query: {0}", queryAll); Console.WriteLine(); // Use BrightstarDB Extension methods to iterate the result rows // and pull out column values foreach (var sparqlResultRow in resultAll.SparqlResultRows()) { var o = sparqlResultRow.GetColumnValue("o"); var p = sparqlResultRow.GetColumnValue("p"); var l = sparqlResultRow.GetColumnValue("l"); Console.WriteLine("o= {0}\tp={1}\tl={2}", o, p, l); } // Shutdown Brightstar processing threads BrightstarService.Shutdown(); Console.WriteLine(); Console.WriteLine("Finished. Press the Return key to exit."); Console.ReadLine(); }
private static void Main(string[] args) { SamplesConfiguration.Register(); Console.WriteLine("BrightstarDB Dynamic Objects Example"); Console.WriteLine("Creating and populating store using dynamic objects"); // gets a new BrightstarDB DataObjectContext var dataObjectContext = BrightstarService.GetDataObjectContext("type=embedded;storesDirectory=" + SamplesConfiguration.StoresDirectory); // create a dynamic context var dynaContext = new BrightstarDynamicContext(dataObjectContext); // open a new store var storeId = "DynamicSample" + Guid.NewGuid().ToString(); var dynaStore = dynaContext.CreateStore(storeId); // create some dynamic objects. dynamic brightstar = dynaStore.MakeNewObject(); dynamic product = dynaStore.MakeNewObject(); // set some properties brightstar.name = "BrightstarDB"; product.rdfs__label = "Product"; var id = brightstar.Identity; // use namespace mapping (RDF and RDFS are defined by default) // Assigning a list creates repeated RDF properties. brightstar.rdfs__label = new[] { "BrightstarDB", "NoSQL Database" }; // objects are connected together in the same way brightstar.rdfs__type = product; dynaStore.SaveChanges(); Console.WriteLine("Reading dynamic object from BrightstarDB"); // open store and read some data dynaStore = dynaContext.OpenStore(storeId); brightstar = dynaStore.GetDataObject(brightstar.Identity); Console.WriteLine("Got item with identity: {0}", brightstar.Identity); // property values are ALWAYS collections. var name = brightstar.name.FirstOrDefault(); Console.WriteLine("\tName = {0}", name); // property can also be accessed by index var nameByIndex = brightstar.name[0]; Console.WriteLine("\tName (using indexed property) = {0}", nameByIndex); // they can be enumerated without a cast Console.WriteLine("Enumerating rdfs:label values"); foreach (var l in brightstar.rdfs__label) { Console.WriteLine("\tLabel = {0}", l); } // object relationships are navigated in the same way Console.WriteLine("Retrieving rdfs:type relationship"); var p = brightstar.rdfs__type.FirstOrDefault(); Console.WriteLine("\tType object ID = {0}", p.Identity); Console.WriteLine("\tType object label = {0}", p.rdfs__label.FirstOrDefault()); // dynamic objects can also be loaded via sparql dynaStore = dynaContext.OpenStore(storeId); Console.WriteLine("Binding SPARQL query to dynamic objects"); var objects = dynaStore.BindObjectsWithSparql("select distinct ?dy where { ?dy ?p ?o }"); foreach (var obj in objects) { Console.WriteLine("\tItem Label: {0}", obj.rdfs__label[0]); } // Shutdown Brightstar processing threads. BrightstarService.Shutdown(); Console.WriteLine(); Console.WriteLine("Example complete. Press return to exit."); Console.ReadLine(); }