Skip to content

Commit

Permalink
读写锁增加try finally
Browse files Browse the repository at this point in the history
  • Loading branch information
macworld committed Apr 23, 2014
1 parent 7477069 commit 514f6c7
Showing 1 changed file with 56 additions and 50 deletions.
106 changes: 56 additions & 50 deletions FileManager/FileBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ private bool SaveFile(Byte[] fileByteStream, string url)
{
if (fileLengthDictionary.ContainsKey(url) || totalFreeSpace < fileLength)//avoid the error causing by other saving action
{
//log.Warn("Warning: File "+url+"is");
return false;
}
LruList.AddFirst(url);
Expand Down Expand Up @@ -204,8 +203,8 @@ private bool SaveFile(Byte[] fileByteStream, string url)
}
urlToPageDic.Add(url, urlPages);//added to the dictionnary which is used as a page table
fileLengthDictionary.Add(url, fileLength);//add to the dictionnary when it has been saved in the memory
log.Debug("Add file: " + url + " in buffer");
}
log.Debug("Add file: " + url + " in buffer");
}
return true;
}
Expand Down Expand Up @@ -274,24 +273,29 @@ private bool RemoveFileInBuffer(string url, long fileLength)
{
// lock (LockRDConflict)//avoid the conflict when read the file which has been deleted
RWLock.AcquireWriterLock(FileManager.Properties.FileManagerSettings.Default.LockTimeOut);
if (totalFreeSpace > fileLength | !LruList.Remove(url))//when the url is not in the LruList
try
{
return false;
if (totalFreeSpace > fileLength | !LruList.Remove(url))//when the url is not in the LruList
{
return false;
}
string pageString = urlToPageDic[url];
urlToPageDic.Remove(url);
fileLengthDictionary.Remove(url);
string[] pageNumStr = pageString.Split(',');
int pageNumInt = 0;
for (int i = 0; i < pageNumStr.Length; ++i)
{
pageNumInt = Convert.ToInt32(pageNumStr[i]);
freePageStack.Push(pageNumInt);//release the buffer
totalFreeSpace += pageSize; //get the new size of FreeSpace
}
}
string pageString = urlToPageDic[url];
urlToPageDic.Remove(url);
fileLengthDictionary.Remove(url);
string[] pageNumStr = pageString.Split(',');
int pageNumInt = 0;
for (int i = 0; i < pageNumStr.Length; ++i)
finally
{
pageNumInt = Convert.ToInt32(pageNumStr[i]);
freePageStack.Push(pageNumInt);//release the buffer
totalFreeSpace += pageSize; //get the new size of FreeSpace
RWLock.ReleaseWriterLock();
}

RWLock.ReleaseWriterLock();

return true;
}

Expand All @@ -318,48 +322,50 @@ public Byte[] readFile(string url, ref int statusCode)
if (fileLengthDictionary.ContainsKey(url))//if the file was exsited in filebuffer
{
RWLock.AcquireReaderLock(FileManager.Properties.FileManagerSettings.Default.LockTimeOut);
if (!fileLengthDictionary.ContainsKey(url))//when the delete part has delete the file in the buffer,
//due to the wait the last contain is not effective now,we need to make sure if the file exist or not again
{
Byte[] readBuffer = FileSystem.GetInstance().readFile(url);
SaveFile(readBuffer, url);//save the file to fileBuffer in memory
statusCode = 200;
return readBuffer;
}
long fileLength = fileLengthDictionary[url];
Byte[] fileByteStream = new Byte[fileLength];
string pageString = urlToPageDic[url];
string[] pageNumStr = pageString.Split(',');
int pageNumInt = 0;
int fileOffset = 0;
for (int i = 0; i < pageNumStr.Length; ++i)
try
//lock (LockRDConflict)
{
pageNumInt = Convert.ToInt32(pageNumStr[i]);
if (fileLength - fileOffset >= pageSize)
if (!fileLengthDictionary.ContainsKey(url))//when the delete part has delete the file in the buffer,
//due to the wait the last contain is not effective now,we need to make sure if the file exist or not again
{
System.Buffer.BlockCopy(memoryBuffer, pageNumInt * pageSize, fileByteStream, (int)fileOffset, pageSize);
Byte[] readBuffer = FileSystem.GetInstance().readFile(url);
SaveFile(readBuffer, url);//save the file to fileBuffer in memory
statusCode = 200;
return readBuffer;
}
else
long fileLength = fileLengthDictionary[url];
Byte[] fileByteStream = new Byte[fileLength];
string pageString = urlToPageDic[url];
string[] pageNumStr = pageString.Split(',');
int pageNumInt = 0;
int fileOffset = 0;
for (int i = 0; i < pageNumStr.Length; ++i)
{
System.Buffer.BlockCopy(memoryBuffer, pageNumInt * pageSize, fileByteStream, (int)fileOffset, (int)(fileLength - fileOffset));
pageNumInt = Convert.ToInt32(pageNumStr[i]);
if (fileLength - fileOffset >= pageSize)
{
System.Buffer.BlockCopy(memoryBuffer, pageNumInt * pageSize, fileByteStream, (int)fileOffset, pageSize);
}
else
{
System.Buffer.BlockCopy(memoryBuffer, pageNumInt * pageSize, fileByteStream, (int)fileOffset, (int)(fileLength - fileOffset));
}
fileOffset += pageSize;
}
fileOffset += pageSize;
}
//to realize LRU,the file which was read need to be put into the head of the list
LinkedListNode<string> readNode = LruList.Find(url);
if (readNode != null)
{
LruList.Remove(url);
LruList.AddFirst(readNode);
//to realize LRU,the file which was read need to be put into the head of the list
LinkedListNode<string> readNode = LruList.Find(url);
if (readNode != null)
{
LruList.Remove(url);
LruList.AddFirst(readNode);
}
statusCode = 200;
return fileByteStream;
}
else
finally
{
LruList.AddFirst(url);

RWLock.ReleaseReaderLock();
}
statusCode = 200;
RWLock.ReleaseReaderLock();
return fileByteStream;
}
else //when the file is not in fileBuffer,read the file from local fileSystem
{
Expand Down Expand Up @@ -420,4 +426,4 @@ internal void OnFileOrDirectoryDeleted(string url)
}

}
}
}

0 comments on commit 514f6c7

Please sign in to comment.