Wednesday, March 28, 2012

Best Practices for Speeding Up Your Web Site

http://developer.yahoo.com/performance/rules.html



Lock and condition java 5 concurrent api


old way:
use synchronized keyword

Object monitorObject;

synchronized(monitorObject){
//critical section
}

new way: use Lock.lock() and Lock.unlock()
Lock lockObject;

try{
lockObject.lock();
}
finally{
lockObject.unlock();
}

----

old way: use wait() and notify() in the critical section

//insdie your critical section

boolean somecondition;
while(somecondition){
wait();
}

boolean someOtherCondition;
if(someOtherCondition){
notify();
}

new way: use await() and signal() on condition variables

Condition conditionVariable = lockObject.newCondition();

boolean somecondition;
while(somecondition){
conitionVariable.await();
}

boolean someothercondition;
if(someOtherCondition){
conditionVariable.signal();
}

Thursday, March 22, 2012

Google question

Tell me what you know about security
What is cursor
Tell me about event system
Dealer inventory, what data structure, if large data,how you display partial while getting the whole
Junit ,what things you need pay attention

Wednesday, March 21, 2012

Python interview questions

http://techpreparation.com/python-interview-questions-answers1.htm

Friday, March 16, 2012

Python ---head first

TAb key Offer suggestion on IDLE
Alt p. alt n previous statements,next statement

array
Movies=["abc",12 ]. Can contain mixed type

Print(movies[1])
Movies.insert(1,sth)
movies.append()

for each_movie in movies
print(each_movie)

while condition:

string either double or single quotes
list of list
isinstance(movies,list)-----true
default recurtion limit 1000
BIF built in function
""" triple quotes add comments

a module is a file ends with.py

Pypi
build a module,distribute it, install

def functionname(arg1,arg2=0) default value make second arg optional

if booleanvariable:

All python code associated with name space
_main_

import nester

import os
os.getcwd() //get current directory
os.chdir(). // change directory

data=open('file.txt')
data.seek(0) // go to beginning of the file
for each line in data
print(each line,end=' ')
(v1,v2)=each line.split(":")
(v1,v2)=each line.split(":",1). //split at most to 2 parts
each line.find(":")""" -1 find nothing
data.close()


try:
except:""" when no argument ,all exceptions
Or except ValueError:""" concrete exception
except IOError as err
print(str(err)) """ str() can be used to access the stringed representation of any data object that supports the conversion.
finally:
Two type of list, square brackets, you can change, regular brackets, immutable
in for loop, pass like continue in java

if os.path.exist('stretch.txt')

line.strip()""" trimming

out = open("out.txt", "w")
print("egghkk", file=out)


try:
data=open('file.txt')
except IOError as err:
finally:
if 'data' in locals() """ locals() returns a collection of variables within the current scope
data.close()

try :
with open('file.txt') as data, open('file1.txt') as data2

When you use with,you don't need worry about closing any open files

---
Pickle
Python ships with standard library called picked, which can save and load almost any Python data object. Once you pickle your data to a file, it is persistent and ready to be read into another program at some later data/time. You can store your pickled data on disk, put it in a database, or transfer it over a network.
when you are ready, reversing this process unpickles your persistent pickled data and recreates your data in its original form within Python's memory.

import pickle
...
try:
with open('mydata.pickle', 'wb') as mysavedata:
pickle.dump([1,2,'three'], mysavedata) """ use dump to save

...
with open('mydata.pickle','rb') as myrestoredata:
a_list = pickle.load(myrestoredata) """ restore the data
except pickle.PickleError as perr:

in-place sorting sort()
Copied sorting sorted()

tt.txt looks like this: 2:34,4-45,2.35

def sanitize(time_string):
if '-' in time_string:
splitter = '-'
if ':' in time time_string:
splitter=':'
else:
return (time_string)
(mins,secs) = time_string.split(splitter)
return(mins+'.'+secs)
with open('tt.txt') as file:
data = file.readline()
list = data.strip().split(',')
cleanlist = []
for item in list
cleanlist.append(sanitize(item))
( this whole thing can be written as cleanlist = [sanitize(item) for item in list] )
print(sorted(cleanlist))
list.pop(1) """ get one element from a list

If you try to add a data item to a set
that already contains the data item, Python simply ignores it.
distances = set()
distances = {10.6, 11, 8, 10.6, "two", 7}
Any duplicates in the supplied list of data values are ignored like 10.6

print(sorted(set([sanitize(t) for t in james]))[0:3])

create dictionary
dictionary1 = {}
dictionary1['Name'] = 'John Cleese'
dictionary1['Occupations'] = ['actor', 'comedian', 'writer', 'film producer']
dictionary2 = dict()
dictionary2= {'Name': 'Michael Palin', 'Occupations': ['comedian', 'actor', 'writer', 'tv']}


---------
define a class

class Athlete:
def __init__(self):
# The code to initialize an "Athlete" object.
create instance
a = Athlete()
When Python processes this line of code, it turns the factory function call into
the following call, which identifies the class, the method (which is automatically
set to __init__()), and the object instance being operated on:
( Athlete.__init__(a) is called)

, not only does the __init__() method require self as its first
argument, but so does every other method defined within your class.

what you write,
d = Athlete("Holy Grail")
d.how_big()
Python executes
Athlete.__init__(d, “Holy Grail”)
Athlete.how_big(d)

, if you save your AthleteList class to a file
called athletelist.py, you can import the into your code
using this line of code:
from athletelist import AthleteList

---------
Python comes with its very own web server,
included in the http.server library module
Here are the five lines of code needed to build a web server in Python.

from http.server import HTTPServer, CGIHTTPRequestHandler
port = 8080
httpd = HTTPServer(('', port), CGIHTTPRequestHandler)
print("Starting simple_httpd on port: " + str(httpd.server_port))
httpd.serve_forever()

data_files = glob.glob("data/*.txt")
form_data = cgi.FieldStorage()
Grab all of the form data and put it in a dictionary
athlete_name = form_data['which_athlete'].value """ which_athlete is a radio button
check which radio button is selected


When your CGI script raises an exception, Python arranges for the error
message to display on STDERR (standard error). The CGI mechanism is
programmed to ignore this output because all it wants is the CGI script’s
standard output.
Python’s standard library comes with a CGI tracking
module (called cgitb) that, when enabled, arranges for detailed error
messages to appear in your web browser.
import cgitb
cgitb.enable()

@property
def top3(self):
return(sorted(set([self.sanitize(t) for t in self]))[0:3])

The use of the @property decorator allows the top3() method to appear
like an attribute to users of the class. So, instead of calling the top3()
method like this:

print(yate.u_list(athletes[athlete_name].top3()))

Treat the top3() method as if it was another class attribute, and call it like
this:
print(yate.u_list(athletes[athlete_name].top3))

The standard library string module
includes a class called Template,
which supports simple string substitutions.
The standard library cgi module
provides support for writing CGI scripts.
ƒ The standard library glob module is
great for working with lists of filenames.

The standard library cgitb module,
when enabled, lets you see CGI coding
errors within your browser.
ƒ Use cgitb.enable() to switch on
CGI tracking in your CGI code.
ƒ Use cgi.FieldStorage() to
access data sent to a web server as part
of a web request; the data arrives as a
Python dictionary.

--
Google provides a cross-platform
Android emulator that lets you develop for the phone as needed
http://developer.android.com/sdk/index.html

Thursday, March 15, 2012

Just Spring reading notes

Today I started reading about Spring.
Dependency Injection (inversion of control)
Provides dependency to your objects at runtime.

Here is the problem:
class DataReaderClient{
FileReader fileReader=new FilerReader();
fileReader.read();

}

in this code, client and reader are tightly coupled together.
improve to:
designing to interfaces
Interface Ireader{
read();
}

class DataReaderClient{
Ireader fileReader=new FilerReader();
fileReader.read();

}

but this is still hardwired, the client has to know which concrete implementation to use.

Spring is to remove this kind of dependency.It works on one single mantra:DI.
When a standalone program starts, it starts the program,creates dependencies,then proceeds to execute the appropriate methods. All dependencies and relationship are created by IoC container and they are injected into the main program as properties.

class DataReaderClient{
Ireader reader=null;
ApplicationContext ctx = null;

public DataReaderClient(){
ctx = new ClassPathXmlApplicationContext("ssss.xml");
}

fetchData(){
reader = (IReader)ctx.getBean("reader");
reader.read();
}
}

sss.xml




------
client will always have to inject a type of IReader, can improve to a service layer

class DataReaderClient{
ReaderService service=null;
ApplicationContext ctx = null;

public DataReaderClient(){
ctx = new ClassPathXmlApplicationContext("ssss.xml");
service = ctx.getBean("readerService");
}

fetchData(){
reader = (IReader)ctx.getBean("reader");
reader.read();
}
}


Tuesday, March 13, 2012

Web container

Web container (also known as a Servlet container) is the component of a web server that interacts with the servlets. A web container is responsible for managing the lifecycle of servlets, mapping a URL to a particular servlet and ensuring that the URL requester has the correct access rights. A web container implements the web component contract of theJava EE architecture, specifying a runtime environment for web components that includes security, concurrency, lifecycle management,transaction, deployment, and other services

Sunday, March 11, 2012

Java IO

Programs use byte streams to perform input and output of 8-bit bytes. All byte stream classes are descended from InputStream and OutputStream.
InputStream.read();
OutputStream.write(c)
both are abstract classes.
FileInputStream in = null;         FileOutputStream out = null;          try {             in = new FileInputStream("xanadu.txt");             out = new FileOutputStream("outagain.txt");
Closing a stream when it's no longer needed is very important — so important that CopyBytes uses afinally block to guarantee that both streams will be closed even if an error occurs. This practice helps avoid serious resource leaks. 
all other stream types are built on byte streams. 
--------------
All character stream classes are descended from Reader and Writer. 
Reader.read();
Writer.write(c)
Line-Oriented IO has to use bufferedReader and BufferedWriter
--
Above are unbuffered IO,This means each read or write request is handled directly by the underlying OS.
to reduce overhead, the Java platform implements buffered I/O streams. 
using wrapping approach
inputStream = new BufferedReader(new FileReader("xanadu.txt")); outputStream = new BufferedWriter(new FileWriter("characteroutput.txt"));
Decorator Design pattern!!!
-----
Objects of type Scanner are useful for breaking down formatted input into tokens and translating individual tokens according to their data type. 
 s = new Scanner(new BufferedReader(new FileReader("xanadu.txt")));              while (s.hasNext()) {                 System.out.println(s.next());             }
s.useDelimiter(",\\s*");
s.hasNextDouble();
---
Stream objects that implement formatting are instances of either PrintWriter, a character stream class, or PrintStream, a byte stream class. 
 System.out  is PrintStream 

Decorator Pattern

Attaches additional responsibilities to an object dynamically. Decorators provides a flexible alternative to subclassing for extending functionality.

http://javapapers.com/design-patterns/decorator-pattern/

(abstract class) compontent{
m1();
m2();
}
concreteComponent{
m1();
m2();
}

(abstract) Decorator extends component{
m1();
m2();
}

you can use the decorator to decorate concreteComponent to concreteNewComponent with something additional

Design pattern book 95~98

Beverage beverage = new Espresso();

Beverage beverage2 = new DarkRoast();
beverage2 = new Mocha(beverage2);
beverage2 = new Mocha(beverage2); // double mocha
beverage2 = new Whip(beverage2); // with whip cream

HOW PL/SQL GETS EXECUTED

http://comsci.liu.edu/~vasilaky/db2/lec1.htm

the PL/SQL engine on the server where it is compiled. The named PL/SQL
block is compiled only at the time of its creation, or if it has been
changed. The compilation process includes syntax checking, binding,
and p-code generation.

Oracle guarantees a read-consistent view
of the data. Until that point, all data that has been inserted or updated
will be held in memory and only be available to the current user. The
rows that have been changed will be locked by the current user and will
not be available for updating to other users until the locks have been re-leased.

Saturday, March 10, 2012

Difference between web server and app server

http://www.javaworld.com/javaworld/javaqa/2002-08/01-qa-0823-appvswebserver.html

A Web server exclusively handles HTTP requests, whereas an application server serves business logic to application programs through any number of protocols.

Monday, March 5, 2012

Servlet basic

SampleServlet extends HttpServlet{
init();
service(httprequest, httpresponse);

}

server engine call service() to handle request, return response.
when the servlet first loaded, init() is called once for initialization
after service() is done, servlet waits till next request

the server can decide to unload this servlet by calling destroy(), you want to
release all the shared resources that were created in init()

two packages
javax.servlet
javax.servlet.http

HttpServlet class methods
service()--serve all requests,defaut implementation is calling doGet, doPost,doDelete,doPut in response to http request
doHeader() defaut calling doGet(), chop off body, only return header

Thread safe Servlet
An application is thread safe if it always behaves predictablly regardless of the number of concurrent threads running in this
process space.
Synchronization in java is the process of locking a variable so that it can be read and modified by only a single thread at a time.
The synchronized keyword eliminates race conditions by locking variables for a specific duration
synchronized(this){
...
..
}

it is possible to synchronize the entire service() method rather than blocks of code, in this way, only on thread at a time
can execute the method. the more code is synchronized, the greater the performance penalty. To aoid this penalty,minimie the amount
of code.

a new servlet object is not instantiated for each new client request, Rather, a single servlet usually services all requests.
This behavior explains how instance variables can often be shared
Because every call to service(), doget(), doPost() is executed in its own thread, variables local to these methods are not shared between
requests and are automatically thread safe.

public class AServlet extends HttpServlet
{
//instance variable
String UserName; // not thread safe

}

public class BServlet extends HttpServlet
{
//instance variable
String userName; // not thread safe

public void service(){
String userName; //thread safe
}

or use SingleThreadModel interface

public class CServlet extends HttpServlet implements SingleThreadModel
{
//instance variable
String userName; // thread safe

}
If servlet implemnts the SinglethreadModel interface, the server guarantees that no more than on thread can execute the service,
doGet, doPost method at a time for a particular servlet instance. this gurantees thread safe.

Http redirects
An Http redirect is a set of instructions included in the header of an Http response that instruct the browser
to issue a new request to a new URL.

An HTTP redirect response uses an HTTP status code of either '301 Moved Permanently" or "302 Moved Temporarily" accompanied
by a Location header containing a valid URL.the browser receives an Http response with status code 301,302,it will
immediately request the resource at the URL designated by the Location Header
sample redirect response

HTTP/1.1 301 Moved Permanently
..
Location:http://www..../....html

sending a redirect with the servlet API
two ways:
service(HttpServletRequest request,HttpServletResponse response){
..
reponse.status(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location", someurl);
}
or
response.setRedirect(url); //this way only set 302, url has to be full qualified url

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.