public FileReplicationState GetOrCreateFileState(string sourceFileName) { var key = sourceFileName.ToLowerInvariant(); _fileStates.TryGetValue(key, out var result); if (result == null) { _fileStates[key] = result = new FileReplicationState(sourceFileName); } return(result); }
private void Replicate(FileInfo sourceFile, FileReplicationState state, ref IDataStore target) { _trace.TraceVerbose("Verifying for modifications inbound file: {0}.", sourceFile.FullName); var source = new FileStore(sourceFile); var lastReadLink = state.LastReadLink; var lastReplicated = GetLastReplicatedItem(lastReadLink, source, ref target, out var isLinkValid); var changeDetected = false; for ( ;;) { var newItem = source.GetNext(lastReplicated); if (newItem == source.Eof) { break; } if (changeDetected == false) { _trace.TraceInformation("Changes detected at {0}. Importing...", sourceFile.FullName); changeDetected = true; } string stateTag = null; ResolveTargetStoreIfNeeded(ref target); target.Write(ref stateTag, newItem.Payload); lastReplicated = newItem; } if (!changeDetected) { _trace.TraceVerbose("No change detected."); if (isLinkValid) { return; } _trace.TraceInformation("Attempting to fix last read link..."); } state.LastReadLink = source.CreateLink(lastReplicated); if (!isLinkValid) { source.ResolveLink(state.LastReadLink); } }
public void Read() { if (!_fileInfo.Exists) { _trace.TraceInformation("Replication state file not found: {0}. Assuming empty state.", _fileInfo.FullName); return; } _trace.TraceVerbose("Reading replication state file: {0}", _fileInfo.FullName); using (var stream = new FileStream(_fileInfo.FullName, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete)) using (var reader = new BinaryReader(stream, Encoding.UTF8, true)) { var count = reader.ReadInt32(); while (count > 0) { var fileState = new FileReplicationState(reader); _fileStates[fileState.Name] = fileState; --count; } } }