Experts... I'm creating another question in continuation with my earlier query.... this question is different from earlier request so I thought better to create new thread instead of confusing experts answers.
Below code connects each alias in tnsfile to the database...
1. Is there anyway I can limit each database connection only once and don't allow different alias connecting to the same database again?
I tried using hash but no able to fix this..
use strict;
use warnings;
if /^(([A-Za-z][A-Za-z0-9]*)(\.([A-Za-z][A-Za-z0-9]*))*)(\s|,|=)/
{
{
$hashref->{$1}="";
}
}
Below regex can select each SID value from file but not able to combine with if...
(/\(CONNECT_DATA\s+=\s+\(SID\s+=\s+(\w+\d+?)(\s+)?\)/)
Sample file (tnsfile.txt)
DB1.UK, DB2.UK =
(
(ADDRESS = (PROTOCAL = TCP))
(CONNECT_DATA = (SID = db1))
)
DB1.EU, DB2.CH =
(
(ADDRESS = (PROTOCAL = TCP))
(CONNECT_DATA = (SID = db1))
)
DB3.UK =
(
(ADDRESS = (PROTOCAL = TCP))
(CONNECT_DATA = (SID = db3))
)
DB3.US =
(
(ADDRESS = (PROTOCAL = TCP))
(CONNECT_DATA = (SID = db3))
)
DB4.UK.US, DB4.US =
(
(ADDRESS = (PROTOCAL = TCP))
(CONNECT_DATA = (SID = db4))
)
Expected $hashref value:
DB1.UK
DB3.UK
DB4.UK.US
Are you caching your database handles? It sounds like you are.
If so, you just need to create a new hash that relates SID to database handles. Then you only optionally create a new handle if that SID hasn't already been loaded.
In psuedocode:
my %dbh_by_sid;
while (<DATA>) {
my $sid = ...;
my $dbh = $dbh_by_sid{$sid} ||= DBI->connect(...)
or die "DB connect failed: $DBI::errstr";
# ...
}
This way if that particular SID has already been connected to, it will reuse the same handle.
This is far from a refined solution, and it breaks if the format of your tns file changes dramatically, but you could try something like this:
use strict;
my (%tns, %sid, $tns);
open IN, 'tnsfile.txt' or die;
while (<IN>) {
if (/^([\w.]+)/) {
($tns) = $1;
}
if (/SID *= *([\w.]+)/) {
$tns{$tns} = $1 unless ($sid{$1}++)
}
}
close IN;
print join "\n", keys %tns;
Related
My script is working so far to open a remote FTP connection, change directory, and download a file. My last two steps would be to delete the remove file once it's fully downloaded and then close the connection. ACF documentation (and cfdocs) seems to have very little information on this. Here's what I have so far:
ftpConnection = ftpService.open(
action = 'open',
connection = variables.ftpConnectionName,
server = variables.ftpServerName,
username = '***************',
password = '***************',
secure='true');
if( ftpConnection.getPrefix().succeeded ){
fileList = ftpService.listdir(directory = variables.ftpPath, connection= variables.ftpConnectionName, name='pendingFiles', stopOnError='true').getResult();
if( fileList.recordCount ){
changeFtpConnectionDir = ftpService.changeDir(
connection = variables.ftpConnectionName,
directory = variables.ftpPath);
getFtpConnection = ftpService.getFile(
connection = variables.ftpConnectionName,
remoteFile = fileList.name,
localFile = local.localPath & fileList.name,
failIfExists = false,
timeout = 3000
);
deleteRemoteFile = ftpService.remove(
connection = variables.ftpConnectionName,
remoteFile = fileList.name
};
closeFtp = ftpService.close(
connection = variables.ftpConnectionName
);
};
};
Error is thrown on the remoteFile = fileList.name. Since I already changed directory I don't think I need to put the full path here.
I put the entire script up since there doesn't seem to be many resources out there about using the newer ftpServer() functions.
D'oh - my issue was a typo:
deleteRemoteFile = ftpService.remove(
connection = variables.ftpConnectionName,
remoteFile = fileList.name
);// had } instead of )
I'll still leave this up as a resource for ftpService()
my tnsnames.ora file has 2 formats :
db_cl =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = a55)(PORT = 1522))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = cl)
)
)
dbcd =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = a66 )(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = cd)
)
)
myx5=
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = v55)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = x5)
)
)
i want to get the hostname of a specific service_name or sid. in some cases it is sid and in some cases it is service_name. what should i search with grep in order to get the hostname? In this example i want to get the string "host_name".
****UPDATE****
I ALSO NEED THE db_name2, if someone can help
If perl is available try this command:
perl -nle 'BEGIN{$service = shift}
$host = $1 if /HOST\s*=\s*([^\s\)]+)/i;
print $host if /\((SID|SERVICE_NAME)\s*=\s*$service\)/;
' blabla tnsnames.ora
It stores the last host value found in HOST = ... and prints it when encounters (**SID/SERVICE_NAME** = blabla)
#drf: try:
awk '/HOST/{sub(/).*/,"",$(NF-2));print $(NF-2)}' Input_file
Simply looking for string HOST for each line then substituting the ).* from the 2nd last filed of the line where awk matches the string HOST in it, it should give you host_name then.
EDIT: For looking for a specific SID or service name try:
awk '/HOST/{sub(/).*/,"",$(NF-2));HOST=$(NF-2);next} /SID/{print HOST}'
Change SID with sid or service name which you want to search and it should work then.
EDIT2:
awk '/db_name/{sub(/=/,"",$1);DB=$1}/HOST/{sub(/).*/,"",$(NF-2));HOST=$(NF-2);next} /chumma/{print DB ORS HOST}'
EDIT3:
awk '{gsub(/\)|\(/,"");;for(i=1;i<=NF;i++){if($i=="HOST"){host=$(i+1)};if($NF=="cd"){val="DB_NAME= "$1", HOST_NAME= "host",SID/SERVICE_NAME= "$NF}};if(val){print val;val=""}}' Input_file
OR(non-one liner form of solution too as follows):
awk '{gsub(/\)|\(/,"");
for(i=1;i<=NF;i++){
if($i=="HOST"){
host=$(i+1)
};
if($NF=="cd") {
val="DB_NAME= "$1", HOST_NAME= "host",SID/SERVICE_NAME= "$NF
}
};
if(val) {
print val;
val=""
}
}
' Input_file
You could put "cd"''s place another service or ssid which you want to search too in above code.
Meaning, they don't have to be distributed. I'm thinking about using memcached or redis for that. Probably the latter one. What I'm concerned about is "we've got to free some memory, so we'll delete this key/value before it expired" thing. But I'm open to other suggestions as well.
tl;dr Use ready-made solution, suggested by developers.
So, I decided not to use memcached for the purpose. Since it's a caching server. I don't see a way to ensure that it doesn't delete my keys because it's out of memory. With, redis that's not an issue as long as maxmemory-policy = noeviction.
There are 3 links I want to share with you. They are basically 3 ways, that I now know, to solve the issue. As long as you have redis >= 2.6.0 that is.
redis >= 2.6.12
If you've got redis >= 2.6.12, you're lucky and can simply use setnx command with its new options ex and nx:
$redis->set($name, <whatever>, array('nx', 'ex' => $ttl));
But we can't just delete the lock in the end, if we are to allow for critical section taking longer then we expected (>= ttl). Consider the following situation:
C1 acquires the lock
lock expires
C2 acquires the lock
C1 deletes C2's lock
For that not to happen we are going to store current timestamp as a value of the lock. Then, knowing that Lua scripts are atomic (see Atomicity of scripts):
$redis->eval('
if redis.call("get", KEYS[1]) == KEYS[2] then
redis.call("del", KEYS[1])
end
', array($name, $now));
However, is it possible for two clients to have equal now values? For that all the above actions should happen within one second and ttl must be equal to 0.
Resulting code:
function get_redis() {
static $redis;
if ( ! $redis) {
$redis = new Redis;
$redis->connect('127.0.0.1');
}
return $redis;
}
function acquire_lock($name, $ttl) {
if ( ! $ttl)
return FALSE;
$redis = get_redis();
$now = time();
$r = $redis->set($name, $now, array('nx', 'ex' => $ttl));
if ( ! $r)
return FALSE;
$lock = new RedisLock($redis, $name, $now);
register_shutdown_function(function() use ($lock) {
$r = $lock->release();
# if ( ! $r) {
# Here we can log the fact that lock has expired too early
# }
});
return $lock;
}
class RedisLock {
var $redis;
var $name;
var $now;
var $released;
function __construct($redis, $name, $now) {
$this->redis = get_redis();
$this->name = $name;
$this->now = $now;
}
function release() {
if ($this->released)
return TRUE;
$r = $this->redis->eval('
if redis.call("get", KEYS[1]) == KEYS[2] then
redis.call("del", KEYS[1])
return 1
else
return 0
end
', array($this->name, $this->now));
if ($r)
$this->released = TRUE;
return $r;
}
}
$l1 = acquire_lock('l1', 4);
var_dump($l1 ? date('H:i:s', $l1->expires_at) : FALSE);
sleep(2);
$l2 = acquire_lock('l1', 4);
var_dump($l2 ? date('H:i:s', $l2->expires_at) : FALSE); # FALSE
sleep(4);
$l3 = acquire_lock('l1', 4);
var_dump($l3 ? date('H:i:s', $l3->expires_at) : FALSE);
expire
The other solution I found here. You simply make the value expire with expire command:
$redis->eval('
local r = redis.call("setnx", ARGV[1], ARGV[2])
if r == 1 then
redis.call("expire", ARGV[1], ARGV[3])
end
', array($name, $now, $ttl));
So, only acquire_lock function changes:
function acquire_lock($name, $ttl) {
if ( ! $ttl)
return FALSE;
$redis = get_redis();
$now = time();
$r = $redis->eval('
local r = redis.call("setnx", ARGV[1], ARGV[2])
if r == 1 then
redis.call("expire", ARGV[1], ARGV[3])
end
return r
', array($name, $now, $ttl));
if ( ! $r)
return FALSE;
$lock = new RedisLock($redis, $name, $now);
register_shutdown_function(function() use ($lock) {
$r = $lock->release();
# if ( ! $r) {
# Here we can log that lock as expired too early
# }
});
return $lock;
}
getset
And the last one is described again in documentation. Marked with "left for historical reasons" note.
This time we store timestamp of the moment when the lock is to expire. We store it with setnx command. If it succeeds, we've acquired the lock. Otherwise, either someone else's holding the lock, or the lock has expired. Be it the latter, we use getset to set new value and if the old value hasn't changed, we've acquired the lock:
$r = $redis->setnx($name, $expires_at);
if ( ! $r) {
$cur_expires_at = $redis->get($name);
if ($cur_expires_at > time())
return FALSE;
$cur_expires_at_2 = $redis->getset($name, $expires_at);
if ($cur_expires_at_2 != $cur_expires_at)
return FALSE;
}
What makes me uncomfortable here is that we seem to have changed someone else's expires_at value, don't we?
On a side note, you can check which redis is it that you're using this way:
function get_redis_version() {
static $redis_version;
if ( ! $redis_version) {
$redis = get_redis();
$info = $redis->info();
$redis_version = $info['redis_version'];
}
return $redis_version;
}
if (version_compare(get_redis_version(), '2.6.12') >= 0) {
...
}
Some debugging functions:
function redis_var_dump($keys) {
foreach (get_redis()->keys($keys) as $key) {
$ttl = get_redis()->ttl($key);
printf("%s: %s%s%s", $key, get_redis()->get($key),
$ttl >= 0 ? sprintf(" (ttl: %s)", $ttl) : '',
nl());
}
}
function nl() {
return PHP_SAPI == 'cli' ? "\n" : '<br>';
}
I want paginate my index http:localhost/mysite/home but if I write only this http:localhost/mysite/home the page said: "page not found", but if I write http:localhost/mysite/home/3 its work! how to I can configure my routing to get null parameters? I tried with (:any) and (num) but not work
My route file is:
$route['404_override'] = 'welcome/no_found';
$route['home/'] = "welcome/home/$1";
My controller:
$config['base_url'] = base_url().'/mysite/home';
$config['total_rows'] = $this->mtestmodel->countNews();
$config['per_page'] = 2;
$config['num_links'] = 20;
$this->pagination->initialize($config);
$data['paginator']=$this->pagination->create_links();
$data['news']=$this->mtestmodel->show_news( $config['per_page'],intval($to) );
//$to is a parameter in url base_url()/mysite/home/3
Change your route.php file to the following:
$route['404_override'] = 'welcome/no_found';
$route['home/(:num)'] = "welcome/home/$1";
$route['home'] = "welcome/home";
This way, your application will catch both requests (http://localhost/mysite/home and http://localhost/mysite/home/3), and will send them both to your controller.
You should then be able to access your $to variable (which has been passed as the first argument into your controller's function) or use $this->uri->segment(2) instead.
e.g.
public function home($to = 0) {
$config['base_url'] = base_url().'/mysite/home';
$config['total_rows'] = $this->mtestmodel->countNews();
$config['per_page'] = 2;
$config['num_links'] = 20;
$this->pagination->initialize($config);
$data['paginator'] = $this->pagination->create_links();
$data['news'] = $this->mtestmodel->show_news( $config['per_page'],intval($to) );
}
Hope that helps!
$q = $this->_em->createQuery("SELECT s FROM app\models\Quest s
LEFT JOIN s.que c
WHERE s.type = '$sub'
AND c.id = '$id'");
Given a query like the one above, how would I retrieve the number of results?
Alternatively one can look at what Doctrine Paginator class does to a Query object to get a count (this aproach is most probably an overkill though, but it answers your question):
public function count()
{
if ($this->count === null) {
/* #var $countQuery Query */
$countQuery = $this->cloneQuery($this->query);
if ( ! $countQuery->getHint(CountWalker::HINT_DISTINCT)) {
$countQuery->setHint(CountWalker::HINT_DISTINCT, true);
}
if ($this->useOutputWalker($countQuery)) {
$platform = $countQuery->getEntityManager()->getConnection()->getDatabasePlatform(); // law of demeter win
$rsm = new ResultSetMapping();
$rsm->addScalarResult($platform->getSQLResultCasing('dctrn_count'), 'count');
$countQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\CountOutputWalker');
$countQuery->setResultSetMapping($rsm);
} else {
$countQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\ORM\Tools\Pagination\CountWalker'));
}
$countQuery->setFirstResult(null)->setMaxResults(null);
try {
$data = $countQuery->getScalarResult();
$data = array_map('current', $data);
$this->count = array_sum($data);
} catch(NoResultException $e) {
$this->count = 0;
}
}
return $this->count;
}
You can either perform a count query beforehand:
$count = $em->createQuery('SELECT count(s) FROM app\models\Quest s
LEFT JOIN s.que c
WHERE s.type=:type
AND c.id=:id)
->setParameter('type', $sub);
->setParameter('id', $id);
->getSingleScalarResult();
Or you can just execute your query and get the size of the results array:
$quests = $q->getResult();
$count = count($quests);
Use the first method if you need the count so that you can make a decision before actually retrieving the objects.