Example #1
0
        private static IEnumerable BSonToIEnumerable(this BSonDoc doc)
        {
#if WINDOWS_PHONE
            List <object> list = new List <object>();
            foreach (var p in doc.Properties)
            {
                var val = doc[p];
                if (val is IBSonNode)
                {
                    list.Add(((IBSonNode)val));
                }
                else
                {
                    list.Add(val);
                }
            }
            return(list);
#else
            ArrayList list = new ArrayList();
            foreach (var p in doc.Properties)
            {
                var val = doc[p];
                if (val is IBSonNode)
                {
                    list.Add(((IBSonNode)val));
                }
                else
                {
                    list.Add(val);
                }
            }
            return(list);
#endif
        }
Example #2
0
 public override void OnHandle(IStore store,
     string collection,
     BSonDoc command,
     BSonDoc document)
 {
     IObjectStore st = store.GetCollection(collection);
       st.Set(document);
 }
Example #3
0
        public static BSonDoc Deserialize(this byte[] data)
        {
            MemoryStream ms = new MemoryStream(data);
            BinaryReader br = new BinaryReader(ms);

            BSonDoc result = NavigateStream(br) as BSonDoc;

            return(result);
        }
Example #4
0
        private static void UpdateSingleDocument(BSonDoc document, IObjectStore store)
        {
            var obj = store.GetById((string)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);
        }
Example #5
0
        private static BSonDoc IDictionaryToBSon(this IDictionary entities)
        {
            BSonDoc doc = new BSonDoc(BSonDocumentType.BSON_Dictionary);

            foreach (DictionaryEntry item in entities)
            {
                doc[item.Key.ToString()] = item.Value.ToBSon();
            }
            return(doc);
        }
Example #6
0
        private static BSonDoc IEnumerableToBSon(this IEnumerable entities)
        {
            BSonDoc doc = new BSonDoc(BSonDocumentType.BSON_DocumentArray);
            int     x   = 0;

            foreach (var item in entities)
            {
                doc[string.Format("i{0}", x++)] = item.ToBSon();
            }
            return(doc);
        }
 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();
 }
Example #8
0
        private static BSonDoc IDictionaryToBSonG <TK, T>(this IDictionary <TK, T> entities) where T : class
        {
            BSonDoc doc = new BSonDoc(BSonDocumentType.BSON_Dictionary);
            var     tt  = typeof(T);

            //doc["_type_"] = tt.FullName;

            foreach (var item in entities)
            {
                doc[item.Key.ToString()] = item.Value.ToBSon();
            }
            return(doc);
        }
Example #9
0
    //#region Constructor

    //internal ObjectStore()
    //  : this(ObjectStoreKeyType.Integer)
    //{ }

    //internal ObjectStore(ObjectStoreKeyType keytype)
    //{

    //  KeyType = keytype;

    //  switch (KeyType)
    //  {
    //    case ObjectStoreKeyType.Integer:
    //      newIdFunction = () => ++_storeintuid;
    //      currentIdFunction = () => _storeintuid;
    //      break;
    //    case ObjectStoreKeyType.LongInteger:
    //      newIdFunction = () => ++_storelonguid;
    //      currentIdFunction = () => _storelonguid;
    //      break;
    //    case ObjectStoreKeyType.GlobalUniqueIdentifier:
    //      newIdFunction = () => { _storeGuid = Guid.NewGuid(); return _storeGuid; };
    //      currentIdFunction = () => _storeGuid;
    //      break;
    //    default:
    //      break;
    //  }

    //}
    //#endregion

    #region Public methods for storing objects

    public void Set(BSonDoc entity)
    {
      ChangesFromLastSave++;
      //var lastid = currentIdFunction();
      var uid = GetEntityUI(entity);

      if (dContains(uid))
      {
        dUpdate(uid, entity);
        return;
      }
      dSet(uid, entity);
    }
Example #10
0
        private static BSonDoc IEnumerableToBSonG <T>(this IEnumerable <T> entities) where T : class
        {
            BSonDoc doc = new BSonDoc(BSonDocumentType.BSON_DocumentArray);
            var     tt  = typeof(T);
            //doc["_type_"] = tt.FullName;

            int x = 0;

            foreach (var item in entities)
            {
                doc[string.Format("i{0}", x++)] = (item.GetBSonType() == BSonTypeEnum.BSON_Document ? (object)item.ToBSon() : (object)item);
            }
            return(doc);
        }
Example #11
0
        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((string)command[CommandKeyword.Id]);
            if (ent != null) st.Remove(ent);
              }

              if (document != null && document.HasProperty(DocumentMetadata.IdPropertyName))
              {
            var ent = st.GetById((string)document[DocumentMetadata.IdPropertyName]);
            if (ent != null) st.Remove(ent);
              }
        }
Example #12
0
        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);
        }
Example #13
0
        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))

              }
              }
        }
Example #14
0
        private static BSonDoc DeserializeDocument(BinaryReader reader, BSonDocumentType doctype)
        {
            var doc = new BSonDoc(doctype);

            doc.Name = reader.ReadString();
            var size        = reader.ReadInt32();
            var endposition = reader.BaseStream.Position + size;

            while (reader.BaseStream.Position < endposition)
            {
                var res = NavigateStream(reader);

                var pp = res as IBSonNode;
                if (pp != null)
                {
                    doc[pp.Name] = pp.Value;
                }
            }

            return(doc);
        }
Example #15
0
        private static IEnumerable <T> BSonToIEnumerableG <T>(this BSonDoc doc)
        {
            List <T> list = new List <T>();

            foreach (var p in doc.Properties)
            {
                var val  = doc[p];
                T   rval = default(T);
                if (val is IBSonNode)
                {
                    rval = ((IBSonNode)val).FromBSon <T>();
                }
                else if (typeof(T).IsAssignableFrom(val.GetType()))
                {
                    rval = (T)val;
                }

                list.Add(rval);
            }
            return(list);
        }
Example #16
0
        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);
              }
        }
Example #17
0
        private static BSonDoc DocumentToBSon <T>(this T entity, bool includetype = true) where T : class
        {
            if (entity is BSonDoc)
            {
                return(entity as BSonDoc);
            }

            BSonDoc doc = new BSonDoc();
            var     tt  = entity.GetType();

            //if (includetype)
            //  doc["_type_"] = tt.FullName;

            var properties = tt.GetProperties();

            foreach (var p in properties)
            {
                if (p.GetCustomAttributes(typeof(IgnorePropertyAttribute), false).Length == 0)
                {
                    doc[p.Name] = entity.NavigateProperty <T>(p.Name);
                }
            }
            return(doc);
        }
Example #18
0
 public abstract void OnHandle(IStore store, 
     string collection ,
     BSonDoc command,
     BSonDoc document);
Example #19
0
 private void dSet(string key, BSonDoc doc)
 {
   lock (_primarystore)
   {
     if (!dUpdate(key, doc))
       dInsert(key, doc);
   }
 }
Example #20
0
    private string GetEntityUI(BSonDoc entity)
    {
      if (entity.HasProperty(DocumentMetadata.IdPropertyName) && entity[DocumentMetadata.IdPropertyName] != null)
        return (string)entity[DocumentMetadata.IdPropertyName];

      entity[DocumentMetadata.IdPropertyName] = newIdFunction();
      return (string)entity[DocumentMetadata.IdPropertyName];
    }
Example #21
0
 protected static BSonDoc GetValue(BSonDoc document)
 {
     if (document.HasProperty(CommandKeyword.Value))
     return document[CommandKeyword.Value] as BSonDoc;
       return document;
 }
Example #22
0
    private void dInsert(string key, BSonDoc doc)
    {
      //doc["@ts#"

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

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

      if (!freedictionary.ContainsKey(key))
        freedictionary.Add(key, doc.Serialize());
    }
Example #23
0
 private static void UpdateCollection(BSonDoc document, IObjectStore store)
 {
     // TODO: completely to do.
 }
Example #24
0
 private bool dUpdate(string key, BSonDoc doc)
 {
   lock (_primarystore)
     foreach (var d in _primarystore)
       if (d.ContainsKey(key))
       {
         d[key] = doc.Serialize();
         return true;
       }
   return false;
 }
Example #25
0
        private static ICommandHandler[] ChechHandlers(BSonDoc command)
        {
            string actionname = string.Empty;
              if (command.HasProperty(CommandKeyword.Action))
            actionname = (command[CommandKeyword.Action] ?? string.Empty).ToString();

              LogWriter.LogInformation(string.Format("Executing action {0}", string.Empty), LogEntryType.Information);

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

              return null;
        }
Example #26
0
 public void Remove(BSonDoc entity)
 {
   //var lastid = currentIdFunction();
   var uid = GetEntityUI(entity);
   if (dContains(uid))
     if (dRemove(uid))
       ChangesFromLastSave++;
 }