int 
LiteDb::checkResult(sqlite3* con, int result)
{
  switch(result)
  {
  case SQLITE_ROW:
  case SQLITE_DONE:
  case SQLITE_OK:
    return result;

  case SQLITE_ABORT:
  case SQLITE_BUSY:
  case SQLITE_LOCKED:
  case SQLITE_INTERRUPT:
    THROW1(SQLException, "locking not supported yet");
    break;
  default:
  {
    RString msg = LITESTR2STR(sqlite3_errmsg(con));
    THROW1(SQLException, msg);
    break;
  }
  }
  return result;
}
void 
ByteBufferReader::reset() 
{ 
  if (_mark == -1)
    THROW1(IOException, "ByteBufferReader: mark was not set");
  _curPos = _mark; 
}
//virtual 
RString 
XMLObjectReader::readStringImpl()
{
  int tk = _tin->nextToken();
  if (tk != XMLTokenizer::TOK_TEXT)
    THROW1(Exception, "expect Text");
  return ::acdk::net::URLDecoder().decode(_tin->element());
}
Example #4
0
/**
 *  @brief Return the number of CPUs in this cpu_set_t
 *  instance.
 *
 *  @param cpu_set The CPU set.
 *
 *  @return The number of CPUs in this cpu_set_t instance.
 */
int cpu_set_get_num_cpus(cpu_set_p cpu_set)
{
  /* error checks */
    if ( cpu_set == NULL )
        THROW1(QPipeException, "Called with NULL cpu_set_t");

    return cpu_set->cpuset_num_cpus;
}
//virtual 
RStorage 
AbstractFilterWriter::getStorage()
{
  if (instanceof(_out, Storage) == true)
    return RStorage(_out);
  else if (instanceof(_out, FilterWriter) == true) {
    return RFilterWriter(_out)->getStorage();
  }
  THROW1(IOException, "Unknown Writer type for getOut" + ((RObject)_out)->toString());
  return Nil;
}
//virtual 
RStorage 
AbstractFilterReader::getStorage()
{
  if (instanceof(_in, Storage) == true)
    return RStorage(_in);
  else if (instanceof(_in, FilterReader) == true)
    return RFilterReader(_in)->getStorage();
  // Interface Writer to RefHolder<Object>

  THROW1(IOException, "Unknown Reader type for getIn" + ((RObject)_in)->toString());
  return Nil;
}
Example #7
0
/*! \fn wrapper function of the residual Function
 *   non-linear solver calls this subroutine fcn(n, x, fvec, iflag, data)
 *
 *
 */
static int wrapper_fvec_hybrj(integer* n, double* x, double* f, double* fjac, integer* ldjac, integer* iflag, void* data, int sysNumber)
{
  int i;
  NONLINEAR_SYSTEM_DATA* systemData = &(((DATA*)data)->simulationInfo.nonlinearSystemData[sysNumber]);
  DATA_HYBRD* solverData = (DATA_HYBRD*)(systemData->solverData);
  int continuous = ((DATA*)data)->simulationInfo.solveContinuous;

  switch(*iflag)
  {
  case 1:
    /* re-scaling x vector */
    if(solverData->useXScaling)
      for(i=0; i<*n; i++)
        x[i] = x[i]*solverData->xScalefactors[i];

    /* call residual function */
    (systemData->residualFunc)(data, x, f, iflag);

    /* Scaling x vector */
    if(solverData->useXScaling)
      for(i=0; i<*n; i++)
        x[i] = (1.0/solverData->xScalefactors[i]) * x[i];
    break;

  case 2:
    /* set residual function continuous for jacobian calculation */
    if(continuous)
      ((DATA*)data)->simulationInfo.solveContinuous = 0;

    /* call apropreated jacobain function */
    if(systemData->jacobianIndex != -1)
      getAnalyticalJacobian(data, fjac, sysNumber);
    else
      getNumericalJacobian(data, fjac, x, f, sysNumber);

    /* reset residual function again */
    if(continuous)
      ((DATA*)data)->simulationInfo.solveContinuous = 1;
    break;

  default:
    THROW1("Well, this is embarrasing. The non-linear solver should never call this case.%d", *iflag);
    break;
  }
  
  return 0;
}
Example #8
0
bool page::read_full_page(int fd) {
    
    /* create an aligned array of bytes we can read into */
    void* aligned_base;
    guard<char> ptr =
        (char*)aligned_alloc(page_size(), 512, &aligned_base);
    assert(ptr != NULL);


    /* read bytes over 'this' */
    /* read system call may return short counts */
    ssize_t size_read = rio_readn(fd, aligned_base, page_size());


    /* check for error */
    if (size_read == -1)
        THROW2(FileException, "::read failed %s", strerror(errno));

    
    /* check for end of file */
    if (size_read == 0)
        return false;


    /* rio_readn ensures we read the proper number of bytes */
    /* save page attributes that we'll be overwriting */
    page_pool* pool  = _pool;
    memcpy(this, aligned_base, size_read);
    _pool = pool;

    
    /* more error checking */
    if ( (page_size() != (size_t)size_read) ) {
        /* The page we read does not have the same size as the
           page object we overwrote. Luckily, we used the object
           size when reading, so we didn't overflow our internal
           buffer. */
        TRACE(TRACE_ALWAYS,
              "Read %zd byte-page with internal page size of %zd bytes. "
              "Sizes should all match.\n",
              size_read,
              page_size());
        THROW1(FileException, "::read read wrong size page");
    }
    
    return true;
}
Example #9
0
/**
 *  @brief Return the specified CPU from the cpu_set_t
 *  instance.
 *
 *  @param cpu_set The CPU set.
 *
 *  @param index The index of the CPU. Must be between 0 and the
 *  result of cpu_set_get_num_cpus(cpu_set).
 *
 *  @return NULL on error. On success, returns the specified CPU.
 */
cpu_t cpu_set_get_cpu(cpu_set_p cpu_set, int index)
{

  /* error checks */
    if ( cpu_set == NULL )
        THROW1(QPipeException, "Called with NULL cpu_set_t");
    
    if ( index < 0 )
        THROW2(OutOfRange, "Called with negative index %d\n", index);
    if ( index >= cpu_set->cpuset_num_cpus )
        THROW3(OutOfRange, 
                        "Called with index %d in a cpu_set_t with %d CPUs\n",
                        index,
                        cpu_set->cpuset_num_cpus);
  
    return &cpu_set->cpuset_cpus[index];
}
Example #10
0
bool page::fread_full_page(FILE* file) {

    // save page attributes that we'll be overwriting
    size_t size = page_size();
    TRACE(0&TRACE_ALWAYS, "Computed page size as %d\n", (int)size);
    page_pool* pool = _pool;

    // write over this page
    size_t size_read = ::fread(this, 1, size, file);
    _pool = pool;
    
    // Check for error
    if ( (size_read == 0) && !feof(file) )
        THROW2(FileException, "::fread failed %s", strerror(errno));
    
    // We expect to read either a whole page or no data at
    // all. Anything else is an error.
    if ( (size_read == 0) && feof(file) )
        // done with file
        return false;

    // check sizes match
    if ( (size != size_read) || (size != page_size()) ) {
        // The page we read does not have the same size as the
        // page object we overwrote. Luckily, we used the object
        // size when reading, so we didn't overflow our internal
        // buffer.
        TRACE(TRACE_ALWAYS,
              "Read %zd byte-page with internal page size of %zd bytes into a buffer of %zd bytes. "
              "Sizes should all match.\n",
              size_read,
              page_size(),
              size);
        THROW1(FileException, "::fread read wrong size page");
    }

    return true;
}
Example #11
0
//virtual 
void 
AORB::run()
{
  if (_serverSocket == Nil)
    THROW1(Exception, "_serverSocket is not initialized! No Object registered?");
  //RServerSocket theSocket = new ServerSocket(_port);
  _isServer = true;
  //_serverSocket->setSoTimeout(1000);
  while (_doShutdown() == false) 
  {
    RSocket clsock = _serverSocket->accept();
    
    if (_doShutdown() == true)
      break;

    if (clsock)
    {
      RClientConnection client_thread = new ClientConnection(this, clsock);
      client_thread->start();
    }
  }
  _serverSocket->close();
  _isServer = false;
}
RSyntaxNode 
SyntaxParseNode_parseToTree(String::iterator* it, String::iterator end)
{
  int tk;
  SyntaxScanner scanner(MKSTR(*it, end), it, end);
  RSyntaxNode curNode = new SyntaxFollow();

  while ((tk = scanner.nextToken()) != Eof)
  {
    switch(tk)
    {
    case EoStatement:
      return curNode;
    case Identifier:
    {
      RString id = scanner.curTokStr();
      if (id->equals("error") == true)
      {
        tk = scanner.nextToken();
        if (tk != GroupStart)
          ; // throw
        tk = scanner.nextToken();
        if (tk != StringLit)
          ; // throw
        RString arg = scanner.curTokStr();
        tk = scanner.nextToken();
        if (tk != GroupStart)
          ; // throw
        curNode->push_back(new SyntaxError(arg));
        break;
      }
      curNode->push_back(new SyntaxRule(scanner.curTokStr()));
      break;
    }
    case Constant:
      curNode->push_back(new SyntaxKeyword(scanner.curTokStr()));
      break;
    case Or:
    {

      RSyntaxNode sor = new SyntaxOr();
      sor->push_back(curNode);
      sor->push_back(SyntaxParseNode_parseToTree(it, end));
      //curNode = sor;
      return sor;
      break;
    }
    case OptStart:
    {
      RSyntaxNode opt = new SyntaxOptional();
      opt->push_back(SyntaxParseNode_parseToTree(it, end));
      curNode->push_back(opt);
      break;
    }
    case OptEnd:
      return curNode;
    case GroupStart:
    {
      curNode->push_back(SyntaxParseNode_parseToTree(it, end));
      break;
    }
    case GroupEnd:
      return curNode;
    case ZeroOrMore:
    {
      RSyntaxNode n = new SyntaxZeroOrMore();
      n->push_back(curNode->pop_back());
      curNode->push_back(n);
      break;
    }
    case OneOrMore:
    {
      RSyntaxNode n = new SyntaxOneOrMore();
      n->push_back(curNode->pop_back());
      curNode->push_back(n);
      break;
    }
    case EvalBlock:
      curNode->push_back(new SyntaxEval(scanner.curTokStr()));
      break;
    
    case SaveRule:
      curNode->push_back(new SyntaxSave());
      break;
    case SaveRuleSub:
      curNode->push_back(new SyntaxSaveIfSub());
      break;
    case HideSubRule:
      curNode->push_back(new SyntaxHideSubRule());
    case GrammarCommit:
    {
      curNode->push_back(new SyntaxCommit());
      break;
    }
    case RegExpToken:
    default:
      THROW1(Error, "unknown token in syntax grammar");
      break;
    }
  }
  return curNode;
}
void 
LiteDb::_checkOpenDb()
{
  if (_con == 0)
    THROW1(SQLException, "connection is closed");
}
Example #14
0
/**
 * @brief Get a page from the tuple_fifo.
 *
 * @return NULL if the tuple_fifo has been closed. A page otherwise.
 */
void tuple_fifo::_flush_write_page(bool done_writing) {

    // after the call to send_eof() the write page is NULL
    assert(!is_done_writing());
    
    // * * * BEGIN CRITICAL SECTION * * *
    critical_section_t cs(_lock);
    _termination_check();


    switch(_state.current()) {
    case tuple_fifo_state_t::IN_MEMORY: {
        
            
        /* Wait for space to free up if we are using a "no flush"
           policy. */
        if (!FLUSH_TO_DISK_ON_FULL) {
            /* tuple_fifo stays in memory */
            /* If the buffer is currently full, we must wait for space to
               open up. Once we start waiting we continue waiting until
               space for '_threshold' pages is available. */
            for(size_t threshold=1;
                _available_in_memory_writes() < threshold; threshold = _threshold) {
                /* wait until something important changes */
                wait_for_reader();
                _termination_check();
            }
        }


        /* At this point, we don't have to wait for space anymore. If
           we still don't have enough space, it must be because we are
           using a disk flush policy. Check whether we can proceed
           without flushing to disk. */
        if (_available_in_memory_writes() >= 1) {
            
            /* Add _write_page to other tuple_fifo pages unless
               empty. */
            if(!_write_page->empty()) {
                _pages.push_back(_write_page.release());
                _pages_in_memory++;
                _pages_in_fifo++;
            }
            
            /* Allocate a new _write_page if necessary. */
            if(done_writing) {
                /* Allocation of a new _write_page is not necessary
                   (because we are done writing). Just do state
                   transition. */
                _state.transition(tuple_fifo_state_t::IN_MEMORY_DONE_WRITING);
                _write_page.done();
            }
            else
                _write_page = _alloc_page();
            
            /* wake the reader if necessary */
            if(_available_in_memory_reads() >= _threshold || is_done_writing())
                ensure_reader_running();
            
            break;
        }


        /* If we are here, we need to flush to disk. */
        /* Create on disk file. */
        c_str filepath = tuple_fifo_directory_t::generate_filepath(_fifo_id);
        _page_file = fopen(filepath.data(), "w+");
        assert(_page_file != NULL);
        if (_page_file == NULL)
            THROW2(FileException,
                   "fopen(%s) failed", filepath.data());
        TRACE(TRACE_ALWAYS, "Created tuple_fifo file %s\n",
              filepath.data());
        
        /* Append this page to _pages and flush the entire
           page_list to disk. */
        if(!_write_page->empty()) {
            _pages.push_back(_write_page.release());
            _pages_in_memory++;
            _pages_in_fifo++;
        }
        for (page_list::iterator it = _pages.begin(); it != _pages.end(); ) {
            qpipe::page* p = *it;
            p->fwrite_full_page(_page_file);

            /* done with page */
            p->clear();
            _free_pages.push_back(p);
            it = _pages.erase(it);

            assert(_pages_in_memory > 0);
            _pages_in_memory--;
        }
        fflush(_page_file);
        
        /* update _file_head_page */
        assert(_file_head_page == 0);
        _file_head_page = _next_page;

        _state.transition(tuple_fifo_state_t::ON_DISK);
        if (done_writing) {
            /* transition again! */
            _state.transition(tuple_fifo_state_t::ON_DISK_DONE_WRITING);
            _write_page.done();
        }
        else {
            /* allocate from free list */
            assert(!_free_pages.empty());
            _write_page = _alloc_page();

            /* TODO It's clear whether we want to replace the
               SENTINAL_PAGE here. On the one hand, if we can replace
               it, we can free the rest of the pages in the free
               list. On the other, we still need to check for the
               SENTINAL_PAGE in _get_read_page since pages may be
               removed using get_page (instead of tuples removed with
               get_tuple). */
            if (_read_page == SENTINEL_PAGE) {
                _set_read_page(_alloc_page());
                /* After this point, we should not release either of
                   these pages. */
            }
        }

        /* wake the reader if necessary */
        if(_available_fifo_reads() >= _threshold || is_done_writing())
            ensure_reader_running();

        break;

    } /* endof case: IN_MEMORY */
        
    case tuple_fifo_state_t::ON_DISK: {

        int fseek_ret = fseek(_page_file, 0, SEEK_END);
        assert(!fseek_ret);
        if (fseek_ret)
            THROW1(FileException, "fseek to EOF");
        _write_page->fwrite_full_page(_page_file);
        fflush(_page_file);
        _pages_in_fifo++;

        if (done_writing) {
            _state.transition(tuple_fifo_state_t::ON_DISK_DONE_WRITING);
            _write_page.done();
        }
        else {
            /* simply reuse write page */
            _write_page->clear();
        }
        
        /* wake the reader if necessary */
        if(_available_fifo_reads() >= _threshold || is_done_writing())
            ensure_reader_running();

        break;
    }

    default:
        unreachable();

    } /* endof switch statement */


    // * * * END CRITICAL SECTION * * *
}