Skip to content

Commit

Permalink
MCPODS-6091_Add Caching Changes to Maintenance.
Browse files Browse the repository at this point in the history
  • Loading branch information
rlakde committed Aug 4, 2023
1 parent 4a07754 commit 5cc64ee
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.jetbrains.annotations.ApiStatus.AvailableSince;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -255,6 +257,26 @@ public long entries() {
return this.size;
}

/**
* Returns the all entries in our FibSet.
*
* @return the set of Entries.
*/
@AvailableSince(NakshaVersion.v2_0_5)
public Set<ENTRY> getAll() {
Set<ENTRY> allValues = new HashSet<>();
List<@NotNull FibLinearProbeTable<KEY, ENTRY>> lpts = getAllLPTs();
for (FibLinearProbeTable<KEY, ENTRY> lpt : lpts) {
Object[] entries = lpt.entries;
for (int i = 0; i < entries.length; i += 2) {
if (entries[i] instanceof FibEntry) {
allValues.add((ENTRY) entries[i]);
}
}
}
return allValues;
}

/**
* Returns a mutable or read-only root.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import com.here.naksha.lib.core.models.TxSignalSet;
import com.here.naksha.lib.core.storage.*;
import com.here.naksha.lib.core.util.fib.FibSet;

import java.lang.ref.WeakReference;
import java.util.List;
import org.jetbrains.annotations.NotNull;

Expand All @@ -36,6 +38,19 @@ public HeapCache(@NotNull HeapCacheConfig config) {
protected final @NotNull HeapCacheConfig config;
protected final @NotNull FibSet<String, CacheEntry> cache = new FibSet<>(CacheEntry::new);

static void gc(@NotNull WeakReference<?> ref) {
System.gc();
while (ref.get() != null) {
Thread.yield();
System.gc();
}
}

//For Cache Eviction
/*public void setWeakReference(@NotNull Object){
gc(new WeakReference<>(new Object()));
}*/

@Override
public void init() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.here.naksha.lib.core.storage.IResultSet;
import java.util.ArrayList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class HeapFeatureReader<F extends XyzFeature> implements IFeatureReader<F> {

Expand All @@ -49,11 +50,25 @@ public class HeapFeatureReader<F extends XyzFeature> implements IFeatureReader<F
return new CacheResultSet<>(featureClass, features);
}

@Override
public @Nullable F getFeatureById(@NotNull String id) {
final CacheEntry entry = cache.cache.get(id);
F feature = null;
if (entry != null && featureClass.isInstance(entry.getValue())) {
feature = featureClass.cast(entry.getValue());
}
return feature;
}

@Override
public @NotNull IResultSet<F> getAll(int skip, int limit) {
// TODO: Implement me!
// Note: The FibSet does currently miss a method to iterate entries!
// I will add it when I'am back from vacation, except you want to try.
throw new UnsupportedOperationException();
final ArrayList<F> features = new ArrayList<>();
final ArrayList<CacheEntry> entries = new ArrayList<>(cache.cache.getAll());
for (final CacheEntry entry : entries) {
if (entry != null && featureClass.isInstance(entry.getValue())) {
features.add(featureClass.cast(entry.getValue()));
}
}
return new CacheResultSet<>(featureClass, features);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
package com.here.naksha.lib.heapcache;

import com.here.naksha.lib.core.models.geojson.implementation.XyzFeature;
import com.here.naksha.lib.core.storage.CollectionInfo;
import com.here.naksha.lib.core.storage.IFeatureWriter;
import com.here.naksha.lib.core.storage.ModifyFeaturesReq;
import com.here.naksha.lib.core.storage.ModifyFeaturesResp;
import com.here.naksha.lib.core.storage.*;
import org.jetbrains.annotations.NotNull;

public class HeapFeatureWriter<F extends XyzFeature> extends HeapFeatureReader<F> implements IFeatureWriter<F> {
Expand All @@ -37,6 +34,19 @@ public class HeapFeatureWriter<F extends XyzFeature> extends HeapFeatureReader<F
final CacheEntry entry = cache.cache.putWeak(feature.getId());
entry.setValue(feature);
}
for (final F feature : req.update()) {
final CacheEntry entry = cache.cache.putWeak(feature.getId());
entry.setValue(feature);
}
for (final F feature : req.upsert()) {
final CacheEntry entry = cache.cache.putWeak(feature.getId());
entry.setValue(feature);
}
for (final @NotNull DeleteOp feature : req.delete()) {
if (cache.cache.get(feature.id()) != null) {
cache.cache.remove(feature.id());
}
}
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,49 @@ static void gc(@NotNull WeakReference<?> ref) {
}

@Test
void basics() {
void CacheSoftReferenceTest() {
final HeapCache cache = new HeapCache(new HeapCacheConfig(null));
try (final IMasterTransaction tx = cache.openMasterTransaction(cache.createSettings())) {
tx.writeFeatures(XyzFeature.class, new CollectionInfo("foo"))
.modifyFeatures(new ModifyFeaturesReq<>().insert(new XyzFeature("x")));
.modifyFeatures(new ModifyFeaturesReq<>().insert(new XyzFeature("x")));
tx.commit();

XyzFeature feature =
tx.readFeatures(XyzFeature.class, new CollectionInfo("foo")).getFeatureById("x");
tx.readFeatures(XyzFeature.class, new CollectionInfo("foo")).getFeatureById("x");
assertNotNull(feature);
assertEquals("x", feature.getId());
}
}

@Test
void CacheWeakReferenceTest() {
final HeapCache cache = new HeapCache(new HeapCacheConfig(null));
try (final IMasterTransaction tx = cache.openMasterTransaction(cache.createSettings())) {
tx.writeFeatures(XyzFeature.class, new CollectionInfo("foo"))
.modifyFeatures(new ModifyFeaturesReq<>().insert(new XyzFeature("x")));
tx.commit();

gc(new WeakReference<>(new Object()));

feature =
tx.readFeatures(XyzFeature.class, new CollectionInfo("foo")).getFeatureById("x");
XyzFeature feature =
tx.readFeatures(XyzFeature.class, new CollectionInfo("foo")).getFeatureById("x");
assertNull(feature);
}
}

@Test
void cacheGetFeaturesByIdTest() {
final HeapCache cache = new HeapCache(new HeapCacheConfig(null));
try (final IMasterTransaction tx = cache.openMasterTransaction(cache.createSettings())) {
tx.writeFeatures(XyzFeature.class, new CollectionInfo("bar"))
.modifyFeatures(new ModifyFeaturesReq<>().insert(new XyzFeature("r")));
tx.commit();

XyzFeature feature = tx.readFeatures(XyzFeature.class, new CollectionInfo("bar"))
.getFeaturesById("r")
.getFeature();
assertNotNull(feature);
assertEquals("r", feature.getId());
}
}
}

0 comments on commit 5cc64ee

Please sign in to comment.