Skip to content

Quick start guide. Songs library

Misha Tarnorutsky edited this page Nov 12, 2018 · 2 revisions
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 Title { 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 + " " + Title + " "+ 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),
                    Title = "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),
                    Title = "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),
                    Title = "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),
                    Title = "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.Title);
                }
                /*
                  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.Title);
                }
                /*
                 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.Title);
                }
                /*
                  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.Title);
                }
                /*
                  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.Title);
                }
                /*
                 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.Title);
                }
                /*
                 I want it all - comes inside because belongs to album The Miracle
                 Bicycle Race
                 */



            }
Clone this wiki locally