• 2013-07-17

    Iterate through zset in SSDB with PHP

    Views: 6931 | No Comments

    Connect to SSDB

    <?php
    require_once("SSDB.php");
    $ssdb = new SimpleSSDB('127.0.0.1', 8888);
    

    Generate test data

    $zname = 'test';
    for($i=0; $i<1000; $i++){
        $key = "k$i";
        $score = mt_rand(0, 100);
        $ssdb->zset($zname, $key, $score);
    }
    $size = $ssdb->zsize($zname);
    echo "total $size items\n";
    

    With this php codes, it will create a zset named zname, and insert 1000 items. Items may have same scores.

    Iterate

    $num = 1;
    $key_start = ''; 
    $score_start = ''; 
    while(1){
        $items = $ssdb->zscan($zname, $key_start, $score_start, '', 10);
        if(!$items){
            break;
        }   
        foreach($items as $key=>$score){
            printf("%5d: %-5s = %5d\n", $num, $key, $score);
            // remember the max key and its score
            $key_start = $key;
            $score_start = $score;
            $num += 1;
        }   
        echo "---\n";
    }
    

    Though we know there are 1000 items in the zset in this case, but for most chances there would be more items in one zset. So we cannot get all items at once.

    As demostration, we choose to get 10 items at a time, then get other 10 items after those. Repeat the same operation until we iterate through all items in the zset.

    Posted by ideawu at 2013-07-17 18:42:37 Tags:
  • 2013-07-11

    The C++ API for SSDB

    Views: 3073 | No Comments
    #include "link.h"
    
    Link *link = Link::connect(ip, port);
    const std::vector<Bytes> *resp = link->request("get", "a");
    if(resp == NULL){
        // error
    }else{
        // ok
    }
    
    Posted by ideawu at 2013-07-11 11:32:37
  • 2013-07-11

    Import existing LevelDB into SSDB

    Views: 7078 | 4 Comments

    Since 1.5.4, ssdb provides the leveldb-import tool to import a existing LevelDB into ssdb server. This tool is quite easy to use:

    Example:
        ./tools/leveldb-import 127.0.0.1 8888 ./leveldb/
    
    Usage:
        ./tools/leveldb-import ip port input_folder
    
    Options:
        ip - ssdb server ip address
        port - ssdb server port number
        input_folder - local leveldb folder
    
    Posted by ideawu at 11:26:36 Tags:
  • 2013-06-04

    Best Practice of JavaScript Inheritence

    Views: 4217 | No Comments

    Unlike other OO languages such as Java, JavaScript have no class grammar, the most similar concept to class is function. These codes below shows how to define a JavaScript class:

    function Base(arg){
        var self = this;
        self.base = 1;
        self.name = 'base';
    }
    

    But how to inherits from other clsss? You need to use prototype.

    function Child(arg){
        var self = this;
        self.name = 'child';
    }
    
    // Magic happens here!
    Child.prototype = new Base();
    Child.prototype.constructor = Child;
    

    You can now use Child as usual:

    var child = new Child();
    alert(child.base + ', ' + child.name); // 1, child
    

    You may want to wrap up the inheritence operation in one function:

    function class_extend(child, base){
        child.prototype = new base();
        child.prototype.constructor = child;
    }
    class_extend(Child, Base);
    
    Posted by ideawu at 2013-06-04 11:10:23
  • 2013-05-31

    Nginx 499 error code and AJAX termination

    Views: 10496 | No Comments

    In a recent project, we encounter strange Nginx 499 error code problem. The web page bind an event handler to an “a” tag, which invoke an AJAX operation, as shown below

    <a class="mybutton">Click Me</a>
    
    $('a.mybutton').click(function(){
        // ajax
    });
    

    The code works fine on most browsers, but doesn’t work on an embed web browser widget. I finally found out that it was an IE6 bug. The browser widget terminates the AJAX request after click event handler ends. To prevent this, you must return false in the event handler

    <a class="mybutton">Click Me</a>
    
    $('a.mybutton').click(function(){
        // ajax
        return false;
    });
    
    Posted by ideawu at 2013-05-31 10:35:14 Tags: , ,
|<<<5678910111213>>>| 9/19 Pages, 91 Results.