public void Commit(CancellationToken cancellationToken, ILocator locator = default(ILocator), bool hasLocator = false, IData fromKey = null, IData toKey = null) { if (disposed) { throw new ObjectDisposedException("WTree"); } Params param; if (!hasLocator) { param = new Params(WalkMethod.CascadeButOnlyLoaded, WalkAction.Store, null, false); } else { if (fromKey == null) { param = new Params(WalkMethod.CascadeButOnlyLoaded, WalkAction.Store, null, false, locator); } else { if (toKey == null) { param = new Params(WalkMethod.CascadeButOnlyLoaded, WalkAction.Store, null, false, locator, fromKey); } else { param = new Params(WalkMethod.CascadeButOnlyLoaded, WalkAction.Store, null, false, locator, fromKey, toKey); } } } lock (RootBranch) { Token token = new Token(CacheSemaphore, cancellationToken); RootBranch.Fall(Depth + 1, token, param); token.CountdownEvent.Signal(); token.CountdownEvent.Wait(); //write settings & root cache (always at handle 0). using (MemoryStream ms = new MemoryStream()) { Settings.Serialize(this, ms); RootBranch.Cache.Store(this, new BinaryWriter(ms)); Heap.Write(0, ms.GetBuffer(), 0, (int)ms.Length); Heap.Commit(); } } }
private void DoWork() { try { TcpServer.Start(); while (!ShutdownTokenSource.Token.IsCancellationRequested) { try { var order = TcpServer.RecievedPackets.Take(ShutdownTokenSource.Token); BinaryReader reader = new BinaryReader(order.Value.Request); MemoryStream ms = new MemoryStream(); BinaryWriter writer = new BinaryWriter(ms); var code = (RemoteHeapCommandCodes)reader.ReadByte(); switch (code) { case RemoteHeapCommandCodes.ObtainHandle: ObtainHandleCommand.WriteResponse(writer, Heap.ObtainNewHandle()); break; case RemoteHeapCommandCodes.ReleaseHandle: { var handle = ReleaseHandleCommand.ReadRequest(reader).Handle; Heap.Release(handle); break; } case RemoteHeapCommandCodes.HandleExist: { long handle = HandleExistCommand.ReadRequest(reader).Handle; HandleExistCommand.WriteResponse(writer, Heap.Exists(handle)); break; } case RemoteHeapCommandCodes.WriteCommand: var cmd = WriteCommand.ReadRequest(reader); Heap.Write(cmd.Handle, cmd.Buffer, cmd.Index, cmd.Count); break; case RemoteHeapCommandCodes.ReadCommand: { var handle = ReadCommand.ReadRequest(reader).Handle; ReadCommand.WriteResponse(writer, Heap.Read(handle)); break; } case RemoteHeapCommandCodes.CommitCommand: Heap.Commit(); break; case RemoteHeapCommandCodes.CloseCommand: Heap.Close(); break; case RemoteHeapCommandCodes.SetTag: Heap.Tag = SetTagCommand.ReadRequest(reader).Tag; break; case RemoteHeapCommandCodes.GetTag: GetTagCommand.WriteResponse(writer, Heap.Tag); break; case RemoteHeapCommandCodes.Size: SizeCommand.WriteResponse(writer, Heap.Size); break; case RemoteHeapCommandCodes.DataBaseSize: DataBaseSizeCommand.WriteResponse(writer, Heap.DataSize); break; default: break; } ms.Position = 0; order.Value.Response = ms; order.Key.PendingPackets.Add(order.Value); } catch (OperationCanceledException) { break; } catch (Exception exc) { TcpServer.LogError(exc); } } } catch (Exception exc) { TcpServer.LogError(exc); } finally { TcpServer.Stop(); Worker = null; } }
private void Commit(CancellationToken cancellationToken, Locator locator = default(Locator), bool hasLocator = false, IData fromKey = null, IData toKey = null) { if (disposed) { throw new ObjectDisposedException("WTree"); } Params param; if (!hasLocator) { param = new Params(WalkMethod.CascadeButOnlyLoaded, WalkAction.Store, null, false); } else { if (fromKey == null) { param = new Params(WalkMethod.CascadeButOnlyLoaded, WalkAction.Store, null, false, locator); } else { if (toKey == null) { param = new Params(WalkMethod.CascadeButOnlyLoaded, WalkAction.Store, null, false, locator, fromKey); } else { param = new Params(WalkMethod.CascadeButOnlyLoaded, WalkAction.Store, null, false, locator, fromKey, toKey); } } } lock (RootBranch) { if (!isRootCacheLoaded) { LoadRootCache(); } Token token = new Token(CacheSemaphore, cancellationToken); RootBranch.Fall(Depth + 1, token, param); token.CountdownEvent.Signal(); token.CountdownEvent.Wait(); //write settings using (MemoryStream ms = new MemoryStream()) { Settings.Serialize(this, ms); heap.Write(HANDLE_SETTINGS, ms.GetBuffer(), 0, (int)ms.Length); } //write scheme using (MemoryStream ms = new MemoryStream()) { scheme.Serialize(new BinaryWriter(ms)); heap.Write(HANDLE_SCHEME, ms.GetBuffer(), 0, (int)ms.Length); } //write root cache using (MemoryStream ms = new MemoryStream()) { RootBranch.Cache.Store(this, new BinaryWriter(ms)); heap.Write(HANDLE_ROOT, ms.GetBuffer(), 0, (int)ms.Length); } heap.Commit(); } }