Пример #1
0
// Start the adaptor as a source
func (r *Rethinkdb) Start() error {
	if r.debug {
		fmt.Printf("getting a changes cursor\n")
	}

	// Grab a changes cursor before sending all rows. The server will buffer
	// changes while we reindex the entire table.
	var ccursor *gorethink.Cursor
	ccursor, err := gorethink.Table(r.table).Changes().Run(r.client)
	if err != nil {
		r.pipe.Err <- err
		return err
	}
	defer ccursor.Close()

	if err := r.sendAllDocuments(); err != nil {
		r.pipe.Err <- err
		return err
	}

	if r.tail {
		if err := r.sendChanges(ccursor); err != nil {
			r.pipe.Err <- err
			return err
		}
	}

	return nil
}
Пример #2
0
func (r *Rethinkdb) sendChanges(table string, ccursor *gorethink.Cursor) error {
	defer ccursor.Close()
	if r.debug {
		fmt.Printf("sending changes for %s\n", table)
	}
	var change rethinkDbChangeNotification
	for ccursor.Next(&change) {
		if stop := r.pipe.Stopped; stop {
			return nil
		}

		if r.debug {
			fmt.Printf("change: %#v\n", change)
		}

		var msg *message.Msg
		if change.Error != "" {
			return errors.New(change.Error)
		} else if change.OldVal != nil && change.NewVal != nil {
			msg = message.NewMsg(message.Update, r.prepareDocument(change.NewVal), r.computeNamespace(table))
		} else if change.NewVal != nil {
			msg = message.NewMsg(message.Insert, r.prepareDocument(change.NewVal), r.computeNamespace(table))
		} else if change.OldVal != nil {
			msg = message.NewMsg(message.Delete, r.prepareDocument(change.OldVal), r.computeNamespace(table))
		}

		if msg != nil {
			r.pipe.Send(msg)
			if r.debug {
				fmt.Printf("msg: %#v\n", msg)
			}
		}
	}

	if err := ccursor.Err(); err != nil {
		return err
	}

	return nil
}