Carbonado (Java)

Last updated
Carbonado
Developer(s) Amazon.com
Stable release
1.2.3 / March 4, 2012 (2012-03-04)
Repository OOjs UI icon edit-ltr-progressive.svg
Written in Java
Operating system Cross-platform (JVM)
Platform Java Virtual Machine
Type Object-Relational mapping
License Apache License 2.0
Website github.com/Carbonado/Carbonado

Carbonado is an open source relational database mapping framework, written in Java. Rather than following a typical O/R mapping approach, the relational model is preserved, while still being object-oriented. Not being tied to specific features of SQL or JDBC, Carbonado also supports non-SQL database products such as Berkeley DB. In doing so, relational features such as queries and indexes are supported, without the overhead of SQL.

Open-source software software licensed to ensure source code usage rights

Open-source software (OSS) is a type of computer software in which source code is released under a license in which the copyright holder grants users the rights to study, change, and distribute the software to anyone and for any purpose. Open-source software may be developed in a collaborative public manner. Open-source software is a prominent example of open collaboration.

Java (programming language) Object-oriented programming language

Java is a general-purpose programming language that is class-based, object-oriented, and designed to have as few implementation dependencies as possible. It is intended to let application developers write once, run anywhere (WORA), meaning that compiled Java code can run on all platforms that support Java without the need for recompilation. Java applications are typically compiled to bytecode that can run on any Java virtual machine (JVM) regardless of the underlying computer architecture. The syntax of Java is similar to C and C++, but it has fewer low-level facilities than either of them. As of 2019, Java was one of the most popular programming languages in use according to GitHub, particularly for client-server web applications, with a reported 9 million developers.

Object-relational mapping in computer science is a programming technique for converting data between incompatible type systems using object-oriented programming languages. This creates, in effect, a "virtual object database" that can be used from within the programming language. There are both free and commercial packages available that perform object-relational mapping, although some programmers opt to construct their own ORM tools.

Contents

History

Carbonado was originally developed for internal use by Amazon.com, as a revision to an earlier framework. It was released as an Apache licensed open-source project in October 2006. [1]

Apache License Free software license developed by the ASF

The Apache License is a permissive free software license written by the Apache Software Foundation (ASF). The Apache License, Version 2.0 requires preservation of the copyright notice and disclaimer. Like other free software licenses, the license allows the user of the software the freedom to use the software for any purpose, to distribute it, to modify it, and to distribute modified versions of the software, under the terms of the license, without concern for royalties. This makes the Apache License a FRAND-RF license. The ASF and its projects release the software they produce under the Apache License. The license is also used by many non-ASF projects.

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 be defined by Java interface alone. Every Storable must have an annotation describing the primary key of the entity.

@PrimaryKey("entityId")publicinterfaceMyEntityextendsStorable{longgetEntityId();voidsetEntityId(longid);StringgetMessage();voidsetMessage(Stringmessage);}

Carbonado Storables are not pure POJOs, 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.

In software engineering, the active record pattern is an architectural pattern found in software that stores in-memory object data in relational databases. It was named by Martin Fowler in his 2003 book Patterns of Enterprise Application Architecture. The interface of an object conforming to this pattern would include functions such as Insert, Update, and Delete, plus properties that correspond more or less directly to the columns in the underlying database table.

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.

In computer programming, boilerplate code or just boilerplate are sections of code that have to be included in many places with little or no alteration. When using languages that are considered verbose, the programmer must write a lot of code to accomplish only minor functionality. Such code is called boilerplate.

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

Repositoryrepo=...Storage<MyEntity>storage=repo.storageFor(MyEntity.class);MyEntityentity=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:

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

All Repositories are created using a builder pattern. 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.

The Builder is a design pattern designed to provide a flexible solution to various object creation problems in object-oriented programming. The intent of the Builder design pattern is to separate the construction of a complex object from its representation. It is one of the Gang of Four design patterns.

BDBRepositoryBuilderbuilder=newBDBRepositoryBuilder();builder.setName(name);builder.setEnvironmentHome(envHome);builder.setTransactionWriteNoSync(true);Repositoryrepo=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<MyEntity>storage=repo.storageFor(MyEntity.class);Query<MyEntity>query=storage.query("message = ?").with(message);List<MyEntity>matches=query.fetch().toList();

Transactions

Transactions are created from a Repository instance, and they define a thread-local scope. Multiple persist operations are automatically grouped together, and commit must be called to complete the transaction.

Transactiontxn=repo.enterTransaction();try{MyEntityentity=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.

Notes

  1. Vogels, Werner (26 October 2006). "Carbonado". All Things Distributed. Retrieved 25 December 2010.


Related Research Articles

Berkeley DB (BDB) is a software library intended to provide a high-performance embedded database for key/value data. Berkeley DB is written in C with API bindings for C++, C#, Java, Perl, PHP, Python, Ruby, Smalltalk, Tcl, and many other programming languages. BDB stores arbitrary key/data pairs as byte arrays, and supports multiple data items for a single key. Berkeley DB is not a relational database.

Database organized collection of data

A database is an organized collection of data, generally stored and accessed electronically from a computer system. Where databases are more complex they are often developed using formal design and modeling techniques.

SQL is a domain-specific language used in programming and designed for managing data held in a relational database management system (RDBMS), or for stream processing in a relational data stream management system (RDSMS). It is particularly useful in handling structured data where there are relations between different entities/variables of the data. SQL offers two main advantages over older read/write APIs like ISAM or VSAM. First, it introduced the concept of accessing many records with one single command; and second, it eliminates the need to specify how to reach a record, e.g. with or without an index.

In computer software, a data access object (DAO) is an object that provides an abstract interface to some type of database or other persistence mechanism. By mapping application calls to the persistence layer, the DAO provides some specific data operations without exposing details of the database. This isolation supports the single responsibility principle. It separates what data access the application needs, in terms of domain-specific objects and data types, from how these needs can be satisfied with a specific DBMS, database schema, etc..

Hibernate ORM is an object-relational mapping tool for the Java programming language. It provides a framework for mapping an object-oriented domain model to a relational database. Hibernate handles object-relational impedance mismatch problems by replacing direct, persistent database accesses with high-level object handling functions.

The object-relational impedance mismatch is a set of conceptual and technical difficulties that are often encountered when a relational database management system (RDBMS) is being served by an application program written in an object-oriented programming language or style, particularly because objects or class definitions must be mapped to database tables defined by a relational schema.

The Java Persistence API (JPA) is a Java application programming interface specification that describes the management of relational data in applications using Java Platform, Standard Edition and Java Platform, Enterprise Edition.

Entity Framework (EF) is an open source object-relational mapping (ORM) framework for ADO.NET. It was a part of .NET Framework, but since Entity framework version 6 it is separated from .NET framework.

An embedded database system is a database management system (DBMS) which is tightly integrated with an application software that requires access to stored data, such that the database system is "hidden" from the application’s end-user and requires little or no ongoing maintenance. It is actually a broad technology category that includes

The Doctrine Project is a set of PHP libraries primarily focused on providing persistence services and related functionality. Its prize projects are an object-relational mapper (ORM) and the database abstraction layer it is built on top of.

HBase is an open-source non-relational distributed database modeled after Google's Bigtable and written in Java. It is developed as part of Apache Software Foundation's Apache Hadoop project and runs on top of HDFS or Alluxio, providing Bigtable-like capabilities for Hadoop. That is, it provides a fault-tolerant way of storing large quantities of sparse data.

The Java Persistence Query Language (JPQL) is a platform-independent object-oriented query language defined as part of the Java Persistence API (JPA) specification.

Apache Empire-db is a Java library that provides a high level object-oriented API for accessing relational database management systems (RDBMS) through JDBC. Apache Empire-db is open source and provided under the Apache License 2.0 from the Apache Software Foundation.

DataObjects.NET is a persistence and object-relational mapping framework for the Microsoft .NET Framework. It allows the developer to define the business logic directly in one of the .NET languages like C#. The persistent objects can be retrieved by LINQ queries. Persistent data can be stored in SQL Servers or in even simpler DBMS which only provide simple indexing operations. In contrast to many other ORM frameworks the database model is generated and maintained automatically.

Java Database Connectivity (JDBC) is an application programming interface (API) for the programming language Java, which defines how a client may access a database. It is a Java-based data access technology used for Java database connectivity. It is part of the Java Standard Edition platform, from Oracle Corporation. It provides methods to query and update data in a database, and is oriented towards relational databases. A JDBC-to-ODBC bridge enables connections to any ODBC-accessible data source in the Java virtual machine (JVM) host environment.

ODB is an object-relational mapping (ORM) system for the C++ language. It allows an application developer to persist C++ objects to a relational database without having to deal with tables, columns, or SQL and without manually writing any mapping code. ODB supports C++98/03 and C++11 language standards and comes with optional profiles for Boost and Qt which allow an application developer to seamlessly use value types, containers, and smart pointers from these libraries in persistent C++ classes. ODB is free software and is dual-licensed under the GPL and a proprietary license.

The following outline is provided as an overview of and topical guide to databases:

ActiveJPA is an open-source application framework written in Java for object-relational mapping.