Skip to content

Commit

Permalink
[POC-4] cache apis
Browse files Browse the repository at this point in the history
  • Loading branch information
apfadler committed Jun 20, 2016
1 parent 4184ec0 commit ef83664
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 141 deletions.
83 changes: 57 additions & 26 deletions common/src/main/java/org/quil/server/Cache.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,82 @@
package org.quil.server;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.*;

import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
import org.apache.ignite.cache.CachePeekMode;
import org.apache.ignite.cache.query.QueryCursor;
import org.apache.ignite.cache.query.SqlFieldsQuery;
import org.apache.ignite.internal.processors.cache.QueryCursorImpl;
import org.apache.ignite.internal.processors.query.GridQueryFieldMetadata;
import org.json.simple.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class Cache {

final Logger logger = LoggerFactory.getLogger(Cache.class);

protected static HashMap<String, Cache> caches = new HashMap<String, Cache>();

final static Logger logger = LoggerFactory.getLogger(Cache.class);

protected String _cacheName;

public String getCacheName() {
return _cacheName;

public static void register(Cache cache) throws Exception {
Ignite ignite = Ignition.ignite();
IgniteCache<String, Cache> managedCaches = ignite.getOrCreateCache("ManagedCaches");

if (managedCaches.containsKey(cache.getCacheName()))
throw new Exception("Cache already exists");

managedCaches.put(cache.getCacheName(), cache);
}

public static HashMap<String, Cache> getAllCaches()
{
return caches;

public static void deregister(Cache cache) {
Ignite ignite = Ignition.ignite();
IgniteCache<String, Cache> managedCaches = ignite.getOrCreateCache("ManagedCaches");

if (managedCaches.containsKey(cache.getCacheName())) {
managedCaches.remove(cache.getCacheName());
}
}

public static Cache findCache(String cacheName)
{
return caches.get(cacheName);

public static Cache findCache(String cacheName) throws Exception {
Ignite ignite = Ignition.ignite();
IgniteCache<String, Cache> managedCaches = ignite.getOrCreateCache("ManagedCaches");

return managedCaches.get(cacheName);
}


public static boolean exists(String cacheName) throws Exception {
Ignite ignite = Ignition.ignite();
IgniteCache<String, Cache> managedCaches = ignite.getOrCreateCache("ManagedCaches");

return managedCaches.containsKey(cacheName);
}

public static HashMap<String, Cache> allCaches() {
Ignite ignite = Ignition.ignite();
IgniteCache<String, Cache> managedCaches = ignite.getOrCreateCache("ManagedCaches");

HashMap<String, Cache> all = new HashMap<String, Cache>();

for (javax.cache.Cache.Entry<String, Cache> entry : managedCaches) {
all.put(entry.getKey(),entry.getValue());
}

return all;
}

public String getCacheName() {

return _cacheName;
}


public synchronized void removeAll()
{
try {
logger.info("Clearing cache " + _cacheName);
Ignite ignite = Ignition.ignite();
ignite.cache(_cacheName).removeAll();
caches.remove(_cacheName);

Iterator<Map.Entry<String,String>> it = ObjectIndex.all().entrySet().iterator();
while(it.hasNext()){
Expand All @@ -55,6 +85,9 @@ public synchronized void removeAll()
ObjectIndex.removeFromIndex(e.getKey(), e.getValue());
}
}

Cache.deregister(this);

}
catch(Exception e) {
logger.error("Failed to clear cache "+_cacheName+": "+e.toString());
Expand All @@ -73,12 +106,10 @@ public int size() {

JSONArray result = new JSONArray();

logger.info("Query: " + query);
logger.debug("Query: " + query);

try (QueryCursor<List<?>> cursor = cache.query(sql)) {

JSONArray jsonHeaderRow = new JSONArray();

return Integer.parseInt(cursor.iterator().next().get(0).toString());
}
}
Expand Down
52 changes: 21 additions & 31 deletions common/src/main/java/org/quil/server/DocumentCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,33 @@ public class DocumentCache extends Cache {

static final Logger logger = LoggerFactory.getLogger(DocumentCache.class);

static public DocumentCache getOrCreate(String cacheName)
{
if (caches.containsKey(cacheName) ) {
static public DocumentCache getOrCreate(String cacheName) throws Exception {

if (Cache.exists(cacheName)) {

Cache cache = caches.get(cacheName);
Cache c = Cache.findCache(cacheName);

if (cache.getClass() == DocumentCache.class)
if (c.getClass() == DocumentCache.class)
{
return (DocumentCache) caches.get(cacheName);
return (DocumentCache)c;
}
else
{
return null;
throw new Exception("Invalid Cache type.");
}
}
else {
DocumentCache cache = new DocumentCache(cacheName);
caches.put(cacheName, cache);

return cache;
}

}

public DocumentCache(IgniteCache<?, ?> cache)
{
_cacheName = cache.getName();
}

public DocumentCache(String cacheName)
{
logger.debug("Creating cache " + cacheName);
Expand All @@ -63,29 +66,16 @@ public DocumentCache(String cacheName)

Ignite ignite = Ignition.ignite();
ignite.getOrCreateCache(cfg);

logger.debug("Cache created");
}

/*public int size()
{
Ignite ignite = Ignition.ignite();
IgniteCache<String, Document> cache = ignite.cache(_cacheName);
int s;
try
{
s = cache.localSize(CachePeekMode.PRIMARY);

try {
Cache.register(this);
} catch (Exception e) {
logger.error("Failed to register Cache");
}
catch (Exception e)
{
System.out.println(e.getMessage());
return 0;
}
return s;
}*/

logger.debug("Cache created");

}

public void put(String key, Document doc)
{
CacheConfiguration<String, Document> cfg = new CacheConfiguration<String, Document>();
Expand Down
13 changes: 1 addition & 12 deletions common/src/main/java/org/quil/server/ObjectIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ public class ObjectIndex {

final static Logger logger = LoggerFactory.getLogger(ObjectIndex.class);

public static ArrayList<String> usedCaches = new ArrayList<String>();

public static void addToIndex(String origKey, String cacheName) {

Ignite ignite = Ignition.ignite();
Expand Down Expand Up @@ -86,7 +84,7 @@ synchronized static public HashMap<String, String> all() {
}


public static void add(String cacheType, String cacheID, String fileID, String filePath) throws IOException {
public static void add(String cacheType, String cacheID, String fileID, String filePath) throws Exception {

FileSystemRepository repo = new FileSystemRepository();

Expand All @@ -95,23 +93,14 @@ public static void add(String cacheType, String cacheID, String fileID, String f
cache.put(fileID, repo.getFile(filePath));

addToIndex(fileID,cacheID);

if (!usedCaches.contains(cacheID)) {
usedCaches.add(cacheID);
}
}

if (cacheType.compareTo("DocumentCache") == 0) {
DocumentCache cache = DocumentCache.getOrCreate(cacheID);
cache.put(fileID, (Document)((new org.quil.JSON.Parser()).parse(repo.getFile(filePath))));

addToIndex(fileID,cacheID);

if (!usedCaches.contains(cacheID)) {
usedCaches.add(cacheID);
}
}

}

}
60 changes: 24 additions & 36 deletions common/src/main/java/org/quil/server/SimpleCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,33 @@
public class SimpleCache extends Cache {

static final Logger logger = LoggerFactory.getLogger(SimpleCache.class);


static public SimpleCache getOrCreate(String cacheName)
{
if (caches.containsKey(cacheName)) {

Cache cache = caches.get(cacheName);

if (cache.getClass() == SimpleCache.class)

static public SimpleCache getOrCreate(String cacheName) throws Exception {
if (Cache.exists(cacheName)) {

Cache c = Cache.findCache(cacheName);

if (c.getClass() == SimpleCache.class)
{
return (SimpleCache) caches.get(cacheName);
}
return (SimpleCache)c;
}
else
{
return null;
throw new Exception("Invalid Cache type.");
}
}
else {
SimpleCache cache = new SimpleCache(cacheName);
caches.put(cacheName, cache);

return cache;
}

}

public SimpleCache(IgniteCache<?, ?> cache)
{
_cacheName = cache.getName();
}

public SimpleCache(String cacheName)
{
logger.debug("Creating cache " + cacheName);
Expand All @@ -63,29 +64,16 @@ public SimpleCache(String cacheName)

Ignite ignite = Ignition.ignite();
ignite.getOrCreateCache(cfg);

logger.debug("Cache created");
}

/*public int size()
{
Ignite ignite = Ignition.ignite();
IgniteCache<?, ?> cache = ignite.cache(_cacheName);
int s;
try
{
s = cache.size(CachePeekMode.PRIMARY);

try {
Cache.register(this);
} catch (Exception e) {
logger.error("Failed to register cache");
}
catch (Exception e)
{
System.out.println(e.getMessage());
return 0;
}
return s;
}*/


logger.debug("Cache created");
}

public void put(String key, String value)
{
CacheConfiguration<String, String> cfg = new CacheConfiguration<String, String>();
Expand Down
Loading

0 comments on commit ef83664

Please sign in to comment.