Пример #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(ccursor *gorethink.Cursor) error {
	if r.debug {
		fmt.Printf("sending changes\n")
	}

	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))
		} else if change.NewVal != nil {
			msg = message.NewMsg(message.Insert, r.prepareDocument(change.NewVal))
		} else if change.OldVal != nil {
			msg = message.NewMsg(message.Delete, r.prepareDocument(change.OldVal))
		}

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

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

	return nil
}