📄 log.cs
字号:
}
catch (Exception e)
{
throw Trace.error(Trace.FILE_IO_ERROR, sFileProperties + " " + e);
}
}
/**
* Method declaration
*
*
* @throws Exception
*/
private void create()
{
if (Trace.TRACE)
{
Trace.trace(sName);
}
XmlTextWriter writer = new XmlTextWriter(sFileProperties, null);
writer.Formatting = Formatting.Indented;
writer.Indentation=4;
writer.WriteStartDocument();
writer.WriteComment("SharpHSQL Configuration");
writer.WriteProcessingInstruction("Instruction","Configuration Record");
writer.WriteStartElement("Properties","");
writer.WriteStartAttribute("LogFile","");
writer.WriteString(sFileScript);
writer.WriteEndAttribute();
writer.WriteStartAttribute("DataFile","");
writer.WriteString(sFileCache);
writer.WriteEndAttribute();
writer.WriteStartAttribute("Backup","");
writer.WriteString(sFileBackup);
writer.WriteEndAttribute();
writer.WriteStartAttribute("Version","");
writer.WriteString("1.0");
writer.WriteEndAttribute();
writer.WriteStartAttribute("ReadOnly","");
writer.WriteString("false");
writer.WriteEndAttribute();
writer.WriteStartAttribute("Modified","");
writer.WriteString("no");
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();
saveProperties();
}
/**
* Method declaration
*
*
* @return
*
* @throws Exception
*/
private bool isAlreadyOpen()
{
// reading the last modified, wait 3 seconds, read again.
// if the same information was read the file was not changed
// and is probably, except the other process is blocked
if (Trace.TRACE)
{
Trace.trace();
}
File f = new File(sFileScript);
DateTime l1 = f.LastWriteTime;
try
{
Thread.Sleep(3000);
}
catch (Exception e) {}
DateTime l2 = f.LastWriteTime;
if (l1 != l2)
{
return true;
}
// check by trying to delete the properties file
// this will not work if some application has the file open
// this is why the properties file is kept open when running ;-)
// todo: check if this works in all operating systems
closeProperties();
if (Trace.TRACE)
{
Trace.trace();
}
try
{
(new File(sFileProperties)).Delete();
}
catch (Exception e)
{
return true;
}
// the file was deleted, so recreate it now
saveProperties();
return false;
}
/**
* Method declaration
*
*
* @throws Exception
*/
private void loadProperties()
{
try
{
XmlTextReader reader = new XmlTextReader(sFileProperties);
//Read the tokens from the reader
while ( reader.Read() )
{
if (XmlNodeType.Element == reader.NodeType)
{
sFileScript = reader.GetAttribute("LogFile");
sFileCache = reader.GetAttribute("DataFile");
sFileBackup = reader.GetAttribute("Backup");
sModified = reader.GetAttribute("Modified");
sVersion = reader.GetAttribute("Version");
bReadOnly = reader.GetAttribute("ReadOnly").ToLower().Equals("true");
}
}
reader.Close();
}
catch (Exception e)
{
Console.WriteLine("Property File Exeception:", e.ToString());
}
if (Trace.TRACE)
{
Trace.trace();
}
}
/**
* Method declaration
*
*
* @throws Exception
*/
private void saveProperties()
{
XmlTextWriter writer = new XmlTextWriter(sFileProperties, null);
writer.Formatting = Formatting.Indented;
writer.Indentation=4;
writer.WriteStartDocument();
writer.WriteComment("SharpHSQL Configuration");
writer.WriteProcessingInstruction("Instruction","Configuration Record");
writer.WriteStartElement("Properties","");
writer.WriteStartAttribute("LogFile","");
writer.WriteString(sFileScript);
writer.WriteEndAttribute();
writer.WriteStartAttribute("DataFile","");
writer.WriteString(sFileCache);
writer.WriteEndAttribute();
writer.WriteStartAttribute("Backup","");
writer.WriteString(sFileBackup);
writer.WriteEndAttribute();
writer.WriteStartAttribute("Version","");
writer.WriteString(sVersion);
writer.WriteEndAttribute();
writer.WriteStartAttribute("ReadOnly","");
if (bReadOnly == true)
{
writer.WriteString("true");
}
else
{
writer.WriteString("false");
}
writer.WriteEndAttribute();
writer.WriteStartAttribute("Modified","");
writer.WriteString(sModified);
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();
closeProperties();
if (Trace.TRACE)
{
Trace.trace();
}
}
/**
* Method declaration
*
*
* @throws Exception
*/
private void backup()
{
if (Trace.TRACE)
{
Trace.trace();
// if there is no cache file then backup is not necessary
}
if (!(new File(sFileCache)).Exists)
{
return;
}
try
{
DateTime time = DateTime.Now;
// create a '.new' file; rename later
BinaryWriter f = new BinaryWriter(new FileStream(sFileBackup + ".new",FileMode.OpenOrCreate,FileAccess.Write));
byte[] b = new byte[COPY_BLOCK_SIZE];
BinaryReader fin = new BinaryReader(new FileStream(sFileCache,FileMode.Open,FileAccess.Read));
while (true)
{
int l = fin.Read(b, 0, COPY_BLOCK_SIZE);
if (l == 0)
{
break;
}
f.Write(b, 0, l);
}
f.Close();
fin.Close();
TimeSpan execution = DateTime.Now.Subtract(time);
if (Trace.TRACE)
{
Trace.trace(execution.TotalMilliseconds.ToInt64());
}
}
catch (Exception e)
{
throw Trace.error(Trace.FILE_IO_ERROR, sFileBackup);
}
}
/**
* Method declaration
*
*
* @throws Exception
*/
private void restoreBackup()
{
if (Trace.TRACE)
{
Trace.trace("not closed last time!");
}
if (!(new File(sFileBackup)).Exists)
{
// the backup don't exists because it was never made or is empty
// the cache file must be deleted in this case
(new File(sFileCache)).Delete();
return;
}
try
{
DateTime time = DateTime.Now;
BinaryReader f = new BinaryReader(new FileStream(sFileBackup,FileMode.Open,FileAccess.Read));
BinaryWriter cache = new BinaryWriter(new FileStream(sFileCache,FileMode.OpenOrCreate,FileAccess.Write));
byte[] b = new byte[COPY_BLOCK_SIZE];
while (true)
{
int l = f.Read(b, 0, COPY_BLOCK_SIZE);
if (l == 0)
{
break;
}
cache.Write(b, 0, l);
}
cache.Close();
f.Close();
TimeSpan execution = DateTime.Now.Subtract(time);
if (Trace.TRACE)
{
Trace.trace(execution.TotalMilliseconds.ToInt64());
}
}
catch (Exception e)
{
throw Trace.error(Trace.FILE_IO_ERROR, sFileBackup);
}
}
/**
* Method declaration
*
*
* @throws Exception
*/
private void openScript()
{
if (Trace.TRACE)
{
Trace.trace();
}
try
{
// todo: use a compressed stream
wScript = new StreamWriter(sFileScript,true);
}
catch (Exception e)
{
Trace.error(Trace.FILE_IO_ERROR, sFileScript);
}
}
/**
* Method declaration
*
*
* @throws Exception
*/
private void closeScript()
{
if (Trace.TRACE)
{
Trace.trace();
}
try
{
if (wScript != null)
{
wScript.Close();
wScript = null;
}
}
catch (Exception e)
{
Trace.error(Trace.FILE_IO_ERROR, sFileScript);
}
}
/**
* Method declaration
*
*
* @throws Exception
*/
private void runScript()
{
if (Trace.TRACE)
{
Trace.trace();
}
if (!(new File(sFileScript)).Exists)
{
return;
}
bRestoring = true;
dDatabase.setReferentialIntegrity(false);
ArrayList channel = new ArrayList();
channel.Add(cSystem);
Channel current = cSystem;
int size = 1;
try
{
DateTime time = DateTime.Now;
StreamReader r = new StreamReader(sFileScript);
while (true)
{
string s = r.ReadLine();
if (s == null)
{
break;
}
if (s.StartsWith("/*C"))
{
int id = Int32.FromString(s.Substring(3,(s.IndexOf('*', 4)-3)));
if (id > channel.Count)
{
current = new Channel(cSystem, id);
channel.Insert(id, current);
dDatabase.registerChannel(current);
}
else
{
current = (Channel) channel[id - 1];
}
s = s.Substring(s.IndexOf('/', 1) + 1);
}
if (!s.Equals(""))
{
dDatabase.execute(s, current);
}
if (s.Equals("DISCONNECT"))
{
int id = current.getId();
current = new Channel(cSystem, id);
channel.RemoveAt(id);
channel.Insert(id, current);
}
}
r.Close();
for (int i = 0; i < size; i++)
{
current = (Channel) channel[i];
if (current != null)
{
current.rollback();
}
}
TimeSpan execution = DateTime.Now.Subtract(time);
if (Trace.TRACE)
{
Trace.trace(execution.TotalMilliseconds.ToInt64());
}
}
catch (IOException e)
{
throw Trace.error(Trace.FILE_IO_ERROR, sFileScript + " " + e);
}
dDatabase.setReferentialIntegrity(true);
bRestoring = false;
}
/**
* Method declaration
*
*
* @param full
*
* @throws Exception
*/
private void writeScript(bool full)
{
if (Trace.TRACE)
{
Trace.trace();
// create script in '.new' file
}
(new File(sFileScript + ".new")).Delete();
// script; but only positions of cached tables, not full
scriptToFile(dDatabase, sFileScript + ".new", full, cSystem);
}
/**
* Method declaration
*
*
* @param w
* @param s
*
* @throws IOException
*/
private static void writeLine(StreamWriter w, string s)
{
w.WriteLine(s);
}
/**
* Method declaration
*
*
* @param r
*
* @return
*
* @throws IOException
*/
private static string readLine(TextReader r)
{
string s = r.ReadLine();
return stringConverter.asciiToUnicode(s);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -