protected override async Task Write(object value, WriteBuffer buf, LengthCache lengthCache, [CanBeNull] NpgsqlParameter parameter, bool async, CancellationToken cancellationToken) { ArraySegment <byte> segment; if (value is ArraySegment <byte> ) { segment = (ArraySegment <byte>)value; if (!(parameter == null || parameter.Size <= 0 || parameter.Size >= segment.Count)) { segment = new ArraySegment <byte>(segment.Array, segment.Offset, parameter.Size); } } else { var array = (byte[])value; var len = parameter == null || parameter.Size <= 0 || parameter.Size >= array.Length ? array.Length : parameter.Size; segment = new ArraySegment <byte>(array, 0, len); } // The entire segment fits in our buffer, copy it as usual. if (segment.Count <= buf.WriteSpaceLeft) { buf.WriteBytes(segment.Array, segment.Offset, segment.Count); return; } // The segment is larger than our buffer. Flush whatever is currently in the buffer and // write the array directly to the socket. await buf.Flush(async, cancellationToken); buf.DirectWrite(segment.Array, segment.Offset, segment.Count); }
public void PrepareWrite(object value, NpgsqlBuffer buf, LengthCache lengthCache, NpgsqlParameter parameter) { _buf = buf; _lengthCache = lengthCache; _value = (NpgsqlRange <TElement>)value; _state = State.Start; }
public void PrepareWrite(object value, NpgsqlBuffer buf, LengthCache lengthCache, NpgsqlParameter parameter = null) { _buf = buf; _charPos = -1; _byteLen = lengthCache.GetLast(); _str = value as string; if (_str != null) { _charLen = parameter == null || parameter.Size <= 0 || parameter.Size >= _str.Length ? _str.Length : parameter.Size; return; } _chars = value as char[]; if (_chars != null) { _charLen = parameter == null || parameter.Size <= 0 || parameter.Size >= _chars.Length ? _chars.Length : parameter.Size; return; } if (value is char) { _singleCharArray[0] = (char)value; _chars = _singleCharArray; _charLen = 1; return; } throw PGUtil.ThrowIfReached(); }
protected override async Task Write(object value, WriteBuffer buf, LengthCache lengthCache, NpgsqlParameter parameter, bool async, CancellationToken cancellationToken) { Debug.Assert(_members != null); var composite = (T)value; if (buf.WriteSpaceLeft < 4) { await buf.Flush(async, cancellationToken); } buf.WriteInt32(_members.Count); foreach (var fieldDescriptor in _members) { var fieldHandler = fieldDescriptor.Handler; var fieldValue = fieldDescriptor.GetValue(composite); if (buf.WriteSpaceLeft < 4) { await buf.Flush(async, cancellationToken); } buf.WriteUInt32(fieldDescriptor.OID); await fieldHandler.WriteWithLength(fieldValue, buf, lengthCache, null, async, cancellationToken); } }
public void PrepareWrite(object value, NpgsqlBuffer buf, LengthCache lengthCache, NpgsqlParameter parameter = null) { _buf = buf; _charPos = -1; _byteLen = lengthCache.GetLast(); if (parameter != null && parameter.ConvertedValue != null) { value = parameter.ConvertedValue; } switch (Type.GetTypeCode(value.GetType())) { case TypeCode.String: _str = (string)value; _charLen = parameter == null || parameter.Size <= 0 || parameter.Size >= _str.Length ? _str.Length : parameter.Size; return; case TypeCode.Object: Contract.Assert(value is char[]); _chars = (char[])value; _charLen = parameter == null || parameter.Size <= 0 || parameter.Size >= _chars.Length ? _chars.Length : parameter.Size; return; case TypeCode.Char: _singleCharArray[0] = (char)value; _chars = _singleCharArray; _charLen = 1; return; default: value = Convert.ToString(value); goto case TypeCode.String; } }
public override void PrepareWrite(object value, WriteBuffer buf, LengthCache lengthCache, NpgsqlParameter parameter = null) { _writeBuf = buf; _pos = -1; _value = value; }
public int ValidateAndGetLength(object value, ref LengthCache lengthCache, NpgsqlParameter parameter = null) { var range = (NpgsqlRange <TElement>)value; var totalLen = 1; if (!range.IsEmpty) { var asChunkingWriter = ElementHandler as IChunkingTypeWriter; if (!range.LowerBoundInfinite) { totalLen += 4 + (asChunkingWriter != null ? asChunkingWriter.ValidateAndGetLength(range.LowerBound, ref lengthCache, parameter) : ((ISimpleTypeWriter)ElementHandler).ValidateAndGetLength(range.LowerBound)); } if (!range.UpperBoundInfinite) { totalLen += 4 + (asChunkingWriter != null ? asChunkingWriter.ValidateAndGetLength(range.UpperBound, ref lengthCache, parameter) : ((ISimpleTypeWriter)ElementHandler).ValidateAndGetLength(range.UpperBound)); } } return(totalLen); }
public override void PrepareWrite(object value, WriteBuffer buf, LengthCache lengthCache, NpgsqlParameter parameter) { _writeBuf = buf; _lengthCache = lengthCache; _value = (NpgsqlRange <TElement>)value; _state = State.Flags; }
public override int ValidateAndGetLength(object value, ref LengthCache lengthCache, NpgsqlParameter parameter = null) { var asBitArray = value as BitArray; if (asBitArray != null) { return(4 + (asBitArray.Length + 7) / 8); } if (value is bool) { return(5); } var asString = value as string; if (asString != null) { if (asString.Any(c => c != '0' && c != '1')) { throw new FormatException("Cannot interpret as ASCII BitString: " + asString); } return(4 + (asString.Length + 7) / 8); } throw CreateConversionException(value.GetType()); }
protected override async Task Write(object value, WriteBuffer buf, LengthCache lengthCache, NpgsqlParameter parameter, bool async, CancellationToken cancellationToken) { var vector = (NpgsqlTsVector)value; if (buf.WriteSpaceLeft < 4) { await buf.Flush(async, cancellationToken); } buf.WriteInt32(vector.Count); foreach (var lexeme in vector) { if (buf.WriteSpaceLeft < MaxSingleLexemeBytes) { await buf.Flush(async, cancellationToken); } buf.WriteString(lexeme.Text); buf.WriteByte(0); buf.WriteInt16(lexeme.Count); for (var i = 0; i < lexeme.Count; i++) { buf.WriteInt16(lexeme[i].Value); } } }
public override int ValidateAndGetLength(object value, ref LengthCache lengthCache, NpgsqlParameter parameter) { ResolveFieldsIfNeeded(); if (lengthCache == null) { lengthCache = new LengthCache(1); } if (lengthCache.IsPopulated) { return(lengthCache.Get()); } // Leave empty slot for the entire composite type, and go ahead an populate the element slots var pos = lengthCache.Position; lengthCache.Set(0); var totalLen = 4; // number of fields foreach (var f in _members) { totalLen += 4 + 4; // type oid + field length var fieldValue = f.GetValue(value); var asChunkingWriter = f.Handler as IChunkingTypeHandler; totalLen += asChunkingWriter?.ValidateAndGetLength(fieldValue, ref lengthCache, parameter) ?? ((ISimpleTypeHandler)f.Handler).ValidateAndGetLength(fieldValue, null); } return(lengthCache.Lengths[pos] = totalLen); }
protected override Task Write(object value, WriteBuffer buf, LengthCache lengthCache, NpgsqlParameter parameter, bool async, CancellationToken cancellationToken) { var bitArray = value as BitArray; if (bitArray != null) { return(WriteBitArray(bitArray, buf, async, cancellationToken)); } if (value is BitVector32) { return(WriteBitVector32((BitVector32)value, buf, async, cancellationToken)); } if (value is bool) { return(WriteBool((bool)value, buf, async, cancellationToken)); } var str = value as string; if (str != null) { return(WriteString(str, buf, async, cancellationToken)); } throw new InvalidOperationException($"Bad type {value.GetType()} some made its way into BitStringHandler.Write()"); }
public override int ValidateAndGetLength(object value, ref LengthCache lengthCache, NpgsqlParameter parameter = null) { if (value is ArraySegment <byte> ) { var arraySegment = (ArraySegment <byte>)value; if (arraySegment.Array == null) { throw new InvalidCastException("Array in ArraySegment<byte> is null"); } return(parameter == null || parameter.Size <= 0 || parameter.Size >= arraySegment.Count ? arraySegment.Count : parameter.Size); } var asArray = value as byte[]; if (asArray != null) { return(parameter == null || parameter.Size <= 0 || parameter.Size >= asArray.Length ? asArray.Length : parameter.Size); } throw CreateConversionException(value.GetType()); }
public override int ValidateAndGetLength(object value, ref LengthCache lengthCache, NpgsqlParameter parameter = null) { if (!(value is NpgsqlRange <TElement>)) { throw CreateConversionException(value.GetType()); } var range = (NpgsqlRange <TElement>)value; var totalLen = 1; var lengthCachePos = lengthCache?.Position ?? 0; if (!range.IsEmpty) { if (!range.LowerBoundInfinite) { totalLen += 4 + ElementHandler.ValidateAndGetLength(range.LowerBound, ref lengthCache); } if (!range.UpperBoundInfinite) { totalLen += 4 + ElementHandler.ValidateAndGetLength(range.UpperBound, ref lengthCache); } } // If we're traversing an already-populated length cache, rewind to first element slot so that // the elements' handlers can access their length cache values if (lengthCache != null && lengthCache.IsPopulated) { lengthCache.Position = lengthCachePos; } return(totalLen); }
public override void PrepareWrite(object value, WriteBuffer buf, LengthCache lengthCache, NpgsqlParameter parameter = null) { Reset(); _inByteaMode = null; _writeBuf = buf; _icol.Clear(); _toWrite = value as PostgisGeometry; if (_toWrite != null) { _inByteaMode = false; return; } _bytes = value as byte[]; if (_bytes != null) { if (_byteaHandler == null) { throw new NpgsqlException("Bytea handler was not found during initialization of PostGIS handler"); } _inByteaMode = true; _byteaHandler.PrepareWrite(_bytes, _writeBuf, lengthCache, parameter); return; } throw new InvalidCastException("IGeometry type expected."); }
public int ValidateAndGetLength(object value, ref LengthCache lengthCache, NpgsqlParameter parameter = null) { // TODO: Implement length cache var vec = (NpgsqlTsVector)value; return(4 + vec.Sum(l => Encoding.UTF8.GetByteCount(l.Text) + 1 + 2 + l.Count * 2)); }
public int ValidateAndGetLength(object value, ref LengthCache lengthCache, NpgsqlParameter parameter = null) { var asBitArray = value as BitArray; if (asBitArray != null) { return(4 + (asBitArray.Length + 7) / 8); } if (value is bool) { return(5); } var asString = value as string; if (asString != null) { if (asString.Any(c => c != '0' && c != '1')) { throw new FormatException("Cannot interpret as ASCII BitString: " + asString); } return(4 + (asString.Length + 7) / 8); } throw new InvalidCastException("Expected BitArray, bool or string"); }
/// <summary> /// Converts the percentLength of a spline to the common t parameter. /// The spline is represented by its LengthCache. /// </summary> public static float t_PercentLength(float percentLength, LengthCache lengthTable) { float[] lengthValues = lengthTable.Values; percentLength = Mathf.Clamp01(percentLength); //// Early Out if (Mathf.Approximately(percentLength, 0f)) return 0f; else if (Mathf.Approximately(percentLength, 1.0f)) return 1f; //// Search for t that matches the length percent float tAsLength = percentLength * lengthValues[lengthValues.Length - 1]; int nearestIndex = Array.BinarySearch<float>(lengthValues, tAsLength); float finalT; //// Get the final output if (nearestIndex < 0) { int lowerValueIndex = ~nearestIndex - 1; float lowerValue = (lowerValueIndex < 0) ? 0f : lengthValues[lowerValueIndex]; float approximateOffset = (tAsLength - lowerValue); approximateOffset /= (lengthValues[lowerValueIndex + 1] - lowerValue); finalT = (lowerValueIndex + approximateOffset) / lengthValues.Length; } else { finalT = (float)nearestIndex / lengthValues.Length; } return finalT; }
public int ValidateAndGetLength(object value, ref LengthCache lengthCache, NpgsqlParameter parameter = null) { if (!(value is NpgsqlPolygon)) { throw new InvalidCastException("Expected an NpgsqlPolygon"); } return(4 + ((NpgsqlPolygon)value).Count * 16); }
public override int ValidateAndGetLength(object value, ref LengthCache lengthCache, NpgsqlParameter parameter = null) { if (!(value is NpgsqlPolygon)) { throw CreateConversionException(value.GetType()); } return(4 + ((NpgsqlPolygon)value).Count * 16); }
public override void PrepareWrite(object value, WriteBuffer buf, LengthCache lengthCache, NpgsqlParameter parameter) { _value = (T)value; _writeBuf = buf; _lengthCache = lengthCache; _fieldIndex = -1; _wroteFieldHeader = false; }
protected override async Task Write(object value, WriteBuffer buf, LengthCache lengthCache, NpgsqlParameter parameter, bool async, CancellationToken cancellationToken) { var query = (NpgsqlTsQuery)value; var numTokens = GetTokenCount(query); if (buf.WriteSpaceLeft < 4) { await buf.Flush(async, cancellationToken); } buf.WriteInt32(numTokens); if (numTokens == 0) { return; } _stack.Push(query); while (_stack.Count > 0) { if (buf.WriteSpaceLeft < 2) { await buf.Flush(async, cancellationToken); } if (_stack.Peek().Kind == NpgsqlTsQuery.NodeKind.Lexeme && buf.WriteSpaceLeft < MaxSingleTokenBytes) { await buf.Flush(async, cancellationToken); } var node = _stack.Pop(); buf.WriteByte(node.Kind == NpgsqlTsQuery.NodeKind.Lexeme ? (byte)1 : (byte)2); if (node.Kind != NpgsqlTsQuery.NodeKind.Lexeme) { buf.WriteByte((byte)node.Kind); if (node.Kind == NpgsqlTsQuery.NodeKind.Not) { _stack.Push(((NpgsqlTsQueryNot)node).Child); } else { _stack.Push(((NpgsqlTsQueryBinOp)node).Right); _stack.Push(((NpgsqlTsQueryBinOp)node).Left); } } else { var lexemeNode = (NpgsqlTsQueryLexeme)node; buf.WriteByte((byte)lexemeNode.Weights); buf.WriteByte(lexemeNode.IsPrefixSearch ? (byte)1 : (byte)0); buf.WriteString(lexemeNode.Text); buf.WriteByte(0); } } _stack.Clear(); }
public override int ValidateAndGetLength(object value, ref LengthCache lengthCache, NpgsqlParameter parameter = null) { if (lengthCache == null) { lengthCache = new LengthCache(1); } if (lengthCache.IsPopulated) { return(lengthCache.Get()); } //return lengthCache.Set(DoValidateAndGetLength(value, parameter)); var asString = value as string; if (asString != null) { return(lengthCache.Set( parameter == null || parameter.Size <= 0 || parameter.Size >= asString.Length ? PGUtil.UTF8Encoding.GetByteCount(asString) : PGUtil.UTF8Encoding.GetByteCount(asString.ToCharArray(), 0, parameter.Size) )); } var asCharArray = value as char[]; if (asCharArray != null) { return(lengthCache.Set( parameter == null || parameter.Size <= 0 || parameter.Size >= asCharArray.Length ? PGUtil.UTF8Encoding.GetByteCount(asCharArray) : PGUtil.UTF8Encoding.GetByteCount(asCharArray, 0, parameter.Size) )); } if (value is char) { _singleCharArray[0] = (char)value; return(lengthCache.Set(PGUtil.UTF8Encoding.GetByteCount(_singleCharArray))); } // Fallback - try to convert the value to string var converted = Convert.ToString(value); if (parameter == null) { throw CreateConversionButNoParamException(value.GetType()); } parameter.ConvertedValue = converted; return(lengthCache.Set( parameter.Size <= 0 || parameter.Size >= converted.Length ? PGUtil.UTF8Encoding.GetByteCount(converted) : PGUtil.UTF8Encoding.GetByteCount(converted.ToCharArray(), 0, parameter.Size) )); }
protected override async Task Write(object value, WriteBuffer buf, LengthCache lengthCache, NpgsqlParameter parameter, bool async, CancellationToken cancellationToken) { if (buf.WriteSpaceLeft < 1) { await buf.Flush(async, cancellationToken); } buf.WriteByte(JsonbProtocolVersion); await base.Write(value, buf, lengthCache, parameter, async, cancellationToken); }
public int ValidateAndGetLength(object value, ref LengthCache lengthCache, NpgsqlParameter parameter = null) { var g = value as PostgisGeometry; if (g == null) { throw new InvalidCastException("IGeometry type expected."); } return(g.GetLen()); }
public int ValidateAndGetLength(object value, ref LengthCache lengthCache, NpgsqlParameter parameter = null) { var vec = (NpgsqlTsQuery)value; if (vec.Kind == NpgsqlTsQuery.NodeKind.Empty) { return(4); } return(4 + GetNodeLength(vec)); }
public void PrepareWrite(object value, NpgsqlBuffer buf, LengthCache lengthCache, NpgsqlParameter parameter = null) { _toWrite = value as PostgisGeometry; if (_toWrite == null) { throw new InvalidCastException("IGeometry type expected."); } _buf = buf; _icol.Clear(); Reset(); }
int GetSingleElementLength(object element, ref LengthCache lengthCache, NpgsqlParameter parameter = null) { if (element == null || element is DBNull) { return(0); } var asChunkingWriter = ElementHandler as IChunkingTypeWriter; return(asChunkingWriter != null ? asChunkingWriter.ValidateAndGetLength(element, ref lengthCache, parameter) : ((ISimpleTypeWriter)ElementHandler).ValidateAndGetLength(element)); }
public int ValidateAndGetLength <TElement>(object value, ref LengthCache lengthCache, NpgsqlParameter parameter = null) { // Take care of single-dimensional arrays and generic IList<T> var asGenericList = value as IList <TElement>; if (asGenericList != null) { if (lengthCache == null) { lengthCache = new LengthCache(1); } if (lengthCache.IsPopulated) { return(lengthCache.Get()); } // Leave empty slot for the entire array length, and go ahead an populate the element slots var pos = lengthCache.Position; lengthCache.Set(0); var lengthCache2 = lengthCache; var len = 12 + (1 * 8) + asGenericList.Sum(e => 4 + GetSingleElementLength(e, ref lengthCache2, parameter)); lengthCache = lengthCache2; return(lengthCache.Lengths[pos] = len); } // Take care of multi-dimensional arrays and non-generic IList, we have no choice but to do // boxing/unboxing var asNonGenericList = value as IList; if (asNonGenericList != null) { if (lengthCache == null) { lengthCache = new LengthCache(1); } if (lengthCache.IsPopulated) { return(lengthCache.Get()); } var asMultidimensional = value as Array; var dimensions = asMultidimensional != null ? asMultidimensional.Rank : 1; // Leave empty slot for the entire array length, and go ahead an populate the element slots var pos = lengthCache.Position; lengthCache.Set(0); var lengthCache2 = lengthCache; var len = 12 + (dimensions * 8) + asNonGenericList.Cast <object>().Sum(element => 4 + GetSingleElementLength(element, ref lengthCache2, parameter)); lengthCache = lengthCache2; lengthCache.Lengths[pos] = len; return(len); } throw new InvalidCastException(String.Format("Can't write type {0} as an array", value.GetType())); }
public override int ValidateAndGetLength(object value, ref LengthCache lengthCache, NpgsqlParameter parameter = null) { // TODO: Implement length cache var vec = value as NpgsqlTsVector; if (vec == null) { throw CreateConversionException(value.GetType()); } return(4 + vec.Sum(l => Encoding.UTF8.GetByteCount(l.Text) + 1 + 2 + l.Count * 2)); }
public int ValidateAndGetLength(object value, ref LengthCache lengthCache, NpgsqlParameter parameter = null) { if (lengthCache == null) { lengthCache = new LengthCache(1); } if (lengthCache.IsPopulated) { return(lengthCache.Get()); } return(lengthCache.Set(DoValidateAndGetLength(value, parameter))); }
/// <summary> /// Builds a cache from the results of evenly evaluating a spline. /// </summary> public static EvaluationCache EvenEvaluationCache(int cacheSize, ISpline spline, LengthCache splineLengthCache) { var points = new Vector3[cacheSize]; for (int i = 0; i < points.Length; i++) { points[i] = EvenlyEvaluate( spline, (float)i / (points.Length - 1), splineLengthCache); } return new EvaluationCache(points); }
/// <summary> /// Evaluates a spline based on the integrated length of the spline. /// </summary> public static Vector3 EvenlyEvaluate(ISpline spline, float percentLength, LengthCache splineLengthCache) { return spline.Evaluate(t_PercentLength(percentLength, splineLengthCache)); }