public DocumentCollection(PackedFile packedFile, IndexFactory indexFactory, byte[] key, JournalPlayer journalPlayer, JournalWriter journalWriter)
 {
     transactionLockObject = new object();
     EnsureValidKey(key);
     documentType = typeof(TDocument);
     try
     {
         journalPlayer.Play();
         SerializerReflectionCache.AddTypes(typeof(MetadataDocument), documentType);
         this.packedFile    = packedFile;
         this.journalPlayer = journalPlayer;
         this.journalWriter = journalWriter;
         byte[] initializationVector;
         if (packedFile.IsEmpty)
         {
             initializationVector = CryptoRandomNumberGenerator.GenerateBytes(16u);
             journalWriter.Start();
             WriteMetadataDocument(initializationVector);
             journalWriter.Finish();
             journalPlayer.Play();
         }
         else
         {
             KeyValuePair <uint, byte[]> keyValuePair = packedFile.Documents.First();
             metadataDocumentId = keyValuePair.Key;
             MetadataDocument metadataDocument = BinarySerializer.Deserialize <MetadataDocument>(keyValuePair.Value);
             initializationVector = metadataDocument.InitializationVector;
         }
         encryptor    = new Aes256Encryptor(key, initializationVector);
         fieldIndexes = new FieldIndexes <TDocument>(indexFactory, encryptor);
     }
     catch (Exception)
     {
         journalWriter.Discard();
         throw;
     }
 }
Пример #2
0
 public FieldIndexes(IndexFactory indexFactory, Aes256Encryptor encryptor)
 {
     typeReflection = DocumentReflectionCache.GetTypeReflection <TDocument>();
     fieldTypes     = new Dictionary <string, Type>();
     fieldIndexes   = new Dictionary <string, object>();
     boolIndexes    = new Dictionary <string, Index <bool> >();
     sbyteIndexes   = new Dictionary <string, Index <sbyte> >();
     byteIndexes    = new Dictionary <string, Index <byte> >();
     shortIndexes   = new Dictionary <string, Index <short> >();
     ushortIndexes  = new Dictionary <string, Index <ushort> >();
     intIndexes     = new Dictionary <string, Index <int> >();
     uintIndexes    = new Dictionary <string, Index <uint> >();
     longIndexes    = new Dictionary <string, Index <long> >();
     ulongIndexes   = new Dictionary <string, Index <ulong> >();
     floatIndexes   = new Dictionary <string, Index <float> >();
     doubleIndexes  = new Dictionary <string, Index <double> >();
     charIndexes    = new Dictionary <string, Index <char> >();
     stringIndexes  = new Dictionary <string, Index <string> >();
     DocumentFieldReflection[] fieldReflections = typeReflection.FieldReflections;
     foreach (DocumentFieldReflection documentFieldReflection in fieldReflections)
     {
         FieldInfo fieldInfo = documentFieldReflection.FieldInfo;
         string    name      = fieldInfo.Name;
         Type      fieldType = fieldInfo.FieldType;
         fieldTypes.Add(name, fieldType);
         if (fieldType == typeof(bool))
         {
             Index <bool> value = indexFactory.Create <bool>(name, encryptor);
             boolIndexes.Add(name, value);
             fieldIndexes.Add(name, value);
             continue;
         }
         if (fieldType == typeof(sbyte))
         {
             Index <sbyte> value2 = indexFactory.Create <sbyte>(name, encryptor);
             sbyteIndexes.Add(name, value2);
             fieldIndexes.Add(name, value2);
             continue;
         }
         if (fieldType == typeof(byte))
         {
             Index <byte> value3 = indexFactory.Create <byte>(name, encryptor);
             byteIndexes.Add(name, value3);
             fieldIndexes.Add(name, value3);
             continue;
         }
         if (fieldType == typeof(short))
         {
             Index <short> value4 = indexFactory.Create <short>(name, encryptor);
             shortIndexes.Add(name, value4);
             fieldIndexes.Add(name, value4);
             continue;
         }
         if (fieldType == typeof(ushort))
         {
             Index <ushort> value5 = indexFactory.Create <ushort>(name, encryptor);
             ushortIndexes.Add(name, value5);
             fieldIndexes.Add(name, value5);
             continue;
         }
         if (fieldType == typeof(int))
         {
             Index <int> value6 = indexFactory.Create <int>(name, encryptor);
             intIndexes.Add(name, value6);
             fieldIndexes.Add(name, value6);
             continue;
         }
         if (fieldType == typeof(uint))
         {
             Index <uint> value7 = indexFactory.Create <uint>(name, encryptor);
             uintIndexes.Add(name, value7);
             fieldIndexes.Add(name, value7);
             continue;
         }
         if (fieldType == typeof(long))
         {
             Index <long> value8 = indexFactory.Create <long>(name, encryptor);
             longIndexes.Add(name, value8);
             fieldIndexes.Add(name, value8);
             continue;
         }
         if (fieldType == typeof(ulong))
         {
             Index <ulong> value9 = indexFactory.Create <ulong>(name, encryptor);
             ulongIndexes.Add(name, value9);
             fieldIndexes.Add(name, value9);
             continue;
         }
         if (fieldType == typeof(float))
         {
             Index <float> value10 = indexFactory.Create <float>(name, encryptor);
             floatIndexes.Add(name, value10);
             fieldIndexes.Add(name, value10);
             continue;
         }
         if (fieldType == typeof(double))
         {
             Index <double> value11 = indexFactory.Create <double>(name, encryptor);
             doubleIndexes.Add(name, value11);
             fieldIndexes.Add(name, value11);
             continue;
         }
         if (fieldType == typeof(char))
         {
             Index <char> value12 = indexFactory.Create <char>(name, encryptor);
             charIndexes.Add(name, value12);
             fieldIndexes.Add(name, value12);
             continue;
         }
         if (fieldType == typeof(string))
         {
             Index <string> value13 = indexFactory.Create <string>(name, encryptor);
             stringIndexes.Add(name, value13);
             fieldIndexes.Add(name, value13);
             continue;
         }
         throw new FormatException("Unhandled field " + name + " with type " + fieldType);
     }
 }