protected void UpdateHelper(IKey key, IValue value, bool IsAppend) { if (!key.GetType().Equals(typeof(KeyType))) { throw new InvalidDataException("Invalid IKey Type"); } if (!value.GetType().Equals(typeof(ValType))) { throw new InvalidDataException("Invalid IValue Type"); } /* * string keyHash = StreamFactory.GetString( * sha1.ComputeHash(StreamFactory.GetBytes(key))); */ List <TS_Offset> offsets; // check if the entry is present (and record offset) if (index.ContainsKey(key)) { // TODO: this could just have been a simple collision // -- make sure that this is the same key // -- if different, use a secondary hash function offsets = index[key]; } else { offsets = new List <TS_Offset>(); index[key] = offsets; } // get file offset; add <key_hash, offset> to index fs_bw.BaseStream.Seek(0, SeekOrigin.End); long offset = fs_bw.BaseStream.Position; // write <op (1B), ts, key, val_len, val> byte op; if (IsAppend) { op = (byte)WriteOp.AppendOp; } else { op = (byte)WriteOp.UpdateOp; } long ts = StreamFactory.NowUtc(); DataBlock <KeyType, ValType> db = new DataBlock <KeyType, ValType>(); db.op = op; db.timestamp = ts; db.key = key; db.value = value; fs_bw.Write(db.SerializeToJsonStream()); fs_bw.Flush(); latest_tso = new TS_Offset(ts, offset); // apply to index if (IsAppend) { offsets.Add(latest_tso); } else { if (offsets.Count == 0) { offsets.Add(latest_tso); } else { offsets[offsets.Count - 1] = latest_tso; } } /* ts_index.Add(new TS_Offset(ts, offset)); */ }
public OldDataFileStream(FqStreamID FQSID, StreamFactory.StreamOp Op, CallerInfo Ci, ISync sync) { if (!typeof(IKey).IsAssignableFrom(typeof(KeyType))) { throw new InvalidDataException("KeyType must implement IKey"); } if (!typeof(IValue).IsAssignableFrom(typeof(ValType))) { throw new InvalidDataException("ValType must implement IValue"); } callerId = Ci.friendlyName; callerSecret = Ci.secret; synchronizer = sync; isClosed = false; disposed = false; sha1 = new SHA1CryptoServiceProvider(); index = new Dictionary <IKey, List <TS_Offset> >(); /* ts_index = new List<TS_Offset>(); */ latest_tso = new TS_Offset(0, 0); // Get the current directory. string BaseDir = Path.GetFullPath((null != Ci.workingDir) ? Ci.workingDir : Directory.GetCurrentDirectory()); targetDir = BaseDir + "/" + FQSID.ToString(); if (!Directory.Exists(targetDir)) { Directory.CreateDirectory(targetDir); } if (synchronizer != null) { synchronizer.SetLocalSource(targetDir); } md = new OldMetaData(targetDir, ".md"); // Check if stream has to be CREATED if (!md.load) { if (FQSID.AppId == callerId) { md.setOwner(FQSID.AppId); md.SetReadAccess(FQSID.AppId); md.SetWriteAccess(FQSID.AppId); md.FlushMetaData(); Console.WriteLine("Created stream " + targetDir + " for " + callerId); } else { throw new InvalidOperationException(callerId + " not permitted to create stream for " + FQSID.AppId); } } // Open stream for read or write if (Op == StreamFactory.StreamOp.Read) { if (!OpenForRead()) { throw new InvalidDataException("Couldn't open stream for reading"); } } else { if (!OpenForWrite()) { throw new InvalidDataException("Couldn't open stream for writing"); } } // Build index try { // permission checks succeeded // load index from file if present // TODO: if not and stream.dat is present: recreate index from stream.dat string IndexFQN = targetDir + "/index.dat"; if (File.Exists(IndexFQN)) { FileStream iout = new FileStream(IndexFQN, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); BinaryReader index_br = new BinaryReader(iout); try { while (true) { string key = index_br.ReadString(); IKey ikey = SerializerHelper <KeyType> .DeserializeFromJsonStream(key) as IKey; int num_offsets = index_br.ReadInt32(); List <TS_Offset> offsets = new List <TS_Offset>(num_offsets); for (int i = 0; i < num_offsets; i++) { long ts = index_br.ReadInt64(); long offset = index_br.ReadInt64(); TS_Offset tso = new TS_Offset(ts, offset); offsets.Add(tso); if (ts > latest_tso.ts) { latest_tso = tso; } } index[ikey] = offsets; } ; } catch (EndOfStreamException) { // done } finally { index_br.Close(); } } /* * // load ts_index * string TsIndexFQN = targetDir + "/ts_index.dat"; * if (File.Exists(TsIndexFQN)) * { * FileStream iout = new FileStream(TsIndexFQN, FileMode.Open, * FileAccess.Read, FileShare.ReadWrite); * * BinaryReader index_br = new BinaryReader(iout); * * try * { * while (true) * { * long ts = index_br.ReadInt64(); * long offset = index_br.ReadInt64(); * ts_index.Add(new TS_Offset(ts, offset)); * }; * } * catch (EndOfStreamException) * { * // done * } * finally * { * index_br.Close(); * } * } */ // create the FileStream fout = new FileStream(targetDir + "/stream.dat", FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite); fout.Seek(0, SeekOrigin.End); fs_bw = new BinaryWriter(fout); fin = new FileStream(targetDir + "/stream.dat", FileMode.Open, FileAccess.Read, FileShare.ReadWrite); fs_br = new BinaryReader(fin); } catch (Exception e) { Console.WriteLine("Failed to open file: " + targetDir + "/stream.dat"); Console.WriteLine("{0} Exception caught.", e); } }