Example #1
0
func (a *rethinkAPIServer) waitMessageByPrimaryKey(
	table Table,
	key interface{},
	message proto.Message,
	predicate func(term gorethink.Term) gorethink.Term,
) (retErr error) {
	cursor, err :=
		a.getTerm(table).
			Get(key).
			Default(gorethink.Error("value not found")).
			Changes().
			Field("new_val").
			Filter(predicate).
			Run(a.session)
	if err != nil {
		return err
	}
	defer func() {
		if err := cursor.Close(); err != nil && retErr == nil {
			retErr = err
		}
	}()
	cursor.Next(message)
	return cursor.Err()
}
func (a *rethinkAPIServer) waitMessageByPrimaryKey(
	table Table,
	key interface{},
	message proto.Message,
	predicate func(term gorethink.Term) gorethink.Term,
) (retErr error) {
	term := a.getTerm(table).
		Get(key).
		Default(gorethink.Error("value not found")).
		Changes(gorethink.ChangesOpts{
			IncludeInitial: true,
		}).
		Field("new_val").
		Filter(predicate)
	cursor, err := term.Run(a.session)
	if err != nil {
		if strings.Contains(err.Error(), "value not found") {
			err = fmt.Errorf("%v %v not found", table, key)
		}
		return err
	}
	defer func() {
		if err := cursor.Close(); err != nil && retErr == nil {
			retErr = err
		}
	}()
	cursor.Next(message)
	return cursor.Err()
}
Example #3
0
func (rd *rethinkDriver) PutBuild(build *datastore.Build) (string, error) {
	result, err := r.Table("builds").Insert(
		r.Table("builds").Filter(
			r.And(
				r.Row.Field("job_id").Eq(build.JobId),
				r.Row.Field("number").Eq(build.Number),
			),
		).CoerceTo("array").Do(func(docs r.Term) interface{} {
			return r.Branch(
				r.Or(docs.IsEmpty(), docs.Field("id").Contains(build.Id)),
				build,
				r.Error("Build with job_id and number exists"),
			)
		}),
		r.InsertOpts{Conflict: "replace"},
	).RunWrite(rd.session)
	if err != nil {
		return "", err
	}

	if len(result.GeneratedKeys) > 0 {
		return result.GeneratedKeys[0], nil
	} else {
		return build.Id, nil
	}
}
Example #4
0
func (a *rethinkAPIServer) getMessageByPrimaryKey(table Table, key interface{}, message proto.Message) error {
	cursor, err := a.getTerm(table).Get(key).Default(gorethink.Error("value not found")).Run(a.session)
	if err != nil {
		return err
	}
	if cursor.Next(message) {
		return cursor.Err()
	}
	return nil
}
Example #5
0
func (a *rethinkAPIServer) getMessageByPrimaryKey(table Table, value interface{}, message proto.Message) error {
	cursor, err := a.getTerm(table).Get(value).Default(gorethink.Error("value not found")).ToJSON().Run(a.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
}
Example #6
0
func (rd rethinkDriver) PutJob(job *datastore.Job) (string, error) {
	result, err := r.Table("jobs").Insert(
		r.Table("jobs").Filter(r.Row.Field("key").Eq(job.Key)).CoerceTo("array").Do(func(docs r.Term) interface{} {
			return r.Branch(
				r.Or(docs.IsEmpty(), docs.Field("id").Contains(job.Id)),
				job,
				r.Error("Job with key exists"),
			)
		}),
		r.InsertOpts{Conflict: "replace"},
	).RunWrite(rd.session)
	if err != nil {
		return "", err
	}

	if len(result.GeneratedKeys) > 0 {
		return result.GeneratedKeys[0], nil
	} else {
		return job.Id, nil
	}
}