Skip to content

Commit

Permalink
Parse inc from BsonTimestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill5k committed Apr 14, 2024
1 parent d0c91f8 commit a9367c8
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class CodecRegistrySpec extends AsyncWordSpec with Matchers with EmbeddedMongo {
val ts = Instant.now.getEpochSecond
val result = for {
coll <- db.getCollection("coll")
_ <- coll.insertOne(TestData.transaction(TestData.gbpAccount).add("timestamp" -> BsonValue.timestamp(ts)))
_ <- coll.insertOne(TestData.transaction(TestData.gbpAccount).add("timestamp" -> BsonValue.timestamp(ts, 1)))
doc <- coll.find.first
} yield doc.get

Expand Down
10 changes: 5 additions & 5 deletions modules/kernel/src/main/scala/mongo4cats/bson/BsonValue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -181,22 +181,22 @@ object BsonValue {

override def asJava: JBsonValue = new BsonDouble(value)
}
final case class BTimestamp(value: Long) extends BsonValue {
final case class BTimestamp(seconds: Long, inc: Int) extends BsonValue {
override def isNull: Boolean = false
override def isUndefined: Boolean = false
override def asInt: Option[Int] = None
override def asLong: Option[Long] = Some(value)
override def asLong: Option[Long] = Some(seconds)
override def asDouble: Option[Double] = None
override def asBigDecimal: Option[BigDecimal] = None
override def asBoolean: Option[Boolean] = None
override def asDocument: Option[Document] = None
override def asObjectId: Option[ObjectId] = None
override def asList: Option[List[BsonValue]] = None
override def asInstant: Option[Instant] = Some(Instant.ofEpochSecond(value))
override def asInstant: Option[Instant] = Some(Instant.ofEpochSecond(seconds))
override def asString: Option[String] = None
override def asUuid: Option[UUID] = None

override def asJava: JBsonValue = new BsonTimestamp(value.toInt, 1)
override def asJava: JBsonValue = new BsonTimestamp(seconds.toInt, inc)
}
final case class BDateTime(value: Instant) extends BsonValue {
override def isNull: Boolean = false
Expand Down Expand Up @@ -394,6 +394,6 @@ object BsonValue {
def binary(value: Array[Byte]): BsonValue = BBinary(value)
def instant(value: Instant): BsonValue = BDateTime(value)
def regex(value: Regex): BsonValue = BRegex(value)
def timestamp(value: Long): BsonValue = BTimestamp(value)
def timestamp(seconds: Long, inc: Int): BsonValue = BTimestamp(seconds, inc)
def uuid(value: UUID): BsonValue = BUuid(value)
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ private[mongo4cats] object ContainerValueReader {
val subtype = reader.peekBinarySubType()
val binary = reader.readBinaryData().getData
Some(BsonValue.uuid(UuidHelper.decodeBinaryToUuid(binary, subtype, UuidRepresentation.STANDARD)))
case BsonType.BINARY => Some(BsonValue.binary(reader.readBinaryData().getData))
case BsonType.OBJECT_ID => Some(BsonValue.objectId(reader.readObjectId()))
case BsonType.BOOLEAN => Some(BsonValue.boolean(reader.readBoolean()))
case BsonType.TIMESTAMP => Some(BsonValue.timestamp(reader.readTimestamp().getTime.toLong))
case BsonType.BINARY => Some(BsonValue.binary(reader.readBinaryData().getData))
case BsonType.OBJECT_ID => Some(BsonValue.objectId(reader.readObjectId()))
case BsonType.BOOLEAN => Some(BsonValue.boolean(reader.readBoolean()))
case BsonType.TIMESTAMP =>
val ts = reader.readTimestamp()
Some(BsonValue.timestamp(ts.getTime.toLong, ts.getInc))
case BsonType.DATE_TIME => Some(BsonValue.instant(Instant.ofEpochMilli(reader.readDateTime())))
case BsonType.REGULAR_EXPRESSION => Some(BsonValue.regex(reader.readRegularExpression().asRegularExpression().getPattern.r))
case _ => None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,24 @@ private[mongo4cats] object ContainerValueWriter {
writer: BsonWriter
): Unit =
bsonValue match {
case BsonValue.BNull => writer.writeNull()
case BsonValue.BUndefined => writer.writeUndefined()
case BsonValue.BMaxKey => writer.writeMaxKey()
case BsonValue.BMinKey => writer.writeMinKey()
case BsonValue.BInt32(value) => writer.writeInt32(value)
case BsonValue.BInt64(value) => writer.writeInt64(value)
case BsonValue.BDouble(value) => writer.writeDouble(value)
case BsonValue.BTimestamp(value) => writer.writeTimestamp(new BsonTimestamp(value.toInt, 1))
case BsonValue.BDateTime(value) => writer.writeDateTime(value.toEpochMilli)
case BsonValue.BUuid(value) => writer.writeBinaryData(new BsonBinary(BsonBinarySubType.UUID_STANDARD, Uuid.toBinary(value)))
case BsonValue.BBinary(value) => writer.writeBinaryData(new BsonBinary(value))
case BsonValue.BBoolean(value) => writer.writeBoolean(value)
case BsonValue.BDecimal(value) => writer.writeDecimal128(new Decimal128(value.bigDecimal))
case BsonValue.BString(value) => writer.writeString(value)
case BsonValue.BObjectId(value) => writer.writeObjectId(value)
case BsonValue.BDocument(value) => writeBsonDocument(value, writer, None)
case BsonValue.BArray(value) => writeBsonArray(value, writer)
case BsonValue.BRegex(value) => writer.writeRegularExpression(new BsonRegularExpression(value.pattern.pattern()))
case BsonValue.BNull => writer.writeNull()
case BsonValue.BUndefined => writer.writeUndefined()
case BsonValue.BMaxKey => writer.writeMaxKey()
case BsonValue.BMinKey => writer.writeMinKey()
case BsonValue.BInt32(value) => writer.writeInt32(value)
case BsonValue.BInt64(value) => writer.writeInt64(value)
case BsonValue.BDouble(value) => writer.writeDouble(value)
case BsonValue.BTimestamp(seconds, inc) => writer.writeTimestamp(new BsonTimestamp(seconds.toInt, inc))
case BsonValue.BDateTime(value) => writer.writeDateTime(value.toEpochMilli)
case BsonValue.BUuid(value) => writer.writeBinaryData(new BsonBinary(BsonBinarySubType.UUID_STANDARD, Uuid.toBinary(value)))
case BsonValue.BBinary(value) => writer.writeBinaryData(new BsonBinary(value))
case BsonValue.BBoolean(value) => writer.writeBoolean(value)
case BsonValue.BDecimal(value) => writer.writeDecimal128(new Decimal128(value.bigDecimal))
case BsonValue.BString(value) => writer.writeString(value)
case BsonValue.BObjectId(value) => writer.writeObjectId(value)
case BsonValue.BDocument(value) => writeBsonDocument(value, writer, None)
case BsonValue.BArray(value) => writeBsonArray(value, writer)
case BsonValue.BRegex(value) => writer.writeRegularExpression(new BsonRegularExpression(value.pattern.pattern()))
}

def write(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class DocumentSpec extends AnyWordSpec with Matchers {
"handle timestamp" in {
val doc = Document.parse("""{"timestamp":{"$timestamp": {"t": 1673600231, "i": 1}}}""")

doc.get("timestamp") mustBe Some(BsonValue.timestamp(1673600231L))
doc.get("timestamp") mustBe Some(BsonValue.timestamp(1673600231L, 1))
doc.getAs[Instant]("timestamp") mustBe Some(Instant.parse("2023-01-13T08:57:11Z"))
doc.getAs[Long]("timestamp") mustBe Some(1673600231L)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ object ZMongoCollectionSpec extends ZIOSpecDefault with EmbeddedMongo {
txs <- db.getCollection("transactions")
_ <- cats.insertMany(TestData.categories)
_ <- txs.insertMany(TestData.transactions(1000000))
res <- txs.find
res <- txs.find
.noCursorTimeout(true)
.cursorType(CursorType.NonTailable)
.boundedStream(100)
Expand Down

0 comments on commit a9367c8

Please sign in to comment.