Skip to content

Instantly share code, notes, and snippets.

@magikcypress
Created November 15, 2012 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magikcypress/4081489 to your computer and use it in GitHub Desktop.
Save magikcypress/4081489 to your computer and use it in GitHub Desktop.
CouchDB

CouchDB

Authentification

Create a _users database on couchdb

With futon : http://127.0.0.1:5984/_utils/

Create a admin users on _users database ====

 {
    "_id": "org.couchdb.user:admin",
    "_rev": "1-463483e542b8bb5e3fa7bc1753674bc9",
    "type": "user",
    "roles": [
        "erlanger"
    ],
    "password_sha": "55403b0975fd10dad41f83b46c089d919d039bf2",
    "salt": "52ae61c1212c11b3143e2d391c7515a2",
    "name": "admin"
 }

Activate a user authentification

In config couchdb http://127.0.0.1:5984/_utils/config.html

Change false by true for require_valid_user on couch_httpd_auth section

Create au password_sha and salt

 import hashlib
 import os
 import sys
 
 if len(sys.argv) < 2:
   sys.exit('Usage: %s password' % sys.argv[0])
 
 salt = os.urandom(16).encode('hex')
 h=hashlib.sha1()
 h.update(sys.argv[1])
 h.update(salt)
 h.digest()
 print "passhword_sha: " + h.hexdigest()
 print "salt: " + salt

Views (_Design)

If you do not want to search for a long time.

Read this doc : http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views

Test your view : http://blog.mudynamics.com/wp-content/uploads/2009/04/icouch.html

Rechercher le nombre de tag dans plusieurs listes

function (doc) {
    if (doc.doc_type == 'Post') {
      var emit_sequence = function(base, disp) {
       if (disp.length > 1) {
         emit(base.concat(disp[0]), 1);
         emit_sequence(base, disp.slice(1, disp.length));
       } else if (disp.length == 1) {
       emit(base.concat(disp[0]), 1);
       }
      }
    emit_sequence([], doc.tags); 
    }
 }

Rechercher le nombre de tag ajouté par un utilisateur

 function(doc) {
    var emit_sequence = function(base, disp) {
      var key = { num: base.concat(disp[0]), user: doc.user };
      if (disp.length > 1) {
        emit((key.num, key.user), 1);
        emit_sequence(base, disp.slice(1, disp.length));
      } else if (disp.length == 1) {
      emit((key.num, key.user), 1);
    }
 }
 emit_sequence([], doc.tags); 

Rechercher le commentaire parent et l'afficher dans key

 function(doc) {
 if (doc.doc_type == 'Post') {
  if (doc.comments == []) return;
      var emit_seq = function(base, disp) {
  	  for (var i in disp) {
		var num = [disp[i].uuid, 
		     	   disp[i].comment_id].length;
		if (disp[i].comment_id) {
		     emit([disp[i].uuid, disp[i].comment_id], num);
		} else if (!disp[i].comment_id) {
		     emit(disp[i].uuid, 1);
		}
	}
 }
 emit_seq([], doc.comments);
  }
 }

Rechercher le commentaire parent et l'afficher dans value

map.js

 function(doc) {
 if (doc.doc_type == 'Post') {
     if (doc.comments == []) return;
     for (var i in doc.comments) {
  	  emit(doc.comments, {uuid: doc.comments[i].uuid, comment_id: doc.comments[i].comment_id});
     }
    }
  }

reduce.js

 function(keys, values, rereduce) {
     log([ keys, values, rereduce ]);
     var rv = {};
     for (i in values) {
        var value = values[i];
        for (k in value) {
            rv[k] = (rv[k] || ',') + value[k];
        }
     }
     return rv;
  }

Rechercher les uuid commentaires dans la key et l'afficher uuid, author et comment_id dans value

  function(doc) {
     if ('comments' in doc) {
       doc.comments.forEach(function(comment) {
                  if ('uuid' in comment) {
                    var key = comment.uuid,
                      value = { uuid: doc.uuid,
                               author: comment.author,
 			  comment_id: comment.comment_id };
                     emit(key, value);
                 }
      });
    }
   }

Scripts

Test bench couchdb

    #!/bin/bash -e
    # Licensed under the Apache License, Version 2.0 (the "License"); you may not
    # use this file except in compliance with the License. You may obtain a copy of
    # the License at
    #
    #   http://www.apache.org/licenses/LICENSE-2.0
    #
    # Unless required by applicable law or agreed to in writing, software
    # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    # License for the specific language governing permissions and limitations under
    # the License.
    #
    
    # usage: time benchbulk.sh
    # it takes about 30 seconds to run on my old MacBook with bulksize 1000
    
    BULKSIZE=100
    DOCSIZE=10
    INSERTS=10
    ROUNDS=10
    DBURL="http://admin:passwd@0.0.0.0:5984/benchbulk"
    POSTURL="$DBURL/_bulk_docs"
    
    make_bulk_docs() {
    ROW=0
    SIZE=$(($1-1))
    START=$2
    BODYSIZE=$3  
    
    BODY=$(printf "%0${BODYSIZE}d")
    
    echo '{"docs":['
    while [ $ROW -lt $SIZE ]; do
        printf '{"_id":"%020d", "body":"'$BODY'"},' $(($ROW + $START))
        let ROW=ROW+1
    done
    printf '{"_id":"%020d", "body":"'$BODY'"}' $(($ROW + $START))
    echo ']}'
    }
    
    echo "Making $INSERTS bulk inserts of $BULKSIZE docs each"
    
    echo "Attempt to delete db at $DBURL"
    curl -X DELETE $DBURL -w\\n
    
    echo "Attempt to create db at $DBURL"
    curl -X PUT $DBURL -w\\n
    
    echo "Running $ROUNDS rounds of $INSERTS concurrent inserts to $POSTURL"
    RUN=0
    while [ $RUN -lt $ROUNDS ]; do
    
    POSTS=0
    while [ $POSTS -lt $INSERTS ]; do
        STARTKEY=$[ POSTS * BULKSIZE + RUN * BULKSIZE * INSERTS ]
        echo "startkey $STARTKEY bulksize $BULKSIZE"
        DOCS=$(make_bulk_docs $BULKSIZE $STARTKEY $DOCSIZE)
        # echo $DOCS
        echo $DOCS | curl -T - -H Content-Type:application/json -X POST $POSTURL -w%{http_code}\ %{time_total}\ sec\\n >/dev/null 2>&1 &
        let POSTS=POSTS+1
    done
    
    echo "waiting"
    wait
    let RUN=RUN+1
    done
    
    curl $DBURL -w\\n

source : https://gist.github.com/1971611

delete document couchdb

    #!/bin/bash -e
    # Licensed under the Apache License, Version 2.0 (the "License"); you may not
    # use this file except in compliance with the License. You may obtain a copy of
    # the License at
    #
    #   http://www.apache.org/licenses/LICENSE-2.0
    #
    # Unless required by applicable law or agreed to in writing, software
    # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    # License for the specific language governing permissions and limitations under
    # the License.
    # 
    
    # curl -X DELETE http://admin:passwd@0.0.0.0:5984/benchbulk/00000000000000000001?rev=1-ca73793e047eb5e266cb27fd5952f89b
    # usage: time delete_document_couchdb.sh
    
    INSERTS=10001
    DBURL="http://admin:passwd@0.0.0.0:5984/benchbulk"
    
    get_docs() {
    START=$1
    printf "%020d" $START
    }
    
    echo "Making $INSERTS bulk inserts of $BULKSIZE docs each"
    
    POSTS=0
    while [ $POSTS -lt $INSERTS ]; do
        STARTKEY=$[ POSTS ]
        echo "startkey $STARTKEY"
        DOCS=$(get_docs $STARTKEY)
        echo $DOCS
        # echo $DOCS | curl -T - -H Content-Type:application/json -X POST $DBURL
        REV=$(curl -T - -H Content-Type:application/json -X GET $DBURL/$DOCS | awk -F\" '{print $8}')
        DEL=$(curl -T - -H Content-Type:application/json -X DELETE $DBURL/$DOCS\?rev\=1-ca73793e047eb5e266cb27fd5952f89b)
        let POSTS=POSTS+1
    done
    
    curl $DBURL -w\\n

Test Apache ab

No keep-alive

  $ ab -A admin:passwd -n 1024 -c 1 https://node.rouquin.me:6984/

With keep-alive

  $ ab -A admin:passwd -n 1024 -c 1 -k https://node.rouquin.me:6984/

Config test performance

  [httpd]
  socket_options = [{nodelay, true}]

http://wiki.apache.org/couchdb/Performance

Documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment