Exemplo n.º 1
0
        public static bool TryDecodeKey <T1>(this IKeyEncoder <T1> decoder, Slice encoded, out T1 item)
        {
            var reader = new SliceReader(encoded);

            //TODO: should we fail if extra bytes?
            return(decoder.TryReadKeyFrom(ref reader, out item));
        }
Exemplo n.º 2
0
        public static Slice EncodeKey <T1>([NotNull] this IKeyEncoder <T1> encoder, T1 value)
        {
            var writer = default(SliceWriter);

            encoder.WriteKeyTo(ref writer, value);
            return(writer.ToSlice());
        }
        public static IEnumerable <Slice> EncodeRange <T>(this IKeyEncoder <T> encoder, [NotNull] IEnumerable <T> values)
        {
            if (encoder == null)
            {
                throw new ArgumentNullException("encoder");
            }
            if (values == null)
            {
                throw new ArgumentNullException("values");
            }

            // note: T=>Slice usually is used for writing batches as fast as possible, which means that keys will be consumed immediately and don't need to be streamed

            var array = values as T[];

            if (array != null)
            {             // optimized path for arrays
                return(EncodeRange <T>(encoder, array));
            }

            var coll = values as ICollection <T>;

            if (coll != null)
            {             // optimized path when we know the count
                var slices = new List <Slice>(coll.Count);
                foreach (var value in coll)
                {
                    slices.Add(encoder.EncodeKey(value));
                }
                return(slices);
            }

            // "slow" path
            return(values.Select(value => encoder.EncodeKey(value)));
        }
Exemplo n.º 4
0
 internal TypedKeySubspace(Slice prefix, [NotNull] IKeyEncoder <T1> encoder)
     : base(prefix)
 {
     Contract.Requires(encoder != null);
     this.KeyEncoder = encoder;
     this.Keys       = new TypedKeys <T1>(this, this.KeyEncoder);
 }
 public static IFdbEncoderSubspace <T> CreateEncoder <T>(Slice slice, IKeyEncoder <T> encoder)
 {
     if (encoder == null)
     {
         throw new ArgumentNullException("encoder");
     }
     return(new FdbEncoderSubspace <T>(slice, encoder));
 }
Exemplo n.º 6
0
        public static T1 DecodeKey <T1>([NotNull] this IKeyEncoder <T1> decoder, Slice encoded)
        {
            var reader = new SliceReader(encoded);

            decoder.ReadKeyFrom(ref reader, out T1 item);
            //TODO: should we fail if extra bytes?
            return(item);
        }
Exemplo n.º 7
0
        public static Slice EncodeKey <T1>([NotNull] this IKeyEncoder <T1> encoder, Slice prefix, T1 value)
        {
            var writer = new SliceWriter(prefix.Count + 16);             // ~16 bytes si T1 = Guid

            writer.WriteBytes(in prefix);
            encoder.WriteKeyTo(ref writer, value);
            return(writer.ToSlice());
        }
Exemplo n.º 8
0
 internal TypedKeys(
     [NotNull] TypedKeySubspace <T1> parent,
     [NotNull] IKeyEncoder <T1> encoder)
 {
     Contract.Requires(parent != null && encoder != null);
     this.Parent  = parent;
     this.Encoder = encoder;
 }
 /// <summary>
 /// Create an instance of this service.
 /// </summary>
 /// <param name="mapper">Object mapper.</param>
 /// <param name="objectService">Service class for managing object operations.</param>
 /// <param name="keyEncoder">Service for encoding/decoding an object's unique identifier.</param>
 public DtoService(
     IMapper mapper,
     IObjectService <TEntity, TEntityKey> objectService,
     IKeyEncoder <TDtoKey, TDtoKey> keyEncoder = null)
 {
     Mapper        = mapper;
     ObjectService = objectService;
     KeyEncoder    = keyEncoder;
 }
 internal FdbEncoderSubspace(Slice rawPrefix, bool copy, [NotNull] IKeyEncoder <T> encoder)
     : base(rawPrefix, copy)
 {
     if (encoder == null)
     {
         throw new ArgumentNullException("encoder");
     }
     m_encoder = encoder;
     m_keys    = new FdbEncoderSubspaceKeys <T>(this, encoder);
 }
 public FdbEncoderSubspace([NotNull] FdbSubspace subspace, [NotNull] IKeyEncoder <T> encoder)
     : base(subspace)
 {
     if (subspace == null)
     {
         throw new ArgumentNullException("subspace");
     }
     if (encoder == null)
     {
         throw new ArgumentNullException("encoder");
     }
     m_parent  = subspace;
     m_encoder = encoder;
 }
Exemplo n.º 12
0
        /// <summary>Create a new counter map, using a specific key encoder.</summary>
        public FdbCounterMap(FdbSubspace subspace, IKeyEncoder <TKey> keyEncoder)
        {
            if (subspace == null)
            {
                throw new ArgumentNullException("subspace");
            }
            if (keyEncoder == null)
            {
                throw new ArgumentNullException("keyEncoder");
            }

            this.Subspace   = subspace;
            this.KeyEncoder = keyEncoder;
            this.Location   = new FdbEncoderSubspace <TKey>(subspace, keyEncoder);
        }
        public static IEnumerable <T> DecodeRange <T>(this IKeyEncoder <T> encoder, [NotNull] IEnumerable <Slice> slices)
        {
            if (encoder == null)
            {
                throw new ArgumentNullException("encoder");
            }
            if (slices == null)
            {
                throw new ArgumentNullException("slices");
            }

            // Slice=>T may be filtered in LINQ queries, so we should probably stream the values (so no optimization needed)

            return(slices.Select(slice => encoder.DecodeKey(slice)));
        }
        public T[] DecodeKeys <T>([NotNull] IKeyEncoder <T> keyEncoder)
        {
            if (keyEncoder == null)
            {
                throw new ArgumentNullException("keyEncoder");
            }

            var results = new T[this.Count];

            for (int i = 0; i < results.Length; i++)
            {
                results[i] = keyEncoder.DecodeKey(this.Chunk[i].Key);
            }
            return(results);
        }
        /// <summary>Create a new counter map, using a specific key encoder.</summary>
        public FdbCounterMap([NotNull] IFdbSubspace subspace, [NotNull] IKeyEncoder <TKey> keyEncoder)
        {
            if (subspace == null)
            {
                throw new ArgumentNullException("subspace");
            }
            if (keyEncoder == null)
            {
                throw new ArgumentNullException("keyEncoder");
            }

            this.Subspace   = subspace;
            this.KeyEncoder = keyEncoder;
            this.Location   = subspace.UsingEncoder(keyEncoder);
        }
        public T[] DecodeKeys <T>([NotNull] FdbSubspace subspace, [NotNull] IKeyEncoder <T> keyEncoder)
        {
            if (subspace == null)
            {
                throw new ArgumentNullException("subspace");
            }
            if (keyEncoder == null)
            {
                throw new ArgumentNullException("keyEncoder");
            }

            var results = new T[this.Count];

            for (int i = 0; i < results.Length; i++)
            {
                results[i] = keyEncoder.DecodeKey(subspace.ExtractKey(this.Chunk[i].Key, boundCheck: true));
            }
            return(results);
        }
        public static Slice[] EncodeRange <T>(this IKeyEncoder <T> encoder, [NotNull] T[] values)
        {
            if (encoder == null)
            {
                throw new ArgumentNullException("encoder");
            }
            if (values == null)
            {
                throw new ArgumentNullException("values");
            }

            var slices = new Slice[values.Length];

            for (int i = 0; i < values.Length; i++)
            {
                slices[i] = encoder.EncodeKey(values[i]);
            }
            return(slices);
        }
        public static T[] DecodeRange <T>(this IKeyEncoder <T> encoder, [NotNull] Slice[] slices)
        {
            if (encoder == null)
            {
                throw new ArgumentNullException("encoder");
            }
            if (slices == null)
            {
                throw new ArgumentNullException("slices");
            }

            var values = new T[slices.Length];

            for (int i = 0; i < slices.Length; i++)
            {
                values[i] = encoder.DecodeKey(slices[i]);
            }
            return(values);
        }
        public static T[] DecodeRange <T>(this IKeyEncoder <T> encoder, [NotNull] KeyValuePair <Slice, Slice>[] items)
        {
            if (encoder == null)
            {
                throw new ArgumentNullException("encoder");
            }
            if (items == null)
            {
                throw new ArgumentNullException("items");
            }

            var values = new T[items.Length];

            for (int i = 0; i < items.Length; i++)
            {
                values[i] = encoder.DecodeKey(items[i].Key);
            }
            return(values);
        }
        public static Slice[] EncodeKeys <TKey, TElement>([NotNull] this IKeyEncoder <TKey> encoder, [NotNull] IEnumerable <TElement> elements, Func <TElement, TKey> selector)
        {
            if (encoder == null)
            {
                throw new ArgumentNullException("encoder");
            }
            if (elements == null)
            {
                throw new ArgumentNullException("elements");
            }
            if (selector == null)
            {
                throw new ArgumentNullException("selector");
            }

            TElement[]             arr;
            ICollection <TElement> coll;

            if ((arr = elements as TElement[]) != null)
            {             // fast path for arrays
                return(EncodeKeys <TKey, TElement>(encoder, arr, selector));
            }
            else if ((coll = elements as ICollection <TElement>) != null)
            {             // we can pre-allocate the result array
                var slices = new Slice[coll.Count];
                int p      = 0;
                foreach (var item in coll)
                {
                    slices[p++] = encoder.EncodeKey(selector(item));
                }
                return(slices);
            }
            else
            {             // slow path
                return(elements
                       .Select((item) => encoder.EncodeKey(selector(item)))
                       .ToArray());
            }
        }
        public static Slice[] EncodeRange <TKey, TElement>(this IKeyEncoder <TKey> encoder, [NotNull] TElement[] elements, Func <TElement, TKey> selector)
        {
            if (encoder == null)
            {
                throw new ArgumentNullException("encoder");
            }
            if (elements == null)
            {
                throw new ArgumentNullException("elements");
            }
            if (selector == null)
            {
                throw new ArgumentNullException("selector");
            }

            var slices = new Slice[elements.Length];

            for (int i = 0; i < elements.Length; i++)
            {
                slices[i] = encoder.EncodeKey(selector(elements[i]));
            }
            return(slices);
        }
        public FdbMap([NotNull] string name, [NotNull] IFdbSubspace subspace, [NotNull] IKeyEncoder <TKey> keyEncoder, [NotNull] IValueEncoder <TValue> valueEncoder)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (subspace == null)
            {
                throw new ArgumentNullException("subspace");
            }
            if (keyEncoder == null)
            {
                throw new ArgumentNullException("keyEncoder");
            }
            if (valueEncoder == null)
            {
                throw new ArgumentNullException("valueEncoder");
            }

            this.Name         = name;
            this.Subspace     = subspace;
            this.Location     = subspace.UsingEncoder(keyEncoder);
            this.ValueEncoder = valueEncoder;
        }
        public KeyValuePair <TKey, TValue>[] Decode <TKey, TValue>([NotNull] IKeyEncoder <TKey> keyEncoder, [NotNull] IValueEncoder <TValue> valueEncoder)
        {
            if (keyEncoder == null)
            {
                throw new ArgumentNullException("keyEncoder");
            }
            if (valueEncoder == null)
            {
                throw new ArgumentNullException("valueEncoder");
            }

            var chunk   = this.Chunk;
            var results = new KeyValuePair <TKey, TValue> [chunk.Length];

            for (int i = 0; i < chunk.Length; i++)
            {
                results[i] = new KeyValuePair <TKey, TValue>(
                    keyEncoder.DecodeKey(chunk[i].Key),
                    valueEncoder.DecodeValue(chunk[i].Value)
                    );
            }

            return(results);
        }
Exemplo n.º 24
0
 public IFdbEncoderSubspace <TNext> ByKey <TNext>(T1 value1, T2 value2, T3 value3, T4 value4, [NotNull] IKeyEncoder <TNext> encoder)
 {
     return(FdbSubspace.CreateEncoder <TNext>(this.Subspace.ConcatKey(this.Encoder.EncodeKey(value1, value2, value3, value4)), encoder));
 }
Exemplo n.º 25
0
        public FdbCompressedBitmapIndex([NotNull] string name, [NotNull] FdbSubspace subspace, IEqualityComparer <TValue> valueComparer, bool indexNullValues, [NotNull] IKeyEncoder <TValue> encoder)
        {
            Contract.NotNull(name, nameof(name));
            Contract.NotNull(subspace, nameof(subspace));
            Contract.NotNull(encoder, nameof(encoder));

            this.Name            = name;
            this.Subspace        = subspace;
            this.ValueComparer   = valueComparer ?? EqualityComparer <TValue> .Default;
            this.IndexNullValues = indexNullValues;
            this.Location        = subspace.UsingEncoder(encoder);
        }
 internal TypedKeySubspace(Slice prefix, IKeyEncoder <T1> encoder, ISubspaceContext context)
     : base(prefix, context)
 {
     Contract.Requires(encoder != null);
     this.KeyEncoder = encoder;
 }
 public static ITypedKeySubspace <T> UsingEncoder <T>(this IKeySubspace subspace, IKeyEncoder <T> encoder, ISubspaceContext?context = null)
 {
     Contract.NotNull(subspace);
     Contract.NotNull(encoder);
     return(new TypedKeySubspace <T>(subspace.GetPrefix(), encoder, context ?? subspace.Context));
 }
 public FdbEncoderSubspaceKeys([NotNull] IFdbSubspace subspace, [NotNull] IKeyEncoder <T> encoder)
 {
     Contract.Requires(subspace != null && encoder != null);
     this.Subspace = subspace;
     this.Encoder  = encoder;
 }
Exemplo n.º 29
0
 public IFdbEncoderSubspace <TNext> ByKey <TNext>(T value, [NotNull] IKeyEncoder <TNext> encoder)
 {
     return(FdbSubspace.CreateEncoder <TNext>(this.Subspace.ConcatKey(this.Encoder.EncodeKey(value)), encoder));
 }
Exemplo n.º 30
0
 public FdbEncoderSubspacePartition([NotNull] IFdbSubspace subspace, [NotNull] IKeyEncoder <T> encoder)
 {
     this.Subspace = subspace;
     this.Encoder  = encoder;
 }