c++/ZDX/ZDXdb.cpp
//////////////////////////////////////////////////////////////////////////////////////////////////
//
// creation, open, connect, disconnect of new ZDXdb instances
//
//////////////////////////////////////////////////////////////////////////////////////////////////
creation, open, connect, disconnect of new ZDXdb instances
creation open connect disconnect of new ZDXdb instances
ZDX::ZDXdb*   ZDX::ZDXdb::create_new(string path, string magic) {
  ZDXdb*  zdxdb = new ZDX::ZDXdb();
  zdxdb->_zdx_path = path;
  strncpy(zdxdb->_header.magic, magic.c_str(), 15); 
  boost::uuids::uuid u = boost::uuids::uuid(boost::uuids::random_generator()());
  int i=0;
  boost::uuids::uuid::const_iterator i_data;
  for(i_data = u.begin(); i_data!=u.end(); ++i_data, ++i) {
    zdxdb->_header.uuid[i] = static_cast<unsigned char>(*i_data);
  }
  zdxdb->_zdx_fd = open(path.c_str(), O_CREAT | O_TRUNC | O_RDWR, 0664);
  if(zdxdb->_zdx_fd == -1) { 
    fprintf(stderr, "zdx error : can't create file [%s]\n", path.c_str()); 
    zdxdb->release();
    return NULL; 
  }
  struct flock fl;
  fl.l_type   = F_WRLCK;  
  fl.l_whence = SEEK_SET; 
  fl.l_start  = 0;        
  fl.l_len    = 0;        
  fl.l_pid    = getpid();   
  fcntl(zdxdb->_zdx_fd, F_SETLKW, &fl);  
  struct stat stbuf;
  if(fstat(zdxdb->_zdx_fd, &stbuf) == 0) {
    if(stbuf.st_size == 0) {
      write(zdxdb->_zdx_fd, &(zdxdb->_header), sizeof(zdx_header));
      fprintf(stderr, "ZDXdb::create_new %s - got write lock\n", path.c_str());
    }
  }
  fl.l_type   = F_UNLCK;         
  fcntl(zdxdb->_zdx_fd, F_SETLK, &fl);  
  zdxdb->disconnect();
  return zdxdb;
}
ZDX :: ZDXdb * ZDX :: ZDXdb :: create_new ( string path , string magic ) { ZDXdb * zdxdb = new ZDX :: ZDXdb ( ) ; zdxdb -> _zdx_path = path ; strncpy ( zdxdb -> _header . magic , magic . c_str ( ) , 15 ) ; boost :: uuids :: uuid u = boost :: uuids :: uuid ( boost :: uuids :: random_generator ( ) ( ) ) ; int i = 0 ; boost :: uuids :: uuid :: const_iterator i_data ; for ( i_data = u . begin ( ) ; i_data != u . end ( ) ; ++ i_data , ++ i ) { zdxdb -> _header . uuid [ i ] = static_cast < unsigned char > ( * i_data ) ; } zdxdb -> _zdx_fd = open ( path . c_str ( ) , O_CREAT | O_TRUNC | O_RDWR , 0664 ) ; if ( zdxdb -> _zdx_fd == -1 ) { fprintf ( stderr , " \n " , path . c_str ( ) ) ; zdxdb -> release ( ) ; return NULL ; } struct flock fl ; fl . l_type = F_WRLCK ; fl . l_whence = SEEK_SET ; fl . l_start = 0 ; fl . l_len = 0 ; fl . l_pid = getpid ( ) ; fcntl ( zdxdb -> _zdx_fd , F_SETLKW , & fl ) ; struct stat stbuf ; if ( fstat ( zdxdb -> _zdx_fd , & stbuf ) == 0 ) { if ( stbuf . st_size == 0 ) { write ( zdxdb -> _zdx_fd , & ( zdxdb -> _header ) , sizeof ( zdx_header ) ) ; fprintf ( stderr , " \n " , path . c_str ( ) ) ; } } fl . l_type = F_UNLCK ; fcntl ( zdxdb -> _zdx_fd , F_SETLK , & fl ) ; zdxdb -> disconnect ( ) ; return zdxdb ; }
creation, open, connect, disconnect of new ZDXdb instances
creation open connect disconnect of new ZDXdb instances
//\0 terminated //printf("%d [%d]\n", i, bytes[i]); //create file and write header //error //use write lock and fstat to see if there is race condition on creating file //if I got the first write-lock the file will be zero size /* F_RDLCK, F_WRLCK, F_UNLCK    */ /* SEEK_SET, SEEK_CUR, SEEK_END */ /* Offset from l_whence         */ /* length, 0 = to EOF           */ /* our PID                      */ // set lock and wait // write out zdx_header  //unlock /* tell it to unlock the region */ /* set the region to unlocked */