Example APP- putting all together

Let's look at the code from start to finish for an app for BangDB where various database objects are used. Later in detail each of these objects will be discussed

	//on linux
	const char *dbPath = "/home/bangdb/data/";
	
	const char *logdbPath = "/home/bangdb/data/dblog";
	
	/*
	//or on windows
	const char *dbPath = "c:\\bangdbdata\\";  //(Or "c:\bangdbdata\";)
	database *db = new database("mydb", NULL, DB_TRANSACTION_NONE, dbPath, logPath);
	//for windows, pass the dbPath as last extra param above
	*/
	
	if(!db)
	{
		bangdb_logger("database create failed");
		return -1;
	}
	table *tbl1 = db->gettable("mytable1");
	if(!tbl1)
	{
		bangdb_logger("table %s created", "mytable1");
		return -1;
	}
	table_env tenv;
	tenv.set_persist_type(INMEM_ONLY);
	tenv.set_idx_type(EXTHASH);
	tenv.set_log_state(false);
	
	table *tbl2 = db->gettable("mytable2", OPENCREATE, &tenv);
	if(!tbl2)
	{
		bangdb_logger("table %s create failed", "mtytable2");
		return -1;
	}
	
	//get a wideTable
	
	tenv.reset();
	tenv.set_persist_type(INMEM_PERSIST);
	tenv.set_idx_type(BTREE);
	tenv.set_log_state(true);
	
	wideTable *wtbl = db->getWideTable("wide_table", OPENCREATE, &tenv);
	if(!wtbl)
	{
		bangdb_logger("table %s create failed", "wide_table");
		return -1;
	}
	
	connection *conn11 = tbl1->getconnection();
	connection *conn21 = tbl2->getconnection();
	
	wideConnection *wconn = wtbl->getconnection();
	
	//some oprations..
	
	//close conn11 and get rid of table tbl1
	
	conn11->closeconnection();
	delete conn11;
	tbl1->closetable();
	delete tbl1;

	bangdb_logger("table %s closed", "mytable1");
	
	//some ops with conn21 (for tbl2) ...
	
	//some ops with wconn (for wtbl) ...
	
	//finally close all resources and db	
	
	conn21->closeconnection();
	delete conn21;
	wconn->closeconnection();
	delete wconn;
	tbl2->closetable();
	delete tbl2;
	wtbl->closetable();
	delete wtbl;
	db->closedatabase();
	delete db;
	
	bangdb_logger("database closed");
	return 0;
	
	//note that we close all created resources and delete their handles before closing the database
	//this is good practice to follow even though internally db closes all resources before closing
	//the db, but many of the times it's desired to close connections and tables when done to free
	//up resources. Hence it's a good practice to close each and every resources (conn, tbls etc...)
	//before closing the db
	
	string dbpath = "c:\\BangDB\\data\\";
	string logPath = "c:\\BangDB\\dblog\\";
	Database db = new Database("mydb", config_path, TransactionType.DBOTransactionNone, dbpath, logPath);
	
	//note that we can pass null for dbpath and logpath, in that case
	//db will read the value of these paths from bangdb.config fle
	
	if(db == null)
	{
		Common.BangDBLogger("database create failed");
		return -1;
	}
	Table tbl1 = db.GetTable("mytable1");
	if(tbl1 == null)
	{
		Common.BangDBLogger("table mtytable1 created");
		return -1;
	}
	TableEnv tenv = new TableEnv();
	tenv.persistType = PersistType.InmemOnly;
	tenv.indexType = IndexType.ExtHash;
	tenv.walState = WALState.Disabled;
	
	Table tbl2 = db.GetTable("mytable2", DBOpenType.OpenCreate, tenv);
	if(tbl2 == null)
	{
		Common.BangDBLogger("table mytable2 create failed");
		return -1;
	}
	
	//get a wide table
	
	tenv.Reset();
	tenv.persistType = PersistType.InmemPersist;
	tenv.indexType = IndexType.Btree;
	tenv.walState - WALState.Enabled;
	
	WideTable wtbl = db.GetWideTable("wide_table", DBOpenType.Opencreate, tenv);
	if(wtbl == null)
	{
		Common.BangDBLogger("table wide_table create failed");
		return -1;
	}
	
	Connection conn11 = tbl1.GetConnection();
	Connection conn21 = tbl2.GetConnection();
	
	WideConnection wconn = wtbl.GetConnection();
	
	//some oprations..
	
	//close conn11 and get rid of table tbl1
	conn11.CloseConnection();
	tbl1.CloseTable();
	
	//some ops with conn21 (for tbl2) ...
	
	//some ops with wconn (for wtbl) ...
	
	//finally close resources and db
	conn21.CloseConnection();
	wconn.CloseConnection();
	tbl2.CloseTable();
	wtbl.CloseTable();
	db.CloseDatabase();
	
	//note that we close all created resources and delete their handles before closing the database
	//this is good practice to follow even though internally db closes all resources before closing
	//the db, but many of the times it's desired to close connections and tables when done to free
	//up resources. Hence it's a good practice to close each and every resources (conn, tbls etc...)
	//before closing the db
	
	//for windows
	String dbPath = "c:\\BangDB\\data\\";
	String logPath = "c:\\BangDB\\dblog\\";
	
	/*
	//for linux
	String dbPath = "/home/userName/bangdbdata/";
	String logPath = "/home/userName/dblogPath/";
	 */
	 
	Database db = new DatabaseImpl("mydb", null, TransactionType .DB_MULTIOPS_TRANSACTION_NONE, dbPath, logPath);
	
	//for windows, pass the dbPath as last extra param above
	
	if(db == null)
	{
		BangDBCommon.bangdb_logger("database create failed");
		return -1;
	}
	
	Table tbl1 = db.getTable("mytable1", DBAccess.OPENCREATE, null);
	if(tbl1 == null)
	{
		BangDBCommon.bangdb_logger("table mtytable1 created");
		return -1;
	}
	
	tenv = new TableEnv();
	tenv.setPersistType(PersistType.INMEM_ONLY);
	tenv.setIndexType(IndexType.EXTHASH);
	tenv.setLogState(false);
	
	Table tbl2 = db.getTable("mytable2", DBAccess.OPENCREATE, null);
	if(tbl2 == null)
	{
		BangDBCommon.bangdb_logger("table mytable2 create failed");
		return -1;
	}
	
	//get a wide table
	
	tenv.reset();
	tenv.setPersistType(PersistType.INMEM_ONLY);
	tenv.setIndexType(IndexType.BTREE);
	tenv.setLogState(true);
	WideTable wtbl = db.getWideTable("wide_table", DBAccess.OPENCREATE, tenv);
	if(wtbl == null)
	{
		BangDBCommon.bangdb_logger("table wide_table create failed");
		return -1;
	}
	
	Connection conn11 = tbl1.getConnection();
	Connection conn21 = tbl2.getConnection();
	
	WideConnection wconn = wtbl.getConnection();
	
	//some oprations..
	
	//close conn11 and get rid of table tbl1
	
	conn11.closeConnection();
	tbl1.closeTable(DBClose.DEFAULT);
	
	//some ops with conn21 (for tbl2) ...
	
	//some ops with wconn (for wtbl) ...
	
	//finally close resources and db
	
	conn21.closeConnection();
	wconn.closeConnection();
	tbl2.closeTable();
	wtbl.closeTable();
	
	db.closeDatabase();
	
	//note that we close all created resources and delete their handles before closing the database
	//this is good practice to follow even though internally db closes all resources before closing
	//the db, but many of the times it's desired to close connections and tables when done to free
	//up resources. Hence it's a good practice to close each and every resources (conn, tbls etc...)
	//before closing the db
	

The details of the objects are discussed in sub topics