Course Announcements. Bacon is due next Monday. Next lab is about drawing UIs. Today s lecture will help thinking about your DB interface.

Similar documents
MURA Office hours Thurs at 12-1pm in CIT 546 Contact for more info.

Comp 11 Lectures. Mike Shah. July 12, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures July 12, / 33

Announcements. CS 188: Artificial Intelligence Fall Causality? Example: Traffic. Topology Limits Distributions. Example: Reverse Traffic

ST-Links. SpatialKit. Version 3.0.x. For ArcMap. ArcMap Extension for Directly Connecting to Spatial Databases. ST-Links Corporation.

Announcements. John Jannotti (cs32) Design Patterns Feb 13, / 1

AI Programming CS S-09 Knowledge Representation

Course Announcements. John Jannotti (cs32) Scope, Collections & Generics Feb 8, / 1

SideNail. Martín Schonaker, Santiago Martínez

Exam 1. March 12th, CS525 - Midterm Exam Solutions

Clojure Concurrency Constructs, Part Two. CSCI 5828: Foundations of Software Engineering Lecture 13 10/07/2014

MITOCW watch?v=fkfsmwatddy

NP-Completeness I. Lecture Overview Introduction: Reduction and Expressiveness

Guide to Proofs on Sets

KB Agents and Propositional Logic

Math Lab 8: Electric Fields Integrating Continuous Charge Distributions II Due noon Thu. Feb. 1 in class

UI Implementation. John Jannotti. Mar 21, /course/cs0320/www/docs/lectures/ John Jannotti (cs32) UI Implementation Mar 21, / 26

OBEUS. (Object-Based Environment for Urban Simulation) Shareware Version. Itzhak Benenson 1,2, Slava Birfur 1, Vlad Kharbash 1

The Inductive Proof Template

Binary addition example worked out

CS145 Midterm Examination

Bayes Nets III: Inference

Math 300: Foundations of Higher Mathematics Northwestern University, Lecture Notes

Number Systems III MA1S1. Tristan McLoughlin. December 4, 2013

Descriptive Statistics (And a little bit on rounding and significant digits)

Database Applications (15-415)

Foundations of Computation

Introducing GIS analysis

Welcome to Comp 411! 2) Course Objectives. 1) Course Mechanics. 3) Information. I thought this course was called Computer Organization

Methods of Mathematics

1 Trees. Listing 1: Node with two child reference. public class ptwochildnode { protected Object data ; protected ptwochildnode l e f t, r i g h t ;

cis32-ai lecture # 18 mon-3-apr-2006

Introduction to Algebra: The First Week

Introduction to Algorithms

Instructor (Brad Osgood)

FACULTY OF SCIENCE ACADEMY OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING OBJECT ORIENTED PROGRAMMING DATE 07/2014 SESSION 8:00-10:00

CS221 / Autumn 2017 / Liang & Ermon. Lecture 15: Bayesian networks III

1 Deterministic Turing Machines

15 Skepticism of quantum computing

CS173 Lecture B, November 3, 2015

Algorithms for Data Science

Mining Data Streams. The Stream Model. The Stream Model Sliding Windows Counting 1 s

Lecture 15: Exploding and Vanishing Gradients

Comp 11 Lectures. Mike Shah. July 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures July 26, / 40

Mining Data Streams. The Stream Model Sliding Windows Counting 1 s

Lecture 5. 1 Review (Pairwise Independence and Derandomization)

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Matroids and Greedy Algorithms Date: 10/31/16

Regression, part II. I. What does it all mean? A) Notice that so far all we ve done is math.

EQ: How do I convert between standard form and scientific notation?

Math101, Sections 2 and 3, Spring 2008 Review Sheet for Exam #2:

One-to-one functions and onto functions

Logic: The Big Picture

Geodatabase An Introduction

Logic. Propositional Logic: Syntax

Practical Data Processing With Haskell

Quadratic Equations Part I

Guest Speaker. CS 416 Artificial Intelligence. First-order logic. Diagnostic Rules. Causal Rules. Causal Rules. Page 1

1 Closest Pair of Points on the Plane

Structural Induction

MITOCW ocw f99-lec30_300k

Sometimes the domains X and Z will be the same, so this might be written:

Prosurv LLC Presents

CS1800: Mathematical Induction. Professor Kevin Gold

CS 124 Math Review Section January 29, 2018

CS1800: Strong Induction. Professor Kevin Gold

Motion II. Goals and Introduction

Physics 201 Lab 2 Air Drag Simulation

1.1 Administrative Stuff

Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018

Designing Information Devices and Systems I Fall 2018 Lecture Notes Note Introduction to Linear Algebra the EECS Way

DIFFERENTIAL EQUATIONS

Mathematical Logic Part Three

Solving a Series. Carmen Bruni

Using Microsoft Excel

ASTRO 114 Lecture Okay. What we re going to discuss today are what we call radiation laws. We ve

Line Integrals and Path Independence

Automatic Differentiation and Neural Networks

CSC2515 Assignment #2

Lecture: Local Spectral Methods (1 of 4)

Generating Function Notes , Fall 2005, Prof. Peter Shor

Proof strategies, or, a manual of logical style

Maps performance tips On server: Maintain DB connections, prepared statements (per thread/request!)

Sequential Logic (3.1 and is a long difficult section you really should read!)

Turing Machines Part III

CSC 5170: Theory of Computational Complexity Lecture 4 The Chinese University of Hong Kong 1 February 2010

Announcements. CS 188: Artificial Intelligence Spring Probability recap. Outline. Bayes Nets: Big Picture. Graphical Model Notation

Ratios, Proportions, Unit Conversions, and the Factor-Label Method

Latches. October 13, 2003 Latches 1

Designing Information Devices and Systems I Spring 2018 Lecture Notes Note Introduction to Linear Algebra the EECS Way

CS 542G: Conditioning, BLAS, LU Factorization

TA guide Physics 208 Spring 2008 Lab 3 (E-1): Electrostatics

CS173 Strong Induction and Functions. Tandy Warnow

Introduction to Computational Complexity

Extensibility Patterns: Extension Access

Knowledge Representation and Reasoning

Communication Engineering Prof. Surendra Prasad Department of Electrical Engineering Indian Institute of Technology, Delhi

Cutting Graphs, Personal PageRank and Spilling Paint

Propositional Logic: Syntax

FORMAL PROOFS DONU ARAPURA

First Order Logic: Syntax and Semantics

15-451/651: Design & Analysis of Algorithms September 13, 2018 Lecture #6: Streaming Algorithms last changed: August 30, 2018

Transcription:

Course Announcements Bacon is due next Monday. Today s lecture will help thinking about your DB interface. Next lab is about drawing UIs. John Jannotti (cs32) ORMs Mar 9, 2017 1 / 24

ORMs John Jannotti /course/cs0320/www/lectures/ Mar 9, 2017 John Jannotti (cs32) ORMs Mar 9, 2017 2 / 24

Rows as Objects To a rough approximation, rows can be mapped to objects. table class row object column primitive field/attribute An ORM is a software layer to make that mapping easy. Object-Relational Mapping. But there s a lot more to it. And it should be said, some argue against ORMs. Learn more: object relational impedance mismatch. You won t use an ORM in Bacon, but you should think about abstraction. John Jannotti (cs32) ORMs Mar 9, 2017 3 / 24

Relationships Between Objects table class row object column primitive field/attribute? object references An employee table Name Salary Company Jim Jones 98293? Steve Johnson 87293? Greg Smith 129349? John Jannotti (cs32) ORMs Mar 9, 2017 4 / 24

Primary Keys ID Name Symbol Price Employees 1 Microsoft MSFT 30.85 35010 2 Google GOOG 595.03 20100 3 Oracle ORCL 26.22 18350 ID Name Salary Company 234 Jim Jones 98293 2 129 Steve Johnson 87293 3 233 Greg Smith 129349 2 John Jannotti (cs32) ORMs Mar 9, 2017 5 / 24

Difficulties with Collections A row may not contain an arbitrary number of anything. To encode a many-to-one relationship between employees and companies: A company ID is embedded in each employee row. May feel backwards This is a foreign key and there are consistency checks. Foreign keys provide associated sets, but not lists, and not many-many relations. John Jannotti (cs32) ORMs Mar 9, 2017 6 / 24

Employees with multiple companies ID Name Symbol Price Employees 1 Microsoft MSFT 30.85 35010 2 Google GOOG 595.03 20100 3 Oracle ORCL 26.22 18350 ID Name Salary 234 Jim Jones 98293 129 Steve Johnson 87293 233 Greg Smith 129349 Employee ID Company ID 234 2 129 3 233 2 129 1 233 3 John Jannotti (cs32) ORMs Mar 9, 2017 7 / 24

Think hard about your many-many relationships Maybe the relationship itself deserves promotion to a type. Does this relationship deserve its own abstraction? Employment Then it has one-to-many relationships to employees and companies. More importantly, a natural place for associated data. For example, salary belongs there, not in employee. Same schema, except the intermediate table is now first-class. And now you have two one-to-many relationships, instead of one many-to-many. How might we take this advice in the Bacon schema? John Jannotti (cs32) ORMs Mar 9, 2017 8 / 24

Advice for database-backed applications Identify your units of work (often a single web, or API request) Use transactions to isolate those units. You can abort cleanly because of atomicity. Keep your state in the database (not in memory). Scaling is more straightforward. A (single!) caching layer makes it fast enough. Large binary data (e.g images) may be an exception. John Jannotti (cs32) ORMs Mar 9, 2017 9 / 24

ORM Software Structure We ve talked about the schema you might use to store objects. How would you structure the code itself? Think about Separating business logic from database access. Creating new objects (allocating IDs, explicit saves) Exposing/Hiding the amount of laziness for fetching object graphs. Maintaining object identity (programmer expectations, efficiency). Performance: Caching lookups, managing connections, concurrency. Sharing code across different objects, collections. John Jannotti (cs32) ORMs Mar 9, 2017 10 / 24

What do you want an ORM to do for you? CRUD: Create, Read, Update, Delete These are the basics. You want to do these without SQL. Simple objects are simple, what about references? Should the ORM chase pointers? Monitor dirtiness? Does the client write explcit operations? Caching As you traverse your business objects, you may cycle. It would be bad to keep querying the DB. It would also be bad to have multiple Toy Story objects. Declarative style Mapping to the existing schema. How much to prefetch vs be lazy. Maybe a config file, maybe attributes. John Jannotti (cs32) ORMs Mar 9, 2017 11 / 24

Clicker Test Which are you struggling with most? A) Opening, pasing, sharing, closing DB resources. B) Performance (Including, but not limited to caching) C) Writing the SQL queries. Keeping them out of business logic D) Unit testing Dijkstra (while avoiding the database). E) Making your graph generic across node, edge types. John Jannotti (cs32) ORMs Mar 9, 2017 12 / 24

One way to do it Let s look at a simpler way you might do this for Bacon. I tell you explicitly: I don t think this is best, but it s decent. That said, don t try to implement Hibernate, stay away from Reflection APIs. First, two sidebars, to understand some of the code try-with-resources: a much better way to close objects that need closing. lambda expression: concise syntactic sugar for anonymous classes (even anonymous classes may be new to you). John Jannotti (cs32) ORMs Mar 9, 2017 13 / 24

Try With Resources You used to think Garbage Collection would always clean up. You found that wasn t true for open files. And lately, database connections, statements, results, etc. Until Java 7, handling these things properly was challenging. 1 p u b l i c v o i d badcode ( ) { 2 InputStream stream = new F i l e I n p u t S t r e a m (... ) ; 3 //... use stream 4 stream. c l o s e ( ) 5 } This code can t be counted on to close() the stream properly. John Jannotti (cs32) ORMs Mar 9, 2017 14 / 24

finally helps, sort of 1 p u b l i c v o i d t e d i o u s C o d e ( ) { 2 InputStream stream ; 3 t r y { 4 stream = new F i l e I n p u t S t r e a m (... ) ; 5 //... use stream 6 } c a t c h ( IOException e ) { 7 // h a n d l e i t 8 } f i n a l l y { 9 t r y { 0 i f ( stream!= n u l l ) { 1 stream. c l o s e ( ) ; 2 } 3 } c atch ( IOException e ) { 4 // h a n d l e y e t a n o t h e r one 5 } 6 } John Jannotti (cs32) ORMs Mar 9, 2017 15 / 24

try-with-resources to the rescue 1 p u b l i c v o i d cleancode ( ) { 2 t r y ( InputStream stream = new F i l e I n p u t S t r e a m (... ) ) { 3 //... use stream 4 } c a t c h ( IOException e ) { 5 // h a n d l e i t 6 } 7 } John Jannotti (cs32) ORMs Mar 9, 2017 16 / 24

What are lambda expressions (in Java)? A concise expression to represent a function. But no changes at the JVM level. So what must a lambda expression really create? John Jannotti (cs32) ORMs Mar 9, 2017 17 / 24

Representing functions before lambda The Comparator interface allows Collections.sort() use the caller s notion of comparison. Think of an object (that implements Comparator) as a function. To make a new one, you need a new class (AgeComparator). 1 p u b l i c c l a s s AgeComparator 2 implements Comparator<Person >() { 3 p u b l i c i n t compare ( Person x, Person y ) { 4 r e t u r n x. getlastname ( ). compareto ( y. getlastname ( ) 5 } 6 } 7 C o l l e c t i o n s. s o r t ( people, new AgeComparator ( ) ) ; John Jannotti (cs32) ORMs Mar 9, 2017 18 / 24

Anonymous class creation is verbose Anonymous classes make it a bit easier, but... 1 C o l l e c t i o n s. s o r t ( people, new Comparator<Person >() { 2 p u b l i c i n t compare ( Person x, Person y ) { 3 r e t u r n x. getlastname ( ). compareto ( y. getlastname ( ) ) ; 4 } 5 } ) ; Both the meaning under boilerplate. John Jannotti (cs32) ORMs Mar 9, 2017 19 / 24

Java Lambdas are syntactic sugar 1 C o l l e c t i o n s. s o r t ( people, new Comparator<Person >() { 2 p u b l i c i n t compare ( Person x, Person y ) { 3 r e t u r n x. getlastname ( ). compareto ( y. getlastname ( ) ) ; 4 } 5 } ) ; becomes 1 C o l l e c t i o n s. s o r t ( people, 2 ( x, y ) > x. getlastname ( ). compareto ( y. getlastname ( ) ) ) John Jannotti (cs32) ORMs Mar 9, 2017 20 / 24

What does a lambda do? Exactly the same thing as the previous code. How? We didn t say anything about Comparator John Jannotti (cs32) ORMs Mar 9, 2017 21 / 24

What does a lambda do? Exactly the same thing as the previous code. How? We didn t say anything about Comparator Collections.sort wants a Comparator Comparator is a Functional interface (one non-default method) So that s what the lambda creates. (If sort were overloaded, you might need to disambiguate.) Important: There s nothing really new here. Just convenience. Method reference syntax, Person::getName, also instantiates a functional interface. John Jannotti (cs32) ORMs Mar 9, 2017 21 / 24

On to example DB code. Highlights to look for The concept of an Entity (rather than a Value class) The use of the Proxy pattern (to hit the DB lazily) The use of Actor and Movie interfaces (to allow Proxy and Bean implementations). The purity of the Bean implementation. (No SQL anywhere.) The caching layer. All in one place, EntityProxy. The strange but important equals() overrides. John Jannotti (cs32) ORMs Mar 9, 2017 22 / 24