Exemple #1
0
func addOptions(q rdb.Term, value string, index string, opts *t.BrowseOpt) rdb.Term {
	var limit uint = 1024 // TODO(gene): pass into adapter as a config param
	var lower, upper interface{}

	if opts != nil {
		if opts.Since > 0 {
			lower = opts.Since
		} else {
			lower = rdb.MinVal
		}

		if opts.Before > 0 {
			upper = opts.Before
		} else {
			upper = rdb.MaxVal
		}

		if value != "" {
			lower = []interface{}{value, lower}
			upper = []interface{}{value, upper}
		}

		if opts.Limit > 0 && opts.Limit < limit {
			limit = opts.Limit
		}
	} else {
		lower = []interface{}{value, rdb.MinVal}
		upper = []interface{}{value, rdb.MaxVal}
	}

	return q.Between(lower, upper, rdb.BetweenOpts{Index: index}).
		OrderBy(rdb.OrderByOpts{Index: rdb.Desc(index)}).Limit(limit)
}
Exemple #2
0
func (this *Instance) Exec(term r.Term) error {
	session, err := this.connect()
	if err != nil {
		return err
	}
	return term.Exec(session)
}
Exemple #3
0
func (this *Instance) RunWrite(term r.Term) (r.WriteResponse, error) {
	session, err := this.connect()
	if err != nil {
		return r.WriteResponse{}, err
	}
	return term.RunWrite(session)
}
Exemple #4
0
func (this *Instance) Run(term r.Term) (*r.Cursor, error) {
	session, err := this.connect()
	if err != nil {
		return nil, err
	}

	return term.Run(session)
}
Exemple #5
0
func asyncDatabaseQuery(client *Client, operation r.Term) {
	go func() {
		err := operation.Exec(client.session)
		if err != nil {
			client.send <- SocketMessage{"error", err.Error()}
		}
	}()
}
Exemple #6
0
func (this *Instance) All(term r.Term, result interface{}) error {
	cursor, err := term.Run(this.session)
	if err != nil {
		return err
	}

	return cursor.All(result)
}
Exemple #7
0
func (c *rethinkClient) addMessage(term gorethink.Term, message proto.Message) error {
	data, err := marshaller.MarshalToString(message)
	if err != nil {
		return err
	}
	_, err = term.Insert(gorethink.JSON(data)).RunWrite(c.session)
	return err
}
Exemple #8
0
func (d *dbService) Run(t r.Term) (*r.Cursor, error) {
	s, err := d.GetSession()
	if err != nil {
		return nil, err
	}
	defer s.Close()

	return t.Run(s)
}
func ListContains(s *r.Session, t r.Term, name string) (bool, error) {
	rows, err := t.Contains(name).Run(s)
	if err != nil {
		return false, err
	}
	var contains bool
	err = rows.One(&contains)
	if err != nil {
		return false, err
	}

	return contains, nil
}
Exemple #10
0
func (c *rethinkClient) getMessageByID(term gorethink.Term, id string, message proto.Message) error {
	cursor, err := term.Get(id).ToJSON().Run(c.session)
	if err != nil {
		return err
	}
	data := ""
	if !cursor.Next(&data) {
		return cursor.Err()
	}
	if err := jsonpb.UnmarshalString(data, message); err != nil {
		return err
	}
	return nil
}
Exemple #11
0
func (s *RequestStore) getMultiRequest(term r.Term, offset uint, count uint) ([]*download.Request, error) {
	var results []*download.Request

	rows, err := term.Slice(offset, (offset + count)).Run(s.Session)
	if err != nil {
		return nil, err
	}

	err = rows.All(&results)
	if err != nil {
		return nil, err
	}

	return results, nil
}
func (s *HookStore) getMultiHook(term r.Term) ([]*download.Hook, error) {
	var results []*download.Hook

	rows, err := term.Run(s.Session)
	if err != nil {
		return results, err
	}

	err = rows.All(&results)
	if err != nil {
		return nil, err
	}

	return results, nil
}
func (a *rethinkAPIServer) getMultiple(
	term gorethink.Term,
	messageConstructor func() proto.Message,
	modifiers ...func(gorethink.Term) gorethink.Term,
) ([]interface{}, error) {
	for _, modifier := range modifiers {
		term = modifier(term)
	}
	term = term.Map(func(row gorethink.Term) interface{} {
		return row.ToJSON()
	})
	cursor, err := term.Run(a.session)
	if err != nil {
		return nil, err
	}
	return processMultipleCursor(cursor, messageConstructor)
}
Exemple #14
0
func (s *RequestStore) getSingleRequest(term r.Term) (*download.Request, error) {
	row, err := term.Run(s.Session)

	if err != nil {
		return nil, err
	}

	if row.IsNil() {
		return nil, nil
	}

	var request download.Request
	err = row.One(&request)
	if err != nil {
		return nil, err
	}
	return &request, nil
}
func (s *DownloadStore) getSingleDownload(term r.Term) (*download.Download, error) {
	row, err := term.Run(s.Session)

	if err != nil {
		return nil, err
	}

	if row.IsNil() {
		return nil, nil
	}

	var download download.Download
	err = row.One(&download)
	if err != nil {
		return nil, err
	}
	return &download, nil
}
func rethinkToJSON(row gorethink.Term) interface{} {
	return row.ToJSON()
}
Exemple #17
0
func ResourceKeyIndex(row r.Term) interface{} {
	return []interface{}{row.Field("URL"), row.Field("Metadata").Field("ETag")}
}
func URLETagIndex(row r.Term) interface{} {
	return []interface{}{
		row.Field("URL"),
		row.Field("Metadata").Field("ETag")}
}
Exemple #19
0
func (t *ThreadsTable) List(
	owner string,
	sort []string,
	offset int,
	limit int,
	labels []string,
) ([]*models.Thread, error) {

	term := t.GetTable()

	if owner != "" {
		term = t.GetTable().GetAllByIndex("owner", owner)
	}

	// If sort array has contents, parse them and add to the term
	if sort != nil && len(sort) > 0 {
		var conds []interface{}
		for _, cond := range sort {
			if cond[0] == '-' {
				conds = append(conds, gorethink.Desc(cond[1:]))
			} else if cond[0] == '+' || cond[0] == ' ' {
				conds = append(conds, gorethink.Asc(cond[1:]))
			} else {
				conds = append(conds, gorethink.Asc(cond))
			}
		}

		term = term.OrderBy(conds...)
	}

	// Parse labels
	hasLabels := []string{}
	excLabels := []string{}
	for _, label := range labels {
		if label[0] == '-' {
			excLabels = append(excLabels, label[1:])
		} else {
			hasLabels = append(hasLabels, label)
		}
	}

	// Transform that into a term
	if len(hasLabels) > 0 || len(excLabels) > 0 {
		var hasTerm gorethink.Term
		if len(hasLabels) == 1 {
			hasTerm = gorethink.Row.Field("labels").Contains(hasLabels[0])
		} else if len(hasLabels) > 0 {
			for i, label := range hasLabels {
				if i == 0 {
					hasTerm = gorethink.Row.Field("labels").Contains(label)
				} else {
					hasTerm = hasTerm.And(gorethink.Row.Field("labels").Contains(label))
				}
			}
		}

		var excTerm gorethink.Term
		if len(excLabels) == 1 {
			excTerm = gorethink.Not(gorethink.Row.Field("labels").Contains(excLabels[0]))
		} else {
			for i, label := range excLabels {
				if i == 0 {
					excTerm = gorethink.Not(gorethink.Row.Field("labels").Contains(label))
				} else {
					excTerm = excTerm.And(gorethink.Not(gorethink.Row.Field("labels").Contains(label)))
				}
			}
		}

		// Append them into the term
		if len(hasLabels) > 0 && len(excLabels) > 0 {
			term = term.Filter(hasTerm.And(excTerm))
		} else if len(hasLabels) > 0 && len(excLabels) == 0 {
			term = term.Filter(hasTerm)
		} else if len(hasLabels) == 0 && len(excLabels) > 0 {
			term = term.Filter(excTerm)
		}
	}

	// Slice the result
	if offset != 0 || limit != 0 {
		term = term.Slice(offset, offset+limit)
	}

	// Add manifests
	term = term.Map(func(thread gorethink.Term) gorethink.Term {
		return thread.Merge(gorethink.Db(t.GetDBName()).Table("emails").Between([]interface{}{
			thread.Field("id"),
			time.Date(1990, time.January, 1, 23, 0, 0, 0, time.UTC),
		}, []interface{}{
			thread.Field("id"),
			time.Date(2090, time.January, 1, 23, 0, 0, 0, time.UTC),
		}, gorethink.BetweenOpts{
			Index: "threadAndDate",
		}).OrderBy(gorethink.OrderByOpts{Index: "threadAndDate"}).
			Nth(0).Pluck("manifest"))
	})

	// Run the query
	cursor, err := term.Run(t.GetSession())
	if err != nil {
		return nil, err
	}
	defer cursor.Close()

	// Fetch the cursor
	var resp []*models.Thread
	err = cursor.All(&resp)
	if err != nil {
		return nil, err
	}

	return resp, nil
}
func HookKeyIndex(row r.Term) interface{} {
	return []interface{}{row.Field("DownloadID"), row.Field("RequestID")}
}