Michael Bowerman 6 Years Ago Hi Daniel,Thanks for sharing your knowledge about how to do some pre-upgrade cleaning to make the upgrade process go more smoothly! One way to improve your script might be to improve the memory usage of it. I noticed you had this line in your script:layouts = LayoutLocalServiceUtil.getLayouts(-1,-1)As you know, this API will fetch every layout in the database into one giant list. Since this has the potential to consume a very large amount of memory (for instance, in your example, the list would be over 2 million entries long), this could be a hindrance to performance since there is less available space for everything else and the garbage collector will need to be run more frequently. It also takes a long time just to generate that list.Liferay actually has an API called ActionableDynamicQuery which you can use for situations like these. By using ActionableDynamicQuery, you can fetch and process objects in smaller batches, substantially improving performance. To use the ActionableDynamicQuery method, you would first create a new ActionableDynamicQuery object corresponding to the type of model you want to process. In this case, the type of model you are processing is Layout, so you would create a LayoutActionableDynamicQuery object (in the com.liferay.portal.service.persistence package). When you create the ActionableDynamicQuery object, you can override the default methods from the BaseActionableDynamicQuery class. The two methods you'll probably be interested in overriding are:1. public void addCriteria(DynamicQuery dynamicQuery)- What the addCriteria method does is it allows you to filter down criteria for objects when fetching them. If you do not implement this method, then all objects of that model type in the database will be fetched. If you do implement the method, you can use it to specify criteria by which objects will be fetched (for instance, in this case, we would want to use this method to only fetch Layouts whose friendlyURL is "/friends"). You will be using hibernate's criteria API when you implement this methodAND2. protected void performAction(Object object)- The performAction method specifies the action to be performed on each object that is fetched. In this case, the object we have fetched is a layout, and the action we will want to perform on it is to delete that layout.Once you have created the ActionableDynamicQuery object that implements these two methods (or at least implements the performAction method), then you can simply invoke actionableDynamicQuery.performActions to fetch the entries and invoke the action on them.The advantage of ActionableDynamicQuery is that it only fetches in batches, using a pre-set size (by default, 10000). Once it finishes processing the first batch, it then fetches the second batch to be processed, and so on and so forth, until it has processed every batch. In comparison to fetching every object into a single list, the performance improvement can actually be quite large depending on how many objects you are fetching.I've created an example script based on your script: https://gist.github.com/mbowerman/709286728084081ade7d2afbc72f1bb1. Please feel free to take a look at it ; hopefully it's clear how it all works.You can do a lot more complicated things with the addCriteria method than the example in that script. If you call dynamicQuery.add on other criteria, it functions as an AND (so each condition must be true for the object to be fetched). You can utilize ORs by using the Disjunction object. You can utilize other condition types besides equal (for instance, less than, greater than, not equal to, or is a member of a list). You can even add subqueries! It's a bit difficult for me to explain in words how to do all these things, but please let me know if you'd like an example of any of them.I also found this documentation which has more information on using ActionableDynamicQuery (although it is for 7.0, so some of the information may not apply the same to 6.2): https://dev.liferay.com/es/develop/tutorials/-/knowledge_base/7-0/dynamic-query Please sign in to reply. Reply as... Cancel Daniel Tyger Michael Bowerman 6 Years Ago Thank you very much, Michael! I had been wanting to explore DynamicQuery and your guidance and example are very valuable tips for improving this and many other scripts. The time you took to do this means a lot to me and I am most grateful. You certainly made my day. -daniel Please sign in to reply. Reply as... Cancel
Daniel Tyger Michael Bowerman 6 Years Ago Thank you very much, Michael! I had been wanting to explore DynamicQuery and your guidance and example are very valuable tips for improving this and many other scripts. The time you took to do this means a lot to me and I am most grateful. You certainly made my day. -daniel Please sign in to reply. Reply as... Cancel