public function add($data, $shard = NULL) { $shard = strval($shard); if (!ctype_digit($shard)) { $shard = Util::currentShard(); } $shard_key = 'shard_' . $shard; $sequence = $this->store->increment($shard_key); if ($sequence < 1 && !$this->store->add($shard_key, $sequence = 1)) { throw new Exception('unable to allocate id'); } $id = Util::composeId($shard, $sequence); $this->store->set($id, $data); $shards = $this->store->get('shards'); if (!is_array($shards)) { $shards = array(); } if (!isset($shards[$shard])) { $shards[$shard] = 1; $this->store->set('shards', $shards); } // use the id to increment the sequence. return $id; }
/** * add a new entry to the skein. returns the id. */ public function add($data, $shard = NULL) { $shard = strval($shard); if (!ctype_digit($shard)) { $shard = Util::currentShard(); } $table = $this->table('index'); $dbi = $this->db($table); DB\Transaction::start(); $dbi->start(); $sql = "INSERT INTO {$table} (thread,shard,sequence) VALUES (%i, %i, @SKEIN_SEQUENCE:=1) ON DUPLICATE KEY UPDATE `sequence` = @SKEIN_SEQUENCE:=( `sequence` + 1 )"; $dbi->execute($sql, $this->thread, $shard); $rs = $dbi->execute('SELECT @SKEIN_SEQUENCE as sequence'); $sequence = NULL; if ($row = $rs->fetch()) { $sequence = $row['sequence']; } $rs->free(); $table = $this->table($shard); $dbs = $this->db($table); $dbs->start(); $sql = "INSERT INTO {$table} (thread, sequence, data) VALUES (%i, %i, %s) ON DUPLICATE KEY UPDATE `data` = VALUES(`data`)"; $dbs->execute($sql, $this->thread, $sequence, $this->serialize($data)); $dbi->commit(); $dbs->commit(); DB\Transaction::commit(); $id = Util::composeId($shard, $sequence); return $id; }
/** * add a new entry to the skein. returns the id. */ public function add($data, $shard = NULL) { $shard = strval($shard); if (!ctype_digit($shard)) { $shard = Util::currentShard(); } $table = $this->table('index'); $dbi = $this->db($table); DB\Transaction::start(); $dbi->start(); $sql = "INSERT OR IGNORE INTO {$table} (thread,shard,sequence) VALUES (%i, %i, 1)"; $rs = $dbi->execute($sql, $this->thread, $shard); if (!$rs->affected()) { $sql = "UPDATE {$table} SET `sequence` = `sequence` + 1 WHERE `thread` = %i AND `shard` = %i"; $dbi->execute($sql, $this->thread, $shard); } $sql = "SELECT `sequence` FROM {$table} WHERE `thread` = %i AND `shard` = %i"; $rs = $dbi->execute($sql, $this->thread, $shard); $sequence = NULL; if ($row = $rs->fetch()) { $sequence = $row['sequence']; } $rs->free(); $table = $this->table($shard); $dbs = $this->db($table); $dbs->start(); $sql = "INSERT OR IGNORE INTO {$table} (thread, sequence, data) VALUES (%i, %i, %s)"; $data = $this->serialize($data); $dbs->execute($sql, $this->thread, $sequence, $data); if (!$rs->affected()) { $sql = "UPDATE {$table} SET `data` = %s WHERE `thread` = %i AND `sequence` = %i"; $dbs->execute($sql, $data, $this->thread, $sequence); } $dbi->commit(); $dbs->commit(); DB\Transaction::commit(); $id = Util::composeId($shard, $sequence); return $id; }