good bye ADO.NET to assets, notice faster startup time

ThreadPoolClientBranch
Sean Dague 2008-01-14 20:42:27 +00:00
parent db40ffb43f
commit 20cf62b417
1 changed files with 67 additions and 104 deletions

View File

@ -45,76 +45,86 @@ namespace OpenSim.Framework.Data.SQLite
/// <summary> /// <summary>
/// Artificial constructor called upon plugin load /// Artificial constructor called upon plugin load
/// </summary> /// </summary>
private const string SelectAssetSQL = "select * from assets where UUID=:UUID";
private const string DeleteAssetSQL = "delete from assets where UUID=:UUID";
private const string InsertAssetSQL = "insert into assets(UUID, Name, Description, Type, InvType, Local, Temporary, Data) values(:UUID, :Name, :Description, :Type, :InvType, :Local, :Temporary, :Data)";
private const string UpdateAssetSQL = "update assets set Name=:Name, Description=:Description, Type=:Type, InvType=:InvType, Local=:Local, Temporary=:Temporary, Data=:Data where UUID=:UUID";
private const string assetSelect = "select * from assets"; private const string assetSelect = "select * from assets";
private DataSet ds; private SqliteConnection m_conn;
private SqliteDataAdapter da;
public void Initialise(string dbfile, string dbname) public void Initialise(string dbfile, string dbname)
{ {
SqliteConnection conn = new SqliteConnection("URI=file:" + dbfile + ",version=3"); m_conn = new SqliteConnection("URI=file:" + dbfile + ",version=3");
TestTables(conn); m_conn.Open();
ds = new DataSet();
da = new SqliteDataAdapter(new SqliteCommand(assetSelect, conn));
lock (ds)
{
ds.Tables.Add(createAssetsTable());
setupAssetCommands(da, conn);
try
{
da.Fill(ds.Tables["assets"]);
}
catch (Exception e)
{
MainLog.Instance.Verbose("SQLITE", e.ToString());
}
}
TestTables(m_conn);
return; return;
} }
public AssetBase FetchAsset(LLUUID uuid) public AssetBase FetchAsset(LLUUID uuid)
{ {
AssetBase asset = new AssetBase();
DataRow row = ds.Tables["assets"].Rows.Find(Util.ToRawUuidString(uuid)); using (SqliteCommand cmd = new SqliteCommand(SelectAssetSQL, m_conn))
if (row != null)
{ {
return buildAsset(row); cmd.Parameters.Add(new SqliteParameter(":UUID", uuid.UUID.ToString()));
} using (IDataReader reader = cmd.ExecuteReader())
else {
{ reader.Read();
return null; if (reader != null)
{
return buildAsset(reader);
}
else
{
return null;
}
}
} }
} }
public void CreateAsset(AssetBase asset) public void CreateAsset(AssetBase asset)
{ {
// no difference for now if (ExistsAsset(asset.FullID))
UpdateAsset(asset); {
UpdateAsset(asset);
}
else
{
using (SqliteCommand cmd = new SqliteCommand(InsertAssetSQL, m_conn))
{
cmd.Parameters.Add(new SqliteParameter(":UUID", asset.FullID.UUID.ToString()));
cmd.Parameters.Add(new SqliteParameter(":Name", asset.Name));
cmd.Parameters.Add(new SqliteParameter(":Description", asset.Description));
cmd.Parameters.Add(new SqliteParameter(":Type", asset.Type));
cmd.Parameters.Add(new SqliteParameter(":InvType", asset.InvType));
cmd.Parameters.Add(new SqliteParameter(":Local", asset.Local));
cmd.Parameters.Add(new SqliteParameter(":Temporary", asset.Temporary));
cmd.Parameters.Add(new SqliteParameter(":Data", asset.Data));
cmd.ExecuteNonQuery();
}
}
} }
public void UpdateAsset(AssetBase asset) public void UpdateAsset(AssetBase asset)
{ {
LogAssetLoad(asset); LogAssetLoad(asset);
DataTable assets = ds.Tables["assets"]; using (SqliteCommand cmd = new SqliteCommand(UpdateAssetSQL, m_conn))
lock (ds)
{ {
DataRow row = assets.Rows.Find(Util.ToRawUuidString(asset.FullID)); cmd.Parameters.Add(new SqliteParameter(":UUID", asset.FullID.UUID.ToString()));
if (row == null) cmd.Parameters.Add(new SqliteParameter(":Name", asset.Name));
{ cmd.Parameters.Add(new SqliteParameter(":Description", asset.Description));
row = assets.NewRow(); cmd.Parameters.Add(new SqliteParameter(":Type", asset.Type));
fillAssetRow(row, asset); cmd.Parameters.Add(new SqliteParameter(":InvType", asset.InvType));
assets.Rows.Add(row); cmd.Parameters.Add(new SqliteParameter(":Local", asset.Local));
} cmd.Parameters.Add(new SqliteParameter(":Temporary", asset.Temporary));
else cmd.Parameters.Add(new SqliteParameter(":Data", asset.Data));
{
fillAssetRow(row, asset); cmd.ExecuteNonQuery();
}
} }
} }
private void LogAssetLoad(AssetBase asset) private void LogAssetLoad(AssetBase asset)
@ -130,30 +140,27 @@ namespace OpenSim.Framework.Data.SQLite
public bool ExistsAsset(LLUUID uuid) public bool ExistsAsset(LLUUID uuid)
{ {
DataRow row = ds.Tables["assets"].Rows.Find(Util.ToRawUuidString(uuid)); return (FetchAsset(uuid) != null);
return (row != null);
} }
public void DeleteAsset(LLUUID uuid) public void DeleteAsset(LLUUID uuid)
{ {
lock (ds) using (SqliteCommand cmd = new SqliteCommand(DeleteAssetSQL, m_conn))
{ {
DataRow row = ds.Tables["assets"].Rows.Find(Util.ToRawUuidString(uuid)); cmd.Parameters.Add(new SqliteParameter(":UUID", uuid.UUID.ToString()));
if (row != null)
{ cmd.ExecuteNonQuery();
row.Delete();
}
} }
} }
public void CommitAssets() // force a sync to the database public void CommitAssets() // force a sync to the database
{ {
MainLog.Instance.Verbose("SQLITE", "Attempting commit"); MainLog.Instance.Verbose("SQLITE", "Attempting commit");
lock (ds) // lock (ds)
{ // {
da.Update(ds, "assets"); // da.Update(ds, "assets");
ds.AcceptChanges(); // ds.AcceptChanges();
} // }
} }
/*********************************************************************** /***********************************************************************
@ -189,7 +196,7 @@ namespace OpenSim.Framework.Data.SQLite
* *
**********************************************************************/ **********************************************************************/
private AssetBase buildAsset(DataRow row) private AssetBase buildAsset(IDataReader row)
{ {
// TODO: this doesn't work yet because something more // TODO: this doesn't work yet because something more
// interesting has to be done to actually get these values // interesting has to be done to actually get these values
@ -208,34 +215,6 @@ namespace OpenSim.Framework.Data.SQLite
} }
private void fillAssetRow(DataRow row, AssetBase asset)
{
row["UUID"] = Util.ToRawUuidString(asset.FullID);
row["Name"] = asset.Name;
if (asset.Description != null)
{
row["Description"] = asset.Description;
}
else
{
row["Description"] = " ";
}
row["Type"] = asset.Type;
row["InvType"] = asset.InvType;
row["Local"] = asset.Local;
row["Temporary"] = asset.Temporary;
row["Data"] = asset.Data;
// ADO.NET doesn't handle NULL very well
foreach (DataColumn col in ds.Tables["assets"].Columns)
{
if (row[col] == null)
{
row[col] = "";
}
}
}
/*********************************************************************** /***********************************************************************
* *
* Database Binding functions * Database Binding functions
@ -245,27 +224,11 @@ namespace OpenSim.Framework.Data.SQLite
* *
**********************************************************************/ **********************************************************************/
private void setupAssetCommands(SqliteDataAdapter da, SqliteConnection conn)
{
da.InsertCommand = createInsertCommand("assets", ds.Tables["assets"]);
da.InsertCommand.Connection = conn;
da.UpdateCommand = createUpdateCommand("assets", "UUID=:UUID", ds.Tables["assets"]);
da.UpdateCommand.Connection = conn;
SqliteCommand delete = new SqliteCommand("delete from assets where UUID = :UUID");
delete.Parameters.Add(createSqliteParameter("UUID", typeof (String)));
delete.Connection = conn;
da.DeleteCommand = delete;
}
private void InitDB(SqliteConnection conn) private void InitDB(SqliteConnection conn)
{ {
string createAssets = defineTable(createAssetsTable()); string createAssets = defineTable(createAssetsTable());
SqliteCommand pcmd = new SqliteCommand(createAssets, conn); SqliteCommand pcmd = new SqliteCommand(createAssets, conn);
conn.Open();
pcmd.ExecuteNonQuery(); pcmd.ExecuteNonQuery();
conn.Close();
} }
private bool TestTables(SqliteConnection conn) private bool TestTables(SqliteConnection conn)