hhblaze / DBreeze

C# .NET NOSQL ( key value store embedded ) ACID multi-paradigm database management system.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using multible DBreezeIndexes creates multiple entries in the DB

thecodrr opened this issue · comments

So let's suppose we have a Database of Songs and I use these indexes:

Indexes = new List<DBreezeIndex>
{
                        new DBreezeIndex(1, record.Path) { PrimaryIndex = true }, //PI Primary Index
                       new DBreezeIndex(2, record.FolderPath), //SI - Secondary Index
                       new DBreezeIndex(3, record.LeadArtist),
                       new DBreezeIndex(4, record.Album),
                       new DBreezeIndex(5, record.Title),
}

There are *5 Indexes here and so DBreeze adds them 5 times. So when I go on and retrieve my songs using this:

var records = tran.SelectForward<byte[], byte[]>(tableName);

I get every song x 5 and that messes things up not to mention the RAM and CPU Load.

Is there any way to avoid this without decreasing the number of Indexes?

The other question I have is this: How do I query for songs by their album, title, artist?

Thank you providing such an awesome and efficient database engine!

Update:
So, I just ran a test and it seems the values are being duplicated 5 times with the same key (primary key by the looks of it).
Update 2:
Ran another test and I was wrong. The key wasn't the same but the primary key was being appended to secondary indexes. I turned that off and now the indexes are all separate. Still, this doesn't solve the main problem of multiple entries.

commented

Hi,
the object layer stores all indexes per entity in one table (that's why you got them 5 times), to fetch entity using any of index use SelectForwardFromTo with index number.
https://github.com/hhblaze/DBreeze/wiki/Quick-start-guide.-Customers-and-orders.-Object-DBreeze

How would I go about using SelectForwardFromTo with Strings? The examples use DateTime which have a range but a piece of string has no such thing. I would love it if you could give a short example. Thanks for the quick response.

commented

Good, give me some time

commented
using DBreeze;
using DBreeze.Utils;

DBreezeEngine engine = null;

 if (engine == null)
                engine = new DBreezeEngine(@"D:\Temp\x1");

//Setting up NetJSON serializer (from NuGet) to be used by DBreeze
DBreeze.Utils.CustomSerializator.ByteArraySerializator = (object o) => { return NetJSON.NetJSON.Serialize(o).To_UTF8Bytes(); };
DBreeze.Utils.CustomSerializator.ByteArrayDeSerializator = (byte[] bt, Type t) => { return NetJSON.NetJSON.Deserialize(t, bt.UTF8_GetString()); };

public class Song
        {
            public long Id { get; set; } 
            public string Path { get; set; } = "";
            public string ArtistName { get; set; } = "";
            public long ArtistId { get; set; }
            public DateTime SongReleaseDate { get; set; }
            public string Titel { get; set; }
            public string Album { get; set; }

            /// <summary>
            /// Helper, forming the view of the song to be searched via text-search engine
            /// </summary>
            /// <returns></returns>
            public string ContainsText()
            {
                return ArtistName + " " + Titel + " "+ Album;
            }

        }

//inserting 
            using (var t = engine.GetTransaction())
            {
                Song s = new Song
                {
                    Id = t.ObjectGetNewIdentity<long>("Songs"),
                    ArtistId = 1,
                    ArtistName = "The Beatles",
                    Album = "Revolver",
                    Path = @"C:\Songs\786788779.mp3",
                    SongReleaseDate = new DateTime(1966, 9, 5),
                    Titel = "Eleanor Rigby"
                };

                t.ObjectInsert<Song>("Songs", new DBreeze.Objects.DBreezeObject<Song>
                {
                    NewEntity = true,
                    Entity = s,
                    //Using standard indexes for range queries
                    Indexes = new List<DBreeze.Objects.DBreezeIndex>
                        {
                            new DBreeze.Objects.DBreezeIndex(1,s.Id) { PrimaryIndex = true },
                            new DBreeze.Objects.DBreezeIndex(2,s.SongReleaseDate),
                            new DBreeze.Objects.DBreezeIndex(3,s.ArtistId, s.SongReleaseDate)
                        }
                });

                //Using text-search engine for the free text search
                t.TextInsert("SongsText", s.Id.To_8_bytes_array_BigEndian(), s.ContainsText());

                s = new Song
                {
                    Id = t.ObjectGetNewIdentity<long>("Songs"),
                    ArtistId = 1,
                    ArtistName = "The Beatles",
                    Album = "Revolver",
                    Path = @"C:\Songs\786788780.mp3",
                    SongReleaseDate = new DateTime(1966, 9, 5),
                    Titel = "Yellow Submarine"
                };

                t.ObjectInsert<Song>("Songs", new DBreeze.Objects.DBreezeObject<Song>
                {
                    NewEntity = true,
                    Entity = s,
                    Indexes = new List<DBreeze.Objects.DBreezeIndex>
                        {
                            new DBreeze.Objects.DBreezeIndex(1,s.Id) { PrimaryIndex = true },
                            new DBreeze.Objects.DBreezeIndex(2,s.SongReleaseDate),
                            new DBreeze.Objects.DBreezeIndex(3,s.ArtistId,s.SongReleaseDate)
                        }
                });

                t.TextInsert("SongsText", s.Id.To_8_bytes_array_BigEndian(), s.ContainsText());

                s = new Song
                {
                    Id = t.ObjectGetNewIdentity<long>("Songs"),
                    ArtistId = 2,
                    ArtistName = "Queen",
                    Album = "Jazz",
                    Path = @"C:\Songs\786788781.mp3",
                    SongReleaseDate = new DateTime(1978, 11, 10),
                    Titel = "Bicycle Race"
                };

                t.ObjectInsert<Song>("Songs", new DBreeze.Objects.DBreezeObject<Song>
                {
                    NewEntity = true,
                    Entity = s,
                    Indexes = new List<DBreeze.Objects.DBreezeIndex>
                        {
                            new DBreeze.Objects.DBreezeIndex(1,s.Id) { PrimaryIndex = true },
                            new DBreeze.Objects.DBreezeIndex(2,s.SongReleaseDate),
                            new DBreeze.Objects.DBreezeIndex(3,s.ArtistId,s.SongReleaseDate)
                        }
                });

                t.TextInsert("SongsText", s.Id.To_8_bytes_array_BigEndian(), s.ContainsText());

                s = new Song
                {
                    Id = t.ObjectGetNewIdentity<long>("Songs"),
                    ArtistId = 2,
                    ArtistName = "Queen",
                    Album = "The Miracle",
                    Path = @"C:\Songs\786788782.mp3",
                    SongReleaseDate = new DateTime(1989, 05, 22),
                    Titel = "I want it all"
                };

                t.ObjectInsert<Song>("Songs", new DBreeze.Objects.DBreezeObject<Song>
                {
                    NewEntity = true,
                    Entity = s,
                    Indexes = new List<DBreeze.Objects.DBreezeIndex>
                        {
                            new DBreeze.Objects.DBreezeIndex(1,s.Id) { PrimaryIndex = true },
                            new DBreeze.Objects.DBreezeIndex(2,s.SongReleaseDate),
                            new DBreeze.Objects.DBreezeIndex(3,s.ArtistId,s.SongReleaseDate)
                        }
                });

                t.TextInsert("SongsText", s.Id.To_8_bytes_array_BigEndian(), s.ContainsText());

                t.Commit();
            }


            //Fetching

            using (var t = engine.GetTransaction())
            {
                //Show all titles
                foreach (var el in t.SelectForwardFromTo<byte[], byte[]>("Songs", 1.ToIndex(long.MinValue), true, 1.ToIndex(long.MaxValue), true))
                {
                    //Console.WriteLine(el.ObjectGet<Song>().Entity.Titel);
                }
                /*
Eleanor Rigby
Yellow Submarine
Bicycle Race   
I want it all 
             */

                //Show titles, released up to year 1975
                foreach (var el in t.SelectForwardFromTo<byte[], byte[]>("Songs", 2.ToIndex(DateTime.MinValue), true, 2.ToIndex(new DateTime(1975,1,1)), true))
                {
                    //Console.WriteLine(el.ObjectGet<Song>().Entity.Titel);
                }
                /*
                 Eleanor Rigby
                Yellow Submarine
                             */              

                //Show all Queen titles (Queen id is 2)
                foreach (var el in t.SelectForwardFromTo<byte[], byte[]>("Songs", 3.ToIndex((long)2,DateTime.MinValue), true, 3.ToIndex((long)2,DateTime.MaxValue), true))
                {
                    //Console.WriteLine(el.ObjectGet<Song>().Entity.Titel);
                }
                /*
                  Bicycle Race
                  I want it all
                 */

                //Show all Queen titles up to year 1983 (Queen id is 2)
                foreach (var el in t.SelectForwardFromTo<byte[], byte[]>("Songs", 3.ToIndex((long)2, DateTime.MinValue), true, 3.ToIndex((long)2, new DateTime(1983, 1, 1)), true))
                {
                    Console.WriteLine(el.ObjectGet<Song>().Entity.Titel);
                }
                /*
                  Bicycle Race                  
                 */


                //------------  search using text-search engine

                foreach (var el in t.TextSearch("SongsText").Block("jazz").GetDocumentIDs())
                {
                    var o = t.Select<byte[], byte[]>("Songs", 1.ToIndex(el)).ObjectGet<Song>();
                    if (o != null)
                        Console.WriteLine(o.Entity.Titel);
                }
                /*
                 Bicycle Race
                 */

                foreach (var el in t.TextSearch("SongsText").Block("queen rac").GetDocumentIDs())
                {
                    var o = t.Select<byte[], byte[]>("Songs", 1.ToIndex(el)).ObjectGet<Song>();
                    if (o != null)
                        Console.WriteLine(o.Entity.Titel);
                }
                /*
                 I want it all - comes inside because belongs to album The Miracle
                 Bicycle Race
                 */



            }
commented

made some code clean up

@hhblaze Thanks a lot! This works perfectly!