public Node getNodeFromCache(Object entity, EntityMetadata entityMetadata, PersistenceDelegator pd) { if (entity == null) { throw new IllegalArgumentException("Entity is null, can't check whether it's in persistence context"); } Object primaryKey = PropertyAccessorHelper.getId(entity, entityMetadata); if (primaryKey == null) { throw new IllegalArgumentException("Primary key not set into entity"); } String nodeId = ObjectGraphUtils.getNodeId(primaryKey, entity.getClass()); return getNodeFromCache(nodeId, pd); }
private void cleanIndividualCache(CacheBase cache) { for (Node node : cache.getAllNodes()) { node.clear(); } }
public synchronized void removeNodeFromCache(Node node) { if (getHeadNodes().contains(node)) { getHeadNodes().remove(node); } if (nodeMappings.get(node.getNodeId()) != null) { nodeMappings.remove(node.getNodeId()); } evictFroml2Cache(node); logCacheEvent("REMOVED FROM ", node.getNodeId()); node = null; // Eligible for GC }
public void addGraphToCache(ObjectGraph graph, PersistenceCache persistenceCache) { // Add each node in the graph to cache for (String key : graph.getNodeMapping().keySet()) { Node thisNode = graph.getNodeMapping().get(key); addNodeToCache(thisNode); // Remove all those head nodes in persistence cache, that are there // in Graph as a non-head node if (!thisNode.isHeadNode() && persistenceCache.getMainCache().getHeadNodes().contains(thisNode)) { persistenceCache.getMainCache().getHeadNodes().remove(thisNode); } } // Add head Node to list of head nodes addHeadNode(graph.getHeadNode()); }
@Test public void testPersistenceCache() { Store store = new Store(1, "Food Bazaar, Noida"); store.addCounter(new BillingCounter(1, "A")); store.addCounter(new BillingCounter(2, "B")); store.addCounter(new BillingCounter(3, "C")); ObjectGraph graph = graphBuilder.getObjectGraph(store, null); pc.getMainCache().addGraphToCache(graph, pc); Assert.assertNotNull(pc.getMainCache()); Assert.assertEquals(1, pc.getMainCache().getHeadNodes().size()); PersistenceDelegator pd = new PersistenceDelegator(emfImpl.getKunderaMetadataInstance(), pc); Node headNode = pc.getMainCache().getNodeFromCache(ObjectGraphUtils.getNodeId("1", Store.class), pd); Assert.assertNotNull(headNode); Assert.assertNull(headNode.getParents()); Assert.assertEquals(3, headNode.getChildren().size()); Assert.assertEquals(4, pc.getMainCache().size()); }
/** * */ private void markAllNodeAsDirty() { if (pc.getMainCache() != null) { Set<Node> headNodes = pc.getMainCache().getHeadNodes(); if (headNodes != null) { for (Node hn : headNodes) { if (hn != null) hn.setDirty(true); } } Collection<Node> allNodes = pc.getMainCache().getAllNodes(); if (allNodes != null) { for (Node node : allNodes) { if (node != null) node.setDirty(true); } } } }
@Test public void test_1_1_1_1() { FlushManager flushManager = new FlushManager(); PhotographerUni_1_1_1_1 a = new PhotographerUni_1_1_1_1(); a.setPhotographerId(1); AlbumUni_1_1_1_1 b = new AlbumUni_1_1_1_1(); b.setAlbumId("b1"); PhotoUni_1_1_1_1 c = new PhotoUni_1_1_1_1(); c.setPhotoId("c1"); a.setAlbum(b); b.setPhoto(c); ObjectGraph graph = graphBuilder.getObjectGraph(a, null); pc.getMainCache().addGraphToCache(graph, pc); PersistenceDelegator pd = new PersistenceDelegator(emfImpl.getKunderaMetadataInstance(), pc); Node headNode = pc.getMainCache() .getNodeFromCache(ObjectGraphUtils.getNodeId("c1", PhotoUni_1_1_1_1.class), pd); markAllNodeAsDirty(); flushManager.buildFlushStack(graph.getHeadNode(), EventType.INSERT); Deque<Node> fs = flushManager.getFlushStack(); Assert.assertEquals(3, fs.size()); }
@Test public void test_multi_1_1_1_1() { FlushManager flushManager = new FlushManager(); PhotographerBi_1_1_1_1 a = new PhotographerBi_1_1_1_1(); a.setPhotographerId(1); AlbumBi_1_1_1_1 b = new AlbumBi_1_1_1_1(); b.setAlbumId("b1"); PhotoBi_1_1_1_1 c = new PhotoBi_1_1_1_1(); c.setPhotoId("c1"); b.setPhotographer(a); b.setPhoto(c); ObjectGraph graph = graphBuilder.getObjectGraph(b, null); pc.getMainCache().addGraphToCache(graph, pc); markAllNodeAsDirty(); flushManager.buildFlushStack(graph.getHeadNode(), EventType.INSERT); Deque<Node> fs = flushManager.getFlushStack(); Assert.assertEquals(2, fs.size()); }
logCacheEvent("ADDED TO ", node.getNodeId()); logCacheEvent("ADDED TO ", node.getNodeId()); nodeMappings.put(node.getNodeId(), node); node.getPersistenceCache().getMainCache().addHeadNode(node);
@Override public void handleMerge(NodeStateContext nodeStateContext) { // Ignored, entity remains in the same state // Mark this entity for saving in database depending upon whether it's // deep equals to the // one in persistence cache // nodeStateContext.setDirty(true); // if (((Node) nodeStateContext).isDirty() || ((Node) nodeStateContext).isInState(DetachedState.class)) // { ((Node) nodeStateContext).setUpdate(true); // Add this node into persistence cache nodeStateContext.getPersistenceCache().getMainCache().addNodeToCache((Node) nodeStateContext); // Cascade merge operation for all related entities for whom // cascade=ALL // or MERGE // recursivelyPerformOperation(nodeStateContext, OPERATION.MERGE); // } }
/** * Cleaned out the data. * */ public void clean() { // Clear main cache. if (mainCache != null) { mainCache.clear(); } /* if (embeddedCache != null) { embeddedCache.clear(); } if (elementCollectionCache != null) { elementCollectionCache.clear(); } if (transactionalCache != null) { transactionalCache.clear(); } */ }
getPersistenceCache().getMainCache().addHeadNode(node);
@Test public void testFlashStockForStore() { FlushManager flushManager = new FlushManager(); Store store = new Store(1, "Food Bazaar, Noida"); store.addCounter(new BillingCounter(1, "A")); store.addCounter(new BillingCounter(2, "B")); store.addCounter(new BillingCounter(3, "C")); ObjectGraph graph = graphBuilder.getObjectGraph(store, null); pc.getMainCache().addGraphToCache(graph, pc); Assert.assertNotNull(pc.getMainCache()); Assert.assertEquals(1, pc.getMainCache().getHeadNodes().size()); PersistenceDelegator pd = new PersistenceDelegator(emfImpl.getKunderaMetadataInstance(), pc); Node headNode = pc.getMainCache().getNodeFromCache(ObjectGraphUtils.getNodeId("1", Store.class), pd); Assert.assertNotNull(headNode); Assert.assertNull(headNode.getParents()); Assert.assertEquals(3, headNode.getChildren().size()); Assert.assertEquals(4, pc.getMainCache().size()); markAllNodeAsDirty(); flushManager.buildFlushStack(headNode, EventType.INSERT); Deque<Node> fs = flushManager.getFlushStack(); Assert.assertEquals(4, fs.size()); }
ObjectGraph graphb3 = graphBuilder.getObjectGraph(b3, null); pc.getMainCache().addGraphToCache(graph, pc); pc.getMainCache().addGraphToCache(graphb2, pc); pc.getMainCache().addGraphToCache(graphb3, pc);
@Override public void handlePersist(NodeStateContext nodeStateContext) { // Transient ---> Managed moveNodeToNextState(nodeStateContext, new ManagedState()); // Mark this entity for saving in database nodeStateContext.setDirty(true); // Add this node into persistence cache nodeStateContext.getPersistenceCache().getMainCache().addNodeToCache((Node) nodeStateContext); // Recurse persist operation on all managed entities for whom // cascade=ALL or PERSIST // recursivelyPerformOperation(nodeStateContext, OPERATION.PERSIST); }
/** * Remove the given entity from the persistence context, causing a managed * entity to become detached. */ public void detach(Object entity) { Node node = getPersistenceCache().getMainCache().getNodeFromCache(entity, getMetadata(entity.getClass()), this); if (node != null) { node.detach(); } }
public void markAllNodesNotTraversed() { for (Node node : persistenceCache.getMainCache().getAllNodes()) { node.setTraversed(false); } /* for (Node node : persistenceCache.getEmbeddedCache().getAllNodes()) { node.setTraversed(false); } for (Node node : persistenceCache.getElementCollectionCache().getAllNodes()) { node.setTraversed(false); } for (Node node : persistenceCache.getTransactionalCache().getAllNodes()) { node.setTraversed(false); }*/ }
ObjectGraph graph3 = graphBuilder.getObjectGraph(a3, null); pc.getMainCache().addGraphToCache(graph1, pc); pc.getMainCache().addGraphToCache(graph2, pc); markAllNodeAsDirty(); flushManager = new FlushManager(); pc.getMainCache().addGraphToCache(graph3, pc); markAllNodeAsDirty(); flushManager = new FlushManager();
@Override public void handleMerge(NodeStateContext nodeStateContext) { // create a new managed entity and copy state of original entity into // this one. Object copiedNodeData = ObjectUtils.deepCopy(nodeStateContext.getData(), nodeStateContext.getPersistenceDelegator().getKunderaMetadata()); nodeStateContext.setData(copiedNodeData); moveNodeToNextState(nodeStateContext, new ManagedState()); nodeStateContext.getPersistenceCache().getMainCache().addNodeToCache((Node) nodeStateContext); }
/** * Check if the instance is a managed entity instance belonging to the * current persistence context. */ boolean contains(Object entity) { Node node = getPersistenceCache().getMainCache().getNodeFromCache(entity, getMetadata(entity.getClass()), this); return node != null && node.isInState(ManagedState.class); }