mbdavid / LiteDB

LiteDB - A .NET NoSQL Document Store in a single data file
http://www.litedb.org
MIT License
8.62k stars 1.25k forks source link

InsertBulk(IEnumerable<E<T>>) => LiteDB.LiteException: Invalid collection name 'CacheDataItem`1': Use only [a-Z$_]. #2131

Closed sigmarsson closed 2 years ago

sigmarsson commented 2 years ago

Version 5.0.11

Describe the bug This is throwing an exception:

public void Persist<T>(IEnumerable<CacheDataItem<T>> set) where T : class
        {
            var existingSet = _ctx.GetCollection<CacheDataItem<T>>();

            var records = existingSet.InsertBulk(set);

            if (records > 0)
            {
                Log4.Debug($"{records} entities inserted out of {set.Count()}");
            }
        }

Screenshots/Stacktrace

  Message: 
    TestCleanup method Test.gesdisc.eosdis.nasa.gov.DownloadTest.DisposeClient threw exception. LiteDB.LiteException: LiteDB.LiteException: Invalid collection name 'CacheDataItem`1': Use only [a-Z$_].

  Stack Trace: 
    CollectionService.CheckName(String name, HeaderPage header)
    CollectionService.Add(String name, CollectionPage& collectionPage)
    CollectionService.Get(String name, Boolean addIfNotExists, CollectionPage& collectionPage)
    Snapshot.ctor(LockMode mode, String collectionName, HeaderPage header, UInt32 transactionID, TransactionPages transPages, LockService locker, WalIndexService walIndex, DiskReader reader, Boolean addIfNotExists)
    TransactionService.<CreateSnapshot>g__create|43_0(<>c__DisplayClass43_0& )
    TransactionService.CreateSnapshot(LockMode mode, String collection, Boolean addIfNotExists)
    <>c__DisplayClass7_0.<Insert>b__0(TransactionService transaction)
    LiteEngine.AutoTransaction[T](Func`2 fn)
    LiteEngine.Insert(String collection, IEnumerable`1 docs, BsonAutoId autoId)
    SharedEngine.Insert(String collection, IEnumerable`1 docs, BsonAutoId autoId)
    LiteCollection`1.InsertBulk(IEnumerable`1 entities, Int32 batchSize)
    WeatherDataCtx.Persist[T](IEnumerable`1 set) line 28
    WeatherDbCache.Persist[T](IDictionary`2 set) line 35
    WeatherDataCache`1.Dispose(Boolean disposing) line 148
    EarthDataClient.Dispose(Boolean disposing) line 142
    WeatherDataCache`1.Dispose() line 142
    DownloadTest.DisposeClient() line 50
bulpet commented 2 years ago

Hi, I'm not similar yet with this great db, but try to change you signature from IEnumerable<CacheDataItem> to List<CacheDataItem>

sigmarsson commented 2 years ago

Same error. I reckon the problem is that generic types are rendered to indicate the number of generic parameters in the hosting type name. => `1 LiteDB doesn't allow that regex. They have an IsWord() method somewhere to check this but I don't know their API.

kcsombrio commented 2 years ago

Hi @sigmarsson, try to create a named collection, like:

var existingSet = _ctx.GetCollection<CacheDataItem<T>>("CacheDataItem");

When you don't specify a collection name, it automatically uses typeof(T).Name, which contains the invalid char ` and causes this exception. Hope this helps, please reopen this issue if it didn't solve your problem.

sigmarsson commented 2 years ago

@kcsombrio Thank you!

I ve just made a test with one particular generic type. Can I use the same string literal for every generic type?

kcsombrio commented 2 years ago

Hi @sigmarsson. Yes, technically you can use the same collection name for every collection you use, that's no restriction for that, but we recommend using one name per collection