Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit 85e3b55

Browse files
author
Julien Jomier
committed
ENH: Improving support for Cassandra
1 parent 9fee581 commit 85e3b55

35 files changed

+587
-284
lines changed

core/controllers/BrowseController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function indexAction()
2424
$header="";
2525

2626
$communities=$this->User->getUserCommunities($this->userSession->Dao);
27-
$communities=array_merge($communities, $this->Community->getPubicCommunities());
27+
$communities=array_merge($communities, $this->Community->getPublicCommunities());
2828

2929
$header.="<ul class='pathBrowser'>";
3030
$header.=" <li class='pathData'><a href='{$this->view->webroot}/browse'>{$this->t('Data')}</a></li>";

core/controllers/CommunityController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function indexAction()
2727
$this->view->json['community']['contentCreateLogin']=$this->t('You need to be logged in to be able to create a community.');
2828

2929
$communities=$this->User->getUserCommunities($this->userSession->Dao);
30-
$communities=array_merge($communities, $this->Community->getPubicCommunities());
30+
$communities=array_merge($communities, $this->Community->getPublicCommunities());
3131
$this->Component->Sortdao->field='name';
3232
$this->Component->Sortdao->order='asc';
3333
usort($communities, array($this->Component->Sortdao,'sortByName'));

core/models/cassandra/CommunityModel.php

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,74 @@
55
*/
66
class CommunityModel extends MIDASCommunityModel
77
{
8-
8+
function getPublicCommunities($limit=20)
9+
{
10+
if(!is_numeric($limit))
11+
{
12+
throw new Zend_Exception("Error parameter.");
13+
}
14+
15+
$communities = array();
16+
17+
// We assume we don't have a lot of communities
18+
// We get from the table emailuser
19+
try
20+
{
21+
$community = new ColumnFamily($this->database->getDB(), 'community');
22+
$allcommunities = $community->get_range("", // start
23+
"", // end
24+
$limit // row count
25+
);
26+
foreach($allcommunities as $key => $com)
27+
{
28+
if($com['privacy'] == MIDAS_COMMUNITY_PRIVATE)
29+
{
30+
$communities[] = $this->initDao('Community', $com);
31+
}
32+
}
33+
}
34+
catch(cassandra_NotFoundException $e)
35+
{
36+
return $communities;
37+
}
38+
catch(Exception $e)
39+
{
40+
throw new Zend_Exception($e);
41+
}
42+
return $communities;
43+
}
44+
45+
/** Get a community by name */
46+
function getByName($name)
47+
{
48+
// We assume we don't have a lot of communities
49+
// Otherwise we'll do an index table
50+
try
51+
{
52+
$community = new ColumnFamily($this->database->getDB(), 'community');
53+
$communitiesarray = $community->get_range();
54+
55+
foreach($communitiesarray as $key => $value)
56+
{
57+
if($value['name'] == $name)
58+
{
59+
// Add the community_id
60+
$value[$this->_key] = $key;
61+
$dao= $this->initDao('Community',$value);
62+
return $dao;
63+
}
64+
}
65+
}
66+
catch(cassandra_NotFoundException $e)
67+
{
68+
return false;
69+
}
70+
catch(Exception $e)
71+
{
72+
throw new Zend_Exception($e);
73+
}
74+
return false;
75+
} // end getByName()
976

1077
} // end class CommunityModel
1178
?>

core/models/cassandra/FeedModel.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
*/
66
class FeedModel extends MIDASFeedModel
77
{
8-
8+
protected function _getFeeds($loggedUserDao,$userDao=null,$communityDao=null,$policy=0,$limit=20)
9+
{
10+
}
911

1012
} // end class
1113
?>

core/models/cassandra/FeedpolicyuserModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
class FeedpolicyuserModel extends MIDASFeedpolicyuserModel
77
{
8-
8+
99

1010
} // end class
1111
?>

core/models/cassandra/FolderModel.php

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,16 @@
44
* \brief Cassandra Model
55
*/
66
class FolderModel extends MIDASFolderModel
7-
{
8-
/** Create a folder */
9-
function createFolder($name,$description,$parent)
10-
{
11-
if(!$parent instanceof FolderDao&&!is_numeric($parent))
12-
{
13-
throw new Zend_Exception("Should be a folder.");
14-
}
15-
if(!is_string($name)||!is_string($description))
16-
{
17-
throw new Zend_Exception("Should be a string.");
18-
}
19-
$this->loadDaoClass('FolderDao');
20-
$folder=new FolderDao();
21-
$folder->setName($name);
22-
$folder->setDescription($description);
23-
$folder->setDate(date('c'));
24-
if($parent instanceof FolderDao)
25-
{
26-
$parentId=$parent->getFolderId();
27-
}
28-
else
29-
{
30-
$parentId=$parent;
31-
}
32-
$folder->setParentId($parentId);
33-
$this->save($folder);
34-
return $folder;
35-
}
36-
7+
{
378
/** Custom save function*/
389
public function save($folder)
3910
{
4011
if(!$folder instanceof FolderDao)
4112
{
4213
throw new Zend_Exception("Should be a folder.");
4314
}
44-
if($folder->getParentId()<=0)
15+
/*
16+
if($folder->getParentId()<=0)
4517
{
4618
$rightParent=0;
4719
}
@@ -50,7 +22,15 @@ public function save($folder)
5022
$parentFolder=$folder->getParent();
5123
$rightParent=$parentFolder->getRightIndice();
5224
}
25+
26+
27+
*/
28+
29+
$rightParent=0; // REMOVE ME
30+
5331
$data = array();
32+
33+
5434
foreach($this->_mainData as $key => $var)
5535
{
5636
if(isset($folder->$key))
@@ -75,7 +55,7 @@ public function save($folder)
7555
unset($data['left_indice']);
7656
unset($data['right_indice']);
7757

78-
$db = Zend_Registry::get('dbAdapter');
58+
$column_family = new ColumnFamily($this->database->getDB(), 'folder');
7959
$column_family->insert($key,$data);
8060
return $key;
8161
}
@@ -87,13 +67,10 @@ public function save($folder)
8767
array('left_indice >= ?'=>$rightParent));
8868
$insertedid = $this->insert($data);
8969
*/
90-
$db = Zend_Registry::get('dbAdapter');
91-
$column_family = new ColumnFamily($db, 'folder');
92-
93-
$uuid = CassandraUtil::uuid1();
94-
$column_family->insert($uuid,$data);
95-
96-
$folder->folder_id = bin2hex($uuid);
70+
$column_family = new ColumnFamily($this->database->getDB(), 'folder');
71+
$uuid = CassandraUtil::uuid1();
72+
$column_family->insert($uuid,$data);
73+
$folder->folder_id = $uuid;
9774
$folder->saved=true;
9875
return true;
9976
}

core/models/cassandra/FolderpolicygroupModel.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,23 @@
55
*/
66
class FolderpolicygroupModel extends MIDASFolderpolicygroupModel
77
{
8-
8+
/** getPolicy
9+
* @return FolderpolicyuserDao
10+
*/
11+
public function getPolicy($user, $folder)
12+
{
13+
if(!$user instanceof UserDao)
14+
{
15+
throw new Zend_Exception("Should be a user.");
16+
}
17+
if(!$folder instanceof FolderDao)
18+
{
19+
throw new Zend_Exception("Should be a folder.");
20+
}
21+
return $this->initDao('Folderpolicyuser',$this->database->fetchRow($this->database->select()
22+
->where('folder_id = ?',$folder->getKey())
23+
->where('user_id = ?',$user->getKey())
24+
));
25+
}
926
} // end class
1027
?>

core/models/cassandra/FolderpolicyuserModel.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,24 @@
55
*/
66
class FolderpolicyuserModel extends MIDASFolderpolicyuserModel
77
{
8-
8+
/** getPolicy
9+
* @return FolderpolicyuserDao
10+
*/
11+
public function getPolicy($user, $folder)
12+
{
13+
if(!$user instanceof UserDao)
14+
{
15+
throw new Zend_Exception("Should be a user.");
16+
}
17+
if(!$folder instanceof FolderDao)
18+
{
19+
throw new Zend_Exception("Should be a folder.");
20+
}
21+
return $this->initDao('Folderpolicyuser',$this->database->fetchRow($this->database->select()
22+
->where('folder_id = ?',$folder->getKey())
23+
->where('user_id = ?',$user->getKey())
24+
));
25+
}
26+
927
} // end class
1028
?>

core/models/cassandra/GroupModel.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,36 @@
55
*/
66
class GroupModel extends MIDASGroupModel
77
{
8-
8+
/** Add an user to a group
9+
* @return void
10+
* */
11+
function addUser($group,$user)
12+
{
13+
if(!$group instanceof GroupDao)
14+
{
15+
throw new Zend_Exception("Should be a group.");
16+
}
17+
if(!$user instanceof UserDao)
18+
{
19+
throw new Zend_Exception("Should be an user.");
20+
}
21+
22+
echo $group->getGroupId()."<br>";
23+
echo $user->getUserId()."<br>";
24+
25+
$column_family = new ColumnFamily($this->database->getDB(), 'group');
26+
27+
$data = array();
28+
$userarray = array();
29+
$userarray[$user->getUserId()] = 1;
30+
$data['users'] = $userarray;
31+
32+
$column_family->insert($group->getGroupId(),$data);
33+
34+
exit();
35+
36+
37+
} // end function addUser
38+
939
} // end class
1040
?>

core/models/cassandra/ItemModel.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,94 @@
55
*/
66
class ItemModel extends MIDASItemModel
77
{
8+
/** get random items
9+
*
10+
* @param UserDao $userDao
11+
* @param type $policy
12+
* @param type $limit
13+
* @return array of ItemDao
14+
*/
15+
function getRandomItems($userDao=null,$policy=0,$limit=10,$thumbnailFilter=false)
16+
{
17+
if($userDao==null)
18+
{
19+
$userId= -1;
20+
}
21+
else if(!$userDao instanceof UserDao)
22+
{
23+
throw new Zend_Exception("Should be an user.");
24+
}
25+
else
26+
{
27+
$userId = $userDao->getUserId();
28+
}
29+
30+
/*
31+
if(Zend_Registry::get('configDatabase')->database->adapter=='PDO_MYSQL')
32+
{
33+
$rand='RAND()';
34+
}
35+
else
36+
{
37+
$rand='random()';
38+
}
39+
$sql=$this->database->select()
40+
->setIntegrityCheck(false)
41+
->from(array('i' => 'item'))
42+
->join(array('tt'=> $this->database->select()
43+
->from(array('i' => 'item'),array('maxid'=>'MAX(item_id)'))
44+
),
45+
' i.item_id >= FLOOR(tt.maxid*'.$rand.')')
46+
->joinLeft(array('ip' => 'itempolicyuser'),'
47+
i.item_id = ip.item_id AND '.$this->database->getDB()->quoteInto('ip.policy >= ?', $policy).'
48+
AND '.$this->database->getDB()->quoteInto('user_id = ? ',$userId).' ',array('userpolicy'=>'ip.policy'))
49+
->joinLeft(array('ipg' => 'itempolicygroup'),'
50+
i.item_id = ipg.item_id AND '.$this->database->getDB()->quoteInto('ipg.policy >= ?', $policy).'
51+
AND ( '.$this->database->getDB()->quoteInto('group_id = ? ',MIDAS_GROUP_ANONYMOUS_KEY).' OR
52+
group_id IN (' .new Zend_Db_Expr(
53+
$this->database->select()
54+
->setIntegrityCheck(false)
55+
->from(array('u2g' => 'user2group'),
56+
array('group_id'))
57+
->where('u2g.user_id = ?' , $userId)
58+
) .'))' ,array('grouppolicy'=>'ipg.policy'))
59+
->where(
60+
'(
61+
ip.item_id is not null or
62+
ipg.item_id is not null)'
63+
)
64+
->limit($limit*2)
65+
;
66+
if($thumbnailFilter)
67+
{
68+
$sql->where('thumbnail != ?','');
69+
}
70+
71+
$rowset = $this->database->fetchAll($sql);
72+
*/
73+
$rowsetAnalysed=array();
74+
/*
75+
foreach ($rowset as $keyRow=>$row)
76+
{
77+
if($row['userpolicy']==null)$row['userpolicy']=0;
78+
if($row['grouppolicy']==null)$row['grouppolicy']=0;
79+
if(!isset($rowsetAnalysed[$row['item_id']])||($rowsetAnalysed[$row['item_id']]->policy<$row['userpolicy']&&$rowsetAnalysed[$row['item_id']]->policy<$row['grouppolicy']))
80+
{
81+
$tmpDao= $this->initDao('Item', $row);
82+
if($row['userpolicy']>=$row['grouppolicy'])
83+
{
84+
$tmpDao->policy=$row['userpolicy'];
85+
}
86+
else
87+
{
88+
$tmpDao->policy=$row['grouppolicy'];
89+
}
90+
$rowsetAnalysed[$row['item_id']] = $tmpDao;
91+
unset($tmpDao);
92+
}
93+
}
94+
*/
95+
return $rowsetAnalysed;
96+
}//end get random
897
}
998
?>

0 commit comments

Comments
 (0)