Example usage for com.mongodb CommandResult get

List of usage examples for com.mongodb CommandResult get

Introduction

In this page you can find the example usage for com.mongodb CommandResult get.

Prototype

public Object get(final String key) 

Source Link

Document

Gets a value from this object

Usage

From source file:com.edgytech.umongo.ServerPanel.java

License:Apache License

public void getLog(ButtonBase button) {
    final DB db = getServerNode().getServerMongoClient().getDB("admin");
    final String type = getStringFieldValue(Item.getLogType);
    final DBObject cmd = new BasicDBObject("getLog", type);
    new DbJob() {

        @Override//ww  w . j a  v a  2 s . c om
        public Object doRun() throws Exception {
            CommandResult res = db.command(cmd);
            res.throwOnError();
            StringBuilder sb = new StringBuilder();
            BasicDBList list = (BasicDBList) res.get("log");
            for (Object str : list) {
                sb.append(str);
                sb.append("\n");
            }
            return sb.toString();
        }

        @Override
        public String getNS() {
            return db.getName();
        }

        @Override
        public String getShortName() {
            return cmd.keySet().iterator().next();
        }

    }.addJob();
}

From source file:com.espirit.moddev.examples.uxbridge.newsdrilldown.jpa.NewsHandler.java

License:Apache License

/**
 * Mongo ID generation like it is done in the grails gorm framework
 *
 * @param collectionName//from w ww .java  2 s.c o m
 *            The name of the collection the id should be generated for
 * @param db
 *            The mongodb connection
 * @return a new id
 */
private Long generateIdentifier(String collectionName, DB db) {

    // get or create the Collection for the ID storage
    DBCollection dbCollection = db.getCollection(collectionName + ".next_id");
    // create entry to store the newly generated id
    DBObject nativeEntry = new BasicDBObject();

    while (true) {
        DBCursor result = dbCollection.find().sort(new BasicDBObject("_id", -1)).limit(1);

        long nextId;
        if (result.hasNext()) {
            final Long current = (Long) result.next().get("_id");
            nextId = current + 1;
        } else {
            nextId = 1;
        }

        nativeEntry.put("_id", nextId);
        final WriteResult writeResult = dbCollection.insert(nativeEntry);
        final CommandResult lastError = writeResult.getLastError();
        if (lastError.ok()) {
            break;
        }

        final Object code = lastError.get("code");
        // duplicate key error try again
        if (code != null && code.equals(11000)) {
            continue;
        }
        break;
    }

    return (Long) nativeEntry.get("_id");
}

From source file:com.hangum.tadpole.mongodb.core.editors.dbInfos.comosites.InstanceInformationComposite.java

License:Open Source License

/**
 * Create the composite.//from w w  w.j ava 2  s.c  o  m
 * @param parent
 * @param style
 */
public InstanceInformationComposite(Composite parent, int style, UserDBDAO userDB,
        CommandResult commandResult) {
    super(parent, style);
    GridLayout gridLayout = new GridLayout(1, false);
    gridLayout.verticalSpacing = 1;
    gridLayout.horizontalSpacing = 1;
    gridLayout.marginHeight = 1;
    gridLayout.marginWidth = 1;
    setLayout(gridLayout);

    this.userDB = userDB;

    compositeServerStatus = new Composite(this, SWT.NONE);
    compositeServerStatus.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
    GridLayout gl_compositeServerStatus = new GridLayout(1, false);
    gl_compositeServerStatus.verticalSpacing = 2;
    gl_compositeServerStatus.horizontalSpacing = 2;
    gl_compositeServerStatus.marginHeight = 2;
    gl_compositeServerStatus.marginWidth = 2;
    compositeServerStatus.setLayout(gl_compositeServerStatus);

    // monitoring start, stop
    Composite compositeHead = new Composite(compositeServerStatus, SWT.NONE);
    compositeHead.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1));
    GridLayout gl_compositeHead = new GridLayout(3, false);
    gl_compositeHead.verticalSpacing = 2;
    gl_compositeHead.horizontalSpacing = 2;
    gl_compositeHead.marginHeight = 2;
    gl_compositeHead.marginWidth = 2;
    compositeHead.setLayout(gl_compositeHead);

    Label lblMonitoring = new Label(compositeHead, SWT.NONE);
    lblMonitoring.setText("Monitoring");

    btnStart = new Button(compositeHead, SWT.NONE);
    btnStart.setBounds(0, 0, 94, 28);
    btnStart.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            startInstanceMon();
        }
    });
    btnStart.setText("Start");

    btnStop = new Button(compositeHead, SWT.NONE);
    btnStop.setEnabled(false);
    btnStop.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            stopInstanceMon();
        }
    });
    btnStop.setText("Stop");

    // create information
    createInstanceInformation();

    Composite cmpMemory = new Composite(compositeServerStatus, SWT.NONE);
    GridLayout gl_grpMemory = new GridLayout(2, false);
    gl_grpMemory.verticalSpacing = 2;
    gl_grpMemory.horizontalSpacing = 2;
    gl_grpMemory.marginHeight = 2;
    gl_grpMemory.marginWidth = 2;
    cmpMemory.setLayout(gl_grpMemory);
    cmpMemory.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

    createConnectionChart(cmpMemory, commandResult);
    createNetworkInformation(cmpMemory, commandResult);

    // show connection, cursor
    Composite cmpConnections = new Composite(compositeServerStatus, SWT.NONE);
    GridLayout gl_grpConnections = new GridLayout(2, false);
    gl_grpConnections.verticalSpacing = 1;
    gl_grpConnections.horizontalSpacing = 1;
    gl_grpConnections.marginHeight = 1;
    gl_grpConnections.marginWidth = 1;
    cmpConnections.setLayout(gl_grpConnections);
    cmpConnections.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

    createMemeoryInformation(cmpConnections, commandResult);
    createCursorsChart(cmpConnections, commandResult);

    // show extra information
    Group grpExtraInfo = new Group(compositeServerStatus, SWT.NONE);
    GridLayout gl_grpExtraInfo = new GridLayout(1, false);
    gl_grpExtraInfo.verticalSpacing = 2;
    gl_grpExtraInfo.horizontalSpacing = 2;
    gl_grpExtraInfo.marginHeight = 2;
    gl_grpExtraInfo.marginWidth = 2;
    grpExtraInfo.setLayout(gl_grpExtraInfo);
    grpExtraInfo.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));
    grpExtraInfo.setText("Extra Information");

    Composite compositeExtraInfo = new FindOneDetailComposite(grpExtraInfo, "Extra Information",
            (DBObject) commandResult.get("extra_info"), false);
    compositeExtraInfo.setLayout(new GridLayout(1, false));
    compositeExtraInfo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
}

From source file:com.hangum.tadpole.mongodb.core.editors.dbInfos.comosites.InstanceInformationComposite.java

License:Open Source License

/**
 * refresh memory//w w w . j  av a 2  s  . c o  m
 * 
 * @param commandResult
 */
private void refreshMemoryData(CommandResult commandResult) {
    DBObject cursorConnections = (DBObject) commandResult.get("mem");
    int bits = cursorConnections == null ? 0 : ENumberUtils.toInt(cursorConnections.get("bits"));
    int resident = cursorConnections == null ? 0 : ENumberUtils.toInt(cursorConnections.get("resident"));
    int virtual = cursorConnections == null ? 0 : ENumberUtils.toInt(cursorConnections.get("virtual"));

    int mapped = cursorConnections == null ? 0 : ENumberUtils.toInt(cursorConnections.get("mapped"));
    int mappedWithJournal = cursorConnections == null ? 0
            : ENumberUtils.toInt(cursorConnections.get("mappedWithJournal"));

    float fBits = virtual == 0 ? 0 : (float) bits / (float) virtual;
    float fResident = virtual == 0 ? 0 : (float) resident / (float) virtual;
    float fVirtual = 0.8f;
    float fMapped = virtual == 0 ? 0 : (float) mapped / (float) virtual;
    float fMappedWithJournal = virtual == 0 ? 0 : (float) mappedWithJournal / (float) virtual;

    ChartItem itemAvailable = barChartMemory.getItems()[0];
    itemAvailable.setText("In (" + NumberFormatUtils.kbMbFormat(bits) + ")");
    itemAvailable.setValue(fBits);

    ChartItem itemCurrent = barChartMemory.getItems()[1];
    itemCurrent.setText("Out (" + NumberFormatUtils.kbMbFormat(resident) + ")");
    itemCurrent.setValue(fResident);

    ChartItem itemNumRequests = barChartMemory.getItems()[2];
    itemNumRequests.setText("Vitrual (" + NumberFormatUtils.commaFormat(virtual) + ")");
    itemNumRequests.setValue(fVirtual);

    ChartItem itemMapped = barChartMemory.getItems()[3];
    itemMapped.setText("Mapped (" + NumberFormatUtils.commaFormat(mapped) + ")");
    itemMapped.setValue(fMapped);

    ChartItem itemMappedWithJournal = barChartMemory.getItems()[4];
    itemMappedWithJournal
            .setText("Mapped With Journal (" + NumberFormatUtils.commaFormat(mappedWithJournal) + ")");
    itemMappedWithJournal.setValue(fMappedWithJournal);
}

From source file:com.hangum.tadpole.mongodb.core.editors.dbInfos.comosites.InstanceInformationComposite.java

License:Open Source License

/**
 * refresh network/*ww w . j av a 2s . com*/
 * @param commandResult
 */
private void refreshNetwork(CommandResult commandResult) {
    DBObject cursorConnections = (DBObject) commandResult.get("network");
    int bytesIn = cursorConnections == null ? 0 : ENumberUtils.toInt(cursorConnections.get("bytesIn"));
    int bytesOut = cursorConnections == null ? 0 : ENumberUtils.toInt(cursorConnections.get("bytesOut"));
    int numRequests = cursorConnections == null ? 0 : ENumberUtils.toInt(cursorConnections.get("numRequests"));

    float floatBI = 0f, floatBO = 0f, floatNf = 0f;
    if (bytesIn < bytesOut) {
        floatBI = (float) bytesIn / (float) bytesOut;
        floatBO = 0.8f;
        floatNf = (float) numRequests / (float) bytesOut;
    } else if (bytesIn == 0 || bytesOut == 0) {
        floatBI = 0.0f;
        floatBO = 0.0f;
        floatNf = 0.0f;
    } else {
        floatBI = 0.0f;
        floatBO = (float) bytesOut / (float) bytesIn;
        floatNf = (float) numRequests / (float) bytesIn;
    }

    ChartItem itemAvailable = barChartNetwork.getItems()[0];
    itemAvailable.setText("In (" + NumberFormatUtils.kbMbFormat(bytesIn) + ")");
    itemAvailable.setValue(floatBI);

    ChartItem itemCurrent = barChartNetwork.getItems()[1];
    itemCurrent.setText("Out (" + NumberFormatUtils.kbMbFormat(bytesOut) + ")");
    itemCurrent.setValue(floatBO);

    ChartItem itemNumRequests = barChartNetwork.getItems()[2];
    itemNumRequests.setText("Requests (" + NumberFormatUtils.commaFormat(numRequests) + ")");
    itemNumRequests.setValue(floatNf);
}

From source file:com.hangum.tadpole.mongodb.core.editors.dbInfos.comosites.InstanceInformationComposite.java

License:Open Source License

/**
 * refresh connections//from ww w . j a va2 s .c o m
 * 
 * @param commandResult
 */
private void refreshConnections(CommandResult commandResult) {
    DBObject cursorConnections = (DBObject) commandResult.get("connections");
    int current = cursorConnections == null ? 0 : ENumberUtils.toInt(cursorConnections.get("current"));
    int available = cursorConnections == null ? 0 : ENumberUtils.toInt(cursorConnections.get("available"));
    float floatCurrent = available == 0 ? 0 : (float) current / (float) available;

    ChartItem itemAvailable = barChartConnection.getItems()[0];
    itemAvailable.setText("Available (" + NumberFormatUtils.commaFormat(available) + ")");
    itemAvailable.setValue(0.80f);

    ChartItem itemCurrent = barChartConnection.getItems()[1];
    itemCurrent.setText("Current (" + NumberFormatUtils.commaFormat(current) + ")");
    itemCurrent.setValue(floatCurrent);
}

From source file:com.hangum.tadpole.mongodb.core.editors.dbInfos.comosites.InstanceInformationComposite.java

License:Open Source License

/**
 * refresh cursors//from   w ww  .ja  va2 s  . com
 * 
 * @param commandResult
 */
private void refreshCursors(CommandResult commandResult) {
    DBObject cursorCursors = (DBObject) commandResult.get("cursors");
    int totalOpen = cursorCursors == null ? 0 : ENumberUtils.toInt(cursorCursors.get("totalOpen"));
    int clientCursors_size = cursorCursors == null ? 0
            : ENumberUtils.toInt(cursorCursors.get("clientCursors_size"));
    int timedOut = cursorCursors == null ? 0 : ENumberUtils.toInt(cursorCursors.get("timedOut"));

    ChartItem itemTotalOpen = pieChartCursors.getItems()[0];
    itemTotalOpen.setText("Total Open (" + totalOpen + ")");
    itemTotalOpen.setValue(totalOpen);

    ChartItem itemClientCursors_size = pieChartCursors.getItems()[1];
    itemClientCursors_size.setText("Client cursors size (" + clientCursors_size + ")");
    itemClientCursors_size.setValue(clientCursors_size);

    ChartItem itemTimedOut = pieChartCursors.getItems()[1];
    itemTimedOut.setText("Timed Out (" + timedOut + ")");
    itemTimedOut.setValue(timedOut);
}

From source file:com.hangum.tadpole.mongodb.core.editors.dbInfos.comosites.InstanceInformationComposite.java

License:Open Source License

/**
 * Show memory information/*  w w  w.ja v a  2 s  .  co  m*/
 */
private void createMemeoryInformation(Composite cmpMemory, CommandResult commandResult) {
    Group compositeMemory = new Group(cmpMemory, SWT.NONE);
    compositeMemory.setLayout(new GridLayout(1, false));
    compositeMemory.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
    compositeMemory.setText("Memory");

    ColorStream colors = Colors.cat20Colors(compositeMemory.getDisplay()).loop();

    barChartMemory = new BarChart(compositeMemory, SWT.NONE);
    barChartMemory.setLayout(new GridLayout(1, false));
    barChartMemory.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
    barChartMemory.setBarWidth(25);

    DBObject cursorConnections = (DBObject) commandResult.get("mem");
    int bits = cursorConnections == null ? 0 : ENumberUtils.toInt(cursorConnections.get("bits"));
    int resident = cursorConnections == null ? 0 : ENumberUtils.toInt(cursorConnections.get("resident"));
    int virtual = cursorConnections == null ? 0 : ENumberUtils.toInt(cursorConnections.get("virtual"));

    int mapped = cursorConnections == null ? 0 : (Integer) cursorConnections.get("mapped");
    int mappedWithJournal = 0;
    try {
        mappedWithJournal = cursorConnections == null ? 0
                : (Integer) cursorConnections.get("mappedWithJournal");
    } catch (Exception e) {
    }

    float fBits = virtual == 0 ? 0 : (float) bits / (float) virtual;
    float fResident = virtual == 0 ? 0 : (float) resident / (float) virtual;
    float fVirtual = 0.8f;
    float fMapped = virtual == 0 ? 0 : (float) mapped / (float) virtual;
    float fMappedWithJournal = virtual == 0 ? 0 : (float) mappedWithJournal / (float) virtual;

    ChartItem itemAvailable = new ChartItem(barChartMemory);
    itemAvailable.setText("In (" + NumberFormatUtils.kbMbFormat(bits) + ")");
    itemAvailable.setColor(colors.next());
    itemAvailable.setValue(fBits);

    ChartItem itemCurrent = new ChartItem(barChartMemory);
    itemCurrent.setText("Out (" + NumberFormatUtils.kbMbFormat(resident) + ")");
    itemCurrent.setColor(colors.next());
    itemCurrent.setValue(fResident);

    ChartItem itemNumRequests = new ChartItem(barChartMemory);
    itemNumRequests.setText("Vitrual (" + NumberFormatUtils.commaFormat(virtual) + ")");
    itemNumRequests.setColor(colors.next());
    itemNumRequests.setValue(fVirtual);

    ChartItem itemMapped = new ChartItem(barChartMemory);
    itemMapped.setText("Mapped (" + NumberFormatUtils.commaFormat(mapped) + ")");
    itemMapped.setColor(colors.next());
    itemMapped.setValue(fMapped);

    ChartItem itemMappedWithJournal = new ChartItem(barChartMemory);
    itemMappedWithJournal
            .setText("Mapped With Journal (" + NumberFormatUtils.commaFormat(mappedWithJournal) + ")");
    itemMappedWithJournal.setColor(colors.next());
    itemMappedWithJournal.setValue(fMappedWithJournal);
}

From source file:com.hangum.tadpole.mongodb.core.editors.dbInfos.comosites.InstanceInformationComposite.java

License:Open Source License

/**
 * Show network information.//  www.j av a 2  s  .  co  m
 */
private void createNetworkInformation(Composite cmpMemory, CommandResult commandResult) {
    Group compositeNetwork = new Group(cmpMemory, SWT.NONE);
    compositeNetwork.setLayout(new GridLayout(1, false));
    compositeNetwork.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
    compositeNetwork.setText("Network");

    ColorStream colors = Colors.cat20Colors(compositeNetwork.getDisplay()).loop();

    barChartNetwork = new BarChart(compositeNetwork, SWT.NONE);
    barChartNetwork.setLayout(new GridLayout(1, false));
    barChartNetwork.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
    barChartNetwork.setBarWidth(25);

    try {
        DBObject cursorConnections = (DBObject) commandResult.get("network");
        int bytesIn = cursorConnections == null ? 0 : ENumberUtils.toInt(cursorConnections.get("bytesIn"));
        int bytesOut = cursorConnections == null ? 0 : ENumberUtils.toInt(cursorConnections.get("bytesOut"));
        int numRequests = cursorConnections == null ? 0
                : ENumberUtils.toInt(cursorConnections.get("numRequests"));

        float floatBI = 0f, floatBO = 0f, floatNf = 0f;
        if (bytesIn < bytesOut) {
            floatBI = (float) bytesIn / (float) bytesOut;
            floatBO = 0.8f;
            floatNf = (float) numRequests / (float) bytesOut;
        } else if (bytesIn == 0) {
            floatBI = 0.0f;
            floatBO = 0.0f;
            floatNf = 0.0f;
        } else {
            floatBI = 0.0f;
            floatBO = (float) bytesOut / (float) bytesIn;
            floatNf = (float) numRequests / (float) bytesIn;
        }

        ChartItem itemAvailable = new ChartItem(barChartNetwork);
        itemAvailable.setText("In (" + NumberFormatUtils.kbMbFormat(bytesIn) + ")");
        itemAvailable.setColor(colors.next());
        itemAvailable.setValue(floatBI);

        ChartItem itemCurrent = new ChartItem(barChartNetwork);
        itemCurrent.setText("Out (" + NumberFormatUtils.kbMbFormat(bytesOut) + ")");
        itemCurrent.setColor(colors.next());
        itemCurrent.setValue(floatBO);

        ChartItem itemNumRequests = new ChartItem(barChartNetwork);
        itemNumRequests.setText("Requests (" + NumberFormatUtils.commaFormat(numRequests) + ")");
        itemNumRequests.setColor(colors.next());
        itemNumRequests.setValue(floatNf);
    } catch (Exception e) {
        logger.error("Network information", e);
    }
}

From source file:com.hangum.tadpole.mongodb.core.editors.dbInfos.comosites.InstanceInformationComposite.java

License:Open Source License

/**
 * create connection pie chart//from  www . ja  v  a2  s. c  om
 */
private void createConnectionChart(Composite cmpConnections, CommandResult commandResult) {
    logger.debug(
            "=============start create newtrok Information================================================");

    Group compositeConnection = new Group(cmpConnections, SWT.NONE);
    compositeConnection.setLayout(new GridLayout(1, false));
    compositeConnection.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
    compositeConnection.setText("Connections");

    ColorStream colors = Colors.cat20Colors(compositeConnection.getDisplay()).loop();

    barChartConnection = new BarChart(compositeConnection, SWT.NONE);
    GridLayout gl_grpConnectionInfo = new GridLayout(1, false);
    gl_grpConnectionInfo.verticalSpacing = 0;
    gl_grpConnectionInfo.horizontalSpacing = 0;
    gl_grpConnectionInfo.marginHeight = 0;
    gl_grpConnectionInfo.marginWidth = 0;
    barChartConnection.setLayout(gl_grpConnectionInfo);
    barChartConnection.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
    barChartConnection.setBarWidth(25);

    int current = 0;
    int available = 0;
    float floatCurrent = 0f;

    try {
        // nullPointExcepiton check - https://github.com/hangum/TadpoleForDBTools/issues/361 
        DBObject cursorConnections = (DBObject) commandResult.get("connections");
        if (cursorConnections != null) {
            current = ENumberUtils.toInt(cursorConnections.get("current"));
            available = ENumberUtils.toInt(cursorConnections.get("available"));
            floatCurrent = (float) current / (float) available;
        }

        ChartItem itemAvailable = new ChartItem(barChartConnection);
        itemAvailable.setText("Available (" + NumberFormatUtils.commaFormat(available) + ")");
        itemAvailable.setColor(colors.next());
        itemAvailable.setValue(0.80f);

        ChartItem itemCurrent = new ChartItem(barChartConnection);
        itemCurrent.setText("Current (" + NumberFormatUtils.commaFormat(current) + ")");
        itemCurrent.setColor(colors.next());
        itemCurrent.setValue(floatCurrent);
    } catch (Exception e) {
        logger.error("Crate Connection chart", e);
    }

    logger.debug(
            "=============start create newtrok Information [end]================================================");
}