Click here to Skip to main content
Click here to Skip to main content
Articles » Database » NoSQL » General » Downloads
 

RaptorDB - the Document Store

By , 18 May 2013
 
RaptorDB_doc_v1.1.zip
RaptorDB
DataTypes
fastBinaryJSON
fastJSON
Helper
Indexes
Mapping
Properties
Resources
Storage
Views
testing
RaptorDB_doc_v1.2.zip
RaptorDB_doc_v1.3.zip
datagridbinding
Properties
Settings.settings
RaptorDB_doc_v1.4.zip
Form1.resources
RaptorDB.Common
fastBinaryJSON
fastJSON
Properties
RaptorDBServer
Properties
Views
Properties
RaptorDB_doc_v1.5.zip
Form1.resources
RaptorDB_doc_v1.6.zip
Form1.resources
RaptorDB_doc_v1.7.zip
Form1.resources
RaptorDB_doc_v1.8.1.zip
Form1.resources
RaptorDB_doc_v1.8.2.zip
Form1.resources
RaptorDB_doc_v1.8.3.zip
Form1.resources
RaptorDB_doc_v1.9.0.zip
Form1.resources
RaptorDB_doc_v1.9.1.zip
Form1.resources
RaptorDB_doc_v1.9.2.zip
Form1.resources
RaptorDB_doc_v2.0.0.zip
Form1.resources
RaptorDB_doc_v2.0.5.zip
Form1.resources
RaptorDB_v1.0.zip
using System;
using System.Diagnostics;
using System.Collections;
using System.IO;
using System.Text;
using System.Threading;
using RaptorDB;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Reflection;
using RaptorDB.Views;
using System.Linq.Expressions;
using System.Linq;
using System.Collections.ObjectModel;

namespace testing
{
    #region [  class definitions  ]
    public class LineItem
    {
        public decimal QTY { get; set; }
        public string Product { get; set; }
        public decimal Price { get; set; }
        public decimal Discount { get; set; }
    }

    public class SalesInvoice
    {
        public SalesInvoice()
        {
            ID = Guid.NewGuid();
        }
        public Guid ID { get; set; }
        public string CustomerName { get; set; }
        public string Address { get; set; }
        public List<LineItem> Items { get; set; }
        public DateTime Date { get; set; }
        public int Serial { get; set; }
        public byte Status { get; set; }
    }
    #endregion

    #region [  views  ]
    public class SalesInvoiceView : View<SalesInvoice>
    {
        public class RowSchema
        {
            [FullText]
            public string CustomerName;
            public DateTime InvoiceDate;
            public string Address;
            public int Serial;
            public byte Status;
        }

        public SalesInvoiceView()
        {
            this.Name = "SalesInvoice";
            this.Description = "A primary view for SalesInvoices";
            this.isPrimaryList = true;
            this.isActive = true;
            this.BackgroundIndexing = true;

            this.Schema = typeof(SalesInvoiceView.RowSchema);

            this.AddFireOnTypes(typeof(SalesInvoice));

            this.Mapper = (api, docid, doc) =>
            {
                api.Emit(docid, doc.CustomerName, doc.Date, doc.Address, doc.Serial, doc.Status);
            };
        }
    }

    public class SalesItemRowsView : View<SalesInvoice>
    {
        public class RowSchema
        {
            public string Product;
            public decimal QTY;
            public decimal Price;
            public decimal Discount;
        }

        public SalesItemRowsView()
        {
            this.Name = "SalesItemRows";
            this.Description = "";
            this.isPrimaryList = false;
            this.isActive = true;
            this.BackgroundIndexing = true;

            this.Schema = typeof(SalesItemRowsView.RowSchema);

            this.AddFireOnTypes(typeof(SalesInvoice));

            this.Mapper = (api, docid, doc) =>
                {
                    if (doc.Status == 3 && doc.Items != null)
                        foreach (var i in doc.Items)
                            api.Emit(docid, i.Product, i.QTY, i.Price, i.Discount);
                };
        }
    }
    #endregion

    public class program
    {
        public static void Main()
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

            RaptorDB.RaptorDB rap = RaptorDB.RaptorDB.Open("RaptorDB");

            if (rap.RegisterView(new SalesInvoiceView()).OK == false)
            {
                Console.WriteLine("Error registering view");
                return;
            }

            if (rap.RegisterView(new SalesItemRowsView()).OK == false)
            {
                Console.WriteLine("Error registering view");
                return;
            }

            bool end = false;

            Console.WriteLine("Press (P)redifined query, (I)nsert 100,000 docs, (S)tring query, (Q)uit");

        redo:
            ConsoleKey key = Console.ReadKey().Key;
            Console.WriteLine();

            switch (key)
            {
                case ConsoleKey.P:
                    predefinedquery(rap);
                    break;
                case ConsoleKey.I:
                    insertdata(rap);
                    break;
                case ConsoleKey.S:
                    stringquery(rap);
                    break;
                case ConsoleKey.Q:
                    end = true;
                    break;
                default:
                    Console.WriteLine("Press (P)redifined query, (I)nsert 100,000 docs, (S)tring query, (Q)uit");
                    break;
            }
            if (!end)
                goto redo;
            rap.Shutdown();
        }

        private static void stringquery(RaptorDB.RaptorDB rap)
        {
            Console.WriteLine("Enter you query in the following format :  viewname , query");
            string[] s = Console.ReadLine().Split(',');
            DateTime dt = FastDateTime.Now;
            try
            {
                var q = rap.Query(s[0].Trim(), s[1].Trim());
                Console.WriteLine("query time = " + FastDateTime.Now.Subtract(dt).TotalSeconds);
                Console.WriteLine("query count = " + q.Count);
                Console.WriteLine();
            }
            catch { Console.WriteLine("error in query."); }
        }

        private static void predefinedquery(RaptorDB.RaptorDB rap)
        {
            DateTime dt = FastDateTime.Now;
            var q = rap.Query(typeof(SalesItemRowsView), (LineItem l) => (l.Product == "prod 1" || l.Product == "prod 3"));
            Console.WriteLine("query lineitems time = " + FastDateTime.Now.Subtract(dt).TotalSeconds);
            Console.WriteLine("query count = " + q.Count);
            Console.WriteLine();
        }

        private static void insertdata(RaptorDB.RaptorDB rap)
        {
            DateTime dt = FastDateTime.Now;

            for (int i = 0; i < 100000; i++)
            {
                var inv = new SalesInvoice()
                {
                    Date = FastDateTime.Now,
                    Serial = i % 10000,
                    CustomerName = "me " + i % 10,
                    Status = (byte)(i % 4),
                    Address = "df asd sdf asdf asdf"
                };
                inv.Items = new List<LineItem>();
                for (int k = 0; k < 5; k++)
                    inv.Items.Add(new LineItem() { Product = "prod " + k, Discount = 0, Price = 10 + k, QTY = 1 + k });
                if (i % 5000 == 0)
                    Console.Write(".");
                rap.Save(inv.ID, inv);
            }
            Console.WriteLine();
            Console.WriteLine("insert time secs = " + FastDateTime.Now.Subtract(dt).TotalSeconds);
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            File.WriteAllText("error.txt", "" + e.ExceptionObject);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of use and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Mehdi Gholam
Architect
United Kingdom United Kingdom
Member
Mehdi first started programming when he was 8 on BBC+128k machine in 6512 processor language, after various hardware and software changes he eventually came across .net and c# which he has been using since v1.0.
He is formally educated as a system analyst Industrial engineer, but his programming passion continues.
 
* Mehdi is the 5th person to get 6 out of 7 Platinums on CodeProject (13th Jan'12)

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 18 May 2013
Article Copyright 2012 by Mehdi Gholam
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid