private Key ConvertKey(Key key) { if (key == null) { return null; } if (key.type != ClassDescriptor.tpArrayOfObject) { throw new StorageError(StorageError.INCOMPATIBLE_KEY_TYPE); } object[] values = (object[]) key.oval; ByteBuffer buf = new ByteBuffer(); int dst = 0; for (int i = 0; i < values.Length; i++) { object v = values[i]; switch (types[i]) { case ClassDescriptor.tpBoolean: buf.Extend(dst + 1); buf.arr[dst++] = (byte) (((bool) v) ? 1 : 0); break; case ClassDescriptor.tpByte: buf.Extend(dst + 1); buf.arr[dst++] = (byte) Convert.ToSByte(((System.ValueType) v)); break; case ClassDescriptor.tpShort: buf.Extend(dst + 2); Bytes.Pack2(buf.arr, dst, Convert.ToInt16(((System.ValueType) v))); dst += 2; break; case ClassDescriptor.tpChar: buf.Extend(dst + 2); Bytes.Pack2(buf.arr, dst, (v is System.ValueType) ? Convert.ToInt16(((System.ValueType) v)) : (short) ((System.Char) v)); dst += 2; break; case ClassDescriptor.tpInt: buf.Extend(dst + 4); Bytes.Pack4(buf.arr, dst, Convert.ToInt32(((System.ValueType) v))); dst += 4; break; case ClassDescriptor.tpObject: buf.Extend(dst + 4); Bytes.Pack4(buf.arr, dst, v == null ? 0 : ((IPersistent) v).Oid); dst += 4; break; case ClassDescriptor.tpLong: buf.Extend(dst + 8); Bytes.Pack8(buf.arr, dst, Convert.ToInt64(((System.ValueType) v))); dst += 8; break; case ClassDescriptor.tpDate: buf.Extend(dst + 8); //UPGRADE_TODO: Method 'java.util.Date.getTime' was converted to 'DateTime.Ticks' which has a different behavior. Bytes.Pack8(buf.arr, dst, v == null ? -1 : ((DateTime) v).Ticks); dst += 8; break; case ClassDescriptor.tpFloat: buf.Extend(dst + 4); float f = (float)v; Bytes.PackF4(buf.arr, dst, f); dst += 4; break; case ClassDescriptor.tpDouble: buf.Extend(dst + 8); double d = (double)v; // TODOPORT: Convert.ToDouble(((System.ValueType) v) ? Bytes.PackF8(buf.arr, dst, d); dst += 8; break; case ClassDescriptor.tpString: { buf.Extend(dst + 4); if (v != null) { string str = (string) v; int len = str.Length; Bytes.Pack4(buf.arr, dst, len); dst += 4; buf.Extend(dst + len * 2); for (int j = 0; j < len; j++) { Bytes.Pack2(buf.arr, dst, (short) str[j]); dst += 2; } } else { Bytes.Pack4(buf.arr, dst, 0); dst += 4; } break; } case ClassDescriptor.tpArrayOfByte: { buf.Extend(dst + 4); if (v != null) { byte[] arr = (byte[]) v; int len = arr.Length; Bytes.Pack4(buf.arr, dst, len); dst += 4; buf.Extend(dst + len); Array.Copy(arr, 0, buf.arr, dst, len); dst += len; } else { Bytes.Pack4(buf.arr, dst, 0); dst += 4; } break; } default: Assert.Failed("Invalid type"); break; } } return new Key(buf.ToArray(), key.inclusion != 0); }
private Key ExtractKey(IPersistent obj) { try { ByteBuffer buf = new ByteBuffer(); int dst = 0; for (int i = 0; i < fld.Length; i++) { FieldInfo f = (FieldInfo) fld[i]; switch (types[i]) { case ClassDescriptor.tpBoolean: buf.Extend(dst + 1); buf.arr[dst++] = (byte) ((bool) f.GetValue(obj) ? 1 : 0); break; case ClassDescriptor.tpByte: buf.Extend(dst + 1); buf.arr[dst++] = (byte) f.GetValue(obj); break; case ClassDescriptor.tpShort: buf.Extend(dst + 2); Bytes.Pack2(buf.arr, dst, (short) f.GetValue(obj)); dst += 2; break; case ClassDescriptor.tpChar: buf.Extend(dst + 2); Bytes.Pack2(buf.arr, dst, (short) f.GetValue(obj)); dst += 2; break; case ClassDescriptor.tpInt: buf.Extend(dst + 4); Bytes.Pack4(buf.arr, dst, (int) f.GetValue(obj)); dst += 4; break; case ClassDescriptor.tpObject: { IPersistent p = (IPersistent) f.GetValue(obj); int oid = p == null ? 0 : p.Oid; buf.Extend(dst + 4); Bytes.Pack4(buf.arr, dst, oid); dst += 4; break; } case ClassDescriptor.tpLong: buf.Extend(dst + 8); Bytes.Pack8(buf.arr, dst, (long) f.GetValue(obj)); dst += 8; break; case ClassDescriptor.tpDate: { DateTime d = (DateTime) f.GetValue(obj); buf.Extend(dst + 8); //UPGRADE_TODO: The 'System.DateTime' structure does not have an equivalent to NULL. //UPGRADE_TODO: Method 'java.util.Date.getTime' was converted to 'DateTime.Ticks' which has a different behavior. Bytes.Pack8(buf.arr, dst, d == null ? -1 : d.Ticks); dst += 8; break; } case ClassDescriptor.tpFloat: buf.Extend(dst + 4); Bytes.PackF4(buf.arr, dst, (float) f.GetValue(obj)); dst += 4; break; case ClassDescriptor.tpDouble: buf.Extend(dst + 8); Bytes.PackF8(buf.arr, dst, (double) f.GetValue(obj)); dst += 8; break; case ClassDescriptor.tpString: { buf.Extend(dst + 4); string str = (string) f.GetValue(obj); if (str != null) { int len = str.Length; Bytes.Pack4(buf.arr, dst, len); dst += 4; buf.Extend(dst + len * 2); for (int j = 0; j < len; j++) { Bytes.Pack2(buf.arr, dst, (short) str[j]); dst += 2; } } else { Bytes.Pack4(buf.arr, dst, 0); dst += 4; } break; } case ClassDescriptor.tpArrayOfByte: { buf.Extend(dst + 4); byte[] arr = (byte[]) f.GetValue(obj); if (arr != null) { int len = arr.Length; Bytes.Pack4(buf.arr, dst, len); dst += 4; buf.Extend(dst + len); Array.Copy(arr, 0, buf.arr, dst, len); dst += len; } else { Bytes.Pack4(buf.arr, dst, 0); dst += 4; } break; } default: Assert.Failed("Invalid type"); break; } } return new Key(buf.ToArray()); } catch (System.Exception x) { throw new StorageError(StorageError.ACCESS_VIOLATION, x); } }
internal Key CreateCompoundKey(int[] types, string[] values) { ByteBuffer buf = new ByteBuffer(); int dst = 0; try { for (int i = 0; i < types.Length; i++) { string val = values[i]; switch (types[i]) { case ClassDescriptor.tpBoolean: buf.Extend(dst + 1); buf.arr[dst++] = (byte) (Int32.Parse(val) != 0 ? 1 : 0); break; case ClassDescriptor.tpByte: buf.Extend(dst + 1); buf.arr[dst++] = (byte) System.SByte.Parse(val); break; case ClassDescriptor.tpChar: buf.Extend(dst + 2); Bytes.Pack2(buf.arr, dst, (short) Int32.Parse(val)); dst += 2; break; case ClassDescriptor.tpShort: buf.Extend(dst + 2); Bytes.Pack2(buf.arr, dst, System.Int16.Parse(val)); dst += 2; break; case ClassDescriptor.tpInt: buf.Extend(dst + 4); Bytes.Pack4(buf.arr, dst, Int32.Parse(val)); dst += 4; break; case ClassDescriptor.tpObject: buf.Extend(dst + 4); Bytes.Pack4(buf.arr, dst, MapId(Int32.Parse(val))); dst += 4; break; case ClassDescriptor.tpLong: case ClassDescriptor.tpDate: buf.Extend(dst + 8); Bytes.Pack8(buf.arr, dst, Int64.Parse(val)); dst += 8; break; case ClassDescriptor.tpFloat: buf.Extend(dst + 4); Bytes.PackF4(buf.arr, dst, Single.Parse(val)); dst += 4; break; case ClassDescriptor.tpDouble: buf.Extend(dst + 8); Bytes.PackF8(buf.arr, dst, Double.Parse(val)); dst += 8; break; case ClassDescriptor.tpString: dst = buf.PackString(dst, val, storage.encoding); break; case ClassDescriptor.tpArrayOfByte: buf.Extend(dst + 4 + (SupportClass.URShift(val.Length, 1))); Bytes.Pack4(buf.arr, dst, SupportClass.URShift(val.Length, 1)); dst += 4; for (int j = 0, n = val.Length; j < n; j += 2) { buf.arr[dst++] = (byte) ((GetHexValue(val[j]) << 4) | GetHexValue(val[j + 1])); } break; default: ThrowException("Bad key type"); break; } } } catch (FormatException) { ThrowException("Failed to convert key value"); } return new Key(buf.ToArray()); }