Exemple #1
0
        public static void Serialize(this TokenSet tokenSet, SerializeContext context)
        {
            var words = tokenSet.ToEnumeration().ToList();

            context.Write(words.Count);
            foreach (var word in words)
            {
                context.Write(word);
            }
        }
Exemple #2
0
        public static ReadOnlySpan <byte> Serialize(this Vector vector)
        {
            using var ms = new MemoryStream();
            using var bw = new BinaryWriter(ms, Encoding.UTF8);
            var context = new SerializeContext(bw);

            context.Write(vector.Count);
            foreach (var value in vector.Save())
            {
                context.Write(value);
            }

            ms.Position = 0;
            return(ms.GetBuffer());
        }
Exemple #3
0
        public static ReadOnlySpan <byte> Serialize(this InvertedIndex invertedIndex)
        {
            using var ms = new MemoryStream();
            using var bw = new BinaryWriter(ms, Encoding.UTF8);
            var context = new SerializeContext(bw);

            var entries =
                invertedIndex.OrderBy(kvp => kvp.Key, StringComparer.Ordinal)
                .ToList();

            context.Write(entries.Count);
            foreach (var entry in entries)
            {
                context.Write(entry.Key);
                entry.Value.Serialize(context);
            }

            ms.Position = 0;
            return(ms.GetBuffer());
        }
Exemple #4
0
        public static void Serialize(this InvertedIndexEntry invertedIndexEntry, SerializeContext context)
        {
            context.Write(invertedIndexEntry.Index);
            context.Write(invertedIndexEntry.Count);

            foreach (KeyValuePair <string, FieldMatches> pair in invertedIndexEntry)
            {
                context.Write(pair.Key);
                context.Write(pair.Value.Count); // FieldMatches

                foreach (KeyValuePair <string, FieldMatchMetadata> fieldMatches in pair.Value)
                {
                    context.Write(fieldMatches.Key);
                    context.Write(fieldMatches.Value.Count); // FieldMatchMetadata

                    foreach (KeyValuePair <string, IList <object?> > fieldMatchMetadata in fieldMatches.Value)
                    {
                        context.Write(fieldMatchMetadata.Key);
                        context.Write(fieldMatchMetadata.Value.Count);

                        foreach (object?meta in fieldMatchMetadata.Value)
                        {
                            var type = meta?.GetType();
                            if (type == null)
                            {
                                continue;
                            }

                            if (!SerializeContext.KnownTypes.TryGetValue(type, out var serializer))
                            {
                                throw new SerializationException($"no serializer registered for '{type.Name}'");
                            }

                            if (context.Write(meta != null && type != null))
                            {
                                context.Write(type?.AssemblyQualifiedName !);
                                context.Write(serializer.Item1(meta ?? throw new InvalidOperationException("unexpected null value")));
                            }
                        }
                    }
                }
            }
        }