// A method that handles densodb events MUST have this delegate.
        public static void Set(IStore dbstore, BSonDoc command)
        {
            // IStore interface gives you a lowlevel access to DB Structure,
              // Every action you take now will jump directly into DB without any event dispatching

              // The Istore is preloaded from densodb, and you should not have access to densodb internals.

              // Now deserialize message from Bson object.
              // should be faster using BsonObject directly but this way is more clear.
              var message = command.FromBSon<Message>();

              // Get the sender UserProfile
              var userprofile = dbstore.GetCollection("users").Where(d => d["UserName"].ToString() == message.From).FirstOrDefault().FromBSon<UserProfile>();

              if (userprofile != null)
              {
            // add message to user's messages
            var profilemessages = dbstore.GetCollection(string.Format("messages_{0}", userprofile.UserName));
            profilemessages.Set(command);

            // add message to user's wall
            var profilewall = dbstore.GetCollection(string.Format("wall_{0}", userprofile.UserName));
            profilewall.Set(command);

            // Now i have user's follower.
            foreach (var follower in userprofile.FollowedBy)
            {
              // Get followers's wall
              var followerwall = dbstore.GetCollection(string.Format("wall_{0}", follower));

              // store the messages in follower's wall.
              followerwall.Set(command);
            }
              }
        }
Exemple #2
0
 public override void OnHandle(IStore store,
                           string collection,
                           BSonDoc command,
                           BSonDoc document)
 {
     IObjectStore st = store.GetCollection(collection);
       st.Set(document);
 }
Exemple #3
0
        private static void ExtractInfoFromCommand(BSonDoc command, ref string type, ref string action)
        {
            if (command.HasProperty("_action"))
            action = (command["_action"] ?? string.Empty).ToString();

              if (command.HasProperty("_type"))
            type = (command["_type"] ?? string.Empty).ToString();
        }
 private static void InternalDelete(BSonDoc document, string collection, IStore store)
 {
     IObjectStore st = store.GetCollection(collection);
       if (document.HasProperty(Configuration.DensoIDKeyName))
       {
     var ent = st.GetById((byte[])document[Configuration.DensoIDKeyName]);
     if (ent != null)
       st.Remove(ent);
       }
 }
        private static void UpdateSingleDocument(BSonDoc document, IObjectStore store)
        {
            var obj = store.GetById((byte[])document[DocumentMetadata.IdPropertyName]);
              BSonDoc val = GetValue(document);
              foreach (var p in GetRealProperties(val)) // remove properties starting with
            if (document.HasProperty(p))
              obj[p] = val[p];

              store.Set(obj);
        }
 protected static string[] GetRealProperties(BSonDoc document)
 {
     string[] invalidproperties = new string[] { CommandKeyword.Action,
                                           CommandKeyword.Collection,
                                           CommandKeyword.Filter,
                                           CommandKeyword.Id,
                                           CommandKeyword.Type,
                                           CommandKeyword.Value,
                                           DocumentMetadata.IdPropertyName,
                                           DocumentMetadata.TimeStampPropertyName };
       return document.Properties.Except(invalidproperties).ToArray();
 }
        private static Type GetDocumentType(BSonDoc command)
        {
            string result = null;
              if (command.HasProperty("_type_"))
            result = command["_type_"].ToString();
              else
            if (command.HasProperty("_value"))
            {
              var value = command["_value"] as BSonDoc;
              if (value != null && value.HasProperty("_type_"))
            result = value["_type_"].ToString();

            }
              if (!string.IsNullOrEmpty(result))
            return Type.GetType(result, false);
              return null;
        }
Exemple #8
0
        private static Action<ObjectStoreWrapper, BSonDoc> FindEventHandler(BSonDoc command)
        {
            var type = string.Empty;
              var action = string.Empty;
              Action<ObjectStoreWrapper, BSonDoc> mi = null;

              ExtractInfoFromCommand(command, ref type, ref action);
              mi = CheckMethodCache(type, action);

              if (mi != null) return mi;

              mi = ReflectMethodInfo(type, action);

              if (mi != null && !_methods[type].ContainsKey(action))
            _methods[type].Add(action, mi);

              return mi;
        }
        public override void OnHandle(IStore store,
                                  string collection,
                                  BSonDoc command,
                                  BSonDoc document)
        {
            IObjectStore st = store.GetCollection(collection);

              if (command.HasProperty(CommandKeyword.Id))
              {
            var ent = st.GetById((byte[])command[CommandKeyword.Id]);
            if (ent != null) st.Remove(ent);
              }

              if (document != null && document.HasProperty(DocumentMetadata.IdPropertyName))
              {
            var ent = st.GetById((byte[])document[DocumentMetadata.IdPropertyName]);
            if (ent != null) st.Remove(ent);
              }
        }
        public override void OnHandle(IStore store,
                                  string collection,
                                  BSonDoc command,
                                  BSonDoc document)
        {
            if (document == null || string.IsNullOrEmpty(collection)) return;
              IObjectStore st = store.GetCollection(collection);

              if (document.DocType == BSonDocumentType.BSON_DocumentArray ||
              document.DocType == BSonDocumentType.BSON_Dictionary)
              {
            var documents = document.ToList();
            if (documents != null)
              foreach (var d in documents)
              {
            //if(d is BSonDoc && ((BSonDoc)d).HasProperty(CommandKeyword.Id))

              }
              }
        }
        public override void OnHandle(IStore store,
                                  string collection,
                                  BSonDoc command,
                                  BSonDoc document)
        {
            if (document == null || string.IsNullOrEmpty(collection)) return;
              IObjectStore st = store.GetCollection(collection);

              if (document.HasProperty(DocumentMetadata.IdPropertyName))
              {
            UpdateSingleDocument(document, st); return;
              }

              if (document.HasProperty(CommandKeyword.Filter))
              {
            UpdateCollection(document, st); return;
              }

              st.Set(document);
        }
        public override void OnHandle(IStore store,
                                  string collection,
                                  BSonDoc command,
                                  BSonDoc document)
        {
            IObjectStore st = store.GetCollection(collection);

              if (document.DocType == BSonDocumentType.BSON_DocumentArray ||
              document.DocType == BSonDocumentType.BSON_Dictionary)
              {
            var documents = document.ToList();
            if (documents != null)
              foreach (var d in documents)
            if (d is BSonDoc)
              st.Set(d as BSonDoc);

              }
              else
              {
            st.Set(document);
              }
        }
        private static void InternalExecute(IStore store, BSonDoc command, InternalMethod method)
        {
            if (command.HasProperty("_value"))
              {
            var value = command["_value"] as BSonDoc;
            var collection = (command["_collection"] ?? string.Empty).ToString();

            method(value, collection, store);
              }
        }
Exemple #14
0
 public void Remove(BSonDoc entity)
 {
     //var lastid = currentIdFunction();
       var uid = GetEntityUI(entity);
       if (dContains(uid))
     if (dRemove(uid))
       ChangesFromLastSave++;
 }
 private static void InsertElement(BSonDoc document, IObjectStore store)
 {
     BSonDoc val = GetValue(document);
       store.Set(val);
 }
 private static BSonDoc GetValue(BSonDoc document)
 {
     if (document.HasProperty("_value"))
     return document["_value"] as BSonDoc;
       return document;
 }
Exemple #17
0
 private bool dUpdate(byte[] key, BSonDoc doc)
 {
     lock (_primarystore)
     foreach (var d in _primarystore)
       if (d.ContainsKey(key))
       {
     d[key] = doc.Serialize();
     return true;
       }
       return false;
 }
Exemple #18
0
        private byte[] GetEntityUI(BSonDoc entity)
        {
            if (entity.HasProperty(Configuration.DensoIDKeyName) && entity[Configuration.DensoIDKeyName] != null)
            return (byte[])entity[Configuration.DensoIDKeyName];

              entity[Configuration.DensoIDKeyName] = newIdFunction();
              return (byte[])entity[Configuration.DensoIDKeyName];
        }
 public static void Set(IStore store, BSonDoc command)
 {
     InternalExecute(store, command, new InternalMethod(InternalSet));
 }
        private static void UpdateSingleDocument(BSonDoc document, IObjectStore store)
        {
            var obj = store.GetById((byte[])document[Configuration.DensoIDKeyName]);
              BSonDoc val = GetValue(document);
              foreach (var p in val.GetRealProperties()) // remove properties starting with
            if (document.HasProperty(p))
              obj[p] = val[p];

              store.Set(obj);
        }
 private static void UpdateCollection(BSonDoc document, IObjectStore store)
 {
 }
Exemple #22
0
        private void dInsert(byte[] key, BSonDoc doc)
        {
            //doc["@ts#"

              Dictionary<byte[], byte[]> freedictionary = null;
              foreach (var d in _primarystore)
            if (d.Count < Configuration.DictionarySplitSize)
            {
              freedictionary = d; break;
            }

              if (freedictionary == null)
              {
            freedictionary = new Dictionary<byte[], byte[]>();
            _primarystore.Add(freedictionary);
              }

              if (!freedictionary.ContainsKey(key))
            freedictionary.Add(key, doc.Serialize());
        }
        private static void InternalUpdate(BSonDoc document, string collection, IStore store)
        {
            IObjectStore st = store.GetCollection(collection);

              if (document.HasProperty(Configuration.DensoIDKeyName))
              {
            UpdateSingleDocument(document, st); return;
              }

              if (document.HasProperty("_filter"))
              {
            UpdateCollection(document, st); return;
              }

              InsertElement(document, st); return;
        }
Exemple #24
0
 private void dSet(byte[] key, BSonDoc doc)
 {
     lock (_primarystore)
       {
     if (!dUpdate(key, doc))
       dInsert(key, doc);
       }
 }
 private static void UpdateCollection(BSonDoc document, IObjectStore store)
 {
     // TODO: completely to do.
 }
 public static void Update(IStore store, BSonDoc command)
 {
     InternalExecute(store, command, new InternalMethod(InternalUpdate));
 }
        private static ICommandHandler[] ChechHandlers(BSonDoc command)
        {
            string actionname = string.Empty;
              if (command.HasProperty(CommandKeyword.Action))
            actionname = (command[CommandKeyword.Action] ?? string.Empty).ToString();

              if (_commandHandlers.ContainsKey(actionname))
            return _commandHandlers[actionname].ToArray();

              return null;
        }
Exemple #28
0
        public void Set(BSonDoc entity)
        {
            ChangesFromLastSave++;
              //var lastid = currentIdFunction();
              var uid = GetEntityUI(entity);

              if (dContains(uid))
              {
            dUpdate(uid, entity);
            return;
              }
              dSet(uid, entity);
        }
 private static void ReplaceSingleDocument(BSonDoc document, IObjectStore store)
 {
     BSonDoc val = GetValue(document);
       store.Set(val);
 }
Exemple #30
0
        private byte[] GetEntityUI(BSonDoc entity)
        {
            if (entity.HasProperty(DocumentMetadata.IdPropertyName) && entity[DocumentMetadata.IdPropertyName] != null)
            return (byte[])entity[DocumentMetadata.IdPropertyName];

              entity[DocumentMetadata.IdPropertyName] = newIdFunction();
              return (byte[])entity[DocumentMetadata.IdPropertyName];
        }