Example #1
0
 /// <summary>
 ///   Opens the stream for writing
 /// </summary>
 /// <param name = "stream"></param>
 public void Open(Stream stream)
 {
     _stream = stream;
     _cache  = new List <WriteCommand>();
     _types  = new IndexGenerator <Type>();
     _names  = new IndexGenerator <string>();
 }
Example #2
0
 /// <summary>
 ///   Opens the stream for writing
 /// </summary>
 /// <param name = "stream"></param>
 public void Open(Stream stream)
 {
     _stream = stream;
     _cache = new List<WriteCommand>();
     _types = new IndexGenerator<Type>();
     _names = new IndexGenerator<string>();
 }
Example #3
0
        public static int Run(string[] args)
        {
            var sw = Stopwatch.StartNew();

            int loadResult = LoadOptions(args);

            if (loadResult != 0)
            {
                return(loadResult);
            }

            _basePath = AbsolutePath(_options.Source);
            AppDomain.CurrentDomain.AssemblyResolve += ResolveAssembly;            // Look in the base path for referenced .dll files instead of the startup path

            Console.Write("Scanning for DTO objects in {0}...  ", _basePath);
            var apiControllers   = GetApiControllers(_basePath);
            var controllerModels = new HashSet <Type>(apiControllers.SelectMany(GetModelsFromController));           // return and parameter models
            var allModels        = GetAllModelsToGenerate(controllerModels);

            Console.WriteLine("Found {0}", allModels.Count);

            var targetPath = AbsolutePath(_options.Destination);
            // Invoke all generators and pass the results to the index generator
            var allGeneratedNames = IndexGenerator.Generate(targetPath,
                                                            EntityGenerator.Generate(targetPath, allModels, _options),
                                                            DataServiceGenerator.Generate(apiControllers, controllerModels, targetPath, _options)
                                                            );

            RemoveNonGeneratedFiles(targetPath, allGeneratedNames);

            Console.WriteLine("Done in {0:N3}s", sw.Elapsed.TotalSeconds);
            return(0);
        }
        private ISyntax GetTypeToHash()
        {
            var tree    = new TreeSyntaxNode();
            var dicBody = new TreeSyntaxNode();

            tree.Add(new ParagraphSyntax());
            tree.Add(new TabSimpleSyntax(2, "private Dictionary<Type, int> GetTypeToHash()"));
            tree.Add(new LeftScopeSyntax(2));
            tree.Add(new TabSimpleSyntax(3, "return new Dictionary<Type, int>()"));
            tree.Add(new LeftScopeSyntax(3));
            tree.Add(dicBody);
            tree.Add(new RightScopeSyntax(3, true));
            tree.Add(new RightScopeSyntax(2));

            for (int i = 0; i < componentTypes.Count; i++)
            {
                var interfaces = componentTypes[i].GetInterfaces();
                var hash       = IndexGenerator.GetIndexForType(componentTypes[i]);

                foreach (var @interface in interfaces)
                {
                    if (@interface.Name.Contains(componentTypes[i].Name))
                    {
                        dicBody.Add(new TabSimpleSyntax(4, $"{{ typeof({@interface.Name}), {hash} }},"));
                    }
                }

                dicBody.Add(new TabSimpleSyntax(4, $"{{ typeof({componentTypes[i].Name}), {hash} }},"));
            }

            return(tree);
        }
Example #5
0
        /// <summary>
        /// Generates a blinding polynomial using an IndexGenerator
        /// </summary>
        ///
        /// <param name="Ig">An Index Generator</param>
        /// <param name="N">The number of coefficients</param>
        /// <param name="Dr">The number of ones / negative ones</param>
        ///
        /// <returns>A blinding polynomial</returns>
        public static SparseTernaryPolynomial GenerateBlindingPoly(IndexGenerator Ig, int N, int Dr)
        {
            int[] coeffs = new int[N];   // an IntegerPolynomial-style representation of the new polynomial

            int[] ones = new int[Dr];
            int   i    = 0;

            while (i < Dr)
            {
                int r = Ig.NextIndex();
                if (coeffs[r] == 0)
                {
                    ones[i]   = r;
                    coeffs[r] = 1;
                    i++;
                }
            }

            int[] negOnes = new int[Dr];
            i = 0;

            while (i < Dr)
            {
                int r = Ig.NextIndex();
                if (coeffs[r] == 0)
                {
                    negOnes[i] = r;
                    coeffs[r]  = -1;
                    i++;
                }
            }

            return(new SparseTernaryPolynomial(N, ones, negOnes));
        }
Example #6
0
        /// <summary>
        /// Deterministically generates a blinding polynomial from a seed and a message representative
        /// </summary>
        ///
        /// <param name="Seed">The seed value</param>
        ///
        /// <returns>A blinding polynomial</returns>
        private IPolynomial GenerateBlindingPoly(byte[] Seed)
        {
            int            N  = _encParams.N;
            IndexGenerator ig = new IndexGenerator(Seed, _encParams);

            if (_encParams.PolyType == TernaryPolynomialType.PRODUCT) //.8, .6
            {
                SparseTernaryPolynomial r1 = SparseTernaryPolynomial.GenerateBlindingPoly(ig, N, _encParams.DR1);
                SparseTernaryPolynomial r2 = SparseTernaryPolynomial.GenerateBlindingPoly(ig, N, _encParams.DR2);
                SparseTernaryPolynomial r3 = SparseTernaryPolynomial.GenerateBlindingPoly(ig, N, _encParams.DR3);

                return(new ProductFormPolynomial(r1, r2, r3));
            }
            else
            {
                if (_encParams.Sparse)
                {
                    return(SparseTernaryPolynomial.GenerateBlindingPoly(ig, N, _encParams.DR));
                }
                else
                {
                    return(DenseTernaryPolynomial.GenerateBlindingPoly(ig, N, _encParams.DR));
                }
            }
        }
Example #7
0
 /// <summary>
 /// Initializes the <see cref="RegistryBase"/>.
 /// </summary>
 /// <param name="indexGenerator">The <see cref="IndexGenerator"/> to use for the new instance.</param>
 /// <param name="keys">The <see cref="IDictionary{TKey,TValue}"/> to use for the new instance.</param>
 protected RegistryBase(IndexGenerator indexGenerator, IDictionary <uint, VirtualRegistryKey> keys)
 {
     _indexGenerator          = indexGenerator;
     _keys                    = keys;
     _keysSynchronizationLock = new ReaderWriterLockSlim();
     _keyAliases              = new Dictionary <uint, uint>();
 }
        public void SingleProductWithSingleWordName()
        {
            var indexGenerator = new IndexGenerator <Product, int>(
                new NonNullImmutableList <ContentRetriever <Product, int> >(new[]
            {
                new ContentRetriever <Product, int>(
                    p => new PreBrokenContent <int>(p.Key, p.Name),
                    token => 1f
                    )
            }),
                new DefaultEqualityComparer <int>(),
                new CaseInsensitiveStringNormaliser(),
                new WhiteSpaceTokenBreaker(),
                weightedValues => weightedValues.Sum(),
                captureSourceLocations: true,
                logger: new NullLogger()
                );
            var index = indexGenerator.Generate(new NonNullImmutableList <Product>(new[]
            {
                new Product()
                {
                    Key = 1, Name = "Product"
                }
            }));

            var expected = new NonNullImmutableList <WeightedEntry <int> >(new[]
            {
                new WeightedEntry <int>(1, 1f, (new[] { new SourceFieldLocation(0, 0, 0, 7, 1f) }).ToNonNullImmutableList())
            });

            EnsureIndexDataMatchesExpectations(
                expected,
                index.GetMatches("Product")
                );
        }
 private void Setup()
 {
     _seed = new byte[100];
     new Random().NextBytes(_seed);
     _parameters = NTRUParamSets.APR2011743;
     _gen        = new IndexGenerator(_seed, _parameters);
     _indices    = initIndices();
 }
 private void Repeatability()
 {
     _gen = new IndexGenerator(_seed, _parameters);
     int[] indices2 = initIndices();
     if (!Compare.AreEqual(_indices, indices2))
     {
         throw new Exception("IndexGenerator repeatability test failed!");
     }
 }
Example #11
0
        public void Initialize()
        {
            var indexGenerator = new IndexGenerator();

            // Reserve the first 20 indices for static virtual keys.
            indexGenerator.ExcludedRanges.Add(new IndexRange(0, 20));
            // Reserved indices for registry rootkeys.
            indexGenerator.ExcludedRanges.Add(new IndexRange(0x80000000, 0x80000006));
            var engineRules = _dataSource.GetRegistryEngineRules();
            var knownKeys   = new ObservableDictionary <uint, VirtualRegistryKey>();

            _dataSource.SynchronizeRegistryWith(knownKeys);
            _switch = new RegistrySwitch(indexGenerator, knownKeys, engineRules);
        }
        private ISyntax GetHMaskBody()
        {
            var tree = new TreeSyntaxNode();

            for (int i = 0; i < componentTypes.Count; i++)
            {
                var className = componentTypes[i].Name.ToLower();
                var classType = componentTypes[i];
                var hash      = IndexGenerator.GetIndexForType(classType);
                tree.Add(new TabSimpleSyntax(4, $"{className} = {GetNewComponentSolved(classType, i, ComponentsCount())}"));
            }

            return(tree);
        }
        private ISyntax GetComponentForTypeMap(int index, int fieldCount, Type c)
        {
            var composite = new TreeSyntaxNode();
            var MaskPart  = new TreeSyntaxNode();
            var maskBody  = new TreeSyntaxNode();

            composite.Add(new ParagraphSyntax());
            composite.Add(new TabSpaceSyntax(3));
            composite.Add(new SimpleSyntax(CParse.LeftScope));
            composite.Add(new CompositeSyntax(new SimpleSyntax(CParse.Space + IndexGenerator.GetIndexForType(c).ToString() + CParse.Comma)));
            composite.Add(new SimpleSyntax($" new ComponentMaskAndIndex {{ComponentName = {CParse.Quote}{ c.Name }{(CParse.Quote)}, ComponentsMask = new {typeof(HECSMask).Name}"));
            composite.Add(new ParagraphSyntax());
            composite.Add(MaskPart);
            composite.Add(new CompositeSyntax(new TabSpaceSyntax(3), new SimpleSyntax("}},")));
            composite.Add(new ParagraphSyntax());

            MaskPart.Add(new LeftScopeSyntax(4));
            MaskPart.Add(maskBody);
            MaskPart.Add(new RightScopeSyntax(4));

            var maskSplitToArray = CalculateIndexesForMask(index, fieldCount);

            maskBody.Add(new TabSimpleSyntax(5, $"Index = {index},"));

            for (int i = 0; i < fieldCount; i++)
            {
                if (maskSplitToArray[fieldCount - 1] > 1 && i < fieldCount - 1)
                {
                    maskBody.Add(new CompositeSyntax(new TabSpaceSyntax(5), new SimpleSyntax($"Mask0{i + 1} = 1ul << {0},")));
                    maskBody.Add(new ParagraphSyntax());
                    continue;
                }

                maskBody.Add(new CompositeSyntax(new TabSpaceSyntax(5), new SimpleSyntax($"Mask0{i + 1} = 1ul << {maskSplitToArray[i]},")));

                if (i > fieldCount - 1)
                {
                    continue;
                }

                maskBody.Add(new ParagraphSyntax());
            }

            return(composite);
        }
        public void IfTheFirstContentRetrieverReturnsNoContentThenNoSourceFieldIndexZeroSourceLocationsAreReturned()
        {
            var indexGenerator = new IndexGenerator <ProductWithDescription, int>(
                new NonNullImmutableList <ContentRetriever <ProductWithDescription, int> >(new[]
            {
                new ContentRetriever <ProductWithDescription, int>(
                    p => new PreBrokenContent <int>(p.Key, p.Name),
                    token => 1f
                    ),
                new ContentRetriever <ProductWithDescription, int>(
                    p => new PreBrokenContent <int>(p.Key, p.Description),
                    token => 1f
                    )
            }),
                new DefaultEqualityComparer <int>(),
                new CaseInsensitiveStringNormaliser(),
                new WhiteSpaceTokenBreaker(),
                weightedValues => weightedValues.Sum(),
                captureSourceLocations: true,
                logger: new NullLogger()
                );
            var index = indexGenerator.Generate(new NonNullImmutableList <ProductWithDescription>(new[]
            {
                new ProductWithDescription()
                {
                    Key = 1, Name = "", Description = "Product"
                }
            }));

            var expected = new NonNullImmutableList <WeightedEntry <int> >(new[]
            {
                new WeightedEntry <int>(
                    1,
                    1f,
                    (new[]
                {
                    new SourceFieldLocation(1, 0, 0, 7, 1f)                              // Match in Description field (source field index 1)
                }).ToNonNullImmutableList())
            });

            EnsureIndexDataMatchesExpectations(
                expected,
                index.GetMatches("Product")
                );
        }
        private ISyntax GetSystemsByHashCode()
        {
            var tree = new TreeSyntaxNode();

            for (int i = 0; i < systems.Count; i++)
            {
                if (i > 0)
                {
                    tree.Add(new ParagraphSyntax());
                }

                var component = systems[i];

                tree.Add(new TabSimpleSyntax(4, $"case {IndexGenerator.GetIndexForType(component)}:"));
                tree.Add(new TabSimpleSyntax(5, $"return new {component.Name}();"));
            }

            return(tree);
        }
Example #16
0
        /// <summary>
        /// Generates an <c>int</c> array containing <c>dr</c> elements equal to <c>1</c>
        /// and <c>dr</c> elements equal to <c>-1</c> using an index generator.
        /// </summary>
        ///
        /// <param name="Ig">An Index Generator</param>
        /// <param name="N">The number of coefficients</param>
        /// <param name="Dr">The number of ones / negative ones</param>
        ///
        /// <returns>An array containing numbers between <c>-1</c> and <c>1</c></returns>
        private static int[] GenerateBlindingCoeffs(IndexGenerator Ig, int N, int Dr)
        {
            int[] r = new int[N];
            for (int coeff = -1; coeff <= 1; coeff += 2)
            {
                int t = 0;
                while (t < Dr)
                {
                    int i = Ig.NextIndex();
                    if (r[i] == 0)
                    {
                        r[i] = coeff;
                        t++;
                    }
                }
            }

            return(r);
        }
        private ISyntax GetHashToTypeDictionary()
        {
            var tree = new TreeSyntaxNode();

            var dicBody = new TreeSyntaxNode();

            tree.Add(new ParagraphSyntax());
            tree.Add(new TabSimpleSyntax(2, "private Dictionary<int, Type> GetHashToTypeDictionary()"));
            tree.Add(new LeftScopeSyntax(2));
            tree.Add(new TabSimpleSyntax(3, "return new Dictionary<int, Type>()"));
            tree.Add(new LeftScopeSyntax(3));
            tree.Add(dicBody);
            tree.Add(new RightScopeSyntax(3, true));
            tree.Add(new RightScopeSyntax(2));

            for (int i = 0; i < componentTypes.Count; i++)
            {
                var hash = IndexGenerator.GetIndexForType(componentTypes[i]);
                dicBody.Add(new TabSimpleSyntax(4, $"{{ {hash}, typeof({componentTypes[i].Name})}},"));
            }

            return(tree);
        }
        public void TestRemovalOfResultFromIndex()
        {
            var indexGenerator = new IndexGenerator <ProductWithDescription, int>(
                new NonNullImmutableList <ContentRetriever <ProductWithDescription, int> >(new[]
            {
                new ContentRetriever <ProductWithDescription, int>(
                    p => new PreBrokenContent <int>(p.Key, p.Name),
                    token => 1f
                    ),
                new ContentRetriever <ProductWithDescription, int>(
                    p => new PreBrokenContent <int>(p.Key, p.Description),
                    token => 1f
                    )
            }),
                new DefaultEqualityComparer <int>(),
                new CaseInsensitiveStringNormaliser(),
                new WhiteSpaceTokenBreaker(),
                weightedValues => weightedValues.Sum(),
                captureSourceLocations: true,
                logger: new NullLogger()
                );
            var index = indexGenerator.Generate(new NonNullImmutableList <ProductWithDescription>(new[]
            {
                new ProductWithDescription()
                {
                    Key = 1, Name = "", Description = "Product"
                },
                new ProductWithDescription()
                {
                    Key = 2, Name = "", Description = "Product"
                }
            }));

            Assert.Equal(2, index.GetMatches("Product").Count);                         // Should get two matches for "Product" at this point
            Assert.Equal(1, index.Remove(key => key == 2).GetMatches("Product").Count); // Should get only one if remove results for Key 2
        }
Example #19
0
 public VirtualRegistry(IndexGenerator indexGenerator, IDictionary <uint, VirtualRegistryKey> knownKeys)
     : base(indexGenerator, knownKeys)
 {
 }
Example #20
0
 /// <summary>
 /// Generates a blinding polynomial using an IndexGenerator
 /// </summary>
 ///
 /// <param name="Ig">An Index Generator</param>
 /// <param name="N">The number of coefficients</param>
 /// <param name="Dr">The number of ones / negative ones</param>
 ///
 /// <returns>A blinding polynomial</returns>
 public static DenseTernaryPolynomial GenerateBlindingPoly(IndexGenerator Ig, int N, int Dr)
 {
     return(new DenseTernaryPolynomial(GenerateBlindingCoeffs(Ig, N, Dr)));
 }
        public void GenerateSitemaps(Guid website, string baseUrl, string baseSitemapUrl, params CultureInfo[] cultures)
        {
            Log.Info("Starting sitemaps generation");

            var stopwatch = Stopwatch.StartNew();

            Guard.NotNullOrWhiteSpace(baseUrl, nameof(baseUrl));
            Guard.NotNullOrEmpty(cultures, nameof(cultures));
            if (website == Guid.Empty)
            {
                throw new ArgumentNullException(nameof(website));
            }

            lock (_exclusiveLock)
            {
                var sitemapDirectory = Config.GetSitemapDirectory(website);
                EnsureDirectoryExists(sitemapDirectory);

                var tasks        = new List <Task>();
                var sitemapNames = new List <string>();

                try
                {
                    EnsureDirectoryExists(Config.GetWorkingDirectory(website));

                    foreach (var culture in cultures)
                    {
                        try
                        {
                            string scope = SiteConfiguration.GetPublishedScopeId(culture, website);

                            foreach (var provider in Providers)
                            {
                                // Start a new task for each provider.
                                // For example we can generate content + product sitemaps at the same time.
                                tasks.Add(Task.Factory.StartNew(() =>
                                {
                                    Log.Info($"Generating sitemap (type:{provider.GetType()}) for {culture.Name} in {scope} scope.");
                                    var sitemapParams = new SitemapParams
                                    {
                                        Website = website,
                                        BaseUrl = baseUrl,
                                        Scope   = scope,
                                        Culture = culture
                                    };

                                    try
                                    {
                                        foreach (var sitemap in provider.GenerateSitemaps(sitemapParams))
                                        {
                                            // Write sitemap to disk
                                            Log.Info($"Writing sitemap {sitemap.Name}");
                                            WriteSitemap(sitemap, website);

                                            // Add sitemap name to the list for the index creation later
                                            lock (sitemapNames)
                                            {
                                                sitemapNames.Add(sitemap.Name);
                                            }
                                        }
                                    }
                                    catch (Exception e) {
                                        Log.Error(e.ToString());
                                    }
                                }));
                            }
                        }
                        catch (ArgumentException) { }
                    }

                    Task.WhenAll(tasks).Wait();

                    // Write sitemap index
                    var index = IndexGenerator.Generate(baseSitemapUrl, sitemapNames);
                    WriteSitemapIndex(index, website);

                    // Deploy sitemaps and sitemap index
                    Log.Info($"Deploying sitemaps to {sitemapDirectory}");
                    DeploySitemaps(website);
                }
                finally
                {
                    DeleteWorkingDirectory();
                }

                // Log stopwatch duration
                Log.Info($"Sitemaps generation completed. Generation took {stopwatch.Elapsed.TotalSeconds} seconds.");
            }
        }
Example #22
0
 /// <summary>
 /// Initializes a new instance of <see cref="RegistrySwitch"/>.
 /// </summary>
 /// <param name="indexGenerator">The <see cref="IndexGenerator"/> to use for generating virtual key handles.</param>
 /// <param name="knownKeys">A list of all known virtual registry keys.</param>
 /// <param name="ruleCollection">The collection of engine rules to consider when deciding on a target registry.</param>
 public RegistrySwitch(IndexGenerator indexGenerator, IDictionary <uint, VirtualRegistryKey> knownKeys, RegistryRuleCollection ruleCollection)
 {
     _transparentRegistry = new TransparentRegistry(indexGenerator);
     _virtualRegistry     = new VirtualRegistry(indexGenerator, knownKeys);
     _engineRules         = ruleCollection;
 }
Example #23
0
 /// <summary>
 /// Initializes the <see cref="RegistryBase"/>.
 /// </summary>
 /// <param name="indexGenerator">The <see cref="IndexGenerator"/> to use for the new instance.</param>
 protected RegistryBase(IndexGenerator indexGenerator)
     : this(indexGenerator, new Dictionary <uint, VirtualRegistryKey>())
 {
 }
Example #24
0
 public TransparentRegistry(IndexGenerator indexGenerator)
     : base(indexGenerator)
 {
     _keysPendingClosure         = new List <uint>();
     _keysPendingClosureSyncRoot = new object();
 }
 protected IndexGeneratorTestContext()
 {
     Generator = new IndexGenerator();
     MockGenerator = new Mock<IndexGenerator>();
     MockGenerator.CallBase = true;
 }