public override bool Equals(object obj) { if (this == obj) { return(true); } if (obj == null) { return(false); } if (Sharpen.Runtime.getClassForObject(this) != Sharpen.Runtime.getClassForObject( obj)) { return(false); } VPackSlice other = (VPackSlice)obj; if (this.start != other.start) { return(false); } if (!java.util.Arrays.equals(this.getRawVPack(), other.getRawVPack())) { return(false); } return(true); }
/// <exception cref="VPackValueTypeException"/> protected internal SliceIterator(VPackSlice slice) : base() { this.slice = slice; this.size = slice.getLength(); this.position = 0; }
/// <returns>the offset for the nth member from a compact Array or Object type</returns> private int getNthOffsetFromCompact(int index) { long end = NumberUtil.readVariableValueLength (this.vpack, this.start + 1, false); long n = NumberUtil.readVariableValueLength (this.vpack, (int)(this.start + end - 1), true); if (index >= n) { throw new System.IndexOutOfRangeException(); } byte head = head(); long offset = 1 + NumberUtil.getVariableValueLength (end); long current = 0; while (current != index) { long byteSize = new VPackSlice(this.vpack, (int)(this.start + offset )).getByteSize(); offset += byteSize; if (head == unchecked ((int)0x14)) { offset += byteSize; } ++current; } return((int)offset); }
/// <exception cref="VPackException"/> public virtual string toJson(VPackSlice vpack, bool includeNullValues ) { java.lang.StringBuilder json = new java.lang.StringBuilder(); this.parse(null, null, vpack, json, includeNullValues); return(json.ToString()); }
/// <summary>translates an integer key into a string, without checks</summary> protected internal virtual VPackSlice translateUnchecked( ) { VPackSlice result = attributeTranslator.translate(this.getAsInt ()); return(result != null ? result : new VPackSlice()); }
public virtual VPackSlice valueAt(int index) { if (!this.isObject()) { throw new VPackValueTypeException(ValueType .OBJECT); } VPackSlice key = this.getNthKey(index); return(new VPackSlice(this.vpack, key.start + key.getByteSize( ))); }
/// <exception cref="com.arangodb.velocypack.exception.VPackValueTypeException"/> /// <exception cref="com.arangodb.velocypack.exception.VPackNeedAttributeTranslatorException /// "/> private VPackSlice searchObjectKeyLinear(string attribute , long ieBase, int offsetsize, long n) { bool useTranslator = VPackSlice.attributeTranslator != null; VPackSlice result = new VPackSlice (); for (long index = 0; index < n; index++) { long offset = ieBase + index * offsetsize; long keyIndex = NumberUtil.toLong(this.vpack, ( int)(this.start + offset), offsetsize); VPackSlice key = new VPackSlice(this.vpack , (int)(this.start + keyIndex)); if (key.isString()) { if (!key.isEqualString(attribute)) { continue; } } else { if (key.isInteger()) { // translate key if (!useTranslator) { // no attribute translator throw new VPackNeedAttributeTranslatorException (); } if (!key.translateUnchecked().isEqualString(attribute)) { continue; } } else { // invalid key type result = new VPackSlice(); break; } } // key is identical. now return value result = new VPackSlice(this.vpack, key.start + key.getByteSize ()); break; } return(result); }
/// <exception cref="VPackException"/> private void parseArray(VPackSlice value, java.lang.StringBuilder json, bool includeNullValues) { json.Append(ARRAY_OPEN); int added = 0; for (int i = 0; i < value.getLength(); i++) { VPackSlice valueAt = value.get(i); if (!valueAt.isNull() || includeNullValues) { if (added++ > 0) { json.Append(SEPARATOR); } this.parse(value, null, valueAt, json, includeNullValues); } } json.Append(ARRAY_CLOSE); }
/// <exception cref="VPackException"/> private void parseObject(VPackSlice value, java.lang.StringBuilder json, bool includeNullValues) { json.Append(OBJECT_OPEN); int added = 0; for (System.Collections.Generic.IEnumerator <KeyValuePair <string, VPackSlice> > iterator = value.objectIterator(); iterator.MoveNext();) { System.Collections.Generic.KeyValuePair <string, VPackSlice > next = iterator.Current; VPackSlice nextValue = next.Value; if (!nextValue.isNull() || includeNullValues) { if (added++ > 0) { json.Append(SEPARATOR); } this.parse(value, next.Key, nextValue, json, includeNullValues); } } json.Append(OBJECT_CLOSE); }
/// <returns>the offset for the nth member from an Array or Object type</returns> private int getNthOffset(int index) { int offset; byte head = head(); if (head == unchecked ((int)0x13) || head == unchecked ((int)0x14)) { // compact Array or Object offset = this.getNthOffsetFromCompact(index); } else { if (head == unchecked ((int)0x01) || head == unchecked ((int)0x0a)) { // special case: empty Array or empty Object throw new System.IndexOutOfRangeException(); } else { long n; int offsetsize = ObjectArrayUtil.getOffsetSize (head); long end = NumberUtil.toLong(this.vpack, this.start + 1, offsetsize); int dataOffset = this.findDataOffset(); if ((sbyte)head <= unchecked ((int)0x05)) { // array with no offset table or length VPackSlice first = new VPackSlice (this.vpack, this.start + dataOffset); n = (end - dataOffset) / first.getByteSize(); } else { if (offsetsize < 8) { n = NumberUtil.toLong(this.vpack, this.start + 1 + offsetsize , offsetsize); } else { n = NumberUtil.toLong(this.vpack, (int)(this.start + end - offsetsize), offsetsize); } } if (index >= n) { throw new System.IndexOutOfRangeException(); } if ((sbyte)head <= unchecked ((int)0x05) || n == 1) { // no index table, but all array items have the same length // or only one item is in the array // now fetch first item and determine its length if (dataOffset == 0) { dataOffset = this.findDataOffset(); } offset = dataOffset + index * new VPackSlice(this.vpack, this.start + dataOffset).getByteSize(); } else { long ieBase = end - n * offsetsize + index * offsetsize - (offsetsize == 8 ? 8 : 0); offset = (int)NumberUtil.toLong(this.vpack, (int )(this.start + ieBase), offsetsize); } } } return(offset); }
/// <exception cref="com.arangodb.velocypack.exception.VPackValueTypeException"/> /// <exception cref="com.arangodb.velocypack.exception.VPackNeedAttributeTranslatorException /// "/> private VPackSlice searchObjectKeyBinary(string attribute , long ieBase, int offsetsize, long n) { bool useTranslator = VPackSlice.attributeTranslator != null; VPackSlice result; long l = 0; long r = n - 1; for (;;) { // midpoint long index = l + (r - l) / 2; long offset = ieBase + index * offsetsize; long keyIndex = NumberUtil.toLong(this.vpack, ( int)(this.start + offset), offsetsize); VPackSlice key = new VPackSlice(this.vpack , (int)(this.start + keyIndex)); int res = 0; if (key.isString()) { res = key.compareString(attribute); } else { if (key.isInteger()) { // translate key if (!useTranslator) { // no attribute translator throw new VPackNeedAttributeTranslatorException (); } res = key.translateUnchecked().compareString(attribute); } else { // invalid key result = new VPackSlice(); break; } } if (res == 0) { // found result = new VPackSlice(this.vpack, key.start + key.getByteSize ()); break; } if (res > 0) { if (index == 0) { result = new VPackSlice(); break; } r = index - 1; } else { l = index + 1; } if (r < l) { result = new VPackSlice(); break; } } return(result); }
/// <exception cref="VPackException"/> public virtual VPackSlice get(string attribute) { if (!this.isObject()) { throw new VPackValueTypeException(ValueType .OBJECT); } byte head = head(); VPackSlice result = new VPackSlice (); if (head == unchecked ((int)0x0a)) { // special case, empty object result = new VPackSlice(); } else { if (head == unchecked ((int)0x14)) { // compact Object result = this.getFromCompactObject(attribute); } else { int offsetsize = ObjectArrayUtil.getOffsetSize (head); long end = NumberUtil.toLong(this.vpack, this.start + 1, offsetsize); long n; if (offsetsize < 8) { n = NumberUtil.toLong(this.vpack, this.start + 1 + offsetsize , offsetsize); } else { n = NumberUtil.toLong(this.vpack, (int)(this.start + end - offsetsize), offsetsize); } if (n == 1) { // Just one attribute, there is no index table! VPackSlice key = new VPackSlice(this.vpack , this.start + this.findDataOffset()); if (key.isString()) { if (key.isEqualString(attribute)) { result = new VPackSlice(this.vpack, key.start + key.getByteSize ()); } else { // no match result = new VPackSlice(); } } else { if (key.isInteger()) { // translate key if (VPackSlice.attributeTranslator == null) { throw new VPackNeedAttributeTranslatorException (); } if (key.translateUnchecked().isEqualString(attribute)) { result = new VPackSlice(this.vpack, key.start + key.getByteSize ()); } else { // no match result = new VPackSlice(); } } else { // no match result = new VPackSlice(); } } } else { long ieBase = end - n * offsetsize - (offsetsize == 8 ? 8 : 0); // only use binary search for attributes if we have at least // this many entries // otherwise we'll always use the linear search long sortedSearchEntriesThreshold = 4; bool sorted = head >= unchecked ((int)0x0b) && (sbyte)head <= unchecked ((int)0x0e); if (sorted && n >= sortedSearchEntriesThreshold) { // This means, we have to handle the special case n == 1 // only in the linear search! result = this.searchObjectKeyBinary(attribute, ieBase, offsetsize, n); } else { result = this.searchObjectKeyLinear(attribute, ieBase, offsetsize, n); } } } } return(result); }
/// <returns>the number of members for an Array, Object or String</returns> public virtual int getLength() { long length; if (this.isString()) { length = this.getStringLength(); } else { if (!this.isArray() && !this.isObject()) { throw new VPackValueTypeException(ValueType .ARRAY, ValueType.OBJECT, ValueType .STRING); } else { byte head = head(); if (head == unchecked ((int)0x01) || head == unchecked ((int)0x0a)) { // empty length = 0; } else { if (head == unchecked ((int)0x13) || head == unchecked ((int)0x14)) { // compact array or object long end = NumberUtil.readVariableValueLength (this.vpack, this.start + 1, false); length = NumberUtil.readVariableValueLength (this.vpack, (int)(this.start + end - 1), true); } else { int offsetsize = ObjectArrayUtil.getOffsetSize (head); long end = NumberUtil.toLong(this.vpack, this.start + 1, offsetsize); if ((sbyte)head <= unchecked ((int)0x05)) { // array with no offset table or length int dataOffset = this.findDataOffset(); VPackSlice first = new VPackSlice (this.vpack, this.start + dataOffset); length = (end - dataOffset) / first.getByteSize(); } else { if (offsetsize < 8) { length = NumberUtil.toLong(this.vpack, this.start + 1 + offsetsize, offsetsize); } else { length = NumberUtil.toLong(this.vpack, (int)(this.start + end - offsetsize), offsetsize); } } } } } } return((int)length); }
/// <exception cref="VPackException"/> public virtual string toJson(VPackSlice vpack) { return(this.toJson(vpack, false)); }
/// <exception cref="VPackException"/> private void parse(VPackSlice parent, string attribute, VPackSlice value, java.lang.StringBuilder json, bool includeNullValues) { VPackJsonDeserializer deserializer = null; if (attribute != null) { appendField(attribute, json); deserializer = this.getDeserializer(attribute, value.type()); } if (deserializer != null) { deserializer.deserialize(parent, attribute, value, json); } else { if (value.isObject()) { this.parseObject(value, json, includeNullValues); } else { if (value.isArray()) { this.parseArray(value, json, includeNullValues); } else { if (value.isBoolean()) { json.Append(value.getAsBoolean()); } else { if (value.isString()) { json.Append(org.json.simple.JSONValue.toJSONString(value.getAsString())); } else { if (value.isNumber()) { json.Append(value.getAsNumber()); } else { if (value.isNull()) { json.Append(NULL); } else { json.Append(org.json.simple.JSONValue.toJSONString(NON_REPRESENTABLE_TYPE)); } } } } } } } }