public override int GetHashCode()
        {
            int result = RepositoryKey.GetHashCode();

            result = 31 * result + ItemPath.GetHashCode();
            return(result);
        }
Пример #2
0
        private RepositoryKey GetUserKey(string id)
        {
            RepositoryKey userKey = new RepositoryKey(this, new ObjectKey("User", id));

            userKey.AddIndex(new IndexKey(this, "Index:Users:All", false));
            return(userKey);
        }
Пример #3
0
        public async Task TestIdentity()
        {
            Redis.PersistencyRegistration.RegisterHashsetSingle <Identity>();

            var key1 = new RepositoryKey(Repository.Object, new ObjectKey("Test1"));

            key1.AddIndex(ListAll);

            var items = Redis.Client.GetRecords <Identity>(ListAll).ToEnumerable().ToArray();

            Assert.AreEqual(0, items.Length);

            var identity = new Identity();

            identity.Environment = "Ev";
            await Redis.Client.AddRecord(key1, identity).ConfigureAwait(false);

            var key2 = new RepositoryKey(Repository.Object, new ObjectKey("Test2"));

            key2.AddIndex(ListAll);
            await Redis.Client.AddRecord(key2, identity).ConfigureAwait(false);

            items = Redis.Client.GetRecords <Identity>(ListAll).ToEnumerable().ToArray();
            Assert.AreEqual(2, items.Length);
            Assert.AreEqual("Ev", items[0].Environment);

            var count = await Redis.Client.Count(ListAll).ConfigureAwait(false);

            Assert.AreEqual(2, count);
        }
Пример #4
0
        private RepositoryKey GetProductKey(string id)
        {
            RepositoryKey productKey = new RepositoryKey(this, new ObjectKey("Product", id));

            productKey.AddIndex(new IndexKey(this, "Index:Products:All", false));
            return(productKey);
        }
Пример #5
0
        public void TestEqual()
        {
            var key1 = new RepositoryKey(repository, new ObjectKey("Test"));
            var key2 = new RepositoryKey(repository, new ObjectKey("Test"));

            Assert.AreEqual(key1, key2);
        }
Пример #6
0
        public async Task AddUpdate()
        {
            Redis.PersistencyRegistration.RegisterHashsetSingle <Identity>();
            RepositoryKey.AddIndex(new IndexKey(Repository.Object, "Data", true));
            await Redis.Client.DeleteAll <Identity>(RepositoryKey).ConfigureAwait(false);

            await Redis.Client.AddRecord(RepositoryKey, Routing).ConfigureAwait(false);

            await Redis.Client.DeleteAll <Identity>(RepositoryKey).ConfigureAwait(false);

            await Redis.Client.AddRecord(RepositoryKey, Routing).ConfigureAwait(false);
        }
Пример #7
0
        public async Task TestMultiple(int type, int total)
        {
            SetupPersistency(type);
            var key1 = new RepositoryKey(Repository.Object, new ObjectKey("Test1"));
            await Redis.Client.AddRecord(key1, new Identity { Environment = "Test1" }).ConfigureAwait(false);

            await Redis.Client.AddRecord(key1, new Identity { Environment = "Test2" }).ConfigureAwait(false);

            await Redis.Client.AddRecord(key1, new Identity { Environment = "Test3" }).ConfigureAwait(false);

            var items = Redis.Client.GetRecords <Identity>(key1).ToEnumerable().OrderBy(item => item.Environment).ToArray();

            Assert.AreEqual(total, items.Length);
        }
Пример #8
0
        public async Task TestNullUser()
        {
            await persistency.Save(tweet).ConfigureAwait(false);

            var user = await persistency.LoadUser(tweet.CreatedBy.Id).ConfigureAwait(false);

            cache.Remove($"User{user.User.Id}");
            cache.Remove($"{user.User.Id}");
            string[] key    = { "User", user.User.Id.ToString() };
            var      repKey = new RepositoryKey(persistency, new ObjectKey(key));
            await link.Client.DeleteAll <TweetUser>(repKey).ConfigureAwait(false);

            var message = persistency.LoadAll().Catch(Observable.Empty <MessageItem>()).ToEnumerable().ToArray();
        }
Пример #9
0
        public void Save(AmazonReview amazon, Dictionary <string, RedisValue> review)
        {
            var value = new Dictionary <string, string>();

            foreach (var redisValue in review)
            {
                value[redisValue.Key] = redisValue.Value.ToString();
            }

            var dataKey = new RepositoryKey(this, new ObjectKey(amazon.Id));

            dataKey.AddIndex(new IndexKey(this, "All", false));
            manager.Client.AddRecord(dataKey, value);
        }
Пример #10
0
        public async Task <LightDocument> GetCached(IDocument original)
        {
            if (original is null)
            {
                throw new ArgumentNullException(nameof(original));
            }

            if (string.IsNullOrEmpty(original.Id))
            {
                throw new ArgumentException("Value cannot be null or empty id.", nameof(original.Id));
            }

            var result = await local.GetCached(original).ConfigureAwait(false);

            if (result != null)
            {
                log.LogDebug("Found in local cache");
                return(result);
            }

            var key = new RepositoryKey(this, new ObjectKey(original.Id));

            result = await manager.Client.GetRecords <LightDocument>(key).LastOrDefaultAsync();

            if (result != null)
            {
                if (result.Text == original.Text)
                {
                    await local.Save(result).ConfigureAwait(false);

                    return(result);
                }

                log.LogWarning("Mistmatch in document text: {0}", original.Id);
            }

            result = await GetById(original.GetId(), original).ConfigureAwait(false);

            if (result != null)
            {
                return(result);
            }

            return(await GetById(original.GetTextId(), original).ConfigureAwait(false));
        }
Пример #11
0
        public async Task Single()
        {
            Redis.PersistencyRegistration.RegisterHashsetSingle <Identity>();
            Routing.ApplicationId = null;
            await Redis.Client.AddRecord(RepositoryKey, Routing).ConfigureAwait(false);

            RepositoryKey.AddIndex(new IndexKey(Repository.Object, "Data", true));
            await Redis.Client.AddRecord(RepositoryKey, Routing).ConfigureAwait(false);

            var result = await Redis.Client.GetRecords <Identity>(RepositoryKey).ToArray();

            var result2 = await Redis.Client.GetRecords <Identity>(new IndexKey(Repository.Object, "Data", true)).ToArray();

            await Redis.Client.DeleteAll <Identity>(RepositoryKey).ConfigureAwait(false);

            Assert.AreEqual(1, result.Length);
            Assert.AreEqual(1, result2.Length);
            Assert.IsTrue(string.IsNullOrEmpty(result[0].ApplicationId));
            Assert.AreEqual("DEV", result[0].Environment);
        }
Пример #12
0
        private async Task <(IDataKey Key, string Command, T Instance)> Convert(ChannelMessage message)
        {
            if (string.Compare(message.Message, "expire", StringComparison.OrdinalIgnoreCase) == 0)
            {
                return(null, message.Message, null);
            }

            var receivedKey = message.Channel.ToString();
            var start       = receivedKey.IndexOf(FieldConstants.ObjectTag) + 8;

            if (start < 0)
            {
                logger.LogWarning("Bad key: {0}", receivedKey);
                return(null, message.Message, null);
            }

            receivedKey = receivedKey.Substring(start);
            var keyItem  = new RepositoryKey(repository, new ObjectKey(receivedKey));
            var instance = await repository.Redis.Client.GetRecords <T>(keyItem).LastOrDefaultAsync();

            return(keyItem, message.Message, instance);
        }
Пример #13
0
        /// <summary>
        /// Gets an xml repository for the specified entity type. If the repository has not been
        /// created yet, a new one is created; otherwise, the existing one is returned.
        /// e.g. RepositoryFor{Test}.WithIdentity(p => p.Name) for a entity named 'Test' and a identity property named 'Name' which is typed as string.
        /// </summary>
        /// <typeparam name="TEntity">The entity type.</typeparam>
        /// <typeparam name="TIdentity">The identity type.</typeparam>
        /// <param name="repositoryModifier">The selector for entity type and identity type.</param>
        /// <returns>An xml repository.</returns>
        public static IXmlRepository <TEntity, TIdentity> Get <TEntity, TIdentity>(IRepositoryModifier <TEntity, TIdentity> repositoryModifier) where TEntity : class, new()
        {
            lock (LockObject)
            {
                var key = new RepositoryKey
                {
                    RepositoryType   = typeof(IXmlRepository <TEntity, TIdentity>),
                    DataProvider     = repositoryModifier.DataProvider ?? DataProvider,
                    PropertyMappings = repositoryModifier.PropertyMappings ?? PropertyMappings,
                    QueryProperty    = repositoryModifier.QueryProperty
                };

                IXmlRepository <TEntity, TIdentity> repository;

                if (!Repositories.ContainsKey(key))
                {
                    repository     = new XmlRepository <TEntity, TIdentity>(repositoryModifier);
                    repository.Key = key;

                    Repositories.Add(key, repository);

                    // Add default mappings.
                    if (repositoryModifier.PropertyMappings != null)
                    {
                        AddDefaultPropertyMappingsFor(typeof(TEntity), repositoryModifier.PropertyMappings);
                    }
                    {
                        AddDefaultPropertyMappingsFor(typeof(TEntity));
                    }
                }
                else
                {
                    repository = Repositories[key] as IXmlRepository <TEntity, TIdentity>;
                }

                // Return the requested repository to the caller.
                return(repository);
            }
        }
Пример #14
0
        public virtual async Task Setup()
        {
            redisInstance = new RedisInside.Redis(i => i.Port(6666).LogTo(item => Global.Logger.LogDebug(item)));
            var config   = XDocument.Load(Path.Combine(TestContext.CurrentContext.TestDirectory, @"Config\redis.config")).XmlDeserialize <RedisConfiguration>();
            var provider = new ModuleHelper(config).Provider;

            Redis = await provider.GetService <IAsyncServiceFactory <IRedisLink> >().GetService(true);

            Redis.Multiplexer.Flush();

            Resilience            = provider.GetService <IResilience>();
            Key                   = new ObjectKey("Key1");
            Routing               = new Identity();
            Routing.ApplicationId = "Test";
            Routing.Environment   = "DEV";
            Repository            = new Mock <ILimitedSizeRepository>();
            Repository.Setup(item => item.Name).Returns("Test");
            Repository.Setup(item => item.Size).Returns(2);
            RepositoryKey = new RepositoryKey(Repository.Object, Key);
            ListAll       = new IndexKey(Repository.Object, "All", true);
            ListAll2      = new IndexKey(Repository.Object, "All2", true);
        }
Пример #15
0
        public async Task <bool> Save(LightDocument document)
        {
            if (document is null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            if (string.IsNullOrEmpty(document.Id))
            {
                throw new ArgumentException("Value cannot be null or empty id.", nameof(document.Id));
            }

            await local.Save(document).ConfigureAwait(false);

            var key = new RepositoryKey(this, new ObjectKey(document.Id));

            key.AddIndex(new IndexKey(this, "Index:All", false));
            key.AddIndex(new IndexKey(this, $"Index:{document.GetId()}", true));
            key.AddIndex(new IndexKey(this, $"Index:{document.GetTextId()}", true));
            await manager.Client.AddRecord(key, document).ConfigureAwait(false);

            return(true);
        }
Пример #16
0
        public async Task AddToLimitedListTransaction()
        {
            var transaction = Redis.StartTransaction();

            RepositoryKey.AddIndex(ListAll);
            var result = await Redis.Client.ContainsRecord <string>(RepositoryKey).ConfigureAwait(false);

            Assert.IsFalse(result);
            var task1 = transaction.Client.AddRecord(RepositoryKey, "Test1");
            var task2 = transaction.Client.AddRecord(RepositoryKey, "Test2");
            var task3 = transaction.Client.AddRecord(RepositoryKey, "Test3");
            await transaction.Commit().ConfigureAwait(false);

            await Task.WhenAll(task1, task2, task3).ConfigureAwait(false);

            result = await Redis.Client.ContainsRecord <string>(RepositoryKey).ConfigureAwait(false);

            Assert.IsTrue(result);
            var value = await Redis.Client.GetRecords <string>(RepositoryKey).ToArray();

            Assert.AreEqual(2, value.Length);
            Assert.AreEqual("Test2", value[0]);
            Assert.AreEqual("Test3", value[1]);
        }
Пример #17
0
        public async Task TestIndex(int type, int batchSize)
        {
            Redis.Client.BatchSize = batchSize;
            SetupPersistency(type);

            var key1 = new RepositoryKey(Repository.Object, new ObjectKey("Test1"));

            key1.AddIndex(ListAll);
            key1.AddIndex(ListAll2);
            await Redis.Client.AddRecord(key1, new Identity { Environment = "Test1" }).ConfigureAwait(false);

            var key2 = new RepositoryKey(Repository.Object, new ObjectKey("Test2"));

            key2.AddIndex(ListAll);
            key2.AddIndex(ListAll2);
            await Redis.Client.AddRecord(key2, new Identity { Environment = "Test2" }).ConfigureAwait(false);

            var key3 = new RepositoryKey(Repository.Object, new ObjectKey("Test3"));

            key3.AddIndex(ListAll);
            await Redis.Client.AddRecord(key3, new Identity { Environment = "Test3" }).ConfigureAwait(false);

            var items = Redis.Client.GetRecords <Identity>(ListAll).ToEnumerable().OrderBy(item => item.Environment).ToArray();

            Assert.AreEqual(3, items.Length);
            Assert.AreEqual("Test1", items[0].Environment);
            Assert.AreEqual("Test2", items[1].Environment);
            Assert.AreEqual("Test3", items[2].Environment);

            items = Redis.Client.GetRecords <Identity>(ListAll2).ToEnumerable().OrderBy(item => item.Environment).ToArray();
            Assert.AreEqual(2, items.Length);
            Assert.AreEqual("Test1", items[0].Environment);
            Assert.AreEqual("Test2", items[1].Environment);

            items = await Redis.Client.GetRecords <Identity>(ListAll, 0, 0).ToArray();

            Assert.AreEqual(1, items.Length);
            Assert.AreEqual("Test3", items[0].Environment);

            items = Redis.Client.GetRecords <Identity>(ListAll, 1, 3).ToEnumerable().OrderBy(item => item.Environment).ToArray();
            Assert.AreEqual(2, items.Length);
            Assert.AreEqual("Test1", items[0].Environment);
            Assert.AreEqual("Test2", items[1].Environment);

            items = await Redis.Client.GetRecords <Identity>(ListAll, 3, 4).ToArray();

            Assert.AreEqual(0, items.Length);

            var factory = new IndexManagerFactory(new NullLoggerFactory(), Redis);
            var manager = factory.Create(ListAll);
            var count   = await manager.Count(Redis.Database, ListAll).ConfigureAwait(false);

            Assert.AreEqual(3, count);

            count = await Redis.Client.Count(ListAll).ConfigureAwait(false);

            Assert.AreEqual(3, count);

            count = (await manager.GetIds(Redis.Database, ListAll).ToArray()).Length;
            Assert.AreEqual(3, count);

            var result = await manager.GetIds(Redis.Database, ListAll, 1, 2).ToArray();

            Assert.AreEqual("Test2", (string)result[0]);
            result = await manager.GetIds(Redis.Database, ListAll, 0, 1).ToArray();

            Assert.AreEqual("Test3", (string)result[0]);
            await manager.Reset(Redis.Database, ListAll).ConfigureAwait(false);

            count = await manager.Count(Redis.Database, ListAll).ConfigureAwait(false);

            Assert.AreEqual(0, count);

            await Redis.Reindex(new NullLoggerFactory(), key2).ConfigureAwait(false);

            count = await manager.Count(Redis.Database, ListAll).ConfigureAwait(false);

            Assert.AreEqual(3, count);
        }
Пример #18
0
        public void Construct()
        {
            var key = new RepositoryKey(repository, new ObjectKey("Test"));

            Assert.AreEqual("Test1:object:Test", key.FullKey);
        }
Пример #19
0
        private RepositoryKey GetAmazonDataKey(string id)
        {
            RepositoryKey userKey = new RepositoryKey(this, new ObjectKey("Text", id));

            return(userKey);
        }