⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlimporter.cs

📁 Perst开源实时数据库
💻 CS
📖 第 1 页 / 共 5 页
字号:
            }
            storage.assignOid(btree, oid);
			
            while ((tkn = scanner.scan()) == XMLScanner.Token.LT)
            {
                if (scanner.scan() != XMLScanner.Token.IDENT || !scanner.Identifier.Equals("ref"))
                {
                    throwException("<ref> element expected");
                }
                XMLElement refElem = readElement("ref");
                Key key;
                if (fieldNames != null) 
                { 
                    String[] values = new String[fieldNames.Length];                
#if USE_GENERICS
                    types = btree.FieldTypes;
#else
                    types = ((BtreeMultiFieldIndex)btree).types;
#endif
                    for (int i = 0; i < values.Length; i++) 
                    { 
                        values[i] = getAttribute(refElem, "key"+i);
                    }
                    key = createCompoundKey(types, values);
                } 
                else if (types != null) 
                { 
                    String[] values = new String[fieldNames.Length];                
                    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(storage, mapId(getIntAttribute(refElem, "id")));
                btree.insert(key, obj, false);
            }
            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 USE_GENERICS
            ByteBuffer buf = new ByteBuffer(storage.encoding);
            buf.extend(ObjectHeader.Sizeof);
            int size = storage.packObject(btree, desc, ObjectHeader.Sizeof, buf, null, false);
            byte[] data = buf.arr;
            ObjectHeader.setSize(data, 0, size);
            ObjectHeader.setType(data, 0, desc.Oid);
#else
            byte[] data = storage.packObject(btree, false);
            int size = ObjectHeader.getSize(data, 0);
#endif
            long pos = storage.allocate(size, 0);
            storage.setPos(oid, pos | StorageImpl.dbModifiedFlag);
			
            storage.pool.put(pos & ~ StorageImpl.dbFlagsMask, data, size);
        }
		
        internal void  createObject(XMLElement elem)
        {
            ClassDescriptor desc = storage.getClassDescriptor(findClassByName(elem.Name));
            int oid = mapId(getIntAttribute(elem, "id"));
            ByteBuffer buf = new ByteBuffer(storage.encoding);
            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 = storage.allocate(offs, 0);
            storage.setPos(oid, pos | StorageImpl.dbModifiedFlag);
            storage.pool.put(pos, buf.arr, offs);
        }
		
        internal int getHexValue(char ch)
        {
            if (ch >= '0' && ch <= '9')
            {
                return ch - '0';
            }
            else if (ch >= 'A' && ch <= 'F')
            {
                return ch - 'A' + 10;
            }
            else if (ch >= 'a' && ch <= 'f')
            {
                return ch - 'a' + 10;
            }
            else
            {
                throwException("Bad hexadecimal constant");
            }
            return - 1;
        }
		
        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;
        }

        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 COMPACT_NET_FRAMEWORK
                                    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);
                            }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -