public static void Main()
        {
            using (var ignite = Ignition.StartFromApplicationConfiguration())
            {
                Console.WriteLine();
                Console.WriteLine(">>> Optimistic transaction example started.");

                // Create Transactional cache.
                var cacheCfg = new CacheConfiguration(CacheName) { AtomicityMode = CacheAtomicityMode.Transactional };

                var cache = ignite.GetOrCreateCache<int, int>(cacheCfg);

                // Put a value.
                cache[1] = 0;

                // Increment a value in parallel within a transaction.
                var task1 = Task.Factory.StartNew(() => IncrementCacheValue(cache, 1));
                var task2 = Task.Factory.StartNew(() => IncrementCacheValue(cache, 2));

                Task.WaitAll(task1, task2);

                Console.WriteLine();
                Console.WriteLine(">>> Resulting value in cache: " + cache[1]);

                Console.WriteLine();
                Console.WriteLine(">>> Example finished, press any key to exit ...");
                Console.ReadKey();
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="CacheConfiguration"/> class,
        /// performing a deep copy of specified cache configuration.
        /// </summary>
        /// <param name="other">The other configuration to perfrom deep copy from.</param>
        public CacheConfiguration(CacheConfiguration other)
        {
            if (other != null)
            {
                using (var stream = IgniteManager.Memory.Allocate().GetStream())
                {
                    other.Write(BinaryUtils.Marshaller.StartMarshal(stream));

                    stream.SynchronizeOutput();
                    stream.Seek(0, SeekOrigin.Begin);

                    Read(BinaryUtils.Marshaller.StartUnmarshal(stream));
                }

                CopyLocalProperties(other);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CacheConfiguration"/> class,
        /// performing a deep copy of specified cache configuration.
        /// </summary>
        /// <param name="other">The other configuration to perfrom deep copy from.</param>
        public CacheConfiguration(CacheConfiguration other)
        {
            if (other != null)
            {
                using (var stream = IgniteManager.Memory.Allocate().GetStream())
                {
                    other.Write(BinaryUtils.Marshaller.StartMarshal(stream));

                    stream.SynchronizeOutput();
                    stream.Seek(0, SeekOrigin.Begin);

                    Read(BinaryUtils.Marshaller.StartUnmarshal(stream));
                }

                // Plugins should be copied directly.
                PluginConfigurations = other.PluginConfigurations;
            }
        }
        /// <summary>
        /// Copies the local properties (properties that are not written in Write method).
        /// </summary>
        internal void CopyLocalProperties(CacheConfiguration cfg)
        {
            Debug.Assert(cfg != null);

            PluginConfigurations = cfg.PluginConfigurations;

            if (QueryEntities != null && cfg.QueryEntities != null)
            {
                var entities = cfg.QueryEntities.Where(x => x != null).ToDictionary(x => GetQueryEntityKey(x), x => x);

                foreach (var entity in QueryEntities.Where(x => x != null))
                {
                    QueryEntity src;

                    if (entities.TryGetValue(GetQueryEntityKey(entity), out src))
                    {
                        entity.CopyLocalProperties(src);
                    }
                }
            }
        }
Ejemplo n.º 5
0
        public DbCache(IIgnite ignite, CacheConfiguration metaCacheConfiguration, 
            CacheConfiguration dataCacheConfiguration)
        {
            IgniteArgumentCheck.NotNull(ignite, "ignite");
            IgniteArgumentCheck.NotNull(metaCacheConfiguration, "metaCacheConfiguration");
            IgniteArgumentCheck.NotNull(dataCacheConfiguration, "metaCacheConfiguration");

            IgniteArgumentCheck.Ensure(metaCacheConfiguration.Name != dataCacheConfiguration.Name, 
                "dataCacheConfiguration", "Meta and Data cache can't have the same name.");

            _metaCache = ignite.GetOrCreateCache<string, long>(metaCacheConfiguration);
            _cache = ignite.GetOrCreateCache<string, object>(dataCacheConfiguration);

            var metaCfg = _metaCache.GetConfiguration();

            if (metaCfg.AtomicityMode != CacheAtomicityMode.Transactional)
                throw new IgniteException("EntityFramework meta cache should be Transactional.");

            if (metaCfg.CacheMode == CacheMode.Partitioned && metaCfg.Backups < 1)
                ignite.Logger.Warn("EntityFramework meta cache is partitioned and has no backups. " +
                                   "This can lead to data loss and incorrect query results.");
        }
Ejemplo n.º 6
0
        public static void Main()
        {
            Console.WriteLine();
            Console.WriteLine(">>> Multi-tiered cache example started.");

            // Configure swap in the current bin directory (where our assembly is located).
            var binDir = Path.GetDirectoryName(typeof(MultiTieredCacheExample).Assembly.Location);
            var swapDir = Path.Combine(binDir, "ignite-swap");

            Console.WriteLine(">>> Swap space directory: " + swapDir);

            var cfg = new IgniteConfiguration
            {
                DiscoverySpi = new TcpDiscoverySpi
                {
                    IpFinder = new TcpDiscoveryMulticastIpFinder
                    {
                        Endpoints = new[] { "127.0.0.1:47500" }
                    }
                },
                SwapSpaceSpi = new FileSwapSpaceSpi
                {
                    BaseDirectory = swapDir
                }
            };

            using (var ignite = Ignition.Start(cfg))
            {
                var cacheCfg = new CacheConfiguration
                {
                    Name = CacheName,
                    Backups = 1,
                    EvictionPolicy = new LruEvictionPolicy
                    {
                        MaxSize = 10 // Maximum number of entries that will be stored in Java heap. 
                    },
                    // Limit off-heap to roughly 10 entries. Actual entry count will be lower due to metadata overhead.
                    OffHeapMaxMemory = EntrySize * 10,
                    // Data will be swapped to disk if there is no more space in off-heap space.
                    EnableSwap = true 
                };

                ICache<int, byte[]> cache = ignite.GetOrCreateCache<int, byte[]>(cacheCfg);

                // Sample data.
                byte[] dataBytes = new byte[EntrySize];

                // Filling out cache and printing its metrics.
                PrintCacheMetrics(cache);

                for (int i = 0; i < 100; i++)
                {
                    cache.Put(i, dataBytes);

                    if (i%10 == 0)
                    {
                        Console.WriteLine(">>> Cache entries created: {0}", i + 1);

                        PrintCacheMetrics(cache);
                    }
                }

                Console.WriteLine(">>> Waiting for metrics final update...");

                Thread.Sleep(TcpDiscoverySpi.DefaultHeartbeatFrequency);

                PrintCacheMetrics(cache);

                Console.WriteLine();
                Console.WriteLine(">>> Example finished, press any key to exit ...");
                Console.ReadKey();
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Initializes Ignite caching for specified <see cref="DbConfiguration"/>.
        /// This method should be used when it is not possible to use or inherit <see cref="IgniteDbConfiguration"/>.
        /// </summary>
        /// <param name="dbConfiguration"><see cref="DbConfiguration"/> instance to be initialized
        /// for Ignite caching.</param>
        /// <param name="ignite">The ignite instance to use.</param>
        /// <param name="metaCacheConfiguration">
        /// Configuration of the metadata cache which holds entity set information. Null for default configuration. 
        /// <para />
        /// This cache holds small amount of data, but should not lose entries. At least one backup recommended.
        /// </param>
        /// <param name="dataCacheConfiguration">
        /// Configuration of the data cache which holds query results. Null for default configuration.
        /// <para />
        /// This cache tolerates lost data and can have no backups.
        /// </param>
        /// <param name="policy">The caching policy. Null for default <see cref="DbCachingPolicy" />.</param>
        private static void InitializeIgniteCachingInternal(DbConfiguration dbConfiguration, IIgnite ignite, 
            CacheConfiguration metaCacheConfiguration, CacheConfiguration dataCacheConfiguration, 
            IDbCachingPolicy policy)
        {
            Debug.Assert(ignite != null);
            Debug.Assert(dbConfiguration != null);

            metaCacheConfiguration = metaCacheConfiguration ?? GetDefaultMetaCacheConfiguration();
            dataCacheConfiguration = dataCacheConfiguration ?? GetDefaultDataCacheConfiguration();

            var efCache = new DbCache(ignite, metaCacheConfiguration, dataCacheConfiguration);

            var txHandler = new DbTransactionInterceptor(efCache);

            AddInterceptorDelegate(dbConfiguration, txHandler);

            RegisterProviderServicesReplacer(dbConfiguration, policy, efCache, txHandler);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Initializes Ignite caching for specified <see cref="DbConfiguration"/>.
        /// This method should be used when it is not possible to use or inherit <see cref="IgniteDbConfiguration"/>.
        /// </summary>
        /// <param name="dbConfiguration"><see cref="DbConfiguration"/> instance to be initialized
        /// for Ignite caching.</param>
        /// <param name="ignite">The ignite instance to use.</param>
        /// <param name="metaCacheConfiguration">
        /// Configuration of the metadata cache which holds entity set information. Null for default configuration. 
        /// <para />
        /// This cache holds small amount of data, but should not lose entries. At least one backup recommended.
        /// </param>
        /// <param name="dataCacheConfiguration">
        /// Configuration of the data cache which holds query results. Null for default configuration.
        /// <para />
        /// This cache tolerates lost data and can have no backups.
        /// </param>
        /// <param name="policy">The caching policy. Null for default <see cref="DbCachingPolicy" />.</param>
        public static void InitializeIgniteCaching(DbConfiguration dbConfiguration, IIgnite ignite,
            CacheConfiguration metaCacheConfiguration, CacheConfiguration dataCacheConfiguration,
            IDbCachingPolicy policy)
        {
            IgniteArgumentCheck.NotNull(ignite, "ignite");
            IgniteArgumentCheck.NotNull(dbConfiguration, "configuration");

            IgniteArgumentCheck.Ensure(!(dbConfiguration is IgniteDbConfiguration), "dbConfiguration",
                "IgniteDbConfiguration.InitializeIgniteCaching should not be called for IgniteDbConfiguration " +
                "instance. This method should be used only when IgniteDbConfiguration can't be inherited.");

            InitializeIgniteCachingInternal(dbConfiguration, ignite, metaCacheConfiguration, dataCacheConfiguration, 
                policy);
        }
Ejemplo n.º 9
0
        public IgniteDbConfiguration(IIgnite ignite, CacheConfiguration metaCacheConfiguration,
            CacheConfiguration dataCacheConfiguration, IDbCachingPolicy policy)
        {
            IgniteArgumentCheck.NotNull(ignite, "ignite");

            InitializeIgniteCachingInternal(this, ignite, metaCacheConfiguration, dataCacheConfiguration, policy);
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="IgniteDbConfiguration" /> class.
 /// </summary>
 /// <param name="igniteConfiguration">The ignite configuration to use for starting Ignite instance.</param>
 /// <param name="metaCacheConfiguration">
 /// Configuration of the metadata cache which holds entity set information. Null for default configuration. 
 /// <para />
 /// This cache holds small amount of data, but should not lose entries. At least one backup recommended.
 /// </param>
 /// <param name="dataCacheConfiguration">
 /// Configuration of the data cache which holds query results. Null for default configuration.
 /// <para />
 /// This cache tolerates lost data and can have no backups.
 /// </param>
 /// <param name="policy">The caching policy. Null for default <see cref="DbCachingPolicy"/>.</param>
 public IgniteDbConfiguration(IgniteConfiguration igniteConfiguration,
     CacheConfiguration metaCacheConfiguration, CacheConfiguration dataCacheConfiguration,
     IDbCachingPolicy policy)
     : this(GetOrStartIgnite(igniteConfiguration), metaCacheConfiguration, dataCacheConfiguration, policy)
 {
     // No-op.
 }
Ejemplo n.º 11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="IgniteDbConfiguration"/> class.
 /// </summary>
 /// <param name="configurationSectionName">Name of the configuration section.</param>
 /// <param name="metaCacheConfiguration">
 /// Configuration of the metadata cache which holds entity set information. Null for default configuration.
 /// <para />
 /// This cache holds small amount of data, but should not lose entries. At least one backup recommended.
 /// </param>
 /// <param name="dataCacheConfiguration">
 /// Configuration of the data cache which holds query results. Null for default configuration.
 /// <para />
 /// This cache tolerates lost data and can have no backups.
 /// </param>
 /// <param name="policy">The caching policy. Null for default <see cref="DbCachingPolicy"/>.</param>
 public IgniteDbConfiguration(string configurationSectionName, CacheConfiguration metaCacheConfiguration,
     CacheConfiguration dataCacheConfiguration, IDbCachingPolicy policy)
     : this(GetConfiguration(configurationSectionName, true), 
           metaCacheConfiguration, dataCacheConfiguration, policy)
 {
     // No-op.
 }
Ejemplo n.º 12
0
        public IgniteDbConfiguration(IIgnite ignite, CacheConfiguration metaCacheConfiguration,
            CacheConfiguration dataCacheConfiguration, IDbCachingPolicy policy)
        {
            IgniteArgumentCheck.NotNull(ignite, "ignite");

            metaCacheConfiguration = metaCacheConfiguration ?? GetDefaultMetaCacheConfiguration();
            dataCacheConfiguration = dataCacheConfiguration ?? GetDefaultDataCacheConfiguration();

            var efCache = new DbCache(ignite, metaCacheConfiguration, dataCacheConfiguration);

            var txHandler = new DbTransactionInterceptor(efCache);

            AddInterceptor(txHandler);

            // SetProviderServices is not suitable. We should replace whatever provider there is with our proxy.
            Loaded += (sender, args) => args.ReplaceService<DbProviderServices>(
                (services, a) => new DbProviderServicesProxy(services, policy, efCache, txHandler));
        }