Carbonado (Java)
Encyclopedia
Carbonado is an open source
Open source
The term open source describes practices in production and development that promote access to the end product's source materials. Some consider open source a philosophy, others consider it a pragmatic methodology...

 relational
Relational database
A relational database is a database that conforms to relational model theory. The software used in a relational database is called a relational database management system . Colloquial use of the term "relational database" may refer to the RDBMS software, or the relational database itself...

 database mapping framework, written in Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

. Rather than following a typical O/R mapping
Object-relational mapping
Object-relational mapping in computer software is a programming technique for converting data between incompatible type systems in object-oriented programming languages. This creates, in effect, a "virtual object database" that can be used from within the programming language...

 approach, the relational model is preserved, while still being object-oriented. Not being tied to specific features of SQL
SQL
SQL is a programming language designed for managing data in relational database management systems ....

 or JDBC, Carbonado also supports non-SQL database products such as Berkeley DB
Berkeley DB
Berkeley DB is a computer software library that provides a high-performance embedded database for key/value data. Berkeley DB is a programmatic software library written in C with API bindings for C++, PHP, Java, Perl, Python, Ruby, Tcl, Smalltalk, and most other programming languages...

. In doing so, relational features such as queries and indexes are supported, without the overhead of SQL.

History

Carbonado was originally developed for internal use by Amazon.com
Amazon.com
Amazon.com, Inc. is a multinational electronic commerce company headquartered in Seattle, Washington, United States. It is the world's largest online retailer. Amazon has separate websites for the following countries: United States, Canada, United Kingdom, Germany, France, Italy, Spain, Japan, and...

, as a revision to an earlier framework. It was released as an Apache licensed
Apache License
The Apache License is a copyfree free software license authored by the Apache Software Foundation . The Apache License requires preservation of the copyright notice and disclaimer....

 open-source project in October 2006.

Entity definitions

Relational entities are known as Storables in Carbonado, and they are defined by an interface or abstract class. Annotations are required to specify features which cannot defined by Java interface alone. Every Storable must have an annotation describing the primary key of the entity.

@PrimaryKey("entityId")
public interface MyEntity extends Storable {
long getEntityId;
void setEntityId(long id);

String getMessage;
void setMessage(String message);
}

Carbonado Storables are not pure POJO
Pojo
Pojo may refer to:* Pohja, the Swedish name for the Finnish municipality* POJO, abbreviation of Plain Old Java Object in computer programming...

s, and they must always extend the Storable superclass. By doing so,
they gain access to various methods built into it. A Storable definition may also contain business logic, following the active record pattern
Active record pattern
In software engineering, the active record pattern is an architectural pattern found in software that stores its data in relational databases. It was named by Martin Fowler in his 2003 book Patterns of Enterprise Application Architecture...

.

The actual implementation of the Storable is generated at runtime by Carbonado itself. The standard object methods of toString, equals and hashCode are also generated. This greatly simplifies the process of defining new entities, since no boilerplate code needs to be written.

The process of loading a Storable by key starts by calling a factory method to create an uninitialized instance:

Repository repo = ...
Storage storage = repo.storageFor(MyEntity.class);
MyEntity entity = storage.prepare;

Next, the key properties are set and load is called:

entity.setEntityId(id);
entity.load;

Repository usage

A Repository is a gateway to the underlying database. A few core implementations are available, which include:
  • JDBC access
  • Berkeley DB
  • Berkeley DB Java Edition
  • An in memory database


In addition, composite Repositories exist which support simple replication and logging.

All Repositories are created using a builder pattern
Builder pattern
The builder pattern is an object creation software design pattern. The intention is to abstract steps of construction of objects so that different implementations of these steps can construct different representations of objects...

. Each type of builder supports options specific to the Repository type. When a Repository instance is built, it only adheres to the standard interface. Access to specific features is provided by a Capability interface.

BDBRepositoryBuilder builder = new BDBRepositoryBuilder;
builder.setName(name);
builder.setEnvironmentHome(envHome);
builder.setTransactionWriteNoSync(true);
Repository repo = builder.build;

Query execution

Carbonado queries are defined by a simple filter expression and an order-by specification. Compared to SQL, the filter closely resembles a "where" clause. Filters can include joined properties and they may also include sub filters. This simple example queries for entities with a given message:

Storage storage = repo.storageFor(MyEntity.class);
Query query = storage.query("message = ?").with(message);
List matches = query.fetch.toList;

Transactions

Transactions
Database transaction
A transaction comprises a unit of work performed within a database management system against a database, and treated in a coherent and reliable way independent of other transactions...

 are created from a Repository instance, and they define a thread-local
Thread-local storage
Thread-local storage is a computer programming method that uses static or global memory local to a thread.This is sometimes needed because normally all threads in a process share the same address space, which is sometimes undesirable...

scope. Multiple persist operations are automatically grouped together, and commit must be called to complete the transaction.

Transaction txn = repo.enterTransaction;
try {
MyEntity entity = storage.prepare;
entity.setEntityId(1);
entity.setMessage("hello");
entity.insert;

entity = storage.prepare;
entity.setEntityId(2);
entity.setMessage("world");
entity.insert;

txn.commit;
} finally {
txn.exit;
}

This design approach shows how Carbonado is not like an O/R mapping framework. Such frameworks typically hide the concept of transactions entirely, often by using sessions which track changes. In Carbonado, all actions are direct.
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK