3
* © Copyright 2009 IntraHealth International, Inc.
5
* This File is part of I2CE
7
* I2CE is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 3 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21
* @author Carl Leitner <litlfred@ibiblio.org>
27
* Class I2CE_MagicDataStorageSysV
32
if (!class_exists('I2CE_MagicDataStorageSysV',false)) {
33
require_once('I2CE_MagicDataStorage.php');
34
class I2CE_MagicDataStorageSysV extends I2CE_MagicDataStorage {
36
const MAX_AQUISITIONS = 1024;
37
const SEG_SIZE = 33554432; //max seg size
40
* Returns true if this storage mechanism is ready to be used. false otherwise.
42
public function isAvailable() {
43
if (!function_exists('sem_get') || !function_exists('shm_get_var')) {
44
I2CE::raiseError("sem_get not available");
47
if ( !is_resource($this->shm_id)) {
48
I2CE::raiseError("no shared memory id");
50
if (self::NUM_SEGS < 1 ) { //prevent stupidity
51
I2CE::raiseError("bad number of segements");
54
for ($i=0; $i < self::NUM_SEGS; $i++) {
55
if (!array_key_exists($i,$this->seg) || !($this->seg[$i])) {
56
I2CE::raiseError("segment $i is not a resource");
57
var_dump($this->seg[$i]);
64
* The semaphore identifier.
65
* @var protected $shm_id
67
protected $shm_id = false;
69
* The array of memory segments
70
* @var protected array $seg
72
protected $seg = array();
75
* Gets the key for the System V Sempahore
78
public function getKey() {
83
* Create a new instance for magic data storage.
84
* @param string $name The name assigned to this storage object
86
public function __construct( $name ) {
87
parent::__construct($name);
88
if (!function_exists('sem_get') || !function_exists('shm_get_var')) {
91
$this->shm_id = sem_get($this->getKey(),self::MAX_AQUISITIONS,0644 | IPC_CREAT);
92
if (!is_resource($this->shm_id)) {
95
if (self::NUM_SEGS < 1) {
98
for ($i=0; $i < self::NUM_SEGS; $i++) {
99
$this->seg[$i] = shm_attach($this->shm_id,self::SEG_SIZE);
101
$this->div = hexdec(str_pad('',32,'f'))/self::NUM_SEGS;
107
* Store the given I2CE_MagicDataNode into the database.
108
* @param I2CE_MagicDataNode $node
109
* @returns boolean. True on sucess
111
public function store( $node ) {
112
$hash = $this->getHash( $node );
113
$seg = $this->getSegment($hash);
115
I2CE::raiseError("Bad segement:$seg");
119
'type' => $node->getType(),
120
'value' => $node->getSaveValue(),
121
'children' => $node->getKeys(null,true)
123
sem_acquire($this->shm_id);
124
$success = shm_put_var($this->seg[$seg],$hash,$save_data);
125
sem_release($this->shm_id);
127
I2CE_MagicDataNode::raiseError( "Error saving to Shared Memory " . $node->getPath() . " Type: " . $node->getType() . " Value: " . $value . " Children: " . implode(',',$children) );
133
public function getSegment($hash) {
134
if (!is_string($hash) || strlen($hash) == 0 || self::NUM_SEGS < 1) {
135
var_dump( array( $hash, !is_string($hash) , strlen($hash) == 0 , self::NUM_SEGS < 1));
138
if (self::NUM_SEGS == 1) {
141
return ((int) (hexdec($hash) / $this->div));
145
* Retrieve the given I2CE_MagicDataNode value and type.
146
* @param I2CE_MagicDataNode $node
149
public function retrieve( $node ) {
150
$hash = $this->getHash( $node );
151
$seg = $this->getSegment($hash);
153
I2CE::raiseError("Bad segement:$seg");
156
return @shm_get_var($this->seg[$seg],$hash);
160
* Erases the given I2CE_MagicDataNode from the storage mechanism
161
* @param I2CE_MagicDataNode
163
public function destroy($node) {
164
$hash = $this->getHash( $node );
165
$seg = $this->getSegment($hash);
167
I2CE::raiseError("Bad segement:$seg");
170
sem_acquire($this->shm_id);
171
$success = shm_remove_var($this->seg[$seg],$hash);
172
sem_release($this->shm_id);
177
* releases all shared memory segments
180
public function clear() {
181
I2CE::raiseError("Clearing all shared memory segemnts");
183
for ($i=0; $i < self::NUM_SEGS; $i++) {
184
$success &= shm_remove($this->seg[$i]);
186
for ($i=0; $i < self::NUM_SEGS; $i++) {
187
$this->seg[$i] = shm_attach($this->shm_id,self::SEG_SIZE);
197
# c-default-style: "bsd"
198
# indent-tabs-mode: nil