Wednesday, 23 November 2011

Hello World - Raven DB

I assume you have started Raven DB server @ localhost:8080.

class Program
{
        class Company
        {
            public string Id { get; set; }
            public string Name { get; set; }
            public string Location { get; set; }
        }

        class Employee
        {
            public string Id { get; set; }
            public string Name { get; set; }
            public string Location { get; set; }
        }


        // Demo the Store & SaveChanges
        static void Save()
        {
            using (var documentStore = new DocumentStore())
            {
                documentStore.Url = "http://localhost:8080/";
                documentStore.Initialize();
               
                using (var session = documentStore.OpenSession())
                {
                    session.Store(new Company { Location = "Bengaluru", Name = "MIC" });
                    session.Store(new Company { Location = "Virginia", Name = "TD" });

                    session.Store(new Employee { Location = "Bengaluru", Name = "Suresh Babu" });
                    session.Store(new Employee { Location = "Virginia", Name = "David Walker" });

                    session.SaveChanges();
                }
            }
        }

        static void Query()
        {
            using (var documentStore = new DocumentStore())
            {
                documentStore.Url = "http://localhost:8080/";
                documentStore.Initialize();

                using (var session = documentStore.OpenSession())
                {
                    var companyResults = session.Query().ToArray();
                    var employeeResults = session.Query().ToArray();

                    foreach (var result in companyResults)
                    {
                        Console.WriteLine("Company Name : {0}, Location : {1}, ID : {2}", result.Name, result.Location, result.Id);
                    }

                    foreach (var result in employeeResults)
                    {
                        Console.WriteLine("Employee Name : {0}, Location : {1}, ID : {2}", result.Name, result.Location, result.Id);
                    }
                }
            }
        }

        static void Load()
        {
            using (var documentStore = new DocumentStore())
            {
                documentStore.Url = "http://localhost:8080/";
                documentStore.Initialize();

                using (var session = documentStore.OpenSession())
                {
                    var companyResult = session.Load("companies/1");
                    var employeeResult = session.Load("employees/1");

                    Console.WriteLine("Company Name : {0}, Location : {1}, ID : {2}",
                                        companyResult.Name, companyResult.Location, companyResult.Id);
                    Console.WriteLine("Employee Name : {0}, Location : {1}, ID : {2}",
                                        employeeResult.Name, employeeResult.Location, employeeResult.Id);

                    companyResult = session.Load("companies/2");
                    employeeResult = session.Load("employees/2");

                    Console.WriteLine("Company Name : {0}, Location : {1}, ID : {2}",
                                        companyResult.Name, companyResult.Location, companyResult.Id);
                    Console.WriteLine("Employee Name : {0}, Location : {1}, ID : {2}",
                                        employeeResult.Name, employeeResult.Location, employeeResult.Id);
                }
            }
        }

        static void LuceneQuery()
        {
            using (var documentStore = new DocumentStore())
            {
                documentStore.Url = "http://localhost:8080/";
                documentStore.Initialize();

                using (var session = documentStore.OpenSession())
                {
                    var companyResults = session.Advanced.LuceneQuery("CompanyLocationIndex")
                                            .Where("Location:Virginia").ToArray();
                    var employeeResults = session.Advanced.LuceneQuery("EmployeeNameIndex")
                                            .Where("Name:\"David Walker\"")
                                            .WaitForNonStaleResults()
                                            .ToArray();

                    foreach (var result in companyResults)
                    {
                        Console.WriteLine("Company Name : {0}, Location : {1}, ID : {2}", result.Name, result.Location, result.Id);
                    }

                    foreach (var result in employeeResults)
                    {
                        Console.WriteLine("Employee Name : {0}, Location : {1}, ID : {2}", result.Name, result.Location, result.Id);
                    }
                }
            }
        }

        static void CreateIndex()
        {
            using (var documentStore = new DocumentStore())
            {
                documentStore.Url = "http://localhost:8080/";
                documentStore.Initialize();

                documentStore.DatabaseCommands.PutIndex("EmployeeNameIndex",
                            new IndexDefinitionBuilder
                            {
                                Map = employees => from employee in employees
                                                   select new { employee.Name }
                            }
                            );

                documentStore.DatabaseCommands.PutIndex("CompanyLocationIndex",
                            new IndexDefinitionBuilder
                            {
                                Map = companies => from company in companies
                                                   select new { company.Location }
                            }
                            );
            }
        }

        static void Main(string[] args)
        {
            Save();
            Query();
            Load();
            CreateIndex();
            LuceneQuery();
        }
    }


No comments: