HelloWorld in Objectivity/C++

Description:

This quintessential helloworld program shows the basics of creating a federated database and a schema, and includes basic application that accesses the database. The application creates a database, creates a container in the database, adds objects in the container, and then retrieves these objects via a name lookup.

See it work:
Console Output

Welcome to the Objectivity HelloWorld Example
No Objectivity FD Bootfile passed as argument; using default: ../../data/helloWorld/HelloWorld.boot
Create database
Create HelloObject and set message
Looking for object with name HelloWorld
Found it!: HelloWorld

HelloWorld test has PASSED

Review Source Code:
SOURCE FILE: main.cpp


#include <iostream>

using namespace std;
#include <ooObjy.h>
#include "../ddlFiles/HelloObject.h"
//-----------------------------------------------------------------------------
int main (int argc, char** argv)
{

  bool errors = true;
  cout << "Welcome to the Objectivity HelloWorld Example" << endl;

  const char* bootfile = "../../data/helloWorld/HelloWorld.boot";

  if (argc == 1)
  {

    cout << "No Objectivity FD Bootfile passed as argument; using default: " 
      << bootfile << endl;
  }
  else
  {

    bootfile = argv[1];
    cout << "Using FD Bootfile passed as argument: " << bootfile << endl;
  }

  int count = 0;
  try
  {

    //
    //	initialize Objectivity/DB
    //
    ooObjy::startup();
    //
    //	establish connection to the Federation
    //
    ooConnection* connection;

    connection = ooObjy::getConnection(bootfile);
    //
    // create a session
    //
    ooSession* session;

    session = connection->createSession("Main Session");
    //
    //  begin an update transaction
    //
    session->begin(oocUpdate);

    //
    //  get a reference to the federated database
    //
    ooHandle(ooFDObj) fd;
    fd = session->fd();

    //
    //  if the database exists open for update and delete it
    //
    char* dbName = "TestDB";
    ooHandle(ooDBObj) db;

    if (db.exist(fd,dbName, oocUpdate))
    {
      cout << "Database " << dbName << " exists; will delete and re-create" << endl;

      ooDelete(db);
    }
    cout << "Create database" << endl;
    //
    //  create a new database
    //

    db = new(fd) ooDBObj(dbName);
    //
    //  create a non-hashed container, with default initial size and growth
    //

    ooHandle(ooContObj) cont;
    cont = new("TestCont",0,0,0,db) ooContObj;

    //
    //  create a Node object, and set its location
    //
    cout << "Create HelloObject and set message" << endl;
    const char name[16] = "HelloWorld";

    ooHandle(HelloObject) hello;
    hello = new(cont) HelloObject;

    hello->setHelloMsg(name);
    //
    //  and name it in the scope of the database
    //
    hello.nameObj(db, name);

    //
    //  commit the transaction
    //
    session->commit();
    //
    //	begin a read transaction
    //
    session->begin(oocRead);

    //
    //	lookup the Node object with name "HelloWorld" in the scope of the database
    //
    cout << "Looking for object with name " << name << endl;

    ooHandle(HelloObject) hello2;

    if (hello2.lookupObj(db, name))
    {
      cout << "Found it!: " << hello2->getHelloMsg() << endl;

      //
      //	commit the transaction
      //
      session->commit();
      //
      // everything worked so far
      //
      errors = false;
    }

    else
    {
      cerr << "HelloObject not found; will abort" << endl;
      session->abort();

      errors = true;
    }
    //
    //	terminate interaction with Objectivity/DB
    //
    ooObjy::shutdown();
  }
  catch ( const ooException & ex )
  {

    cerr << "exception in main: " << ex.what() << endl;
    errors = true;
  }

  catch(...)
  {
    cerr << "unknown exception in main" << endl;
    errors = true;
  }

  if (errors)
  {
    cerr << endl << "HelloWorld test has FAILED" << endl;
  }

  else
  {
    cout << endl << "HelloWorld test has PASSED" << endl;
  }

  return 0;
}
DDL FILE: helloWorld.ddl

#include <iostream> 
// Class HelloObject represents the HelloWorld object
class HelloObject : public ooObj
{
 public:

  // constructor
  HelloObject();

  // returns the current helloMsg value
  const char* getHelloMsg() const;

  // sets the helloMsg value to the provided value
  void setHelloMsg(const char* value);

 private:

  ooVString mHelloMsg;
    
};

//Class Functions
HelloObject::HelloObject()
   :mHelloMsg(0)
{
}

//------------------------
const char* 
HelloObject::getHelloMsg() const
{
  return mHelloMsg.head()? mHelloMsg.head() : "";
}

//------------------------
void 
HelloObject::setHelloMsg(const char* value)
{
  mHelloMsg = value;
}


 

Download:

Download Source
C++