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); }*/ }
/** * 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(); } }
/** * 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); }
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()); }
/** * Refresh the state of the instance from the database, overwriting changes * made to the entity, if any. */ public void refresh(Object entity) { if (contains(entity)) { MainCache mainCache = (MainCache) getPersistenceCache().getMainCache(); Node node = mainCache.getNodeFromCache(entity, getMetadata(entity.getClass()), this); // Locking as it might read from persistence context. try { lock.readLock().lock(); node.setPersistenceDelegator(this); node.refresh(); } finally { lock.readLock().unlock(); } } else { throw new IllegalArgumentException("This is not a valid or managed entity, can't be refreshed"); } }
private NodeState getChildNodeState(EntityMetadata metadata, Object childObj) { Object childId = PropertyAccessorHelper.getId(childObj, metadata); String childNodeId = ObjectGraphUtils.getNodeId(childId, childObj.getClass()); Node childNodeInCache = persistenceCache.getMainCache().getNodeFromCache(childNodeId, pd); return childNodeInCache != null ? childNodeInCache.getCurrentNodeState() : new TransientState(); }
@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); // } }
@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); }
/** * @param entity * @param pd * @param entityId */ public static void addEntityToPersistenceCache(Object entity, PersistenceDelegator pd, Object entityId) { MainCache mainCache = (MainCache) pd.getPersistenceCache().getMainCache(); String nodeId = ObjectGraphUtils.getNodeId(entityId, entity.getClass()); Node node = new Node(nodeId, entity.getClass(), new ManagedState(), pd.getPersistenceCache(), entityId, pd); node.setData(entity); node.setPersistenceDelegator(pd); mainCache.addNodeToCache(node); }
/** * */ 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 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()); }
@Override public void handleFlush(NodeStateContext nodeStateContext) { // Entity state to remain as Removed // Flush this node to database Client client = nodeStateContext.getClient(); Node node = (Node) nodeStateContext; Object entityId = node.getEntityId(); client.remove(node.getData(), entityId); // Since node is flushed, mark it as NOT dirty nodeStateContext.setDirty(false); // Remove this node from Persistence Cache nodeStateContext.getPersistenceCache().getMainCache().removeNodeFromCache(node); }
node.getPersistenceCache().getMainCache().addHeadNode(node);
@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); }
@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()); }
@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()); }
@Test public void test_1_1_1_M() { FlushManager flushManager = new FlushManager(); PhotographerUni_1_1_1_M a = new PhotographerUni_1_1_1_M(); a.setPhotographerId(1); AlbumUni_1_1_1_M b = new AlbumUni_1_1_1_M(); b.setAlbumId("b1"); PhotoUni_1_1_1_M c1 = new PhotoUni_1_1_1_M(); c1.setPhotoId("c1"); PhotoUni_1_1_1_M c2 = new PhotoUni_1_1_1_M(); c2.setPhotoId("c2"); PhotoUni_1_1_1_M c3 = new PhotoUni_1_1_1_M(); c3.setPhotoId("c3"); a.setAlbum(b); b.addPhoto(c1); b.addPhoto(c2); b.addPhoto(c3); ObjectGraph graph = graphBuilder.getObjectGraph(a, null); pc.getMainCache().addGraphToCache(graph, pc); markAllNodeAsDirty(); flushManager.buildFlushStack(graph.getHeadNode(), EventType.INSERT); Deque<Node> fs = flushManager.getFlushStack(); Assert.assertEquals(5, fs.size()); }
@Test public void test_1_M_M_M() { FlushManager flushManager = new FlushManager(); PhotographerUni_1_M_M_M a = new PhotographerUni_1_M_M_M(); a.setPhotographerId(1); AlbumUni_1_M_M_M b1 = new AlbumUni_1_M_M_M(); b1.setAlbumId("b1"); AlbumUni_1_M_M_M b2 = new AlbumUni_1_M_M_M(); b2.setAlbumId("b2"); PhotoUni_1_M_M_M c1 = new PhotoUni_1_M_M_M(); c1.setPhotoId("c1"); PhotoUni_1_M_M_M c2 = new PhotoUni_1_M_M_M(); c2.setPhotoId("c2"); PhotoUni_1_M_M_M c3 = new PhotoUni_1_M_M_M(); c3.setPhotoId("c3"); b1.addPhoto(c1); b1.addPhoto(c2); b2.addPhoto(c2); b2.addPhoto(c3); a.addAlbum(b1); a.addAlbum(b2); ObjectGraph graph = graphBuilder.getObjectGraph(a, null); pc.getMainCache().addGraphToCache(graph, pc); markAllNodeAsDirty(); flushManager.buildFlushStack(graph.getHeadNode(), EventType.INSERT); Deque<Node> fs = flushManager.getFlushStack(); Assert.assertEquals(3, fs.size()); }
pc.getMainCache().addGraphToCache(graph, pc);