Lightcouch Pet Peeves, Features, and Insights

This specific content was written 14 years ago. Please keep this in mind as it may be outdated and not adhering to best-practices.

Introduction

We’ve been using lightcouch over at AIT, and over time have come up with some specific requirements and general insights about lightcouch. I’ve decided to make a list (and add to it over time), as well as implement a type of utility class which builds upon the original lightcouch functionality for the features we would like.

On to the list…

1) What if a user wishes to insert a JSON string directly instead of creating a JSON object and adding overhead?

I’ve asked the original author on the lightcouch google group (hope the links works) about this, and the answer was for architectural and error checking reasons, specifically:

“Having JSON properly formatted as an object should be more convenient and portable, by means of parsing to JsonObject (by Gson) or other method.  The overhead of doing so should not be a big concern I think, but it’s good to conform to the API.” – Ahmed Yehia

However, as we still wanted this feature we’ve added utilized the method described here.

2) Is it possible to check if a couchdb database exists without creating a CouchDBClient?

There is currently no independent checkIfDBExists Method within CouchDB, but instead the code is enclosed within the CreateDB method of the CouchDBContext.java file. This is utilized only when a CouchDBClient is created. It would be quite useful to have a separate static utility method for this purpose.

a) How does CouchDBClient check if a database exists?

Couchdb unfortunately has enclosed the capability to check if a DB exists, within CouchDBClient, and furthermore within the CreateDB method of CouchDB Context. [If I’m missing something please tell me 🙂 ]
Therefore, there is currently no independent checkIfDBExists Method within CouchDB.

In CouchDBClient.java, on line 121, the following is called:

context = new CouchDbContext(this, getConfig());

which leads to the constructor of CouchDbContext.java:

CouchDbContext(CouchDbClient dbc, CouchDbConfig config) {
  this.dbc = dbc;
  if (config.isCreateDbIfNotExist()) {
    createDB(config.getDbName());
  } else {
    serverVersion(); // pre warm up client
  }
}

The createDB(String dbName) method then basically called the HttpPut class of Apache Httpclient and requests the database URL:

http://IP:PORT/dbName

If the database does not exist, the server responds with  {"error":"not_found","reason":"no_db_file"}.

CouchDB interprets this as an exception and continues by creating the database:

URI uri = builder(dbc.getBaseUri()).path(dbName).build();
try {
headresp = dbc.head(uri);
} catch (NoDocumentException e) { // db doesn't exist
HttpPut put = new HttpPut(uri);
putresp = dbc.executeRequest(put);
log.info(String.format("Database: '%s' is created.", dbName));
}

3) Each CouchDbClient is limited to one database.

We are currently using lightcouch within a servlet which will handle mutliple requests concerning multiple databases.
During our servlet iterations we’ve used the following:

a) Create a separate local CouchDBClient for each request. This however is too ineficient.

b) Create a HashMap of persistent CouchDBClients for each different database, and use the appropriate one for each request.
However, the question is why to have multiple CouchDBClient objects for each database?
Having a setDatabase method and only one client would not work due to multi-threading race conditions.
Three concurrent http requests to the servlet could result in the Database of the client being changed three times.

The conclusion we reached was to stay at 2) unless a static utility class was written providing the ability for methods such as:

public static boolean addJSONDocument(String dbName, String JSONText) though this would be sufficient for us.

A more generic setup would be:

public static boolean addJSONDocument(String Host, String Port, String dbName, String JSONText)

or

public static boolean addJSONDocument(CouchDbConfig config, String JSONText)

4) Further Discussion

More discussion will be added over time if issues appear.



Menelaos Bakopoulos

Menelaos Bakopoulos

Mr. Menelaos Bakopoulos is currently pursuing his PhD both at Center for TeleInFrastruktur (CTiF) at Aalborg University (AAU) in Denmark and Athens Information Technology (AIT) in Athens, Greece. He received a Master in Information Technology and Telecommunications Systems from Athens Information Technology and a B.Sc. in Computer Science & Management Information Systems from the American College of Thessaloniki. Since April 2008 he has been a member of the Multimedia, Knowledge, and Web Technologies Group.

More Posts