Example #1
0
        public void Read(BionReader reader)
        {
            reader.Read(BionToken.StartArray);
            reader.Read(BionToken.Integer);
            int wordCount = (int)reader.CurrentInteger();

            Words.Clear();

            Index.Clear();
            Indexed = false;

            while (true)
            {
                reader.Read();
                if (reader.TokenType == BionToken.EndArray)
                {
                    break;
                }

                // NOTE: Not copying word. Must use a 'ReadAll' BufferedReader
                reader.Expect(BionToken.String);
                String8 value = reader.CurrentString8();

                reader.Read(BionToken.Integer);
                int count = (int)reader.CurrentInteger();

                // Add to List, but not Index. Index will be populated when first needed.
                Words.Add(value);
                Counts.Add(count);
            }

            // Set capacity exact to save RAM
            Words.SetCapacity(Words.LengthBytes);
        }
        private static void Expect(BionReader reader, BionToken requiredToken, string parseContext)
        {
            if (reader.TokenType == BionToken.None)
            {
                reader.Read();
            }

            if (reader.TokenType != requiredToken)
            {
                throw new IOException($"Reader found invalid token type '{requiredToken}' while parsing {parseContext}.");
            }

            reader.Read();
        }
Example #3
0
        private static void Count(string filePath, string fromDictionaryPath)
        {
            VerifyFileExists(filePath);
            VerifyFileExists(fromDictionaryPath);
            long tokenCount = 0;

            using (new ConsoleWatch($"Reading [Count] {filePath} ({FileLength.MB(filePath)})...",
                                    () => $"Done; {tokenCount:n0} tokens found in file"))
            {
                if (filePath.EndsWith(".bion", StringComparison.OrdinalIgnoreCase))
                {
                    using (WordCompressor compressor = (fromDictionaryPath == null ? null : WordCompressor.OpenRead(fromDictionaryPath)))
                        using (BionReader reader = new BionReader(File.OpenRead(filePath), compressor: compressor))
                        {
                            while (reader.Read())
                            {
                                tokenCount++;
                            }
                        }
                }
                else
                {
                    using (JsonTextReader reader = new JsonTextReader(new StreamReader(filePath)))
                    {
                        while (reader.Read())
                        {
                            tokenCount++;
                        }
                    }
                }
            }
        }
Example #4
0
        private static void RoundTrip(Action <BionWriter> write, Action <BionReader> readAndVerify)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                // Write desired value(s) without closing stream
                using (BionWriter writer = new BionWriter(new BufferedWriter(stream)
                {
                    CloseStream = false
                }))
                {
                    write(writer);
                }

                // Track position and seek back
                long length = stream.Position;
                stream.Seek(0, SeekOrigin.Begin);

                // Read, verify, validate position and no more content
                using (BionReader reader = new BionReader(new BufferedReader(stream)
                {
                    CloseStream = false
                }))
                {
                    readAndVerify(reader);

                    Assert.AreEqual(length, reader.BytesRead);
                    Assert.IsFalse(reader.Read());
                }
            }
        }
Example #5
0
        public override bool Read()
        {
            bool success = _reader.Read();

            switch (_reader.TokenType)
            {
            case BionToken.EndArray:
                SetToken(JsonToken.EndArray);
                break;

            case BionToken.EndObject:
                SetToken(JsonToken.EndObject);
                break;

            case BionToken.False:
                SetToken(JsonToken.Boolean, false);
                break;

            case BionToken.Float:
                SetToken(JsonToken.Float, _reader.CurrentFloat());
                break;

            case BionToken.Integer:
                SetToken(JsonToken.Integer, (int)_reader.CurrentInteger());
                break;

            case BionToken.None:
                SetToken(JsonToken.None);
                break;

            case BionToken.Null:
                SetToken(JsonToken.Null, null);
                break;

            case BionToken.PropertyName:
                SetToken(JsonToken.PropertyName, _reader.CurrentString());
                break;

            case BionToken.StartArray:
                SetToken(JsonToken.StartArray);
                break;

            case BionToken.StartObject:
                SetToken(JsonToken.StartObject);
                break;

            case BionToken.String:
                SetToken(JsonToken.String, _reader.CurrentString());
                break;

            case BionToken.True:
                SetToken(JsonToken.Boolean, true);
                break;

            default:
                throw new NotImplementedException($"BionDataReader.Convert not implemented for '{_reader.TokenType}'.");
            }

            return(success);
        }
Example #6
0
        public void ContainerIndex_EndToEnd()
        {
            string jsonFilePath   = @"Content\Medium.json";
            string bionFilePath   = Path.ChangeExtension(jsonFilePath, ".bion");
            string dictionaryPath = Path.ChangeExtension(bionFilePath, "dict.bion");
            string comparePath    = Path.ChangeExtension(jsonFilePath, "compare.json");

            JsonBionConverter.JsonToBion(jsonFilePath, bionFilePath, dictionaryPath);

            using (WordCompressor compressor = WordCompressor.OpenRead(dictionaryPath))
                using (ContainerIndex cIndex = ContainerIndex.OpenRead(Path.ChangeExtension(bionFilePath, ".cdx")))
                    using (BionReader reader = new BionReader(File.OpenRead(bionFilePath), cIndex, compressor))
                    {
                        for (int i = 0; i < cIndex.Count; ++i)
                        {
                            ContainerEntry container = cIndex[i];

                            // Seek to container start
                            reader.Seek(container.StartByteOffset);

                            // Verify a container start is there
                            int depth = reader.Depth;
                            reader.Read();

                            bool isObject = (reader.TokenType == BionToken.StartObject);
                            Assert.AreEqual((isObject ? BionToken.StartObject : BionToken.StartArray), reader.TokenType);

                            // Read until the depth is back to the same value
                            while (reader.Depth != depth)
                            {
                                reader.Read();
                            }

                            // Verify this is the end container position
                            Assert.AreEqual((isObject ? BionToken.EndObject : BionToken.EndArray), reader.TokenType);
                            Assert.AreEqual(container.EndByteOffset, reader.BytesRead);
                        }
                    }
        }
        public Region2 DeserializeRegion2(BionReader reader)
        {
            Region2 result = new Region2();

            Expect(reader, BionToken.StartObject, "Region2");

            while (reader.TokenType == BionToken.PropertyName)
            {
                String8 propertyName = reader.CurrentString8();

                if (!classFieldParsers.TryGetValue(propertyName, out Action <BionReader, Region2> parser))
                {
                    throw new NotImplementedException($"Unknown property Region.{propertyName}. Stopping.");
                }

                reader.Read();
                parser(reader, result);
                reader.Read();
            }

            Expect(reader, BionToken.EndObject, "Region2");
            return(result);
        }
Example #8
0
        private void WriteRunSubsetStart(JsonTextWriter writer)
        {
            int depth = _bionReader.Depth;

            // Read and copy everything in the run until the end object except the large collections
            while (_bionReader.Read() && _bionReader.Depth > depth)
            {
                if (_bionReader.TokenType == BionToken.PropertyName)
                {
                    String8 propertyName = _bionReader.CurrentString8();
                    if (propertyName.Equals(Files) || propertyName.Equals(Results))
                    {
                        _bionReader.Skip();
                        continue;
                    }
                }

                JsonBionConverter.WriteToken(_bionReader, writer);
            }

            // Start a results array and leave open
            writer.WritePropertyName("results");
            writer.WriteStartArray();
        }
Example #9
0
        private static void Read(string filePath, string fromDictionaryPath)
        {
            VerifyFileExists(filePath);
            VerifyFileExists(fromDictionaryPath);
            long tokenCount = 0;

            using (new ConsoleWatch($"Reading [Full] {filePath} ({FileLength.MB(filePath)})...",
                                    () => $"Done; {tokenCount:n0} tokens found in file"))
            {
                if (filePath.EndsWith(".bion", StringComparison.OrdinalIgnoreCase))
                {
                    using (WordCompressor compressor = (fromDictionaryPath == null ? null : WordCompressor.OpenRead(fromDictionaryPath)))
                        using (BionReader reader = new BionReader(File.OpenRead(filePath), compressor: compressor))
                        {
                            while (reader.Read())
                            {
                                tokenCount++;

                                switch (reader.TokenType)
                                {
                                case BionToken.PropertyName:
                                case BionToken.String:
                                    String8 value8 = reader.CurrentString8();
                                    //string valueS = reader.CurrentString();
                                    break;

                                case BionToken.Integer:
                                    long valueI = reader.CurrentInteger();
                                    break;

                                case BionToken.Float:
                                    double valueF = reader.CurrentFloat();
                                    break;
                                }
                            }
                        }
                }
                else
                {
                    using (JsonTextReader reader = new JsonTextReader(new StreamReader(filePath)))
                    {
                        while (reader.Read())
                        {
                            tokenCount++;

                            switch (reader.TokenType)
                            {
                            case JsonToken.PropertyName:
                            case JsonToken.String:
                                string valueS = (string)reader.Value;
                                break;

                            case JsonToken.Integer:
                                long valueI = (long)reader.Value;
                                break;

                            case JsonToken.Float:
                                double valueF = (double)reader.Value;
                                break;
                            }
                        }
                    }
                }
            }
        }