Skip to content

Commit

Permalink
Make writing buffer more for listsymbols and findfiles. This solves a…
Browse files Browse the repository at this point in the history
… problem where the socket gets the finished event before receiving all write events.
  • Loading branch information
Andersbakken committed Sep 14, 2012
1 parent 6439830 commit a5d1217
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/Connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Connection::Connection(LocalClient* client)

Connection::~Connection()
{
destroyed()(this);
mDestroyed(this);
delete mClient;
}

Expand Down
2 changes: 1 addition & 1 deletion src/FindFileJob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "FileManager.h"

FindFileJob::FindFileJob(const QueryMessage &query, const shared_ptr<Project> &project)
: Job(query, 0, project)
: Job(query, WriteBuffered, project)
{
const ByteArray q = query.query();
if (!q.isEmpty()) {
Expand Down
44 changes: 27 additions & 17 deletions src/Job.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ Job::~Job()
{
delete mPathFilters;
delete mPathFiltersRegExp;
if (mId != -1)
EventLoop::instance()->postEvent(Server::instance(), new JobOutputEvent(this, mBuffer, true));
}

bool Job::write(const ByteArray &out)
bool Job::write(const ByteArray &out, unsigned flags)
{
if (mJobFlags & WriteUnfiltered || filter(out)) {
if (mJobFlags & QuoteOutput) {
Expand All @@ -57,35 +59,45 @@ bool Job::write(const ByteArray &out)
}
}
o.truncate(l);
return writeRaw(o);
return writeRaw(o, flags);
} else {
return writeRaw(out);
return writeRaw(out, flags);
}
}
return true;
}

bool Job::writeRaw(const ByteArray &out)
bool Job::writeRaw(const ByteArray &out, unsigned flags)
{
switch (mMax) {
case 0:
return false;
case -1:
break;
default:
--mMax;
break;
if (!(flags & IgnoreMax)) {
switch (mMax) {
case 0:
case -1:
break;
default:
--mMax;
break;
}
}

if (mJobFlags & OutputSignalEnabled) {
output()(out);
} else if (mJobFlags & WriteBuffered) {
enum { BufSize = 1024 };
if (mBuffer.size() + out.size() + 1 > BufSize) {
EventLoop::instance()->postEvent(Server::instance(), new JobOutputEvent(this, mBuffer, false));
mBuffer.clear();
mBuffer.reserve(1024);
}
mBuffer.append(out);
mBuffer.append('\n');
} else {
EventLoop::instance()->postEvent(Server::instance(), new JobOutputEvent(this, out));
EventLoop::instance()->postEvent(Server::instance(), new JobOutputEvent(this, out, false));
}
return true;
}

bool Job::write(const Location &location, const CursorInfo &ci)
bool Job::write(const Location &location, const CursorInfo &ci, unsigned flags)
{
if (ci.symbolLength) {
char buf[1024];
Expand All @@ -100,7 +112,7 @@ bool Job::write(const Location &location, const CursorInfo &ci)
for (Set<Location>::const_iterator rit = ci.references.begin(); rit != ci.references.end(); ++rit) {
const Location &l = *rit;
snprintf(buf, sizeof(buf), " %s", l.key().constData());
return write(buf);
return write(buf, flags);
}
}
return true;
Expand All @@ -109,8 +121,6 @@ bool Job::write(const Location &location, const CursorInfo &ci)
void Job::run()
{
execute();
if (mId != -1)
EventLoop::instance()->postEvent(Server::instance(), new JobCompleteEvent(mId));
}

unsigned Job::keyFlags() const
Expand Down
31 changes: 13 additions & 18 deletions src/Job.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class Job : public ThreadPool::Job, public AbortInterface
None = 0x0,
WriteUnfiltered = 0x1,
QuoteOutput = 0x2,
OutputSignalEnabled = 0x4
OutputSignalEnabled = 0x4,
WriteBuffered = 0x8
};
enum { Priority = 10 };
Job(const QueryMessage &msg, unsigned jobFlags, const shared_ptr<Project> &proj);
Expand All @@ -32,9 +33,13 @@ class Job : public ThreadPool::Job, public AbortInterface
bool hasFilter() const { return mPathFilters || mPathFiltersRegExp; }
int id() const { return mId; }
void setId(int id) { mId = id; }
bool write(const ByteArray &out);
bool writeRaw(const ByteArray &out);
bool write(const Location &location, const CursorInfo &info);
enum WriteFlag {
NoWriteFlags = 0x0,
IgnoreMax = 0x1
};
bool write(const ByteArray &out, unsigned flags = NoWriteFlags);
bool writeRaw(const ByteArray &out, unsigned flags = NoWriteFlags);
bool write(const Location &location, const CursorInfo &info, unsigned flags = NoWriteFlags);
unsigned jobFlags() const { return mJobFlags; }
void setJobFlags(unsigned flags) { mJobFlags = flags; }
unsigned queryFlags() const { return mQueryFlags; }
Expand All @@ -54,6 +59,7 @@ class Job : public ThreadPool::Job, public AbortInterface
List<ByteArray> *mPathFilters;
List<RegExp> *mPathFiltersRegExp;
int mMax;
ByteArray mBuffer;
};

inline bool Job::filter(const ByteArray &val) const
Expand All @@ -74,28 +80,17 @@ inline bool Job::filter(const ByteArray &val) const
return false;
}

class JobCompleteEvent : public Event
{
public:
enum { Type = 1 };
JobCompleteEvent(int i)
: Event(Type), id(i)
{}

const int id;
};


class JobOutputEvent : public Event
{
public:
enum { Type = 2 };
JobOutputEvent(Job *j, const ByteArray &o)
: Event(Type), job(j), out(o)
JobOutputEvent(Job *j, const ByteArray &o, bool f)
: Event(Type), job(j), out(o), finish(f)
{}

Job *job;
const ByteArray out;
const bool finish;
};

#endif
12 changes: 9 additions & 3 deletions src/ListSymbolsJob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
#include "Log.h"
#include "RTags.h"

enum {
DefaultFlags = Job::WriteUnfiltered|Job::WriteBuffered,
ElispFlags = DefaultFlags|Job::QuoteOutput
};


ListSymbolsJob::ListSymbolsJob(const QueryMessage &query, const shared_ptr<Project> &proj)
: Job(query, query.flags() & QueryMessage::ElispList ? Job::QuoteOutput|Job::WriteUnfiltered : Job::WriteUnfiltered, proj),
: Job(query, query.flags() & QueryMessage::ElispList ? ElispFlags : DefaultFlags, proj),
string(query.query())
{
}
Expand All @@ -18,7 +24,7 @@ void ListSymbolsJob::execute()
const bool elispList = queryFlags & QueryMessage::ElispList;

if (elispList)
writeRaw("(list");
writeRaw("(list", IgnoreMax);
if (project()->indexer) {
Scope<const SymbolNameMap &> scope = project()->lockSymbolNamesForRead();
const SymbolNameMap &map = scope.data();
Expand Down Expand Up @@ -76,7 +82,7 @@ void ListSymbolsJob::execute()
}

if (elispList) {
writeRaw(")");
writeRaw(")", IgnoreMax);
} else {
if (queryFlags & QueryMessage::ReverseSort) {
std::sort(out.begin(), out.end(), std::greater<ByteArray>());
Expand Down
18 changes: 8 additions & 10 deletions src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -864,21 +864,19 @@ void Server::onMakefileModified(const Path &path)
void Server::event(const Event *event)
{
switch (event->type()) {
case JobCompleteEvent::Type: {
const JobCompleteEvent *e = static_cast<const JobCompleteEvent*>(event);
Map<int, Connection*>::iterator it = mPendingLookups.find(e->id);
if (it == mPendingLookups.end())
return;
it->second->finish();
break; }
case JobOutputEvent::Type: {
const JobOutputEvent *e = static_cast<const JobOutputEvent*>(event);
Map<int, Connection*>::iterator it = mPendingLookups.find(e->job->id());
if (it == mPendingLookups.end())
if (it == mPendingLookups.end()) {
break;
}
ResponseMessage msg(e->out);
if (it->second->isConnected() && !it->second->send(&msg)) {
e->job->abort();
if (it->second->isConnected()) {
if (!it->second->send(&msg)) {
e->job->abort();
} else if (e->finish) {
it->second->finish();
}
}
break; }
case MakefileParserDoneEvent::Type: {
Expand Down

0 comments on commit a5d1217

Please sign in to comment.