Beispiel #1
0
 public DatabaseBuilder AddArray <T>(
     IConstSizeSerializer <T> serializer,
     Func <Database, IEnumerable <T> > elements)
     where T : notnull
 {
     return(AddArray(serializer, elements, x => x, null));
 }
Beispiel #2
0
 public override DatabaseBuilder AddArray <T, TKey>(IConstSizeSerializer <T> serializer,
                                                    Func <Database, IEnumerable <T> > elements,
                                                    Func <T, TKey> selector,
                                                    IComparer <TKey>?comparer)
 {
     builder.AddArray(serializer);
     return(this);
 }
Beispiel #3
0
 public DatabaseBuilder AddArray <T, TKey>(
     IConstSizeSerializer <T> serializer,
     Func <Database, IEnumerable <T> > elements,
     Func <T, TKey> keySelector)
     where T : notnull
 {
     return(AddArray(serializer, elements, keySelector, Comparer <TKey> .Default));
 }
 public MappingConstSizeSerializer(
     IConstSizeSerializer <TUnderlyingType> serializer,
     Func <TEnum, TUnderlyingType> toFunc,
     Func <TUnderlyingType, TEnum> fromFunc)
 {
     this.serializer = serializer;
     this.toFunc     = toFunc;
     this.fromFunc   = fromFunc;
 }
Beispiel #5
0
        public override DatabaseBuilder AddArray <T, TKey>(
            IConstSizeSerializer <T> serializer,
            Func <Database, IEnumerable <T> > elements,
            Func <T, TKey> selector,
            IComparer <TKey>?comparer)
        {
            try
            {
                var headerPosition = stream.Position;
                // write a dummy header
                var dummyHeaderBytes = new ArrayHeader(serializer).AsBytes();
                stream.Write(dummyHeaderBytes);
                var elementLength = serializer.ElementSize;
                Utility.EnsureArrayOfMinimalSize(ref buffer, elementLength);
                long elementCount       = 0;
                var  elementsEnumerable = elements(db);
                if (comparer != null)
                {
                    elementsEnumerable = elementsEnumerable.OrderBy(selector, comparer);
                }
                foreach (var element in elementsEnumerable)
                {
                    serializer.TrySerialize(element, buffer.AsSpan(), out _);
                    stream.Write(buffer, 0, elementLength);
                    elementCount++;
                }

                var pastEndPosition = stream.Position;
                stream.Seek(headerPosition, SeekOrigin.Begin);
                var arrayHeader = new ArrayHeader(serializer)
                {
                    OverallLength = elementLength * elementCount,
                    RecordCount   = elementCount,
                    StartsAt      = headerPosition + dummyHeaderBytes.Length,
                    EndsAt        = pastEndPosition,
                    Type          = 1
                };
                stream.Write(arrayHeader.AsBytes());
                stream.Seek(pastEndPosition, SeekOrigin.Begin);
                headers.Add(arrayHeader);
            }
            catch
            {
                stream.Dispose();
                db.Dispose();
                throw;
            }

            return(this);
        }
Beispiel #6
0
 public DatabaseOpeningBuilder AddArray <T>(IConstSizeSerializer <T> serializer)
     where T : notnull
 {
     try
     {
         var header = ReadNextHeader(serializer);
         if (serializer.ElementSize != ClusteredReadOnlyDiskArray <T> .GetRecordLength(header))
         {
             throw new InvalidDataException();
         }
         if (header.Type != 1)
         {
             throw new InvalidDataException();
         }
         headers.Add(header);
         return(this);
     }
     catch
     {
         stream.Dispose();
         throw;
     }
 }
Beispiel #7
0
 public abstract DatabaseBuilder AddArray <T, TKey>(
     IConstSizeSerializer <T> serializer,
     Func <Database, IEnumerable <T> > elements,
     Func <T, TKey> keySelector,
     IComparer <TKey>?comparer)
     where T : notnull;