ndb: add initial search interface

Still needs updating because of the tuple array
This commit is contained in:
William Casarin
2023-12-02 15:05:15 -08:00
parent 6b8cf51720
commit 9502fc30ba
3 changed files with 65 additions and 0 deletions

View File

@@ -10,6 +10,11 @@ import OSLog
fileprivate let APPLICATION_GROUP_IDENTIFIER = "group.com.damus"
enum NdbSearchOrder {
case oldest_first
case newest_first
}
class Ndb {
let ndb: ndb_t
let owns_db_file: Bool // Determines whether this class should be allowed to create or move the db file.
@@ -133,6 +138,45 @@ class Ndb {
return NdbNote(note: note_p, owned_size: nil, key: key)
}
func text_search(query: String, limit: Int = 32, order: NdbSearchOrder = .newest_first) -> [NoteKey] {
let txn = NdbTxn(ndb: self)
var results = ndb_text_search_results()
let res = query.withCString { q in
let order = order == .newest_first ? NDB_ORDER_DESCENDING : NDB_ORDER_ASCENDING
var config = ndb_text_search_config(order: order, limit: Int32(limit))
return ndb_text_search(&txn.txn, q, &results, &config)
}
if res == 0 {
return []
}
var note_ids = [NoteKey]()
for i in 0..<results.num_results {
// seriously wtf
switch i {
case 0: note_ids.append(results.results.0.key.note_id)
case 1: note_ids.append(results.results.1.key.note_id)
case 2: note_ids.append(results.results.2.key.note_id)
case 3: note_ids.append(results.results.3.key.note_id)
case 4: note_ids.append(results.results.4.key.note_id)
case 5: note_ids.append(results.results.5.key.note_id)
case 6: note_ids.append(results.results.6.key.note_id)
case 7: note_ids.append(results.results.7.key.note_id)
case 8: note_ids.append(results.results.8.key.note_id)
case 9: note_ids.append(results.results.9.key.note_id)
case 10: note_ids.append(results.results.10.key.note_id)
case 11: note_ids.append(results.results.11.key.note_id)
case 12: note_ids.append(results.results.12.key.note_id)
case 13: note_ids.append(results.results.13.key.note_id)
default:
break
}
}
return note_ids
}
func lookup_note_by_key(_ key: NoteKey) -> NdbTxn<NdbNote?> {
return NdbTxn(ndb: self) { txn in
lookup_note_by_key_with_txn(key, txn: txn)

View File

@@ -81,6 +81,23 @@ final class NdbTests: XCTestCase {
}
func test_ndb_seach() throws {
do {
let ndb = try! Ndb(path: db_dir)!
let ok = ndb.process_events(test_wire_events)
XCTAssertTrue(ok)
}
do {
let ndb = try! Ndb(path: db_dir)!
let note_ids = ndb.text_search(query: "barked")
XCTAssertEqual(note_ids.count, 1)
let expected_note_id = NoteId(hex: "b17a540710fe8495b16bfbaf31c6962c4ba8387f3284a7973ad523988095417e")!
let note_id = ndb.lookup_note_by_key(note_ids[0]).map({ n in n?.id }).value
XCTAssertEqual(note_id, .some(expected_note_id))
}
}
func test_ndb_note() throws {
let note = NdbNote.owned_from_json(json: test_contact_list_json)
XCTAssertNotNil(note)