Exemple #1
0
        public int AddIndex(long position, Guid MetaHash = default(Guid))
        {
            lock (_locker)
            {
                long currentposition = this.IndexStream.Length;
                // check to make sure it is the right position.
                var left = currentposition % 24;
                if (left != 0)
                {
                    var count = (int)currentposition / 24;
                    if (count * 8 > currentposition)
                    {
                        currentposition = count * 24;
                    }
                    else
                    {
                        currentposition = (count + 1) * 24;
                    }
                    this.IndexStream.SetLength(currentposition);
                }

                this.IndexStream.Position = currentposition;
                DocInfo info = new DocInfo()
                {
                    Position = position, MetaHash = MetaHash
                };

                this.IndexStream.Write(info.ToBytes(), 0, 24);
                int id = (int)(currentposition / 24);

                this.Docs[id] = info;
                return(id);
            }
        }
Exemple #2
0
        public void LoadIndex(string fileName = null)
        {
            if (fileName == null)
            {
                fileName = this.IndexFile;
            }

            if (File.Exists(fileName))
            {
                var allBytes = Lib.Helper.IOHelper.ReadAllBytes(fileName);
                int len      = allBytes.Length;

                int maxindex = (int)(len / 24);
                if (maxindex * 24 > len)
                {
                    maxindex = maxindex - 1;
                }
                for (int i = 0; i < maxindex; i++)
                {
                    DocInfo info = new DocInfo();

                    byte[] infobytes = new byte[24];
                    Buffer.BlockCopy(allBytes, i * 24, infobytes, 0, 24);
                    info.FromBytes(infobytes);
                    if (info.Position > 0)
                    {
                        this.Docs[i] = info;
                    }
                }
            }
        }
Exemple #3
0
        public void UpdateIndex(int id, long newposition, string meta)
        {
            var  position = id * 24;
            Guid metahash = Lib.Security.Hash.ComputeGuidIgnoreCase(meta);
            var  total    = this.IndexStream.Length;

            if (total >= position + 24)
            {
                lock (_locker)
                {
                    if (this.Docs.ContainsKey(id))
                    {
                        this.IndexStream.Position = position;
                        DocInfo newinfo = new DocInfo()
                        {
                            Position = newposition, MetaHash = metahash
                        };
                        this.IndexStream.Write(newinfo.ToBytes(), 0, 24);
                        this.Docs[id] = newinfo;
                    }
                }
            }
        }