Berkeley之官方实例--类似SQL



    /**
     * Do prefix query, similar to the SQL statement:
     * <blockquote><pre>
     * SELECT * FROM table WHERE col LIKE 'prefix%';
     * </pre></blockquote>
     *
     * @param index
     * @param prefix
     * @return
     * @throws DatabaseException
     */
    public <V> EntityCursor<V> doPrefixQuery(EntityIndex<String, V> index,
                                             String prefix)
        throws DatabaseException {


        assert (index != null);
        assert (prefix.length() > 0);


        /* Opens a cursor for traversing entities in a key range. */
        char[] ca = prefix.toCharArray();
        final int lastCharIndex = ca.length - 1;
        ca[lastCharIndex]++;
        return doRangeQuery(index, prefix, true, String.valueOf(ca), false);
    }


    /**
     * Do range query, similar to the SQL statement:
     * <blockquote><pre>
     * SELECT * FROM table WHERE col >= fromKey AND col <= toKey;
     * </pre></blockquote>
     *
     * @param index
     * @param fromKey
     * @param fromInclusive
     * @param toKey
     * @param toInclusive
     * @return
     * @throws DatabaseException
     */
    public <K, V> EntityCursor<V> doRangeQuery(EntityIndex<K, V> index,
                                               K fromKey,
                                               boolean fromInclusive,
                                               K toKey,
                                               boolean toInclusive)
        throws DatabaseException {


        assert (index != null);


        /* Opens a cursor for traversing entities in a key range. */
        return index.entities(fromKey,
                              fromInclusive,
                              toKey,
                              toInclusive);
    }


    /**
     * Do a "AND" join on a single primary database, similar to the SQL:
     * <blockquote><pre>
     * SELECT * FROM table WHERE col1 = key1 AND col2 = key2;
     * </pre></blockquote>
     *
     * @param pk
     * @param sk1
     * @param key1
     * @param sk2
     * @param key2
     * @return
     * @throws DatabaseException
     */
    public <PK, SK1, SK2, E> ForwardCursor<E>
        doTwoConditionsJoin(PrimaryIndex<PK, E> pk,
                            SecondaryIndex<SK1, PK, E> sk1,
                            SK1 key1,
                            SecondaryIndex<SK2, PK, E> sk2,
                            SK2 key2)
        throws DatabaseException {


        assert (pk != null);
        assert (sk1 != null);
        assert (sk2 != null);


        EntityJoin<PK, E> join = new EntityJoin<PK, E>(pk);
        join.addCondition(sk1, key1);
        join.addCondition(sk2, key2);


        return join.entities();
    }


    /**
     * Query the Employee database by Department's secondary-key: deptName. 
     * <blockquote><pre>
     * SELECT * FROM employee e, department d
     * WHERE e.departmentId = d.departmentId
     * AND d.departmentName = 'deptName';
     * </pre></blockquote>
     *
     * @param deptName
     * @throws DatabaseException
     */
    public void getEmployeeByDeptName(String deptName)
        throws DatabaseException {


        Department dept = departmentByName.get(deptName);
        /* Do an inner join on Department and Employee. */
        EntityCursor<Employee> empCursor =
            employeeByDepartmentId.
            subIndex(dept.getDepartmentId()).entities();
        try {
            for (Employee emp : empCursor) {
                System.out.println(emp);
            }
            System.out.println();
        } finally {
            empCursor.close();
        }
    }
    
    /**
     * Query the Employee database by adding a filter on Department's
     * non-secondary-key: deptLocation.
     * <blockquote><pre>
     * SELECT * FROM employee e, department d
     * WHERE e.departmentId = d.departmentId
     * AND d.location = 'deptLocation';
     * </pre></blockquote>
     * 
     * @param deptLocation
     * @throws DatabaseException
     */
    public void getEmployeeByDeptLocation(String deptLocation)
        throws DatabaseException {


        /* Iterate over Department database. */
        Collection<Department> departments =
            departmentById.sortedMap().values();
        for (Department dept : departments) {
            if (dept.getLocation().equals(deptLocation)) {
                /* A nested loop to do an equi-join. */
                EntityCursor<Employee> empCursor =
                    employeeByDepartmentId.
                    subIndex(dept.getDepartmentId()).
                    entities();
                try {
                    /* Iterate over all employees in dept. */
                    for (Employee emp : empCursor) {
                        System.out.println(emp);
                    }
                } finally {
                    empCursor.close();
                }
            }
        }

    }

类型实例来源官方例子截取,通过这种方式类似SQL方式进行查询。

但是需要借助EntityStore、PrimaryIndex和SecondaryIndex

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值