Skip to content

Commit

Permalink
regenerated protobufs for changed search parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
or-else committed May 10, 2018
1 parent 665f467 commit 7d4feac
Show file tree
Hide file tree
Showing 9 changed files with 1,402 additions and 545 deletions.
1,820 changes: 1,338 additions & 482 deletions pbx/model.pb.go

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions pbx/model.proto
Original file line number Diff line number Diff line change
Expand Up @@ -433,13 +433,13 @@ message ClientReq {

message SearchQuery {
string user_id = 1;
repeated string terms = 2;
string query = 2;
}

message SearchFound {
RespCode status = 1;
// New search terms If status == REPLACE, otherwise unset.
repeated string terms = 2;
// New search query If status == REPLACE, otherwise unset.
string query = 2;
// Search results.
repeated TopicSub result = 3;
}
Expand Down
14 changes: 7 additions & 7 deletions pbx/model_pb2.py

Large diffs are not rendered by default.

56 changes: 30 additions & 26 deletions server/db/mysql/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1169,32 +1169,29 @@ func (a *adapter) SubsDelForUser(user t.Uid) error {

// Returns a list of users who match given tags, such as "email:jdoe@example.com" or "tel:18003287448".
// Searching the 'users.Tags' for the given tags using respective index.
func (a *adapter) FindUsers(uid t.Uid, tags []string) ([]t.Subscription, error) {
func (a *adapter) FindUsers(uid t.Uid, req, opt []string) ([]t.Subscription, error) {
index := make(map[string]struct{})
var args []interface{}
for _, tag := range tags {
for _, tag := range append(req, opt...) {
args = append(args, tag)
index[tag] = struct{}{}
}

// Query for selecting matches where every group includes at least one required match (restricting search to
// group members).
/*
SELECT u.id, u.createdat, u.tags, COUNT(*) AS matches
FROM users as u LEFT JOIN usertags AS t ON t.userid=u.id
WHERE t.tag IN (<all tags here>)
GROUP BY u.id, u.createdat, u.tags
HAVING COUNT(t.tag IN (<required tags here>) OR NULL)>0
ORDER BY matches DESC LIMIT 20;
*/
query := "SELECT u.id,u.createdat,u.updatedat,u.public,u.tags,COUNT(*) AS matches " +
"FROM users AS u LEFT JOIN usertags AS t ON t.userid=u.id " +
"WHERE t.tag IN (?" + strings.Repeat(",?", len(req)+len(opt)-1) + ") " +
"GROUP BY u.id,u.createdat,u.updatedat,u.public,u.tags "
if len(req) > 0 {
query += "HAVING COUNT(t.tag IN (?" + strings.Repeat(",?", len(req)-1) + ") OR NULL)>=? "
for _, tag := range req {
args = append(args, tag)
}
args = append(args, len(req))
}
query += "ORDER BY matches DESC LIMIT ?"

// Get users matched by tags, sort by number of matches from high to low.
rows, err := a.db.Queryx(
"SELECT u.id,u.createdat,u.updatedat,u.public,u.tags,COUNT(*) AS matches "+
"FROM users AS u LEFT JOIN usertags AS t ON t.userid=u.id "+
"WHERE t.tag IN (?"+strings.Repeat(",?", len(tags)-1)+") "+
"GROUP BY u.id,u.createdat,u.updatedat,u.public,u.tags ORDER BY matches DESC LIMIT ?",
append(args, maxResults)...)
rows, err := a.db.Queryx(query, append(args, maxResults)...)

if err != nil {
return nil, err
Expand Down Expand Up @@ -1238,20 +1235,27 @@ func (a *adapter) FindUsers(uid t.Uid, tags []string) ([]t.Subscription, error)

// Returns a list of topics with matching tags.
// Searching the 'topics.Tags' for the given tags using respective index.
func (a *adapter) FindTopics(tags []string) ([]t.Subscription, error) {
func (a *adapter) FindTopics(req, opt []string) ([]t.Subscription, error) {
index := make(map[string]struct{})
var args []interface{}
for _, tag := range tags {
for _, tag := range append(req, opt...) {
args = append(args, tag)
index[tag] = struct{}{}
}

rows, err := a.db.Queryx(
"SELECT t.name AS topic,t.createdat,t.updatedat,t.public,t.tags,COUNT(*) AS matches "+
"FROM topics AS t LEFT JOIN topictags AS tt ON t.name=tt.topic "+
"WHERE tt.tag IN (?"+strings.Repeat(",?", len(tags)-1)+") "+
"GROUP BY t.name,t.createdat,t.updatedat,t.public,t.tags "+
"ORDER BY matches DESC LIMIT ?", append(args, maxResults)...)
query := "SELECT t.name AS topic,t.createdat,t.updatedat,t.public,t.tags,COUNT(*) AS matches " +
"FROM topics AS t LEFT JOIN topictags AS tt ON t.name=tt.topic " +
"WHERE tt.tag IN (?" + strings.Repeat(",?", len(req)+len(opt)-1) + ") " +
"GROUP BY t.name,t.createdat,t.updatedat,t.public,t.tags "
if len(req) > 0 {
query += "HAVING COUNT(tt.tag IN (?" + strings.Repeat(",?", len(req)-1) + ") OR NULL)>=? "
for _, tag := range append(req) {
args = append(args, tag)
}
args = append(args, len(req))
}
query += "ORDER BY matches DESC LIMIT ?"
rows, err := a.db.Queryx(query, append(args, maxResults)...)

if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions server/db/rethinkdb/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ func (a *adapter) SubsDelForUser(user t.Uid) error {

// Returns a list of users who match given tags, such as "email:jdoe@example.com" or "tel:18003287448".
// Searching the 'users.Tags' for the given tags using respective index.
func (a *adapter) FindUsers(uid t.Uid, tags []string) ([]t.Subscription, error) {
func (a *adapter) FindUsers(uid t.Uid, req, opt []string) ([]t.Subscription, error) {
index := make(map[string]struct{})
var query []interface{}
for _, tag := range tags {
Expand Down Expand Up @@ -980,7 +980,7 @@ func (a *adapter) FindUsers(uid t.Uid, tags []string) ([]t.Subscription, error)

// Returns a list of topics with matching tags.
// Searching the 'topics.Tags' for the given tags using respective index.
func (a *adapter) FindTopics(tags []string) ([]t.Subscription, error) {
func (a *adapter) FindTopics(req, opt []string) ([]t.Subscription, error) {
index := make(map[string]struct{})
var query []interface{}
for _, tag := range tags {
Expand Down
12 changes: 6 additions & 6 deletions server/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,14 +399,14 @@ func pluginFireHose(sess *Session, msg *ClientComMessage) (*ClientComMessage, *S
}

// Ask plugin to perform search.
func pluginFind(user types.Uid, query []string) ([]string, []types.Subscription, error) {
func pluginFind(user types.Uid, query string) (string, []types.Subscription, error) {
if globals.plugins == nil {
return query, nil, nil
}

find := &pbx.SearchQuery{
UserId: user.UserId(),
Terms: query,
Query: query,
}
for _, p := range globals.plugins {
if !p.filterFind {
Expand All @@ -430,17 +430,17 @@ func pluginFind(user types.Uid, query []string) ([]string, []types.Subscription,
}
// DROP means stop processing the request
if respStatus == pbx.RespCode_DROP {
return nil, nil, nil
return "", nil, nil
}
// REPLACE: query string was changed. Use the new one for further processing.
if respStatus == pbx.RespCode_REPLACE {
return resp.GetTerms(), nil, nil
return resp.GetQuery(), nil, nil
}
// RESPOND: Plugin provided a specific response. Use it
return nil, pbSubSliceDeserialize(resp.GetResult()), nil
return "", pbSubSliceDeserialize(resp.GetResult()), nil
} else {
log.Println("plugins: Find call failed", p.name, err)
return nil, nil, err
return "", nil, err
}
}

Expand Down
4 changes: 2 additions & 2 deletions server/store/adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ type Adapter interface {
SubsDelForUser(user t.Uid) error

// FindUsers searches for new contacts given a list of tags
FindUsers(user t.Uid, tags []string) ([]t.Subscription, error)
FindUsers(user t.Uid, req, opt []string) ([]t.Subscription, error)
// FindTopics searches for group topics given a list of tags
FindTopics(tags []string) ([]t.Subscription, error)
FindTopics(req, opt []string) ([]t.Subscription, error)

// Messages
MessageSave(msg *t.Message) error
Expand Down
6 changes: 3 additions & 3 deletions server/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,12 @@ func (UsersObjMapper) GetSubs(id types.Uid) ([]types.Subscription, error) {
}

// FindSubs loads a list of users for the given tags.
func (UsersObjMapper) FindSubs(id types.Uid, query []string) ([]types.Subscription, error) {
usubs, err := adp.FindUsers(id, query)
func (UsersObjMapper) FindSubs(id types.Uid, required, optional []string) ([]types.Subscription, error) {
usubs, err := adp.FindUsers(id, required, optional)
if err != nil {
return nil, err
}
tsubs, err := adp.FindTopics(query)
tsubs, err := adp.FindTopics(required, optional)
if err != nil {
return nil, err
}
Expand Down
25 changes: 11 additions & 14 deletions server/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -1439,25 +1439,22 @@ func (t *Topic) replyGetSub(sess *Session, id string, opts *MsgGetOpts) error {
subs, err = store.Users.GetTopicsAny(sess.uid)
isSharer = true
} else if t.cat == types.TopicCatFnd {
// TODO: refactor .private from a slice of interfaces to a string.
// TODO: check the query (.private) against the set of allowed tags.
// Given a query provided in .private, fetch user's contacts. Private contains a slice of interfaces.
if ifquery, ok := t.perUser[sess.uid].private.([]interface{}); ok && len(ifquery) > 0 {
// Convert slice of interfaces to a slice of strings.
var query []string
for _, ifq := range ifquery {
switch str := ifq.(type) {
case string:
query = append(query, str)
default:
}
}
// Given a query provided in .private, fetch user's contacts. Private contains a string.
if query, ok := t.perUser[sess.uid].private.(string); ok {
if len(query) > 0 {
query, subs, err = pluginFind(sess.uid, query)
if err == nil && subs == nil && query != nil {
subs, err = store.Users.FindSubs(sess.uid, query)
if err == nil && subs == nil && query != "" {
var req, opt []string
req, opt, err = parseSearchQuery(query)
if err == nil {
subs, err = store.Users.FindSubs(sess.uid, req, opt)
}
}
}
} else {
sess.queueOut(ErrMalformed(id, t.original(sess.uid), now))
return types.ErrMalformed
}
} else {
// FIXME(gene): don't load subs from DB, use perUserData - it already contains subscriptions.
Expand Down

0 comments on commit 7d4feac

Please sign in to comment.