1
1
/*
2
2
* jPOS Project [http://jpos.org]
3
- * Copyright (C) 2000-2021 jPOS Software SRL
3
+ * Copyright (C) 2000-2023 jPOS Software SRL
4
4
*
5
5
* This program is free software: you can redistribute it and/or modify
6
6
* it under the terms of the GNU Affero General Public License as
17
17
*/
18
18
19
19
package org .jpos .space ;
20
- import org .jpos .util .Loggeable ;
21
20
21
+ import org .jpos .util .Loggeable ;
22
22
import java .io .PrintStream ;
23
23
import java .io .Serializable ;
24
- import java .time .Duration ;
25
24
import java .util .*;
26
25
import java .util .concurrent .TimeUnit ;
27
26
@@ -40,8 +39,9 @@ public class TSpace<K,V> implements LocalSpace<K,V>, Loggeable, Runnable {
40
39
private static final long GCLONG = 60 *1000 ;
41
40
private static final long NRD_RESOLUTION = 500L ;
42
41
private static final int MAX_ENTRIES_IN_DUMP = 1000 ;
42
+ private static final long ONE_MILLION = 1_000_000L ; // multiplier millis --> nanos
43
43
private final Set [] expirables ;
44
- private Duration lastLongGC = Duration . ofNanos ( System .nanoTime () );
44
+ private long lastLongGC = System .nanoTime ();
45
45
46
46
public TSpace () {
47
47
super ();
@@ -70,7 +70,7 @@ public void out (K key, V value, long timeout) {
70
70
throw new NullPointerException ("key=" + key + ", value=" + value );
71
71
Object v = value ;
72
72
if (timeout > 0 ) {
73
- v = new Expirable (value , Duration . ofNanos ( System .nanoTime ()). plus ( Duration . ofMillis ( timeout ) ));
73
+ v = new Expirable (value , System .nanoTime () + ( timeout * ONE_MILLION ));
74
74
}
75
75
synchronized (this ) {
76
76
List l = getList (key );
@@ -112,18 +112,18 @@ public synchronized V in (Object key) {
112
112
113
113
@ Override
114
114
public synchronized V in (Object key , long timeout ) {
115
- Object obj ;
116
- Duration to = Duration . ofMillis ( timeout );
117
- Duration now = Duration . ofNanos ( System . nanoTime ()) ;
118
- Duration duration ;
119
- while ((obj = inp (key )) == null &&
120
- to . compareTo ( duration = Duration . ofNanos ( System .nanoTime ()). minus ( now )) > 0 )
115
+ V obj ;
116
+ long now = System . nanoTime ( );
117
+ long to = now + timeout * ONE_MILLION ;
118
+ long waitFor ;
119
+ while ( (obj = inp (key )) == null &&
120
+ ( waitFor = ( to - System .nanoTime ())) >= 0 )
121
121
{
122
122
try {
123
- this .wait (Math .max (to . minus ( duration ). toMillis () , 1L ));
123
+ this .wait (Math .max (waitFor / ONE_MILLION , 1L ));
124
124
} catch (InterruptedException e ) { }
125
125
}
126
- return ( V ) obj ;
126
+ return obj ;
127
127
}
128
128
129
129
@ Override
@@ -139,18 +139,18 @@ public synchronized V rd (Object key) {
139
139
140
140
@ Override
141
141
public synchronized V rd (Object key , long timeout ) {
142
- Object obj ;
143
- Duration to = Duration . ofMillis ( timeout );
144
- Duration now = Duration . ofNanos ( System . nanoTime () );
145
- Duration duration ;
146
- while ((obj = rdp (key )) == null &&
147
- to . compareTo ( duration = Duration . ofNanos ( System .nanoTime ()). minus ( now )) > 0 )
142
+ V obj ;
143
+ long now = System . nanoTime ( );
144
+ long to = now + ( timeout * ONE_MILLION );
145
+ long waitFor ;
146
+ while ( (obj = rdp (key )) == null &&
147
+ ( waitFor = ( to - System .nanoTime ())) >= 0 )
148
148
{
149
149
try {
150
- this .wait (Math .max (to . minus ( duration ). toMillis () , 1L ));
150
+ this .wait (Math .max (waitFor / ONE_MILLION , 1L ));
151
151
} catch (InterruptedException e ) { }
152
152
}
153
- return ( V ) obj ;
153
+ return obj ;
154
154
}
155
155
156
156
@ Override
@@ -164,18 +164,19 @@ public synchronized void nrd (Object key) {
164
164
165
165
@ Override
166
166
public synchronized V nrd (Object key , long timeout ) {
167
- Object obj ;
168
- Duration to = Duration . ofMillis ( timeout );
169
- Duration now = Duration . ofNanos ( System . nanoTime () );
170
- Duration duration ;
171
- while ((obj = rdp (key )) != null &&
172
- to . compareTo ( duration = Duration . ofNanos ( System .nanoTime ()). minus ( now )) > 0 )
167
+ V obj ;
168
+ long now = System . nanoTime ( );
169
+ long to = now + ( timeout * ONE_MILLION );
170
+ long waitFor ;
171
+ while ( (obj = rdp (key )) != null &&
172
+ ( waitFor = ( to - System .nanoTime ())) >= 0 )
173
173
{
174
174
try {
175
- this .wait (Math .min (NRD_RESOLUTION , Math .max (to .minus (duration ).toMillis (), 1L )));
175
+ this .wait (Math .min (NRD_RESOLUTION ,
176
+ Math .max (waitFor / ONE_MILLION , 1L )));
176
177
} catch (InterruptedException ignored ) { }
177
178
}
178
- return ( V ) obj ;
179
+ return obj ;
179
180
}
180
181
181
182
@ Override
@@ -189,9 +190,9 @@ public void run () {
189
190
190
191
public void gc () {
191
192
gc (0 );
192
- if (Duration . ofMillis ( GCLONG ). compareTo ( Duration . ofNanos ( System .nanoTime ()). minus ( lastLongGC )) > 0 ) {
193
+ if (System .nanoTime () - lastLongGC > GCLONG ) {
193
194
gc (1 );
194
- lastLongGC = Duration . ofNanos ( System .nanoTime () );
195
+ lastLongGC = System .nanoTime ();
195
196
}
196
197
}
197
198
@@ -339,7 +340,7 @@ public void push (K key, V value, long timeout) {
339
340
throw new NullPointerException ("key=" + key + ", value=" + value );
340
341
Object v = value ;
341
342
if (timeout > 0 ) {
342
- v = new Expirable (value , Duration . ofNanos ( System .nanoTime ()). plus ( Duration . ofMillis ( timeout ) ));
343
+ v = new Expirable (value , System .nanoTime () + ( timeout * ONE_MILLION ));
343
344
}
344
345
synchronized (this ) {
345
346
List l = getList (key );
@@ -376,7 +377,7 @@ public void put (K key, V value, long timeout) {
376
377
throw new NullPointerException ("key=" + key + ", value=" + value );
377
378
Object v = value ;
378
379
if (timeout > 0 ) {
379
- v = new Expirable (value , Duration . ofNanos ( System .nanoTime ()). plus ( Duration . ofMillis ( timeout ) ));
380
+ v = new Expirable (value , System .nanoTime () + ( timeout * ONE_MILLION ));
380
381
}
381
382
synchronized (this ) {
382
383
List l = new LinkedList ();
@@ -402,15 +403,15 @@ public boolean existAny (K[] keys) {
402
403
403
404
@ Override
404
405
public boolean existAny (K [] keys , long timeout ) {
405
- Duration to = Duration . ofMillis ( timeout );
406
- Duration now = Duration . ofNanos ( System . nanoTime () );
407
- Duration duration ;
408
- while (to . compareTo ( duration = Duration . ofNanos ( System .nanoTime ()). minus ( now )) > 0 ) {
406
+ long now = System . nanoTime ( );
407
+ long to = now + ( timeout * ONE_MILLION );
408
+ long waitFor ;
409
+ while (( waitFor = ( to - System .nanoTime ())) >= 0 ) {
409
410
if (existAny (keys ))
410
411
return true ;
411
412
synchronized (this ) {
412
413
try {
413
- wait (Math .max (to . minus ( duration ). toMillis () , 1L ));
414
+ this . wait (Math .max (waitFor / ONE_MILLION , 1L ));
414
415
} catch (InterruptedException e ) { }
415
416
}
416
417
}
@@ -521,19 +522,24 @@ private void unregisterExpirable(Object k) {
521
522
522
523
static class Expirable implements Comparable , Serializable {
523
524
524
- static final long serialVersionUID = 0xA7F22BF5 ;
525
+ private static final long serialVersionUID = 0xA7F22BF5 ;
525
526
526
527
Object value ;
527
- Duration expires ;
528
528
529
- public Expirable (Object value , Duration expires ) {
529
+ /**
530
+ * When to expire, in the future, as given by monotonic System.nanoTime().<br>
531
+ * IMPORTANT: always use a nanosec offset from System.nanoTime()!
532
+ */
533
+ long expires ;
534
+
535
+ Expirable (Object value , long expires ) {
530
536
super ();
531
537
this .value = value ;
532
538
this .expires = expires ;
533
539
}
534
540
535
- public boolean isExpired () {
536
- return expires . compareTo ( Duration . ofNanos ( System .nanoTime ())) < 0 ;
541
+ boolean isExpired () {
542
+ return ( System .nanoTime () - expires ) > 0 ;
537
543
}
538
544
539
545
@ Override
@@ -544,20 +550,16 @@ public String toString() {
544
550
+ ",expired=" + isExpired ();
545
551
}
546
552
547
- public Object getValue () {
553
+ Object getValue () {
548
554
return isExpired () ? null : value ;
549
555
}
550
556
551
557
@ Override
552
- public int compareTo (Object obj ) {
553
- Expirable other = (Expirable ) obj ;
554
- Duration otherExpires = other .expires ;
555
- if (otherExpires .equals (expires ))
556
- return 0 ;
557
- else if (expires .compareTo (otherExpires ) < 0 )
558
- return -1 ;
559
- else
560
- return 1 ;
558
+ public int compareTo (Object other ) {
559
+ long diff = this .expires - ((Expirable )other ).expires ;
560
+ return diff > 0 ? 1 :
561
+ diff < 0 ? -1 :
562
+ 0 ;
561
563
}
562
564
}
563
565
0 commit comments