Пример #1
0
        public void ResizeTable(uint tableID, int szDatum)
        {
            Assert(Tables[tableID] != null);
            Assert(szDatum >= Tables[tableID].DatumSize);

            lock (m_lock)
            {
                IDBTable tbl = GetTable(tableID, true);

                if (tbl.DatumSize < szDatum)
                {
                    string tmpFile = Path.GetTempFileName();
                    using (new AutoReleaser(() => File.Delete(tmpFile)))
                        using (FileStream fs = File.Create(tmpFile))
                        {
                            var tmpWriter       = new RawDataWriter(fs, Encoding.UTF8);
                            IDBTableProvider dp = m_accessPath.GetDataProvider(tableID);

                            using (dp.Lock())
                            {
                                foreach (IDataRow row in dp.Enumerate())
                                {
                                    row.Write(tmpWriter);
                                }


                                var tmpReader = new RawDataReader(fs, Encoding.UTF8);
                                int count     = dp.Count;

                                BeginTableProcessing?.Invoke(tableID);

                                using (new AutoReleaser(() => EndTableProcessing?.Invoke(tableID)))
                                {
                                    uint ver = tbl.Version;
                                    uint tag = tbl.Tag;

                                    dp.Disconnect();
                                    tbl.Disconnect();
                                    File.Delete(tbl.FilePath);
                                    tbl.Create(szDatum, tag);
                                    dp.Connect();

                                    fs.Position = 0;

                                    for (int i = 0; i < count; ++i)
                                    {
                                        IDataRow datum = m_dataFactory.CreateDatum(tableID);
                                        datum.Read(tmpReader);
                                        dp.Insert(datum);
                                    }

                                    tbl.Version = ver;
                                    tbl.Flush();
                                }
                            }
                        }
                }
            }
        }
Пример #2
0
        IDBTable GetTable(uint tableID, bool connect)
        {
            IDBTable tbl = AllTables.FirstOrDefault(t => t.ID == tableID);

            if (!connect)
            {
                return(tbl);
            }

            Dbg.Assert(Program.Settings.ClientInfo != null);

            if (tbl != null)
            {
                if (!tbl.IsConnected)
                {
                    try
                    {
                        tbl.Connect();

                        if (tbl.Tag != Program.Settings.ClientInfo.ClientID)
                        {
                            throw new BadTagException(tbl.Name);
                        }
                    }
                    catch (FileNotFoundException)
                    {
                        System.Diagnostics.Debug.WriteLine($"Impossible d'ouvrir la table {tbl.Name}\nLancement de la procedure de création...");

                        try
                        {
                            tbl.Create(1, Program.Settings.ClientInfo.ClientID);
                        }
                        catch (Exception ex)
                        {
                            System.Diagnostics.Debug.WriteLine($"Erreur lors de la création du fichier!\n Exception: {ex.Message}");
                            throw;
                        }

                        System.Diagnostics.Debug.WriteLine("Création ok");
                    }
                }
            }

            return(tbl);
        }