Click here to Skip to main content
15,881,882 members
Articles / Web Development / HTML

RaptorDB - The Document Store

Rate me:
Please Sign up or sign in to vote.
4.96/5 (278 votes)
24 Jul 2019CPOL86 min read 2.3M   16.3K   653  
NoSql, JSON based, Document store database with compiled .net map functions and automatic hybrid bitmap indexing and LINQ query filters (now with standalone Server mode, Backup and Active Restore, Transactions, Server side queries, MonoDroid support, HQ-Branch Replication, working in Linux, .net
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 Service 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)


Written By
Architect -
United Kingdom United Kingdom
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 Platinum's on Code-Project (13th Jan'12)
* Mehdi is the 3rd person to get 7 out of 7 Platinum's on Code-Project (26th Aug'16)

Comments and Discussions