/
MapTransactionLogRecord.java
152 lines (130 loc) · 4.4 KB
/
MapTransactionLogRecord.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.map.impl.tx;
import com.hazelcast.internal.nearcache.NearCachingHook;
import com.hazelcast.map.impl.MapDataSerializerHook;
import com.hazelcast.map.impl.MapRecordKey;
import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import com.hazelcast.nio.serialization.Data;
import com.hazelcast.spi.Operation;
import com.hazelcast.transaction.impl.TransactionLogRecord;
import com.hazelcast.util.ThreadUtil;
import java.io.IOException;
/**
* Represents an operation on the map in the transaction log.
*/
public class MapTransactionLogRecord implements TransactionLogRecord {
private int partitionId;
private String name;
private Data key;
private long threadId = ThreadUtil.getThreadId();
private String ownerUuid;
private Operation op;
private transient NearCachingHook nearCachingHook = NearCachingHook.EMPTY_HOOK;
public MapTransactionLogRecord() {
}
public MapTransactionLogRecord(String name, Data key, int partitionId,
Operation op, String ownerUuid, NearCachingHook nearCachingHook) {
assert nearCachingHook != null;
this.name = name;
this.key = key;
if (!(op instanceof MapTxnOperation)) {
throw new IllegalArgumentException();
}
this.op = op;
this.ownerUuid = ownerUuid;
this.partitionId = partitionId;
this.nearCachingHook = nearCachingHook;
}
@Override
public Operation newPrepareOperation() {
TxnPrepareOperation operation = new TxnPrepareOperation(partitionId, name, key, ownerUuid);
operation.setThreadId(threadId);
return operation;
}
@Override
public Operation newCommitOperation() {
MapTxnOperation operation = (MapTxnOperation) op;
operation.setThreadId(threadId);
operation.setOwnerUuid(ownerUuid);
op.setPartitionId(partitionId);
return op;
}
@Override
public void onCommitSuccess() {
assert nearCachingHook != null;
nearCachingHook.onRemoteCallSuccess();
}
@Override
public void onCommitFailure() {
assert nearCachingHook != null;
nearCachingHook.onRemoteCallFailure();
}
@Override
public Operation newRollbackOperation() {
TxnRollbackOperation operation = new TxnRollbackOperation(partitionId, name, key, ownerUuid);
operation.setThreadId(threadId);
return operation;
}
@Override
public void writeData(ObjectDataOutput out) throws IOException {
out.writeUTF(name);
out.writeInt(partitionId);
boolean isNullKey = key == null;
out.writeBoolean(isNullKey);
if (!isNullKey) {
out.writeData(key);
}
out.writeLong(threadId);
out.writeUTF(ownerUuid);
out.writeObject(op);
}
@Override
public void readData(ObjectDataInput in) throws IOException {
name = in.readUTF();
partitionId = in.readInt();
boolean isNullKey = in.readBoolean();
if (!isNullKey) {
key = in.readData();
}
threadId = in.readLong();
ownerUuid = in.readUTF();
op = in.readObject();
}
@Override
public Object getKey() {
return new MapRecordKey(name, key);
}
@Override
public String toString() {
return "MapTransactionRecord{"
+ "name='" + name + '\''
+ ", key=" + key
+ ", threadId=" + threadId
+ ", ownerUuid='" + ownerUuid + '\''
+ ", op=" + op
+ '}';
}
@Override
public int getFactoryId() {
return MapDataSerializerHook.F_ID;
}
@Override
public int getId() {
return MapDataSerializerHook.MAP_TRANSACTION_LOG_RECORD;
}
}