public Collection<SpaceTypeDescriptor> loadMetadata() {

    DBCollection metadata = getCollection(METADATA_COLLECTION_NAME);

    DBCursor cursor = metadata.find(new BasicDBObject());

    while (cursor.hasNext()) {
      DBObject type = cursor.next();

      Object b = type.get(TYPE_DESCRIPTOR_FIELD_NAME);

      readMetadata(b);
    }

    return getSortedTypes();
  }
  public void performBatch(List<BatchUnit> rows) {
    if (logger.isTraceEnabled()) {
      logger.trace("MongoClientWrapper.performBatch(" + rows + ")");
      logger.trace("Batch size to be performed is " + rows.size());
    }
    // List<Future<? extends Number>> pending = new ArrayList<Future<? extends Number>>();

    for (BatchUnit row : rows) {
      SpaceDocument spaceDoc = row.getSpaceDocument();
      SpaceTypeDescriptor typeDescriptor = types.get(row.getTypeName()).getTypeDescriptor();
      SpaceDocumentMapper<DBObject> mapper = getMapper(typeDescriptor);

      DBObject obj = mapper.toDBObject(spaceDoc);

      DBCollection col = getCollection(row.getTypeName());
      switch (row.getDataSyncOperationType()) {
        case WRITE:
        case UPDATE:
          col.save(obj);
          break;
        case PARTIAL_UPDATE:
          DBObject query =
              BasicDBObjectBuilder.start()
                  .add(Constants.ID_PROPERTY, obj.get(Constants.ID_PROPERTY))
                  .get();

          DBObject update = normalize(obj);
          col.update(query, update);
          break;
          // case REMOVE_BY_UID: // Not supported by this implementation
        case REMOVE:
          col.remove(obj);
          break;
        default:
          throw new IllegalStateException(
              "Unsupported data sync operation type: " + row.getDataSyncOperationType());
      }
    }

    /*long totalCount = waitFor(pending);

    if (logger.isTraceEnabled()) {
    	logger.trace("total accepted replies is: " + totalCount);
    }*/
  }
  boolean _isMaster(DBObject res) {
    Object x = res.get("ismaster");
    if (x == null) throw new IllegalStateException("ismaster shouldn't be null: " + res);

    if (x instanceof Boolean) return (Boolean) x;

    if (x instanceof Number) return ((Number) x).intValue() == 1;

    throw new IllegalArgumentException("invalid ismaster [" + x + "] : " + x.getClass().getName());
  }
  private static DBObject normalize(DBObject obj) {
    BasicDBObjectBuilder builder = BasicDBObjectBuilder.start();

    Iterator<String> iterator = obj.keySet().iterator();
    builder.push("$set");
    while (iterator.hasNext()) {

      String key = iterator.next();

      if (Constants.ID_PROPERTY.equals(key)) continue;

      Object value = obj.get(key);

      if (value == null) continue;

      builder.add(key, value);
    }

    return builder.get();
  }
Example #5
0
  // --------------------------------------------------------------------------------
  private void drawTreeRoot(Tree _tree, DBObject data) {
    // reset tree
    _tree.removeAll();

    if (data != null) {
      TreeItem root = new TreeItem(_tree, SWT.NONE);
      root.setText(data.get("_id") + "");
      root.setImage(documentImage);
      root.setData("fieldName", "");
      root.setData("value", data.get("_id"));
      fieldNameTreeItemMap.put("", root);

      boolean expand = (data.keySet().size() < 35);
      drawItem("", root, data.toMap(), expand);
    }

    nameText.setText("");
    typeCombo.select(0);
    typeCombo.setEnabled(false);
    valueText.setText("");
    updateButton.setEnabled(false);
  }
    synchronized void update(Set<Node> seenNodes) {
      try {
        long start = System.currentTimeMillis();
        CommandResult res = _port.runCommand(_mongo.getDB("admin"), _isMasterCmd);
        _lastCheck = System.currentTimeMillis();
        _pingTime = _lastCheck - start;

        if (res == null) {
          _ok = false;
          return;
        }

        _ok = true;
        _isMaster = res.getBoolean("ismaster", false);
        _isSecondary = res.getBoolean("secondary", false);
        _lastPrimarySignal = res.getString("primary");

        if (res.containsField("hosts")) {
          for (Object x : (List) res.get("hosts")) {
            String host = x.toString();
            Node node = _addIfNotHere(host);
            if (node != null && seenNodes != null) seenNodes.add(node);
          }
        }

        if (res.containsField("passives")) {
          for (Object x : (List) res.get("passives")) {
            String host = x.toString();
            Node node = _addIfNotHere(host);
            if (node != null && seenNodes != null) seenNodes.add(node);
          }
        }

        if (_isMaster) {
          // max size was added in 1.8
          if (res.containsField("maxBsonObjectSize"))
            maxBsonObjectSize = ((Integer) res.get("maxBsonObjectSize")).intValue();
          else maxBsonObjectSize = Bytes.MAX_OBJECT_SIZE;
        }

      } catch (MongoException e) {
        Throwable root = e;
        if (e.getCause() != null) root = e.getCause();
        _logger.log(Level.FINE, "node down: " + _addr + " " + root);
        _ok = false;
      } catch (Exception e) {
        _logger.log(Level.SEVERE, "can't update node: " + _addr, e);
        _ok = false;
      }

      if (!_isMaster) return;

      try {
        DB db = _mongo.getDB("local");
        _port.checkAuth(db);
        DBObject config = _port.findOne(db, "system.replset", new BasicDBObject());
        if (config == null) {
          // probably a replica pair
          // TODO: add this in when pairs are really gone
          // _logger.log( Level.SEVERE , "no replset config!" );
        } else if (config.get("$err") != null
            && UNAUTHENTICATED_ERROR_CODE == (Integer) config.get("code")) {
          _logger.log(
              Level.WARNING,
              "Replica Set updater cannot get results, call authenticate on 'local' or 'admin' db");
        } else {

          String setName = config.get("_id").toString();
          if (_setName == null) {
            _setName = setName;
            _logger = Logger.getLogger(_rootLogger.getName() + "." + setName);
          } else if (!_setName.equals(setName)) {
            _logger.log(Level.SEVERE, "mis match set name old: " + _setName + " new: " + setName);
            return;
          }

          // TODO: look at members
        }
      } catch (MongoException e) {
        if (_setName != null) {
          // this probably means the master is busy, so going to ignore
        } else {
          _logger.log(Level.SEVERE, "can't get initial config from node: " + _addr, e);
        }
      } catch (Exception e) {
        _logger.log(Level.SEVERE, "unexpected error getting config from node: " + _addr, e);
      }
    }
Example #7
0
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    try {
      MongoClient mc = new MongoClient("localhost");
      DB db = mc.getDB("mydb");
      DBCollection dbc = db.getCollection("books");
      /*BasicDBObject book=
      		new BasicDBObject();
      book.put("name", "자바의 정석");
      book.put("pages", 600);
      dbc.insert(book);

      book=
      		new BasicDBObject();
      book.put("name", "스프링4");
      book.put("pages", 250);
      dbc.insert(book);
      book=
      		new BasicDBObject();
      book.put("name", "JQuery");
      book.put("pages", 150);
      dbc.insert(book);
      book=
      		new BasicDBObject();
      book.put("name", "오라클 프로그램");
      book.put("pages", 800);
      dbc.insert(book);
      book=
      		new BasicDBObject();
      book.put("name", "AngularJS");
      book.put("pages", 130);
      dbc.insert(book);
      book=
      		new BasicDBObject();
      book.put("name", "MongoDB");
      book.put("pages", 650);
      dbc.insert(book);
      book=
      		new BasicDBObject();
      book.put("name", "Hadoop");
      book.put("pages", 300);
      dbc.insert(book);
      book=
      		new BasicDBObject();
      book.put("name", "HIVE");
      book.put("pages", 350);
      dbc.insert(book);
      book=
      		new BasicDBObject();
      book.put("name", "PIG");
      book.put("pages", 650);
      dbc.insert(book);
      book=
      		new BasicDBObject();
      book.put("name", "HTML5");
      book.put("pages", 360);
      dbc.insert(book);*/
      String map =
          "function(){"
              + "var category;"
              + "if(this.pages>=300){"
              + "category='Big Books';"
              + "}else{"
              + "category='Small Books';}"
              + "emit(category,{name:this.name});}";
      String reduce =
          "function(key,values){"
              + "var sum=0;"
              + "values.forEach(function(doc){"
              + "sum+=1;"
              + "});"
              + "return {books:sum};}";
      MapReduceCommand cmd =
          new MapReduceCommand(dbc, map, reduce, null, MapReduceCommand.OutputType.INLINE, null);
      MapReduceOutput out = dbc.mapReduce(cmd);

      for (DBObject o : out.results()) {
        System.out.println(o.toString());
      }
    } catch (Exception ex) {
      System.out.println(ex.getMessage());
    }
  }