2017-05-29 84 views
2

這是我第一次使用Mongo Scala驅動程序(版本2.1),我注意到在處理我的一些case類時的奇怪行爲 - 其中一些沒有被識別因此classToCaseClassMap的結果是,其中一個解碼數據沿着錯誤的路徑行進,並以org.mongodb.scala.bson.codecs.macrocodecs.MacroCodec中204行的錯誤java.util.NoSuchElementException: head of empty list結束,因爲fieldClazzTypeArgs列表爲空。Mongo Scala驅動程序2.1 - case class error

這裏是頂部三分之一左右的堆棧跟蹤的:

error: class java.util.NoSuchElementException: head of empty list 
scala.collection.immutable.Nil$.head(List.scala:420) 
scala.collection.immutable.Nil$.head(List.scala:417) 
org.mongodb.scala.bson.codecs.macrocodecs.MacroCodec$class.readDocument(MacroCodec.scala:204) 
palladium.calendar.dal.mongodb.Implicits$$anon$2$GoogleCalendarEventMacroCodec$3.readDocument(GoogleCalendarDAO.scala:55) 
org.mongodb.scala.bson.codecs.macrocodecs.MacroCodec$class.readValue(MacroCodec.scala:173) 
palladium.calendar.dal.mongodb.Implicits$$anon$2$GoogleCalendarEventMacroCodec$3.readValue(GoogleCalendarDAO.scala:55) 
org.mongodb.scala.bson.codecs.macrocodecs.MacroCodec$class.decode(MacroCodec.scala:104) 
palladium.calendar.dal.mongodb.Implicits$$anon$2$GoogleCalendarEventMacroCodec$3.decode(GoogleCalendarDAO.scala:55) 
org.mongodb.scala.bson.codecs.macrocodecs.MacroCodec$class.readDocument(MacroCodec.scala:197) 
palladium.calendar.dal.mongodb.Implicits$$anon$8$GoogleCalendarMacroCodec$3.readDocument(GoogleCalendarDAO.scala:61) 
org.mongodb.scala.bson.codecs.macrocodecs.MacroCodec$class.readValue(MacroCodec.scala:173) 
palladium.calendar.dal.mongodb.Implicits$$anon$8$GoogleCalendarMacroCodec$3.readValue(GoogleCalendarDAO.scala:61) 
org.mongodb.scala.bson.codecs.macrocodecs.MacroCodec$class.readArray(MacroCodec.scala:187) 
palladium.calendar.dal.mongodb.Implicits$$anon$8$GoogleCalendarMacroCodec$3.readArray(GoogleCalendarDAO.scala:61) 
org.mongodb.scala.bson.codecs.macrocodecs.MacroCodec$class.readValue(MacroCodec.scala:174) 
palladium.calendar.dal.mongodb.Implicits$$anon$8$GoogleCalendarMacroCodec$3.readValue(GoogleCalendarDAO.scala:61) 
org.mongodb.scala.bson.codecs.macrocodecs.MacroCodec$class.decode(MacroCodec.scala:104) 
palladium.calendar.dal.mongodb.Implicits$$anon$8$GoogleCalendarMacroCodec$3.decode(GoogleCalendarDAO.scala:61) 
com.mongodb.operation.CommandResultArrayCodec.decode(CommandResultArrayCodec.java:52) 
com.mongodb.operation.CommandResultDocumentCodec.readValue(CommandResultDocumentCodec.java:53) 
org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:84) 
org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:41) 
org.bson.codecs.configuration.LazyCodec.decode(LazyCodec.java:47) 
org.bson.codecs.BsonDocumentCodec.readValue(BsonDocumentCodec.java:101) 
com.mongodb.operation.CommandResultDocumentCodec.readValue(CommandResultDocumentCodec.java:56) 
org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:84) 
org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:41) 
com.mongodb.connection.ReplyMessage.<init>(ReplyMessage.java:57) 
com.mongodb.connection.CommandProtocol.getResponseDocument(CommandProtocol.java:139) 
com.mongodb.connection.CommandProtocol.access$000(CommandProtocol.java:51) 
com.mongodb.connection.CommandProtocol$CommandResultCallback.callCallback(CommandProtocol.java:271) 
com.mongodb.connection.ResponseCallback.onResult(ResponseCallback.java:48) 
com.mongodb.connection.ResponseCallback.onResult(ResponseCallback.java:23) 
com.mongodb.connection.DefaultConnectionPool$PooledConnection$2.onResult(DefaultConnectionPool.java:470) 
com.mongodb.connection.DefaultConnectionPool$PooledConnection$2.onResult(DefaultConnectionPool.java:464) 
com.mongodb.connection.UsageTrackingInternalConnection$3.onResult(UsageTrackingInternalConnection.java:119) 
com.mongodb.connection.UsageTrackingInternalConnection$3.onResult(UsageTrackingInternalConnection.java:115) 
com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:49) 
com.mongodb.connection.InternalStreamConnection.executeCallbackAndReceiveResponse(InternalStreamConnection.java:378) 
com.mongodb.connection.InternalStreamConnection.access$1700(InternalStreamConnection.java:66) 
com.mongodb.connection.InternalStreamConnection$ResponseBuffersCallback.onResult(InternalStreamConnection.java:420) 
com.mongodb.connection.InternalStreamConnection$ResponseBuffersCallback.onResult(InternalStreamConnection.java:389) 
com.mongodb.connection.InternalStreamConnection$ResponseHeaderCallback.onSuccess(InternalStreamConnection.java:562) 
com.mongodb.connection.InternalStreamConnection$ResponseHeaderCallback.access$2200(InternalStreamConnection.java:517) 
com.mongodb.connection.InternalStreamConnection$ResponseHeaderCallback$ResponseBodyCallback.onResult(InternalStreamConnection.java:584) 
com.mongodb.connection.InternalStreamConnection$ResponseHeaderCallback$ResponseBodyCallback.onResult(InternalStreamConnection.java:568) 
com.mongodb.connection.InternalStreamConnection$3.completed(InternalStreamConnection.java:447) 
com.mongodb.connection.InternalStreamConnection$3.completed(InternalStreamConnection.java:444) 
com.mongodb.connection.AsynchronousSocketChannelStream$BasicCompletionHandler.completed(AsynchronousSocketChannelStream.java:218) 
com.mongodb.connection.AsynchronousSocketChannelStream$BasicCompletionHandler.completed(AsynchronousSocketChannelStream.java:201) 
sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:126) 
sun.nio.ch.Invoker.invokeDirect(Invoker.java:157) 
sun.nio.ch.UnixAsynchronousSocketChannelImpl.implRead(UnixAsynchronousSocketChannelImpl.java:553) 
sun.nio.ch.AsynchronousSocketChannelImpl.read(AsynchronousSocketChannelImpl.java:276) 
sun.nio.ch.AsynchronousSocketChannelImpl.read(AsynchronousSocketChannelImpl.java:297) 

我的應用程序模型別人的谷歌日曆。這裏是我的情況下類:

case class GoogleDateTime(date: Option[DateTime] = None, dateTime: Option[DateTime]) 

case class LongId(id: Long) 

case class GoogleCalendarEvent(id: String, 
          status: Option[String] = None, 
          htmlLink: Option[String] = None, 
          created: Option[DateTime] = None, 
          updated: Option[DateTime] = None, 
          creator: Option[GoogleWho] = None, 
          organizer: Option[GoogleWho] = None, 
          summary: Option[String], 
          description: Option[String] = None, 
          location: Option[String] = None, 
          start: GoogleDateTime, 
          end: GoogleDateTime, 
          extendedProperties: Option[GoogleEventExtendedProperties] = None) 

case class GoogleCalendarEventList(nextPageToken: Option[String], 
           items: Option[List[GoogleCalendarEvent]]) 

case class GoogleWho(email: Option[String], 
       displayName: Option[String]) 

case class GoogleEventSharedProperties(createdBy: Option[String] = None, 
            sessionType: Option[String] = None, 
            sessionId: Option[LongId] = None, 
            locationId: Option[LongId] = None, 
            roomId: Option[LongId] = None) 

case class GoogleEventExtendedProperties(shared: Option[GoogleEventSharedProperties]) 

Here is a link to a screenshot showing the contents of the classToCaseClassMap在發生錯誤的點。

的截圖顯示了六個項目,包括我的case類的四個地圖:GoogleCalendarEventGoogleWhoGoogleDateTimeGoogleEventExtendedProperties。只有第一個(GoogleCalendarEvent)被正確識別爲案例類。

我試圖讓有問題的類sealed但沒有幫助。

這裏是我創建CodecRegistry:

val dtMap = Map(BsonType.DATE_TIME -> classOf[DateTime]) 

val typeClassReplacedMap = new BsonTypeClassMap(JavaConversions.mapAsJavaMap(dtMap)) 

val gcCodecProvider = new DocumentCodecProvider(typeClassReplacedMap) 

implicit val gcRegistry: CodecRegistry = fromRegistries(fromCodecs(dtCodec), 
fromProviders(gcCodecProvider, 
classOf[GoogleCalendarEvent], 
classOf[GoogleDateTime], 
classOf[LongId], 
classOf[GoogleEventExtendedProperties], 
classOf[GoogleEventSharedProperties], 
classOf[GoogleWho], 
classOf[GoogleCalendar]), 
DEFAULT_CODEC_REGISTRY) 

Here是示出數據訪問碼要旨,我如何使用它(在SBT控制檯或IntelliJ IDEA的Scala的控制檯)。

我正在重構的應用程序在Reactive Mongo版本0.11.7下工作正常,但我們需要將其遷移到正式支持的Mongo Scala驅動程序以使用新的Atlas數據庫。在此時間之後

+0

嗨「其中一些不被識別」,哪些不被識別。你可以在你使用mongoDB驅動程序的地方發佈代碼片段嗎?對異常的堆棧跟蹤也會有幫助。 – Teliatko

+0

也許我在閱讀我的文章後添加了這個內容(對不起,我一直在添加它)。該圖像將顯示包含六個項目的地圖,包括我的四個案例類:GoogleCalendarEvent,GoogleWho,GoogleDateTime和GoogleEventExtendedProperties。只有第一個(GoogleCalendarEvent)被正確識別爲案例類。 – KevinD

+0

我添加了儘可能多的棧跟蹤(前三名左右) – KevinD

回答

0

FWIW,看看https://jira.mongodb.org/browse/SCALA-338

我有同樣的問題,由於你的文章,我發現,這是莫名其妙的編解碼器供應商的排序。

最初我雖然可以通過確保持有其他案例類類型值的案例類在這些其他案例類之前列出來解決。但現在,即使這樣也不能解決我的問題,但也許它可以幫助解決您的具體情況。

簡單地從2.1.0降級到2.0.0爲我解決了這個問題。

0

我修復了從Providers項目重新排序的類似問題。 這傢伙類A(B:B)和B類(我的:int)應該是相反的順序: fromProviders(classOf [B],classOf [A])

所以,看起來你應該把classOf [ GoogleCalendarEvent]之後[GoogleEventExtendedProperties]

相關問題