private static int weighField(Object field) { if (field instanceof List) { int size = SIZE_OF_ARRAY_OVERHEAD; for (Object listItem : (List) field) { size += weighField(listItem); } return size; } if (field instanceof String) { return ((String) field).getBytes(Charset.defaultCharset()).length; } else if (field instanceof Boolean) { return SIZE_OF_BOOLEAN; } else if (field instanceof BigDecimal) { return SIZE_OF_BIG_DECIMAL; } else if (field instanceof CacheReference) { return SIZE_OF_CACHE_REFERENCE_OVERHEAD + ((CacheReference) field).key().getBytes(Charset.defaultCharset()).length; } else if (field == null) { return SIZE_OF_NULL; } throw new IllegalStateException("Unknown field type in Record. " + field.getClass().getName()); }
@SuppressWarnings("unchecked") private List valueForList(List values) { if (values == null) { return null; } List result = new ArrayList(); for (Object value : values) { if (value instanceof CacheReference) { CacheReference reference = (CacheReference) value; Record referencedRecord = readableCache.read(reference.key(), cacheHeaders); if (referencedRecord == null) { // we are unable to find record in the cache by reference, // means it was removed intentionally by using imperative store API or // evicted from LRU cache, we must prevent of further resolving cache response as it's broken throw new IllegalStateException("Cache MISS: failed to find record in cache by reference"); } result.add(referencedRecord); } else if (value instanceof List) { result.add(valueForList((List) value)); } else { result.add(value); } } return result; }
private Record valueForObject(Record record, ResponseField field) { CacheReference cacheReference; CacheKey fieldCacheKey = cacheKeyResolver.fromFieldArguments(field, variables); if (!fieldCacheKey.equals(CacheKey.NO_KEY)) { cacheReference = new CacheReference(fieldCacheKey.key()); } else { cacheReference = fieldValue(record, field); } if (cacheReference != null) { Record referencedRecord = readableCache.read(cacheReference.key(), cacheHeaders); if (referencedRecord == null) { // we are unable to find record in the cache by reference, // means it was removed intentionally by using imperative store API or // evicted from LRU cache, we must prevent of further resolving cache response as it's broken throw new IllegalStateException("Cache MISS: failed to find record in cache by reference"); } return referencedRecord; } return null; }
@Override public boolean remove(@NotNull final CacheKey cacheKey, final boolean cascade) { checkNotNull(cacheKey, "cacheKey == null"); boolean result; result = nextCache().map(new Function<NormalizedCache, Boolean>() { @NotNull @Override public Boolean apply(@NotNull NormalizedCache cache) { return cache.remove(cacheKey, cascade); } }).or(Boolean.FALSE); Record record = lruCache.getIfPresent(cacheKey.key()); if (record != null) { lruCache.invalidate(cacheKey.key()); result = true; if (cascade) { for (CacheReference cacheReference : record.referencedFields()) { result = result & remove(CacheKey.from(cacheReference.key()), true); } } } return result; }
@Override public boolean remove(@NotNull final CacheKey cacheKey, final boolean cascade) { checkNotNull(cacheKey, "cacheKey == null"); boolean result = nextCache().map(new Function<NormalizedCache, Boolean>() { @NotNull @Override public Boolean apply(@NotNull NormalizedCache cache) { return cache.remove(cacheKey, cascade); } }).or(Boolean.FALSE); RecordJournal recordJournal = lruCache.getIfPresent(cacheKey.key()); if (recordJournal != null) { lruCache.invalidate(cacheKey.key()); result = true; if (cascade) { for (CacheReference cacheReference : recordJournal.snapshot.referencedFields()) { result = result & remove(CacheKey.from(cacheReference.key()), true); } } } return result; }
private static int weighField(Object field) { if (field instanceof List) { int size = SIZE_OF_ARRAY_OVERHEAD; for (Object listItem : (List) field) { size += weighField(listItem); } return size; } if (field instanceof String) { return ((String) field).getBytes().length; } else if (field instanceof Boolean) { return SIZE_OF_BOOLEAN; } else if (field instanceof BigDecimal) { return SIZE_OF_BIG_DECIMAL; } else if (field instanceof CacheReference) { return SIZE_OF_CACHE_REFERENCE_OVERHEAD + ((CacheReference) field).key().getBytes().length; } else if (field == null) { return SIZE_OF_NULL; } throw new IllegalStateException("Unknown field type in Record. " + field.getClass().getName()); }
private static int weighField(Object field) { if (field instanceof List) { int size = SIZE_OF_ARRAY_OVERHEAD; for (Object listItem : (List) field) { size += weighField(listItem); } return size; } if (field instanceof String) { return ((String) field).getBytes().length; } else if (field instanceof Boolean) { return SIZE_OF_BOOLEAN; } else if (field instanceof BigDecimal) { return SIZE_OF_BIG_DECIMAL; } else if (field instanceof CacheReference) { return SIZE_OF_CACHE_REFERENCE_OVERHEAD + ((CacheReference) field).key().getBytes().length; } else if (field == null) { return SIZE_OF_NULL; } throw new IllegalStateException("Unknown field type in Record. " + field.getClass().getName()); }
@SuppressWarnings("unchecked") private List valueForList(List values) { if (values == null) { return null; } List result = new ArrayList(); for (Object value : values) { if (value instanceof CacheReference) { CacheReference reference = (CacheReference) value; Record referencedRecord = readableCache.read(reference.key(), cacheHeaders); if (referencedRecord == null) { // we are unable to find record in the cache by reference, // means it was removed intentionally by using imperative store API or // evicted from LRU cache, we must prevent of further resolving cache response as it's broken throw new IllegalStateException("Cache MISS: failed to find record in cache by reference"); } result.add(referencedRecord); } else if (value instanceof List) { result.add(valueForList((List) value)); } else { result.add(value); } } return result; }
@SuppressWarnings("unchecked") private List valueForList(List values) { if (values == null) { return null; } List result = new ArrayList(); for (Object value : values) { if (value instanceof CacheReference) { CacheReference reference = (CacheReference) value; Record referencedRecord = readableCache.read(reference.key(), cacheHeaders); if (referencedRecord == null) { // we are unable to find record in the cache by reference, // means it was removed intentionally by using imperative store API or // evicted from LRU cache, we must prevent of further resolving cache response as it's broken throw new IllegalStateException("Cache MISS: failed to find record in cache by reference"); } result.add(referencedRecord); } else if (value instanceof List) { result.add(valueForList((List) value)); } else { result.add(value); } } return result; }
private Record valueForObject(Record record, ResponseField field) { CacheReference cacheReference; CacheKey fieldCacheKey = cacheKeyResolver.fromFieldArguments(field, variables); if (fieldCacheKey != CacheKey.NO_KEY) { cacheReference = new CacheReference(fieldCacheKey.key()); } else { cacheReference = fieldValue(record, field); } if (cacheReference != null) { Record referencedRecord = readableCache.read(cacheReference.key(), cacheHeaders); if (referencedRecord == null) { // we are unable to find record in the cache by reference, // means it was removed intentionally by using imperative store API or // evicted from LRU cache, we must prevent of further resolving cache response as it's broken throw new IllegalStateException("Cache MISS: failed to find record in cache by reference"); } return referencedRecord; } return null; }
private Record valueForObject(Record record, ResponseField field) { CacheReference cacheReference; CacheKey fieldCacheKey = cacheKeyResolver.fromFieldArguments(field, variables); if (fieldCacheKey != CacheKey.NO_KEY) { cacheReference = new CacheReference(fieldCacheKey.key()); } else { cacheReference = fieldValue(record, field); } if (cacheReference != null) { Record referencedRecord = readableCache.read(cacheReference.key(), cacheHeaders); if (referencedRecord == null) { // we are unable to find record in the cache by reference, // means it was removed intentionally by using imperative store API or // evicted from LRU cache, we must prevent of further resolving cache response as it's broken throw new IllegalStateException("Cache MISS: failed to find record in cache by reference"); } return referencedRecord; } return null; }