public IBin FindTargetBin(IItem item, List <IBin> bins)
        {
            var highestScore = -1.0;
            var targetBin    = bins[0];

            foreach (var bin in bins)
            {
                var score = bin.ScoreItem(item);
                if (score > highestScore)
                {
                    highestScore = score;
                    targetBin    = bin;
                }
            }

            if (Math.Abs(highestScore - 1.0) < Double.Epsilon)
            {
                // Rotate list

                IBin first = bins[0];
                bins.RemoveAt(0);
                bins.Add(first);
                //System.Console.WriteLine("Score {2:0.00}: {0} > {1}", item, targetBin, highestScore);
            }
            return(targetBin);
        }
Esempio n. 2
0
            public void Run()
            {
                saveAsBin.Sync();
                saveAsBin.Close();
                try
                {
                    File4.Copy(oldUri, newUri);
                }
                catch (Exception e)
                {
                    this._enclosing.ReopenOldConfiguration(saveAsBin, oldConfiguration, newUri, e);
                }
                BinConfiguration newConfiguration = this._enclosing.PointToNewUri(oldConfiguration
                                                                                  , newUri);

                try
                {
                    IBin newBin = this._enclosing._storage.Open(newConfiguration);
                    saveAsBin.DelegateTo(newBin);
                    this._enclosing._binRecords.Remove(oldUri);
                    this._enclosing._binRecords.Put(newUri, new SaveAsStorage.BinRecord(newConfiguration
                                                                                        , saveAsBin));
                }
                catch (Exception e)
                {
                    this._enclosing.ReopenOldConfiguration(saveAsBin, oldConfiguration, newUri, e);
                }
            }
            /// <exception cref="Db4objects.Db4o.Ext.Db4oIOException"></exception>
            public override IBin Open(BinConfiguration config)
            {
                IBin bin = base.Open(config);

                ((IBlockSize)Environments.My(typeof(IBlockSize))).Register((IListener4)bin);
                return(bin);
            }
Esempio n. 4
0
        public PortioningMachine(Container container, ILog log, int nBins)
        {
            _container = container;
            _log       = log;

            for (int i = 0; i < nBins; i++)
            {
                _bins.Add(new Bin(container, i));
            }

            _portioner = new Portioner();
            _weight    = new Weight(_portioner);
            _inFeed    = new InFeed(_weight);


            IAssignmentAlgorithm assignmentAlgorithm = new BinScoreAlgorithm();

            _controlUnit = new ControlUnit(_weight, _portioner, _bins, assignmentAlgorithm);

            _inFeed.ItemArrived += new ItemArrivedHandler(delegate(object o, IItem i)
            {
                //_log.LogMessage($"Item with id {i.Id} arrived at infeed");
            });

            foreach (IBin bin in _bins)
            {
                bin.BinEmptied += new BinEmptiedHandler(delegate(object o, BinStat binStat)
                {
                    IBin bin = o as IBin;
                    _log.LogMessage($"Bin {binStat.BinNumber} emptied. Current weight: {binStat.Weight:n0}. Target weight: {binStat.TargetWeight:n0}, Give-away: {binStat.Giveaway:n2}%");
                });
            }
        }
        public virtual void TestExistsWithZeroLengthFile()
        {
            IBin storage = subject.Open(new BinConfiguration(TempFile(), false, 0, false));

            storage.Close();
            Assert.IsFalse(subject.Exists(TempFile()));
        }
Esempio n. 6
0
        /// <exception cref="System.IO.IOException"></exception>
        private static void CopyBin(IStorage sourceStorage, IStorage targetStorage, string
                                    sourcePath, string targetPath)
        {
            IBin origBin = sourceStorage.Open(new BinConfiguration(sourcePath, true, 0, true)
                                              );

            try
            {
                IBin backupBin = targetStorage.Open(new BinConfiguration(targetPath, true, origBin
                                                                         .Length(), false));
                try
                {
                    byte[] buffer    = new byte[4096];
                    int    bytesRead = -1;
                    int    pos       = 0;
                    while ((bytesRead = origBin.Read(pos, buffer, buffer.Length)) >= 0)
                    {
                        backupBin.Write(pos, buffer, bytesRead);
                        pos += bytesRead;
                    }
                }
                finally
                {
                    SyncAndClose(backupBin);
                }
            }
            finally
            {
                SyncAndClose(origBin);
            }
        }
Esempio n. 7
0
        public void item_handler(object source, IItem item)
        {
            _logger.item_arrived(item);
            IBin bin = _algorithm.find_bin(item, _bins);

            bin.add(item);
        }
		protected virtual void Open(bool readOnly)
		{
			if (null != _bin)
			{
				throw new InvalidOperationException();
			}
			_bin = Storage().Open(new BinConfiguration(TempFile(), false, 0, readOnly));
		}
 protected virtual void Open(bool readOnly)
 {
     if (null != _bin)
     {
         throw new InvalidOperationException();
     }
     _bin = Storage().Open(new BinConfiguration(TempFile(), false, 0, readOnly));
 }
Esempio n. 10
0
 /// <exception cref="System.Exception"></exception>
 private void AssertReadWriteString(IBin adapter, string str)
 {
     byte[] data = Sharpen.Runtime.GetBytesForString(str);
     byte[] read = new byte[2048];
     adapter.Write(0, data, data.Length);
     adapter.Read(0, read, read.Length);
     Assert.AreEqual(str, Sharpen.Runtime.GetStringForBytes(read, 0, data.Length));
 }
Esempio n. 11
0
            protected override IBin ProduceBin(BinConfiguration config, int pageSize)
            {
                IBin bin = base.ProduceBin(config, pageSize);

                bin.Write(0, PagingMemoryStorageTestCase.Data, PagingMemoryStorageTestCase.Data.Length
                          );
                return(bin);
            }
        /// <exception cref="System.Exception"></exception>
        public virtual void SetUp()
        {
            storage = new MemoryStorage();
            IBin bin = storage.Open(new BinConfiguration(DbPath, false, 0, false));

            bin.Write(0, new byte[] { 1, 2, 3 }, 3);
            bin.Close();
        }
Esempio n. 13
0
        private void CheckBinWeight(object o, IItem item)
        {
            IBin bin = (o as IBin);

            if (bin.CurrentWeight >= bin.TargetWeight)
            {
                bin.Empty();
            }
        }
Esempio n. 14
0
        private void ReopenOldConfiguration(SaveAsStorage.SaveAsBin saveAsBin, BinConfiguration
                                            config, string newUri, Exception e)
        {
            IBin safeBin = _storage.Open(config);

            saveAsBin.DelegateTo(safeBin);
            throw new Db4oException("Copying to " + newUri + " failed. Reopened " + config.Uri
                                        (), e);
        }
Esempio n. 15
0
		protected virtual void Close()
		{
			if (null != _bin)
			{
				_bin.Sync();
				_bin.Close();
				_bin = null;
			}
		}
 protected virtual void Close()
 {
     if (null != _bin)
     {
         _bin.Sync();
         _bin.Close();
         _bin = null;
     }
 }
			protected override IBin Decorate(BinConfiguration config, IBin bin)
			{
				MsgExceptionHandlingTestCase.CloseAwareBin decorated = new MsgExceptionHandlingTestCase.CloseAwareBin
					(this, bin);
				lock (_openBins)
				{
					_openBins[decorated] = decorated;
				}
				return decorated;
			}
Esempio n. 18
0
 protected override IBin Decorate(BinConfiguration config, IBin bin)
 {
     MsgExceptionHandlingTestCase.CloseAwareBin decorated = new MsgExceptionHandlingTestCase.CloseAwareBin
                                                                (this, bin);
     lock (_openBins)
     {
         _openBins[decorated] = decorated;
     }
     return(decorated);
 }
Esempio n. 19
0
        /// <exception cref="System.IO.IOException"></exception>
        public virtual void Rename(string oldUri, string newUri)
        {
            IBin bin = ((IBin)Sharpen.Collections.Remove(_binsByUri, oldUri));

            if (bin == null)
            {
                throw new IOException("Bin not found: " + oldUri);
            }
            _binsByUri[newUri] = bin;
        }
Esempio n. 20
0
            private long BackupLength()
            {
                IBin backupBin = BackupStorage().Open(new BinConfiguration(TempFile(), true, 0, true
                                                                           ));
                long backupLength = backupBin.Length();

                backupBin.Sync();
                backupBin.Close();
                return(backupLength);
            }
Esempio n. 21
0
 public LogReplayer(String logFilePath, IBin bin, ISet commands)
 {
     _logFilePath = logFilePath;
     _bin = bin;
     _commands = commands;
     _counts = new Hashtable();
     foreach (object com in commands)
     {
         _counts[com] = (Int64)0;
     }
 }
Esempio n. 22
0
 public LogReplayer(String logFilePath, IBin bin, ISet commands)
 {
     _logFilePath = logFilePath;
     _bin         = bin;
     _commands    = commands;
     _counts      = new Hashtable();
     foreach (object com in commands)
     {
         _counts[com] = (Int64)0;
     }
 }
Esempio n. 23
0
		/// <exception cref="Db4objects.Db4o.Ext.Db4oIOException"></exception>
		public CachingBin(IBin bin, ICache4 cache, int pageCount, int pageSize) : base(bin
			)
		{
			_onDiscardPage = new _IProcedure4_22(this);
			_producerFromDisk = new _IFunction4_138(this);
			_producerFromPool = new _IFunction4_147(this);
			_pageSize = pageSize;
			_pagePool = new SimpleObjectPool(NewPagePool(pageCount));
			_cache = cache;
			_fileLength = _bin.Length();
		}
 private void SyncAndClose(IBin bin)
 {
     try
     {
         bin.Sync();
     }
     finally
     {
         bin.Close();
     }
 }
Esempio n. 25
0
        /// <summary>opens a Bin for the given URI.</summary>
        /// <remarks>opens a Bin for the given URI.</remarks>
        /// <exception cref="Db4objects.Db4o.Ext.Db4oIOException"></exception>
        public override IBin Open(BinConfiguration config)
        {
            IBin storage = base.Open(config);

            if (config.ReadOnly())
            {
                return(new ReadOnlyBin(new CachingStorage.NonFlushingCachingBin(storage, NewCache
                                                                                    (), _pageCount, _pageSize)));
            }
            return(new CachingBin(storage, NewCache(), _pageCount, _pageSize));
        }
Esempio n. 26
0
 /// <exception cref="Db4objects.Db4o.Ext.Db4oIOException"></exception>
 public CachingBin(IBin bin, ICache4 cache, int pageCount, int pageSize) : base(bin
                                                                                )
 {
     _onDiscardPage    = new _IProcedure4_22(this);
     _producerFromDisk = new _IFunction4_138(this);
     _producerFromPool = new _IFunction4_147(this);
     _pageSize         = pageSize;
     _pagePool         = new SimpleObjectPool(NewPagePool(pageCount));
     _cache            = cache;
     _fileLength       = _bin.Length();
 }
Esempio n. 27
0
            protected override IBin Decorate(BinConfiguration config, IBin bin)
            {
                var decorated = new CloseAwareBin
                                    (this, bin);

                lock (_openBins)
                {
                    _openBins[decorated] = decorated;
                }
                return(decorated);
            }
Esempio n. 28
0
        public virtual void Test()
        {
            PagingMemoryStorage storage = new _PagingMemoryStorage_13();
            IBin testBin = storage.Open(new BinConfiguration(string.Empty, true, 0, false));

            Assert.AreEqual(Data.Length, testBin.Length());
            int actualLength = (int)testBin.Length();

            byte[] read = new byte[actualLength];
            testBin.Read(0, read, actualLength);
            ArrayAssert.AreEqual(Data, read);
        }
Esempio n. 29
0
        private IBin AcquireBin(BinConfiguration config)
        {
            IBin storage = Bin(config.Uri());

            if (null != storage)
            {
                return(storage);
            }
            IBin newStorage = ProduceBin(config, _pageSize);

            _binsByUri[config.Uri()] = newStorage;
            return(newStorage);
        }
Esempio n. 30
0
        public IBin find_bin(IItem item, List <IBin> bins)
        {
            IBin curr_bin = bins[0];

            foreach (var bin in bins)
            {
                if (bin.score_item(item) > curr_bin.score_item(item))
                {
                    curr_bin = bin;
                }
            }
            return(curr_bin);
        }
Esempio n. 31
0
        private IBin ProduceBin(BinConfiguration config)
        {
            IBin storage = Bin(config.Uri());

            if (null != storage)
            {
                return(storage);
            }
            IBin newStorage = new PagingMemoryBin(_pageSize, config.InitialLength());

            _binsByUri[config.Uri()] = newStorage;
            return(newStorage);
        }
Esempio n. 32
0
	protected override IBin Decorate(BinConfiguration config, IBin bin) 
	{
        try 
		{
			var file = new FileStream(_fileName, FileMode.Create);

			var @out = new StreamWriter(file);
			return new LoggingBin(bin, @out, _config);
		} 
        catch(FileNotFoundException e) 
		{
			throw new Db4oIOException(e);
		}
	}
Esempio n. 33
0
    protected override IBin Decorate(BinConfiguration config, IBin bin)
    {
        try
        {
            var file = new FileStream(_fileName, FileMode.Create);

            var @out = new StreamWriter(file);
            return(new LoggingBin(bin, @out, _config));
        }
        catch (FileNotFoundException e)
        {
            throw new Db4oIOException(e);
        }
    }
Esempio n. 34
0
        public virtual void TestInitialLength()
        {
            Storage().Open(new BinConfiguration(TempFile(), false, 1000, false)).Close();
            IBin bin = Storage().Open(new BinConfiguration(TempFile(), false, 0, false));

            try
            {
                Assert.AreEqual(1000, bin.Length());
            }
            finally
            {
                bin.Close();
            }
        }
Esempio n. 35
0
        private IBin ProduceStorage(BinConfiguration config)
        {
            IBin storage = Bin(config.Uri());

            if (null != storage)
            {
                return(storage);
            }
            MemoryBin newStorage = new MemoryBin(new byte[(int)config.InitialLength()], _growthStrategy
                                                 );

            _bins[config.Uri()] = newStorage;
            return(newStorage);
        }
Esempio n. 36
0
 private static void SyncAndClose(IBin bin)
 {
     if (bin != null)
     {
         try
         {
             bin.Sync();
         }
         finally
         {
             bin.Close();
         }
     }
 }
Esempio n. 37
0
			/// <exception cref="Db4objects.Db4o.Ext.Db4oIOException"></exception>
			public NonFlushingCachingBin(IBin bin, ICache4 cache, int pageCount, int pageSize
				) : base(bin, cache, pageCount, pageSize)
			{
			}
Esempio n. 38
0
 public LogReplayer(String logFilePath, IBin bin): this(logFilePath, bin, LogConstants.AllEntries())
 {
 }
		public ThreadedSyncBin(IBin bin) : base(bin)
		{
			_thread = new Thread(new _IRunnable_23(this), "ThreadedSyncBin");
			_thread.Start();
		}
Esempio n. 40
0
 public MockBin(IBin bin, string password) : base(bin)
 {
     _password = password;
 }
		protected override IBin Decorate(BinConfiguration config, IBin bin)
		{
			ResetShutdownState();
			return new ExceptionSimulatingStorage.ExceptionSimulatingBin(bin, _exceptionFactory
				, _triggerCondition);
		}
Esempio n. 42
0
			public virtual void DelegateTo(IBin bin)
			{
				_bin = bin;
			}
Esempio n. 43
0
		protected virtual IBin Decorate(BinConfiguration config, IBin bin)
		{
			return bin;
		}
		private static void SyncAndClose(IBin bin)
		{
			if (bin != null)
			{
				try
				{
					bin.Sync();
				}
				finally
				{
					bin.Close();
				}
			}
		}
Esempio n. 45
0
			public _ICodeBlock_21(IBin adapter)
			{
				this.adapter = adapter;
			}
Esempio n. 46
0
		private void AssertReadOnly(IBin adapter)
		{
			Assert.Expect(typeof(Db4oIOException), new _ICodeBlock_21(adapter));
		}
		protected override IBin Decorate(BinConfiguration config, IBin storage)
		{
			return new NonFlushingStorage.NonFlushingBin(storage);
		}
Esempio n. 48
0
		public BlockAwareBin(IBin bin) : base(bin)
		{
		}
			public NonFlushingBin(IBin storage) : base(storage)
			{
			}
 protected override IBin Decorate(BinConfiguration config, IBin bin)
 {
     var decorated = new CloseAwareBin
         (this, bin);
     lock (_openBins)
     {
         _openBins[decorated] = decorated;
     }
     return decorated;
 }
Esempio n. 51
0
 protected override IBin Decorate(BinConfiguration config, IBin bin)
 {
     return new MonitoredBin(bin);
 }
Esempio n. 52
0
 protected override IBin Decorate(BinConfiguration config, IBin bin)
 {
     return new MockBin(bin, password);
 }
 public CloseAwareBin(CloseAwareStorage storage, IBin
     bin) : base(bin)
 {
     _storage = storage;
 }
Esempio n. 54
0
 public MonitoredBin(IBin bin) : base(bin)
 {
     _bytesWrittenCounter = Db4oPerformanceCounters.CounterFor(PerformanceCounterSpec.BytesWrittenPerSec, false);
     _bytesReadCounter = Db4oPerformanceCounters.CounterFor(PerformanceCounterSpec.BytesReadPerSec, false);
 }
Esempio n. 55
0
			internal SaveAsBin(IBin delegate_)
			{
				_bin = delegate_;
			}
Esempio n. 56
0
 protected override IBin Decorate(BinConfiguration config, IBin bin)
 {
     return new LoggingBin(base.Decorate(config, bin));
 }
Esempio n. 57
0
		public ReadOnlyBin(IBin storage) : base(storage)
		{
		}
Esempio n. 58
0
		public virtual void Replay(IBin bin)
		{
			bin.Read(_pos, PrepareBuffer(), _length);
		}
			internal ExceptionSimulatingBin(IBin bin, IExceptionFactory exceptionFactory, ExceptionSimulatingStorage.ExceptionTriggerCondition
				 triggerCondition) : base(bin)
			{
				_exceptionFactory = exceptionFactory;
				_triggerCondition = triggerCondition;
			}
Esempio n. 60
0
		public virtual void Replay(IBin bin)
		{
			bin.Sync();
		}