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.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Threading;

namespace RaptorDB.Views
{
    internal class ViewManager
    {
        public ViewManager(string viewfolder, KeyStoreGuid objstore)
        {
            _Path = viewfolder;
            _objectStore = objstore;
        }

        private KeyStoreGuid _objectStore;
        private ILog _log = LogManager.GetLogger(typeof(ViewManager));
        private string _Path = "";
        // list of views
        private SafeDictionary<string, ViewHandler> _views = new SafeDictionary<string, ViewHandler>();
        // primary view list
        private SafeDictionary<Type, string> _primaryView = new SafeDictionary<Type, string>();
        // other views type->list of view names to call
        private SafeDictionary<Type, List<string>> _otherViews = new SafeDictionary<Type, List<string>>();
        private TaskQueue _que = new TaskQueue();

        internal Result Query<T>(Type objtype, Expression<Predicate<T>> filter)
        {
            string viewname = null;
            // find view from name
            if (_primaryView.TryGetValue(objtype, out viewname))
                return Query(viewname, filter);

            // FIX : add search for viewtype here

            
            _log.Error("view not found", viewname);
            return new Result(false, new Exception("view not found : "+ viewname));
        }

        internal Result Query<T>(string viewname, Expression<Predicate<T>> filter)
        {
            ViewHandler view = null;
            // find view from name
            if (_views.TryGetValue(viewname, out view))
                return view.Query(filter);
            
            _log.Error("view not found", viewname);
            return new Result(false, new Exception("view not found : " + viewname));
        }

        internal Result Query(string viewname)
        {
            ViewHandler view = null;
            // find view from name
            if (_views.TryGetValue(viewname, out view))
                return view.Query();

            _log.Error("view not found", viewname);
            return new Result(false, new Exception("view not found : " + viewname));
        }

        internal Result Query(Type view)
        {
            string viewname = null;
            // find view from name
            if (_primaryView.TryGetValue(view, out viewname))
                return Query(viewname);

            // FIX : add search for viewtype here

            _log.Error("view not found", viewname);
            return new Result(false, new Exception("view not found : " + viewname));
        }

        internal void Insert<T>(string viewname, Guid docid, T data)
        {
            ViewHandler vman = null;
            // find view from name
            if (_views.TryGetValue(viewname, out vman))
            {
                if (vman._view.isActive == false)
                {
                    _log.Debug("view is not active, skipping insert : " + viewname);
                    return;
                }
                if (vman._view.BackgroundIndexing) 
                    _que.AddTask(() => vman.Insert<T>(docid, data));
                else
                    vman.Insert<T>(docid, data);

                return;
            }
            _log.Error("view not found", viewname);
        }

        public object Fetch(Guid guid)
        {
            byte[] b = null;
            if (_objectStore.Get(guid, out b))
                return fastJSON.JSON.Instance.ToObject(Encoding.ASCII.GetString(b));
            
            return null;
        }

        internal string GetPrimaryViewForType(Type type)
        {
            string vn = "";
            if (type == null)
                return vn;
            // find direct
            if (_primaryView.TryGetValue(type, out vn))
                return vn;
            // recurse basetype
            return GetPrimaryViewForType(type.BaseType);
        }

        internal List<string> GetOtherViewsList(Type type)
        {
            List<string> list = new List<string>();
            _otherViews.TryGetValue(type, out list);
            return list;
        }

        internal Result RegisterView<T>(View<T> view)
        {
            Result ret = view.Verify();
            if (ret.OK == false) return ret;

            ViewHandler vh = null;
            if (_views.TryGetValue(view.Name, out vh))
            {
                // FEATURE : already exists -> replace? regen?
                _log.Error("View already added and exists : " + view.Name);
            }
            else
            {
                vh = new ViewHandler(_Path, this);
                vh.SetView(view);
                _views.Add(view.Name, vh);
                if (view.isPrimaryList)
                {
                    foreach (string tn in view.FireOnTypes)
                        _primaryView.Add(Type.GetType(tn), view.Name);
                }
                else
                {
                    foreach (string tn in view.FireOnTypes)
                    {
                        // add to other views
                        List<string> list = null;
                        Type t = Type.GetType(tn);
                        if (_otherViews.TryGetValue(t, out list))
                            list.Add(view.Name);
                        else
                        {
                            list = new List<string>();
                            list.Add(view.Name);
                            _otherViews.Add(t, list);
                        }
                    }
                }
            }

            // FEATURE : add existing data to this view

            return new Result(true);
        }

        internal void ShutDown()
        {
            _log.Debug("View Manager shutdown");
            _que.Shutdown();
            // shutdown views
            foreach (var v in _views)
            {
                _log.Debug(" shutting down : " + v.Value._view.Name);
                v.Value.Shutdown();
            }
        }
    }
}

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.130516.1 | Last Updated 18 May 2013
Article Copyright 2012 by Mehdi Gholam
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid