Timeout when fetching large amounts of data from MySQL? - php

I'm trying to fetch an item from my queue, prioritizing items with girl names in them. I've ran into memory issues so started using chunk instead of QueueItem::all() but I'm still getting timeouts and the endpoint is taking at least 30 seconds to process if it doesn't time out before that.
Here is the controller
public function __invoke()
{
$queueItem = null;
QueueItem::chunk(1000, function ($queueItems) use ($queueItem) {
foreach ($queueItems as $item) {
if ($queueItem != null || QueueHelpers::hasGirlsName($item->item)) {
$queueItem = $item;
break;
}
}
});
if (! $queueItem instanceof QueueItem)
{
$queueItem = QueueItem::orderBy('id', 'DESC')->first();
}
if ($queueItem == null) {
abort(404);
}
$queueItem->delete();
return new QueueResource($queueItem);
}
Here is the QueueHelpers::hasGirlsName method
public static function hasGirlsName($item): bool
{
foreach (self::$girlNames as $name)
{
if (stripos($item, $name) !== false) {
return true;
}
}
return false;
}
My $girlNames have around 500+ items in them but no more than 2,000.

Related

Opinion about using SESSION with PHP

I need opinion about using session with php. I'm useing session to store data. For instance - configuration:
First I'm loading data from config.ini to $_SESSION['config']
Then I'm using custom session class to get specific data from $_SESSION['config'][$key];
This is config function:
public static function load_config($process_sections = FALSE)
{
$array = parse_ini_file(CONFIG . DS . 'config.ini', $process_sections);
if ($array)
{
$_SESSION['configuration'] = $array;
return TRUE;
}
else
{
$_SESSION['configuration'] = array();
return FALSE;
}
}
function config($key, $default = NULL)
{
if (isset($_SESSION['configuration']))
{
if (isset($_SESSION['configuration'][$key]))
{
return $_SESSION['configuration'][$key];
}
else
{
return $default;
}
}
}
That same is with user object. I'm getting user data not from DB, but API, and storing it in $_SESSION['user']. When user object is constructs, I'm attributing all properties just from $_SESSION['user'][...], for instance:
public function __construct()
{
parent::__construct();
$this->initUser();
}
private function initUser()
{
if (Session::is('user'))
{
return $this->setUserData(Session::get('user'));
}
else
{
return FALSE;
}
}
private function setUserData($data)
{
foreach ($data as $key => $value)
{
if(property_exists($this->_name, $key))
{
$this->{$key} = $value;
}
}
return TRUE;
}
Properties are defined in class. I'm doing it just on time, when object is constructing. Is that right practice? It's working very good for me but I doubt if my method overolads server.

When should I return?

I am struggling to create an access object to sections stored in the Database. This is a skellington of the process, this contains static data until I can get the principle working.
class User {
const IS_ADMIN = 1;
const IS_MODERATOR = 2;
const IS_MEMBER = 4;
}
This class will auto-load data from the database eventually but for the time being, this class has default values.
class Scope {
private $priv = [];
public function __construct() {
$this->priv = [1];
}
public function getPrivilidges() {
return $this->priv;
}
}
This is where it messes up, I can tell that the second and third conditions cannot be met if the first fails, how can I stop this?
class Priverlidges {
public function canView($type, Scope $scope) {
if($type & User::IS_ADMIN) {
foreach($scope->getPrivilidges() as $p) {
if($p == User::IS_ADMIN) continue;
return false;
}
return true;
}
if($type & User::IS_MODERATOR) {
foreach($scope->getPrivilidges() as $p) {
if($p == User::IS_MODERATOR) continue;
return false;
}
return true;
}
if($type & User::IS_MEMBER) {
foreach($scope->getPrivilidges() as $p) {
if($p == User::IS_MEMBER) continue;
return false;
}
return true;
}
}
}
Example usage which works fine when the default value of the priverlidge is 1:
echo (int)(new Priverlidges)->canView(User::IS_ADMIN, new Scope());
Example usage which works fine when the default value of the priverlidge is 2:
echo (int)(new Priverlidges)->canView(User::IS_MODERATOR | User::IS_ADMIN, new Scope()); // it returns false at the first condition
Can anyone help me with when to return true or false? Thanks in advance.
P.S - Users can be both Mods and Admins
EDIT: I have tried to use in_array() and still am unsure when to return the value true or false because it get's overwrite if the second method runs.
I figured it out. First, check the user is not already authenticated using a placeholder ($this->_state). Then check the type of user and check it is inside the scope.
class Priverlidges {
private $_state = false;
public function canView($type, Scope $scope) {
if(!$this->_state && $type & User::IS_ADMIN && in_array(User::IS_ADMIN, $scope->getPrivilidges())) {
$this->_state = true;
}
if(!$this->_state && $type & User::IS_MODERATOR && in_array(User::IS_MODERATOR, $scope->getPrivilidges())) {
$this->_state = true;
}
if(!$this->_state && $type & User::IS_MEMBER && in_array($scope->getPrivilidges(), User::IS_MEMBER)) {
$this->_state = true;
}
return $this->_state;
}
}

How do I get the next item in a Laravel collection?

Let's say I have a child of a parent collection and I want to know who the next sibling is. My parent collection is ordered differently than internal id so I can't use the method described here:
Laravel previous and next records
Which would work except I'm sorting by name and time, not internal id. I'm hoping that there's a way to just get the parent collection, find this child's position within it, and then look forward or back within that collection to get next or previous.
Edit:
So, I made this, which works, but seems clunky. Is there a more efficient way to do this?
public function next()
{
$previous = null;
foreach ($this->album->media as $media)
{
if(!empty($previous && $previous->id == $this->id))
{
// Yay! Our current record is the 'next' record.
return $media->id;
}
$previous = $media;
}
return null;
}
public function previous()
{
$previous = null;
foreach ($this->album->media as $media)
{
if(!empty($previous && $media->id == $this->id))
{
// Yay! Our previous record is the 'previous' record.
return $previous;
}
$previous = $media->id;
}
return null;
}
You should never load the entire table to loop through it to find the next/previous item, instead do it this way:
$next = $this->album->media()->orderBy('id')->where('id', '>', $this->id)->first();
Here is the simple line of code
// next
function next($product_id)
{
$next = Product::where('id', '>', $product_id)->first();
return $next;
}
// previous
function previous($product_id)
{
$previous = Product::where('id', '<', $product_id)->first();
return $previous;
}
This did the trick:
public function next()
{
$previous = null;
foreach ($this->album->media as $media)
{
if(!empty($previous && $previous->id == $this->id))
{
// Yay! Our current record is the 'next' record.
return $media->id;
}
$previous = $media;
}
return null;
}
public function previous()
{
$previous = null;
foreach ($this->album->media as $media)
{
if(!empty($previous && $media->id == $this->id))
{
// Yay! Our previous record is the 'previous' record.
return $previous;
}
$previous = $media->id;
}
return null;
}

PHP Bi-Directional map

I'm porting to PHP a piece of Java code that uses a lot of Bi-directional maps (Guava's BiMap). Java-like maps are provided by PHP arrays or SplObjectStorage, but is there a library PHP Bi-Directional map available?
This class should provide for most needs of a bi-directional map :
class BiMap
{
private $KtoV, $VtoK;
public function __constructor()
{
$this->KtoV = []; // for version < 5.4.0, syntax must be: $this->KtoV = array();
$this->VtoK = [];
}
public function getKey($v)
{
if($this->hasValue($v))
{
return $this->VtoK[$v];
}
else
{
return null;
}
}
public function getAllKeys()
{
if($this->KtoV)
{
return array_keys($this->KtoV);
}
else
{
return $this->KtoV;
}
}
public function getValue($k)
{
if($this->hasKey($k))
{
return $this->KtoV[$k];
}
else
{
return null;
}
}
public function getAllValues()
{
if($this->VtoK)
{
return array_keys($this->VtoK);
}
else
{
return $this->VtoK;
}
}
public function hasKey($k)
{
return isset($this->KtoV[$k]);
}
public function hasValue($v)
{
return isset($this->VtoK[$v]);
}
public function put($k, $v)
{
if($this->hasKey($k))
{
$this->removeKey($k);
}
if($this->hasValue($v))
{
$this->removeValue($v);
}
$this->KtoV[$k] = $v;
$this->VtoK[$v] = $k;
}
public function putAll($array)
{
foreach($array as $k => $v)
{
$this->put($k, $v);
}
}
public function removeKey($k)
{
if($this->hasKey($k))
{
unset($this->VtoK[$this->KtoV[$k]]);
$v = $this->KtoV[$k];
unset($this->KtoV[$k]);
return $v;
}
else
{
return null;
}
}
public function removeValue($v)
{
if($this->hasValue($v))
{
unset($this->KtoV[$this->VtoK[$v]]);
$k = $this->VtoK[$v];
unset($this->VtoK[$v]);
return $k;
}
else
{
return null;
}
}
}
However, if you require null checking for key/values and/or object/array checking then handling similar to the following lines of code should be given in the body of a function and called appropriately within the hasKey($k), hasValue($v) and put($k, $v) methods :
if($item === null)
{
throw new Exception('null as BiMap key / value is invalid.');
}
if(is_object($item) || is_array($item))
{
throw new Exception('Object / Array as BiMap key / value is invalid.');
}
I did that once putting the values into 2 arrays. If keySet() and valueSet() are disjunct you can even use one value. Example:
$mapKtoV = array();
$mapVtoK = array();
function putInMap ($key,$value)
{
$mapKtoV[$key] = $value;
$mapVtoK[$value] = $key;
}
Of course you can also put them into a class.
Do you also think that this solution appears dodgy and smells? Yes, true, welcome to the world of PHP, which is usually dominated by bad code design. If you are really looking for a good solution, you should actually port you source from PHP to Java ;)
Hope it helps.

Checking which function is true and false

Sorry for the terrible headline, let me try to explain below.
I have written a bunch of small functions that either returns true or false.
validateName()
validateEmail()
validateAddr()
validateBirtd()
validateUsername()
Now I am looping through a lot of data imported with a CSV file, and checkin which data is valid or not (returns true or false).
I do it this way:
if (validateName($data[0]) == true AND validateEmail($data[1]) == true AND validateAddr($data[3]) == true AND validateBirtd($data[5]) == true AND validateUsername($data[6])==true) {
// create array to import etc etc
}else{
// create other array with data who failed validation, to show user later..etc etc
}
My question is - is there a more clever way to do this? Would it be possible to create a list of for each failed validation ? Say 3 entrys has fails the validateEmail() function, and 10 both fails validateEmail and validateName().
Would there be a way for me to sort this so I can tell the user "these entrys failed email validation" and "these entrys failed Name and email validation".
I thought about validating one field at a time, but this way I would have duplicates if one entry has more than one validation error.
Would be cool if there was some kind of logic that I don't know of where I could do this
You could create a function.
function validate($data) {
$errors = array();
$fields = array('Name', 'Email', 'Addr', 'Birtd', 'UserName');
foreach ($fields as $i => $field) {
$func = 'validate'.$field;
if (!$func($data[$i])) {
$errors[] = $field;
}
}
return $errors;
}
$errors = validate($data);
if (empty($errors)) {
// create array to import etc etc
} else {
// errors
echo 'There are errors with ' . implode(',', $errors);
}
You can use Iterators to get CSV content and filter at the same time. you can also add different callback to each CSV index
Example
$csv = new CSVFilter(new CSVIterator("log.txt"));
$csv->addFilter(0, "validateName"); //<------------ Means validate Index at 0
$csv->addFilter(1, "validateEmail");
$csv->addFilter(2, "validateAddr");
$csv->addFilter(3, "validateBirtd");
$csv->addFilter(4, "validateName");
$csv->addFilter(5, "validateUsername");
foreach ( $csv as $data ) {
var_dump($data);
}
//To get Errors
var_dump($csv->getErrors());
CSV Filter
class CSVFilter extends FilterIterator {
protected $filter = array();
protected $errors = array();
public function __construct(Iterator $iterator) {
parent::__construct($iterator);
}
public function addFilter($index, Callable $callable) {
$this->filter[$index] = $callable;
$this->errors[$callable] = 0;
}
public function getErrors() {
return $this->errors;
}
public function accept() {
$line = $this->getInnerIterator()->current();
$x = true;
foreach ($this->filter as $key => $var ) {
if (isset($line[$key])) {
$func = $this->filter[$key];
$func($var) or $this->errors[$func] ++ and $x = false;
}
}
return $x;
}
}
CSVIterator
class CSVIterator implements \Iterator {
protected $fileHandle;
protected $line;
protected $i;
public function __construct($fileName) {
if (! $this->fileHandle = fopen($fileName, 'r')) {
throw new \RuntimeException('Couldn\'t open file "' . $fileName . '"');
}
}
public function rewind() {
fseek($this->fileHandle, 0);
$this->line = fgetcsv($this->fileHandle);
$this->i = 0;
}
public function valid() {
return false !== $this->line;
}
public function current() {
return array_map("trim", $this->line);
}
public function key() {
return $this->i;
}
public function next() {
if (false !== $this->line) {
$this->line = fgetcsv($this->fileHandle);
$this->i ++;
}
}
public function __destruct() {
fclose($this->fileHandle);
}
}
Simple Random Functions
function validateName($var) {
return mt_rand(0, 5);
}
function validateEmail($var) {
return mt_rand(0, 5);
}
function validateAddr($var) {
return mt_rand(0, 5);
}
function validateBirtd($var) {
return mt_rand(0, 5);
}
function validateUsername($var) {
return mt_rand(0, 5);
}
If you something a little more encapsulated, you can try this. It allows you to write different validators for each CSV file you may be validating. Additionally, you could write methods in either class that would allow you to perform additional tasks on each row. I just find it a little cleaner and easier to maintain than having a bunch of globally-named functions.
Note: I'm obviously using some pretty basic validator examples and exceptions. The idea here is that I'm providing a layout for you to follow; you can customize any specific behaviors however you'd like.
usage
$c = new UserCsvValidator('user_data.csv');
try {
$c->validate();
}
catch (Exception $e) {
echo $e->getMessage();
}
implementation; parent class
<?php
class CsvValidator {
private $filename;
private $fh;
protected $fields = array();
public function validate__construct($filename, ) {
$this->filename = $filename;
// open file
if ( ($this->fh = fopen($this->filename, 'r')) === false) {
throw new Exception("could not open file: {$this->filename}");
}
}
public function validate() {
while( ($row=fgetcsv($this->fh)) !== false) {
// create hash
if ( ($hash = array_combine($this->fields, $row)) === false) {
throw new Exception("invalid row" . print_r($row, true));
}
// validate
foreach ($hash as $field => $value) {
// determine method call
$method = "validate_{$field}";
if (!method_exists($this, $method)) {
throw new Exception("validation method not defined: {$method}");
}
// validate the field
if (call_user_func(array($this, $method), $value) === false) {
throw new Exception("invalid value for {$field}: {$value}");
}
}
}
}
}
implementation; subclass
<?php
class UserCsvValidator extends CsvValidator {
protected $fields = array('name', 'email', 'address', 'birth_date', 'username');
// example functions for each field
protected function validate_name($value) {
return !empty($value);
}
protected function validate_email($value) {
return strpos($value, '#') !== false;
}
protected function validate_address($value) {
return !empty($value);
}
protected function validate_birth_date($value) {
return date('Y-m-d', strtotime($value)) == $value;
}
protected function validate_username($value) {
return !empty($value);
}
}

Categories