lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aro...@apache.org
Subject svn commit: r836306 - in /incubator/lucene.net/trunk/C#/src: Lucene.Net/Store/FSDirectory.cs Lucene.Net/SupportClass.cs Test/TestSupportClass.cs
Date Sun, 15 Nov 2009 01:21:50 GMT
Author: aroush
Date: Sun Nov 15 01:21:50 2009
New Revision: 836306

URL: http://svn.apache.org/viewvc?rev=836306&view=rev
Log:
Patch of LUCENENET-216: FSDirectory.Sync Fix to Ensure Flush to Disk

Modified:
    incubator/lucene.net/trunk/C#/src/Lucene.Net/Store/FSDirectory.cs
    incubator/lucene.net/trunk/C#/src/Lucene.Net/SupportClass.cs
    incubator/lucene.net/trunk/C#/src/Test/TestSupportClass.cs

Modified: incubator/lucene.net/trunk/C#/src/Lucene.Net/Store/FSDirectory.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Lucene.Net/Store/FSDirectory.cs?rev=836306&r1=836305&r2=836306&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Lucene.Net/Store/FSDirectory.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Lucene.Net/Store/FSDirectory.cs Sun Nov 15 01:21:50
2009
@@ -810,29 +810,8 @@
 					try
 					{
                         file = new System.IO.FileStream(fullFile.FullName, System.IO.FileMode.OpenOrCreate,
System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite);
-                        
-                        //TODO
-                        // {{dougsale-2.4}}:
-                        // 
-                        // in Lucene (Java):
-                        // file.getFD().sync();
-                        // file is a java.io.RandomAccessFile
-                        // getFD() returns java.io.FileDescriptor
-                        // sync() documentation states: "Force all system buffers to synchronize
with the underlying device."
-                        //
-                        // what they try to do here is get ahold of the underlying file descriptor
-                        // for the given file name and make sure all (downstream) associated
system buffers are
-                        // flushed to disk
-                        // 
-                        // how do i do that in .Net?  flushing the created stream might inadvertently
do it, or it
-                        // may do nothing at all.  I can find references to the file HANDLE,
but it's not really
-                        // a type, simply an int pointer.
-                        //
-                        // where is FSDirectory.Sync(string name) called from - if this isn't
a new feature but rather a refactor, maybe
-                        // i can snip the old code from where it was re-factored...
-
-                        file.Flush();
-						success = true;
+                        SupportClass.FileSupport.Sync(file);
+                        success = true;
 					}
 					finally
 					{

Modified: incubator/lucene.net/trunk/C#/src/Lucene.Net/SupportClass.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Lucene.Net/SupportClass.cs?rev=836306&r1=836305&r2=836306&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Lucene.Net/SupportClass.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Lucene.Net/SupportClass.cs Sun Nov 15 01:21:50 2009
@@ -468,6 +468,46 @@
             list.CopyTo(retFiles);
             return retFiles;
         }
+
+		// Disable the obsolete warning since we must use FileStream.Handle
+		// because Mono does not support FileSystem.SafeFileHandle at present.
+#pragma warning disable 618
+
+		/// <summary>
+		/// Flushes the specified file stream. Ensures that all buffered
+		/// data is actually written to the file system.
+		/// </summary>
+		/// <param name="fileStream">The file stream.</param>
+		public static void Sync(System.IO.FileStream fileStream)
+		{
+			if (fileStream == null)
+				throw new ArgumentNullException("fileStream");
+
+			fileStream.Flush();
+
+			if (OS.IsWindows)
+			{
+				if (!FlushFileBuffers(fileStream.Handle))
+					throw new System.IO.IOException();
+			}
+			else if (OS.IsUnix)
+			{
+				if (fsync(fileStream.Handle) != IntPtr.Zero)
+				throw new System.IO.IOException();
+			}
+			else
+			{
+				throw new NotImplementedException();
+			}
+		}
+
+#pragma warning restore 618
+
+		[System.Runtime.InteropServices.DllImport("libc")]
+		extern static IntPtr fsync(IntPtr fd);
+
+		[System.Runtime.InteropServices.DllImport("kernel32.dll")]
+		extern static bool FlushFileBuffers(IntPtr hFile);
     }
 
     /// <summary>
@@ -1739,4 +1779,41 @@
         internal Type Key;
         internal Lucene.Net.Util.AttributeImpl Value;
     }
+
+	/// <summary>
+	/// Provides platform infos.
+	/// </summary>
+	public class OS
+	{
+		static bool isUnix;
+		static bool isWindows;
+
+		static OS()
+		{
+			PlatformID pid = Environment.OSVersion.Platform;
+			isWindows = pid == PlatformID.Win32NT || pid == PlatformID.Win32Windows;
+
+			// we use integers instead of enum tags because "MacOS"
+			// requires 2.0 SP2, 3.0 SP2 or 3.5 SP1.
+			// 128 is mono's old platform tag for Unix.
+			int id = (int)pid;
+			isUnix = id == 4 || id == 6 || id == 128;
+		}
+
+		/// <summary>
+		/// Whether we run under a Unix platform.
+		/// </summary>
+		public static bool IsUnix
+		{
+			get { return isUnix; }
+		}
+
+		/// <summary>
+		/// Whether we run under a supported Windows platform.
+		/// </summary>
+		public static bool IsWindows
+		{
+			get { return isWindows; }
+		}
+	}
 }

Modified: incubator/lucene.net/trunk/C#/src/Test/TestSupportClass.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Test/TestSupportClass.cs?rev=836306&r1=836305&r2=836306&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Test/TestSupportClass.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Test/TestSupportClass.cs Sun Nov 15 01:21:50 2009
@@ -679,4 +679,27 @@
             }
         }
     }
+
+    [TestFixture]
+    public class TestOSClass
+    {
+        // LUCENENET-216
+        [Test]
+        public void TestFSDirectorySync()
+        {
+            System.IO.FileInfo path = new System.IO.FileInfo(System.IO.Path.Combine(SupportClass.AppSettings.Get("tempDir",
""), "testsync"));
+            Lucene.Net.Store.Directory directory = new Lucene.Net.Store.SimpleFSDirectory(path,
null);
+            try
+            {
+                Lucene.Net.Store.IndexOutput io = directory.CreateOutput("syncfile");
+                io.Close();
+                directory.Sync("syncfile");
+            }
+            finally
+            {
+                directory.Close();
+                Lucene.Net.Util._TestUtil.RmDir(path);
+            }
+        }
+    }
 }
\ No newline at end of file



Mime
View raw message