Esempio n. 1
0
        public IEnumerable <Document> GetEntities(SearchConditions searchConditions, IEnumerable <string> projectedFields, string orderByFieldName, bool orderByDesc)
        {
            string indexKey     = this.GetIndexKey(searchConditions);
            string indexListKey = this.GetIndexListKeyInCache();

            try
            {
                Document[] result = null;

                // if a full index was found
                if (this._redis.HashFieldExistsWithRetries(indexListKey, indexKey))
                {
                    result = RedisIndex.LoadIndexEntities(this._redis, indexKey, indexListKey);
                }
                else if (projectedFields != null)
                {
                    // then trying to use a projection index
                    indexKey = this.GetIndexKey(searchConditions, projectedFields);

                    if (this._redis.HashFieldExistsWithRetries(indexListKey, indexKey))
                    {
                        result = RedisProjectionIndex.LoadProjectionIndexEntities(this._redis, indexKey, indexListKey);
                    }
                }

                // if we failed to load both full and projection index
                if (result == null)
                {
                    this.OnMiss.FireSafely();
                    return(null);
                }

                this.OnHit.FireSafely();
                this.Log("Index ({0}) with {1} items successfully loaded from cache", indexKey, result.Length);

                if (string.IsNullOrEmpty(orderByFieldName))
                {
                    return(result);
                }

                // creating a comparer to sort the results
                var comparer = PrimitiveComparer.GetComparer(this._tableEntityType, orderByFieldName);

                return(orderByDesc
                    ?
                       result.OrderByDescending(doc => doc[orderByFieldName].AsPrimitive(), comparer)
                    :
                       result.OrderBy(doc => doc[orderByFieldName].AsPrimitive(), comparer)
                       );
            }
            catch (Exception ex)
            {
                this.Log("Failed to load index ({0}) from cache. {1}", indexKey, ex);
                this.OnMiss.FireSafely();
                return(null);
            }
        }
Esempio n. 2
0
 private Task <bool> AddToIndex(RedisIndex index, string value, double indexScore)
 {
     return(this._redisDb.SortedSetAddAsync(index.ToString(), value, indexScore));
 }
Esempio n. 3
0
 public Task <bool> AddToIndex(RedisIndex index, UInt256 hash, double indexScore)
 {
     return(this.AddToIndex(index, hash.ToString(), indexScore));
 }
Esempio n. 4
0
        public async Task <UInt256> GetFromHashIndex(RedisIndex index, double indexScore)
        {
            var values = (await _redisDb.SortedSetRangeByScoreAsync(index.ToString(), indexScore, indexScore)).ToStringArray();

            return(values.Any() ? UInt256.Parse(values.First()) : null);
        }
Esempio n. 5
0
 public IndexAttribute(RedisIndex index)
 {
     Index = index;
 }
Esempio n. 6
0
 /// <summary>
 /// Retrieves the length / number of elements in an index
 /// </summary>
 /// <param name="index">Index to retrieve the length of</param>
 /// <returns></returns>
 public long GetIndexLength(RedisIndex index)
 {
     return(_redisDb.SortedSetLength(index.ToString()));
 }
Esempio n. 7
0
 /// <summary>
 /// Retrieves a specified range of values from a Redis Sorted Set based on a range of scores
 /// </summary>
 /// <param name="index">Index to search</param>
 /// <param name="startIndexScore">Range starting score</param>
 /// <param name="endIndexScore">Range end score</param>
 /// <returns></returns>
 public RedisValue[] GetRangeFromIndex(RedisIndex index, double startIndexScore, double endIndexScore)
 {
     return(_redisDb.SortedSetRangeByScore(index.ToString(), startIndexScore, endIndexScore));
 }
Esempio n. 8
0
 /// <summary>
 /// Retrieves a specified value from a Redis Sorted Set based on score.
 /// </summary>
 /// <param name="index">Index to search</param>
 /// <param name="indexScore">Score to retrieve.</param>
 /// <returns></returns>
 public RedisValue[] GetFromIndex(RedisIndex index, double indexScore)
 {
     return(_redisDb.SortedSetRangeByScore(index.ToString(), indexScore, indexScore));
 }
Esempio n. 9
0
 /// <summary>
 /// Adds a specified value and score to a Redis Sorted Set.  Using a Redis Sorted Set will give us a O(log(n)) complexity on search.
 /// </summary>
 /// <param name="index">Index to write</param>
 /// <param name="indexScore">Score to assign</param>
 /// <param name="value">Value associated with score</param>
 public void AddToIndex(RedisIndex index, double indexScore, string value)
 {
     _redisDb.SortedSetAdd(index.ToString(), value, indexScore);
 }