19 using System.Collections.Generic;
23 using Lucene.Net.Support;
28 namespace Lucene.Net.Store
106 private static System.Security.Cryptography.HashAlgorithm DIGESTER;
112 DIGESTER = Cryptography.HashAlgorithm;
114 catch (System.Exception e)
116 throw new System.SystemException(e.ToString(), e);
120 private bool checked_Renamed;
122 internal void CreateDir()
124 if (!checked_Renamed)
126 if (!this.internalDirectory.Exists)
130 this.internalDirectory.Create();
134 throw new System.IO.IOException(
"Cannot create directory: " + internalDirectory);
136 this.internalDirectory.Refresh();
139 checked_Renamed =
true;
146 protected internal void InitOutput(System.String name)
150 System.IO.FileInfo file =
new System.IO.FileInfo(System.IO.Path.Combine(internalDirectory.FullName, name));
159 throw new System.IO.IOException(
"Cannot overwrite: " + file);
165 protected internal System.IO.DirectoryInfo internalDirectory = null;
177 if (lockFactory == null)
186 internalDirectory = path;
189 if (!internalDirectory.Exists && System.IO.File.Exists(internalDirectory.FullName))
193 SetLockFactory(lockFactory);
199 FSLockFactory lf = (FSLockFactory)lockFactory;
200 System.IO.DirectoryInfo dir = lf.
LockDir;
204 lf.
LockDir = this.internalDirectory;
207 else if (dir.FullName.Equals(
this.internalDirectory.FullName))
236 return Open(
new DirectoryInfo(path), null);
261 return Open(path, null);
297 public static System.String[] ListAll(System.IO.DirectoryInfo dir)
303 else if (System.IO.File.Exists(dir.FullName))
309 System.IO.FileInfo[] files = dir.GetFiles();
310 System.String[] result =
new System.String[files.Length];
311 for (
int i = 0; i < files.Length; i++)
313 result[i] = files[i].Name;
328 public override System.String[] ListAll()
331 return ListAll(internalDirectory);
335 public override bool FileExists(System.String name)
338 System.IO.FileInfo file =
new System.IO.FileInfo(System.IO.Path.Combine(internalDirectory.FullName, name));
343 public override long FileModified(System.String name)
346 System.IO.FileInfo file =
new System.IO.FileInfo(System.IO.Path.Combine(internalDirectory.FullName, name));
347 return (
long)file.LastWriteTime.ToUniversalTime().Subtract(
new DateTime(1970, 1, 1, 0, 0, 0)).TotalMilliseconds;
351 public static long FileModified(System.IO.FileInfo directory, System.String name)
353 System.IO.FileInfo file =
new System.IO.FileInfo(System.IO.Path.Combine(directory.FullName, name));
354 return (
long)file.LastWriteTime.ToUniversalTime().Subtract(
new DateTime(1970, 1, 1, 0, 0, 0)).TotalMilliseconds;
358 public override void TouchFile(System.String name)
361 System.IO.FileInfo file =
new System.IO.FileInfo(System.IO.Path.Combine(internalDirectory.FullName, name));
362 file.LastWriteTime = System.DateTime.Now;
366 public override long FileLength(System.String name)
369 System.IO.FileInfo file =
new System.IO.FileInfo(System.IO.Path.Combine(internalDirectory.FullName, name));
370 return file.Exists ? file.Length : 0;
374 public override void DeleteFile(System.String name)
377 System.IO.FileInfo file =
new System.IO.FileInfo(System.IO.Path.Combine(internalDirectory.FullName, name));
384 throw new System.IO.IOException(
"Cannot delete " + file);
388 public override void Sync(System.String name)
391 System.IO.FileInfo fullFile =
new System.IO.FileInfo(System.IO.Path.Combine(internalDirectory.FullName, name));
392 bool success =
false;
394 System.IO.IOException exc = null;
395 while (!success && retryCount < 5)
398 System.IO.FileStream file = null;
403 file =
new System.IO.FileStream(fullFile.FullName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite);
413 catch (System.IO.IOException ioe)
419 System.Threading.Thread.Sleep(5);
424 if (!success && exc != null)
437 private static readonly
char[] HEX_DIGITS =
new char[]{
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'a',
'b',
'c',
'd',
'e',
'f'};
440 public override string GetLockId()
443 System.String dirName;
446 dirName = internalDirectory.FullName;
448 catch (System.IO.IOException e)
450 throw new System.SystemException(e.ToString(), e);
456 digest = DIGESTER.ComputeHash(System.Text.Encoding.UTF8.GetBytes(dirName));
458 System.Text.StringBuilder buf =
new System.Text.StringBuilder();
459 buf.Append(
"lucene-");
460 for (
int i = 0; i < digest.Length; i++)
463 buf.Append(HEX_DIGITS[(b >> 4) & 0xf]);
464 buf.Append(HEX_DIGITS[b & 0xf]);
467 return buf.ToString();
470 protected override void Dispose(
bool disposing)
486 return internalDirectory;
491 public override System.String ToString()
493 return this.GetType().FullName +
"@" + internalDirectory +
" lockFactory=" +
LockFactory;
502 public static readonly
int DEFAULT_READ_CHUNK_SIZE =
Constants.JRE_IS_64BIT ?
int.MaxValue: 100 * 1024 * 1024;
505 private int chunkSize = DEFAULT_READ_CHUNK_SIZE;
512 public int ReadChunkSize
524 throw new System.ArgumentException(
"chunkSize must be positive");
528 this.chunkSize = value;