Ejemplo n.º 1
0
        public new void Append(List <IKey> listOfKeys, IValue value)
        {
            StrValue strDataFilePathValue = null;

            byte[] hash      = null;
            long   timestamp = StreamFactory.NowUtc();

            foreach (IKey key in listOfKeys)
            {
                if (logger != null)
                {
                    logger.Log("Start ValueDataStream Append");
                }

                if (strDataFilePathValue == null)
                {
                    Tuple <byte[], StrValue> temp = UpdateHelper(key, value, true, timestamp);
                    hash = temp.Item1;
                    strDataFilePathValue = temp.Item2;
                }
                else
                {
                    base.UpdateHelper(key, strDataFilePathValue, true, hash, timestamp);
                }
                if (logger != null)
                {
                    logger.Log("End ValueDataStream Append");
                }
            }
        }
Ejemplo n.º 2
0
        protected Tuple <Byte[], StrValue> UpdateHelper(IKey key, IValue value, bool IsAppend, long timestamp = -1)
        {
            if (!value.GetType().Equals(typeof(ByteValue)))
            {
                throw new InvalidDataException("Invalid IValue Type.  ByteValue expected.");
            }

            if (logger != null)
            {
                logger.Log("Start FileDataStream Delete Old DataBlock");
            }
            // check if the entry is present, so that the old file can be deleted
            IValue valueDataFilePathOld = base.Get(key);

            // remove old entry/file if present and update has been called
            if (valueDataFilePathOld != null && !IsAppend)
            {
                System.IO.FileInfo fi = new System.IO.FileInfo(valueDataFilePathOld.ToString());
                try
                {
                    fi.Delete();
                }
                catch (System.IO.IOException e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            if (logger != null)
            {
                logger.Log("End FileDataStream Delete Old DataBlock");
            }



            if (logger != null)
            {
                logger.Log("Start FileDataStream Construct DataBlock");
            }
            long ts;  // timestamp

            ts = StreamFactory.HighResTick();
            string   dataFilePath         = targetDir + "/" + Convert.ToString(ts) + ".dat";
            StrValue strDataFilePathValue = new StrValue(Convert.ToString(ts) + ".dat");

            if (streamtype == StreamFactory.StreamSecurityType.Secure)
            {
                value = new ByteValue(Crypto.EncryptBytesSimple(value.GetBytes(), Crypto.KeyDer(acl_md.encKey), acl_md.IV));
            }
            else
            {
                value = new ByteValue(value.GetBytes());
            }


            // if (logger != null) logger.Log("Start FileDataStream Construct DBI");
            Byte[] hash;
            if (streamtype == StreamFactory.StreamSecurityType.Secure)
            {
                hash = hasher.ComputeHash(value.GetBytes());
            }
            else
            {
                hash = null;
            }
            // if (logger != null) logger.Log("End FileDataStream Construct DBI");
            if (logger != null)
            {
                logger.Log("End FileDataStream Construct DataBlock");
            }

            if (logger != null)
            {
                logger.Log("Start FileDataStream Update FilePathValue");
            }
            base.UpdateHelper(key, strDataFilePathValue, IsAppend, hash, timestamp);
            if (logger != null)
            {
                logger.Log("End FileDataStream Update FilePathValue");
            }

            // write <val> to file
            if (logger != null)
            {
                logger.Log("Start FileDataStream WriteToDisc DataBlock");
            }
            FileStream fout = new FileStream(dataFilePath,
                                             FileMode.OpenOrCreate,
                                             FileAccess.Write,
                                             FileShare.ReadWrite);

            fout.Write(value.GetBytes(), 0, (int)value.Size());
            fout.Flush(true);
            fout.Close();
            if (logger != null)
            {
                logger.Log("End FileDataStream WriteToDisc DataBlock");
            }

            return(new Tuple <byte[], StrValue>(hash, strDataFilePathValue));
        }