示例#1
0
// Index stores the blob into the specified storage under the provided id for
// a given repository.
func (b *simpleBlobStore) Store(storage Storage, repo *Repository, blob *blob.Blob) error {
	switch storage {
	// Live is an index containing the webhook events. In this particular case,
	// we use the delivery id as the document index.
	//
	// When storing a live event, we always update the next two indices.
	case StoreLiveEvent:
		liveIndex := repo.LiveIndexForTimestamp(blob.Timestamp)
		log.Debugf("store live event to %s/%s/%s", liveIndex, blob.Type, blob.ID)
		if err := b.indexer.Index(liveIndex, blob); err != nil {
			return fmt.Errorf("store live event %s data: %v", blob.ID, err)
		}
		// Before falling through, replace the blob with the snapshot data from
		// the event, if any.
		if blob = blob.Snapshot(); blob == nil {
			return nil
		}
		fallthrough
	// Current state is an index containing the last version of items at a
	// given moment in time, and is updated at a frequency configured by the
	// user.
	//
	// When storing a current state, we always update the next index.
	case StoreCurrentState:
		stateIndex := repo.StateIndexForTimestamp(blob.Timestamp)
		log.Debugf("store current state to %s/%s/%s", stateIndex, blob.Type, blob.ID)
		if err := b.indexer.Index(stateIndex, blob); err != nil {
			return fmt.Errorf("store current state %s data: %v", blob.ID, err)
		}
		fallthrough
	// Snapshot is an index containing the last version of all items, opened or
	// closed.
	case StoreSnapshot:
		log.Debugf("store snapshot to %s/%s/%s", repo.SnapshotIndex(), blob.Type, blob.ID)
		if err := b.indexer.Index(repo.SnapshotIndex(), blob); err != nil {
			return fmt.Errorf("store snapshot %s data: %v", blob.ID, err)
		}
	}
	return nil
}