Skip to content

Commit

Permalink
Fix Mongo Projections
Browse files Browse the repository at this point in the history
  • Loading branch information
mloukili committed Mar 2, 2020
1 parent 9036221 commit 6a2b26e
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 22 deletions.
10 changes: 5 additions & 5 deletions assets/shells/install-centos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ sudo rpm -qa | grep -qw nfs-utils || sudo yum install -y nfs-utils
sudo rpm -qa | grep -qw java-1.8.0-openjdk || sudo yum -y install java-1.8.0-openjdk

echo "Download and install BlueNimble"
wget --no-cache https://github.com/bluenimble/serverless/releases/download/v2.35.0/bluenimble-2.35.0-bin.tar.gz && \
sudo tar -xvzf bluenimble-2.35.0-bin.tar.gz -C /opt/bluenimble && \
rm -f bluenimble-2.35.0-bin.tar.gz
wget --no-cache https://github.com/bluenimble/serverless/releases/download/v2.36.0-SNAPSHOT/bluenimble-2.36.0-SNAPSHOT-bin.tar.gz && \
sudo tar -xvzf bluenimble-2.36.0-SNAPSHOT-bin.tar.gz -C /opt/bluenimble && \
rm -f bluenimble-2.36.0-SNAPSHOT-bin.tar.gz

sudo mv /opt/bluenimble/bluenimble-2.35.0 /opt/bluenimble/platform
sudo mv /opt/bluenimble/bluenimble-2.36.0-SNAPSHOT /opt/bluenimble/platform

if [ $CLEAN = 'clean' ] ; then
sudo rm -fr /opt/bluenimble/plugins/bluenimble-plugin-dev.playground-2.35.0
sudo rm -fr /opt/bluenimble/plugins/bluenimble-plugin-dev.playground-2.36.0-SNAPSHOT
sudo rm -fr /opt/bluenimble/spaces/playground
fi

Expand Down
10 changes: 5 additions & 5 deletions assets/shells/install-ubuntu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ sudo apt-cache pkgnames java-1.8.0-openjdk || sudo apt-get install java-1.8.0-op
sudo apt-cache pkgnames systemd || sudo apt-get install systemd

echo "Download and install BlueNimble"
wget --no-cache https://github.com/bluenimble/serverless/releases/download/v2.35.0/bluenimble-2.35.0-bin.tar.gz && \
sudo tar -xvzf bluenimble-2.35.0-bin.tar.gz -C /opt/bluenimble && \
rm -f bluenimble-2.35.0-bin.tar.gz
wget --no-cache https://github.com/bluenimble/serverless/releases/download/v2.36.0-SNAPSHOT/bluenimble-2.36.0-SNAPSHOT-bin.tar.gz && \
sudo tar -xvzf bluenimble-2.36.0-SNAPSHOT-bin.tar.gz -C /opt/bluenimble && \
rm -f bluenimble-2.36.0-SNAPSHOT-bin.tar.gz

sudo mv /opt/bluenimble/bluenimble-2.35.0 /opt/bluenimble/platform
sudo mv /opt/bluenimble/bluenimble-2.36.0-SNAPSHOT /opt/bluenimble/platform

if [ $CLEAN = 'clean' ] ; then
sudo rm -fr /opt/bluenimble/plugins/bluenimble-plugin-dev.playground-2.35.0
sudo rm -fr /opt/bluenimble/plugins/bluenimble-plugin-dev.playground-2.36.0-SNAPSHOT
sudo rm -fr /opt/bluenimble/spaces/playground
fi

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.bluenimble.platform.Json;
import com.bluenimble.platform.Lang;
import com.bluenimble.platform.api.tracing.Tracer;
import com.bluenimble.platform.api.tracing.Tracer.Level;
import com.bluenimble.platform.db.Database;
import com.bluenimble.platform.db.DatabaseException;
import com.bluenimble.platform.db.DatabaseObject;
Expand Down Expand Up @@ -98,6 +99,8 @@ interface Describe {

interface SpiDescribe {
String Size = "size";
String DbStats = "dbStats";
String DbSize = "dbSize";
String CollStats = "collStats";
}

Expand Down Expand Up @@ -154,6 +157,17 @@ public MongoDatabaseImpl (MongoClient client, String databaseName, Tracer tracer
this.tracer = tracer;
this.allowProprietaryAccess = allowProprietaryAccess;
}

@Override
public long size () throws DatabaseException {
Document dbStats = null;
if (session == null) {
dbStats = db.runCommand (new Document (SpiDescribe.DbStats, 1));
} else {
dbStats = db.runCommand (session, new Document (SpiDescribe.DbStats, 1));
}
return dbStats.getLong (SpiDescribe.DbSize);
}

@Override
public void createEntity (String entity, Field... fields) throws DatabaseException {
Expand Down Expand Up @@ -270,7 +284,7 @@ public List<DatabaseObject> find (String entity, Query query, Visitor visitor) t
return null;
}

return toList (entity, result, visitor, query.select () != null);
return toList (entity, result, visitor, false);
}

@Override
Expand Down Expand Up @@ -578,6 +592,17 @@ private Object _query (String entity, Query.Construct construct, final Query que
cursor = db.getCollection (entity).find (session, mQuery);
}

Select select = query.select ();
tracer.log (Level.Info, "Select Fields -> " + select);
if (select != null && select.count () > 0) {
String [] pFields = new String [select.count ()];
for (int i = 0; i < select.count (); i++) {
pFields [i] = select.get (i);
}
System.out.println ("Projections -> " + Projections.include (pFields));
cursor.projection (Projections.include (pFields));
}

// start / skip
if (query.start () > 0) {
cursor.skip (query.start ());
Expand Down Expand Up @@ -607,15 +632,6 @@ private Object _query (String entity, Query.Construct construct, final Query que
cursor.sort (Sorts.orderBy (sorts));
}

Select select = query.select ();
if (select != null && select.count () > 0) {
String [] pFields = new String [select.count ()];
for (int i = 0; i < select.count (); i++) {
pFields [i] = select.get (i);
}
cursor.projection (Projections.include (pFields));
}

return cursor;
} else if (Query.Construct.delete.equals (construct)) {
if (session == null) {
Expand Down Expand Up @@ -865,5 +881,27 @@ public String entity (String entity) {
}
return entity;
}

@Override
public JsonObject describeEntity (String enity) throws DatabaseException {
JsonObject oEntity = new JsonObject ();
oEntity.set (Describe.Name, enity);

Document callStats = null;
if (session == null) {
callStats = db.runCommand (new Document (SpiDescribe.CollStats, enity));
} else {
callStats = db.runCommand (session, new Document (SpiDescribe.CollStats, enity));
}

// clean
callStats.remove (Tokens.WiredTiger);
callStats.remove (Tokens.IndexDetails);

oEntity.putAll (callStats);

return oEntity;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public MongoClient client () {
);

MongoClientURI uri = new MongoClientURI (
"mongodb+srv://usr:pwd@displaystream-primary-9zlqv.xyz.net",
"mongodb+srv://dsapi:Im%40ne1977@displaystream-primary-9zlqv.mongodb.net",
MongoClientOptions.builder ().cursorFinalizerEnabled (false).codecRegistry (codecRegistry).retryWrites (true)
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.
*/
package com.bluenimble.platform.plugins.database.mongodb.tests;

import com.bluenimble.platform.db.Database;
import com.bluenimble.platform.db.DatabaseException;

public class DatabaseSize {

public static void main (String [] args) throws DatabaseException {

Database db = new DatabaseServer ().get ();

System.out.println (db.size ());

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static void main (String [] args) throws Exception {
);

for (DatabaseObject story : stories) {
System.out.println (story.get ("name") + " - " + story.get ("description"));
System.out.println (story.toJson (null));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ public OrientDatabase (ODatabaseDocumentTx db, Tracer tracer, boolean allowPropr
this.allowProprietaryAccess = allowProprietaryAccess;
}

@Override
public long size () throws DatabaseException {
throw new DatabaseException ("Unsupported Operation");
}

@Override
public DatabaseObject create (String entity) throws DatabaseException {
if (Lang.isNullOrEmpty (entity)) {
Expand Down Expand Up @@ -691,6 +696,11 @@ public long update (String entity, Query query, JsonObject data) throws Database
throw new UnsupportedOperationException ("update (String entity, Query query, JsonObject data)");
}

@Override
public JsonObject describeEntity (String enity) throws DatabaseException {
throw new UnsupportedOperationException ("describeEntity(String enity)");
}

/*
@Override
public void createIndex (String eType, IndexType type, String name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public JpaDatabase (Tracer tracer, EntityManager entityManager, JpaMetadata meta
this.allowProprietaryAccess = allowProprietaryAccess;
}

@Override
public long size () throws DatabaseException {
throw new DatabaseException ("Unsupported Operation");
}

@Override
public void createEntity (String entityName, Field... fields) throws DatabaseException {
throw new DatabaseException ("Unsupported Operation");
Expand Down Expand Up @@ -455,4 +460,9 @@ public long update (String entity, Query query, JsonObject data) throws Database
throw new UnsupportedOperationException ("update (String entity, Query query, JsonObject data)");
}

@Override
public JsonObject describeEntity (String enity) throws DatabaseException {
throw new UnsupportedOperationException ("describeEntity(String enity)");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,25 @@ var Database = function (api, proxy) {

return proxy.bulk (JC_ValueConverter.convert (values));
};

/**
Describe an entity<br />
@param {string} - the entity name
*/
this.describeEntity = function (entity) {
if (!entity) {
throw "missing entity argument";
}
return proxy.describeEntity (entity);
};

/**
Describe an entity<br />
@param {string} - the entity name
*/
this.size = function () {
return proxy.size ();
};

};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ interface Proprietary {
}

Object proprietary (String name);
long size () throws DatabaseException;

Database trx ();
Database commit () throws DatabaseException;
Expand Down Expand Up @@ -120,6 +121,9 @@ interface Proprietary {

long count (String entity) throws DatabaseException;

JsonObject describeEntity
(String enity) throws DatabaseException;

JsonObject describe () throws DatabaseException;

}

0 comments on commit 6a2b26e

Please sign in to comment.