@@ -368,3 +368,148 @@ func (m *mapClient) Events(ctx context.Context, opts ...EventsOption) (EventStre
368368 return nil , ctx .Err ()
369369 }
370370}
371+
372+ func (m * mapClient ) Transaction (ctx context.Context ) Transaction [string , []byte ] {
373+ return & transactionClient {
374+ mapClient : m ,
375+ ctx : ctx ,
376+ }
377+ }
378+
379+ type transactionClient struct {
380+ * mapClient
381+ ctx context.Context
382+ operations []mapv1.CommitRequest_Operation
383+ }
384+
385+ func (t * transactionClient ) Put (key string , value []byte , opts ... PutOption ) Transaction [string , []byte ] {
386+ request := & mapv1.PutRequest {
387+ Key : key ,
388+ Value : value ,
389+ }
390+ for _ , opt := range opts {
391+ opt .beforePut (request )
392+ }
393+ t .operations = append (t .operations , mapv1.CommitRequest_Operation {
394+ Operation : & mapv1.CommitRequest_Operation_Put {
395+ Put : & mapv1.CommitRequest_Put {
396+ Key : request .Key ,
397+ Value : request .Value ,
398+ TTL : request .TTL ,
399+ PrevVersion : request .PrevVersion ,
400+ },
401+ },
402+ })
403+ return t
404+ }
405+
406+ func (t * transactionClient ) Insert (key string , value []byte , opts ... InsertOption ) Transaction [string , []byte ] {
407+ request := & mapv1.InsertRequest {
408+ Key : key ,
409+ Value : value ,
410+ }
411+ for _ , opt := range opts {
412+ opt .beforeInsert (request )
413+ }
414+ t .operations = append (t .operations , mapv1.CommitRequest_Operation {
415+ Operation : & mapv1.CommitRequest_Operation_Insert {
416+ Insert : & mapv1.CommitRequest_Insert {
417+ Key : request .Key ,
418+ Value : request .Value ,
419+ TTL : request .TTL ,
420+ },
421+ },
422+ })
423+ return t
424+ }
425+
426+ func (t * transactionClient ) Update (key string , value []byte , opts ... UpdateOption ) Transaction [string , []byte ] {
427+ request := & mapv1.UpdateRequest {
428+ Key : key ,
429+ Value : value ,
430+ }
431+ for _ , opt := range opts {
432+ opt .beforeUpdate (request )
433+ }
434+ t .operations = append (t .operations , mapv1.CommitRequest_Operation {
435+ Operation : & mapv1.CommitRequest_Operation_Update {
436+ Update : & mapv1.CommitRequest_Update {
437+ Key : request .Key ,
438+ Value : request .Value ,
439+ TTL : request .TTL ,
440+ PrevVersion : request .PrevVersion ,
441+ },
442+ },
443+ })
444+ return t
445+ }
446+
447+ func (t * transactionClient ) Remove (key string , opts ... RemoveOption ) Transaction [string , []byte ] {
448+ request := & mapv1.RemoveRequest {
449+ Key : key ,
450+ }
451+ for _ , opt := range opts {
452+ opt .beforeRemove (request )
453+ }
454+ t .operations = append (t .operations , mapv1.CommitRequest_Operation {
455+ Operation : & mapv1.CommitRequest_Operation_Remove {
456+ Remove : & mapv1.CommitRequest_Remove {
457+ Key : request .Key ,
458+ PrevVersion : request .PrevVersion ,
459+ },
460+ },
461+ })
462+ return t
463+ }
464+
465+ func (t * transactionClient ) Commit () ([]* Entry [string , []byte ], error ) {
466+ request := & mapv1.CommitRequest {
467+ ID : runtimev1.PrimitiveID {
468+ Name : t .Name (),
469+ },
470+ Operations : t .operations ,
471+ }
472+ response , err := t .client .Commit (t .ctx , request )
473+ if err != nil {
474+ return nil , err
475+ }
476+ entries := make ([]* Entry [string , []byte ], len (t .operations ))
477+ for i , operation := range t .operations {
478+ result := response .Results [i ]
479+ switch o := operation .Operation .(type ) {
480+ case * mapv1.CommitRequest_Operation_Put :
481+ entries [i ] = & Entry [string , []byte ]{
482+ Key : o .Put .Key ,
483+ Versioned : primitive.Versioned [[]byte ]{
484+ Value : o .Put .Value ,
485+ Version : primitive .Version (result .GetPut ().Version ),
486+ },
487+ }
488+ case * mapv1.CommitRequest_Operation_Insert :
489+ entries [i ] = & Entry [string , []byte ]{
490+ Key : o .Insert .Key ,
491+ Versioned : primitive.Versioned [[]byte ]{
492+ Value : o .Insert .Value ,
493+ Version : primitive .Version (result .GetInsert ().Version ),
494+ },
495+ }
496+ case * mapv1.CommitRequest_Operation_Update :
497+ entries [i ] = & Entry [string , []byte ]{
498+ Key : o .Update .Key ,
499+ Versioned : primitive.Versioned [[]byte ]{
500+ Value : o .Update .Value ,
501+ Version : primitive .Version (result .GetUpdate ().Version ),
502+ },
503+ }
504+ case * mapv1.CommitRequest_Operation_Remove :
505+ entries [i ] = & Entry [string , []byte ]{
506+ Key : o .Remove .Key ,
507+ Versioned : primitive.Versioned [[]byte ]{
508+ Value : result .GetRemove ().Value .Value ,
509+ Version : primitive .Version (result .GetRemove ().Value .Version ),
510+ },
511+ }
512+ }
513+ }
514+ return entries , nil
515+ }
0 commit comments