66 "fmt"
77 "google.golang.org/grpc/credentials/insecure"
88 "log"
9+ "regexp"
10+ "strconv"
911 "testing"
1012 "time"
1113
@@ -118,12 +120,15 @@ func ExampleRepository_Write() {
118120 if err != nil {
119121 log .Fatalln (err )
120122 }
123+
124+ fmt .Println ("cells-for_contact-101" )
121125 for _ , family := range row {
122126 for _ , cell := range family {
123127 fmt .Println (cell .Column , string (cell .Value ))
124128 }
125129 }
126130
131+ fmt .Println ("cells-for_contact-102" )
127132 row , err = tbl .ReadRow (ctx , "contact-102" )
128133 if err != nil {
129134 log .Fatalln (err )
@@ -138,25 +143,29 @@ func ExampleRepository_Write() {
138143 if err != nil {
139144 log .Fatalln (err )
140145 }
146+ fmt .Println ("mapped-event-for_contact-102" )
141147 for _ , event := range readSet .Events ["front" ] {
142148 fmt .Println (event .Date .UTC ())
143149 fmt .Println (event .RowKey )
144150 fmt .Println (event .Cells ["event_type" ])
145151 fmt .Println (event .Cells ["device_type" ])
146152 }
147153 // Output:
148- // front:11 1
149- // front:12 1
150- // front:13 1
154+ // cells-for_contact-101
151155 // front:d 1
152156 // front:d 1
153157 // front:d 1
158+ // front:e 13
159+ // front:e 12
160+ // front:e 11
154161 // front:u https://example.org/some/product
155162 // front:u https://example.org/some/product
156163 // front:u https://example.org/some/product
157- // front:12 1
164+ // cells-for_contact-102
158165 // front:d 2
166+ // front:e 12
159167 // front:u https://example.org/some/product
168+ // mapped-event-for_contact-102
160169 // 2018-01-01 00:02:00 +0000 UTC
161170 // contact-102
162171 // add_to_cart
@@ -239,7 +248,7 @@ func ExampleRepository_Search() {
239248 if err != nil {
240249 log .Fatalln (err )
241250 }
242- events := sortByDate (readSet .Events ["front" ])
251+ events := sortByContactID (readSet .Events ["front" ])
243252 for _ , event := range events {
244253 fmt .Println (event .Date .UTC ())
245254 fmt .Println (event .RowKey )
@@ -248,20 +257,22 @@ func ExampleRepository_Search() {
248257 }
249258
250259 // Output:
251- // 2018-01-01 00:00 :00 +0000 UTC
260+ // 2018-01-01 00:02 :00 +0000 UTC
252261 // contactx-101
253- // page_view
262+ // purchase
254263 // Smartphone
255264 // 2018-01-01 00:02:00 +0000 UTC
256265 // contactx-102
257266 // add_to_cart
258267 // Computer
259268}
260269
261- func sortByDate (events []* data.Event ) []* data.Event {
270+ func sortByContactID (events []* data.Event ) []* data.Event {
262271 for i := 0 ; i < len (events ); i ++ {
272+ iID := extractID (events [i ].RowKey )
263273 for j := 0 ; j < len (events ); j ++ {
264- if events [i ].Date .Unix () < events [j ].Date .Unix () {
274+ jID := extractID (events [j ].RowKey )
275+ if iID < jID {
265276 perm := events [j ]
266277 events [j ] = events [i ]
267278 events [i ] = perm
@@ -271,6 +282,29 @@ func sortByDate(events []*data.Event) []*data.Event {
271282 return events
272283}
273284
285+ func TestExtractID (t * testing.T ) {
286+ l := "contact-113"
287+ i := extractID (l )
288+ if i != 113 {
289+ t .Fatalf ("unexpected value: %+v" , i )
290+ }
291+ }
292+
293+ func extractID (literalID string ) int {
294+ re := regexp .MustCompile ("[0-9]+" )
295+ m := re .FindAllString (literalID , 1 )
296+ out := 0
297+ for _ , f := range m {
298+ i , err := strconv .Atoi (f )
299+ if err != nil {
300+ log .Fatalln (err )
301+ return out
302+ }
303+ out = i
304+ }
305+ return out
306+ }
307+
274308func ExampleRepository_ReadLast () {
275309 ctx := context .Background ()
276310 client := getBigTableClient (ctx )
@@ -339,9 +373,8 @@ func ExampleRepository_ReadLast() {
339373 fmt .Println (event .Cells ["event_type" ])
340374 fmt .Println (event .Cells ["device_type" ])
341375 }
376+
342377 // Output:
343- // add_to_cart
344- //
345378 // purchase
346379 // Smartphone
347380}
@@ -684,9 +717,9 @@ func (a mockAdapter) ReadRow(_ context.Context, row string, _ ...bigtable.ReadOp
684717 },
685718 {
686719 Row : row ,
687- Column : "11 " ,
720+ Column : "e " ,
688721 Timestamp : t1 ,
689- Value : []byte ("1 " ),
722+ Value : []byte ("11 " ),
690723 },
691724 {
692725 Row : row ,
@@ -702,9 +735,9 @@ func (a mockAdapter) ReadRow(_ context.Context, row string, _ ...bigtable.ReadOp
702735 },
703736 {
704737 Row : row ,
705- Column : "12 " ,
738+ Column : "e " ,
706739 Timestamp : t2 ,
707- Value : []byte ("1 " ),
740+ Value : []byte ("12 " ),
708741 },
709742 {
710743 Row : row ,
@@ -720,9 +753,9 @@ func (a mockAdapter) ReadRow(_ context.Context, row string, _ ...bigtable.ReadOp
720753 },
721754 {
722755 Row : row ,
723- Column : "13 " ,
756+ Column : "e " ,
724757 Timestamp : t3 ,
725- Value : []byte ("1 " ),
758+ Value : []byte ("13 " ),
726759 },
727760 },
728761 }
@@ -791,12 +824,12 @@ func generateMutations(numEvents int) []*bigtable.Mutation {
791824 t := bigtable .Time (time .Now ().Add (- time .Duration (i ) * time .Minute ))
792825 mut .Set ("front" , "u" , t , []byte (fmt .Sprintf ("https://www.example.com/products/%d" , mod )))
793826 switch mod {
794- case 1 , 2 :
795- mut .Set ("front" , "12 " , t , []byte ("1 " ))
827+ case 2 :
828+ mut .Set ("front" , "e " , t , []byte ("12 " ))
796829 case 3 :
797- mut .Set ("front" , "13 " , t , []byte ("1 " ))
830+ mut .Set ("front" , "e " , t , []byte ("13 " ))
798831 default :
799- mut .Set ("front" , "11 " , t , []byte ("1 " ))
832+ mut .Set ("front" , "e " , t , []byte ("11 " ))
800833 }
801834 mut .Set ("front" , "d" , t , []byte (fmt .Sprintf ("%d" , 1 + (i % 2 ))))
802835 data = append (data , mut )
0 commit comments