Tuesday, February 14, 2012

Restful Web Services (Note from the book by the same name)

In RESTful architectures, the method information goes into the HTTP method. In Re-
source-Oriented Architectures, the scoping information goes into the URI.

http://www.myservice.org/discussion/2008/12/10/{topic}

If the HTTP method doesn’t
match the method information, the service isn’t RESTful. If the scoping information
isn’t in the URI, the service isn’t resource-oriented.

RPC stands for Remote Procedure Call
An RPC-style web service accepts an envelope full of data from its client, and sends a
similar envelope back. such as SOAP
XML-RPC is mostly a legacy protocol these days,

Every RPC-style service defines a brand new vocabulary. By contrast, all RESTful web services share a standard vocabulary of
HTTP methods.

Sunday, February 12, 2012

HTTP and more

HTTP:
connectionless and stationless
each request requires its own connection.
it has no memory of prior connections.

GET method. containing no body content, a GET request is comprised of only a method statement and various request header fields
GET /login.html HTTP/1.1
User-Agent ...
Even though a GET request does not send any information in the body of the message, data can still be pased as part of the GET statement itself
GET /login.html?username=dustin&...
GET method is normally used to request static data.
conditional GET: if a GET request includes and IF-Modified-Since
http servers tranditionally store GET parameters in system environment variables that can be accessed by CGI programs.

POST Method
is commonly used for passing user input to the server. all the info is stored in the body of the request rather than in the URL portion of the method statement.
advantages: info is not visible in the URL and there is no limit to the amount of information that can be passed.
POST /login.html http:/1.1
more header info

(in the body) username=dustin&...

basic authentication:
GET /salary.html HTTP/1.0
Authorization: Basic TATATATTTT (BASE64 encoding of the username and password)

MIME
Multipurpose Internet Mail Extensions

Saturday, February 11, 2012

Web Basics

URI:Uniform Resource Identifer
is a term referring to any specification that identifies an object on the internet. there are two types URIs--URL(Uniform Resource Locator) and Uniform Resource Name.

URL is a specification for identifying an object, such as a file, newsgroup,CGI program by indicating its exact location on the internet.
URN is a method for referencing an object without declaring the full path to the object. rather by its location, a resource is referenced by its an alias. in case the resource is removed, all there are multiple copies on different servers in different parts of the world. when a request is made for a URN, the browser could locate the nearest copy of the resource and return it. URN is not supported by most software.

format
http://host[:port]/path/resource_name[#section][?query_string]
ftp://username:password@host[:port]/path
try{
url=new URL("http://yahoo.com/index.html")
}
catch(MalformedURLException e){
}

BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

String nextLine = in.readLine

this return html files

absolute URL
relative URL

URL encoding:
URLs are comprised of a string of printable characters within ASCII coded character set. Any unsafe or nonprintable characters within a URL must be encoded. URL encoding involves replacing those characters with % followed by two hexadecimal digits corresponding to the character's ASCII value.
%20 is space, but it is so common, it is encoded as a single plus +
then + itself is encoded as %2B

Java includes a class to help convert resource names and other references into URL safe strings
URLEncoder.encode(string)

Web browser: is a client application that requests, receives, and displays HTML tags
web server is also called httpserver, responds to requests from a web browser by returning html,images ..also responsible for enforcing security polices, logging , cach
simple web server implementation
ServerSocket serverConnect ServerSocket(port); listen on port
HttpServer server = new HttpServer(serverConnect.accept())

CGI: common Gateway Interface
is a standard interface between an HTTP server and an application.CGI enables the Web server to generate dynamic content rather than just return static HTML files.In addition, because CGI can accept and process information submitted by the user,it can provide two way interaction between the client and the web server.


Wednesday, February 8, 2012

线程之间怎么通讯?什么是critical section/semaphore/mutax,区别?

线程之间怎么通讯?什么是critical section/semaphore/mutax,区别?
网上找到这么一段话,

how do 2 threads communicate?

Basically via memory i.e. member fields of a class. Any thread can write into a single field and let any other thread read its value.
But if you need to make sure that any value written into the field will also be found/read by another thread, you need to put the statements accessing your communication field into blocks

synchronized( lockObj ){ commField= ...; }

The lockObj is a central object (also called semaphore or mutex - for mutual exclusion ) you should choose carefully so that it can be accessed from all your classes and very early: think of an instance of java.lang.Class : that's a foolproof singleton.

I want to discourage you from using the "fast and easy" synchronized qualifier for methods. This way you end up with a mess of Locks and creating deadlocks!

what is critical section

In concurrent programming a critical section is a piece of code that accesses a shared resource (data structure or device) that must not be concurrently accessed by more than one thread of execution. A critical section will usually terminate in fixed time, and a thread, task or process will have to wait a fixed time to enter it (aka bounded waiting). Some synchronization mechanism is required at the entry and exit of the critical section to ensure exclusive use.

--
what is semaphore

In computer science, a semaphore is a variable or abstract data type that provides a simple but useful abstraction for controlling access by multiple processes to a common resource in a parallel programming environment.

--
A mutex is essentially the same thing as a binary semaphore, and sometimes uses the same basic implementation. However, the term "mutex" is used to describe a construct which prevents two processes from executing the same piece of code, or accessing the same data, at the same time. The term "binary semaphore" is used to describe a construct which limits access to a single resource.

In many cases a mutex has a concept of an "owner": the process which locked the mutex is the only process allowed to unlock it. In contrast, semaphores generally do not have this restriction, something the producer-consumer example above depends upon.

ejb migration


http://onjava.com/pub/a/onjava/2005/03/09/ejb-migration.html

Thursday, February 2, 2012

heap vs stack

http://www.maxi-pedia.com/what+is+heap+and+stack

very good article about heap and stack

What is stack?

The two sections other from the code segment in the memory are used for data. The stack is the section of memory that is allocated for automatic variables within functions.

Data is stored in stack using the Last In First Out (LIFO) method. This means that storage in the memory is allocated and deallocated at only one end of the memory called the top of the stack. Stack is a section of memory and its associated registers that is used for temporary storage of information in which the most recently stored item is the first to be retrieved.

What is heap?

On the other hand, heap is an area of memory used for dynamic memory allocation. Blocks of memory are allocated and freed in this case in an arbitrary order. The pattern of allocation and size of blocks is not known until run time. Heap is usually being used by a program for many different purposes.

The stack is much faster than the heap but also smaller and

Semaphores in JDK 1.5

http://www.developerfusion.com/article/84294/semaphores-in-jdk-15/

very good article about threads and semaphore, easy to understand

Wednesday, February 1, 2012

Nth Highest Salary in Oracle

http://www.oratable.com/nth-highest-salary-in-oracle/

First things first: The question is ambiguous!

Let’s say this is your data:

NameSalary
KING5000
FORD3000
SCOTT3000
JONES2975
BLAKE2850
CLARK2850
ALLEN1600

Who is second – FORD or SCOTT or both?

What will you say about JONES’s salary – is it the 3rd highest salary, or the 4th highest?

If you are looking for the set of people earning the Nth highest salary, with no gaps in case of ties, then JONES should be ranked 3rd, after KING [5000, 1st], followed by FORD and SCOTT [both 3000, 2nd].

If you are looking for exact ranks with gaps if there are ties, then JONES is the 4th highest paid employee, as there are 3 people earning more than him – KING, FORD and SCOTT. In this system of ranking, FORD and SCOTT are 2nd jointly and no employee is 3rd.

This is how your ranks will look, in the 2 cases:

NameSalaryRank
KING50001
FORD30002
SCOTT30002
JONES29753
BLAKE28504
CLARK28504
ALLEN16005
NameSalaryRank
KING50001
FORD30002
SCOTT30002
JONES29754
BLAKE28505
CLARK28505
ALLEN16007

Scenario 1: No gaps in case of ties Scenario 2: Gaps in case of ties

Once you have your question sorted out -

(a) Set of people earning the Nth highest salary, with continuous ranks if there are ties, OR

(b) Set of people earning the Nth highest salary, with skipped rank numbers if there are ties

Then you can proceed to writing the queries.

Scenario 1: DENSE_RANK () for Nth highest row, no gaps in case of ties

The analytic function dense_rank() will rank the rows with no gaps in ranking sequence if there are ties.

The ranks are calculated as: SQL> select ename

2        ,sal 3        ,dense_rank() over (order by sal desc) ranking 4  from   emp;  ENAME             SAL    RANKING ---------- ---------- ---------- KING             5000          1 FORD             3000          2 SCOTT            3000          2 JONES            2975          3 CLARK            2850          4 BLAKE            2850          4 ALLEN            1600          5

Wrap a filter around and pick out the Nth highest salary, say the 4th highest salary.

SQL> select *

2  from 3  ( 4    select ename 5          ,sal 6          ,dense_rank() over (order by sal desc) ranking 7    from   emp 8  ) 9  where ranking = 4 -- Replace 4 with any value of N 10  /  ENAME             SAL    RANKING ---------- ---------- ---------- BLAKE            2850          4 CLARK            2850          4

The 4th position has a tie between BLAKE and CLARK.

Scenario 2: RANK () for Nth highest row, gaps in case of ties

The analytic function rank() will rank the rows with gaps in ranking sequence if there are ties.

The ranks are calculated as:

SQL> select ename

2        ,sal 3        ,rank() over (order by sal desc) ranking 4  from   emp;  ENAME             SAL    RANKING ---------- ---------- ---------- KING             5000          1 FORD             3000          2 SCOTT            3000          2 JONES            2975          4 CLARK            2850          5 BLAKE            2850          5 ALLEN            1600          7 TURNER           1500          8

Wrap a filter around and pick out the Nth highest salary, say the 4th highest salary.

SQL> select *

2  from 3  ( 4    select ename 5          ,sal 6          ,rank() over (order by sal desc) ranking 7    from   emp 8  ) 9  where ranking = 4 -- Replace 4 with any value of N 10  /  ENAME             SAL    RANKING ---------- ---------- ---------- JONES            2975          4

A different answer from the previous query, as there is no rank 3 because of the tied 2nd place.

Closing Notes

The requirement to “find Nth highest row” is incomplete, until the following questions are also answered:

  1. Can the result match more than one value? If not, on what basis should the one record be chosen if there is a tie?
  2. How should the subsequent records be ranked in case of ties – contiguously or with gaps?

Depending on the answer for (2), DENSE_RANK (for contiguous) or RANK (for gaps) can be used. Depending on the answer for (1), extra filter criteria can be applied to the SQL.

There are other approaches for calculating the Nth highest row, too. The next is a non-analytic approach, which works the same way as the RANK query (gaps for ties).

SQL> select ename 2       , sal 3  from emp a 4  where 3 = ( select count(*) -- Replace 3 with any value of (N - 1) 5              from emp b 6              where b.sal > a.sal) 7  /  ENAME             SAL ---------- ---------- JONES            2975

However, tests have shown the analytics approach to be more efficient than the non-analytics one for Nth highest or Top-N type of queries.

sample SQL

1. which year did most people join the company

select to_char(hiredate,'YYYY')
from employee
group by to_char(hiredate, 'YYYY')
where count(empid)=select max(count(empid)) from employee group by to_char(hiredate,'YYYY')

2. list out the employees who earn more than average salary of their department

select *
from employee e1
group by dept_id
having salary>(select avg(salary) from employee e2
where e1.dept_id = e2.dept_id )

3. find nth maximum salary

select ename
from employee a
where &N =( select count(distinct(sal)) from employee b where a.sal < b.sal)
N = 5

4. select duplicate records in the employee table
select * from employee e
where e.empno in(select b.empno from employee b group by b.empno
having count(b.empno)<1)
and a.rowid!=min(b.rowid)

5. list the length of service the employee (n years and m months)

select ename,
to_char(trunc(months_between(sysdate,hiredate)/12)) || 'year' ||
to_char(trunk(months_between(systdate,hiredate)/12)) || 'month'
from employee

SQL constraints

Constraints are used to limit the type of data that can go into a table.

Constraints can be specified when a table is created (with the CREATE TABLE statement) or after the table is created (with the ALTER TABLE statement).


CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)

The UNIQUE constraint uniquely identifies each record in a database table.

The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for a column or set of columns.

A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it.

MySQL:

CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
UNIQUE (P_Id)
)

SQL Server / Oracle / MS Access:

CREATE TABLE Persons
(
P_Id int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)

CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
CONSTRAINT uc_PersonID UNIQUE (P_Id,LastName)
)


add UNIQUE
ALTER TABLE Persons
ADD UNIQUE (P_Id)

ALTER TABLE Persons
ADD CONSTRAINT uc_PersonID UNIQUE (P_Id,LastName)

To drop a UNIQUE constraint, use the following SQL:

ALTER TABLE Persons
DROP INDEX uc_PersonID

ALTER TABLE Persons
DROP CONSTRAINT uc_PersonID


.... to be continued

SQL syntax

http://www.w3schools.com/sql/sql_intro.asp

RDBMS stands for Relational Database Management System.

SQL DML and DDL

SQL can be divided into two parts: The Data Manipulation Language (DML) and the Data Definition Language (DDL).

The query and update commands form the DML part of SQL:

  • SELECT - extracts data from a database
  • UPDATE - updates data in a database
  • DELETE - deletes data from a database
  • INSERT INTO - inserts new data into a database

The DDL part of SQL permits database tables to be created or deleted. It also defines indexes (keys), specifies links between tables, and imposes constraints between tables. The most important DDL statements in SQL are:

  • CREATE DATABASE - creates a new database
  • ALTER DATABASE - modifies a database
  • CREATE TABLE - creates a new table
  • ALTER TABLE - modifies a table
  • DROP TABLE - deletes a table
  • CREATE INDEX - creates an index (search key)
  • DROP INDEX - deletes an index
SELECT column_name(s)
FROM table_name
ORDER BY column_name(s) ASC|DESC

INSERT INTO Persons
VALUES (4,'Nilsen', 'Johan', 'Bakken 2', 'Stavanger')

INSERT INTO Persons (P_Id, LastName, FirstName)
VALUES (5, 'Tjessem', 'Jakob')

UPDATE Persons
SET Address='Nissestien 67', City='Sandnes'
WHERE LastName='Tjessem' AND FirstName='Jakob'

DELETE FROM Persons
WHERE LastName='Tjessem' AND FirstName='Jakob'

Delete All Rows

DELETE FROM table_name

or

DELETE * FROM table_name

----------------------------------
SQL TOP Clause

Not all database systems support the TOP clause.
sybase support top, oracle use rownum

SQL Server
SELECT TOP number|percent column_name(s)
FROM table_name
MySQL Syntax

SELECT column_name(s)
FROM table_name
LIMIT number

Oracle Syntax
SELECT column_name(s)
FROM table_name
WHERE ROWNUM <= number

----------------------------
SELECT * FROM Persons
WHERE City LIKE '%tav%'

SELECT * FROM Persons
WHERE City NOT LIKE '%tav%'

---------

SQL Alias Syntax for Tables

SELECT column_name(s)
FROM table_name
AS alias_name

SQL Alias Syntax for Columns

SELECT column_name AS alias_name
FROM table_name


------

Different SQL JOINs

Before we continue with examples, we will list the types of JOIN you can use, and the differences between them.

  • JOIN: Return rows when there is at least one match in both tables
  • LEFT JOIN: Return all rows from the left table, even if there are no matches in the right table
  • RIGHT JOIN: Return all rows from the right table, even if there are no matches in the left table
  • FULL JOIN: Return rows when there is a match in one of the tables


----------

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
INNER JOIN Orders
ON Persons.P_Id=Orders.P_Id
ORDER BY Persons.LastName

The LEFT JOIN keyword returns all rows from the left table (table_name1), even if there are no matches in the right table (table_name2).

SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name

The RIGHT JOIN keyword returns all the rows from the right table (table_name2), even if there are no matches in the left table (table_name1).

SELECT column_name(s)
FROM table_name1
RIGHT JOIN table_name2
ON table_name1.column_name=table_name2.column_name

The FULL JOIN keyword return rows when there is a match in one of the tables.
SELECT column_name(s)
FROM table_name1
FULL JOIN table_name2
ON table_name1.column_name=table_name2.column_name

-------------
The SQL UNION operator combines two or more SELECT statements.
SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2

The UNION command selects only distinct values.

UNION ALL will include everything
SELECT E_Name FROM Employees_Norway
UNION ALL

---
create DB
CREATE DATABASE database_name

create table
CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
column_name3 data_type,
....
)

SELECT E_Name FROM Employees_USA

create tables

CREATE TABLE Persons
(
P_Id int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)

--

CREATE a table from another table


CREATE TABLE new_table
AS (SELECT * FROM old_table);

CREATE TABLE suppliers
AS (SELECT *
FROM companies
WHERE id > 1000);

This would create a new table called suppliers that included all columns from the companies table.

CREATE TABLE suppliers
AS (SELECT companies.id, companies.address, categories.cat_type
FROM companies, categories
WHERE companies.id = categories.id
AND companies.id > 1000);

This would create a new table called suppliers based on columns from both the companies and categories tables.

create a table from another table without copying any values from the old table?

CREATE TABLE new_table
AS (SELECT * FROM old_table WHERE 1=2);