bool marky::Backend_SQLite::get_next(const State& state, selector_t selector, scorer_t scorer, const words_t& search_words, word_t& next) { #ifdef READ_DEBUG_ENABLED DEBUG("get_next(%s)", str(search_words).c_str()); #endif if (!bind_words(stmt_get_nexts, 1, search_words)) { sqlite3_clear_bindings(stmt_get_nexts); sqlite3_reset(stmt_get_nexts); return false; } bool ok = true; snippets_ptr_t snippets(new snippet_ptr_set_t); for (;;) { int step = sqlite3_step(stmt_get_nexts); bool done = false; switch (step) { case SQLITE_DONE: done = true; break; case SQLITE_ROW: { words_t words; unpack((const char*)sqlite3_column_text(stmt_get_nexts, 0), words); snippet_t snippet(new Snippet(words, sqlite3_column_int64(stmt_get_nexts, 1), sqlite3_column_int64(stmt_get_nexts, 2), sqlite3_column_int64(stmt_get_nexts, 3))); snippets->insert(snippet); break; } default: ok = false; ERROR("Error when parsing response to '%s': %d/%s", QUERY_GET_NEXTS, step, sqlite3_errmsg(db)); break; } if (!ok || done) { break; } } sqlite3_clear_bindings(stmt_get_nexts); sqlite3_reset(stmt_get_nexts); if (snippets->empty()) { if (search_words.size() >= 2) { words_t search_words_shortened(++search_words.begin(), search_words.end()); #ifdef READ_DEBUG_ENABLED DEBUG(" get_next -> %s", str(search_words_shortened).c_str()); #endif /* recurse with shorter search */ return get_next(state, selector, scorer, search_words_shortened, next); } else { #ifdef READ_DEBUG_ENABLED DEBUG(" next_snippet -> NOTFOUND"); #endif next = IBackend::LINE_END; } } else { const words_t& next_snippet = selector(*snippets, scorer, state)->words; #ifdef READ_DEBUG_ENABLED for (snippet_ptr_set_t::const_iterator siter = snippets->begin(); siter != snippets->end(); ++siter) { DEBUG(" nexts%s = snippet(%s, %lu)", str(search_words).c_str(), str((*siter)->words).c_str(), (*siter)->score(scorer, state)); } DEBUG(" next_snippet -> %s", str(next_snippet).c_str()); #endif next = next_snippet.back(); } return ok; }
int main(int, char**) { snippets(); return 0; }