Database

Databases and Data Sources

Any data stored directly in the web server's memory (in a servlet or in a session), is volatile in that it will be lost if the web server terminates. The use of database of other data source, even directly writing to the file system, can overcome with this. In some cases, data might be stored on the web browser in the form of a cookie.


Rational Database

Relational database like MySQL and Apache Derby are common.

core syntax is common and well understood

however, embedding SQL operations in programming language is awkward

also, optimising database accesses (by database administrators) requires directly working with the SQL

therefore it is very desirable to separate the SQL operations from the domain logic

mapping objects to a relational database can be quite cumbersome.


Structural Mapping

Objects and relations handle links in two different ways:

objects use pointers or memory references to refer to other objects

    a) objects can have multivalued fields to handle collections of other objects;

    b) objects in a collection don't need a reference back to the containing object.

relational databases handle links by forming a foreign key into another table

    a) all relation links are single-valued

To handle this, each object needs to maintain an identity field which is the relational identity for the object.


Inheritance

The tradeoffs are about duplication of data structure and speed-of-access.

There are three patterns to consider here:

Single table inheritance: uses one table to store all classes in a hierarchy.

                                        advantage: easy to program and modify as the hierarchy changes.

                                        disadvantage: wastes space in the database, but avoids join operations.

Concrete table inheritance: uses one table to store each concrete class a hierarchy.

                                            advantage: more tables, less wasted space.

                                            disadvantage: changes to superclasses or altering the hierarchy leads to altering 

                                                                    all dependent tables. Avoids join operations.

Class table inheritance: uses one table to store each class in a hierarchy.

                                       advantage: the simplest relationship between the classes and tables.

                                       disadvantage: requires join operations at the database in order to pull back an object.


Relational Database Libraries

Rather than directly program for a relational database it is more convenient to use a library.

Eg. Hibernate allows annotations to POJOs (Plain Old Java Objects) that it transforms into appropriate object/relational mappings.


Object-Oriented Database

With OO database you don't worry about the mapping.

You work with a large structure of interconnected objects, and the database figures out when to move objects on or off disk.

Java Database is an example.

a) improve productivity: a ball-park one-third of programming effort is concerned with mapping to a relational database

b) risky: less used and not as well developed as SQL


Non SQL Database

In some cases your application may not require the features of a relational database for the most part of its operation.

Also, it may be much more convenient for you to work with data storage formats that are identical to the formats used for transmitting data between client and server, such as JSON or XML.


So called "NoSQL" database use looser consistency models than traditional relational databases. They do in fact allow SQL-like languages to be used, but largely they use simple key-value stores with a goal being significant performance benefits in terms of latency and throughput.


CouchDB

Apache CouchDB is a database that uses JSON for documents, JavaScript for MapReduce queries, and regular HTTP for API.

A CouchDB database lacks a schema, or rigid pre-defined data structures such as tables.

Data stored in CouchDB is a JSON document(s).

The structure of the data or document(s) can change dynamically to accomodate evolving needs.


CouchDB Basics

A CouchDB server hosts named database, which store documents. Each document is uniquely named in the database, and CouchDB provides a RESTful (Representational state transfer) HTTP API for reading and updating (add, edit, delete) database documents.

Documents are the primary unit of data in CouchDB and consist of any number of fields and attachments.

The CouchDB document update model is lockless and optimisitc. Document edits are made by client applications loading documents, applying changes, and saving them back to the database. If another client editing the same document saves their changes first, the client get an edit conflict error on save. To resolve the update confilct, the latest document version can be opened, the latest document can be opened, the edits reapplied and the updated tried again.

Documents updates (add, edit, delete) are all or nothing, either succeeding entirely or failing completely. The database never contains partially saved or edited documents.


CouchDB Document

A CouchDB document is a JSON object that consists of named fields. Field values may be strings, numbers, dates, or even ordered list and associative maps.


Use with JavaScript

The installed CouchDB server contains  a JavaScript library to access it.


jQuery and CouchDB

$. couch is used to communicate with a CouchDB server,  the server methods can be called directly without creating an instance.

If you simply point your browser to the CouchDB URL then the response is a JSON string with that information.


LightCouch for Java

LightCouch is a light-weight library for accessing CouchDB from Java.

There are many such libraries, another is couchdb4j.

Saving a JSON document

Saving POJOs and Maps

Retrieving documents


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章