Exemple #1
0
// Load loads all events for the aggregate id from the database.
// Returns ErrNoEventsFound if no events can be found.
func (s *EventStore) Load(id eh.UUID) ([]eh.Event, error) {
	params := &dynamodb.QueryInput{
		TableName:              aws.String(s.config.Table),
		KeyConditionExpression: aws.String("AggregateID = :id"),
		ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
			":id": {S: aws.String(id.String())},
		},
		ConsistentRead: aws.Bool(true),
	}
	resp, err := s.service.Query(params)
	if err != nil {
		return nil, err
	}

	if len(resp.Items) == 0 {
		return []eh.Event{}, nil
	}

	eventRecords := make([]*eventRecord, len(resp.Items))
	for i, item := range resp.Items {
		record := &eventRecord{}
		if err := dynamodbattribute.UnmarshalMap(item, record); err != nil {
			return nil, err
		}
		eventRecords[i] = record
	}

	events := make([]eh.Event, len(eventRecords))
	for i, record := range eventRecords {
		// Create an event of the correct type.
		event, err := eh.CreateEvent(record.EventType)
		if err != nil {
			return nil, err
		}

		if err := dynamodbattribute.UnmarshalMap(record.Payload, event); err != nil {
			// 	return nil, ErrCouldNotUnmarshalEvent
			return nil, err
		}
		events[i] = event
	}

	return events, nil
}
Exemple #2
0
func (b *EventBus) recv(ready chan struct{}) {
	for {
		switch n := b.conn.Receive().(type) {
		case redis.PMessage:
			// Extract the event type from the channel name.
			eventType := eh.EventType(strings.TrimPrefix(n.Channel, b.prefix))

			// Create an event of the correct type.
			event, err := eh.CreateEvent(eventType)
			if err != nil {
				log.Printf("error: event bus receive: %v\n", err)
				continue
			}

			// Manually decode the raw BSON event.
			data := bson.Raw{3, n.Data}
			if err := data.Unmarshal(event); err != nil {
				log.Printf("error: event bus receive: %v\n", ErrCouldNotUnmarshalEvent)
				continue
			}

			for o := range b.observers {
				o.Notify(event)
			}
		case redis.Subscription:
			switch n.Kind {
			case "psubscribe":
				close(ready)
			case "punsubscribe":
				if n.Count == 0 {
					close(b.exit)
					return
				}
			}
		case error:
			log.Printf("error: event bus receive: %v\n", n)
			close(b.exit)
			return
		}
	}
}