File Coverage

blib/lib/Net/Etcd/KV/Txn.pm
Criterion Covered Total %
statement 27 38 71.0
branch 0 6 0.0
condition 0 3 0.0
subroutine 9 10 90.0
pod 1 1 100.0
total 37 58 63.7


line stmt bran cond sub pod time code
1 9     9   47 use utf8;
  9         13  
  9         47  
2             package Net::Etcd::KV::Txn;
3              
4 9     9   283 use strict;
  9         15  
  9         150  
5 9     9   33 use warnings;
  9         14  
  9         174  
6              
7 9     9   33 use Moo;
  9         12  
  9         45  
8 9     9   2679 use Types::Standard qw(InstanceOf Str Int Bool HashRef ArrayRef);
  9         17  
  9         60  
9 9     9   8064 use MIME::Base64;
  9         17  
  9         371  
10 9     9   43 use Data::Dumper;
  9         20  
  9         381  
11 9     9   49 use JSON;
  9         13  
  9         40  
12              
13             with 'Net::Etcd::Role::Actions';
14              
15 9     9   850 use namespace::clean;
  9         24  
  9         178  
16              
17             =head1 NAME
18              
19             Net::Etcd::KV::Txn
20              
21             =cut
22              
23             our $VERSION = '0.022';
24              
25             =head1 DESCRIPTION
26              
27             Txn processes multiple requests in a single transaction. A txn request increments
28             the revision of the key-value store and generates events with the same revision for
29             every completed request. It is not allowed to modify the same key several times
30             within one txn.
31             From google paxosdb paper: Our implementation hinges around a powerful primitive which
32             we call MultiOp. All other database operations except for iteration are implemented as
33             a single call to MultiOp. A MultiOp is applied atomically and consists of three components:
34              
35             1. A list of tests called guard. Each test in guard checks a single entry in the database.
36             It may check for the absence or presence of a value, or compare with a given value. Two
37             different tests in the guard may apply to the same or different entries in the database.
38             All tests in the guard are applied and MultiOp returns the results. If all tests are true,
39             MultiOp executes t op (see item 2 below), otherwise it executes f op (see item 3 below).
40            
41             2. A list of database operations called t op. Each operation in the list is either an insert,
42             delete, or lookup operation, and applies to a single database entry. Two different operations
43             in the list may apply to the same or different entries in the database. These operations are
44             executed if guard evaluates to true.
45            
46             3. A list of database operations called f op. Like t op, but executed if guard evaluates to false.
47              
48             =head1 ACCESSORS
49              
50             =head2 endpoint
51              
52             /v3alpha/kv/txn
53              
54             =cut
55              
56             has endpoint => (
57             is => 'ro',
58             isa => Str,
59             default => '/kv/txn'
60             );
61              
62             =head2 compare
63              
64             compare is a list of predicates representing a conjunction of terms. If the comparisons
65             succeed, then the success requests will be processed in order, and the response will
66             contain their respective responses in order. If the comparisons fail, then the failure
67             requests will be processed in order, and the response will contain their respective
68             responses in order.
69              
70             =cut
71              
72             has compare => (
73             is => 'ro',
74             isa => ArrayRef,
75             required => 1,
76             );
77              
78             =head2 success
79              
80             success is a list of requests which will be applied when compare evaluates to true.
81              
82             =cut
83              
84             has success => (
85             is => 'ro',
86             isa => ArrayRef,
87             );
88              
89             =head2 failure
90              
91             failure is a list of requests which will be applied when compare evaluates to false.
92              
93             =cut
94              
95             has failure => (
96             is => 'ro',
97             isa => ArrayRef,
98             );
99              
100             =head1 PUBLIC METHODS
101              
102             =head2 create
103              
104             create txn
105              
106             =cut
107              
108             #TODO hack alert
109              
110             sub create {
111 0     0 1   my $self = shift;
112 0           my $compare = $self->compare;
113 0           my $success = $self->success;
114 0           my $failure = $self->failure;
115              
116 0           my $txn ='"compare":[' . join(',',@$compare) . '],';
117 0 0         $txn .= '"success":[' . join(',', @$success) . ']' if defined $success;
118 0 0 0       $txn .= ',' if defined $success and defined $failure;
119 0 0         $txn .= '"failure":[ ' . join(',', @$failure) . ']' if defined $failure;
120 0           $self->{json_args} = '{' . $txn . '}';
121             # print STDERR Dumper($self);
122 0           $self->request;
123 0           return $self;
124             }
125              
126             1;