Beispiel #1
0
        private void GenerateAllIndiceHashtags()
        {
            Console.Clear();

            try
            {
                Console.WriteLine("Aguarde. Gerando índice.");

                List <FileStream> listaStream = new List <FileStream>();
                listaStream.Add(new FileStream(strNameFile, FileMode.Open));
                listaStream.Add(new FileStream(strNameIndiceHashtags, FileMode.Open));

                listaStream[0].Seek(0, SeekOrigin.Begin);
                while (listaStream[0].Position < listaStream[0].Length)
                {
                    if (listaStream[0].Position > 0)
                    {
                        listaStream[0].Position += 1;
                    }

                    BinarySearchAlgorithm bsa = new BinarySearchAlgorithm();
                    StrFile  oReturn          = bsa.GetFileValue <StrFile>(listaStream[0]);
                    string[] hashtagsarray    = oReturn.HashTags.Split("#");
                    foreach (string hashtag in hashtagsarray)
                    {
                        if (hashtag.Trim() != "" && (hashtag.ToLower().Contains("rock") || hashtag.ToLower().Contains("metal") || hashtag.ToLower().Contains("punk") || hashtag.ToLower().Contains("hard")))
                        {
                            FileWrite fw         = new FileWrite();
                            bool      achou      = false;
                            string    hashtagAux = "#" + hashtag.Trim();
                            listaStream[1].Seek(0, SeekOrigin.Begin);
                            while (listaStream[1].Position < listaStream[1].Length)
                            {
                                if (listaStream[1].Position > 0)
                                {
                                    listaStream[1].Position += 1;
                                }

                                StrFileIndiceHashtags oReturnInd = bsa.GetFileValue <StrFileIndiceHashtags>(listaStream[1]);
                                if (oReturnInd.Hashtag.Trim() == hashtagAux.Trim())
                                {
                                    achou = true;
                                    for (int i = 1; i < 1000; i++)
                                    {
                                        if (oReturnInd.Ids[i] == 0)
                                        {
                                            oReturnInd.Ids[i] = oReturn.Id;
                                            break;
                                        }
                                    }
                                    long tamanhoBytes = Marshal.SizeOf(typeof(StrFileIndiceHashtags));
                                    if (listaStream[1].Position > 0)
                                    {
                                        listaStream[1].Position -= tamanhoBytes;
                                    }
                                    byte[] buf = fw.GetBytes(oReturnInd);
                                    listaStream[1].Write(buf);
                                }
                            }

                            if (!achou)
                            {
                                StrFileIndiceHashtags sfih = new StrFileIndiceHashtags();
                                sfih.Hashtag = hashtagAux;
                                sfih.Ids     = new long[1000];
                                sfih.Ids[0]  = oReturn.Id;
                                sfih.NewLine = '\n';
                                long position = 0;
                                if (listaStream[1].Length > 0)
                                {
                                    position = listaStream[1].Length + 1;
                                    listaStream[1].Position = position;
                                }
                                byte[] buf = fw.GetBytes(sfih);
                                listaStream[1].Write(buf);
                            }
                        }
                    }
                }

                listaStream[0].Close();
                listaStream[1].Close();
                listaStream[0].Dispose();
                listaStream[1].Dispose();

                Console.WriteLine("Pressione uma tecla para continuar.");
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }