Esempio n. 1
0
        public void StackExchangeRedisKeyValueStore_Hash_Exceptions()
        {
            var store = new StackExchangeRedisKeyValueStore("localhost:6379");

            Assert.ThrowsException <ArgumentException>(() =>
            {
                store.Set(StackExchangeRedisDataTypesEnum.Hash, new List <Tuple <string, byte[], object> >
                {
                    Tuple.Create <string, byte[], object>("testKey2_1", BinarySerializerHelper.SerializeObject(1), new StackExchangeRedisHashParams {
                        HashField = "testKey2_1_1"
                    }),
                    Tuple.Create <string, byte[], object>("testKey2_2", BinarySerializerHelper.SerializeObject(2), new StackExchangeRedisHashParams {
                        HashField = "testKey2_1_2"
                    })
                });
            });

            Assert.ThrowsException <ArgumentException>(() =>
            {
                store.Get(StackExchangeRedisDataTypesEnum.Hash, new List <Tuple <string, object> >
                {
                    Tuple.Create <string, object>("testKey2_1", new StackExchangeRedisHashParams {
                        HashField = "testKey2_1_1"
                    }),
                    Tuple.Create <string, object>("testKey2_2", new StackExchangeRedisHashParams {
                        HashField = "testKey2_1_2"
                    })
                });
            });
        }
        public bool Set <T>(TDataTypesEnum type, string key, T value, object extraParams = null)
        {
            if (Fork.ReadOnly)
            {
                return(false);
            }

            KeyValueStore.Delete(type, KeyGenerator.GenerateForkNullKey(AppId, Fork.Id, key), extraParams);;

            var forkedKey = KeyGenerator.GenerateForkValueKey(AppId, Fork.Id, key);

            var byteValue = value as byte[] ?? BinarySerializerHelper.SerializeObject(value);

            return(KeyValueStore.Set(type, forkedKey, byteValue, extraParams));
        }
Esempio n. 3
0
        public void StackExchangeRedisKeyValueStore_String()
        {
            var store = new StackExchangeRedisKeyValueStore("localhost:6379");

            store.Set(StackExchangeRedisDataTypesEnum.String, "testKey1", BinarySerializerHelper.SerializeObject(123));
            var res = (int)BinarySerializerHelper.DeserializeObject(store.Get(StackExchangeRedisDataTypesEnum.String, "testKey1"));

            Assert.AreEqual(123, res);

            store.Set(StackExchangeRedisDataTypesEnum.String, new List <Tuple <string, byte[], object> >
            {
                Tuple.Create <string, byte[], object>("testKey1_1", BinarySerializerHelper.SerializeObject(1), null),
                Tuple.Create <string, byte[], object>("testKey1_2", BinarySerializerHelper.SerializeObject(2), null),
                Tuple.Create <string, byte[], object>("testKey1_3", BinarySerializerHelper.SerializeObject(3), null)
            });

            var multipleRes = store.Get(StackExchangeRedisDataTypesEnum.String, new List <Tuple <string, object> >
            {
                Tuple.Create <string, object>("testKey1_1", null),
                Tuple.Create <string, object>("testKey1_2", null),
                Tuple.Create <string, object>("testKey1_3", null)
            });

            Assert.AreEqual(1, (int)BinarySerializerHelper.DeserializeObject(multipleRes["testKey1_1"]));
            Assert.AreEqual(2, (int)BinarySerializerHelper.DeserializeObject(multipleRes["testKey1_2"]));
            Assert.AreEqual(3, (int)BinarySerializerHelper.DeserializeObject(multipleRes["testKey1_3"]));


            store.Delete(StackExchangeRedisDataTypesEnum.String, new List <Tuple <string, object> >
            {
                Tuple.Create <string, object>("testKey1_1", null),
                Tuple.Create <string, object>("testKey1_2", null)
            });

            multipleRes = store.Get(StackExchangeRedisDataTypesEnum.String, new List <Tuple <string, object> >
            {
                Tuple.Create <string, object>("testKey1_1", null),
                Tuple.Create <string, object>("testKey1_2", null),
                Tuple.Create <string, object>("testKey1_3", null)
            });

            Assert.AreEqual(3, (int)BinarySerializerHelper.DeserializeObject(multipleRes["testKey1_3"]));
            Assert.AreEqual(1, multipleRes.Count);
        }
        /// <summary>
        /// Creates a new app and a master fork
        /// On success sets the app id
        /// </summary>
        /// <param name="appId"></param>
        /// <param name="name"></param>
        /// <param name="description"></param>
        public void CreateApp(int appId, string name, string description)
        {
            var        bytesAppIds = KeyValueStore.Get(KeyValueStore.DefaultType, KeyGenerator.AppsKey, null);
            List <int> appIds      = null;

            if (bytesAppIds != null)
            {
                appIds = (List <int>)BinarySerializerHelper.DeserializeObject(bytesAppIds);
            }
            else
            {
                appIds = new List <int>();
                KeyValueStore.Set(KeyValueStore.DefaultType, KeyGenerator.AppsKey, BinarySerializerHelper.SerializeObject(appIds), null);
            }

            if (appIds.Contains(appId))
            {
                throw new ArgumentException(nameof(appId));
            }

            appIds.Add(appId);

            KeyValueStore.Set(KeyValueStore.DefaultType, KeyGenerator.AppsKey, BinarySerializerHelper.SerializeObject(appIds), null);

            var res = new App
            {
                Id          = appId,
                Name        = name,
                Description = description
            };

            KeyValueStore.Set(KeyValueStore.DefaultType, KeyGenerator.GenerateAppKey(appId), ProtoBufSerializerHelper.Serialize(res), null);
            KeyValueStore.Set(KeyValueStore.DefaultType, KeyGenerator.GenerateForksKey(appId), BinarySerializerHelper.SerializeObject(new List <int>()), null);

            SetApp(appId);

            CreateMasterFork();
        }
Esempio n. 5
0
        public void StackExchangeRedisKeyValueStore_Hash()
        {
            var store = new StackExchangeRedisKeyValueStore("localhost:6379");

            store.Set(StackExchangeRedisDataTypesEnum.Hash, "testKey2", BinarySerializerHelper.SerializeObject(123), new StackExchangeRedisHashParams {
                HashField = "testKey2Hash"
            });
            var res = (int)BinarySerializerHelper.DeserializeObject(store.Get(StackExchangeRedisDataTypesEnum.Hash, "testKey2", new StackExchangeRedisHashParams {
                HashField = "testKey2Hash"
            }));

            Assert.AreEqual(123, res);

            store.Set(StackExchangeRedisDataTypesEnum.Hash, new List <Tuple <string, byte[], object> >
            {
                Tuple.Create <string, byte[], object>("testKey2_1", BinarySerializerHelper.SerializeObject(4), new StackExchangeRedisHashParams {
                    HashField = "testKey2_1_1"
                }),
                Tuple.Create <string, byte[], object>("testKey2_1", BinarySerializerHelper.SerializeObject(5), new StackExchangeRedisHashParams {
                    HashField = "testKey2_1_2"
                }),
                Tuple.Create <string, byte[], object>("testKey2_1", BinarySerializerHelper.SerializeObject(6), new StackExchangeRedisHashParams {
                    HashField = "testKey2_1_3"
                })
            });

            var multipleRes = store.Get(StackExchangeRedisDataTypesEnum.Hash, new List <Tuple <string, object> >
            {
                Tuple.Create <string, object>("testKey2_1", new StackExchangeRedisHashParams {
                    HashField = "testKey2_1_1"
                }),
                Tuple.Create <string, object>("testKey2_1", new StackExchangeRedisHashParams {
                    HashField = "testKey2_1_2"
                }),
                Tuple.Create <string, object>("testKey2_1", new StackExchangeRedisHashParams {
                    HashField = "testKey2_1_3"
                })
            });

            Assert.AreEqual(4, (int)BinarySerializerHelper.DeserializeObject(multipleRes["testKey2_1_1"]));
            Assert.AreEqual(5, (int)BinarySerializerHelper.DeserializeObject(multipleRes["testKey2_1_2"]));
            Assert.AreEqual(6, (int)BinarySerializerHelper.DeserializeObject(multipleRes["testKey2_1_3"]));


            store.Delete(StackExchangeRedisDataTypesEnum.Hash, new List <Tuple <string, object> >
            {
                Tuple.Create <string, object>("testKey2_1", new StackExchangeRedisHashParams {
                    HashField = "testKey2_1_2"
                }),
                Tuple.Create <string, object>("testKey2_1", new StackExchangeRedisHashParams {
                    HashField = "testKey2_1_3"
                })
            });
            multipleRes = store.Get(StackExchangeRedisDataTypesEnum.Hash, new List <Tuple <string, object> >
            {
                Tuple.Create <string, object>("testKey2_1", new StackExchangeRedisHashParams {
                    HashField = "testKey2_1_1"
                }),
                Tuple.Create <string, object>("testKey2_1", new StackExchangeRedisHashParams {
                    HashField = "testKey2_1_2"
                }),
                Tuple.Create <string, object>("testKey2_1", new StackExchangeRedisHashParams {
                    HashField = "testKey2_1_3"
                })
            });

            Assert.AreEqual(4, (int)BinarySerializerHelper.DeserializeObject(multipleRes["testKey2_1_1"]));
            Assert.AreEqual(1, multipleRes.Count);

            store.Delete(StackExchangeRedisDataTypesEnum.Hash, "testKey2_1", null);
            multipleRes = store.Get(StackExchangeRedisDataTypesEnum.Hash, new List <Tuple <string, object> >
            {
                Tuple.Create <string, object>("testKey2_1", new StackExchangeRedisHashParams {
                    HashField = "testKey2_1_1"
                }),
                Tuple.Create <string, object>("testKey2_1", new StackExchangeRedisHashParams {
                    HashField = "testKey2_1_2"
                }),
                Tuple.Create <string, object>("testKey2_1", new StackExchangeRedisHashParams {
                    HashField = "testKey2_1_3"
                })
            });
            Assert.AreEqual(0, multipleRes.Count);
        }
        public bool Set <T>(TDataTypesEnum type, IEnumerable <Tuple <string, T, object> > values)
        {
            if (Fork.ReadOnly)
            {
                return(false);
            }

            KeyValueStore.Delete(type, values.Select(x => Tuple.Create(KeyGenerator.GenerateForkNullKey(AppId, Fork.Id, x.Item1), x.Item3)));

            var forkedValues = values.Select(x => Tuple.Create(KeyGenerator.GenerateForkValueKey(AppId, Fork.Id, x.Item1), x.Item2 as byte[] ?? BinarySerializerHelper.SerializeObject(x.Item2), x.Item3));

            return(KeyValueStore.Set(type, forkedValues));
        }
        public bool Delete(TDataTypesEnum type, string key, object extraParams = null)
        {
            if (Fork.ReadOnly)
            {
                return(false);
            }

            var res = KeyValueStore.Delete(type, KeyGenerator.GenerateForkValueKey(AppId, Fork.Id, key), extraParams);

            if (Fork.GetAllParents().Any(x => KeyValueStore.Exists(type, KeyGenerator.GenerateForkValueKey(AppId, x.Id, key), extraParams)))
            {
                return(KeyValueStore.Set(type, KeyGenerator.GenerateForkNullKey(AppId, Fork.Id, key), BinarySerializerHelper.SerializeObject(type), extraParams));
            }

            return(res);
        }
 private void SetFork(ForkRawData fork)
 {
     KeyValueStore.Set(KeyValueStore.DefaultType, KeyGenerator.GenerateForkKey(AppId, fork.Id), ProtoBufSerializerHelper.Serialize(fork), null);
     KeyValueStore.Set(KeyValueStore.DefaultType, KeyGenerator.GenerateForkTimeStampKey(AppId, fork.Id), BinarySerializerHelper.SerializeObject(DateTime.UtcNow), null);
 }
 private void SetForkIds(List <int> forkIds)
 {
     KeyValueStore.Set(KeyValueStore.DefaultType, KeyGenerator.GenerateForksKey(AppId), BinarySerializerHelper.SerializeObject(forkIds), null);
 }