📄 btreemultifieldindex.cs
字号:
v = oid == 0 ? null : ((StorageImpl)Storage).lookupObject(oid, null);
offs += 4;
break;
}
case ClassDescriptor.FieldType.tpLong:
v = Bytes.unpack8(data, offs);
offs += 8;
break;
case ClassDescriptor.FieldType.tpDate:
{
v = new DateTime(Bytes.unpack8(data, offs));
offs += 8;
break;
}
case ClassDescriptor.FieldType.tpULong:
v = (ulong)Bytes.unpack8(data, offs);
offs += 8;
break;
case ClassDescriptor.FieldType.tpFloat:
v = Bytes.unpackF4(data, offs);
offs += 4;
break;
case ClassDescriptor.FieldType.tpDouble:
v = Bytes.unpackF8(data, offs);
offs += 8;
break;
case ClassDescriptor.FieldType.tpDecimal:
v = Bytes.unpackDecimal(data, offs);
offs += 16;
break;
case ClassDescriptor.FieldType.tpGuid:
v = Bytes.unpackGuid(data, offs);
offs += 16;
break;
case ClassDescriptor.FieldType.tpString:
{
int len = Bytes.unpack4(data, offs);
offs += 4;
char[] sval = new char[len];
for (int j = 0; j < len; j++)
{
sval[j] = (char) Bytes.unpack2(pg.data, offs);
offs += 2;
}
v = new String(sval);
break;
}
case ClassDescriptor.FieldType.tpArrayOfByte:
{
int len = Bytes.unpack4(data, offs);
offs += 4;
byte[] val = new byte[len];
Array.Copy(pg.data, offs, val, 0, len);
offs += len;
v = val;
break;
}
default:
Debug.Assert(false, "Invalid type");
break;
}
values[i] = v;
}
return values;
}
private Key extractKey(IPersistent obj)
{
ByteBuffer buf = new ByteBuffer(null);
int dst = 0;
for (int i = 0; i < types.Length; i++)
{
object val = mbr[i] is FieldInfo ? ((FieldInfo)mbr[i]).GetValue(obj) : ((PropertyInfo)mbr[i]).GetValue(obj, null);
dst = packKeyPart(buf, dst, types[i], val);
}
return new Key(buf.toArray());
}
private Key convertKey(Key key)
{
if (key == null)
{
return null;
}
if (key.type != ClassDescriptor.FieldType.tpArrayOfObject)
{
throw new StorageError(StorageError.ErrorCode.INCOMPATIBLE_KEY_TYPE);
}
Object[] values = (Object[])key.oval;
ByteBuffer buf = new ByteBuffer(null);
int dst = 0;
for (int i = 0; i < values.Length; i++)
{
dst = packKeyPart(buf, dst, types[i], values[i]);
}
return new Key(buf.toArray(), key.inclusion != 0);
}
private int packKeyPart(ByteBuffer buf, int dst, ClassDescriptor.FieldType type, object val)
{
switch (type)
{
case ClassDescriptor.FieldType.tpBoolean:
dst = buf.packBool(dst, (bool)val);
break;
case ClassDescriptor.FieldType.tpByte:
dst = buf.packI1(dst, (byte)val);
break;
case ClassDescriptor.FieldType.tpSByte:
dst = buf.packI1(dst, (sbyte)val);
break;
case ClassDescriptor.FieldType.tpShort:
dst = buf.packI2(dst, (short)val);
break;
case ClassDescriptor.FieldType.tpUShort:
dst = buf.packI2(dst, (ushort)val);
break;
case ClassDescriptor.FieldType.tpChar:
dst = buf.packI2(dst, (char)val);
break;
case ClassDescriptor.FieldType.tpInt:
case ClassDescriptor.FieldType.tpOid:
case ClassDescriptor.FieldType.tpEnum:
dst = buf.packI4(dst, (int)val);
break;
case ClassDescriptor.FieldType.tpUInt:
dst = buf.packI4(dst, (int)(uint)val);
break;
case ClassDescriptor.FieldType.tpObject:
if (val != null) {
IPersistent obj = (IPersistent)val;
if (!obj.IsPersistent())
{
Storage.MakePersistent(obj);
}
dst = buf.packI4(dst, obj.Oid);
} else {
dst = buf.packI4(dst, 0);
}
break;
case ClassDescriptor.FieldType.tpLong:
dst = buf.packI8(dst, (long)val);
break;
case ClassDescriptor.FieldType.tpULong:
dst = buf.packI8(dst, (long)(ulong)val);
break;
case ClassDescriptor.FieldType.tpDate:
dst = buf.packDate(dst, (DateTime)val);
break;
case ClassDescriptor.FieldType.tpFloat:
dst = buf.packF4(dst, (float)val);
break;
case ClassDescriptor.FieldType.tpDouble:
dst = buf.packF8(dst, (double)val);
break;
case ClassDescriptor.FieldType.tpDecimal:
dst = buf.packDecimal(dst, (decimal)val);
break;
case ClassDescriptor.FieldType.tpGuid:
dst = buf.packGuid(dst, (Guid)val);
break;
case ClassDescriptor.FieldType.tpString:
dst = buf.packString(dst, (string)val);
break;
case ClassDescriptor.FieldType.tpArrayOfByte:
buf.extend(dst+4);
if (val != null)
{
byte[] arr = (byte[])val;
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:
Debug.Assert(false, "Invalid type");
break;
}
return dst;
}
#if USE_GENERICS
public bool Put(V obj)
#else
public bool Put(IPersistent obj)
#endif
{
return base.Put(extractKey(obj), obj);
}
#if USE_GENERICS
public V Set(V obj)
#else
public IPersistent Set(IPersistent obj)
#endif
{
return base.Set(extractKey(obj), obj);
}
#if USE_GENERICS
public override bool Remove(V obj)
#else
public bool Remove(IPersistent obj)
#endif
{
try
{
base.remove(new BtreeKey(extractKey(obj), obj.Oid));
}
catch (StorageError x)
{
if (x.Code == StorageError.ErrorCode.KEY_NOT_FOUND)
{
return false;
}
throw x;
}
return true;
}
#if USE_GENERICS
public override V Remove(Key key)
#else
public override IPersistent Remove(Key key)
#endif
{
return base.Remove(convertKey(key));
}
#if USE_GENERICS
public override bool Contains(V obj)
#else
public bool Contains(IPersistent obj)
#endif
{
Key key = extractKey(obj);
if (unique)
{
return base.Get(key) != null;
}
else
{
IPersistent[] mbrs = Get(key, key);
for (int i = 0; i < mbrs.Length; i++)
{
if (mbrs[i] == obj)
{
return true;
}
}
return false;
}
}
#if USE_GENERICS
public void Append(V obj)
#else
public void Append(IPersistent obj)
#endif
{
throw new StorageError(StorageError.ErrorCode.UNSUPPORTED_INDEX_TYPE);
}
#if !USE_GENERICS
public override IPersistent[] Get(Key from, Key till)
{
return base.Get(convertKey(from), convertKey(till));
}
public override IPersistent[] ToArray()
{
IPersistent[] arr = (IPersistent[])Array.CreateInstance(cls, nElems);
if (root != 0)
{
BtreePage.traverseForward((StorageImpl)Storage, root, type, height, arr, 0);
}
return arr;
}
#else
public override V[] Get(Key from, Key till)
{
return base.Get(convertKey(from), convertKey(till));
}
#endif
#if USE_GENERICS
public override V Get(Key key)
#else
public override IPersistent Get(Key key)
#endif
{
return base.Get(convertKey(key));
}
#if USE_GENERICS
public override IEnumerable<V> Range(Key from, Key till, IterationOrder order)
#else
public override IEnumerable Range(Key from, Key till, IterationOrder order)
#endif
{
return base.Range(convertKey(from), convertKey(till), order);
}
public override IDictionaryEnumerator GetDictionaryEnumerator(Key from, Key till, IterationOrder order)
{
return base.GetDictionaryEnumerator(convertKey(from), convertKey(till), order);
}
#if !USE_GENERICS
public IEnumerable Select(string predicate)
{
Query query = new QueryImpl(Storage);
return query.Select(cls, this, predicate);
}
#endif
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -