Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongo async support #1825

Closed
wants to merge 1 commit into from
Closed

Conversation

marekzebrowski
Copy link
Contributor

@marekzebrowski marekzebrowski commented Jan 24, 2017

as discussed
sgrouples/rogue-fsqio#15
mongo async support for lift 3.1
pre-requisite for porting rogue-async to lift (separate project):

https://github.com/sgrouples/rogue-fsqio/tree/lift31

@Shadowfiend
Copy link
Member

! 💯 Don't know that I can review this, but this souuunds pretty sweet :)

Copy link
Member

@eltimn eltimn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just have the one question. I'll test it out locally in the next few days. Thanks for sending this over.

if (elem.isInstanceOf[org.bson.Document]) {
setFromDocumentList(jlist.asInstanceOf[java.util.List[org.bson.Document]])
} else {
setBox(Full(jlist.asScala.toList.asInstanceOf[MyType]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the toList call here still needed?

if(elem.isInstanceOf[org.bson.Document]) {
setFromDocumentList(jlist.asInstanceOf[java.util.List[org.bson.Document]])
} else {
setBox(Full(jlist.toList.asInstanceOf[MyType]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another toList call

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunately it is needed to match override type MyType = List[ListType] as jlist is java list and we need Scala List[ListType]. Same thing with previous toList

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I saw an asScala call before the toList call. The previous one has an asScala call before the toList call, which, I would think, would already have converted it to a Scala List.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well - maybe it can be done better - I'm open for suggestions. when I tried in scala REPL:

scala> :paste
// Entering paste mode (ctrl-D to finish)

val l = new java.util.ArrayList[Int](2)
l.add(1)
l.add(2)
import scala.collection.JavaConverters._
l.asScala

// Exiting paste mode, now interpreting.

l: java.util.ArrayList[Int] = [1, 2]
import scala.collection.JavaConverters._
res6: scala.collection.mutable.Buffer[Int] = Buffer(1, 2)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I understand now. asScala returns a scala.collection.mutable.Buffer in this instance, which still needs to be converted to List.

Anyway, this should be good then. Thanks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I could go through and change every concrete List declaration in Lift to Seq, I would sooo do it <_<

Copy link
Member

@eltimn eltimn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to take a closer look. Looking good, just a few minor things and a few questions.

useCollAsync { coll =>
val p = Promise[BaseRecord]
val doc: Document = inst.asDocument
val options = new UpdateOptions().upsert(true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably want to use the passed in parameter here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks to be the only thing not addressed. The upsert parameter should be used here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marekzebrowski I think this was the only thing left, do you mind if I just go ahead and update the branch?

* replaces document with new one with given id. if `upsert` is set to true inserts new document
* in similar way as save() from sync api
*/
def replaceOneAsync(inst: BaseRecord, upsert: Boolean, concern: WriteConcern) = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of adding default values for upsert (true) and concern (ACKNOWLEDGED)?

Also, please add an explicit return type.

val doc: Document = inst.asDocument
val options = new UpdateOptions().upsert(true)
val filter = new org.bson.Document("_id", doc.get("_id"))
coll.replaceOne(filter, doc, options, new SingleResultCallback[UpdateResult] {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be calling before/afterSave and resetDirty here, like the other functions?

@@ -147,6 +160,11 @@ class MongoListField[OwnerType <: BsonRecord[OwnerType], ListType: Manifest](rec
// set this field's value using a DBObject returned from Mongo.
def setFromDBObject(dbo: DBObject): Box[MyType] =
setBox(Full(dbo.asInstanceOf[BasicDBList].toList.asInstanceOf[MyType]))

def setFromDocumentList(list: java.util.List[Document]): Box[MyType] = {
throw new RuntimeException("Warning, , setting Document as field with no converstion, probably not something you want to do")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: converstion

val map = Map("a" -> "4", "b" -> "5", "c" -> "6")
val doc = new Document(map)
rec.mandatoryStringMapField.setFromDocument(doc)
rec.mandatoryStringMapField.get must_== map
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codacy complained about this get call here. It must think it's being called on an Option. Changing it to value should shut it up.

*/
private[this] val dbs = new ConcurrentHashMap[ConnectionIdentifier, (MongoClient, String)]
val codecRegistry = CodecRegistries.fromRegistries(com.mongodb.MongoClient.getDefaultCodecRegistry(),
CodecRegistries.fromCodecs(new LongPrimitiveCodec, new IntegerPrimitiveCodec)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why the need to add these 2 codecs? Does it have to do with Java/Scala differences?

Also, should we make the registry customizable?

I'm still wrapping my head around the new java driver and the async support.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without that codecs old-style (2.x with BasicDbObject) fails to work. That is caused because default com.mongodb.async.client.MongoClientImpl.getDatabase does not include the same codec registry as old com.mongodb.MongoClient.getDatabase that leads to runtime errors in serialization/deserialization.
Yes, it could be made customizable - in Mongo driver it is.

@farmdawgnation farmdawgnation added this to the 3.1 milestone Feb 2, 2017
limit scope of privates in MongoAsync

CR fixes

use upsert parameter
@marekzebrowski
Copy link
Contributor Author

marekzebrowski commented Feb 27, 2017 via email

@eltimn
Copy link
Member

eltimn commented Mar 5, 2017

Since this branch is on the sgrouples org and I don't have access to modify it, I'm going to close this PR and open a new one.

@eltimn eltimn closed this Mar 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants