mbdavid / LiteDB

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

Find causing "Stream does not support writing" exception #690

Closed ghost closed 4 years ago

ghost commented 7 years ago

Hi,

Using the following code causes a "Stream does not support writing" exception:

public IEnumerable getCaseByCaseGUID(Guid CaseGUID) { using (var db = new LiteDatabase(NoSQLDatabaseReadOnly)) { var Case = db.GetCollection(NoSQLCollectionName);

            var results = Case.Find(x => x.CaseGUID == CaseGUID);

            return results;
        }
    }

My connection string (NoSQLDatabaseReadOnly) has Mode=ReadOnly in it.

Trying to figure out why a Find call is attempting to write to the database? Is this by design? What are the workarounds?

Thanks.

mbdavid commented 7 years ago

Hi @testbench, if your collection doesnt contains an index in CaseGUID column, LiteDB try to auto-create this index and if you are readonly mode you get this error.

Try open shared/exclusive mode and create this index.

In next v3.5 version there is no more auto index and in this case, LiteDB will do a full scan search.

ghost commented 7 years ago

Hi,

thanks for the quick response. I have two issues with this, being:

1 - When set to exclusive mode I get errors with the file being locked by another process 2 - When attempting to set Mode=Shared I get the error "'Invalid connection string value type for [mode]'"

My packages.config contains: package id="LiteDB" version="3.1.2" targetFramework="net452"

mbdavid commented 7 years ago

Hi,

1- Exclusive mode can be open only one-per-time. Dont forget to always use using statement to compiler call Dispose() method 2- There is no shared option in NetStandard version. You must use net35 version to enable this option.

ghost commented 7 years ago

Whats the process for using the net35 version?

ghost commented 7 years ago

do you simply mean having my project target .Net 3.5?

mbdavid commented 7 years ago

I was reviewing nuget and net451 are the same net35. Downgrade you problem to net451 and get from nuget

ghost commented 7 years ago

Removed LiteDB, downgraded to 4.5.1 and used nuget to install litedb 3.1.2, same error "'Invalid connection string value type for [mode]'"

ghost commented 7 years ago

Any ideas?

ghost commented 7 years ago

Hi @mbdavid

Retargetting to 4.5.1 and reinstalling litedb did not make any difference. Same error. Any other suggestions?

Thanks

mbdavid commented 7 years ago

Ok, now comparing new .csproj in master branch with dev branch they are different. You must set your project to .NET3.5 to download correct version. I'm fixing this for new 3.5 version

ghost commented 7 years ago

OK will try this thanks

ghost commented 7 years ago

@mbdavid I have it running fine with v3.1.1 - when do you expect the new v3.5 to be available?

mbdavid commented 7 years ago

HI @testbench, I will delivery beta version next week. An them, I will wait community helps me testing and finding bugs (especially about concurrency access).

ghost commented 7 years ago

Thanks @mbdavid - well done on the project too, it's great!

a-a-k commented 6 years ago

I'm new in LiteDB. So guys, what is the solution? I have db as embedded resource and here is the usage:

        public static void AddUser(long id, string name)
        {
            using (var memory = typeof(DbActions).GetTypeInfo().Assembly.GetManifestResourceStream("HadithBot.UmmahDb.db"))
            {
                using (var db = new LiteDatabase(memory))
                {
                    var table = db.GetCollection<User>("Users");
                    if (!table.Exists(u => u.ChatId == id))
                    {
                        var user = new User()
                        {
                            Name = name,
                            ChatId = id,
                            IsAdmin = false
                        };

                        table.Insert(user);
                        table.EnsureIndex("ChatId", true);
                    }
                }
            }
        }

There I get this error. What is wrong? I'm work with netcore.

mbdavid commented 6 years ago

Hi @a-a-k, it's not possible modify embedded assembly resource. Use an external file or in-memory stream

a-a-k commented 6 years ago

@mbdavid thanks for your response. Could you provide any example of usage "in-memory stream"?

mbdavid commented 6 years ago

You can use MemoryStrem class as your database stream.

var mem = new MemoryStrem();

using(var db = new LiteDatabase(mem))
{
    // ...
}

var databaseInBytes = mem.ToArray();
a-a-k commented 6 years ago

@mbdavid I've saw that at Quickstart page but this usage unclear for me. What is the profit of such way? Will it only runtime data? Please, explain this way or give me link to explanation )

mbdavid commented 6 years ago

Memory databases are useful for unit test or some in-memory processing, like create a temporary database for sort data using an index. In this thread https://github.com/mbdavid/LiteDB/issues/805 I made some examples

lbnascimento commented 4 years ago

Hi! With the objective of organizing our issues, we are closing old unsolved issues. Please check the latest version of LiteDB and open a new issue if your problem/question/suggestion still applies. Thanks!