public ServerHistoryProvider( CharacterName characterName, IPersistent<PersistentModel.ServerHistory> persistentData, IWurmLogsMonitorInternal logsMonitor, IWurmLogsHistory logsSearcher, IWurmServerList wurmServerList, ILogger logger, IWurmCharacterLogFiles wurmCharacterLogFiles) { if (characterName == null) throw new ArgumentNullException("characterName"); if (persistentData == null) throw new ArgumentNullException("persistentData"); if (logsMonitor == null) throw new ArgumentNullException("logsMonitor"); if (logsSearcher == null) throw new ArgumentNullException("logsSearcher"); if (wurmServerList == null) throw new ArgumentNullException("wurmServerList"); if (logger == null) throw new ArgumentNullException("logger"); if (wurmCharacterLogFiles == null) throw new ArgumentNullException("wurmCharacterLogFiles"); this.characterName = characterName; this.sortedServerHistory = new SortedServerHistory(persistentData); this.persistentData = persistentData; this.logsMonitor = logsMonitor; this.logsSearcher = logsSearcher; this.wurmServerList = wurmServerList; this.logger = logger; this.wurmCharacterLogFiles = wurmCharacterLogFiles; logsMonitor.SubscribeInternal(characterName, LogType.Event, HandleEventLogEntries); }
public V Set(Key key, V obj) { IPersistent s = index.Get(key); if (s == null) { Relation <V, V> r = Database.CreateRelation <V, V>(null); r.Add(obj); index.Put(key, r); nElems += 1; Modify(); return(null); } else if (s is Relation <V, V> ) { Relation <V, V> r = (Relation <V, V>)s; if (r.Count == 1) { V prev = r[0]; r[0] = obj; return(prev); } } throw new DatabaseException(DatabaseException.ErrorCode.KEY_NOT_UNIQUE); }
public void Invalidate() { while (true) { lock (this) { for (int i = 0; i < table.Length; i++) { for (Entry e = table[i]; e != null; e = e.next) { IPersistent obj = (IPersistent)e.oref.Target; if (obj != null) { if (obj.IsModified()) { e.dirty = 0; unpinObject(e); obj.Invalidate(); } } else if (e.dirty != 0) { goto waitFinalization; } } table[i] = null; } count = 0; return; } waitFinalization: GC.WaitForPendingFinalizers(); } }
public void AddElement(string name, IPersistent child) { if (child != null) { element.AppendChild(child.ToXml(name, doc)); } }
/// <summary> /// Sets the value of the field defined by info on the object o /// to the value value. If the fields aren't of the same type /// it will be silently ignored. /// </summary> private void TrySetValue(IPersistent o, FieldInfo info, Object value) { if (value.GetType() == info.FieldType) { info.SetValue(o, value); } }
IPersistent loadItem(int i) { IPersistent mbr = item[i]; mbr.Load(); return(mbr); }
/// <summary> Project specified selection</summary> /// <param name="selection">array with selected object /// </param> public virtual void Project(IPersistent[] selection) { for (int i = 0; i < selection.Length; i++) { Map(selection[i]); } }
public virtual int IndexOf(IPersistent obj) { int oid; if (obj != null && (oid = ((IPersistent)obj).Oid) != 0) { for (int i = used; --i >= 0;) { IPersistent elem = arr[i]; if (elem != null && elem.Oid == oid) { return(i); } } } else { for (int i = used; --i >= 0;) { if (arr[i] == obj) { return(i); } } } return(-1); }
/// <summary> /// Sets the value of the property defined by info on the object o /// to the value value. If the fields aren't of the same type /// it will be silently ignored. /// </summary> static void TrySetValue(IPersistent o, PropertyInfo info, Object value) { if (value.GetType() == info.PropertyType) { info.SetValue(o, value, null); } }
/// <summary> /// Initializes fields and properties of the object o, with /// data of the specified category and of the specified id. /// </summary> /// <param name="o">Object to get its fields initialized.</param> /// <param name="type"> /// The category this object belongs, all objects /// of the same category will get the same values. /// </param> /// <param name="id"> /// The id of the object which identifies it inside /// its category, it /// </param> public void InitializeObject(IPersistent o) { var t = o.GetType(); var ids = o.Ids.Split(CommaDelimiter, StringSplitOptions.RemoveEmptyEntries); // Hey, lock here, there might be other threads // trying to initialize their values also. lock (_data) { foreach (var id in ids) { if (!_data.ContainsKey(id)) // Do we have something for this id? { continue; } // Iterate over what we have for this id foreach (var pair in _data[id]) { // Does o has a member we can write to? var members = t.GetMember(pair.Key); foreach (var info in members) { if (info is FieldInfo) { TrySetValue(o, (FieldInfo)info, _data[id][info.Name]); } else if (info is PropertyInfo) { TrySetValue(o, (PropertyInfo)info, _data[id][info.Name]); } } } } } }
public virtual void Append(IPersistent obj) { lock (this) { Key key; try { switch (type) { case ClassDescriptor.tpInt: key = new Key((int) autoincCount); fld.SetValue(obj, (int) autoincCount); break; case ClassDescriptor.tpLong: key = new Key(autoincCount); fld.SetValue(obj, autoincCount); break; default: throw new StorageError(StorageError.UNSUPPORTED_INDEX_TYPE, fld.FieldType); } } catch (System.Exception x) { throw new StorageError(StorageError.ACCESS_VIOLATION, x); } autoincCount += 1; obj.Modify(); base.Insert(key, obj, false); } }
public void Flush() { while (true) { lock (this) { for (int i = 0; i < table.Length; i++) { for (Entry e = table[i]; e != null; e = e.next) { IPersistent obj = (IPersistent)e.oref.Target; if (obj != null) { if (obj.IsModified()) { obj.Store(); } } else if (e.dirty != 0) { goto waitFinalization; } } } return; } waitFinalization: GC.WaitForPendingFinalizers(); } }
public virtual void Put(IPersistent obj, int mask) { StorageImpl db = (StorageImpl) Storage; if (db == null) { throw new StorageError(StorageError.DELETED_OBJECT); } if (!obj.IsPersistent()) { db.MakePersistent(obj); } Key ins = new Key(mask, obj.Oid); if (root == 0) { root = BitIndexPage.Allocate(db, 0, ins); height = 1; } else { int result = BitIndexPage.Insert(db, root, ins, height); if (result == op_overflow) { root = BitIndexPage.Allocate(db, root, ins); height += 1; } } updateCounter += 1; nElems += 1; Modify(); }
public void Put(int oid, IPersistent obj) { lock (this) { Entry[] tab = table; int index = (oid & 0x7FFFFFFF) % tab.Length; for (Entry e = tab[index]; e != null; e = e.next) { if (e.oid == oid) { e.oref.Target = obj; pinObject(e, obj); return; } } if (count >= threshold) { // Rehash the table if the threshold is exceeded rehash(); tab = table; index = (oid & 0x7FFFFFFF) % tab.Length; } // Creates the new entry. tab[index] = new Entry(oid, new WeakReference(obj), tab[index]); pinObject(tab[index], obj); count++; } }
public void Put(int oid, IPersistent obj) { lock (this) { Entry[] tab = table; int index = (oid & 0x7FFFFFFF) % tab.Length; for (Entry e = tab[index]; e != null; e = e.next) { if (e.oid == oid) { e.oref = obj; return; } } if (count >= threshold) { // Rehash the table if the threshold is exceeded rehash(); tab = table; index = (oid & 0x7FFFFFFF) % tab.Length; } // Creates the new entry. tab[index] = new Entry(oid, obj, tab[index]); count++; } }
public virtual void Set(int i, IPersistent obj) { if (i < 0 || i >= used) { throw new IndexOutOfRangeException(); } arr[i] = obj; }
public virtual int Get(IPersistent obj) { StorageImpl db = (StorageImpl) Storage; if (root == 0) throw new StorageError(StorageError.KEY_NOT_FOUND); return BitIndexPage.Find(db, root, obj.Oid, height); }
protected PersistentEntityBase(IPersistent <TEntity> persistent) { if (persistent == null) { throw new ArgumentNullException(nameof(persistent)); } this.persistent = persistent; }
protected virtual void InitParent( IPersistent parent ) { Check.VerifyNotNull( parent, Error.NullParameter, "parent" ); Check.Verify( parent.IsPersisted || ! parentMap.IsAutoGeneratedPrimaryKey, Error.DeveloperError, "The parent object must have been persisted before you can use the list." ); this.parent = parent; parentMap = ObjectFactory.GetMap( broker, parent ); }
public PersistentDataWithMigrationNoFilter(IPersistent <Dto> persistent) : base(persistent) { RunMigration(0, 1, dto => { dto.Data = TestValues.ValueAfterMigration; }); }
public SortedServerHistory(IPersistent<PersistentModel.ServerHistory> persistentData) { if (persistentData == null) throw new ArgumentNullException("persistentData"); this.persistentData = persistentData; Rebuild(persistentData.Entity.ServerStamps); }
public static void Initialize(this IPersistent persistent, Guid?currentUserId = null) { persistent.Oid = Guid.NewGuid(); persistent.UpdateUserId = currentUserId; persistent.UpdateTime = DateTime.UtcNow; persistent.AfterConstruction(); }
protected virtual void InitParent(IPersistent parent) { Check.VerifyNotNull(parent, Error.NullParameter, "parent"); Check.Verify(parent.IsPersisted || !parentMap.IsAutoGeneratedPrimaryKey, Error.DeveloperError, "The parent object must have been persisted before you can use the list."); this.parent = parent; parentMap = ObjectFactory.GetMap(broker, parent); }
public bool Add(IPersistent o) { if (!o.IsPersistent()) { ((StorageImpl)Storage).storeObject(o); } return(base.Put(new Key(o), o)); }
private void AddItem(IPersistent obj, DataGrid dataGrid) { Database.AddRecord(obj); // Adding new record to Database ((IList)dataGrid.ItemsSource).Insert(0, obj); dataGrid.SelectedItem = obj; ShowDetailPanel(dataGrid); ((DetailPanel)swDetail.Content).FocusFirstTextBox(); }
internal void linkAfter(Entry head, IPersistent obj) { mru = head.mru; mru.lru = this; head.mru = this; lru = head; pin = obj; }
private void AddItem(IPersistent obj, DataGrid dataGrid) { Database.AddRecord(obj); // Adding new record to Database ((IList)dataGrid.ItemsSource).Insert(0, obj); dataGrid.SelectedItem = obj; ShowDetailPanel(dataGrid); ((DetailPanel)swDetail.Content).FocusFirstTextBox(); }
public LogHistorySaved(IPersistent <ServersData> persistenceManager) { if (persistenceManager == null) { throw new ArgumentNullException(nameof(persistenceManager)); } this.persistenceManager = persistenceManager; }
public virtual IPersistent[] ToArray() { IPersistent[] a = new IPersistent[used]; for (int i = used; --i >= 0;) { a[i] = loadElem(i); } return(a); }
internal void reserveSpace(int len) { if (used + len > arr.Length) { IPersistent[] newArr = new IPersistent[used + len > arr.Length * 2?used + len:arr.Length * 2]; Array.Copy(arr, 0, newArr, 0, used); arr = newArr; } }
public static IPersistent CreateObject(XmlDocument doc, XmlElement element) { String typeName = element.Attributes["type"].Value; String assemblyName = element.Attributes["assembly"].Value; IPersistent persistent = (IPersistent)AssemblyCache.CreateObject(typeName, assemblyName); persistent.FromXml(element); return(persistent); }
private Key extractKey(IPersistent obj) { object[] keys = new object[mbr.Length]; for (int i = 0; i < keys.Length; i++) { keys[i] = mbr[i] is FieldInfo ? ((FieldInfo)mbr[i]).GetValue(obj) : ((PropertyInfo)mbr[i]).GetValue(obj, null); } return(new Key(new CompoundKey(keys))); }
public IPersistent[] ToArray() { IPersistent[] arr = new IPersistent[nMembers]; if (root != null) { root.toArray(arr, 0); } return(arr); }
void IDataMapper.Insert(IPersistent entity) { CustomSetting e = (CustomSetting)entity; bool ok = false; using (SqlConnection connection = new SqlConnection(ConnectionString)) { connection.Open(); SqlCommand command = connection.CreateCommand(); command.CommandType = CommandType.Text; command.CommandText = InsertCommandText; SqlParameter parameter = null; parameter = new SqlParameter("key", SqlDbType.UniqueIdentifier); parameter.Direction = ParameterDirection.Input; parameter.Value = e.identity; command.Parameters.Add(parameter); parameter = new SqlParameter("owner_", SqlDbType.Int); parameter.Direction = ParameterDirection.Input; parameter.Value = (e.owner == null) ? 0 : e.owner.TypeCode; command.Parameters.Add(parameter); parameter = new SqlParameter("owner", SqlDbType.UniqueIdentifier); parameter.Direction = ParameterDirection.Input; parameter.Value = (e.owner == null) ? Guid.Empty : e.owner.Identity; command.Parameters.Add(parameter); parameter = new SqlParameter("name", SqlDbType.NVarChar); parameter.Direction = ParameterDirection.Input; parameter.Value = (e.name == null) ? string.Empty : e.name; command.Parameters.Add(parameter); parameter = new SqlParameter("value", SqlDbType.NVarChar); parameter.Direction = ParameterDirection.Input; parameter.Value = (e._value == null) ? string.Empty : e._value; command.Parameters.Add(parameter); SqlDataReader reader = command.ExecuteReader(); if (reader.Read()) { e.version = (byte[])reader[0]; ok = true; } reader.Close(); connection.Close(); } if (!ok) { throw new ApplicationException("Error executing insert command."); } }
private T loadElem(int i) { IPersistent elem = arr[i]; if (elem != null && elem.IsRaw()) { elem = ((DatabaseImpl)elem.Database).lookupObject(elem.Oid, null); } return((T)elem); }
internal RtreePage(IPersistent obj, Rectangle r) { b = new Branch[card]; n = 1; b[0] = new Branch(r, obj); for (int i = 1; i < card; i++) { b[i] = new Branch(); } }
/// <summary> /// Each subscriber can call this method to include any data it wishes to persist. This data is intended to be user state metadata. It is not intended to be used for /// wholesale application database data. /// </summary> /// <param name="model"> /// The model to persist. The type of this model will be used as a key and when data is loaded back from persistent storage this key (type) will be used /// to retrieve it. No other component should use this type, or collisions will occur. /// </param> /// <exception cref="System.Data.DuplicateNameException">Attempt to save application state with a model that has already been saved.</exception> public void PersistThisModel(IPersistent model) { if (this.modelsToPersist.Any(m => m.GetType() == model.GetType())) { throw new DuplicateNameException( "Attempt to save application state with a model that has already been saved."); } this.modelsToPersist.Add(model); }
protected virtual void InitType( Type containedType, IPersistent parent, Type viaType ) { containedMap = ObjectFactory.GetMap( broker, containedType ); Check.Verify( containedType.GetInterface( "IPersistent", false ) != null, Error.UnsupportedType, containedType ); listType = parent == null ? GentleListType.StandAlone : (viaType == null ? GentleListType.OneToMany : GentleListType.ManyToMany); }
protected virtual void InitType(Type containedType, IPersistent parent, Type viaType) { containedMap = ObjectFactory.GetMap(broker, containedType); Check.Verify(containedType.GetInterface("IPersistent", false) != null, Error.UnsupportedType, containedType); listType = parent == null ? GentleListType.StandAlone : (viaType == null ? GentleListType.OneToMany : GentleListType.ManyToMany); }
public SortedServerHistory(IPersistent <PersistentModel.ServerHistory> persistentData) { if (persistentData == null) { throw new ArgumentNullException(nameof(persistentData)); } this.persistentData = persistentData; Rebuild(persistentData.Entity.ServerStamps); }
internal IPersistent remove(ulong key, int keyLength) { if (keyLength >= this.keyLength) { if (key == this.key && keyLength == this.keyLength) { IPersistent obj = this.obj; this.obj = null; return(obj); } else { int keyLengthCommon = getCommonPart(key, keyLength, this.key, this.keyLength); int keyLengthDiff = keyLength - keyLengthCommon; ulong keyCommon = key >> keyLengthDiff; ulong keyDiff = key - (keyCommon << keyLengthDiff); if (firstDigit(keyDiff, keyLengthDiff) == 1) { if (childOne != null) { IPersistent obj = childOne.findBestMatch(keyDiff, keyLengthDiff); if (obj != null) { if (childOne.isNotUsed()) { Modify(); childOne.Deallocate(); childOne = null; } return(obj); } } } else { if (childZero != null) { IPersistent obj = childZero.findBestMatch(keyDiff, keyLengthDiff); if (obj != null) { if (childZero.isNotUsed()) { Modify(); childZero.Deallocate(); childZero = null; } return(obj); } } } } } return(null); }
public CharacterMonthlyLogHeuristics(IPersistent<WurmCharacterLogsEntity> persistentData, MonthlyHeuristicsExtractorFactory monthlyHeuristicsExtractorFactory, IWurmCharacterLogFiles wurmCharacterLogFiles) { if (persistentData == null) throw new ArgumentNullException("persistentData"); if (monthlyHeuristicsExtractorFactory == null) throw new ArgumentNullException("monthlyHeuristicsExtractorFactory"); if (wurmCharacterLogFiles == null) throw new ArgumentNullException("wurmCharacterLogFiles"); this.persistentData = persistentData; this.monthlyHeuristicsExtractorFactory = monthlyHeuristicsExtractorFactory; this.wurmCharacterLogFiles = wurmCharacterLogFiles; }
internal RtreeR2Page(IDatabase db, IPersistent obj, RectangleR2 r) { branch = db.CreateLink<IPersistent>(card); branch.Length = card; b = new RectangleR2[card]; setBranch(0, new RectangleR2(r), obj); n = 1; for (int i = 1; i < card; i++) { b[i] = new RectangleR2(); } }
internal RtreePage AddBranch(Storage storage, Rectangle r, IPersistent obj) { if (n < card) { SetBranch(n++, r, obj); return null; } else { return SplitPage(storage, r, obj); } }
internal RtreePage(Storage storage, IPersistent obj, Rectangle r) { branch = storage.CreateLink(card); branch.Size = card; b = new Rectangle[card]; SetBranch(0, new Rectangle(r), obj); n = 1; for (int i = 1; i < card; i++) { b[i] = new Rectangle(); } }
/// <summary> /// Create a new list for storing the specified type of objects. /// </summary> public GentleRelation( PersistenceBroker broker, Type containedType, IPersistent parent, params Type[] relatedTypes ) : base( broker ) { Check.VerifyNotNull( relatedTypes, Error.NullParameter, "relatedTypes" ); /*Check.Verify( parent == null && relatedTypes.Length == 2 || parent != null && relatedTypes.Length == 1, Error.NotImplemented, "The GentleRelation class is currently only able to manage two-way relations." ); */ this.relatedTypes = relatedTypes; this.parent = parent; containedMap = ObjectFactory.GetMap( broker, containedType ); // note: "this." required on broker param or original null param might get passed relations = new GentleList( this.broker, containedType, parent ); // verify that the linked types are supported by Gentle foreach( Type relatedType in relatedTypes ) { Check.Verify( relatedType.IsSubclassOf( typeof(Persistent) ) || relatedType.GetInterface( "IPersistent" ) != null, Error.UnsupportedType, relatedType ); } }
public virtual bool Contains(IPersistent obj) { 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; } }
/// <summary> Add new member to collection</summary> /// <param name="obj">new member /// </param> /// <returns> <code>true</code> if object is successfully added in the index, /// <code>false</code> if collection was declared as unique and there is already member with such value /// of the key in the collection. /// </returns> public virtual bool Add(IPersistent obj) { TtreePage newRoot; if (root == null) { newRoot = new TtreePage(obj); } else { TtreePage.PageReference ref_Renamed = new TtreePage.PageReference(root); if (root.Insert(comparator, obj, unique, ref_Renamed) == TtreePage.NOT_UNIQUE) { return false; } newRoot = ref_Renamed.pg; } root = newRoot; nMembers += 1; Modify(); return true; }
public virtual void Remove(IPersistent obj) { StorageImpl db = (StorageImpl) Storage; if (db == null) { throw new StorageError(StorageError.DELETED_OBJECT); } if (root == 0) { throw new StorageError(StorageError.KEY_NOT_FOUND); } int result = BitIndexPage.Remove(db, root, obj.Oid, height); if (result == op_not_found) { throw new StorageError(StorageError.KEY_NOT_FOUND); } nElems -= 1; if (result == op_underflow) { Page pg = db.GetPage(root); if (BitIndexPage.GetItemsCount(pg) == 0) { int newRoot = 0; if (height != 1) { newRoot = BitIndexPage.GetItem(pg, BitIndexPage.maxItems - 1); } db.FreePage(root); root = newRoot; height -= 1; } db.pool.Unfix(pg); } updateCounter += 1; Modify(); }
internal Entry(int oid, IPersistent oref, Entry chain) { next = chain; this.oid = oid; this.oref = oref; }
internal virtual void Unpin() { Unlink(); lru = mru = null; pin = null; }
internal virtual void LinkAfter(Entry head, IPersistent obj) { mru = head.mru; mru.lru = this; head.mru = this; lru = head; pin = obj; }
private void PinObject(Entry e, IPersistent obj) { if (pinLimit != 0) { if (e.pin != null) { e.Unlink(); } else { if (nPinned == pinLimit) { pinList.lru.Unpin(); } else { nPinned += 1; } } e.LinkAfter(pinList, obj); } }
public virtual void Put(int oid, IPersistent obj) { lock (this) { WeakReference ref_Renamed = CreateReference(obj); Entry[] tab = table; int index = (oid & 0x7FFFFFFF) % tab.Length; for (Entry e = tab[index]; e != null; e = e.next) { if (e.oid == oid) { e.ref_Renamed = ref_Renamed; PinObject(e, obj); return; } } if (count >= threshold) { // Rehash the table if the threshold is exceeded Rehash(); tab = table; index = (oid & 0x7FFFFFFF) % tab.Length; } // Creates the new entry. tab[index] = new Entry(oid, ref_Renamed, tab[index]); PinObject(tab[index], obj); count++; } }
public virtual void LockObject(IPersistent obj) { if (useSerializableTransactions) { ThreadTransactionContext ctx = TransactionContext; if (ctx.nested != 0) { // serializable transaction ctx.locked.Add(obj); } } }
public virtual void LoadObject(IPersistent obj) { lock (this) { if (obj.IsRaw()) { LoadStub(obj.Oid, obj, obj.GetType()); } } }
public virtual void DeallocateObject(IPersistent obj) { lock (this) { lock (objectCache) { int oid = obj.Oid; if (oid == 0) { return; } long pos = GetPos(oid); objectCache.Remove(oid); int offs = (int) pos & (Page.pageSize - 1); if ((offs & (dbFreeHandleFlag | dbPageObjectFlag)) != 0) { throw new StorageError(StorageError.DELETED_OBJECT); } Page pg = pool.GetPage(pos - offs); offs &= ~ dbFlagsMask; int size = ObjectHeader.GetSize(pg.data, offs); pool.Unfix(pg); FreeId(oid); if ((pos & dbModifiedFlag) != 0) { Free(pos & ~ dbFlagsMask, size); } else { CloneBitmap(pos, size); } obj.AssignOid(this, 0, false); } } }
public virtual Relation CreateRelation(IPersistent owner) { return new RelationImpl(owner); }
private void StoreObject0(IPersistent obj) { obj.OnStore(); int oid = obj.Oid; bool newObject = false; if (oid == 0) { oid = AllocateId(); if (!obj.Deleted) { objectCache.Put(oid, obj); } obj.AssignOid(this, oid, false); newObject = true; } else if (obj.Modified) { objectCache.ClearDirty(oid); } byte[] data = PackObject(obj); long pos; int newSize = ObjectHeader.GetSize(data, 0); if (newObject || (pos = GetPos(oid)) == 0) { pos = Allocate(newSize, 0); SetPos(oid, pos | dbModifiedFlag); } else { int offs = (int) pos & (Page.pageSize - 1); if ((offs & (dbFreeHandleFlag | dbPageObjectFlag)) != 0) { throw new StorageError(StorageError.DELETED_OBJECT); } Page pg = pool.GetPage(pos - offs); offs &= ~ dbFlagsMask; int size = ObjectHeader.GetSize(pg.data, offs); pool.Unfix(pg); if ((pos & dbModifiedFlag) == 0) { CloneBitmap(pos & ~ dbFlagsMask, size); pos = Allocate(newSize, 0); SetPos(oid, pos | dbModifiedFlag); } else { if (((newSize + dbAllocationQuantum - 1) & ~ (dbAllocationQuantum - 1)) > ((size + dbAllocationQuantum - 1) & ~ (dbAllocationQuantum - 1))) { long newPos = Allocate(newSize, 0); CloneBitmap(pos & ~ dbFlagsMask, size); Free(pos & ~ dbFlagsMask, size); pos = newPos; SetPos(oid, pos | dbModifiedFlag); } else if (newSize < size) { ObjectHeader.SetSize(data, 0, size); } } } modified = true; pool.Put(pos & ~ dbFlagsMask, data, newSize); }
internal int UnpackObject(object obj, ClassDescriptor desc, bool recursiveLoading, byte[] body, int offs, IPersistent po) { ClassDescriptor.FieldDescriptor[] all = desc.allFields; ReflectionProvider provider = ClassDescriptor.ReflectionProvider; int len; for (int i = 0, n = all.Length; i < n; i++) { ClassDescriptor.FieldDescriptor fd = all[i]; FieldInfo f = fd.field; if (f == null || obj == null) { switch (fd.type) { case ClassDescriptor.tpBoolean: case ClassDescriptor.tpByte: offs += 1; continue; case ClassDescriptor.tpChar: case ClassDescriptor.tpShort: offs += 2; continue; case ClassDescriptor.tpInt: case ClassDescriptor.tpFloat: case ClassDescriptor.tpObject: offs += 4; continue; case ClassDescriptor.tpLong: case ClassDescriptor.tpDouble: case ClassDescriptor.tpDate: offs += 8; continue; case ClassDescriptor.tpString: len = Bytes.Unpack4(body, offs); offs += 4; if (len > 0) { offs += len * 2; } else if (len < -1) { offs -= (len + 2); } continue; case ClassDescriptor.tpValue: offs = UnpackObject((object) null, fd.valueDesc, recursiveLoading, body, offs, po); continue; case ClassDescriptor.tpRaw: case ClassDescriptor.tpArrayOfByte: case ClassDescriptor.tpArrayOfBoolean: len = Bytes.Unpack4(body, offs); offs += 4; if (len > 0) { offs += len; } else if (len < -1) { offs += ClassDescriptor.Sizeof[-2 - len]; } continue; case ClassDescriptor.tpArrayOfShort: case ClassDescriptor.tpArrayOfChar: len = Bytes.Unpack4(body, offs); offs += 4; if (len > 0) { offs += len * 2; } continue; case ClassDescriptor.tpArrayOfInt: case ClassDescriptor.tpArrayOfFloat: case ClassDescriptor.tpArrayOfObject: case ClassDescriptor.tpLink: len = Bytes.Unpack4(body, offs); offs += 4; if (len > 0) { offs += len * 4; } continue; case ClassDescriptor.tpArrayOfLong: case ClassDescriptor.tpArrayOfDouble: case ClassDescriptor.tpArrayOfDate: len = Bytes.Unpack4(body, offs); offs += 4; if (len > 0) { offs += len * 8; } continue; case ClassDescriptor.tpArrayOfString: len = Bytes.Unpack4(body, offs); offs += 4; if (len > 0) { for (int j = 0; j < len; j++) { int strlen = Bytes.Unpack4(body, offs); offs += 4; if (strlen > 0) { offs += strlen * 2; } else if (strlen < -1) { offs -= (strlen + 2); } } } continue; case ClassDescriptor.tpArrayOfValue: len = Bytes.Unpack4(body, offs); offs += 4; if (len > 0) { ClassDescriptor valueDesc = fd.valueDesc; for (int j = 0; j < len; j++) { offs = UnpackObject((object) null, valueDesc, recursiveLoading, body, offs, po); } } continue; case ClassDescriptor.tpArrayOfRaw: len = Bytes.Unpack4(body, offs); offs += 4; if (len > 0) { for (int j = 0; j < len; j++) { int rawlen = Bytes.Unpack4(body, offs); offs += 4; if (rawlen > 0) { offs += rawlen; } else if (rawlen < -1) { offs += ClassDescriptor.Sizeof[-2 - rawlen]; } } } continue; } } else { switch (fd.type) { case ClassDescriptor.tpBoolean: provider.SetBoolean(f, obj, body[offs++] != 0); continue; case ClassDescriptor.tpByte: provider.SetByte(f, obj, body[offs++]); continue; case ClassDescriptor.tpChar: provider.SetChar(f, obj, (char) Bytes.Unpack2(body, offs)); offs += 2; continue; case ClassDescriptor.tpShort: provider.SetShort(f, obj, Bytes.Unpack2(body, offs)); offs += 2; continue; case ClassDescriptor.tpInt: provider.SetInt(f, obj, Bytes.Unpack4(body, offs)); offs += 4; continue; case ClassDescriptor.tpLong: provider.SetLong(f, obj, Bytes.Unpack8(body, offs)); offs += 8; continue; case ClassDescriptor.tpFloat: provider.SetFloat(f, obj, Bytes.UnpackF4(body, offs)); offs += 4; continue; case ClassDescriptor.tpDouble: provider.SetDouble(f, obj, Bytes.UnpackF8(body, offs)); offs += 8; continue; case ClassDescriptor.tpString: { len = Bytes.Unpack4(body, offs); offs += 4; string str = null; if (len >= 0) { char[] chars = new char[len]; for (int j = 0; j < len; j++) { chars[j] = (char) Bytes.Unpack2(body, offs); offs += 2; } str = new string(chars); } else if (len < -1) { if (encoding != null) { string tempStr; //UPGRADE_TODO: The differences in the Format of parameters for constructor 'java.lang.String.String' may cause compilation errors. tempStr = System.Text.Encoding.GetEncoding(encoding).GetString(body); str = new string(tempStr.ToCharArray(), offs, -2 - len); } else { str = new string(SupportClass.ToCharArray(body), offs, -2 - len); } offs -= (2 + len); } provider.Set(f, obj, str); continue; } case ClassDescriptor.tpDate: { long msec = Bytes.Unpack8(body, offs); offs += 8; //UPGRADE_TODO: The 'System.DateTime' structure does not have an equivalent to NULL. DateTime date; // = null; if (msec >= 0) { //UPGRADE_TODO: Constructor 'java.util.Date.Date' was converted to 'DateTime.DateTime' which has a different behavior. date = new DateTime(msec); } else date = new DateTime(); provider.Set(f, obj, date); continue; } case ClassDescriptor.tpObject: { provider.Set(f, obj, Unswizzle(Bytes.Unpack4(body, offs), f.FieldType, recursiveLoading)); offs += 4; continue; } case ClassDescriptor.tpValue: { object val = fd.valueDesc.NewInstance(); offs = UnpackObject(val, fd.valueDesc, recursiveLoading, body, offs, po); provider.Set(f, obj, val); continue; } case ClassDescriptor.tpRaw: len = Bytes.Unpack4(body, offs); offs += 4; if (len >= 0) { System.IO.MemoryStream streamIn = new System.IO.MemoryStream(body, offs, len); BinaryFormatter formatter = new BinaryFormatter(); object val = formatter.Deserialize(streamIn); provider.Set(f, obj, val); streamIn.Close(); offs += len; } else if (len < 0) { object val = null; switch (-2 - len) { case ClassDescriptor.tpBoolean: val = Convert.ToBoolean(body[offs++]); break; case ClassDescriptor.tpByte: val = (byte) body[offs++]; break; case ClassDescriptor.tpChar: val = (char) Bytes.Unpack2(body, offs); offs += 2; break; case ClassDescriptor.tpShort: val = (short) Bytes.Unpack2(body, offs); offs += 2; break; case ClassDescriptor.tpInt: val = (Int32) Bytes.Unpack4(body, offs); offs += 4; break; case ClassDescriptor.tpLong: val = (long) Bytes.Unpack8(body, offs); offs += 8; break; case ClassDescriptor.tpFloat: val = Bytes.UnpackF4(body, offs); break; case ClassDescriptor.tpDouble: val = Bytes.UnpackF8(body, offs); offs += 8; break; case ClassDescriptor.tpDate: //UPGRADE_TODO: Constructor 'java.util.Date.Date' was converted to 'DateTime.DateTime' which has a different behavior. val = new DateTime(Bytes.Unpack8(body, offs)); offs += 8; break; case ClassDescriptor.tpObject: val = Unswizzle(Bytes.Unpack4(body, offs), typeof(Persistent), recursiveLoading); offs += 4; break; } provider.Set(f, obj, val); } continue; case ClassDescriptor.tpArrayOfByte: len = Bytes.Unpack4(body, offs); offs += 4; if (len < 0) { provider.Set(f, obj, (object) null); } else { byte[] arr = new byte[len]; Array.Copy(body, offs, arr, 0, len); offs += len; provider.Set(f, obj, arr); } continue; case ClassDescriptor.tpArrayOfBoolean: len = Bytes.Unpack4(body, offs); offs += 4; if (len < 0) { provider.Set(f, obj, (object) null); } else { bool[] arr = new bool[len]; for (int j = 0; j < len; j++) { arr[j] = body[offs++] != 0; } provider.Set(f, obj, arr); } continue; case ClassDescriptor.tpArrayOfShort: len = Bytes.Unpack4(body, offs); offs += 4; if (len < 0) { provider.Set(f, obj, (object) null); } else { short[] arr = new short[len]; for (int j = 0; j < len; j++) { arr[j] = Bytes.Unpack2(body, offs); offs += 2; } provider.Set(f, obj, arr); } continue; case ClassDescriptor.tpArrayOfChar: len = Bytes.Unpack4(body, offs); offs += 4; if (len < 0) { provider.Set(f, obj, (object) null); } else { char[] arr = new char[len]; for (int j = 0; j < len; j++) { arr[j] = (char) Bytes.Unpack2(body, offs); offs += 2; } provider.Set(f, obj, arr); } continue; case ClassDescriptor.tpArrayOfInt: len = Bytes.Unpack4(body, offs); offs += 4; if (len < 0) { provider.Set(f, obj, (object) null); } else { int[] arr = new int[len]; for (int j = 0; j < len; j++) { arr[j] = Bytes.Unpack4(body, offs); offs += 4; } provider.Set(f, obj, arr); } continue; case ClassDescriptor.tpArrayOfLong: len = Bytes.Unpack4(body, offs); offs += 4; if (len < 0) { provider.Set(f, obj, (object) null); } else { long[] arr = new long[len]; for (int j = 0; j < len; j++) { arr[j] = Bytes.Unpack8(body, offs); offs += 8; } provider.Set(f, obj, arr); } continue; case ClassDescriptor.tpArrayOfFloat: len = Bytes.Unpack4(body, offs); offs += 4; if (len < 0) { provider.Set(f, obj, (object) null); } else { float[] arr = new float[len]; for (int j = 0; j < len; j++) { arr[j] = Bytes.UnpackF4(body, offs); offs += 4; } provider.Set(f, obj, arr); } continue; case ClassDescriptor.tpArrayOfDouble: len = Bytes.Unpack4(body, offs); offs += 4; if (len < 0) { provider.Set(f, obj, (object) null); } else { double[] arr = new double[len]; for (int j = 0; j < len; j++) { arr[j] = Bytes.UnpackF8(body, offs); offs += 8; } provider.Set(f, obj, arr); } continue; case ClassDescriptor.tpArrayOfDate: len = Bytes.Unpack4(body, offs); offs += 4; if (len < 0) { provider.Set(f, obj, (object) null); } else { DateTime[] arr = new DateTime[len]; for (int j = 0; j < len; j++) { long msec = Bytes.Unpack8(body, offs); offs += 8; if (msec >= 0) { //UPGRADE_TODO: Constructor 'java.util.Date.Date' was converted to 'DateTime.DateTime' which has a different behavior. arr[j] = new DateTime(msec); } } provider.Set(f, obj, arr); } continue; case ClassDescriptor.tpArrayOfString: len = Bytes.Unpack4(body, offs); offs += 4; if (len < 0) { provider.Set(f, obj, (object) null); } else { string[] arr = new string[len]; for (int j = 0; j < len; j++) { int strlen = Bytes.Unpack4(body, offs); offs += 4; if (strlen >= 0) { char[] chars = new char[strlen]; for (int k = 0; k < strlen; k++) { chars[k] = (char) Bytes.Unpack2(body, offs); offs += 2; } arr[j] = new string(chars); } else if (strlen < -1) { if (encoding != null) { string tempStr2; //UPGRADE_TODO: The differences in the Format of parameters for constructor 'java.lang.String.String' may cause compilation errors. tempStr2 = System.Text.Encoding.GetEncoding(encoding).GetString(body); arr[j] = new string(tempStr2.ToCharArray(), offs, -2 - strlen); } else { arr[j] = new string(SupportClass.ToCharArray(body), offs, -2 - strlen); } offs -= (2 + strlen); } } provider.Set(f, obj, arr); } continue; case ClassDescriptor.tpArrayOfObject: len = Bytes.Unpack4(body, offs); offs += 4; if (len < 0) { provider.Set(f, obj, (object) null); } else { Type elemType = f.FieldType.GetElementType(); IPersistent[] arr = (IPersistent[]) System.Array.CreateInstance(elemType, len); for (int j = 0; j < len; j++) { arr[j] = Unswizzle(Bytes.Unpack4(body, offs), elemType, recursiveLoading); offs += 4; } provider.Set(f, obj, arr); } continue; case ClassDescriptor.tpArrayOfValue: len = Bytes.Unpack4(body, offs); offs += 4; if (len < 0) { provider.Set(f, obj, (object) null); } else { Type elemType = f.FieldType.GetElementType(); object[] arr = (object[]) System.Array.CreateInstance(elemType, len); ClassDescriptor valueDesc = fd.valueDesc; for (int j = 0; j < len; j++) { object val = valueDesc.NewInstance(); offs = UnpackObject(val, valueDesc, recursiveLoading, body, offs, po); arr[j] = val; } provider.Set(f, obj, arr); } continue; case ClassDescriptor.tpArrayOfRaw: len = Bytes.Unpack4(body, offs); offs += 4; if (len < 0) { provider.Set(f, obj, (object) null); } else { Type elemType = f.FieldType.GetElementType(); object[] arr = (object[]) System.Array.CreateInstance(elemType, len); for (int j = 0; j < len; j++) { int rawlen = Bytes.Unpack4(body, offs); offs += 4; if (rawlen >= 0) { // TODOPORT: System.IO.MemoryStream streamIn = new System.IO.MemoryStream(body, offs, rawlen); //UPGRADE_TODO: Class 'java.io.ObjectInputStream' was converted to 'System.IO.BinaryReader' which has a different behavior. //System.IO.BinaryReader streamIn = new PersistentObjectInputStream(this, bin); //UPGRADE_WARNING: Method 'java.io.ObjectInputStream.readObject' was converted to 'SupportClass.Deserialize' which may throw an exception. BinaryFormatter formatter = new BinaryFormatter(); object val = formatter.Deserialize(streamIn); arr[j] = val; streamIn.Close(); offs += rawlen; } else { object val = null; switch (-2 - rawlen) { case ClassDescriptor.tpBoolean: val = Convert.ToBoolean(body[offs++]); break; case ClassDescriptor.tpByte: val = (byte) body[offs++]; break; case ClassDescriptor.tpChar: val = (char) Bytes.Unpack2(body, offs); offs += 2; break; case ClassDescriptor.tpShort: val = (short) Bytes.Unpack2(body, offs); offs += 2; break; case ClassDescriptor.tpInt: val = (Int32) Bytes.Unpack4(body, offs); offs += 4; break; case ClassDescriptor.tpLong: val = (long) Bytes.Unpack8(body, offs); offs += 8; break; case ClassDescriptor.tpFloat: val = Bytes.UnpackF4(body, offs); offs += 4; break; case ClassDescriptor.tpDouble: val = Bytes.UnpackF8(body, offs); offs += 8; break; case ClassDescriptor.tpDate: //UPGRADE_TODO: Constructor 'java.util.Date.Date' was converted to 'DateTime.DateTime' which has a different behavior. val = new DateTime(Bytes.Unpack8(body, offs)); offs += 8; break; case ClassDescriptor.tpObject: val = Unswizzle(Bytes.Unpack4(body, offs), typeof(Persistent), recursiveLoading); offs += 4; break; } arr[j] = val; } } provider.Set(f, obj, arr); } continue; case ClassDescriptor.tpLink: len = Bytes.Unpack4(body, offs); offs += 4; if (len < 0) { provider.Set(f, obj, (object) null); } else { IPersistent[] arr = new IPersistent[len]; for (int j = 0; j < len; j++) { int elemOid = Bytes.Unpack4(body, offs); offs += 4; if (elemOid != 0) { arr[j] = new PersistentStub(this, elemOid); } } provider.Set(f, obj, new LinkImpl(arr, po)); } break; } } } return offs; }