Compute lucene-style scores for our hits.
[beagle.git] / Util / FileAdvise.cs
blob6eeaf3272327d150646dc3a309d32de4efeace0f
1 //
2 // FileAdvise.cs
3 //
4 // Copyright (C) 2004 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 // DEALINGS IN THE SOFTWARE.
27 // FIXME: This is not portable to Win32
29 using System;
30 using System.IO;
31 using System.Text;
32 using System.Runtime.InteropServices;
33 using Mono.Posix;
35 using Beagle.Util;
37 namespace Beagle.Util {
39 public class FileAdvise {
41 // FIXME: On 64-bit architectures, we need to use "long" not "int" here for
42 // "offset" and "len"
43 [DllImport ("libc", SetLastError=true)]
44 static extern int posix_fadvise (int fd, int offset, int len, int advice);
46 // The following are from /usr/include/linux/fadvise.h and will not change
47 private const int AdviseNormal = 0; // POSIX_FADV_NORMAL
48 private const int AdviseRandom = 1; // POSIX_FADV_RANDOM
49 private const int AdviseSequential = 2; // POSIX_FADV_SEQUENTIAL
50 private const int AdviseWillNeed = 3; // POSIX_FADV_WILLNEED
51 private const int AdviseDontNeed = 4; // POSIX_FADV_DONTNEED
52 private const int AdviseNoReUse = 5; // POSIX_FADV_NOREUSE
54 static private int GiveAdvice (FileStream file, int advice)
56 int fd = file.Handle.ToInt32 ();
57 return posix_fadvise (fd, 0, 0, advice);
60 static public void FlushCache (FileStream file)
62 GiveAdvice (file, AdviseDontNeed);
65 static public void PreLoad (FileStream file)
67 GiveAdvice (file, AdviseWillNeed);
70 static public void IncreaseReadAhead (FileStream file)
72 GiveAdvice (file, AdviseSequential);
75 static public void DisableReadAhead (FileStream file)
77 GiveAdvice (file, AdviseRandom);
80 static public void NormalReadAhead (FileStream file)
82 GiveAdvice (file, AdviseNormal);
85 static public void WillAccessOnlyOnce (FileStream file)
87 GiveAdvice (file, AdviseNoReUse);
90 static public void TestAdvise ()
92 try {
93 FileStream file = new FileStream ("/etc/fstab",
94 System.IO.FileMode.Open,
95 FileAccess.Read);
97 int ret = GiveAdvice (file, AdviseNormal);
98 if (ret != 0) {
99 Logger log = Logger.Get ("FileAdvise");
100 log.Error ("FileAdvise failed: " +
101 Syscall.strerror (Marshal.GetLastWin32Error()));
104 file.Close ();
105 } catch { }