code: public function getLowestFreeIndexNumber()
      {
          $query = $this->createQuery();
          $query->statement('SELECT index_number from ' . strtolower($this->objectType) . ' WHERE deleted=0');
          $index = 1;
          $results = $query->execute(true);
          if (false === is_array($results) || sizeof($results) < 1) {
              return $index;
          }
          $indexes = array();
          foreach ($results as $result) {
              $indexes[] = (integer)$result['index_number'];
          }
          for ($index = 1; $index <= sizeof($indexes) + 1; $index++) {
              if (false === in_array($index, $indexes)) {
                  break;
              }
          }
          return $index;
      }
docs: Returns the smallest index which is not used.
  
  @return integer