return 0;
}
+/** @brief One entry in the search league */
struct search_entry {
char *word;
int n;
};
+/** @brief Add a word to the search league
+ * @param se Pointer to search league
+ * @param count Maximum size for search league
+ * @param nse Current size of search league
+ * @param word New word, or NULL
+ * @param n How often @p word appears
+ * @return New size of search league
+ */
+static int register_search_entry(struct search_entry *se,
+ int count,
+ int nse,
+ char *word,
+ int n) {
+ int i;
+
+ if(word && (nse < count || n > se[nse - 1].n)) {
+ /* Find the starting point */
+ if(nse == count)
+ i = nse - 1;
+ else
+ i = nse++;
+ /* Find the insertion point */
+ while(i > 0 && n > se[i - 1].n)
+ --i;
+ memmove(&se[i + 1], &se[i], (nse - i - 1) * sizeof *se);
+ se[i].word = word;
+ se[i].n = n;
+ }
+ return nse;
+}
+
/* find the top COUNT words in the search database */
static int search_league(struct vector *v, int count, DB_TXN *tid) {
struct search_entry *se;
cursor = trackdb_opencursor(trackdb_searchdb, tid);
se = xmalloc(count * sizeof *se);
+ /* Walk across the whole database counting up the number of times each
+ * word appears. */
while(!(err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
DB_NEXT))) {
if(word && wl == k.size && !strncmp(word, k.data, wl))
- ++n;
+ ++n; /* same word again */
else {
-#define FINALIZE() do { \
- if(word && (nse < count || n > se[nse - 1].n)) { \
- if(nse == count) \
- i = nse - 1; \
- else \
- i = nse++; \
- while(i > 0 && n > se[i - 1].n) \
- --i; \
- memmove(&se[i + 1], &se[i], (nse - i) * sizeof *se); \
- se[i].word = word; \
- se[i].n = n; \
- } \
-} while(0)
- FINALIZE();
+ nse = register_search_entry(se, count, nse, word, n);
word = xstrndup(k.data, wl = k.size);
n = 1;
}
}
if(trackdb_closecursor(cursor)) err = DB_LOCK_DEADLOCK;
if(err) return err;
- FINALIZE();
+ nse = register_search_entry(se, count, nse, word, n);
byte_xasprintf(&str, "Top %d search words:", nse);
vector_append(v, str);
for(i = 0; i < nse; ++i) {
static void stats_complete(struct stats_details *d) {
char *s;
-
+
if(!(d->exited && d->closed))
return;
byte_xasprintf(&s, "\n"
check:
${PYTHON} ${srcdir}/alltests
-EXTRA_DIST=alltests dtest.py nothing.py version.py dbversion.py search.py \
+EXTRA_DIST=alltests dtest.py dbversion.py search.py \
queue.py dump.py play.py
dtest.stop_daemon()
# Revert to default configuration
dtest.copyfile(configsave, config)
- print "Testing daemon manages to upgrade..."
+ print " testing daemon manages to upgrade..."
dtest.start_daemon()
assert dtest.check_files() == 0, "dtest.check_files"
+ print " getting server version"
+ c = disorder.client()
+ v = c.version()
+ print "Server version: %s" % v
+ print " getting server stats"
+ s = c.stats()
if __name__ == '__main__':
dtest.run()
+++ /dev/null
-#! /usr/bin/env python
-#
-# This file is part of DisOrder.
-# Copyright (C) 2007 Richard Kettlewell
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful, but
-# WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-# General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
-# USA
-#
-import dtest,time
-
-def test():
- """Just start the server and then stop it"""
- dtest.start_daemon()
-
-if __name__ == '__main__':
- dtest.run()
+++ /dev/null
-#! /usr/bin/env python
-#
-# This file is part of DisOrder.
-# Copyright (C) 2007 Richard Kettlewell
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful, but
-# WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-# General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
-# USA
-#
-import dtest,time,disorder
-
-def test():
- """Ask the server its version number"""
- dtest.start_daemon()
- c = disorder.client()
- v = c.version()
- print "Server version: %s" % v
-
-if __name__ == '__main__':
- dtest.run()