Commit c6833af5 authored by Daniel Espinosa Ortiz's avatar Daniel Espinosa Ortiz

server: improved adding using namespaces

Improves performance on adding already in use namespace.
parent 9b92a19f
......@@ -61,8 +61,7 @@ public class GVls.GServer : GLib.Object, ContainerHashable, Server
public void add_to_root_server (Server rserver) throws GLib.Error {
rserver.servers.remove (this);
rserver.add_server (this);
rserver.servers.add (this);
_root_server = rserver;
}
public void parse (GLib.File file) throws GLib.Error {
......@@ -161,17 +160,17 @@ public class GVls.GServer : GLib.Object, ContainerHashable, Server
_symbols.add (sc);
}
}
var l = find_using_namespaces ();
for (int i = 0; i < l.get_n_items (); i++) {
var so = l.get_item (i) as StringObject;
if (root_server != null) {
root_server.add_namespace (so.val);
} else {
try {
add_namespace (so.val);
} catch (GLib.Error e) {
message ("Error adding namespace: %s: %s", so.val, e.message);
}
if (root_server != null) {
try {
root_server.add_all_using_namespaces ();
} catch (GLib.Error e) {
message ("Error adding namespace at root server: %s", e.message);
}
} else {
try {
add_using_namespaces ();
} catch (GLib.Error e) {
message ("Error adding namespace: %s", e.message);
}
}
CodeContext.pop ();
......@@ -181,6 +180,9 @@ public class GVls.GServer : GLib.Object, ContainerHashable, Server
public GLib.ListModel find_using_namespaces () {
var list = new GLib.ListStore (typeof (StringObject));
if (source == null) {
return list;
}
foreach (Vala.UsingDirective d in source.current_using_directives) {
var s = new StringObject ();
if (d.namespace_symbol != null) {
......
......@@ -79,7 +79,7 @@ public interface GVls.Server : GLib.Object, ContainerHashable
k.key = s.hash ();
var isr = servers.find (k);
if (isr != null) return;
servers.add (s);
s.add_to_root_server (this);
}
public abstract Symbol? find_at (Location loc) throws GLib.Error;
/**
......@@ -185,12 +185,23 @@ public interface GVls.Server : GLib.Object, ContainerHashable
*
*/
public virtual bool add_namespace (string ns) throws GLib.Error {
if (root_server != null) {
if (root_server.get_symbol (ns) != null) {
return false;
}
} else if (get_symbol (ns) != null) {
return false;
}
var nsf = find_namespace_file (ns);
if (nsf == null) {
throw new ServerError.NO_VAPI_DIR_ERROR ("Namespace not found in current VAPI dirs: %s", ns);
}
var srv = create_pkg_server (nsf);
add_server (srv);
if (root_server != null) {
root_server.add_server (srv);
} else {
add_server (srv);
}
return true;
}
/**
......@@ -219,7 +230,8 @@ public interface GVls.Server : GLib.Object, ContainerHashable
return;
}
for (int i = 0; i < nss.get_n_items (); i++) {
add_namespace ((nss.get_item (i) as StringObject).val);
var ons = nss.get_item (i) as StringObject;
add_namespace (ons.val);
}
}
/**
......
......@@ -305,19 +305,14 @@ class Tests {
Server server = new GServer () as Server;
Server server1 = new GServer () as Server;
Server server2 = new GServer () as Server;
Server server3 = new GServer () as Server;
try {
server3.parse (file2);
message ("Servers: %u", server3.servers.get_n_items ());
assert (server3.servers.get_n_items () == 1);
assert (server3.get_symbol ("Cairo") != null);
server1.parse (file1);
server2.parse (file2);
server.add_server (server1);
server.add_server (server2);
assert (server.servers.get_n_items () == 2);
server.add_all_using_namespaces ();
server1.parse (file1);
server2.parse (file2);
message ("Servers: %u", server.servers.get_n_items ());
assert (server.servers.get_n_items () == 4);
assert (server.get_symbol ("GLib") != null);
assert (server.get_symbol ("string") != null);
assert (server.get_symbol ("string.replace") != null);
assert (server.get_symbol ("Cairo") != null);
......
......@@ -29,15 +29,11 @@ class Tests {
try {
Test.timer_start ();
for (int i = 0; i < 100; i++) {
var srv = new GServer ();
server.add_server (srv);
string t = """
using GLib;
""";
srv.content = t;
message ("Trying to add GLib ns: %d", i);
server.add_namespace ("GLib");
}
var t1 = Test.timer_elapsed ();
message ("In seconts: t1: %s;", t1.to_string ());
message ("In seconds: t1: %s;", t1.to_string ());
} catch (GLib.Error e) {
message ("Error: %s", e.message);
assert_not_reached ();
......
......@@ -157,8 +157,9 @@ public class App {
}
""";
assert (server.get_symbol ("GLib") != null);
message ("Current Servers: %u", server.servers.get_n_items ());
assert (server.servers.get_n_items () == 3);
assert (server.servers.get_n_items () == 2);
assert (server.get_symbol ("t") != null);
});
win.show_all ();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment