From d957b09eedb5bc4593826f3fc29c152941f68942 Mon Sep 17 00:00:00 2001 From: Alejandro Revilla Date: Tue, 2 Feb 2021 15:56:49 -0300 Subject: [PATCH] added SeqNoMgr.get methods --- .../src/main/java/org/jpos/ee/SeqNo.java | 5 +++ .../main/java/org/jpos/ee/SeqNoManager.java | 32 +++++++++++++++---- .../src/test/java/org/jpos/ee/SeqNoTest.java | 23 +++++++++++++ 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/modules/seqno/src/main/java/org/jpos/ee/SeqNo.java b/modules/seqno/src/main/java/org/jpos/ee/SeqNo.java index e7d9b2d8e2..bb37f16659 100644 --- a/modules/seqno/src/main/java/org/jpos/ee/SeqNo.java +++ b/modules/seqno/src/main/java/org/jpos/ee/SeqNo.java @@ -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(); } diff --git a/modules/seqno/src/main/java/org/jpos/ee/SeqNoManager.java b/modules/seqno/src/main/java/org/jpos/ee/SeqNoManager.java index fc99a3c937..eeb519e8a5 100644 --- a/modules/seqno/src/main/java/org/jpos/ee/SeqNoManager.java +++ b/modules/seqno/src/main/java/org/jpos/ee/SeqNoManager.java @@ -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' * @@ -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) { } diff --git a/modules/seqno/src/test/java/org/jpos/ee/SeqNoTest.java b/modules/seqno/src/test/java/org/jpos/ee/SeqNoTest.java index e96d473ceb..2c7575d4f4 100644 --- a/modules/seqno/src/test/java/org/jpos/ee/SeqNoTest.java +++ b/modules/seqno/src/test/java/org/jpos/ee/SeqNoTest.java @@ -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;