コード例 #1
0
ファイル: join_test.go プロジェクト: sburnett/transformer
func ExampleJoin_inner() {
	left := store.SliceStore{}
	left.BeginWriting()
	left.WriteRecord(store.NewRecord("1", "news", 0))
	left.WriteRecord(store.NewRecord("2", "book", 0))
	left.EndWriting()

	right := store.SliceStore{}
	right.BeginWriting()
	right.WriteRecord(store.NewRecord("1", "paper", 0))
	right.WriteRecord(store.NewRecord("3", "brush", 0))
	right.EndWriting()

	output := store.SliceStore{}

	joiner := Join(nil, nil)
	transformer.RunTransformer(transformer.MakeGroupDoFunc(joiner), store.NewDemuxingReader(&left, &right), &output)

	output.BeginReading()
	for {
		record, err := output.ReadRecord()
		if err != nil {
			panic(err)
		}
		if record == nil {
			break
		}
		fmt.Printf("%s: %s\n", record.Key, record.Value)
	}
	output.EndReading()

	// Output:
	//
	// 1: newspaper
}
コード例 #2
0
func ExampleMultipleOutputsDoer() {
	multiDoer := MakeMultipleOutputsDoFunc(func(inputRecord *store.Record, outputChans ...chan *store.Record) {
		outputChans[0] <- &store.Record{
			Key:   inputRecord.Key,
			Value: bytes.Join([][]byte{inputRecord.Value, inputRecord.Value}, []byte(",")),
		}
		outputChans[1] <- &store.Record{
			Key:   inputRecord.Key,
			Value: bytes.Join([][]byte{inputRecord.Value, inputRecord.Value}, []byte(";")),
		}
	}, 2)

	inputChan := make(chan *store.Record, 3)
	inputChan <- store.NewRecord("a", "b", 0)
	inputChan <- store.NewRecord("c", "d", 0)
	inputChan <- store.NewRecord("e", "f", 0)
	close(inputChan)

	outputChan := make(chan *store.Record, 6)

	multiDoer.Do(inputChan, outputChan)
	close(outputChan)

	for record := range outputChan {
		fmt.Printf("[%d] %s: %s\n", record.DatabaseIndex, record.Key, record.Value)
	}

	// Output:
	// [0] a: b,b
	// [1] a: b;b
	// [0] c: d,d
	// [1] c: d;d
	// [0] e: f,f
	// [1] e: f;f
}
コード例 #3
0
func ExampleDoer() {
	doer := MakeDoFunc(func(inputRecord *store.Record, outputChan chan *store.Record) {
		outputChan <- &store.Record{
			Key:   inputRecord.Key,
			Value: bytes.Join([][]byte{inputRecord.Value, inputRecord.Value}, []byte(",")),
		}
		outputChan <- &store.Record{
			Key:   inputRecord.Key,
			Value: bytes.Join([][]byte{inputRecord.Value, inputRecord.Value}, []byte(";")),
		}
	})

	inputChan := make(chan *store.Record, 3)
	inputChan <- store.NewRecord("a", "b", 0)
	inputChan <- store.NewRecord("c", "d", 0)
	inputChan <- store.NewRecord("e", "f", 0)
	close(inputChan)

	outputChan := make(chan *store.Record, 6)

	doer.Do(inputChan, outputChan)
	close(outputChan)

	for record := range outputChan {
		fmt.Printf("%s: %s\n", record.Key, record.Value)
	}

	// Output:
	// a: b,b
	// a: b;b
	// c: d,d
	// c: d;d
	// e: f,f
	// e: f;f
}
コード例 #4
0
func ExampleMapper() {
	mapper := MakeMapFunc(func(inputRecord *store.Record) *store.Record {
		return &store.Record{
			Key:   inputRecord.Key,
			Value: bytes.Join([][]byte{inputRecord.Value, inputRecord.Value}, []byte(",")),
		}
	})

	inputChan := make(chan *store.Record, 3)
	inputChan <- store.NewRecord("a", "b", 0)
	inputChan <- store.NewRecord("c", "d", 0)
	inputChan <- store.NewRecord("e", "f", 0)
	close(inputChan)

	outputChan := make(chan *store.Record, 3)

	mapper.Do(inputChan, outputChan)
	close(outputChan)

	for record := range outputChan {
		fmt.Printf("%s: %s\n", record.Key, record.Value)
	}

	// Output:
	// a: b,b
	// c: d,d
	// e: f,f
}
コード例 #5
0
func ExampleGroupDoer() {
	groupDoer := MakeGroupDoFunc(func(inputRecords []*store.Record, outputChan chan *store.Record) {
		for _, inputRecord := range inputRecords {
			switch inputRecord.DatabaseIndex {
			case 0:
				outputChan <- &store.Record{
					Key:   inputRecord.Key,
					Value: bytes.Join([][]byte{inputRecord.Value, inputRecord.Value}, []byte(",")),
				}
			case 1:
				outputChan <- &store.Record{
					Key:   inputRecord.Key,
					Value: bytes.Join([][]byte{inputRecord.Value, inputRecord.Value}, []byte(";")),
				}
			}
		}
	})

	inputChan := make(chan *store.Record, 4)
	inputChan <- store.NewRecord("a", "b", 0)
	inputChan <- store.NewRecord("c", "d", 1)
	inputChan <- store.NewRecord("e", "f", 0)
	inputChan <- store.NewRecord("g", "h", 1)
	close(inputChan)

	outputChan := make(chan *store.Record, 4)

	groupDoer.Do(inputChan, outputChan)
	close(outputChan)

	for record := range outputChan {
		fmt.Printf("%s: %s\n", record.Key, record.Value)
	}

	// Output:
	// a: b,b
	// c: d;d
	// e: f,f
	// g: h;h
}
コード例 #6
0
ファイル: join_test.go プロジェクト: sburnett/transformer
func ExampleJoin_multiple() {
	left := store.SliceStore{}
	left.BeginWriting()
	left.WriteRecord(store.NewRecord("1", "what", 0))
	left.WriteRecord(store.NewRecord("2", "news", 0))
	left.EndWriting()

	middle := store.SliceStore{}
	middle.BeginWriting()
	middle.WriteRecord(store.NewRecord("1", "so", 0))
	middle.WriteRecord(store.NewRecord("3", "the", 0))
	middle.EndWriting()

	right := store.SliceStore{}
	right.BeginWriting()
	right.WriteRecord(store.NewRecord("1", "ever", 0))
	right.WriteRecord(store.NewRecord("3", "less", 0))
	right.EndWriting()

	output := store.SliceStore{}

	joiner := Join([]byte("never"), []byte("paper"), []byte("man"))
	transformer.RunTransformer(transformer.MakeGroupDoFunc(joiner), store.NewDemuxingReader(&left, &middle, &right), &output)

	output.BeginReading()
	for {
		record, err := output.ReadRecord()
		if err != nil {
			panic(err)
		}
		if record == nil {
			break
		}
		fmt.Printf("%s: %s\n", record.Key, record.Value)
	}
	output.EndReading()

	// Output:
	//
	// 1: whatsoever
	// 2: newspaperman
	// 3: nevertheless
}