db_atoms database_rep::query (tree ql, db_time t, int limit) { //cout << "query " << ql << ", " << t << ", " << limit << LF; ql= normalize_query (ql); //cout << "normalized query " << ql << ", " << t << ", " << limit << LF; db_atoms ids= ansatz (ql, t); //cout << "ansatz ids= " << ids << LF; bool sort_flag= false; if (is_tuple (ql)) for (int i=0; i<N(ql); i++) sort_flag= sort_flag || is_tuple (ql[i], "order", 2); ids= filter (ids, ql, t, max (limit, sort_flag? 1000: 0)); //cout << "filtered ids= " << ids << LF; for (int i=0; i<N(ql); i++) { if (is_tuple (ql[i], "modified", 2) && is_atomic (ql[i][1]) && is_atomic (ql[i][2]) && is_quoted (ql[i][1]->label) && is_quoted (ql[i][2]->label)) { string t1= scm_unquote (ql[i][1]->label); string t2= scm_unquote (ql[i][2]->label); if (is_int (t1) && is_int (t2)) ids= filter_modified (ids, (db_time) as_long_int (t1), (db_time) as_long_int (t2)); } } //cout << "filtered on modified ids= " << ids << LF; ids= sort_results (ids, ql, t); //cout << "sorted ids= " << ids << LF; if (N(ids) > limit) ids= range (ids, 0, limit); return ids; }
/* needs to be already locked */ static void buffer_add_query(double duration, const char * query) { static char header[256]; int len, hlen; char * q = NULL; /* do we need to normalize the query? */ if (normalize) { q = normalize_query(q); } else { q = (char*)query; } len = strlen(q); /* prepare the line prefix */ format_prefix(header, 256, MyBackendId, duration, len); hlen = strlen(header); /* add the query to the buffer */ LWLockAcquire(query_buffer->lock, LW_EXCLUSIVE); if (query_buffer->next + hlen + len + 2 > buffer_size) { LWLockAcquire(log_file->lock, LW_EXCLUSIVE); buffer_write(); LWLockRelease(log_file->lock); query_buffer->next = 0; } if (len + hlen + 2 > buffer_size) { LWLockAcquire(log_file->lock, LW_EXCLUSIVE); query_write(duration, q, len, header, hlen); LWLockRelease(log_file->lock); } else { memcpy(&(query_buffer->buffer[query_buffer->next]), header, hlen); query_buffer->next = query_buffer->next + hlen; memcpy(&(query_buffer->buffer[query_buffer->next]), q, len); query_buffer->buffer[query_buffer->next + len] = '\n'; query_buffer->next = query_buffer->next + len + 1; } LWLockRelease(query_buffer->lock); }