private S putAndGetOld(K key, int keyGroupIndex, N namespace, S value) { checkKeyNamespacePreconditions(key, namespace); Map<N, Map<K, S>> namespaceMap = getMapForKeyGroup(keyGroupIndex); if (namespaceMap == null) { namespaceMap = new HashMap<>(); setMapForKeyGroup(keyGroupIndex, namespaceMap); } Map<K, S> keyedMap = namespaceMap.computeIfAbsent(namespace, k -> new HashMap<>()); return keyedMap.put(key, value); }
/** * Sets the given map for the given key-group. */ private void setMapForKeyGroup(int keyGroupId, Map<N, Map<K, S>> map) { try { state[indexToOffset(keyGroupId)] = map; } catch (ArrayIndexOutOfBoundsException e) { throw new IllegalArgumentException("Key group index " + keyGroupId + " is out of range of key group " + "range [" + keyGroupOffset + ", " + (keyGroupOffset + state.length) + ")."); } }
@Override public void put(K key, int keyGroupIndex, N namespace, S value) { putAndGetOld(key, keyGroupIndex, namespace, value); }
S get(K key, int keyGroupIndex, N namespace) { checkKeyNamespacePreconditions(key, namespace); Map<N, Map<K, S>> namespaceMap = getMapForKeyGroup(keyGroupIndex); if (namespaceMap == null) { return null; } Map<K, S> keyedMap = namespaceMap.get(namespace); if (keyedMap == null) { return null; } return keyedMap.get(key); }
@Override public S get(K key, N namespace) { int keyGroup = KeyGroupRangeAssignment.assignToKeyGroup(key, keyContext.getNumberOfKeyGroups()); return get(key, keyGroup, namespace); }
private void remove(K key, int keyGroupIndex, N namespace) { removeAndGetOld(key, keyGroupIndex, namespace); }
@Override public void remove(N namespace) { remove(keyContext.getCurrentKey(), keyContext.getCurrentKeyGroupIndex(), namespace); }
@Override public boolean containsKey(N namespace) { return containsKey(keyContext.getCurrentKey(), keyContext.getCurrentKeyGroupIndex(), namespace); }
@Override public void put(N namespace, S state) { put(keyContext.getCurrentKey(), keyContext.getCurrentKeyGroupIndex(), namespace, state); }
/** * Implementation note: we currently chose the same format between {@link NestedMapsStateTable} and * {@link CopyOnWriteStateTable}. * * <p>{@link NestedMapsStateTable} could naturally support a kind of * prefix-compressed format (grouping by namespace, writing the namespace only once per group instead for each * mapping). We might implement support for different formats later (tailored towards different state table * implementations). */ @Override public void writeStateInKeyGroup(@Nonnull DataOutputView dov, int keyGroupId) throws IOException { final Map<N, Map<K, S>> keyGroupMap = owningStateTable.getMapForKeyGroup(keyGroupId); if (null != keyGroupMap) { Map<N, Map<K, S>> filteredMappings = filterMappingsInKeyGroupIfNeeded(keyGroupMap); dov.writeInt(countMappingsInKeyGroup(filteredMappings)); for (Map.Entry<N, Map<K, S>> namespaceEntry : filteredMappings.entrySet()) { final N namespace = namespaceEntry.getKey(); final Map<K, S> namespaceMap = namespaceEntry.getValue(); for (Map.Entry<K, S> keyEntry : namespaceMap.entrySet()) { writeElement(namespace, keyEntry, dov); } } } else { dov.writeInt(0); } }
private boolean containsKey(K key, int keyGroupIndex, N namespace) { checkKeyNamespacePreconditions(key, namespace); Map<N, Map<K, S>> namespaceMap = getMapForKeyGroup(keyGroupIndex); if (namespaceMap == null) { return false; } Map<K, S> keyedMap = namespaceMap.get(namespace); return keyedMap != null && keyedMap.containsKey(key); }
@Override public S get(N namespace) { return get(keyContext.getCurrentKey(), keyContext.getCurrentKeyGroupIndex(), namespace); }
private void remove(K key, int keyGroupIndex, N namespace) { removeAndGetOld(key, keyGroupIndex, namespace); }
@Override public void remove(N namespace) { remove(keyContext.getCurrentKey(), keyContext.getCurrentKeyGroupIndex(), namespace); }
@Override public boolean containsKey(N namespace) { return containsKey(keyContext.getCurrentKey(), keyContext.getCurrentKeyGroupIndex(), namespace); }
@Override public void put(N namespace, S state) { put(keyContext.getCurrentKey(), keyContext.getCurrentKeyGroupIndex(), namespace, state); }
/** * Implementation note: we currently chose the same format between {@link NestedMapsStateTable} and * {@link CopyOnWriteStateTable}. * * <p>{@link NestedMapsStateTable} could naturally support a kind of * prefix-compressed format (grouping by namespace, writing the namespace only once per group instead for each * mapping). We might implement support for different formats later (tailored towards different state table * implementations). */ @Override public void writeStateInKeyGroup(@Nonnull DataOutputView dov, int keyGroupId) throws IOException { final Map<N, Map<K, S>> keyGroupMap = owningStateTable.getMapForKeyGroup(keyGroupId); if (null != keyGroupMap) { Map<N, Map<K, S>> filteredMappings = filterMappingsInKeyGroupIfNeeded(keyGroupMap); dov.writeInt(countMappingsInKeyGroup(filteredMappings)); for (Map.Entry<N, Map<K, S>> namespaceEntry : filteredMappings.entrySet()) { final N namespace = namespaceEntry.getKey(); final Map<K, S> namespaceMap = namespaceEntry.getValue(); for (Map.Entry<K, S> keyEntry : namespaceMap.entrySet()) { writeElement(namespace, keyEntry, dov); } } } else { dov.writeInt(0); } }
private S putAndGetOld(K key, int keyGroupIndex, N namespace, S value) { checkKeyNamespacePreconditions(key, namespace); Map<N, Map<K, S>> namespaceMap = getMapForKeyGroup(keyGroupIndex); if (namespaceMap == null) { namespaceMap = new HashMap<>(); setMapForKeyGroup(keyGroupIndex, namespaceMap); } Map<K, S> keyedMap = namespaceMap.computeIfAbsent(namespace, k -> new HashMap<>()); return keyedMap.put(key, value); }
private boolean containsKey(K key, int keyGroupIndex, N namespace) { checkKeyNamespacePreconditions(key, namespace); Map<N, Map<K, S>> namespaceMap = getMapForKeyGroup(keyGroupIndex); if (namespaceMap == null) { return false; } Map<K, S> keyedMap = namespaceMap.get(namespace); return keyedMap != null && keyedMap.containsKey(key); }
/** * Sets the given map for the given key-group. */ private void setMapForKeyGroup(int keyGroupId, Map<N, Map<K, S>> map) { try { state[indexToOffset(keyGroupId)] = map; } catch (ArrayIndexOutOfBoundsException e) { throw new IllegalArgumentException("Key group index " + keyGroupId + " is out of range of key group " + "range [" + keyGroupOffset + ", " + (keyGroupOffset + state.length) + ")."); } }