Ejemplo n.º 1
0
        public async Task ShouldWork()
        {
            var client1 = NewAsyncClient(0, fileSystemName: "shard1");
            var client2 = NewAsyncClient(1, fileSystemName: "shard2");

            var shards = new Dictionary <string, IAsyncFilesCommands>
            {
                { "Europe", client1 },
                { "Asia", client2 },
            };

            var strategy = new ShardStrategy(shards);

            strategy.ShardResolutionStrategy = new RegionMetadataBasedResolutionStrategy(shards.Keys.ToList(), strategy.ModifyFileName, strategy.Conventions);

            var client = new AsyncShardedFilesServerClient(strategy);

            var fileName = await client.UploadAsync("test1", new RavenJObject()
            {
                {
                    "Region", "Europe"
                }
            }, new MemoryStream());

            Assert.Equal("/Europe/test1", fileName);

            fileName = await client.UploadAsync("test2", new RavenJObject()
            {
                {
                    "region", "asia"
                }
            }, new MemoryStream());

            Assert.Equal("/Asia/test2", fileName);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            /*
             * Run functions from here if you don't have a test runner in VisualStudio
             */


            var shards = new Dictionary <string, IAsyncFilesCommands>
            {
                { "Europe", new AsyncFilesServerClient("http://localhost:8080", "ShardEurope") },
                { "Asia", new AsyncFilesServerClient("http://localhost:8080", "ShardAsia") },
            };

            var shardStrategy = new ShardStrategy(shards)
            {
                /*
                 * ShardAccessStrategy = ...
                 * ShardResolutionStrategy = ...
                 * ModifyFileName = ...
                 */
            };

            var shardedCommands = new AsyncShardedFilesServerClient(shardStrategy);

            // Don't forget to dispose the document / files store
            Store.Dispose();

            Console.WriteLine("Done!");
            Console.Read();
        }
        public string UploadFile(string filePath, string country)
        {
            var command = new AsyncShardedFilesServerClient(FileStore.ShardStrategy);

            string fileName = String.Empty;

            try
            {
                using (FileStream fileStream = File.Open(filePath, FileMode.Open))
                {
                    string file = Path.GetFileName(filePath);

                    fileName = command.UploadAsync(file, new RavenJObject()
                    {
                        {
                            "Country", country
                        }
                    }, fileStream).Result;
                }
            }
            catch (Exception ex)
            {
                throw;
            }

            return(fileName);
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            // Set filter for file extension and default file extension


            // Display OpenFileDialog by calling ShowDialog method
            Nullable <bool> result = dlg.ShowDialog();

            string country = ((Button)cbCountry.SelectedValue).Content.ToString();

            // Get the selected file name and display in a TextBox
            if (result == true)
            {
                string filename = dlg.FileName;

                try
                {
                    using (FileStream fileStream = File.Open(filename, FileMode.Open))
                    {
                        var command = new AsyncShardedFilesServerClient(FileStore.ShardStrategy);

                        string ravenFileName = command.UploadAsync(System.IO.Path.GetFileName(filename), new RavenJObject()
                        {
                            { "Country", country }
                        }, fileStream).Result;

                        string content = String.Empty;
                        DocX   doc     = DocX.Load(fileStream);

                        content = doc.Text;

                        fileStream.Position = 0;


                        using (IDocumentSession session = RavenConnection.DocumentStore.OpenSession())
                        {
                            Documento documento = new Documento()
                            {
                                Titolo    = System.IO.Path.GetFileName(filename),
                                Contenuto = content,
                                NomeFile  = System.IO.Path.GetFileName(filename),
                                Indirizzo = ravenFileName
                            };

                            session.Store(documento);

                            session.SaveChanges();
                        }

                        ReloadFiles();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
Ejemplo n.º 5
0
		public async Task Foo()
		{
			#region sharding_1
			var shards = new Dictionary<string, IAsyncFilesCommands>
			{
				{"europe", new AsyncFilesServerClient("http://localhost:8080", "NorthwindFS")},
				{"asia", new AsyncFilesServerClient("http://localhost:8081", "NorthwindFS")},
			};

			var shardStrategy = new ShardStrategy(shards)
			{
				/*
				ShardAccessStrategy = ...
				ShardResolutionStrategy = ...
				ModifyFileName = ...
				*/
			};

			var shardedCommands = new AsyncShardedFilesServerClient(shardStrategy);
			#endregion

			#region file_operations
			string fileName = await shardedCommands.UploadAsync("test.bin", new RavenJObject()
			{
				{
					"Owner", "Admin"
				}
			}, new MemoryStream()); // will return either /europe/test.bin or /asia/test.bin name

			// you need to pass the returned file name here to let the client know on which shard the file exists
			using (var content = await shardedCommands.DownloadAsync(fileName)) 
			{
				
			}

			string renamed = await shardedCommands.RenameAsync(fileName, "new.bin");

			await shardedCommands.DeleteAsync(renamed);

			#endregion

			#region search_browse_operations
			FileHeader[] fileHeaders = await shardedCommands.BrowseAsync();

			SearchResults searchResults = await shardedCommands.SearchAsync("__fileName:test*");
			#endregion

			#region custom_shard_res_strategy_2
			var strategy = new ShardStrategy(shards);

			strategy.ShardResolutionStrategy = new RegionMetadataBasedResolutionStrategy(shards.Keys.ToList(), strategy.ModifyFileName, strategy.Conventions);

			var client = new AsyncShardedFilesServerClient(strategy);
			#endregion
		}
Ejemplo n.º 6
0
        public async Task Foo()
        {
            #region sharding_1
            var shards = new Dictionary <string, IAsyncFilesCommands>
            {
                { "europe", new AsyncFilesServerClient("http://localhost:8080", "NorthwindFS") },
                { "asia", new AsyncFilesServerClient("http://localhost:8081", "NorthwindFS") },
            };

            var shardStrategy = new ShardStrategy(shards)
            {
                /*
                 * ShardAccessStrategy = ...
                 * ShardResolutionStrategy = ...
                 * ModifyFileName = ...
                 */
            };

            var shardedCommands = new AsyncShardedFilesServerClient(shardStrategy);
            #endregion

            #region file_operations
            string fileName = await shardedCommands.UploadAsync("test.bin", new RavenJObject()
            {
                {
                    "Owner", "Admin"
                }
            }, new MemoryStream());             // will return either /europe/test.bin or /asia/test.bin name

            // you need to pass the returned file name here to let the client know on which shard the file exists
            using (var content = await shardedCommands.DownloadAsync(fileName))
            {
            }

            string renamed = await shardedCommands.RenameAsync(fileName, "new.bin");

            await shardedCommands.DeleteAsync(renamed);

            #endregion

            #region search_browse_operations
            FileHeader[] fileHeaders = await shardedCommands.BrowseAsync();

            SearchResults searchResults = await shardedCommands.SearchAsync("__fileName:test*");

            #endregion

            #region custom_shard_res_strategy_2
            var strategy = new ShardStrategy(shards);

            strategy.ShardResolutionStrategy = new RegionMetadataBasedResolutionStrategy(shards.Keys.ToList(), strategy.ModifyFileName, strategy.Conventions);

            var client = new AsyncShardedFilesServerClient(strategy);
            #endregion
        }
Ejemplo n.º 7
0
        public SimpleSharding()
        {
            var client1 = NewAsyncClient(0, fileSystemName: "shard1");
            var client2 = NewAsyncClient(1, fileSystemName: "shard2");

            shardedClient = new AsyncShardedFilesServerClient(new ShardStrategy(new Dictionary <string, IAsyncFilesCommands>
            {
                { "1", client1 },
                { "2", client2 },
            }));
        }
        private void button_Click_2(object sender, RoutedEventArgs e)
        {
            try
            {
                string locazioneFile = String.Empty;

                if (lstFiles.SelectedItem != null)
                {
                    locazioneFile = ((SearchResult)(lstFiles.SelectedItem)).Indirizzo;
                }
                if (filesList.SelectedItem != null)
                {
                    locazioneFile = ((FileHeader)(filesList.SelectedItem)).FullPath;
                }

                using (IDocumentSession session = RavenConnection.DocumentStore.OpenSession())
                {
                    Documento documento = session.Query <Documento>().Where(x => x.Indirizzo == locazioneFile.TrimStart(new[] { '/' })).FirstOrDefault();
                    if (documento != null)
                    {
                        session.Delete <Documento>(documento);
                    }

                    var command = new AsyncShardedFilesServerClient(FileStore.ShardStrategy);

                    if (documento != null)
                    {
                        command.DeleteAsync(documento.Indirizzo);
                    }
                    else
                    {
                        command.DeleteAsync(locazioneFile);
                    }

                    session.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            ReloadFiles();
        }
        private void SaveFile_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Microsoft.Win32.SaveFileDialog saveFileDialog = new Microsoft.Win32.SaveFileDialog();

                saveFileDialog.ShowDialog();

                string locazioneFile = String.Empty;

                if (lstFiles.SelectedItem != null)
                {
                    locazioneFile = ((SearchResult)(lstFiles.SelectedItem)).Indirizzo;
                }
                if (filesList.SelectedItem != null)
                {
                    locazioneFile = ((FileHeader)(filesList.SelectedItem)).FullPath;
                }

                Stream file = null;

                var command = new AsyncShardedFilesServerClient(FileStore.ShardStrategy);

                file = command.DownloadAsync(locazioneFile).Result;


                if (String.IsNullOrEmpty(saveFileDialog.FileName) == false)
                {
                    string fileName = saveFileDialog.FileName;

                    using (FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write))
                    {
                        file.CopyTo(stream);
                        stream.Close();
                        file.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private async void ReloadFiles()
        {
            RavenManager ravenManager = new RavenManager();

            var command = new AsyncShardedFilesServerClient(FileStore.ShardStrategy);

            var folders = await command.GetFoldersAsync();


            folders.ToList().ForEach(async x =>
            {
                filesList.Items.Clear();

                var files = await command.GetFilesAsync(x);

                foreach (var file in files.Files)
                {
                    filesList.Items.Add(file);
                }
            });
        }