001/* 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2026, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v2.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.core.kryo; 015 016import java.util.HashMap; 017import java.util.LinkedHashMap; 018import java.util.Map; 019 020import static ch.qos.logback.core.kryo.KryoCoreConstants.cappedSerializerCapacity; 021 022/** 023 * Value↔id map with access-order LRU eviction at {@code capacity}. 024 * 025 * <p>{@link #getIdByValue} and {@link #getValueById} count as uses, so entries that 026 * keep being looked up stay; the least-recently-used mapping is dropped when a new 027 * one would exceed capacity. Evicted ids are reused for later inserts. {@code idToValue} 028 * is a plain {@link HashMap}; LRU order lives only in {@code valueToId}. 029 */ 030public class BidirectionalObjectToIdMapDefault<V> implements BidirectionalObjectToIdMap<V> { 031 032 static final boolean ACCESS_ORDER = true; 033 034 final int capacity; 035 036 private final Map<Integer, V> idToValue; 037 private final LinkedHashMap<V, Integer> valueToId; 038 private final SerializerIdPool idPool = new DualSerializerIdPool(); 039 040 public BidirectionalObjectToIdMapDefault(int capacity) { 041 this.capacity = cappedSerializerCapacity(capacity); 042 this.idToValue = new HashMap<>(this.capacity); 043 this.valueToId = new LinkedHashMap<>(this.capacity, 0.75f, ACCESS_ORDER) { 044 @Override 045 protected boolean removeEldestEntry(Map.Entry<V, Integer> eldest) { 046 if (size() > BidirectionalObjectToIdMapDefault.this.capacity) { 047 recycle(eldest.getValue()); 048 return true; 049 } 050 return false; 051 } 052 }; 053 } 054 055 @Override 056 public Integer getIdByValue(V value) { 057 return valueToId.get(value); 058 } 059 060 @Override 061 public Integer insertNewEntryOnWrite(V value) { 062 ensureIdAvailable(); 063 Integer boxed = idPool.allocate(); 064 V existingValue = idToValue.put(boxed, value); 065 if (existingValue != null) { 066 valueToId.remove(existingValue); 067 } 068 valueToId.put(value, boxed); 069 return boxed; 070 } 071 072 @Override 073 public V insertNewEntryOnRead(int id, V value) { 074 Integer boxed = BoxedIds.of(id); 075 idPool.unfree(boxed); 076 V existingValue = idToValue.put(boxed, value); 077 if (existingValue != null) { 078 valueToId.remove(existingValue); 079 } 080 Integer previousId = valueToId.put(value, boxed); 081 if (previousId != null && previousId.intValue() != id) { 082 recycle(previousId); 083 } 084 return existingValue; 085 } 086 087 @Override 088 public V getValueById(int id) { 089 V value = idToValue.get(BoxedIds.of(id)); 090 if (value != null) { 091 valueToId.get(value); 092 } 093 return value; 094 } 095 096 /** 097 * Makes a free id available before {@link SerializerIdPool#allocate()} on the write path. 098 * 099 * <p>Ids are assigned <em>before</em> {@code valueToId.put}, so LRU 100 * {@code removeEldestEntry} cannot supply the id for this insert. If the pool has 101 * nothing free and the map is already at {@link #capacity}, this drops the 102 * least-recently-used mapping and {@link #recycle(Integer) recycles} its id. 103 * {@code allocate()} then reuses that id instead of minting {@code nextId}, which 104 * would eventually exceed the 12-bit header. 105 * 106 * <p>No-op when a recycled id is already in the pool, or when the map is below 107 * capacity (a new id can be minted). 108 */ 109 private void ensureIdAvailable() { 110 if (!idPool.hasFree() && valueToId.size() >= capacity && !valueToId.isEmpty()) { 111 Map.Entry<V, Integer> eldest = valueToId.entrySet().iterator().next(); 112 valueToId.remove(eldest.getKey()); 113 recycle(eldest.getValue()); 114 } 115 } 116 117 private void recycle(Integer id) { 118 idToValue.remove(id); 119 idPool.recycle(id); 120 } 121 122 Map<Integer, V> getIdToValueMap() { 123 return idToValue; 124 } 125 126 Map<V, Integer> getValueToIdMap() { 127 return valueToId; 128 } 129}