Docsity

Java Query Language (JQL) and Object Querying, Lecture notes of Object Oriented Programming

Partial preview of the text

Download Java Query Language (JQL) and Object Querying and more Lecture notes Object Oriented Programming in PDF only on Docsity!

JQL : The Java Query Language

Object

Querying

-^ JQL is a simple extension for Java.•^ JQL adds support for

Object Querying.

-^ Object Querying

helps us pick sets of

objects from other sets. • JQL can simplify working with collections.

Hypothetical

IM^ Client

SessionString name;send_msg(Msg);recv_msg(Msg);

WindowString name;int id;show_msg(Msg);enter_msg();

A Hypothetical Instant Messaging Client

Querying

:^ Nested

Loop

-^ We're^

joining^ the sets of Sessions and Windows.

-^ An easy way – the nested loop join:–^ ArrayList results = new Arraylist();^ –^ for(Session s : sessions)^ –^ for(Window w : windows)^ –^ if(w.name.equals(s.name))^ –^

results.add(new Object[w,s]);

-^ This takes

O(| sessions

| * | windows

|)^ time.

Joining

Dilemma

A^ B
BA
A^ B

JQL^ for

Collection

Operation

-^ results = selectAll(Session s =sessions,

»^

Window w = windows: »^ s.name.equals(w.name));

-^ Let the Query Evaluator take care of thedetails!

Query

Expressions

•^ w.ID^

==^5

•^ w.ID^

==^5 &&

w.name

==^ s.name

•^ w.ID^

>^5 &&

w.ID^

<^10 &&

w.name

==^ s.name

s.isActive()

&&^ ...

Query

Expressions

^ Method calls are allowed ^ Side effects are possible ^ These expressions do not short circuit• So what else can we do with queries?

A^ Query

Invariant

-^ assert null == selectA(BinTree a, BinTree b : •^

(a != b && a.left == b.left) || •

(a != b && a.right == b.right)|| •

a.left == b.right);

Uses^ Object Tracking

to check all

BinTrees.

Object

Tracking

-^ public class GroupChat{^ –^ private List participants;^ –^ public SomeClass(){^ –^ participants = new ArrayList();^ –^ ...^ –^ }^ –^ public void addChatter(String name){^ –^ ...^ –^ participants.add(new Session(name));^ –^ ...^ –^ } •^ }

ObjectTracker docsity.com

A^ Query

Pipeline

Sessions Windows

w.name ==s.name Temp. Results w.ID == 5 w.ID == 5 && w.name == s.name

Ordering

the^ Pipeline

There are two factors to consider: • Cost-Very dependent on input sizes- Input sizes are dependent on selectivity… • Selectivity- The proportion of results rejected by thestage- We estimate it based on the expression used:^ ==, < >, !=, aMethod() - Or, we can test it with sampling

Querying

Performance^ Four versions of: selectAll(Integer a=as,Integer b=bs,Integer c=cs,Integer d=ds:a == b &&b != c &&c < d);

HANDOPT

Implementation

-^ HashMap<Integer,ArrayList>map; •^ map^ = newHashMap<Integer,ArrayList>(); •^ for(Integer i1 : array1) {^ –^ ArrayList grp =map.get(i1);^ –^ if(grp == null) {^ •^ grp

= newArrayList(); • map.put(i1,grp); – } – grp.add(i1);

-^ } •^ ArrayList<Object[]> matches =

new ArrayList<Object[]>(); • Collections.sort(array4); • for(Integer i2 : array2) {^ –^ int b=i2;^ –^ ArrayList grp =map.get(i2);^ –^ if(grp != null) {

for(Integer i1 : grp) {

int a=i1; for(Integer i3 : array3) {int c=i3;if(b != c) {for(intx=array4.size();x!=0;x=x1){int d=array4.get(x-1);if(c<d){Object[] t = new Object[4];t[0]=a;t[1]=b;t[2]=c;t[3]=d;matches.add(t);}^ else {^

break; }} } } } } return matches;