Lines 531-540
public abstract class IndexWriter
Link Here
|
531 |
|
531 |
|
532 |
public static class IntIndexCollector extends IntIndex<ArrayIntCompressed> implements IOne2OneIndex |
532 |
public static class IntIndexCollector extends IntIndex<ArrayIntCompressed> implements IOne2OneIndex |
533 |
{ |
533 |
{ |
|
|
534 |
static class IndexedThreadLocalCache<T> |
535 |
{ |
536 |
final ThreadLocal<Integer> pageNumber = ThreadLocal.withInitial(() -> -1); |
537 |
final ThreadLocal<SoftReference<T>> page = ThreadLocal.withInitial(() -> null); |
538 |
|
539 |
void set(int pageNumber, T page) |
540 |
{ |
541 |
this.pageNumber.set(pageNumber); |
542 |
this.page.set(new SoftReference(page)); |
543 |
} |
544 |
|
545 |
T get(int pageNumber) |
546 |
{ |
547 |
return (this.pageNumber.get() == pageNumber) ? this.page.get().get() : null; |
548 |
} |
549 |
} |
550 |
|
534 |
final int mostSignificantBit; |
551 |
final int mostSignificantBit; |
535 |
final int size; |
552 |
final int size; |
536 |
final int pageSize = IndexWriter.PAGE_SIZE_INT; |
553 |
final int pageSize = IndexWriter.PAGE_SIZE_INT; |
537 |
final ConcurrentHashMap<Integer, ArrayIntCompressed> pages = new ConcurrentHashMap<Integer, ArrayIntCompressed>(); |
554 |
final ConcurrentHashMap<Integer, ArrayIntCompressed> pages = new ConcurrentHashMap<Integer, ArrayIntCompressed>(); |
|
|
555 |
final IndexedThreadLocalCache<ArrayIntCompressed> localPageCache = new IndexedThreadLocalCache<>(); |
538 |
|
556 |
|
539 |
public IntIndexCollector(int size, int mostSignificantBit) |
557 |
public IntIndexCollector(int size, int mostSignificantBit) |
540 |
{ |
558 |
{ |
Lines 544-556
public abstract class IndexWriter
Link Here
|
544 |
|
562 |
|
545 |
protected ArrayIntCompressed getPage(int page) |
563 |
protected ArrayIntCompressed getPage(int page) |
546 |
{ |
564 |
{ |
547 |
ArrayIntCompressed existing = pages.get(page); |
565 |
ArrayIntCompressed existing = localPageCache.get(page); |
548 |
if (existing != null) return existing; |
566 |
if (existing != null) |
|
|
567 |
return existing; |
568 |
|
569 |
existing = pages.get(page); |
570 |
if (existing != null) |
571 |
{ |
572 |
localPageCache.set(page, existing); |
573 |
return existing; |
574 |
} |
549 |
|
575 |
|
550 |
int ps = page < (size / pageSize) ? pageSize : size % pageSize; |
576 |
int ps = page < (size / pageSize) ? pageSize : size % pageSize; |
551 |
ArrayIntCompressed newArray = new ArrayIntCompressed(ps, 31 - mostSignificantBit, 0); |
577 |
ArrayIntCompressed newArray = new ArrayIntCompressed(ps, 31 - mostSignificantBit, 0); |
552 |
existing = pages.putIfAbsent(page, newArray); |
578 |
existing = pages.putIfAbsent(page, newArray); |
553 |
return (existing != null) ? existing : newArray; |
579 |
final ArrayIntCompressed result = (existing != null) ? existing : newArray; |
|
|
580 |
localPageCache.set(page, result); |
581 |
return result; |
554 |
} |
582 |
} |
555 |
|
583 |
|
556 |
public IIndexReader.IOne2OneIndex writeTo(File indexFile) throws IOException |
584 |
public IIndexReader.IOne2OneIndex writeTo(File indexFile) throws IOException |