Example #1
0
        private object InternalSet(string key, object value)
        {
            var objType = value.GetType();

            if (objType.IsPrimitiveOrSimple())
            {
                value = new PrimitiveWrapper()
                {
                    val = value
                };
            }
            var oldBson = GetBson(key);
            var newVal  = bsonMapper.ToDocument(value);

            if (oldBson == null)
            {
                lock (threadLock) { collection.Insert(key, newVal); }
                return(null);
            }
            else
            {
                var oldVal = InternalGet(oldBson, objType);
                lock (threadLock) { collection.Update(key, newVal); }
                return(oldVal);
            }
        }
Example #2
0
        public LiteDBStorageItem(UltraLiteDatabase db, string key)
        {
            Key         = key;
            _database   = db;
            _collection = _database.GetCollection <Definition>(TableName);
            _filter     = Query.EQ("Key", new BsonValue(key));
            var doc = _collection.FindOne(_filter);

            if (doc == null)
            {
                _collection.Insert(new Definition {
                    Key = key
                });
            }
            else if (!string.IsNullOrWhiteSpace(doc.Json))
            {
                data = JsonConvert.DeserializeObject <T>(doc.Json);
            }
        }