Example #1
0
        public bool SendBitField(bool[] bitField, bool obsf)
        {
            var obsfIDs = new UInt32[0];

            if (obsf && bitField.Length > 32)
            {
                var    rand      = new Random();
                var    obsfCount = (UInt32)Math.Min(16, bitField.Length / 16);
                UInt32 distObsf  = 0;
                obsfIDs = new UInt32[obsfCount];

                while (distObsf < obsfCount)
                {
                    var piece = (UInt32)rand.Next(0, bitField.Length);
                    if (obsfIDs.Contains(piece))
                    {
                        continue;
                    }

                    obsfIDs[distObsf] = piece;
                    distObsf++;
                }
            }

            var bytes = new byte[bitField.Length / 8];

            for (UInt32 i = 0; i < bitField.Length; i++)
            {
                if (obsfIDs.Contains(i))
                {
                    continue;
                }

                var x = (int)Math.Floor((double)i / 8);
                var p = (ushort)(i % 8);

                if (bitField[i])
                {
                    bytes[x] = bytes[x].SetBit(p);
                }
            }

            var sent = Socket.Send(new PeerMessageBuilder(5).Add(bytes).Message());

            if (obsfIDs.Length > 0)
            {
                foreach (var obsfID in obsfIDs)
                {
                    SendHave(obsfID);
                }
            }

            return(sent == (5 + bitField.Length));
        }
Example #2
0
        private Dictionary <int, int> GetMaxCharacterWidth(SheetData sheetData)
        {
            //iterate over all cells getting a max char value for each column
            var maxColWidth = new Dictionary <int, int>();
            var rows        = sheetData.Elements <Row>();
            //TODO: Be smarter about this for our set styles
            var numberStyles = new UInt32[] { 5, 6, 7, 8 };          //styles that will add extra chars
            var boldStyles   = new UInt32[] { 1, 2, 3, 4, 6, 7, 8 }; //styles that will bold

            foreach (var r in rows)
            {
                var cells = r.Elements <Cell>().ToArray();

                //using cell index as my column
                for (int i = 0; i < cells.Length; i++)
                {
                    var cell           = cells[i];
                    var cellValue      = cell.CellValue == null ? string.Empty : cell.CellValue.InnerText;
                    var cellTextLength = cellValue.Length;

                    if (cell.StyleIndex != null && numberStyles.Contains(cell.StyleIndex))
                    {
                        int thousandCount = (int)Math.Truncate((double)cellTextLength / 4);

                        //add 3 for '.00'
                        cellTextLength += (3 + thousandCount);
                    }

                    if (cell.StyleIndex != null && boldStyles.Contains(cell.StyleIndex))
                    {
                        //add an extra char for bold - not 100% acurate but good enough for what i need.
                        cellTextLength += 1;
                    }

                    if (maxColWidth.ContainsKey(i))
                    {
                        var current = maxColWidth[i];
                        if (cellTextLength > current)
                        {
                            maxColWidth[i] = cellTextLength;
                        }
                    }
                    else
                    {
                        maxColWidth.Add(i, cellTextLength);
                    }
                }
            }

            return(maxColWidth);
        }
Example #3
0
        protected Dictionary <int, int> GetColumnMaxCharacters(SheetData sheetData)
        {
            //iterate over all cells getting a max char value for each column
            Dictionary <int, int> returnCollection = new Dictionary <int, int>();
            var rows = sheetData.Elements <Row>();

            UInt32[] numberStyles = new UInt32[] { 5, 6, 7, 8 };          //styles that will add extra chars
            UInt32[] boldStyles   = new UInt32[] { 1, 2, 3, 4, 6, 7, 8 }; //styles that will bold

            var rowNumber = 0;

            foreach (var r in rows)
            {
                rowNumber++;

                if (RowsToExcludeMaxCharacters.Contains(rowNumber))
                {
                    continue;
                }

                var cells = r.Elements <Cell>().ToArray();

                //using cell index as my column
                for (int i = 0; i < cells.Length; i++)
                {
                    var cell      = cells[i];
                    var cellValue = cell.CellValue == null ? string.Empty : cell.CellValue.InnerText;

                    var cellTextLength = cellValue.Length;

                    if (cellValue.IndexOf(Environment.NewLine) != -1)
                    {
                        cellTextLength = 0;

                        var values = cellValue.Replace(Environment.NewLine, "|").Split('|');

                        foreach (var value in values)
                        {
                            var valueLength = value.Length;

                            if (valueLength > cellTextLength)
                            {
                                cellTextLength = valueLength;
                            }
                        }
                    }

                    if (cell.StyleIndex != null && numberStyles.Contains(cell.StyleIndex))
                    {
                        int thousandCount = (int)Math.Truncate((double)cellTextLength / 4);

                        //add 3 for '.00'
                        cellTextLength += (3 + thousandCount);
                    }

                    if (cell.StyleIndex != null && boldStyles.Contains(cell.StyleIndex))
                    {
                        //add an extra char for bold - not 100% acurate but good enough for what i need.
                        cellTextLength += 1;
                    }

                    if (returnCollection.ContainsKey(i))
                    {
                        var current = returnCollection[i];
                        if (cellTextLength > current)
                        {
                            returnCollection[i] = cellTextLength;
                        }
                    }
                    else
                    {
                        returnCollection.Add(i, cellTextLength);
                    }
                }
            }

            return(returnCollection);
        }
Example #4
0
        /// <summary>
        /// Restores database files, pages and objects from a .csv file data created with ExportToCSV
        /// </summary>
        /// <param name="session">the active session</param>
        /// <param name="csvDirectory">Path to directory containing CSV files</param>
        static public void ImportFromCSV(this SessionBase session, string csvDirectory)
        {
            const char    fieldSeperator = ',';
            DirectoryInfo di             = new DirectoryInfo(csvDirectory);

#if WINDOWS_PHONE
            List <FileInfo> files = di.GetFiles("*.csv").ToList();
#else
            List <FileInfo> files = di.GetFiles("*.csv", SearchOption.TopDirectoryOnly).ToList();
#endif
            Schema schema = session.OpenSchema(false);
            using (StreamReader textReader = new StreamReader(Path.Combine(csvDirectory, "Database.csv")))
            {
                string header       = textReader.ReadLine();
                string dbInfoString = textReader.ReadLine();
                while (dbInfoString != null)
                {
                    string[] dbInfo = dbInfoString.Split(fieldSeperator);
                    UInt32   dbNum  = UInt32.Parse(dbInfo[0]);
                    string   dbName = dbInfo[1].Trim(new char[] { '"' });
                    Database db     = null;
                    if (dbNum < 10)
                    {
                        db = session.OpenDatabase(dbNum, false, false);
                    }
                    if (db == null)
                    {
                        db = session.NewDatabase(dbNum, 0, dbName);
                    }
                    dbInfoString = textReader.ReadLine();
                }
            }
            using (StreamReader textReader = new StreamReader(Path.Combine(csvDirectory, "Page.csv")))
            {
                string header         = textReader.ReadLine();
                string pageInfoString = textReader.ReadLine();
                while (pageInfoString != null)
                {
                    int      i             = 0;
                    string[] pageInfo      = pageInfoString.Split(fieldSeperator);
                    UInt32   dbNum         = UInt32.Parse(pageInfo[i++]);
                    UInt16   pageNum       = UInt16.Parse(pageInfo[i++]);
                    UInt16   numberOfSlots = UInt16.Parse(pageInfo[i++]);
                    UInt16   firstFreeSlot = UInt16.Parse(pageInfo[i++]);
                    UInt64   versionNumber = UInt64.Parse(pageInfo[i++]);
                    PageInfo.encryptionKind  encryptionKind = (PageInfo.encryptionKind)Enum.Parse(typeof(PageInfo.encryptionKind), pageInfo[i++]);
                    PageInfo.compressionKind compressed     = (PageInfo.compressionKind)Enum.Parse(typeof(PageInfo.compressionKind), pageInfo[i++]);
                    UInt32   typeVersion = UInt32.Parse(pageInfo[i]);
                    Database db          = session.OpenDatabase(dbNum, false, false);
                    Page     page        = db.CachedPage(pageNum);
                    if (page == null)
                    {
                        page = new Page(db, pageNum, typeVersion, numberOfSlots);
                        page.PageInfo.Compressed    = compressed;
                        page.PageInfo.Encryption    = encryptionKind;
                        page.PageInfo.VersionNumber = versionNumber;
                        page.PageInfo.NumberOfSlots = numberOfSlots;
                        page.PageInfo.FirstFreeSlot = firstFreeSlot;
                        session.UpdatePage(ref page);
                    }
                    pageInfoString = textReader.ReadLine();
                }
            }

            var schemaInternalTypeFiles = new[] {
                "VelocityDb.TypeInfo.Schema65747.csv",
                "VelocityDb.TypeInfo.TypeVersion65749.csv",
                "VelocityDb.TypeInfo.VelocityDbType65753.csv",
                "VelocityDb.TypeInfo.DataMember65745.csv",
                "VelocityDb.Collection.BTree.BTreeSetOidShortVelocityDb.TypeInfo.VelocityDbType65623.csv",
                "VelocityDb.Collection.Comparer.VelocityDbTypeComparer65655.csv"
            };
            var schemaInternalTypeFileInfo = new List <FileInfo>();
            foreach (var fileName in schemaInternalTypeFiles)
            {
                schemaInternalTypeFileInfo.Add(di.GetFiles(fileName, SearchOption.TopDirectoryOnly).First());
            }

            foreach (FileInfo info in schemaInternalTypeFileInfo)
            {
                string numberString = Regex.Match(info.Name, @"\d+").Value;
                if (numberString.Length > 0)
                {
                    UInt32      typeShortId = UInt32.Parse(numberString);
                    UInt16      slotNumber  = (UInt16)typeShortId;
                    TypeVersion tv          = schema.GetTypeVersion(typeShortId, session);
                    if (tv != null)
                    {
                        using (StreamReader textReader = new StreamReader(info.FullName))
                        {
                            CsvReader csvReader   = new CsvReader(textReader, true);
                            string[]  fileldNames = csvReader.GetFieldHeaders();
                            foreach (string[] record in csvReader)
                            {
                                tv.ObjectBytesFromStrings(record, fileldNames, session, schema);
                            }
                        }
                    }
                }
            }
            Database schemaDb   = session.OpenDatabase(Schema.SchemaDB);
            Page     schemaPage = schemaDb.CachedPage(1);
            schemaPage.FinishUpCsvImport();
            var schemaTypes = new UInt32[] { 65747, 65749, 65753, 65745, 65623, 65655 };
            for (int i = 0; i < 2; i++)
            {
                foreach (FileInfo info in files)
                {
                    string numberString = Regex.Match(info.Name, @"\d+").Value;
                    if (numberString.Length > 0)
                    {
                        UInt32 typeShortId = UInt32.Parse(numberString);
                        UInt16 slotNumber  = (UInt16)typeShortId;
                        if (((i == 0 && slotNumber < Schema.s_bootupTypeCountExpanded) || (i == 1 && slotNumber >= Schema.s_bootupTypeCountExpanded)) && !schemaTypes.Contains(typeShortId))
                        {
                            TypeVersion tv = schema.GetTypeVersion(typeShortId, session);
                            if (tv != null)
                            {
                                using (StreamReader textReader = new StreamReader(info.FullName))
                                {
                                    var      csvReader   = new CsvReader(textReader, true);
                                    string[] fileldNames = csvReader.GetFieldHeaders();
                                    foreach (string[] record in csvReader)
                                    {
                                        tv.ObjectBytesFromStrings(record, fileldNames, session, schema);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Example #5
0
        public bool SendBitField(bool[] bitField, bool obsf)
        {
            UInt32[] obsfIDs = new UInt32[0];

            if (obsf && bitField.Length > 32)
            {
                Random rand = new Random();
                UInt32 obsfCount = (UInt32)Math.Min(16, bitField.Length / 16);
                UInt32 distObsf = 0;
                obsfIDs = new UInt32[obsfCount];

                while (distObsf < obsfCount)
                {
                    UInt32 piece = (UInt32)rand.Next(0, bitField.Length);
                    if (obsfIDs.Contains(piece)) continue;

                    obsfIDs[distObsf] = piece;
                    distObsf++;
                }
            }

            byte[] bytes = new byte[bitField.Length / 8];

            for (UInt32 i = 0; i < bitField.Length; i++)
            {
                if (obsfIDs.Contains(i)) continue;

                int x = (int)Math.Floor((double)i/8);
                ushort p = (ushort) (i%8);

                if (bitField[i])
                {
                    bytes[x] = bytes[x].SetBit(p);
                }
            }

            int sent = Socket.Send(new PeerMessageBuilder(5).Add(bytes).Message());

            if (obsfIDs.Length > 0)
            {
                foreach (UInt32 obsfID in obsfIDs)
                {
                    SendHave(obsfID);
                }
            }

            return sent == (5 + bitField.Length);
        }