Skip to content

Commit

Permalink
search: switch to nostrdb profile searching
Browse files Browse the repository at this point in the history
Changelog-Changed: Switch to nostrdb for @'s and user search
  • Loading branch information
jb55 committed Sep 21, 2023
1 parent fafe3b4 commit 7a85ae2
Show file tree
Hide file tree
Showing 11 changed files with 325 additions and 33 deletions.
4 changes: 1 addition & 3 deletions damus/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ struct ContentView: View {
likes: EventCounter(our_pubkey: pubkey),
boosts: EventCounter(our_pubkey: pubkey),
contacts: Contacts(our_pubkey: pubkey),
profiles: Profiles(user_search_cache: user_search_cache, ndb: ndb),
profiles: Profiles(ndb: ndb),
dms: home.dms,
previews: PreviewCache(),
zaps: Zaps(our_pubkey: pubkey),
Expand All @@ -646,7 +646,6 @@ struct ContentView: View {
muted_threads: MutedThreadsManager(keypair: keypair),
wallet: WalletModel(settings: settings),
nav: self.navigationCoordinator,
user_search_cache: user_search_cache,
music: MusicController(onChange: music_changed),
video: VideoController(),
ndb: ndb
Expand Down Expand Up @@ -919,7 +918,6 @@ func handle_unfollow(state: DamusState, unfollow: FollowRef) -> Bool {
switch unfollow {
case .pubkey(let pk):
state.contacts.remove_friend(pk)
state.user_search_cache.updateOwnContactsPetnames(id: state.pubkey, oldEvent: old_contacts, newEvent: ev)
case .hashtag:
// nothing to handle here really
break
Expand Down
5 changes: 1 addition & 4 deletions damus/Models/DamusState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ struct DamusState {
let muted_threads: MutedThreadsManager
let wallet: WalletModel
let nav: NavigationCoordinator
let user_search_cache: UserSearchCache
let music: MusicController?
let video: VideoController
let ndb: Ndb
Expand Down Expand Up @@ -62,7 +61,6 @@ struct DamusState {
}

static var empty: DamusState {
let user_search_cache = UserSearchCache()
let empty_pub: Pubkey = .empty
let empty_sec: Privkey = .empty
let kp = Keypair(pubkey: empty_pub, privkey: nil)
Expand All @@ -73,7 +71,7 @@ struct DamusState {
likes: EventCounter(our_pubkey: empty_pub),
boosts: EventCounter(our_pubkey: empty_pub),
contacts: Contacts(our_pubkey: empty_pub),
profiles: Profiles(user_search_cache: user_search_cache, ndb: .empty),
profiles: Profiles(ndb: .empty),
dms: DirectMessagesModel(our_pubkey: empty_pub),
previews: PreviewCache(),
zaps: Zaps(our_pubkey: empty_pub),
Expand All @@ -90,7 +88,6 @@ struct DamusState {
muted_threads: MutedThreadsManager(keypair: kp),
wallet: WalletModel(settings: UserSettingsStore()),
nav: NavigationCoordinator(),
user_search_cache: user_search_cache,
music: nil,
video: VideoController(),
ndb: .empty
Expand Down
2 changes: 0 additions & 2 deletions damus/Models/HomeModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -734,8 +734,6 @@ func load_our_contacts(state: DamusState, m_old_ev: NostrEvent?, ev: NostrEvent)
}
}
}

state.user_search_cache.updateOwnContactsPetnames(id: contacts.our_pubkey, oldEvent: m_old_ev, newEvent: ev)
}


Expand Down
9 changes: 5 additions & 4 deletions damus/Nostr/Profiles.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ class Profiles {
@MainActor
var nip05_pubkey: [String: Pubkey] = [:]

let user_search_cache: UserSearchCache

init(user_search_cache: UserSearchCache, ndb: Ndb) {
self.user_search_cache = user_search_cache
init(ndb: Ndb) {
self.ndb = ndb
}

Expand Down Expand Up @@ -84,6 +81,10 @@ class Profiles {
return ndb.lookup_profile_by_key(key: key)
}

func search<Y>(_ query: String, limit: Int, txn: NdbTxn<Y>) -> [Pubkey] {
return ndb.search_profile(query, limit: limit, txn: txn)
}

func lookup(id: Pubkey) -> NdbTxn<Profile?> {
return ndb.lookup_profile(id).map({ pr in pr?.profile })
}
Expand Down
4 changes: 1 addition & 3 deletions damus/TestData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,14 @@ var test_damus_state: DamusState = ({
print("opening \(tempDir!)")
let ndb = Ndb(path: tempDir)!
let our_pubkey = test_pubkey
let user_search_cache = UserSearchCache()
let pool = RelayPool(ndb: ndb)
let settings = UserSettingsStore()
let damus = DamusState(pool: pool,
keypair: test_keypair,
likes: .init(our_pubkey: our_pubkey),
boosts: .init(our_pubkey: our_pubkey),
contacts: .init(our_pubkey: our_pubkey),
profiles: .init(user_search_cache: user_search_cache, ndb: ndb),
profiles: .init(ndb: ndb),
dms: .init(our_pubkey: our_pubkey),
previews: .init(),
zaps: .init(our_pubkey: our_pubkey),
Expand All @@ -91,7 +90,6 @@ var test_damus_state: DamusState = ({
muted_threads: .init(keypair: test_keypair),
wallet: .init(settings: settings),
nav: .init(),
user_search_cache: user_search_cache,
music: .init(onChange: {_ in }),
video: .init(),
ndb: ndb)
Expand Down
3 changes: 2 additions & 1 deletion damus/Views/Posting/UserSearch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ struct UserSearch: View {
@EnvironmentObject var tagModel: TagModel

var users: [Pubkey] {
return search_profiles(profiles: damus_state.profiles, search: search)
let txn = NdbTxn(ndb: damus_state.ndb)
return search_profiles(profiles: damus_state.profiles, search: search, txn: txn)
}

func on_user_tapped(pk: Pubkey) {
Expand Down
3 changes: 1 addition & 2 deletions damus/Views/Profile/ProfilePicView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ func get_profile_url(picture: String?, pubkey: Pubkey, profiles: Profiles) -> UR
}

func make_preview_profiles(_ pubkey: Pubkey) -> Profiles {
let user_search_cache = UserSearchCache()
let profiles = Profiles(user_search_cache: user_search_cache, ndb: test_damus_state.ndb)
let profiles = Profiles(ndb: test_damus_state.ndb)
let picture = "http://cdn.jb55.com/img/red-me.jpg"
let profile = Profile(name: "jb55", display_name: "William Casarin", about: "It's me", picture: picture, banner: "", website: "https://jb55.com", lud06: nil, lud16: nil, nip05: "jb55.com", damus_donation: nil)
//let ts_profile = TimestampedProfile(profile: profile, timestamp: 0, event: test_note)
Expand Down
16 changes: 10 additions & 6 deletions damus/Views/SearchResultsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ struct SearchResultsView: View {
}
.frame(maxHeight: .infinity)
.onAppear {
self.result = search_for_string(profiles: damus_state.profiles, search)
let txn = NdbTxn.init(ndb: damus_state.ndb)
self.result = search_for_string(profiles: damus_state.profiles, search: search, txn: txn)
}
.onChange(of: search) { new in
self.result = search_for_string(profiles: damus_state.profiles, new)
let txn = NdbTxn.init(ndb: damus_state.ndb)
self.result = search_for_string(profiles: damus_state.profiles, search: search, txn: txn)
}
}
}
Expand All @@ -125,7 +127,7 @@ struct SearchResultsView_Previews: PreviewProvider {
*/


func search_for_string(profiles: Profiles, _ new: String) -> Search? {
func search_for_string<Y>(profiles: Profiles, search new: String, txn: NdbTxn<Y>) -> Search? {
guard new.count != 0 else {
return nil
}
Expand Down Expand Up @@ -154,7 +156,7 @@ func search_for_string(profiles: Profiles, _ new: String) -> Search? {
return .note(NoteId(decoded.data))
}

let multisearch = MultiSearch(hashtag: make_hashtagable(new), profiles: search_profiles(profiles: profiles, search: new))
let multisearch = MultiSearch(hashtag: make_hashtagable(new), profiles: search_profiles(profiles: profiles, search: new, txn: txn))
return .multi(multisearch)
}

Expand All @@ -171,7 +173,7 @@ func make_hashtagable(_ str: String) -> String {
return String(new.filter{$0 != " "})
}

func search_profiles(profiles: Profiles, search: String) -> [Pubkey] {
func search_profiles<Y>(profiles: Profiles, search: String, txn: NdbTxn<Y>) -> [Pubkey] {
// Search by hex pubkey.
if let pubkey = hex_decode_pubkey(search),
profiles.lookup_key_by_pubkey(pubkey) != nil
Expand All @@ -189,5 +191,7 @@ func search_profiles(profiles: Profiles, search: String) -> [Pubkey] {
}

let new = search.lowercased()
return profiles.user_search_cache.search(key: new)

return profiles.search(search, limit: 10, txn: txn)
}

26 changes: 26 additions & 0 deletions nostrdb/Ndb.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,32 @@ class Ndb {
}
}

func search_profile<Y>(_ search: String, limit: Int, txn: NdbTxn<Y>) -> [Pubkey] {
var pks = Array<Pubkey>()

return search.withCString { q in
var s = ndb_search()
guard ndb_search_profile(&txn.txn, &s, q) != 0 else {
return pks
}

defer { ndb_search_profile_end(&s) }
pks.append(Pubkey(Data(bytes: &s.key.pointee.id.0, count: 32)))

var n = limit
while n > 0 {
guard ndb_search_profile_next(&s) != 0 else {
return pks
}
pks.append(Pubkey(Data(bytes: &s.key.pointee.id.0, count: 32)))

n -= 1
}

return pks
}
}

deinit {
ndb_destroy(ndb.ndb)
}
Expand Down
Loading

0 comments on commit 7a85ae2

Please sign in to comment.