Hi all,

For the TODO item:

* Cache datatypes used by dlgColumn as reloading them when adding multiple columns to a new table can be slow over WANs.

Attached is a patch for it. Please give your comments on it.

--
Regards,
Sachin Srivastava
www.enterprisedb.com

Index: include/dlg/dlgProperty.h
===================================================================
--- include/dlg/dlgProperty.h   (revision 7964)
+++ include/dlg/dlgProperty.h   (working copy)
@@ -15,6 +15,7 @@
 
 
 #include <wx/notebook.h>
+#include <wx/dynarray.h>
 #include "schema/pgObject.h"
 #include "db/pgConn.h"
 #include "ctl/ctlSecurityPanel.h"
@@ -29,6 +30,23 @@
 #define lstColumns      CTRL_LISTVIEW("lstColumns")
 #define cbColumns       CTRL_COMBOBOX("cbColumns")
 
+class dataType
+{
+public:
+    void SetTypename(wxString name);
+    wxString GetTypename();
+ 
+    void SetOid(OID id);
+    OID GetOid();
+
+private:
+    wxString typeName;
+    OID      oid;
+       
+};
+
+WX_DEFINE_ARRAY(dataType *, dataTypeCache);
+
 class dlgProperty : public DialogWithHelp
 {
 public:
@@ -51,6 +69,7 @@
     virtual wxString GetHelpPage(bool forCreate) const { return wxEmptyString; 
}
     void SetConnection(pgConn *conn) { connection=conn; }
     void SetDatabase(pgDatabase *db);
+    void SetDatatypeCache(dataTypeCache datatypeCache);
     virtual int Go(bool modal=false);
     virtual void CheckChange() =0;
 
@@ -120,6 +139,7 @@
     bool readOnly;
     bool processing;
     pgaFactory *factory;
+    dataTypeCache datatypeCache;
 
 private:
     bool tryUpdate(wxTreeItemId collectionItem);
Index: include/dlg/dlgTable.h
===================================================================
--- include/dlg/dlgTable.h      (revision 7964)
+++ include/dlg/dlgTable.h      (working copy)
@@ -29,10 +29,12 @@
     wxString GetSql();
     pgObject *CreateObject(pgCollection *collection);
     pgObject *GetObject();
+    ~dlgTable();
 
 private:
     pgSchema *schema;
     pgTable *table;
+    dataTypeCache datatypeCache;
 
     void OnOK(wxCommandEvent &ev);
     void OnChangeTable(wxCommandEvent &ev);
@@ -58,6 +60,7 @@
     void FillConstraint();
     void FillAutoVacuumParameters(wxString& setString, wxString& resetStr,
                                  const wxString& parameter, const wxString& 
val);
+    void PopulateDatatypeCache();
     wxString GetItemConstraintType(ctlListView *list, long pos);
     bool hasPK;
 
Index: dlg/dlgProperty.cpp
===================================================================
--- dlg/dlgProperty.cpp (revision 7964)
+++ dlg/dlgProperty.cpp (working copy)
@@ -70,6 +70,26 @@
 };
 
 
+void dataType::SetOid(OID id)
+{
+    oid = id;
+}
+
+void dataType::SetTypename(wxString name)
+{
+    typeName = name;
+}
+
+OID dataType::GetOid()
+{
+   return oid;
+}
+
+wxString dataType::GetTypename()
+{
+   return typeName;
+}
+
 #define CTRLID_CHKSQLTEXTFIELD 1000
 
 
@@ -192,6 +212,10 @@
         connection=db->GetConnection();
 }
 
+void dlgProperty::SetDatatypeCache(dataTypeCache dtCache)
+{
+    datatypeCache=dtCache;
+}
     
 void dlgProperty::EnableOK(bool enable)
 {
@@ -1189,20 +1213,39 @@
     FillDatatype(cb, 0, withDomains);
 }
 
-
 void dlgTypeProperty::FillDatatype(ctlComboBox *cb, ctlComboBox *cb2, bool 
withDomains)
 {
-    DatatypeReader tr(database, withDomains);
-    while (tr.HasMore())
+     
+    if (datatypeCache.IsEmpty())
     {
-        pgDatatype dt=tr.GetDatatype();
+        // A column dialog is directly called, no datatype caching is done.
+        // Fetching datatypes from server. 
+        DatatypeReader tr(database, withDomains);
+        while (tr.HasMore())
+        {
+            pgDatatype dt=tr.GetDatatype();
 
-        AddType(wxT("?"), tr.GetOid(), dt.FullName());
-        cb->Append(dt.FullName());
-        if (cb2)
-            cb2->Append(dt.FullName());
-        tr.MoveNext();
+            AddType(wxT("?"), tr.GetOid(), dt.FullName());
+            cb->Append(dt.FullName());
+            if (cb2)
+                cb2->Append(dt.FullName());
+            tr.MoveNext();
+        }
     }
+    else
+    { 
+        // A column dialog is called from a table dialog where we have already 
cached the datatypes.  
+        // Using cached datatypes.
+        size_t i;
+        for (i = 0; i < datatypeCache.GetCount(); i++)
+        {
+            AddType(wxT("?"), datatypeCache.Item(i)->GetOid(), 
datatypeCache.Item(i)->GetTypename()); 
+            cb->Append(datatypeCache.Item(i)->GetTypename());
+            if (cb2)
+                cb2->Append(datatypeCache.Item(i)->GetTypename());
+        }
+    }
+
 }
 
 
Index: dlg/dlgTable.cpp
===================================================================
--- dlg/dlgTable.cpp    (revision 7964)
+++ dlg/dlgTable.cpp    (working copy)
@@ -31,9 +31,9 @@
 #include "schema/pgCheck.h"
 #include "schema/pgForeignKey.h"
 #include "schema/pgIndexConstraint.h"
+#include "schema/pgDatatype.h"
 
 
-
 #define stHasOids       CTRL_STATIC("stHasOids")
 #define chkHasOids      CTRL_CHECKBOX("chkHasOids")
 #define lbTables        CTRL_LISTBOX("lbTables")
@@ -185,6 +185,13 @@
     lstConstraints->CreateColumns(0, _("Constraint name"), _("Definition"), 
90);
 }
 
+dlgTable::~dlgTable()
+{
+    //Clear the cached datatypes
+    size_t i;
+    for (i = 0; i < datatypeCache.GetCount(); i++)
+        delete datatypeCache.Item(i);
+}
 
 pgObject *dlgTable::GetObject()
 {
@@ -199,6 +206,7 @@
     AddGroups();
     AddUsers(cbOwner);
     PrepareTablespace(cbTablespace);
+    PopulateDatatypeCache();
 
     hasPK=false;
 
@@ -1743,12 +1751,30 @@
     CheckChange();
 }
 
+// Cache datatypes to avoid multiple calls to server when adding multiple 
columns to a table. 
+void dlgTable::PopulateDatatypeCache()
+{
+    DatatypeReader tr(database, true);
+    while (tr.HasMore())
+    {
+        pgDatatype dt=tr.GetDatatype();
 
+        dataType *dtype = new dataType();
+        dtype->SetOid(tr.GetOid());
+        dtype->SetTypename(dt.FullName());
+        datatypeCache.Add(dtype);
+
+        tr.MoveNext();
+    }
+}
+
+
 void dlgTable::OnAddCol(wxCommandEvent &ev)
 {
     dlgColumn col(&columnFactory, mainForm, NULL, table);
     col.CenterOnParent();
     col.SetDatabase(database);
+    col.SetDatatypeCache(datatypeCache);   
     if (col.Go(true) != wxID_CANCEL)
     {
         long pos = lstColumns->AppendItem(columnFactory.GetIconId(), 
col.GetName(), col.GetDefinition());
-- 
Sent via pgadmin-hackers mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgadmin-hackers

Reply via email to