Skip to content

Commit ad5f947

Browse files
[TH2-5165] corrected after review
1 parent 10c1056 commit ad5f947

File tree

3 files changed

+27
-36
lines changed

3 files changed

+27
-36
lines changed

cradle-core/src/main/java/com/exactpro/cradle/BookInfo.java

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
import com.github.benmanes.caffeine.cache.Caffeine;
2020
import com.github.benmanes.caffeine.cache.LoadingCache;
21-
import org.apache.commons.lang3.concurrent.AtomicInitializer;
22-
import org.apache.commons.lang3.concurrent.ConcurrentException;
2321
import org.slf4j.Logger;
2422
import org.slf4j.LoggerFactory;
2523

@@ -32,6 +30,7 @@
3230
import java.util.Map;
3331
import java.util.Map.Entry;
3432
import java.util.TreeMap;
33+
import java.util.concurrent.atomic.AtomicReference;
3534

3635
/**
3736
* Information about a book
@@ -52,9 +51,10 @@ public class BookInfo
5251
private final LoadingCache<Long, IPageInterval> randomAccessCache;
5352

5453
// AtomicInitializer call initialize method again if previous value is null
55-
private final AtomicInitializer<PageInfo> firstPage;
54+
private final AtomicReference<PageInfo> firstPage = new AtomicReference<>();
5655
private final PagesLoader pagesLoader;
57-
private final PageLoader lastPagesLoader;
56+
private final PageLoader firstPageLoader;
57+
private final PageLoader lastPageLoader;
5858

5959
public BookInfo(BookId id,
6060
String fullName,
@@ -69,23 +69,16 @@ public BookInfo(BookId id,
6969
this.desc = desc;
7070
this.created = created;
7171
this.pagesLoader = pagesLoader;
72-
this.lastPagesLoader = lastPageLoader;
72+
this.firstPageLoader = firstPageLoader;
73+
this.lastPageLoader = lastPageLoader;
7374

74-
this.firstPage = new AtomicInitializer<>() {
75-
@Override
76-
protected PageInfo initialize() {
77-
return firstPageLoader.load(id);
78-
}
79-
};
8075
this.hotCache = Caffeine.newBuilder()
8176
.maximumSize(HOT_CACHE_SIZE)
8277
.build(this::createPageInterval);
8378

8479
this.randomAccessCache = Caffeine.newBuilder()
8580
.maximumSize(cacheSize)
8681
.build(this::createPageInterval);
87-
88-
initializeHotCache();
8982
}
9083

9184
public BookId getId()
@@ -119,17 +112,22 @@ public Collection<PageInfo> getPages()
119112

120113
public @Nullable PageInfo getFirstPage()
121114
{
122-
try {
123-
return firstPage.get();
124-
} catch (ConcurrentException e) {
125-
LOGGER.error("Unexpected exception during first page lazy initialization", e);
126-
return null;
127-
}
115+
PageInfo result = firstPage.get();
116+
117+
if (result == null) {
118+
result = firstPageLoader.load(id);
119+
if (!firstPage.compareAndSet(null, result)) {
120+
// another thread has initialized the reference
121+
result = firstPage.get();
122+
}
123+
}
124+
125+
return result;
128126
}
129127

130128
public @Nullable PageInfo getLastPage()
131129
{
132-
return lastPagesLoader.load(id);
130+
return lastPageLoader.load(id);
133131
}
134132

135133
public PageInfo getPage(PageId pageId)
@@ -161,7 +159,7 @@ public PageInfo getPreviousPage(Instant startTimestamp)
161159
}
162160

163161
void invalidate() {
164-
// FIXME: invalidate initializer
162+
firstPage.set(null);
165163
hotCache.invalidateAll();
166164
randomAccessCache.invalidateAll();
167165
}
@@ -191,14 +189,6 @@ private void invalidate(long epochDay) {
191189
randomAccessCache.invalidate(epochDay);
192190
}
193191

194-
@SuppressWarnings("ResultOfMethodCallIgnored")
195-
private void initializeHotCache() {
196-
long currentEpochDay = currentEpochDay();
197-
for (int shift = HOT_CACHE_SIZE - 1; shift >= 0; shift--) {
198-
this.hotCache.get(currentEpochDay - shift);
199-
}
200-
}
201-
202192
private IPageInterval getPageInterval(long epochDate) {
203193
long currentEpochDate = currentEpochDay();
204194
IPageInterval pageInterval = currentEpochDate - epochDate < 2
@@ -300,13 +290,13 @@ public PageInfo find(Instant timestamp)
300290

301291
@Override
302292
public PageInfo next(Instant startTimestamp) {
303-
Entry<Instant, PageInfo> result = pageByInstant.ceilingEntry(startTimestamp.plus(1, ChronoUnit.NANOS));
293+
Entry<Instant, PageInfo> result = pageByInstant.higherEntry(startTimestamp);
304294
return result != null ? result.getValue() : null;
305295
}
306296

307297
@Override
308298
public PageInfo previous(Instant startTimestamp) {
309-
Entry<Instant, PageInfo> result = pageByInstant.floorEntry(startTimestamp.minus(1, ChronoUnit.NANOS));
299+
Entry<Instant, PageInfo> result = pageByInstant.lowerEntry(startTimestamp);
310300
return result != null ? result.getValue() : null;
311301
}
312302

cradle-core/src/main/java/com/exactpro/cradle/CradleStorage.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1469,7 +1469,6 @@ public PageInfo updatePageName(BookId bookId, String pageName, String newPageNam
14691469
}
14701470

14711471
public PageInfo updatePageName(BookId bookId, Instant pageStart, String pageName, String newPageName) throws CradleStorageException {
1472-
getBookCache().getBook(bookId);
14731472
PageInfo updatedPageInfo = doUpdatePageName(bookId, pageName, newPageName);
14741473

14751474
try {

cradle-core/src/test/java/com/exactpro/cradle/BookInfoTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,22 @@
3232
public class BookInfoTest {
3333
public static Random RANDOM = new Random();
3434
public static final BookId BOOK_ID = new BookId("test-book");
35-
public static final List<PageInfo> PAGES;
35+
private static final List<PageInfo> PAGES;
3636

3737
static {
38-
PAGES = new ArrayList<>();
38+
List<PageInfo> pages = new ArrayList<>();
3939
Instant start = Instant.now().minus(7, ChronoUnit.DAYS);
4040
Instant end = Instant.now();
4141
Instant current = start;
4242
Instant previous;
4343
do {
4444
previous = current;
4545
current = current.plus(1, ChronoUnit.HOURS);
46-
PAGES.add(createPageInfo(previous, current));
46+
pages.add(createPageInfo(previous, current));
4747
} while (current.isBefore(end));
48-
PAGES.add(createPageInfo(current, current));
48+
pages.add(createPageInfo(current, current));
49+
50+
PAGES = Collections.unmodifiableList(pages);
4951
}
5052

5153
@Test

0 commit comments

Comments
 (0)