Exemplo n.º 1
0
        private ImmutableObject ReadObject(long beginning, long end)
        {
            ImmutableObject obj = null;

            lock (_chainStreamLock)
            {
                _chainStream.Position = beginning;
                byte[] data = new byte[end - beginning];
                _chainStream.Read(data, 0, data.Length);
                obj = ZeroFormatterSerializer.Deserialize <ImmutableObject>(data);
            }
            return(obj);
        }
Exemplo n.º 2
0
        public int Insert(ImmutableObject obj)
        {
            ImmutableObject previousObject = LastObject();

            // If the object is new then we need to set the id to the next unique id available
            if (obj.Version == -1)
            {
                obj.Id = _latestVersionIndex.Last() + 1;
            }
            obj.InsertOp(previousObject.Index + 1, previousObject.Hash);

            WriteObject(obj);

            return(obj.Id);
        }
Exemplo n.º 3
0
        private void WriteObject(ImmutableObject obj)
        {
            byte[] data = ZeroFormatterSerializer.Serialize(obj);
            lock (_chainStreamLock)
            {
                long pos = _chainStream.Length;
                _chainStream.Position = pos;

                _index.SetAtIndex(obj.Index, pos);
                _latestVersionIndex.Add(obj.Id, obj.Index);

                _chainStream.Write(data, 0, data.Length);
                _chainStream.Flush();
            }
        }
Exemplo n.º 4
0
        public Chain(string dbName)
        {
            _index = new Index <long>(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), $"{dbName}.index"));
            _latestVersionIndex = new LinkedIndex <int>(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), $"{dbName}.latestVersion");

            _chainStream = new FileStream(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), $"{dbName}.data"), FileMode.OpenOrCreate, FileAccess.ReadWrite);

            if (!Any)
            {
                var genesis = new ImmutableObject()
                {
                    Json = string.Empty
                };
                genesis.InsertOp(0, string.Empty);
                WriteObject(genesis);
            }
        }
Exemplo n.º 5
0
        public bool Validate()
        {
            bool   valid        = true;
            string previousHash = null;
            int    count        = _index.Count();

            for (int i = 0; i < count; i++)
            {
                ImmutableObject obj = ReadObject(i);
                if (previousHash != null && obj.PreviousHash != previousHash)
                {
                    valid = false;
                    break;
                }
                previousHash = obj.CalculateHash();
            }
            return(valid);
        }