Inheritance: IPersistent
Ejemplo n.º 1
0
 public void Unpin()
 {
     for (int i = 0, n = used; i < n; i++)
     {
         IPersistent elem = arr[i];
         if (elem != null && !elem.IsRaw() && elem.IsPersistent())
         {
             arr[i] = new PersistentStub(elem.Database, elem.Oid);
         }
     }
 }
Ejemplo n.º 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
        }