Exemplo n.º 1
0
 /// <summary>
 ///  Create a copy of a String8. Use when the source of the String8s
 ///  will change (like a reader reusing the same byte[] buffer) and
 ///  you need to keep a copy of specific values with minimal object overhead.
 /// </summary>
 /// <param name="source">String8 to copy</param>
 /// <returns>String8 copy which will persist</returns>
 public String8 GetCopy(ITabularValue source)
 {
     if (source is String8TabularValue)
     {
         return(GetCopy(source.ToString8()));
     }
     return(GetCopy(source.ToString()));
 }
        private static void ITabularValue_Basics(string value, String8 value8, ITabularValue itv)
        {
            Assert.AreEqual(String.IsNullOrEmpty(value), itv.IsNullOrEmpty());
            Assert.AreEqual(value8, itv.ToString8());
            Assert.AreEqual(value, itv.ToString());

            bool asBoolean;

            if (bool.TryParse(value, out asBoolean))
            {
                Assert.AreEqual(asBoolean, itv.ToBoolean());
            }
            else
            {
                Verify.Exception <FormatException>(() => { var result = itv.ToBoolean(); });
            }

            int asInteger;

            if (int.TryParse(value, out asInteger))
            {
                Assert.AreEqual(asInteger, itv.ToInteger());
            }
            else
            {
                Verify.Exception <FormatException>(() => { var result = itv.ToInteger(); });
            }

            DateTime asDateTime;

            if (DateTime.TryParse(value, CultureInfo.CurrentCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal, out asDateTime))
            {
                Assert.AreEqual(asDateTime, itv.ToDateTime());
            }
            else
            {
                Verify.Exception <FormatException>(() => { var result = itv.ToDateTime(); });
            }
        }
Exemplo n.º 3
0
        private static TableMetadata Build(IStreamProvider streamProvider, string tableRootPath)
        {
            TableMetadata metadata       = new TableMetadata();
            string        schemaFilePath = Path.Combine(tableRootPath, SchemaFileName);

            using (ITabularReader sr = TabularFactory.BuildReader(streamProvider.OpenRead(schemaFilePath), SchemaFileName))
            {
                int nameIndex = sr.ColumnIndex("Name");
                int typeIndex = sr.ColumnIndex("Type");

                while (sr.NextRow())
                {
                    metadata.Schema.Add(new ColumnDetails(sr.Current(nameIndex).ToString(), TypeProviderFactory.Get(sr.Current(typeIndex).ToString()).Type));
                }
            }

            using (ITabularReader mr = TabularFactory.BuildReader(streamProvider.OpenRead(Path.Combine(tableRootPath, MetadataFileName)), MetadataFileName))
            {
                int nameIndex    = mr.ColumnIndex("Name");
                int contextIndex = mr.ColumnIndex("Context");
                int valueIndex   = mr.ColumnIndex("Value");

                while (mr.NextRow())
                {
                    String8       name    = mr.Current(nameIndex).ToString8();
                    String8       context = mr.Current(contextIndex).ToString8();
                    ITabularValue value   = mr.Current(valueIndex);

                    if (name.Equals("RowCount"))
                    {
                        long rowCount;
                        if (value.ToString8().TryToLong(out rowCount))
                        {
                            metadata.RowCount = rowCount;
                        }
                    }
                    else
                    {
                        throw new NotImplementedException($"TableMetadataSerializer.Read doesn't know how to read Metadata '{name}'");
                    }
                }
            }

            metadata.Query = streamProvider.ReadAllText(Path.Combine(tableRootPath, ConfigQueryPath));

            string partitionsPath = Path.Combine(tableRootPath, PartitionsFileName);

            if (streamProvider.Attributes(partitionsPath).Exists)
            {
                using (ITabularReader pr = TabularFactory.BuildReader(streamProvider.OpenRead(partitionsPath), PartitionsFileName))
                {
                    int nameIndex = pr.ColumnIndex("Name");

                    while (pr.NextRow())
                    {
                        metadata.Partitions.Add(pr.Current(nameIndex).ToString());
                    }
                }
            }

            return(metadata);
        }