Document serialization

30 views
Skip to first unread message

slav...@gmail.com

unread,
Oct 29, 2016, 4:59:29 PM10/29/16
to Siaqodb - NoSQL embedded database for .NET
When providing a custom serializer for the document, Document itself is also serialized. At the same time it's not decorated with any attributes, such as DataContract and DataMember.

First, serialization happens twice, on Document.SetContent and then when document is added to the bucket. Is this really the best way to do it?

And in addition, even though documentation states we can provide custom serializer, such as Protobuf, app would crash on attempting to add document to the bucket since Document isn't decorated correctly with attributes. It's possible to work around, at least in protobuf case, but this case needs to be well documented showing how to correctly implement custom serializer to where it's usable.

slav...@gmail.com

unread,
Oct 29, 2016, 10:40:52 PM10/29/16
to Siaqodb - NoSQL embedded database for .NET, slav...@gmail.com
Btw, just in case anyone needs to use protobuf with document storage, here it is (although in F#). I haven't tested extensively, but it should correctly save and load tags in the document:

let tagToType = Dictionary<int, Type>()
tagToType.[1] <- typeof<int>
tagToType.[2] <- typeof<int64>
tagToType.[3] <- typeof<double>
tagToType.[4] <- typeof<single>
tagToType.[5] <- typeof<DateTime>
tagToType.[6] <- typeof<string>
tagToType.[7] <- typeof<bool>

let typeToTag = System.Linq.Enumerable.ToDictionary<KeyValuePair<int, Type>, Type, int>(tagToType, (fun kv -> kv.Value), (fun kv -> kv.Key))

let serializeDocument (stream: Stream) (doc: Document) =
    let model = RuntimeTypeModel.Default
    model.SerializeWithLengthPrefix(stream, doc.Content, typeof<byte []>, PrefixStyle.Base128, 0)
    model.SerializeWithLengthPrefix(stream, doc.Key, typeof<string>, PrefixStyle.Base128, 0)
    model.SerializeWithLengthPrefix(stream, (if not (isNull doc.Version) then doc.Version else ""), typeof<string>, PrefixStyle.Base128, 0)

    if isNull doc.Tags then
        model.SerializeWithLengthPrefix(stream, 0, typeof<int>, PrefixStyle.Base128, 0)
    else
        let elementsCount = doc.Tags.Count
        model.SerializeWithLengthPrefix(stream, elementsCount, typeof<int>, PrefixStyle.Base128, 0)

        for kv in doc.Tags do
            model.SerializeWithLengthPrefix(stream, kv.Key, typeof<int>, PrefixStyle.Base128, 0)
            let valueType = kv.Value.GetType()
            model.SerializeWithLengthPrefix(stream, kv.Value, valueType, PrefixStyle.Base128, typeToTag.[valueType])

let deserializeDocument (stream: Stream) : Document =
    let model = RuntimeTypeModel.Default
    let doc = Document()

    doc.Content <- model.DeserializeWithLengthPrefix(stream, null, typeof<byte []>, PrefixStyle.Base128, 0) :?> byte[]
    doc.Key <- model.DeserializeWithLengthPrefix(stream, null, typeof<string>, PrefixStyle.Base128, 0) :?> string
    let version = model.DeserializeWithLengthPrefix(stream, null, typeof<string>, PrefixStyle.Base128, 0) :?> string
    if not (String.IsNullOrEmpty version) then doc.Version <- version

    let elementsCount = model.DeserializeWithLengthPrefix(stream, null, typeof<int>, PrefixStyle.Base128, 0) :?> int
    if elementsCount > 0 then
        let tags = Dictionary<string, obj>(elementsCount)

        for i = 1 to elementsCount do
            let tag = model.DeserializeWithLengthPrefix(stream, null, typeof<string>, PrefixStyle.Base128, 0) :?> string
            let value = model.DeserializeWithLengthPrefix(stream, null, null, PrefixStyle.Base128, 0, (fun t -> tagToType.[t]))
            tags.[tag] <- value

        doc.Tags <- tags

    doc

type ProtoSerializer() =
    interface IDocumentSerializer with
        member x.Deserialize(objType, objectBytes) =
            use memStream = new MemoryStream(objectBytes)
            if objType = typeof<Document> then
                deserializeDocument memStream :> obj
            else
                Serializer.Deserialize(objType, memStream)

        member x.Serialize(obj) =
            use memStream = new MemoryStream()
            match obj with
            | :? Document as doc -> serializeDocument memStream doc
            | _ -> Serializer.Serialize(memStream, obj)
            memStream.Flush()
            memStream.ToArray()

Alana Marshall

unread,
Dec 16, 2023, 1:19:26 AM12/16/23
to Siaqodb - NoSQL embedded database for .NET
What is Bandarbola855? This time the developer will discuss about Bandarbola855 which will give bettors a big win every time they play on this site.

This is a great opportunity for bettors to increase their luck playing in the Slot Jackpot Terbesar by maximizing auto spins by placing the lowest bet at 200 rupiah in every other video slot game that bettors can play in any slot game.

The Slot Jackpot Terbesar site is very easy to find for bettors who want to play on this site, because this site has been included in the Google ranking which will be very easy for bettors to find, especially as there are still many Bandarbola855 Biggest Jackpot Slot lovers who want to play on this site.

There are so many secret buttons every time you play on this Bandarbola855 Slot Jackpot Terbesar site. Among them is autospin with x10 and even more, this really affects the biggest win for the bettor. How not Bandarbola855 Biggest Jackpot Slot always gives promising wins for bettors every time they play carefully.

In the event that you're a newbie, you'll want to make sure that you've got a good idea of what you're getting yourself into. If you're looking for the most effective way to get rid of the excess weight, you've come to the right place.
Reply all
Reply to author
Forward
0 new messages