Thursday, April 14, 2011

[PHP] AMFPHP + Zend Session

Hi, I have tried to use ZendSession class with AMFPHP, but AMFPHP uses raw PHP sessions in some of its functions and methods. I strongly need to manage all session variables by ZendSession. Is there any smart method to get it, instead of hack and modify original AMFPHP code?

From stackoverflow
  • I know this is not the answer to your question, but did you take a look at http://framework.zend.com/download/amf ?

  • I've commented text above, but maybe it's not visible for first look, so I write it once again. I'm not interested in ZendAMF, because it seems to be very slow... But thanx for the answer :) So - any other idea?

    Simon Groenewolt : If you want a comment like yours to be visible it is best to edit your original question - that way everyone will see it. Do not use the answers to make additional comments, because the order can change.

Is it faster to search for a large string in a DB by its hashcode?

If I need to retrieve a large string from a DB, Is it faster to search for it using the string itself or would I gain by hashing the string and storing the hash in the DB as well and then search based on that?

If yes what hash algorithm should I use (security is not an issue, I am looking for performance)

If it matters: I am using C# and MSSQL2005

From stackoverflow
  • If you use a fixed length field and an index it will probably be faster...

  • In general: probably not, assuming the column is indexed. Database servers are designed to do such lookups quickly and efficiently. Some databases (e.g. Oracle) provide options to build indexes based on hashing.

    However, in the end this can be only answered by performance testing with representative (of your requirements) data and usage patterns.

  • If your strings are short (less than 100 charaters in general), strings will be faster.

    If the strings are large, HASH search may and most probably will be faster.

    HashBytes(MD4) seems to be the fastest on DML.

  • Though I've never done it, it sounds like this would work in principle. There's a chance you may get false positives but that's probably quite slim.

    I'd go with a fast algorithm such as MD5 as you don't want to spend longer hashing the string than it would have taken you to just search for it.

    The final thing I can say is that you'll only know if it is better if you try it out and measure the performance.

  • Are you doing an equality match, or a containment match? For an equality match, you should let the db handle this (but add a non-clustered index) and just test via WHERE table.Foo = @foo. For a containment match, you should perhaps look at full text index.

  • I'd be surprised if this offered huge improvement and I would recommend not using your own performance optimisations for a DB search.

    If you use a database index there is scope for performance to be tuned by a DBA using tried and trusted methods. Hard coding your own index optimisation will prevent this and may stop you gaining for any performance improvements in indexing in future versions of the DB.

  • I am confused and am probably misunderstanding your question.

    If you already have the string (thus you can compute the hash), why do you need to retrieve it?

    Do you use a large string as the key for something perhaps?

    Sruly : Good point. I think i didnt make myself clear. I have the string but I want to retrive other information related to it that is stored in the DB.
    Lasse V. Karlsen : Then why not consider using something other than the string to find those related things? But in any case, I agree with the top answer (atm), you should test and measure.
  • First - MEASURE it. That is the only way to tell for sure.
    Second - If you don't have an issue with the speed of the string searching, then keep it simple and don't use a Hash.

    However, for your actual question (and just because it is an interesting thought). It depends on how similar the strings are. Remember that the DB engine doesn't need to compare all the characters in a string, only enough to find a difference. If you are looking through 10 million strings that all start with the same 300 characters then the hash will almost certainly be faster. If however you are looking for the only string that starts with an x, then i the string comparison could be faster. I think though that SQL will still have to get the entire string from disc, even if it then only uses the first byte (or first few bytes for multi byte characters), so the total string length will still have an impact.

    If you are trying the hash comparison then you should make the hash an indexed calculated column. It will not be faster if you are working out the hashes for all the strings each time you run a query!

    You could also consider using SQL's CRC function. It produces an int, which will be even quicker to comapre and is faster to calculate. But you will have to double check the results of this query by actually testing the string values because the CRC function is not designed for this sort of usage and is much more likly to return duplicate values. You will need to do the CRC or Hash check in one query, then have an outer query that compares the strings. You will also want to watch the QEP generated to make sure the optimiser is processing the query in the order you intended. It might decide to do the string comparisons first, then the CRC or Hash checks second.

    As someone else has pointed out, this is only any good if you are doing an exact match. A hash can't help if you are trying to do any sort of range or partial match.

    : Well, the hash value is a number, so it's always faster to compare a single number to another number than it is to compare strings. Even in your example of the only string starting with an x, it still needs to compare Ascii values.
    pipTheGeek : The Hash value isn't a single number, Its a varbinary. And isn't the ascii value for x a number?
  • TIP: if you are going to store the hash in the database, a MD5 Hash is always 16 bytes, so can be saved in a uniqueidentifier column (and System.Guid in .NET)

    This might offer some performance gain over saving hashes in a different way (I use this method to check for binary/ntext field changes but not for strings/nvarchars).

  • The 'ideal' answer is definitely yes. String matching against an indexed column will always be slower than matching a hashvalue stored in an index column. This is what hashvalues are designed for, because they take a large dataset (e.g. 3000 comparison points, one per character) and coalesce it into a smaller dataset, (e.g. 16 comparison points, one per byte).

    So, the most optimized string comparison tool will be slower than the optimized hash value comparison.

    However, as has been noted, implementing your own optimized hash function is dangerous and likely to not go well. (I've tried and failed miserably) Hash collisions are not particulrly a problem, because then you will just have to fall back on the string matching algorithm, which means that would be (at worst) exactly as fast as your string comparison method.

    But, this is all assuming that your hashing is done in an optimal fashion, (which it probably won't be) and that there will not be any bugs in your hashing component (which there will be) and that the performance increase will be worth the effort (probably not). String comparison algorithms, especially in indexed columns are already pretty fast, and the hashing effort (programmer time) is likely to be much higher than your possible gain.

    And if you want to know about performance, Just Measure It.

Strange error while executing a C code in MSVC 2005

Hi,

I am facing following quirky error.

I have a workspace in MSVS2005 of all C code. I have declared a global variable in one C file.(file1.c) This file has function main() in which, I initilaize the value of that variable = 0.In other C file(file2.c). From main there is a function call to a function(func1 in file2.c) which sets the value of this global variable to 1. In file2.c I have declared the global variable as "extern .." and accessed it. But what i noticed is that in the main function moment the code execution enter the function func2, I see in the watch window that the address of that global variable itself is changed to a totally different address(In watch window I am watching &variable). As a result, when the value of that variable is set to 1, it writes the value 1 to altogether different memory address itself. So when later I use this variable to check in a if condition(if variable == 1), it still shows value of 0 and does not go satisfy the if condition and does not take that code path , where it was expected to have taken that path.

Workaround: I declared that variable in one of my exisitng global structure, and then accessed this variable for doing the same operations; the code works as expected.

So what could be the explanation for the error which is causing the address of the global variable to be changed if its declared as a global in some C file? It does not matter in which *.c file i declare it and which file I access it using "extern" , the result is same global variable address change and subsequent errorneous operation.No optimization option is enabled.

Thanks,

-AD

From stackoverflow
  • Maybe try declaring it volatile (not sure if that's even valid for globals) and disable any compiler optimizations in case it's getting tricky somehow.

  • If the variable has a different address in different translation units, you are not seeing one but at least two variables with the same name.

    Most common cause: You may have accidently declared a local variable on the stack with the same name. Check your code for this. If the variables are really global the linker should complain if two translation units contain the same symbol.

    If this does not help, and if you still see multiple copies of the same symbol-name it's probably best to take a look at the map file (can be enabled in the linker-settings).

    All external symbols are listed there with their name, address and (most important in your case) the object-file that contained them.

    The addresses in the map-file may be just offsets. In this case do all your calculations relative to a symbol that is known to exist only once. the main() entrypoint might be good for this.

  • Probably a typo or some thing similar in your code. Try this working demo:

    file1.c

    int variable;
    void fun1(int k);
    
    int main()
    {
        printf("%d\n", variable);
        fun1(4);
        printf("%d\n", variable);
    }
    

    file2.c

    extern int variable;
    
    void fun1(int k)
    {
        variable = k;
    }
    

    Output:

    0
    4
    

    To compile:

    cl.exe file1.c file2.c
    
  • Can only guess without actually seeing the code, but here are 2 possibilities:

    1. the global variable is being hidden by a local in either main() or func2() (or maybe func1() - the question mentions func1() but I suspect that's a typo - this is why cutting and pasting code is quite important);
    2. you are mistakenly declaring the global variable as static in file1.c and have an initializer on your extern declaration in file2.c. Having an initializer on the extern declaration will cause that declaration to be a definition, too.

A non-deprecated exact equivalent of Date(String s) in Java?

I have old code that uses new Date(dateString) to parse a date string. Compiling the code produces the deprecation warning Date(java.lang.String) in java.util.Date has been deprecated.

The javadoc unhelpfully advises me to use DateFormat.parse(), even though the DateFormat class does not have a static parse method.

Now, I know how to use SimpleDateFormat, but I want to make sure I'm getting the exact same behaviour of the deperecated Date constructor.

From stackoverflow
  • DateFormat has static methods that return DateFormat instances. I don't know which one (if any) has the same behavior as Date(String s) but here you go:

    DateFormat.getInstance()
    DateFormat.getDateInstance()
    DateFormat.getTimeInstance()
    DateFormat.getDateTimeInstance()
    
    itsadok : None of these seem to work
  • Here's my guess (I posted as community wiki so you can vote up if I'm right):

    Date parsed = new Date();
    try {
        SimpleDateFormat format =
            new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
        parsed = format.parse(dateString);
    }
    catch(ParseException pe) {
        throw new IllegalArgumentException();
    }
    
    Paul Tomblin : Yeah, that's pretty much how I'd do it if I wanted to get exactly the same behaviour. The reason why DateFormat.getInstance() is better is it returns the appropriate formatter for the current locale.
    sleske : Please, pretty please never, ever do a "new IllegalgArgumentException()" :O . At the very least chain the original exception (new IllegalArgumentException(pe)).
  • Short answer (before further investigation) is: no, it is not equivalent. the Date(String toParse) constructor is equivalent to the parse method of the class Date (which is also deprecated)... And the javadoc of this method claims:

    Note that this is slightly different from the interpretation of years less than 100 that is used in SimpleDateFormat.

    If it is the only change, I guess you can go on this way.

  • SimpleDateFormat is the way to go. Can I point out, however, that you may feel compelled to define one SimpleDateFormat instance and build Date objects using this. If you do, beware that SimpleDateFormat is not thread-safe and you may be exposing yourself to some potentially hard-to-debug issues!

    I'd recommend taking this opportunity to look at Joda which is a much better thought-out (and thread-safe) API. It forms the basis of JSR-310, which is the new proposed Java Date API.

    I understand this is a bit more work. However it's probably worthwhile given that you're having to refactor code at the moment.

  • If you take a look at source of the Date.parse(String s) method that Nicolas mentions, you'll see that it will be difficult or impossible to construct a date format that exactly reproduces the behavior.

    If you just want to eliminate the warning, you could put @SuppressWarnings({“deprecation”}) outside the method calling the Date(String) constructor.

    If you really want to ensure future access to this behavior with future JREs, you might be able to just extract the method from the JDK sources and put it into your own sources. This would require a careful read of the source code licenses and consideration of their application to your specific project, and might not be permissible at all.

Wordpress previous_posts_link() leads to a 404 error not found

Below is the code I am using. I have tried everything I have been able to find and it still doesn't work. My permalink structure is /%category%/%postname%/. I believe that the url is correct that it is trying to go to i.e. http://localhost:8888/wordpress/blog/page/2. Annoyingly, the exact same code works on another site I have designed previously.

Could someone point me in the right direction please? Thanks

<?php get_header(); ?>
 <div id="content" class="narrowcolumn">
 <?php 
  $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  query_posts("cat=3&showposts=2&paged=" . $paged);

  $wp_query->is_archive = true; $wp_query->is_home = false;
 ?>

 <?php if (have_posts()) : ?>
 <div id="lefttop"></div>

 <div id="blogpoint">
 <div id="leftcol">
  <?php while (have_posts()) : the_post(); ?>

   <div id="leftsquidge">
    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2><br /><br />

     <?php the_excerpt(); ?>
   </div> 
   <div id="rightsquidge">
    <?php the_tags( '<p><strong>File under:</strong> ', ', ', '</p>'); ?>
    <?php the_time('F jS, Y') ?>  by <strong><?php the_author() ?></strong>
   </div>
   <div style="clear:both;"></div> 
   <br /><br />
  <?php endwhile; ?>
  <div class="navigation" style="padding:0px;margin:0px;">
   <div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
   <div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
  </div>
 <?php endif; ?> 
  <div style="clear:both;"></div> 
  </div>

  </div>
  <div id="leftbot"></div>
 </div>

<?php get_sidebar(); ?>

<?php get_footer(); ?>


EDIT

I have answered my own question. It was something I had tried before and wasn't working. You have to create a page, on the dashboard, that uses your category as the template.

From stackoverflow
  • If the same code works fine in another site then check your settings for this site. Compare your permalinks settings on both the sites.

    Does both the site work on the same environment (Apache or iis)?

    Drew : Yeah, the settings are the same on both sites. The site that works runs on apache and this site runs locally at the moment.
    Drew : on a mamp sertup

Graphics.drawImage() in Java is EXTREMELY slow on some computers yet much faster on others

I'm having a strange problem, basically in Java Graphics.drawImage() is extremely slow on some computers and faster on others. This isn't related to the computers power either, some weaker computers run it fine while some stronger ones seem to choke up at the drawImage call.

It may or may not be related to the width and height, I have a very, very large width and height defined (something like 5000 by 2500). I wouldn't think it's the issue except like I said it runs in real time speed on some computers and slower on others and doesn't seem to be tied to the computers relative power.

Both computers have the same version of Java, both use Vista. One has a 1.83ghz Core 2 Duo with 1gb RAM and onboard graphics (runs everything fine), the other has a 2.53 ghz core 2 duo with a 9600GS (latest nVidia drivers) and 4gb of RAM and it literally chugs on the drawImage call.

Any ideas?

edit: ok this is really wierd, I'm drawing the image to a window in Swing, now when I resize the window and make it really small the image gets scaled down too and it becomes small. Suddenly everything runs smoothly, when I scale it back up to the size it was before it's still running smoothly!

It also has multiple monitor issues, if I do the resize trick to make it run faster on one monitor then scroll it over to another monitor when more than half of the window is in the new monitor it starts chugging again. I have to resize the window again to small then back to its original size to get back the speed.

If I do the resize trick on one monitor, move it over to the other it of course chugs, but if I return it back to the original monitor on which I did the resize trick it works 100%

If I have two swing windows open (displaying the same image) they both run slow, but if I do the resize trick on one window they both start running smoothly (however this isn't always the case).

*when I say resize the window I mean make it as small as possible to the point the image can't actually be seen.

Could this be a bug in Java maybe?

From stackoverflow
  • There are several things that could influence performance here:

    • Available RAM
    • CPU speed
    • Graphic card (onboard or seperate)
    • Graphic driver
    • Java version
    • Used video mode (resolution, bitdepth, acceleration support)

    EDIT: Having a look at the edited question, I'd propose to check if the 9600GS system has the newest NVIDIA drivers installed. I recently installed a driver for an Intel onboard graphics card that replaced the generic Windows driver and made moving windows, watching videos, browsing etc. a lot faster.

    All the other specs look good. Perhaps Java doesn't detect the 9600GS and doesn't use hardware acceleration, but I doubt this.

    Also check the OS configuration. On Windows, you can turn off hardware acceleration for debugging purposes.

    Of course the best way to handle this would be to change your code - resize the image or split it up into chunks as DNS proposed. You'll never be able to see the whole image as it is on the screen.

  • How are you judging the computers' power? A 50x25 K 32-bit image takes more than 4.5 GB RAM to hold in memory (50000 * 25000 * 4 bytes). If one computer has more RAM than another, that can make a huge difference in speed, because it won't have to swap to disk as often. You should consider grabbing subsections of the image and working with those, instead of the whole thing.

    Edit: Are you using the latest Java & graphics drivers? If your image is only 5Kx2.5K, the only thing I can think of is that it's doing it without any hardware acceleration.

  • What is different, what is the same ? "Some computers" is too vague - are the operating systems the same ? Same versions ? Are your Java installations all the same version ?

  • Check the screen settings. My bet is that pixel depth is different on the two systems, and that the slow one has an odd pixel depth related to the image object you are trying to display.

  • Since Java uses OpenGL to do 2D drawing, the performance of your app will be affected by the OpenGL performance of the graphics chip in the respective computer. Support for OpenGL is dwindling in the 3D industry, which means that (ironically) newer chips may be slower at OpenGL rendering than older ones - not only due to hardware but also drivers.

  • If you are using sun's java try some of the following system properties, either as command line parameters or the first lines in main

    sun.java2d.opengl=true //force ogl
    sun.java2d.ddscale=true //only when using direct3d
    sun.java2d.translaccel=true //only when using direct3d

    more flags can be viewed at this page Look at sun.java2d.trace which can allow you to

    determine the source of less-than-desirable graphics performance

  • Performance of writing an image to a screen is very much affected by the format in which the image is stored. If the format is the same as the screen memory wants then it can be very fast; if it is not then a conversion must be done, sometimes pixel by pixel, which is very slow.

    If you have any control over how the image is stored, you should store it in a format that the screen is looking for. Here is some sample code:

        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice device = env.getDefaultScreenDevice();
        GraphicsConfiguration config = device.getDefaultConfiguration();
        BufferedImage buffy = config.createCompatibleImage(width, height, Transparency.TRANSLUCENT);
    

    If you are going to draw the image many times it may be worth converting to a compatible format even if it came in some other format.

    Drawing an image will also be slower if you are transforming it as you draw, which the 'resizing' part of your description makes me think you might be.

JMS alternative? something for decoupling sending emails from http reqs

Hi,

we have a web application that does various things and sometimes emails users depending on a given action. I want to decouple the http request threads from actually sending the email in case there is some trouble with the SMTP server or a backlog. In the past I've used JMS for this and had no problem with it. However at the moment for the web app we're doing JMS just feels a bit of an over kill right now (in terms of setup etc) and I was wondering what other alternative there are out there. Ideally I just like something that I can run in-process (JVM/Tomcat), but when the servlet context is unloaded any pending items in the queue would be swapped to disk/db. I could of course just code something together involving an in memory Q, but I'm looking to gain the benfit of opensource projects, so wondering whats out there if anything.

If JMS really is the answer anyone know of somethign that could fit our simple requirements. thanks

From stackoverflow
  • I agree that JMS is overkill for this.

    You can just send the e-mail in a separate thread (i.e. separate from the request handling thread). The only thing to be careful about is that if your app gets any kind of traffic at all, you may want to use a thread pool to avoid resource depletion issues. The java.util.concurrent package has some nice stuff for thread pools.

    toolkit : last time I looked, the use application-created threads was considered non-portable, since different servlet containers might limit the creation of Thread instances.
    Willie Wheeler : I believe that thread creation happens through the SecurityManager, which the admin can configure as desired.
  • You could use a scheduler. Have a look at Quartz.

    The idea is that you schedule a job to start at regular intervals. All requests need to be persisted somewhere. The scheduled job will read them and process them. You need to define the interval between two subsequent jobs to fit your needs.

    This is the recommended way of doing things. Full-fledged application servers offer JEE Timers for this, but these aren't available in Tomcat. Quartz is fine though and you could avoid starting your own threads, which will cause mess in some situations (e.g. in application updates).

  • We have the exact same problem. This may sound a little simplistic but it does work:

    1. Write the request to disk to an "outgoing" mail folder.
    2. Email process reads in the request.
    3. When the message has been sent, the outgoing mail message is deleted.
    4. Plan is to use Amazon S3 as needed to help distribute the message transmission across servers if needed.
  • I'm using JMS for something similar. Our reasons for using JMS:

    • We already had a JMS server for something else (so it was just adding a new queue)
    • We wanted our application be decoupled from the processing process, so errors on either side would stay on their side
    • The app could drop the message in a queue, commit, and go on. No need to worry about how to persist the messages, how to start over after a crash, etc. JMS does all that for you.
  • Wow, this issue comes up a lot. CommonJ WorkManagager is what you are looking for. A Tomcat implementation can be found here. It allows you to safely create threads in a JEE environment but is much lighter weight than using JMS (which will obviously work as well).

  • Since you say the app "sometimes" emails users it doesn't sound like you're talking about a high volume of mail. A quick and dirty solution would be to just Runtime.getRuntime().exec():

    sendmail recipient@domain.com

    and dump the message into the resulting Process's getOutputStream(). After that it's sendmail's problem.

    Figure a minute to see if you have sendmail available on the server, about fifteen minutes to throw together a test if you do, and nothing to install assuming you found sendmail. A few more minutes to construct the email headers properly (easy - here are some examples) and you're done.

    Hope this helps...

  • I would think spring integration would work in this case as well.

    http://www.springsource.org/spring-integration

  • Beyond JMS, for short messages you could also use Amazon Simple Queue Service (SQS). While you might think it an overkill too, consider the fact there's minimal maintenance required, scales nicely, has ultra-high availability, and doesn't cost all that much. No cost for creating new queues etc; or having account. As far as I recall, it's purely based on number of operations you do (sending messages, polling/retrieving).

    Main limitation really is the message size (there are others, like not guaranteeing ordering due to distributed nature etc); but that might work as is. Or for larger messages, using related AWS service, s3, for storing actual body, and just passing headers through SQS.