Skip to content

Commit

Permalink
added SeqNoMgr.get methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ar committed Feb 2, 2021
1 parent bd475f1 commit d957b09
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
5 changes: 5 additions & 0 deletions modules/seqno/src/main/java/org/jpos/ee/SeqNo.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public SeqNo(String id) {
this.id = id;
}

public SeqNo(String id, long value) {
this.id = id;
this.value = value;
}

public SeqNo() {
super();
}
Expand Down
32 changes: 25 additions & 7 deletions modules/seqno/src/main/java/org/jpos/ee/SeqNoManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,26 @@ public void reset (String id, long value) {
getOrCreate(id).setValue(value);
}

/**
* get SeqNo value
* @param id sequencer id
* @return current value
*/
public long get (String id) {
return getOrCreate(id).getValue();
}

/**
* get SeqNo value
* @param id sequencer id
* @param initialValue for newly created sequencers
* @return current value
*/
public long get (String id, long initialValue) {
return getOrCreate(id, initialValue).getValue();
}

/* Asynchronous methods */

/**
* Asynchronous 'lock'
*
Expand Down Expand Up @@ -154,23 +171,24 @@ public boolean release (String id, long lockedBy) {
return false;
}




private SeqNo getOrCreate(String id) {
return getOrCreate(id, 0L);
}

private SeqNo getOrCreate(String id, long initialValue) {
SeqNo seq = db.session().get(SeqNo.class, id, LockMode.PESSIMISTIC_WRITE);
if (seq == null) {
create (id);
create (id, initialValue);
seq = db.session().get(SeqNo.class, id, LockMode.PESSIMISTIC_WRITE);
}
return seq;
}

private void create (String id) {
private void create (String id, long initialValue) {
try (DB db = new DB()) {
db.open();
db.beginTransaction();
SeqNo seq = new SeqNo(id);
SeqNo seq = new SeqNo(id, initialValue);
db.session().save(seq);
db.commit();
} catch (Exception ignored) { }
Expand Down
23 changes: 23 additions & 0 deletions modules/seqno/src/test/java/org/jpos/ee/SeqNoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,29 @@ public static void setUp() throws DocumentException {
}
}

@Test
public void testNewSeqNoWithDefault() {
try (DB db = new DB()) {
db.open();
SeqNoManager mgr = new SeqNoManager(db);
db.beginTransaction();
assertEquals(5, mgr.get("test.new.default.5", 5L));
db.commit();
}
try (DB db = new DB()) {
db.open();
SeqNoManager mgr = new SeqNoManager(db);
db.beginTransaction();
assertEquals(6, mgr.next("test.new.default.5", 10L));
assertEquals(7, mgr.next("test.new.default.5", 10L));
assertEquals(8, mgr.next("test.new.default.5", 10L));
assertEquals(9, mgr.next("test.new.default.5", 10L));
assertEquals(10, mgr.next("test.new.default.5", 10L));
assertEquals(1, mgr.next("test.new.default.5", 10L));
db.commit();
}
}

@Test
public void testSyncLock() throws InterruptedException {
int runs = 20;
Expand Down

0 comments on commit d957b09

Please sign in to comment.