aboutsummaryrefslogtreecommitdiff
path: root/node_modules/montage/data/transaction-id.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/montage/data/transaction-id.js')
-rwxr-xr-xnode_modules/montage/data/transaction-id.js267
1 files changed, 267 insertions, 0 deletions
diff --git a/node_modules/montage/data/transaction-id.js b/node_modules/montage/data/transaction-id.js
new file mode 100755
index 00000000..85470b0e
--- /dev/null
+++ b/node_modules/montage/data/transaction-id.js
@@ -0,0 +1,267 @@
1/* <copyright>
2 This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
3 No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
4 (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5 </copyright> */
6/**
7 @module montage/data/transaction-id
8 @requires montage/core/core
9 @requires montage/core/uuid
10 @requires montage/core/logger
11 */
12var Montage = require("montage").Montage;
13var Uuid = require("core/uuid").Uuid;
14var logger = require("core/logger").logger("transaction-id");
15/**
16 @private
17 */
18var _lastTimestamp = Date.now();
19/**
20 @private
21 */
22var _lastNanos = 1;
23/**
24 @private
25 */
26var _transactionManagerInstance = null;
27/**
28 @class module:montage/data/transaction-id.TransactionId
29 @extends module:montage/core/core.Montage
30 */
31var TransactionId = exports.TransactionId = Montage.create(Montage, /** @lends module:montage/data/transaction-id.TransactionId# */ {
32
33 _mappingFolderName:{
34 serializable:true,
35 enumerable:false,
36 value:""
37 },
38
39 mappingFolderName:{
40 get:function () {
41 return this._mappingFolderName;
42 }
43 },
44
45 /**
46 This is used to guarantee unicity.
47 @private
48 */
49 _uuid:{
50 serializable:true,
51 enumerable:false,
52 value:null
53 },
54
55 /**
56 This is used to order transactions.
57 @private
58 */
59 _timestamp:{
60 serializable:true,
61 enumerable:false,
62 value:null
63 },
64
65 /**
66 This is used to order transactions.
67 @private
68 */
69 _nanos:{
70 serializable:true,
71 enumerable:false,
72 value:null
73 },
74
75 /**
76 Description TODO
77 @param {name} Mapping folder name used for this transaction
78 @function
79 @returns itself
80 */
81 initWithMappingFolderName:{
82 serializable:false,
83 enumerable:false,
84 value:function (name) {
85 this._mappingFolderName = name;
86 this._uuid = Uuid.generate();
87 var timestamp = Date.now();
88 if (_lastTimestamp === timestamp) {
89 _lastNanos = _lastNanos + 1;
90 } else {
91 _lastTimestamp = timestamp;
92 _lastNanos = 1
93 }
94 this._timestamp = _lastTimestamp;
95 this._nanos = _lastNanos;
96 if (logger.isDebug) {
97 logger.debug(this, "New Transaction ID: " + this._timestamp);
98 }
99 return this;
100 }
101 },
102
103 /**
104 Factory method used to create new Transaction IDs.<br>
105 This factory supports a delegate so that application requiring subclassing can do so easily.<br>
106 The factory delegate should implement <code>createTransactionId</code> method.
107 @function
108 @returns TransactionId.create().init() A newly initialized transaction ID.
109 */
110 factory:{
111 value:function () {
112 if (this.factory.delegate && typeof this.factory.delegate.createTransactionId === "function") {
113 return this.factory.delegate.createTransactionId();
114 } else {
115 return TransactionId.create().init();
116 }
117 }},
118
119 /**
120 Description TODO
121 @function
122 @param {Property} transactionId For comparison purposes.
123 @returns {Boolean} true If transactionId is after the target transaction ID.
124 */
125 before:{
126 value:function (transactionId) {
127 if (this._timestamp === transactionId._timestamp) {
128 return this._nanos < transactionId._nanos;
129 }
130 return this._timestamp < transactionId._timestamp;
131
132 }},
133
134 /**
135 Description TODO
136 @function
137 @param {Property} transactionId For comparison purposes.
138 @returns {Boolean} true If transactionId is before the target transaction ID.
139 */
140 after:{
141 value:function (transactionId) {
142 if (this._timestamp === transactionId._timestamp) {
143 return this._nanos > transactionId._nanos;
144 }
145 return this._timestamp > transactionId._timestamp;
146 }},
147
148 /**
149 Returns the transaction manager.<br>
150 The transaction manager is a unique object in charge of openning and closing transactions.
151 @function
152 @returns transaction manager
153 */
154 manager:{
155 get:function () {
156 if (_transactionManagerInstance === null) {
157 _transactionManagerInstance = TransactionManager.create().init();
158 }
159 return _transactionManagerInstance;
160 }
161 }
162
163
164});
165/**
166 @class module:montage/data/transaction-id.TransactionManager
167 */
168var TransactionManager = exports.TransactionManager = Montage.create(Montage, /** @lends module:montage/data/transaction-id.TransactionManager# */ {
169
170 /*
171 * @private
172 */
173 _currentTransaction:{
174 serializable:false,
175 enumerable:false,
176 value:null
177 },
178
179 /**
180 Enables the trace of creation starts.<br>
181 When enabled, the transaction ID will memorize the state of the thread stack when created.
182 @type {Property} Function
183 @default {Boolean} false
184 */
185 traceTransactionStart:{
186 serializable:false,
187 enumerable:false,
188 value:false
189 },
190
191 /**
192 Description TODO
193 @function
194 @returns itself
195 */
196 init:{
197 serializable:false,
198 enumerable:false,
199 value:function () {
200 return this;
201 }
202 },
203
204 /**
205 Opens a new transaction ID for this thread.
206 @function
207 @param {name} Mapping folder name used for this transaction
208 @returns null or new transaction ID
209 @throws IllegalStateException if a transaction is already open for this thread.
210 */
211 startTransaction:{ value:function (name) {
212 if (this._currentTransaction) {
213 throw new Error("Transaction Open: " + JSON.stringify(this._currentTransaction));
214 }
215 this._currentTransaction = TransactionId.create().initWithMappingFolderName(name);
216 return this._currentTransaction;
217 }},
218
219 /**
220 Returns the current transaction ID for this thread.
221 @function
222 @returns null or current transaction ID
223 */
224 currentTransaction:{ value:function () {
225 return this._currentTransaction;
226 }},
227
228 /**
229 Checks if the current thread has an open transaction.
230 @function
231 @returns {Boolean} <code>true</code> if the current thread has an open transaction, <code>flase</code> otherwise.
232 */
233 hasCurrentTransaction:{ value:function () {
234 return this._currentTransaction != null;