2007-10-16 17:12:48 +00:00
< ? php
2007-10-26 21:14:35 +00:00
require_once ( " ../inc/db_conn.inc " );
2008-06-17 17:38:02 +00:00
require_once ( " ../inc/util_basic.inc " );
2007-10-21 20:54:24 +00:00
2007-10-26 17:04:46 +00:00
class BossaDb extends DbConn {
public static $instance ;
static function get () {
if ( ! isset ( $instance )) {
2007-10-30 22:31:13 +00:00
$config = get_config ();
$name = parse_config ( $config , '<bossa_db_name>' );
2008-06-29 20:57:21 +00:00
if ( ! $name ) {
$name = parse_config ( $config , '<db_name>' );
$user = parse_config ( $config , '<db_user>' );
$passwd = parse_config ( $config , '<db_passwd>' );
$host = parse_config ( $config , '<db_host>' );
} else {
$user = parse_config ( $config , '<bossa_db_user>' );
$passwd = parse_config ( $config , '<bossa_db_passwd>' );
$host = parse_config ( $config , '<bossa_db_host>' );
}
2007-10-30 22:31:13 +00:00
if ( $host == null ) {
$host = " localhost " ;
}
2007-10-26 17:04:46 +00:00
$instance = new DbConn ();
2007-10-30 22:31:13 +00:00
$retval = $instance -> init_conn ( $user , $passwd , $host , $name );
2007-10-26 21:14:35 +00:00
if ( ! $retval ) return null ;
2007-10-26 17:04:46 +00:00
}
return $instance ;
}
2008-02-11 23:38:31 +00:00
static function escape_string ( $string ) {
$db = self :: get ();
return $db -> base_escape_string ( $string );
}
2008-07-14 19:13:19 +00:00
static function start_transaction () {
2008-07-20 21:27:44 +00:00
//echo "start transaction\n";
2008-07-17 20:58:42 +00:00
return ;
2008-07-14 19:13:19 +00:00
$db = BossaDb :: get ();
$db -> do_query ( " start transaction " );
}
static function commit () {
2008-07-20 21:27:44 +00:00
//echo "commit\n";
2008-07-17 20:58:42 +00:00
return ;
2008-07-14 19:13:19 +00:00
$db = BossaDb :: get ();
$db -> do_query ( " commit " );
}
2007-10-26 17:04:46 +00:00
}
2008-07-17 20:58:42 +00:00
// use this as a local to ensure that transaction is committed
// regardless of exceptions
//
class BossaTransaction {
function __construct () {
BossaDb :: start_transaction ();
}
function __destruct () {
BossaDb :: commit ();
}
}
2008-02-13 04:25:52 +00:00
class BossaUser {
static $cache ;
static function lookup_userid ( $id ) {
$db = BossaDb :: get ();
return $db -> lookup ( 'bossa_user' , 'BossaUser' , " user_id= $id " );
}
static function insert ( $clause ) {
$db = BossaDb :: get ();
return $db -> insert ( 'bossa_user' , $clause );
}
static function lookup ( & $user ) {
if ( ! $user ) return ;
if ( isset ( $user -> bossa )) return ;
if ( isset ( self :: $cache [ $user -> id ])) {
$bossa = self :: $cache [ $user -> id ];
} else {
$bossa = self :: lookup_userid ( $user -> id );
if ( ! $bossa ) {
self :: insert ( " (user_id) values ( $user->id ) " );
$bossa = self :: lookup_userid ( $user -> id );
}
self :: $cache [ $user -> id ] = $bossa ;
}
$user -> bossa = $bossa ;
}
function update ( $clause ) {
$db = BossaDb :: get ();
$clause = " $clause where user_id= $this->user_id " ;
return $db -> update_aux ( 'bossa_user' , $clause );
}
2008-07-17 20:58:42 +00:00
// app-callable:
//
2008-07-30 21:37:41 +00:00
function get_opaque_data () {
2008-07-17 20:58:42 +00:00
return unserialize ( $this -> info );
}
2008-07-30 21:37:41 +00:00
function set_opaque_data ( $info ) {
2008-07-17 20:58:42 +00:00
$info = serialize ( $info );
$this -> update ( " info=' $info ' " );
}
2008-02-13 04:25:52 +00:00
}
2007-10-26 17:04:46 +00:00
class BossaApp {
2008-07-13 04:26:23 +00:00
static function insert ( $clause ) {
2007-10-26 17:04:46 +00:00
$db = BossaDb :: get ();
2008-02-11 23:38:31 +00:00
return $db -> insert ( 'bossa_app' , $clause );
2007-10-16 17:12:48 +00:00
}
2008-07-13 04:26:23 +00:00
static function lookup ( $clause ) {
2007-10-26 17:04:46 +00:00
$db = BossaDb :: get ();
2008-07-13 04:26:23 +00:00
return $db -> lookup ( 'bossa_app' , 'BossaApp' , $clause );
2007-10-16 17:12:48 +00:00
}
2007-10-16 23:02:13 +00:00
static function lookup_id ( $id ) {
2007-10-26 17:04:46 +00:00
$db = BossaDb :: get ();
return $db -> lookup_id ( $id , 'bossa_app' , 'BossaApp' );
2007-10-16 17:12:48 +00:00
}
2007-10-21 20:54:24 +00:00
static function enum () {
2007-10-26 17:04:46 +00:00
$db = BossaDb :: get ();
return $db -> enum ( 'bossa_app' , 'BossaApp' );
2007-10-21 20:54:24 +00:00
}
2008-02-15 15:25:44 +00:00
function update ( $clause ) {
$db = BossaDb :: get ();
return $db -> update ( $this , 'bossa_app' , $clause );
}
2007-10-16 23:02:13 +00:00
}
2007-10-16 17:12:48 +00:00
2008-07-15 21:43:45 +00:00
// values for bossa_job.state
//
define ( " BOSSA_JOB_EMBARGOED " , 0 );
define ( " BOSSA_JOB_IN_PROGRESS " , 1 );
define ( " BOSSA_JOB_DONE " , 2 );
define ( " BOSSA_JOB_INCONCLUSIVE " , 3 );
2007-10-26 17:04:46 +00:00
class BossaJob {
2008-07-13 04:26:23 +00:00
static function insert ( $clause ) {
2007-10-26 17:04:46 +00:00
$db = BossaDb :: get ();
2008-07-13 04:26:23 +00:00
$ret = $db -> insert ( 'bossa_job' , $clause );
if ( ! $ret ) return 0 ;
return $db -> insert_id ();
2007-10-16 17:12:48 +00:00
}
2007-10-16 23:02:13 +00:00
function update ( $clause ) {
2007-10-26 17:04:46 +00:00
$db = BossaDb :: get ();
2008-02-12 15:04:27 +00:00
return $db -> update ( $this , 'bossa_job' , $clause );
2007-10-16 23:02:13 +00:00
}
static function lookup_id ( $id ) {
2007-10-26 17:04:46 +00:00
$db = BossaDb :: get ();
return $db -> lookup_id ( $id , 'bossa_job' , 'BossaJob' );
2007-10-16 23:02:13 +00:00
}
2007-10-21 20:54:24 +00:00
static function enum ( $clause ) {
2007-10-26 17:04:46 +00:00
$db = BossaDb :: get ();
return $db -> enum ( 'bossa_job' , 'BossaJob' , $clause );
2007-10-21 20:54:24 +00:00
}
2008-07-15 21:43:45 +00:00
static function count ( $clause ) {
$db = BossaDb :: get ();
return $db -> count ( 'bossa_job' , $clause );
}
2008-07-14 19:13:19 +00:00
// app-callable:
//
2008-07-30 21:37:41 +00:00
function get_opaque_data () {
2008-07-14 19:13:19 +00:00
return unserialize ( $this -> info );
}
function set_priority ( $x ) {
return $this -> update ( " priority_0= $x " );
}
2008-07-15 21:43:45 +00:00
function get_instances () {
return BossaJobInst :: enum ( " job_id = $this->id " );
}
function get_finished_instances () {
return BossaJobInst :: enum ( " job_id = $this->id and finish_time>0 " );
}
2008-07-30 21:37:41 +00:00
function set_state ( $s ) {
2008-07-15 21:43:45 +00:00
$this -> update ( " state= $s " );
}
2007-10-16 23:02:13 +00:00
}
2007-10-16 17:12:48 +00:00
2007-10-26 17:04:46 +00:00
class BossaJobInst {
2008-07-13 04:26:23 +00:00
function insert ( $clause ) {
2007-10-26 17:04:46 +00:00
$db = BossaDb :: get ();
2008-07-13 04:26:23 +00:00
$ret = $db -> insert ( 'bossa_job_inst' , $clause );
if ( ! $ret ) return 0 ;
return $db -> insert_id ();
2007-10-16 17:12:48 +00:00
}
2007-10-16 23:02:13 +00:00
static function lookup_id ( $id ) {
2007-10-26 17:04:46 +00:00
$db = BossaDb :: get ();
return $db -> lookup_id ( $id , 'bossa_job_inst' , 'BossaJobInst' );
2007-10-16 23:02:13 +00:00
}
2007-10-21 20:54:24 +00:00
static function enum ( $clause ) {
2007-10-26 17:04:46 +00:00
$db = BossaDb :: get ();
return $db -> enum ( 'bossa_job_inst' , 'BossaJobInst' , $clause );
2007-10-21 20:54:24 +00:00
}
2007-10-16 23:02:13 +00:00
function update ( $clause ) {
2007-10-26 17:04:46 +00:00
$db = BossaDb :: get ();
return $db -> update ( $this , 'bossa_job_inst' , $clause );
2007-10-16 23:02:13 +00:00
}
2007-10-16 17:12:48 +00:00
// Assign a job from the given app to the given user.
// Returns the job instance or NULL.
//
2007-10-16 23:02:13 +00:00
static function assign ( $app , $user ) {
2008-05-30 03:29:31 +00:00
$db = BossaDb :: get ();
2008-07-15 21:43:45 +00:00
2008-07-13 04:26:23 +00:00
// first look for unfinished jobs previously assigned to this user
2008-05-30 03:29:31 +00:00
//
$query = " select * from DBNAME.bossa_job_inst where app_id= $app->id and user_id= $user->id and finish_time=0 limit 1 " ;
$result = $db -> do_query ( $query );
if ( $result ) {
$ji = mysql_fetch_object ( $result , 'BossaJobInst' );
mysql_free_result ( $result );
2008-06-01 03:43:47 +00:00
if ( $ji ) return $ji ;
2008-05-30 03:29:31 +00:00
}
2008-07-15 21:43:45 +00:00
if ( $app -> calibration_frac && drand () < $app -> calibration_frac ) {
2008-07-17 20:58:42 +00:00
$query = " select * from DBNAME.bossa_job where app_id= $app->id and (select count(*) from DBNAME.bossa_job_inst where job_id=bossa_job.id and user_id= $user->id ) = 0 and state=1 and calibration=1 order by priority_0 desc limit 1 " ;
2008-07-15 21:43:45 +00:00
$result = $db -> do_query ( $query );
if ( ! $result ) return null ;
$job = mysql_fetch_object ( $result , 'BossaJob' );
if ( ! $job ) return null ;
2008-07-17 20:58:42 +00:00
$job -> update ( " priority_0=priority_0-1 " );
2008-07-15 21:43:45 +00:00
mysql_free_result ( $result );
2008-07-14 19:13:19 +00:00
} else {
2008-07-15 21:43:45 +00:00
if ( isset ( $user -> bossa -> category )) {
$prio = " priority_ " . $user -> bossa -> category ;
} else {
$prio = " priority_0 " ;
}
// skips jobs for which this user
// has already been assigned an instance
//
$query = " select * from DBNAME.bossa_job where app_id= $app->id and (select count(*) from DBNAME.bossa_job_inst where job_id=bossa_job.id and user_id= $user->id ) = 0 and state=1 and calibration=0 order by $prio desc limit 1 " ;
$result = $db -> do_query ( $query );
if ( ! $result ) return null ;
$job = mysql_fetch_object ( $result , 'BossaJob' );
if ( ! $job ) return null ;
mysql_free_result ( $result );
2008-07-14 19:13:19 +00:00
}
2007-10-16 17:12:48 +00:00
2008-02-12 15:04:27 +00:00
$now = time ();
2008-07-20 21:27:44 +00:00
$clause = " (create_time, app_id, job_id, user_id, calibration) values ( $now , $app->id , $job->id , $user->id , $job->calibration ) " ;
2008-07-13 04:26:23 +00:00
$id = BossaJobInst :: insert ( $clause );
return BossaJobInst :: lookup_id ( $id );
2007-10-16 23:02:13 +00:00
}
2008-07-14 19:13:19 +00:00
// app-callable functions
//
2008-07-30 21:37:41 +00:00
function set_opaque_data ( $info ) {
2008-07-14 19:13:19 +00:00
$info = serialize ( $info );
return $this -> update ( " info=' $info ' " );
}
2008-07-30 21:37:41 +00:00
function get_opaque_data () {
2008-07-15 21:43:45 +00:00
return unserialize ( $this -> info );
}
2008-07-17 20:58:42 +00:00
function get_user () {
$user = BoincUser :: lookup_id ( $this -> user_id );
BossaUser :: lookup ( $user );
return $user ;
}
2008-07-13 04:26:23 +00:00
}
2007-10-16 23:02:13 +00:00
2008-07-13 04:26:23 +00:00
class BossaBatch {
function insert ( $clause ) {
$db = BossaDb :: get ();
$ret = $db -> insert ( 'bossa_batch' , $clause );
if ( ! $ret ) return 0 ;
return $db -> insert_id ();
2007-10-16 23:02:13 +00:00
}
2008-07-14 22:32:20 +00:00
static function enum ( $clause ) {
$db = BossaDb :: get ();
return $db -> enum ( 'bossa_batch' , 'BossaBatch' , $clause );
}
static function lookup_id ( $id ) {
$db = BossaDb :: get ();
return $db -> lookup_id ( $id , 'bossa_batch' , 'BossaBatch' );
}
2007-10-16 17:12:48 +00:00
}
?>