You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by ch...@apache.org on 2014/07/25 12:27:50 UTC

svn commit: r1613379 - in /jackrabbit/oak/trunk/oak-run: README.md src/main/js/ src/main/js/oak-mongo.js

Author: chetanm
Date: Fri Jul 25 10:27:49 2014
New Revision: 1613379

URL: http://svn.apache.org/r1613379
Log:
OAK-1990 - Utility js methods to manage Oak data in Mongo

Added:
    jackrabbit/oak/trunk/oak-run/src/main/js/
    jackrabbit/oak/trunk/oak-run/src/main/js/oak-mongo.js   (with props)
Modified:
    jackrabbit/oak/trunk/oak-run/README.md

Modified: jackrabbit/oak/trunk/oak-run/README.md
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-run/README.md?rev=1613379&r1=1613378&r2=1613379&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-run/README.md (original)
+++ jackrabbit/oak/trunk/oak-run/README.md Fri Jul 25 10:27:49 2014
@@ -566,3 +566,22 @@ The following runmodes are currently ava
     * upgrade     : Upgrade from Jackrabbit 2.x repository to Oak.
     * benchmark   : Run benchmark tests against Jackrabbit 2.x repository fixture.
     * server      : Run the JR2 Server.
+
+Oak Mongo Shell Helpers
+=======================
+
+To simplify making sense of data created by Oak in Mongo a javascript file oak-mongo.js
+is provided. It includes some useful function to navigate the data in Mongo
+
+    $ wget https://svn.apache.org/repos/asf/jackrabbit/oak/trunk/oak-run/src/main/js/oak-mongo.js
+    $ mongo localhost/oak --shell oak-mongo.js
+    MongoDB shell version: 2.6.3
+    connecting to: localhost/oak
+    type "help" for help
+    > oak.countChildren('/oak:index/')
+    356787
+    > oak.getChildStats('/oak:index')
+    { "count" : 356788, "size" : 127743372, "simple" : "121.83 MB" }
+    > oak.getChildStats('/')
+    { "count" : 593191, "size" : 302005011, "simple" : "288.01 MB" }
+    >
\ No newline at end of file

Added: jackrabbit/oak/trunk/oak-run/src/main/js/oak-mongo.js
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-run/src/main/js/oak-mongo.js?rev=1613379&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-run/src/main/js/oak-mongo.js (added)
+++ jackrabbit/oak/trunk/oak-run/src/main/js/oak-mongo.js Fri Jul 25 10:27:49 2014
@@ -0,0 +1,137 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+/*global print, _, db, Object, ObjectId */
+
+var oak = (function(global){
+    "use strict";
+
+    var api;
+
+    api = function(){
+
+    };
+
+    api.indexStats = function () {
+        var result = [];
+        var totalCount = 0;
+        var totalSize = 0;
+        db.nodes.find({'_id': /^2\:\/oak\:index\//}, {_id: 1}).forEach(function (doc) {
+            var stats = api.getChildStats(api.pathFromId(doc._id));
+            stats.id = doc._id;
+            result.push(stats);
+
+            totalCount += stats.count;
+            totalSize += stats.size;
+        });
+
+        result.push({id: "summary", count: totalCount, size: totalSize, "simple": this.humanFileSize(totalSize)});
+        return result;
+    };
+
+    /**
+     * Determines the number of child node (including all sub tree)
+     * for a given parent node path. This would be faster compared to
+     * {@link getChildStats} as it does not load the doc and works on
+     * index only.
+     *
+     * Note that there might be some difference between db.nodes.count()
+     * and countChildren('/') as split docs, intermediate docs are not
+     * accounted for
+     *
+     * @param path
+     * @returns {number}
+     */
+    api.countChildren = function(path){
+        var depth = this.pathDepth(path);
+        var totalCount = 0;
+        while (true) {
+            var count = db.nodes.count({_id: this.pathFilter(depth++, path)});
+            if( count === 0){
+                break;
+            }
+            totalCount += count;
+        }
+        return totalCount;
+    };
+
+    /**
+     * Provides stats related to number of child nodes
+     * below given path or total size taken by such nodes
+     *
+     * @param path
+     * @returns {{count: number, size: number}}
+     */
+    api.getChildStats = function(path){
+        var count = 0;
+        var size = 0;
+        this.forEachChild(path, function(doc){
+            count++;
+            size +=  Object.bsonsize(doc);
+        });
+        return {"count" : count, "size" : size, "simple" : this.humanFileSize(size)};
+    };
+
+    /**
+     * Performs a breadth first traversal for nodes under given path
+     * and invokes the passed function for each child node
+     *
+     * @param path
+     * @param callable
+     */
+    api.forEachChild = function(path, callable) {
+        var depth = this.pathDepth(path);
+        while (true) {
+            var cur = db.nodes.find({_id: this.pathFilter(depth++, path)});
+            if(!cur.hasNext()){
+                break;
+            }
+            cur.forEach(callable);
+        }
+    };
+
+    api.pathFilter = function (depth, prefix){
+        return new RegExp("^"+ depth + ":" + prefix);
+    };
+
+    api.pathDepth = function(path){
+        if(path === '/'){
+            return 0;
+        }
+        var depth = 0;
+        for(var i = 0; i < path.length; i++){
+            if(path.charAt(i) === '/'){
+                depth++;
+            }
+        }
+        return depth;
+    };
+
+    api.pathFromId = function(id) {
+        var index = id.indexOf(':');
+        return id.substring(index + 1);
+    };
+
+    //http://stackoverflow.com/a/20732091/1035417
+    api.humanFileSize = function (size) {
+        var i = Math.floor( Math.log(size) / Math.log(1024) );
+        return ( size / Math.pow(1024, i) ).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i];
+    };
+
+    return api;
+}(this));
\ No newline at end of file

Propchange: jackrabbit/oak/trunk/oak-run/src/main/js/oak-mongo.js
------------------------------------------------------------------------------
    svn:eol-style = native