Exemple #1
0
        /// <summary>
        /// Flushing all
        /// </summary>
        public void Flush()
        {
            if (!isUsed)
            {
                return;
            }

            LTrie table       = null;
            bool  WasOperated = false;

            byte[] deletedValue = null;
            byte[] btKey        = null;
            byte[] btVal        = null;

            foreach (var el1 in _dRemove.OrderBy(r => r.Key))
            {
                if (!tbls.TryGetValue(el1.Key, out table))
                {
                    table         = _t.GetWriteTableFromBuffer(el1.Key);
                    tbls[el1.Key] = table;
                }

                foreach (var el2 in el1.Value.OrderBy(r => r.Key))
                {
                    //_t.RemoveKey<byte[]>(el1.Key, el2.Value);
                    btKey = el2.Value;
                    table.Remove(ref btKey, out WasOperated, false, out deletedValue);
                }
            }

            _dRemove.Clear();

            foreach (var el1 in _dInsert.OrderBy(r => r.Key))
            {
                if (!tbls.TryGetValue(el1.Key, out table))
                {
                    table         = _t.GetWriteTableFromBuffer(el1.Key);
                    tbls[el1.Key] = table;
                }

                //List<string> tt = el1.Value.OrderBy(r => r.Key).Select(r => r.Key).ToList();
                foreach (var el2 in el1.Value.OrderBy(r => r.Key))
                {
                    //_t.Insert<byte[], byte[]>(el1.Key, el2.Value.Key, el2.Value.Value);
                    btKey = el2.Value.Key;
                    btVal = el2.Value.Value;
                    table.Add(ref btKey, ref btVal, out WasOperated, false);
                }
            }

            _dInsert.Clear();
            _cnt.Clear();

            WasAutomaticallyFlushed = false;

            isUsed = false;
        }
Exemple #2
0
        /// <summary>
        /// Deletes user table
        /// </summary>
        /// <param name="userTableName"></param>
        public void DeleteTable(string userTableName)
        {
            string tableName = GetUserTableNameAsString(userTableName);

            this.cachedTableNames.Remove(tableName);

            //Blocking Schema
            _sync_openTablesHolder.EnterWriteLock();
            try
            {
                if (_openTablesHolder.ContainsKey(tableName))
                {
                    //Someone can use this table
                    //We dispose table, what will cause disposing DBstorage and RollbackStorage
                    //In this moment parallel reading table threads inside of Iterations, can get Exceptions - What is acceptable for now.
                    _openTablesHolder[tableName].Dispose();


                    _openTablesHolder[tableName] = null;

                    //Deleting table from the holder
                    _openTablesHolder.Remove(tableName);
                }

                //Trying to get full file name, via globilzed function which will also support mapping outside the DB main directory
                string physicalDbFileName = GetPhysicalPathToTheUserTable(userTableName);

                if (physicalDbFileName == String.Empty)
                {
                    return; //fake
                }
                //Removing record from the schema

                byte[] btTableName = GetUserTableNameAsByte(userTableName);

                //ulong cc = LTrie.Count();
                LTrie.Remove(ref btTableName);
                LTrie.Commit();
                //cc = LTrie.Count();

                //Deleting file physically
                if (physicalDbFileName != "MEMORY")
                {
                    DeleteAllReleatedTableFiles(physicalDbFileName);
                }
            }
            catch (System.Exception ex)
            {
                DBreezeException.Throw(DBreezeException.eDBreezeExceptions.SCHEME_TABLE_DELETE_FAILED, userTableName, ex);
            }
            finally
            {
                _sync_openTablesHolder.ExitWriteLock();
            }
        }
Exemple #3
0
        /// <summary>
        /// Removes resources from database and In-Memory dictionary
        /// </summary>
        public void Remove(IList <string> resourcesNames)
        {
            if (resourcesNames == null || resourcesNames.Count == 0)
            {
                return;
            }

            byte[] btKey;
            string rn = String.Empty;

            _sync.EnterWriteLock();
            try
            {
                foreach (var rs in resourcesNames)
                {
                    if (String.IsNullOrEmpty(rs))
                    {
                        continue;
                    }

                    rn = _urp + rs;
                    _d.Remove(rn);

                    btKey = DataTypesConvertor.ConvertKey <string>(rn);
                    LTrie.Remove(ref btKey);
                }

                LTrie.Commit();
            }
            catch (Exception ex)
            {
                throw DBreezeException.Throw(DBreezeException.eDBreezeExceptions.DBREEZE_RESOURCES_CONCERNING, "in Remove batch", ex);
            }
            finally
            {
                _sync.ExitWriteLock();
            }
        }
Exemple #4
0
        public void Flush(string tableName)
        {
            LTrie table       = null;
            bool  WasOperated = false;

            byte[] deletedValue = null;
            byte[] btKey        = null;
            byte[] btVal        = null;

            if (_dRemove.ContainsKey(tableName))
            {
                if (!tbls.TryGetValue(tableName, out table))
                {
                    table           = _t.GetWriteTableFromBuffer(tableName);
                    tbls[tableName] = table;
                }

                foreach (var el2 in _dRemove[tableName].OrderBy(r => r.Key))
                {
                    // _t.RemoveKey<byte[]>(tableName, el2.Value);
                    btKey = el2.Value;
                    table.Remove(ref btKey, out WasOperated, false, out deletedValue);
                }

                _dRemove[tableName].Clear();
                _dRemove.Remove(tableName);
            }

            if (_dInsert.ContainsKey(tableName))
            {
                if (!tbls.TryGetValue(tableName, out table))
                {
                    table           = _t.GetWriteTableFromBuffer(tableName);
                    tbls[tableName] = table;
                }

                foreach (var el2 in _dInsert[tableName].OrderBy(r => r.Key))
                {
                    //_t.Insert<byte[],byte[]>(tableName, el2.Value.Key, el2.Value.Value);
                    btKey = el2.Value.Key;
                    btVal = el2.Value.Value;
                    table.Add(ref btKey, ref btVal, out WasOperated, false);
                }
                _dInsert[tableName].Clear();
                _dInsert.Remove(tableName);
            }

            _cnt[tableName]         = 0;
            WasAutomaticallyFlushed = true;
        }
Exemple #5
0
        public void FinishTransaction(ulong tranNumber)
        {
            //_sync_transactionsTables.EnterReadLock();
            _sync_transactionsTables.EnterWriteLock();
            try
            {
                Dictionary <string, ITransactable> tbls = null;
                _transactionsTables.TryGetValue(tranNumber, out tbls);

                if (tbls != null)
                {
                    //Starting procedure

                    //1. Saving all table names into db - needed in case if something happens (Power loss or whatever).
                    //   Then restarted TransactionalJournal will delete rollback files for these tables (they are all committed)

                    List <string> committedTablesNames = new List <string>();
                    foreach (var tt in tbls)
                    {
                        committedTablesNames.Add(tt.Key);
                    }


                    string serTbls   = committedTablesNames.SerializeXml();
                    byte[] btSerTbls = System.Text.Encoding.UTF8.GetBytes(serTbls);

                    byte[] key = tranNumber.To_8_bytes_array_BigEndian();

                    LTrie.Add(ref key, ref btSerTbls);
                    LTrie.Commit();

                    //2. Calling transaction End for all tables
                    try
                    {
                        foreach (var tt in tbls)
                        {
                            tt.Value.ITRCommitFinished();
                        }
                    }
                    catch (Exception ex)
                    {
                        //CASCADE from ITRCommitFinished, brings to NON-OPERATABLE
                        throw ex;
                    }

                    //3. Deleting Record in Journal
                    LTrie.Remove(ref key);
                    LTrie.Commit();

                    //Clearing transaction number
                    tbls.Clear();
                    _transactionsTables.Remove(tranNumber);


                    //When Transaction File becomes big we try to clean it.
                    if (LTrie.Storage.Length > MaxlengthOfTransactionFile && _transactionsTables.Count() == 0)
                    {
                        LTrie.Storage.RecreateFiles();
                        LTrie.Dispose();

                        Storage         = new StorageLayer(Path.Combine(Engine.MainFolder, JournalFileName), LTrieSettings, Engine.Configuration);
                        LTrie           = new LTrie(Storage);
                        LTrie.TableName = "DBreeze.TranJournal";
                    }
                }
            }
            catch (System.Exception ex)
            {
                //CASCADE
                throw ex;
            }
            finally
            {
                _sync_transactionsTables.ExitWriteLock();
                //_sync_transactionsTables.ExitReadLock();
            }
        }
Exemple #6
0
        /// <summary>
        /// Indexer self
        /// </summary>
        void Indexer()
        {
            int maximalIterations = 10; //Iterations then a breath
            int currentItter      = 0;

            Dictionary <byte[], Dictionary <string, HashSet <uint> > > defTasks = new Dictionary <byte[], Dictionary <string, HashSet <uint> > >();
            Dictionary <string, HashSet <uint> >       defTask = null;
            Dictionary <string, TextSearchHandler.ITS> itbls   = new Dictionary <string, TextSearchHandler.ITS>();

            while (true)
            {
                currentItter = 0;
                defTasks.Clear();
                itbls.Clear();

                lock (lock_operation)
                {
                    foreach (var row in LTrie.IterateForward(false).Take(maximalIterations))
                    {
                        currentItter++;
                        defTask = new Dictionary <string, HashSet <uint> >();
                        DBreeze.Utils.Biser.Decode_DICT_PROTO_STRING_UINTHASHSET(row.GetFullValue(true), defTask, Compression.eCompressionMethod.NoCompression);
                        defTasks.Add(row.Key, defTask);

                        foreach (var el in defTask)
                        {
                            if (!itbls.ContainsKey(el.Key))
                            {
                                itbls[el.Key] = new TextSearchHandler.ITS();
                            }

                            foreach (var el1 in el.Value)
                            {
                                itbls[el.Key].ChangedDocIds.Add((int)el1);
                            }
                        }
                    }

                    if (currentItter == 0)
                    {
                        inDeferredIndexer = 0;  //going out
                        return;
                    }
                }

                //Indexing defTasks
                using (var tran = this.DBreezeEngine.GetTransaction())
                {
                    tran.tsh = new TextSearchHandler(tran);
                    tran.SynchronizeTables(itbls.Keys.ToList());
                    tran.tsh.DoIndexing(tran, itbls);
                    tran.Commit();

                    itbls.Clear();
                }

                //Removing indexed docs from LTrie
                lock (lock_operation)
                {
                    byte[] key = null;

                    foreach (var el in defTasks)
                    {
                        key = el.Key;
                        LTrie.Remove(ref key);
                    }

                    LTrie.Commit();
                }
            } //eo while
        }     //eof