extend() публичный Метод

public extend ( int size ) : void
size int
Результат void
        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:
                dst = buf.packI4(dst, val != null ? (int)((IPersistent)val).Oid : 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);
        }
Пример #2
0
        internal void createIndex(String indexType)
        {
            XMLScanner.Token tkn;
            int oid = 0;
            bool unique = false;
            String className = null;
            String fieldName = null;
            String[] fieldNames = null;
            long autoinc = 0;
            String type = null;
            while ((tkn = scanner.scan()) == XMLScanner.Token.IDENT)
            {
                System.String attrName = scanner.Identifier;
                if (scanner.scan() != XMLScanner.Token.EQ || scanner.scan() != XMLScanner.Token.SCONST)
                {
                    throwException("Attribute value expected");
                }
                System.String attrValue = scanner.String;
                if (attrName.Equals("id"))
                {
                    oid = mapId(parseInt(attrValue));
                }
                else if (attrName.Equals("unique"))
                {
                    unique = parseInt(attrValue) != 0;
                }
                else if (attrName.Equals("class"))
                {
                    className = attrValue;
                }
                else if (attrName.Equals("type"))
                {
                    type = attrValue;
                }
                else if (attrName.Equals("autoinc"))
                {
                    autoinc = parseInt(attrValue);
                }
                else if (attrName.StartsWith("field"))
                {
                    int len = attrName.Length;
                    if (len == 5)
                    {
                        fieldName = attrValue;
                    }
                    else
                    {
                        int fieldNo = Int32.Parse(attrName.Substring(5));
                        if (fieldNames == null || fieldNames.Length <= fieldNo)
                        {
                            String[] newFieldNames = new String[fieldNo + 1];
                            if (fieldNames != null)
                            {
                                Array.Copy(fieldNames, 0, newFieldNames, 0, fieldNames.Length);
                            }
                            fieldNames = newFieldNames;
                        }
                        fieldNames[fieldNo] = attrValue;
                    }
                }
            }
            if (tkn != XMLScanner.Token.GT)
            {
                throwException("Unclosed element tag");
            }
            if (oid == 0)
            {
                throwException("ID is not specified or index");
            }
            ClassDescriptor desc = db.getClassDescriptor(findClassByName(indexType));
            #if WITH_OLD_BTREE
            OldBtree btree = (OldBtree)desc.newInstance();
            if (className != null)
            {
                Type cls = findClassByName(className);
                if (fieldName != null)
                {
                    btree.init(cls, ClassDescriptor.FieldType.tpLast, new string[] { fieldName }, unique, autoinc);
                }
                else if (fieldNames != null)
                {
                    btree.init(cls, ClassDescriptor.FieldType.tpLast, fieldNames, unique, autoinc);
                }
                else
                {
                    throwException("Field name is not specified for field index");
                }
            }
            else
            {
                if (type == null)
                {
                    if (indexType.StartsWith("Volante.Impl.PersistentSet"))
                    {
                    }
                    else
                    {
                        throwException("Key type is not specified for index");
                    }
                }
                else
                {
                    if (indexType.StartsWith("Volante.impl.BitIndexImpl"))
                    {
                    }
                    else
                    {
                        btree.init(null, mapType(type), null, unique, autoinc);
                    }
                }
            }
            db.assignOid(btree, oid);
            #endif

            while ((tkn = scanner.scan()) == XMLScanner.Token.LT)
            {
                if (scanner.scan() != XMLScanner.Token.IDENT || !scanner.Identifier.Equals("ref"))
                {
                    throwException("<ref> element expected");
                }
            #if WITH_OLD_BTREE
                XMLElement refElem = readElement("ref");
                Key key;
                if (fieldNames != null)
                {
                    String[] values = new String[fieldNames.Length];
                    ClassDescriptor.FieldType[] types = btree.FieldTypes;
                    for (int i = 0; i < values.Length; i++)
                    {
                        values[i] = getAttribute(refElem, "key" + i);
                    }
                    key = createCompoundKey(types, values);
                }
                else
                {
                    key = createKey(btree.FieldType, getAttribute(refElem, "key"));
                }
                IPersistent obj = new PersistentStub(db, mapId(getIntAttribute(refElem, "id")));
                btree.insert(key, obj, false);
            #endif
            }
            if (tkn != XMLScanner.Token.LTS
                || scanner.scan() != XMLScanner.Token.IDENT
                || !scanner.Identifier.Equals(indexType)
                || scanner.scan() != XMLScanner.Token.GT)
            {
                throwException("Element is not closed");
            }
            #if WITH_OLD_BTREE
            ByteBuffer buf = new ByteBuffer();
            buf.extend(ObjectHeader.Sizeof);
            int size = db.packObject(btree, desc, ObjectHeader.Sizeof, buf, null);
            byte[] data = buf.arr;
            ObjectHeader.setSize(data, 0, size);
            ObjectHeader.setType(data, 0, desc.Oid);
            long pos = db.allocate(size, 0);
            db.setPos(oid, pos | DatabaseImpl.dbModifiedFlag);

            db.pool.put(pos & ~DatabaseImpl.dbFlagsMask, data, size);
            #endif
        }
Пример #3
0
        internal int packObject(XMLElement objElem, ClassDescriptor desc, int offs, ByteBuffer buf)
        {
            ClassDescriptor.FieldDescriptor[] flds = desc.allFields;
            for (int i = 0, n = flds.Length; i < n; i++)
            {
                ClassDescriptor.FieldDescriptor fd = flds[i];
                FieldInfo f = fd.field;
                String fieldName = fd.fieldName;
                XMLElement elem = (objElem != null) ? objElem.getSibling(fieldName) : null;

                switch (fd.type)
                {
                    case ClassDescriptor.FieldType.tpByte:
                    case ClassDescriptor.FieldType.tpSByte:
                        buf.extend(offs + 1);
                        if (elem != null)
                        {
                            if (elem.isIntValue())
                            {
                                buf.arr[offs] = (byte)elem.IntValue;
                            }
                            else if (elem.isRealValue())
                            {
                                buf.arr[offs] = (byte)elem.RealValue;
                            }
                            else
                            {
                                throwException("Conversion for field " + fieldName + " is not possible");
                            }
                        }
                        offs += 1;
                        continue;

                    case ClassDescriptor.FieldType.tpBoolean:
                        buf.extend(offs + 1);
                        if (elem != null)
                        {
                            if (elem.isIntValue())
                            {
                                buf.arr[offs] = (byte)(elem.IntValue != 0 ? 1 : 0);
                            }
                            else if (elem.isRealValue())
                            {
                                buf.arr[offs] = (byte)(elem.RealValue != 0.0 ? 1 : 0);
                            }
                            else
                            {
                                throwException("Conversion for field " + fieldName + " is not possible");
                            }
                        }
                        offs += 1;
                        continue;

                    case ClassDescriptor.FieldType.tpShort:
                    case ClassDescriptor.FieldType.tpUShort:
                    case ClassDescriptor.FieldType.tpChar:
                        buf.extend(offs + 2);
                        if (elem != null)
                        {
                            if (elem.isIntValue())
                            {
                                Bytes.pack2(buf.arr, offs, (short)elem.IntValue);
                            }
                            else if (elem.isRealValue())
                            {
                                Bytes.pack2(buf.arr, offs, (short)elem.RealValue);
                            }
                            else
                            {
                                throwException("Conversion for field " + fieldName + " is not possible");
                            }
                        }
                        offs += 2;
                        continue;

                    case ClassDescriptor.FieldType.tpEnum:
                        buf.extend(offs + 4);
                        if (elem != null)
                        {
                            if (elem.isIntValue())
                            {
                                Bytes.pack4(buf.arr, offs, (int)elem.IntValue);
                            }
                            else if (elem.isRealValue())
                            {
                                Bytes.pack4(buf.arr, offs, (int)elem.RealValue);
                            }
                            else if (elem.isStringValue())
                            {
                                try
                                {
            #if CF
                                    Bytes.pack4(buf.arr, offs, (int)ClassDescriptor.parseEnum(f.FieldType, elem.StringValue));
            #else
                                    Bytes.pack4(buf.arr, offs, (int)Enum.Parse(f.FieldType, elem.StringValue));
            #endif
                                }
                                catch (ArgumentException)
                                {
                                    throwException("Invalid enum value");
                                }
                            }
                            else
                            {
                                throwException("Conversion for field " + fieldName + " is not possible");
                            }
                        }
                        offs += 4;
                        continue;

                    case ClassDescriptor.FieldType.tpInt:
                    case ClassDescriptor.FieldType.tpUInt:
                        buf.extend(offs + 4);
                        if (elem != null)
                        {
                            if (elem.isIntValue())
                            {
                                Bytes.pack4(buf.arr, offs, (int)elem.IntValue);
                            }
                            else if (elem.isRealValue())
                            {
                                Bytes.pack4(buf.arr, offs, (int)elem.RealValue);
                            }
                            else
                            {
                                throwException("Conversion for field " + fieldName + " is not possible");
                            }
                        }
                        offs += 4;
                        continue;

                    case ClassDescriptor.FieldType.tpLong:
                    case ClassDescriptor.FieldType.tpULong:
                        buf.extend(offs + 8);
                        if (elem != null)
                        {
                            if (elem.isIntValue())
                            {
                                Bytes.pack8(buf.arr, offs, elem.IntValue);
                            }
                            else if (elem.isRealValue())
                            {
                                Bytes.pack8(buf.arr, offs, (long)elem.RealValue);
                            }
                            else
                            {
                                throwException("Conversion for field " + fieldName + " is not possible");
                            }
                        }
                        offs += 8;
                        continue;

                    case ClassDescriptor.FieldType.tpFloat:
                        buf.extend(offs + 4);
                        if (elem != null)
                        {
                            if (elem.isIntValue())
                            {
                                Bytes.packF4(buf.arr, offs, (float)elem.IntValue);
                            }
                            else if (elem.isRealValue())
                            {
                                Bytes.packF4(buf.arr, offs, (float)elem.RealValue);
                            }
                            else
                            {
                                throwException("Conversion for field " + fieldName + " is not possible");
                            }
                        }
                        offs += 4;
                        continue;

                    case ClassDescriptor.FieldType.tpDouble:
                        buf.extend(offs + 8);
                        if (elem != null)
                        {
                            if (elem.isIntValue())
                            {
                                Bytes.packF8(buf.arr, offs, (double)elem.IntValue);
                            }
                            else if (elem.isRealValue())
                            {
                                Bytes.packF8(buf.arr, offs, elem.RealValue);
                            }
                            else
                            {
                                throwException("Conversion for field " + fieldName + " is not possible");
                            }
                        }
                        offs += 8;
                        continue;

                    case ClassDescriptor.FieldType.tpDecimal:
                        buf.extend(offs + 16);
                        if (elem != null)
                        {
                            decimal d = 0;
                            if (elem.isIntValue())
                            {
                                d = elem.IntValue;
                            }
                            else if (elem.isRealValue())
                            {
                                d = (decimal)elem.RealValue;
                            }
                            else if (elem.isStringValue())
                            {
                                try
                                {
                                    d = Decimal.Parse(elem.StringValue);
                                }
                                catch (FormatException)
                                {
                                    throwException("Invalid date");
                                }
                            }
                            else
                            {
                                throwException("Conversion for field " + fieldName + " is not possible");
                            }
                            Bytes.packDecimal(buf.arr, offs, d);

                        }
                        offs += 16;
                        continue;

                    case ClassDescriptor.FieldType.tpGuid:
                        buf.extend(offs + 16);
                        if (elem != null)
                        {
                            if (elem.isStringValue())
                            {
                                Guid guid = new Guid(elem.StringValue);
                                byte[] bits = guid.ToByteArray();
                                Array.Copy(bits, 0, buf.arr, offs, 16);
                            }
                            else
                            {
                                throwException("Conversion for field " + fieldName + " is not possible");
                            }
                        }
                        offs += 16;
                        continue;

                    case ClassDescriptor.FieldType.tpDate:
                        buf.extend(offs + 8);
                        if (elem != null)
                        {
                            if (elem.isIntValue())
                            {
                                Bytes.pack8(buf.arr, offs, elem.IntValue);
                            }
                            else if (elem.isNullValue())
                            {
                                Bytes.pack8(buf.arr, offs, -1);
                            }
                            else if (elem.isStringValue())
                            {
                                try
                                {
                                    Bytes.packDate(buf.arr, offs, DateTime.Parse(elem.StringValue));
                                }
                                catch (FormatException)
                                {
                                    throwException("Invalid date");
                                }
                            }
                            else
                            {
                                throwException("Conversion for field " + fieldName + " is not possible");
                            }
                        }
                        offs += 8;
                        continue;

                    case ClassDescriptor.FieldType.tpString:
                        if (elem != null)
                        {
                            System.String val = null;
                            if (elem.isIntValue())
                            {
                                val = System.Convert.ToString(elem.IntValue);
                            }
                            else if (elem.isRealValue())
                            {
                                val = elem.RealValue.ToString();
                            }
                            else if (elem.isStringValue())
                            {
                                val = elem.StringValue;
                            }
                            else if (elem.isNullValue())
                            {
                                val = null;
                            }
                            else
                            {
                                throwException("Conversion for field " + fieldName + " is not possible");
                            }
                            offs = buf.packString(offs, val);
                            continue;
                        }
                        buf.extend(offs + 4);
                        Bytes.pack4(buf.arr, offs, -1);
                        offs += 4;
                        continue;

                    case ClassDescriptor.FieldType.tpOid:
                    case ClassDescriptor.FieldType.tpObject:
                        {
                            int oid = 0;
                            if (elem != null)
                            {
                                XMLElement refElem = elem.getSibling("ref");
                                if (refElem == null)
                                {
                                    throwException("<ref> element expected");
                                }
                                oid = mapId(getIntAttribute(refElem, "id"));
                            }
                            buf.extend(offs + 4);
                            Bytes.pack4(buf.arr, offs, oid);
                            offs += 4;
                            continue;
                        }

                    case ClassDescriptor.FieldType.tpValue:
                        offs = packObject(elem, fd.valueDesc, offs, buf);
                        continue;

                    case ClassDescriptor.FieldType.tpRaw:
                    case ClassDescriptor.FieldType.tpArrayOfByte:
                    case ClassDescriptor.FieldType.tpArrayOfSByte:
                        offs = importBinary(elem, offs, buf, fieldName);
                        continue;

                    case ClassDescriptor.FieldType.tpArrayOfBoolean:
                        if (elem == null || elem.isNullValue())
                        {
                            buf.extend(offs + 4);
                            Bytes.pack4(buf.arr, offs, -1);
                            offs += 4;
                        }
                        else
                        {
                            XMLElement item = elem.getSibling("element");
                            int len = (item == null) ? 0 : item.Counter;
                            buf.extend(offs + 4 + len);
                            Bytes.pack4(buf.arr, offs, len);
                            offs += 4;
                            while (--len >= 0)
                            {
                                if (item.isIntValue())
                                {
                                    buf.arr[offs] = (byte)(item.IntValue != 0 ? 1 : 0);
                                }
                                else if (item.isRealValue())
                                {
                                    buf.arr[offs] = (byte)(item.RealValue != 0.0 ? 1 : 0);
                                }
                                else
                                {
                                    throwException("Conversion for field " + fieldName + " is not possible");
                                }
                                item = item.NextSibling;
                                offs += 1;
                            }
                        }
                        continue;

                    case ClassDescriptor.FieldType.tpArrayOfChar:
                    case ClassDescriptor.FieldType.tpArrayOfShort:
                    case ClassDescriptor.FieldType.tpArrayOfUShort:
                        if (elem == null || elem.isNullValue())
                        {
                            buf.extend(offs + 4);
                            Bytes.pack4(buf.arr, offs, -1);
                            offs += 4;
                        }
                        else
                        {
                            XMLElement item = elem.getSibling("element");
                            int len = (item == null) ? 0 : item.Counter;
                            buf.extend(offs + 4 + len * 2);
                            Bytes.pack4(buf.arr, offs, len);
                            offs += 4;
                            while (--len >= 0)
                            {
                                if (item.isIntValue())
                                {
                                    Bytes.pack2(buf.arr, offs, (short)item.IntValue);
                                }
                                else if (item.isRealValue())
                                {
                                    Bytes.pack2(buf.arr, offs, (short)item.RealValue);
                                }
                                else
                                {
                                    throwException("Conversion for field " + fieldName + " is not possible");
                                }
                                item = item.NextSibling;
                                offs += 2;
                            }
                        }
                        continue;

                    case ClassDescriptor.FieldType.tpArrayOfEnum:
                        if (elem == null || elem.isNullValue())
                        {
                            buf.extend(offs + 4);
                            Bytes.pack4(buf.arr, offs, -1);
                            offs += 4;
                        }
                        else
                        {
                            XMLElement item = elem.getSibling("element");
                            int len = (item == null) ? 0 : item.Counter;
                            Type elemType = f.FieldType.GetElementType();
                            buf.extend(offs + 4 + len * 4);
                            Bytes.pack4(buf.arr, offs, len);
                            offs += 4;
                            while (--len >= 0)
                            {
                                if (item.isIntValue())
                                {
                                    Bytes.pack4(buf.arr, offs, (int)item.IntValue);
                                }
                                else if (item.isRealValue())
                                {
                                    Bytes.pack4(buf.arr, offs, (int)item.RealValue);
                                }
                                else if (item.isStringValue())
                                {
                                    try
                                    {
            #if CF
                                        Bytes.pack4(buf.arr, offs, (int)ClassDescriptor.parseEnum(elemType, item.StringValue));
            #else
                                        Bytes.pack4(buf.arr, offs, (int)Enum.Parse(elemType, item.StringValue));
            #endif
                                    }
                                    catch (ArgumentException)
                                    {
                                        throwException("Invalid enum value");
                                    }
                                }
                                else
                                {
                                    throwException("Conversion for field " + fieldName + " is not possible");
                                }
                                item = item.NextSibling;
                                offs += 4;
                            }
                        }
                        continue;

                    case ClassDescriptor.FieldType.tpArrayOfInt:
                    case ClassDescriptor.FieldType.tpArrayOfUInt:
                        if (elem == null || elem.isNullValue())
                        {
                            buf.extend(offs + 4);
                            Bytes.pack4(buf.arr, offs, -1);
                            offs += 4;
                        }
                        else
                        {
                            XMLElement item = elem.getSibling("element");
                            int len = (item == null) ? 0 : item.Counter;
                            buf.extend(offs + 4 + len * 4);
                            Bytes.pack4(buf.arr, offs, len);
                            offs += 4;
                            while (--len >= 0)
                            {
                                if (item.isIntValue())
                                {
                                    Bytes.pack4(buf.arr, offs, (int)item.IntValue);
                                }
                                else if (item.isRealValue())
                                {
                                    Bytes.pack4(buf.arr, offs, (int)item.RealValue);
                                }
                                else
                                {
                                    throwException("Conversion for field " + fieldName + " is not possible");
                                }
                                item = item.NextSibling;
                                offs += 4;
                            }
                        }
                        continue;

                    case ClassDescriptor.FieldType.tpArrayOfLong:
                    case ClassDescriptor.FieldType.tpArrayOfULong:
                        if (elem == null || elem.isNullValue())
                        {
                            buf.extend(offs + 4);
                            Bytes.pack4(buf.arr, offs, -1);
                            offs += 4;
                        }
                        else
                        {
                            XMLElement item = elem.getSibling("element");
                            int len = (item == null) ? 0 : item.Counter;
                            buf.extend(offs + 4 + len * 8);
                            Bytes.pack4(buf.arr, offs, len);
                            offs += 4;
                            while (--len >= 0)
                            {
                                if (item.isIntValue())
                                {
                                    Bytes.pack8(buf.arr, offs, item.IntValue);
                                }
                                else if (item.isRealValue())
                                {
                                    Bytes.pack8(buf.arr, offs, (long)item.RealValue);
                                }
                                else
                                {
                                    throwException("Conversion for field " + fieldName + " is not possible");
                                }
                                item = item.NextSibling;
                                offs += 8;
                            }
                        }
                        continue;

                    case ClassDescriptor.FieldType.tpArrayOfFloat:
                        if (elem == null || elem.isNullValue())
                        {
                            buf.extend(offs + 4);
                            Bytes.pack4(buf.arr, offs, -1);
                            offs += 4;
                        }
                        else
                        {
                            XMLElement item = elem.getSibling("element");
                            int len = (item == null) ? 0 : item.Counter;
                            buf.extend(offs + 4 + len * 4);
                            Bytes.pack4(buf.arr, offs, len);
                            offs += 4;
                            while (--len >= 0)
                            {
                                if (item.isIntValue())
                                {
                                    Bytes.packF4(buf.arr, offs, (float)item.IntValue);
                                }
                                else if (item.isRealValue())
                                {
                                    Bytes.packF4(buf.arr, offs, (float)item.RealValue);
                                }
                                else
                                {
                                    throwException("Conversion for field " + fieldName + " is not possible");
                                }
                                item = item.NextSibling;
                                offs += 4;
                            }
                        }
                        continue;

                    case ClassDescriptor.FieldType.tpArrayOfDouble:
                        if (elem == null || elem.isNullValue())
                        {
                            buf.extend(offs + 4);
                            Bytes.pack4(buf.arr, offs, -1);
                            offs += 4;
                        }
                        else
                        {
                            XMLElement item = elem.getSibling("element");
                            int len = (item == null) ? 0 : item.Counter;
                            buf.extend(offs + 4 + len * 8);
                            Bytes.pack4(buf.arr, offs, len);
                            offs += 4;
                            while (--len >= 0)
                            {
                                if (item.isIntValue())
                                {
                                    Bytes.packF8(buf.arr, offs, (double)item.IntValue);
                                }
                                else if (item.isRealValue())
                                {
                                    Bytes.packF8(buf.arr, offs, item.RealValue);
                                }
                                else
                                {
                                    throwException("Conversion for field " + fieldName + " is not possible");
                                }
                                item = item.NextSibling;
                                offs += 8;
                            }
                        }
                        continue;

                    case ClassDescriptor.FieldType.tpArrayOfDate:
                        if (elem == null || elem.isNullValue())
                        {
                            buf.extend(offs + 4);
                            Bytes.pack4(buf.arr, offs, -1);
                            offs += 4;
                        }
                        else
                        {
                            XMLElement item = elem.getSibling("element");
                            int len = (item == null) ? 0 : item.Counter;
                            buf.extend(offs + 4 + len * 8);
                            Bytes.pack4(buf.arr, offs, len);
                            offs += 4;
                            while (--len >= 0)
                            {
                                if (item.isNullValue())
                                {
                                    Bytes.pack8(buf.arr, offs, -1);
                                }
                                else if (item.isStringValue())
                                {
                                    try
                                    {
                                        Bytes.packDate(buf.arr, offs, DateTime.Parse(item.StringValue));
                                    }
                                    catch (FormatException)
                                    {
                                        throwException("Conversion for field " + fieldName + " is not possible");
                                    }
                                }
                                item = item.NextSibling;
                                offs += 8;
                            }
                        }
                        continue;

                    case ClassDescriptor.FieldType.tpArrayOfDecimal:
                        if (elem == null || elem.isNullValue())
                        {
                            buf.extend(offs + 4);
                            Bytes.pack4(buf.arr, offs, -1);
                            offs += 4;
                        }
                        else
                        {
                            XMLElement item = elem.getSibling("element");
                            int len = (item == null) ? 0 : item.Counter;
                            buf.extend(offs + 4 + len * 16);
                            Bytes.pack4(buf.arr, offs, len);
                            offs += 4;
                            while (--len >= 0)
                            {
                                if (item.isStringValue())
                                {
                                    try
                                    {
                                        Bytes.packDecimal(buf.arr, offs, Decimal.Parse(item.StringValue));
                                    }
                                    catch (FormatException)
                                    {
                                        throwException("Conversion for field " + fieldName + " is not possible");
                                    }
                                }
                                item = item.NextSibling;
                                offs += 16;
                            }
                        }
                        continue;

                    case ClassDescriptor.FieldType.tpArrayOfGuid:
                        if (elem == null || elem.isNullValue())
                        {
                            buf.extend(offs + 4);
                            Bytes.pack4(buf.arr, offs, -1);
                            offs += 4;
                        }
                        else
                        {
                            XMLElement item = elem.getSibling("element");
                            int len = (item == null) ? 0 : item.Counter;
                            buf.extend(offs + 4 + len * 16);
                            Bytes.pack4(buf.arr, offs, len);
                            offs += 4;
                            while (--len >= 0)
                            {
                                if (item.isStringValue())
                                {
                                    try
                                    {
                                        Bytes.packGuid(buf.arr, offs, new Guid(item.StringValue));
                                    }
                                    catch (FormatException)
                                    {
                                        throwException("Conversion for field " + fieldName + " is not possible");
                                    }
                                }
                                item = item.NextSibling;
                                offs += 16;
                            }
                        }
                        continue;

                    case ClassDescriptor.FieldType.tpArrayOfString:
                        if (elem == null || elem.isNullValue())
                        {
                            buf.extend(offs + 4);
                            Bytes.pack4(buf.arr, offs, -1);
                            offs += 4;
                        }
                        else
                        {
                            XMLElement item = elem.getSibling("element");
                            int len = (item == null) ? 0 : item.Counter;
                            buf.extend(offs + 4);
                            Bytes.pack4(buf.arr, offs, len);
                            offs += 4;
                            while (--len >= 0)
                            {
                                System.String val = null;
                                if (item.isIntValue())
                                {
                                    val = System.Convert.ToString(item.IntValue);
                                }
                                else if (item.isRealValue())
                                {
                                    val = item.RealValue.ToString();
                                }
                                else if (item.isStringValue())
                                {
                                    val = item.StringValue;
                                }
                                else if (item.isNullValue())
                                {
                                    val = null;
                                }
                                else
                                {
                                    throwException("Conversion for field " + fieldName + " is not possible");
                                }
                                offs = buf.packString(offs, val);
                                item = item.NextSibling;
                            }
                        }
                        continue;

                    case ClassDescriptor.FieldType.tpArrayOfObject:
                    case ClassDescriptor.FieldType.tpArrayOfOid:
                    case ClassDescriptor.FieldType.tpLink:
                        if (elem == null || elem.isNullValue())
                        {
                            buf.extend(offs + 4);
                            Bytes.pack4(buf.arr, offs, -1);
                            offs += 4;
                        }
                        else
                        {
                            XMLElement item = elem.getSibling("element");
                            int len = (item == null) ? 0 : item.Counter;
                            buf.extend(offs + 4 + len * 4);
                            Bytes.pack4(buf.arr, offs, len);
                            offs += 4;
                            while (--len >= 0)
                            {
                                XMLElement href = item.getSibling("ref");
                                if (href == null)
                                {
                                    throwException("<ref> element expected");
                                }
                                int oid = mapId(getIntAttribute(href, "id"));
                                Bytes.pack4(buf.arr, offs, oid);
                                item = item.NextSibling;
                                offs += 4;
                            }
                        }
                        continue;

                    case ClassDescriptor.FieldType.tpArrayOfValue:
                        if (elem == null || elem.isNullValue())
                        {
                            buf.extend(offs + 4);
                            Bytes.pack4(buf.arr, offs, -1);
                            offs += 4;
                        }
                        else
                        {
                            XMLElement item = elem.getSibling("element");
                            int len = (item == null) ? 0 : item.Counter;
                            Bytes.pack4(buf.arr, offs, len);
                            offs += 4;
                            ClassDescriptor elemDesc = fd.valueDesc;
                            while (--len >= 0)
                            {
                                offs = packObject(item, elemDesc, offs, buf);
                                item = item.NextSibling;
                            }
                        }
                        continue;

                    case ClassDescriptor.FieldType.tpArrayOfRaw:
                        if (elem == null || elem.isNullValue())
                        {
                            buf.extend(offs + 4);
                            Bytes.pack4(buf.arr, offs, -1);
                            offs += 4;
                        }
                        else
                        {
                            XMLElement item = elem.getSibling("element");
                            int len = (item == null) ? 0 : item.Counter;
                            Bytes.pack4(buf.arr, offs, len);
                            offs += 4;
                            while (--len >= 0)
                            {
                                offs = importBinary(item, offs, buf, fieldName);
                                item = item.NextSibling;
                            }
                        }
                        continue;
                }
            }
            return offs;
        }
Пример #4
0
 internal int importBinary(XMLElement elem, int offs, ByteBuffer buf, String fieldName)
 {
     if (elem == null || elem.isNullValue())
     {
         buf.extend(offs + 4);
         Bytes.pack4(buf.arr, offs, -1);
         offs += 4;
     }
     else if (elem.isStringValue())
     {
         String hexStr = elem.StringValue;
         int len = hexStr.Length;
         if (hexStr.StartsWith("#"))
         {
             buf.extend(offs + 4 + len / 2 - 1);
             Bytes.pack4(buf.arr, offs, -2 - getHexValue(hexStr[1]));
             offs += 4;
             for (int j = 2; j < len; j += 2)
             {
                 buf.arr[offs++] = (byte)((getHexValue(hexStr[j]) << 4) | getHexValue(hexStr[j + 1]));
             }
         }
         else
         {
             buf.extend(offs + 4 + len / 2);
             Bytes.pack4(buf.arr, offs, len / 2);
             offs += 4;
             for (int j = 0; j < len; j += 2)
             {
                 buf.arr[offs++] = (byte)((getHexValue(hexStr[j]) << 4) | getHexValue(hexStr[j + 1]));
             }
         }
     }
     else
     {
         XMLElement refElem = elem.getSibling("ref");
         if (refElem != null)
         {
             buf.extend(offs + 4);
             Bytes.pack4(buf.arr, offs, mapId(getIntAttribute(refElem, "id")));
             offs += 4;
         }
         else
         {
             XMLElement item = elem.getSibling("element");
             int len = (item == null) ? 0 : item.Counter;
             buf.extend(offs + 4 + len);
             Bytes.pack4(buf.arr, offs, len);
             offs += 4;
             while (--len >= 0)
             {
                 if (item.isIntValue())
                 {
                     buf.arr[offs] = (byte)item.IntValue;
                 }
                 else if (item.isRealValue())
                 {
                     buf.arr[offs] = (byte)item.RealValue;
                 }
                 else
                 {
                     throwException("Conversion for field " + fieldName + " is not possible");
                 }
                 item = item.NextSibling;
                 offs += 1;
             }
         }
     }
     return offs;
 }
Пример #5
0
        internal void createObject(XMLElement elem)
        {
            ClassDescriptor desc = db.getClassDescriptor(findClassByName(elem.Name));
            int oid = mapId(getIntAttribute(elem, "id"));
            ByteBuffer buf = new ByteBuffer();
            int offs = ObjectHeader.Sizeof;
            buf.extend(offs);

            offs = packObject(elem, desc, offs, buf);

            ObjectHeader.setSize(buf.arr, 0, offs);
            ObjectHeader.setType(buf.arr, 0, desc.Oid);

            long pos = db.allocate(offs, 0);
            db.setPos(oid, pos | DatabaseImpl.dbModifiedFlag);
            db.pool.put(pos, buf.arr, offs);
        }
Пример #6
0
        Key createCompoundKey(ClassDescriptor.FieldType[] types, String[] values)
        {
            ByteBuffer buf = new ByteBuffer();
            int dst = 0;

            for (int i = 0; i < types.Length; i++)
            {
                String val = values[i];
                switch (types[i])
                {
                    case ClassDescriptor.FieldType.tpBoolean:
                        dst = buf.packBool(dst, Int32.Parse(val) != 0);
                        break;

                    case ClassDescriptor.FieldType.tpByte:
                    case ClassDescriptor.FieldType.tpSByte:
                        dst = buf.packI1(dst, Int32.Parse(val));
                        break;

                    case ClassDescriptor.FieldType.tpChar:
                    case ClassDescriptor.FieldType.tpShort:
                    case ClassDescriptor.FieldType.tpUShort:
                        dst = buf.packI2(dst, Int32.Parse(val));
                        break;

                    case ClassDescriptor.FieldType.tpInt:
                        dst = buf.packI4(dst, Int32.Parse(val));
                        break;

                    case ClassDescriptor.FieldType.tpEnum:
                    case ClassDescriptor.FieldType.tpUInt:
                        dst = buf.packI4(dst, (int)UInt32.Parse(val));
                        break;

                    case ClassDescriptor.FieldType.tpObject:
                    case ClassDescriptor.FieldType.tpOid:
                        dst = buf.packI4(dst, mapId((int)UInt32.Parse(val)));
                        break;

                    case ClassDescriptor.FieldType.tpLong:
                        dst = buf.packI8(dst, Int64.Parse(val));
                        break;

                    case ClassDescriptor.FieldType.tpULong:
                        dst = buf.packI8(dst, (long)UInt64.Parse(val));
                        break;

                    case ClassDescriptor.FieldType.tpDate:
                        dst = buf.packDate(dst, DateTime.Parse(val));
                        break;

                    case ClassDescriptor.FieldType.tpFloat:
                        dst = buf.packF4(dst, Single.Parse(val));
                        break;

                    case ClassDescriptor.FieldType.tpDouble:
                        dst = buf.packF8(dst, Double.Parse(val));
                        break;

                    case ClassDescriptor.FieldType.tpDecimal:
                        dst = buf.packDecimal(dst, Decimal.Parse(val));
                        break;

                    case ClassDescriptor.FieldType.tpGuid:
                        dst = buf.packGuid(dst, new Guid(val));
                        break;

                    case ClassDescriptor.FieldType.tpString:
                        dst = buf.packString(dst, val);
                        break;

                    case ClassDescriptor.FieldType.tpArrayOfByte:
                        buf.extend(dst + 4 + (val.Length >> 1));
                        Bytes.pack4(buf.arr, dst, 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;
                }
            }
            return new Key(buf.toArray());
        }