/* * public void FlushManual() * { * // flush ACL out to FQFilename * // write meta-data in JSON format * mdtw.WriteLine("{\"md\": {"); * mdtw.WriteLine(" \"Owner\": \"" + Owner + "\","); * mdtw.WriteLine(" \"Sharing\": {"); * foreach (KeyValuePair<string, bool> item in RACL) * { * bool read = item.Value; * bool write = false; * * if (WACL.ContainsKey(item.Key)) * { * write = true; * WACL.Remove(item.Key); * } * * string acl = read ? "R" : ""; * acl += write ? "W" : ""; * * mdtw.WriteLine(" \"" + item.Key + "\": \"" + acl + "\","); * } * * foreach (KeyValuePair<string, bool> item in WACL) * { * mdtw.WriteLine(" \"" + item.Key + "\": \"W\","); * } * * * mdtw.WriteLine(" }"); * mdtw.WriteLine("}"); * mdtw.Close(); * } */ public void LoadMetaData() { try { TextReader mdtr = new StreamReader(FQFilename); string json = mdtr.ReadToEnd(); mdtr.Close(); MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)); DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(OldMetaData)); OldMetaData md = (OldMetaData)ser.ReadObject(ms); ms.Close(); Owner = md.Owner; RACL = md.RACL; WACL = md.WACL; Readers = md.Readers; Writers = md.Writers; } catch (Exception e) { Owner = string.Empty; RACL.Clear(); WACL.Clear(); Readers.Clear(); Writers.Clear(); Console.WriteLine("Failed to load metadata file: " + FQFilename); Console.WriteLine("{0} Exception caught.", e); } }
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); } }