LiteDB icon indicating copy to clipboard operation
LiteDB copied to clipboard

[BUG] Index was outside the bounds of the array.

Open sgf opened this issue 4 years ago • 0 comments

Version LiteDB 5.0.11ver .net 6.0 Win10 X64

Describe the bug

concurrent read and write to the database. there's read error on Read Task im post the test code,Before the probelm run end,this BUG Almost throw every time .

Code to Reproduce

Code to Reproduce

//Exception Point
  try {
                   sw.Restart();
                   var rlt = sets.FindById(key);
               } catch (Exception ex) {
                   Console.WriteLine(ex.Message);
               }
//Full Code
class Program {
        static void Main(string[] args) {
            try {
                LiteDBTest.TestFileNameLengthEffects0();
            } catch (Exception ex) {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }
}

    public class LiteDBTest {
        static LiteDBTest() {
            try {
                db = new LiteDatabase("Filename=TestDB.ldb;");//初始化数据库connection=shared
                BsonMapper.Global.IncludeFields = true;
            } catch (Exception ex) {
                Console.WriteLine(ex.Message);
            }
        }

        public static LiteDatabase db;


        public static List<CTS1> DataGen(ref int current, int count) {
            List<CTS1> list = new(count);
            for (int i = 0; i < count; i++) {
                var val = (current + i);
                var key = $"0d3deed1234567890abcd{(100000000 - (current + i)):D12}";
                list.Add(new(key, val));
            }
            current = current + count;
            return list;
        }

        static Random r = new Random();

        public static async void SelectTest() {
            while (!IsExit()) {
                await Task.Delay(r.Next(1, 10));//随机触发查询时间
                var sw = Stopwatch.StartNew();
                var sets = db.GetCollection<CTS1>();
                var rs = r.Next(0, 10000000);
                var key = $"0d3deed1234567890abcd{(100000000 - rs):D12}";
                var rlt = sets.FindById(key);
                if (sw.Elapsed.Seconds > 1)
                    Console.WriteLine($"查询时间大于1秒-{sw.ElapsedMilliseconds}ms");
                Interlocked.Increment(ref QueryCount);
            }
        }


        public static bool IsExit() {
            return Interlocked.Read(ref exit) == 1;
        }

        static long exit = 0;

        public static long QueryCount;

        public static void TestFileNameLengthEffects0() {
            //using var db1 = new LiteDatabase("Filename=TestDB0.ldb;");
            var sw = Stopwatch.StartNew();
            var swOutput = Stopwatch.StartNew();
            for (int t = 0; t < 50; t++)
                Task.Factory.StartNew(SelectTest);

            var set = db.GetCollection<CTS1>();
            int totalIdx = 26860000;
            for (int i = 0;i < 10000; i++) {
                var list = DataGen(ref totalIdx, 20000); //每2w一个批次
                try {
                    var rlt = set.InsertBulk(list);
                } catch (Exception ex) {
                    Console.WriteLine(ex.Message);
                }
                if (swOutput.Elapsed.TotalSeconds > 8) {
                    Console.WriteLine($" 当前数量:{totalIdx} 平均插入速度:{(totalIdx / sw.Elapsed.TotalSeconds)}op/s , 查询次数{(QueryCount / sw.Elapsed.TotalSeconds)}/s");
                    swOutput.Restart();
                }
            }
            exit = 1;
        }

    /// <summary>
    /// CodeTaskSmall
    /// </summary>
    public class CTS1 {
        public CTS1(string code, int taskId) {
            Code = code;
            TaskId = taskId;
        }
        [LiteDB.BsonId]
        public string Code;
        [LiteDB.BsonField("t")]
        public int TaskId;
    }

Expected behavior A clear and concise description of what you expected to happen.

Screenshots/Stacktrace

{"Index was outside the bounds of the array."}
在 LiteDB.Engine.IndexService.Find(CollectionIndex index, BsonValue value, Boolean sibling, Int32 order)
   在 LiteDB.Engine.IndexEquals.<Execute>d__3.MoveNext()
   在 LiteDB.LinqExtensions.<>c__DisplayClass2_0`2.<<DistinctBy>g___|0>d.MoveNext()
   在 LiteDB.Engine.BasePipe.<LoadDocument>d__6.MoveNext()
   在 LiteDB.Engine.QueryPipe.<Select>d__2.MoveNext()
   在 LiteDB.Engine.QueryExecutor.<>c__DisplayClass10_0.<<ExecuteQuery>g__RunQuery|0>d.MoveNext()
   在 LiteDB.Engine.QueryExecutor.ExecuteQuery(Boolean executionPlan)
   在 LiteDB.Engine.LiteEngine.Query(String collection, Query query)
   在 LiteDB.LiteQueryable`1.<ToDocuments>d__26.MoveNext()
   在 System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext()
   在 System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Boolean& found)
   在 System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
   在 ConsoleAppTest.LiteDBTest.<SelectTest>d__6.MoveNext() 在 D:\\MySrc\\DLBSvr\\ConsoleAppTest\\LiteDBTest.cs 中: 第 113 行

Additional context Add any other context about the problem here.

sgf avatar Dec 17 '21 06:12 sgf