Search in sources :

Example 1 with Node

use of com.github.drinkjava2.jsqlbox.entitynet.Node in project jSqlBox by drinkjava2.

the class EntityNetQueryTest method testJoinParents.

@Test
public void testJoinParents() {
    System.out.println("==============testJoinParents================ ");
    new User().put("id", "u1").put("userName", "user1").insert();
    new User().put("id", "u2").put("userName", "user2").insert();
    new Email().putFields("id", "emailName", "userId");
    new Email().putValues("e1", "email1", "u1").insert();
    new Email().putValues("e2", "email2", "u1").insert();
    List<Map<String, Object>> listMap = ctx.nQuery(new EntitySqlMapListHandler(new Email().alias("e")), "select e.id as e_id from emailtb e");
    EntityNet net = (EntityNet) ctx.netCreate(listMap);
    Assert.assertEquals(2, net.size());
    Node emailNode = net.getOneNode(Email.class, "e1");
    // e1 have no userId
    Assert.assertNull(emailNode.getParentRelations());
    // field
    List<Map<String, Object>> listMap2 = ctx.nQuery(new EntitySqlMapListHandler(Email.class), "select e.** from emailtb e");
    ctx.netJoinList(net, listMap2);
    Assert.assertEquals(2, net.size());
    emailNode = net.getOneNode(Email.class, "e1");
    Assert.assertNotNull(emailNode.getParentRelations());
}
Also used : User(com.github.drinkjava2.functionstest.entitynet.entities.User) Email(com.github.drinkjava2.functionstest.entitynet.entities.Email) EntityNet(com.github.drinkjava2.jsqlbox.entitynet.EntityNet) Node(com.github.drinkjava2.jsqlbox.entitynet.Node) Map(java.util.Map) EntitySqlMapListHandler(com.github.drinkjava2.jsqlbox.handler.EntitySqlMapListHandler) Test(org.junit.Test)

Example 2 with Node

use of com.github.drinkjava2.jsqlbox.entitynet.Node in project jSqlBox by drinkjava2.

the class EntityNetQueryTest method testFindParent.

@Test
public void testFindParent() {
    System.out.println("==============testFindParent================ ");
    int sampleSize = 20;
    int queyrTimes = 10;
    for (int i = 0; i < sampleSize; i++) {
        new User().put("id", "usr" + i).put("userName", "user" + i).insert();
        for (int j = 0; j < sampleSize; j++) new Email().put("id", "email" + i + "_" + j, "userId", "usr" + i).insert();
    }
    EntityNet net = ctx.netLoad(new User(), Email.class);
    Map<Class<?>, Set<Node>> result = null;
    long start = System.currentTimeMillis();
    start = System.currentTimeMillis();
    for (int i = 0; i < queyrTimes; i++) {
        result = net.findNodeMapByEntities(new Path("S+", Email.class).nextPath("P+", User.class, "userId"));
    }
    printTimeUsed(start, "Find parent (no cache)");
    System.out.println("user selected2:" + result.get(User.class).size());
    System.out.println("email selected2:" + result.get(Email.class).size());
}
Also used : Path(com.github.drinkjava2.jsqlbox.entitynet.Path) User(com.github.drinkjava2.functionstest.entitynet.entities.User) Email(com.github.drinkjava2.functionstest.entitynet.entities.Email) Set(java.util.Set) EntityNet(com.github.drinkjava2.jsqlbox.entitynet.EntityNet) Test(org.junit.Test)

Example 3 with Node

use of com.github.drinkjava2.jsqlbox.entitynet.Node in project jSqlBox by drinkjava2.

the class EntityNetTreeTest method testSearchTreeChild.

@Test
public void testSearchTreeChild() {
    EntityNet net = ctx.netLoad(TreeNode.class);
    Set<TreeNode> TreeNodes = net.findEntitySet(TreeNode.class, new Path("S+", TreeNode.class).where("id=? or id=?", "B", "D").nextPath("C*", TreeNode.class, "pid"));
    for (TreeNode node : TreeNodes) System.out.print(node.getId() + " ");
    Assert.assertEquals(9, TreeNodes.size());
}
Also used : Path(com.github.drinkjava2.jsqlbox.entitynet.Path) TreeNode(com.github.drinkjava2.functionstest.entitynet.entities.TreeNode) EntityNet(com.github.drinkjava2.jsqlbox.entitynet.EntityNet) Test(org.junit.Test)

Example 4 with Node

use of com.github.drinkjava2.jsqlbox.entitynet.Node in project jSqlBox by drinkjava2.

the class EntityNetUtils method buildNodeId.

/**
 * Join PKey values into one String, used for node ID
 */
public static String buildNodeId(TableModel model, Object entity) {
    if (model == null || entity == null)
        return null;
    StringBuilder sb = new StringBuilder();
    for (ColumnModel col : model.getColumns()) {
        if (col.getPkey() && !col.getTransientable()) {
            EntityNetException.assureNotEmpty(col.getEntityField(), "EntityField not found for FKey column '" + col.getColumnName() + "'");
            if (sb.length() > 0)
                sb.append(EntityNet.COMPOUND_COLUMNNAME_SEPARATOR);
            sb.append(ClassCacheUtils.readValueFromBeanField(entity, col.getEntityField()));
        }
    }
    if (sb.length() == 0)
        throw new EntityNetException("Table '" + model.getTableName() + "' no Prime Key columns set");
    return sb.toString();
}
Also used : ColumnModel(com.github.drinkjava2.jdialects.model.ColumnModel)

Example 5 with Node

use of com.github.drinkjava2.jsqlbox.entitynet.Node in project jSqlBox by drinkjava2.

the class EntityNet method findNodeSetforNodes.

/**
 * According given path and input Node Set, find related node set
 *
 * @param level
 *            search level, start from 0
 * @param path
 *            The Path
 * @param input
 *            The input node collection
 * @param output
 *            The output node collection
 * @return Related node set
 */
private void findNodeSetforNodes(Integer level, Path path, Collection<Node> input, Map<Class<?>, Set<Node>> result) {
    if (level > 1000)
        throw new EntityNetException("Search level beyond 1000, this may caused by a circular reference path chain.");
    TableModel model = path.getTargetModel();
    String type0 = path.getType().substring(0, 1);
    String type1 = path.getType().substring(1, 2);
    Class<?> targetClass = model.getEntityClass();
    Set<Node> selected = new LinkedHashSet<Node>();
    String pathUniqueString = path.getUniqueIdString();
    Integer pathId = pathIdCache.get(pathUniqueString);
    // Start
    if ("S".equalsIgnoreCase(type0)) {
        if (level != 0)
            throw new EntityNetException("'S' type can only be used on path start");
        // Check if cached
        Map<Integer, Set<Node>> rootCache = queryCache.get("ROOT");
        if (this.allowQueryCache && path.getCacheable() && pathId != null && (rootCache != null)) {
            Set<Node> cachedNodes = rootCache.get(pathId);
            if (cachedNodes != null)
                selected = cachedNodes;
        } else {
            // check all targetClass
            Collection<Node> nodesToCheck = getAllNodeSet(targetClass);
            validateSelected(level, path, selected, nodesToCheck);
            // cache it if allow cache
            if (this.allowQueryCache && path.getCacheable() && !StrUtils.isEmpty(pathUniqueString)) {
                cacheSelected("ROOT", pathUniqueString, selected);
            }
        }
    } else // Child
    if ("C".equalsIgnoreCase(type0) && input != null && !input.isEmpty()) {
        for (Node inputNode : input) {
            Map<Integer, Set<Node>> childCache = queryCache.get(inputNode.getId());
            if (this.allowQueryCache && path.getCacheable() && pathId != null && childCache != null) {
                Set<Node> cachedNodes = childCache.get(pathId);
                if (cachedNodes != null)
                    selected.addAll(cachedNodes);
            } else {
                // Find childNodes meat class/columns/id condition
                Set<Node> nodesToCheck = new LinkedHashSet<Node>();
                for (Entry<String, Node> cNode : body.get(targetClass).entrySet()) {
                    List<ParentRelation> prs = cNode.getValue().getParentRelations();
                    if (prs != null)
                        for (ParentRelation pr : prs) {
                            if (inputNode.getId().equals(pr.getParentId()) && pr.getRefColumns().equalsIgnoreCase(path.getRefColumns())) {
                                nodesToCheck.add(cNode.getValue());
                                break;
                            }
                        }
                }
                validateSelected(level, path, selected, nodesToCheck);
                // now cached childNodes on parentNode
                if (this.allowQueryCache && path.getCacheable() && !StrUtils.isEmpty(pathUniqueString)) {
                    cacheSelected(inputNode.getId(), pathUniqueString, selected);
                }
            }
        }
    } else // Parent
    if ("P".equalsIgnoreCase(type0) && input != null && !input.isEmpty()) {
        String targetTableName = model.getTableName();
        EntityNetException.assureNotEmpty(targetTableName, "targetTableName can not be null");
        for (Node inputNode : input) {
            // Find parent nodes meat tableName/refColumns/nodeId condition
            Set<Node> nodesToCheck = new LinkedHashSet<Node>();
            List<ParentRelation> prs = inputNode.getParentRelations();
            if (prs != null)
                for (ParentRelation pr : prs) {
                    if (targetTableName.equalsIgnoreCase(pr.getParentTable()) && path.getRefColumns().equalsIgnoreCase(pr.getRefColumns())) {
                        Node node = this.getOneNode(targetClass, pr.getParentId());
                        if (node != null)
                            nodesToCheck.add(node);
                    }
                }
            validateSelected(level, path, selected, nodesToCheck);
            // now cached childNodes on parentNode
            if (this.allowQueryCache && path.getCacheable() && !StrUtils.isEmpty(pathUniqueString)) {
                cacheSelected(inputNode.getId(), pathUniqueString, selected);
            }
        }
    }
    Set<Node> nodes = result.get(targetClass);
    if (nodes == null) {
        nodes = new LinkedHashSet<Node>();
        result.put(targetClass, nodes);
    }
    if ("+".equals(type1) || "*".equals(type1))
        nodes.addAll(selected);
    if (!(path.getCacheable() && StrUtils.isEmpty(path.getUniqueIdString()))) {
        if (selected.size() > 100000)
            throw new EntityNetException("Query result return more than 100000 records to cache in memory, this may caused by careless programming.");
    }
    if (level > 10000)
        throw new EntityNetException("Search depth >10000, this may caused by careless programming.");
    if ("*".equals(type1) && !selected.isEmpty())
        findNodeSetforNodes(level + 1, path, selected, result);
    if (path.getNextPath() != null) {
        findNodeSetforNodes(level + 1, path.getNextPath(), selected, result);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Entry(java.util.Map.Entry) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TableModel(com.github.drinkjava2.jdialects.model.TableModel)

Aggregations

EntityNet (com.github.drinkjava2.jsqlbox.entitynet.EntityNet)7 Test (org.junit.Test)7 Path (com.github.drinkjava2.jsqlbox.entitynet.Path)6 Set (java.util.Set)5 Email (com.github.drinkjava2.functionstest.entitynet.entities.Email)4 User (com.github.drinkjava2.functionstest.entitynet.entities.User)4 TreeNode (com.github.drinkjava2.functionstest.entitynet.entities.TreeNode)3 TableModel (com.github.drinkjava2.jdialects.model.TableModel)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 Map (java.util.Map)2 ColumnModel (com.github.drinkjava2.jdialects.model.ColumnModel)1 FKeyModel (com.github.drinkjava2.jdialects.model.FKeyModel)1 Node (com.github.drinkjava2.jsqlbox.entitynet.Node)1 EntitySqlMapListHandler (com.github.drinkjava2.jsqlbox.handler.EntitySqlMapListHandler)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Entry (java.util.Map.Entry)1