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 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 virtual bool Contains(object o) { if (o is IPersistent) { Key key = new Key((IPersistent) o); IEnumerator i = GetEnumerator(key, key, TenderBase.IndexSortOrder.Ascent); //UPGRADE_TODO: Method 'java.util.Iterator.hasNext' was converted to 'IEnumerator.MoveNext' which has a different behavior. return i.MoveNext(); } return false; }
public virtual IPersistent Get(Key key) { IPersistent s = index.Get(key); if (s == null) return null; if (s is Relation) { Relation r = (Relation) s; if (r.Size == 1) return r.Get(0); } throw new StorageError(StorageError.KEY_NOT_UNIQUE); }
internal static int CompareStr(Key key, Page pg, int i) { char[] chars = (char[]) key.oval; int alen = chars.Length; int blen = BtreePage.GetKeyStrSize(pg, i); int minlen = alen < blen ? alen : blen; int offs = BtreePage.GetKeyStrOffs(pg, i) + BtreePage.firstKeyOffs; byte[] b = pg.data; for (int j = 0; j < minlen; j++) { int diff = chars[j] - (char) Bytes.Unpack2(b, offs); if (diff != 0) { return diff; } offs += 2; } return alen - blen; }
public virtual IEnumerator GetEnumerator(Key from, Key till, IndexSortOrder order) { return new BtreeSelectionIterator(this, CheckKey(from), CheckKey(till), order); }
internal static int Compare(Key key, Page pg, int i) { long i8; int i4; float r4; double r8; switch (key.type) { case ClassDescriptor.tpBoolean: case ClassDescriptor.tpByte: return (byte) key.ival - pg.data[BtreePage.firstKeyOffs + i]; case ClassDescriptor.tpShort: return (short) key.ival - Bytes.Unpack2(pg.data, BtreePage.firstKeyOffs + i * 2); case ClassDescriptor.tpChar: return (char) key.ival - (char) Bytes.Unpack2(pg.data, BtreePage.firstKeyOffs + i * 2); case ClassDescriptor.tpObject: case ClassDescriptor.tpInt: i4 = Bytes.Unpack4(pg.data, BtreePage.firstKeyOffs + i * 4); return key.ival < i4 ? -1 : (key.ival == i4 ? 0 : 1); case ClassDescriptor.tpLong: case ClassDescriptor.tpDate: i8 = Bytes.Unpack8(pg.data, BtreePage.firstKeyOffs + i * 8); return key.lval < i8 ? -1 : (key.lval == i8 ? 0 : 1); case ClassDescriptor.tpFloat: r4 = Bytes.UnpackF4(pg.data, BtreePage.firstKeyOffs + i * 4); return key.dval < r4 ? -1 : (key.dval == r4 ? 0 : 1); case ClassDescriptor.tpDouble: r8 = Bytes.UnpackF8(pg.data, BtreePage.firstKeyOffs + i * 8); return key.dval < r8 ? -1 : (key.dval == r8 ? 0 : 1); } Assert.Failed("Invalid type"); return 0; }
private Key ConvertKey(Key key) { if (key == null) { return null; } if (key.type != ClassDescriptor.tpArrayOfObject) { throw new StorageError(StorageError.INCOMPATIBLE_KEY_TYPE); } object[] values = (object[]) key.oval; ByteBuffer buf = new ByteBuffer(); int dst = 0; for (int i = 0; i < values.Length; i++) { object v = values[i]; switch (types[i]) { case ClassDescriptor.tpBoolean: buf.Extend(dst + 1); buf.arr[dst++] = (byte) (((bool) v) ? 1 : 0); break; case ClassDescriptor.tpByte: buf.Extend(dst + 1); buf.arr[dst++] = (byte) Convert.ToSByte(((System.ValueType) v)); break; case ClassDescriptor.tpShort: buf.Extend(dst + 2); Bytes.Pack2(buf.arr, dst, Convert.ToInt16(((System.ValueType) v))); dst += 2; break; case ClassDescriptor.tpChar: buf.Extend(dst + 2); Bytes.Pack2(buf.arr, dst, (v is System.ValueType) ? Convert.ToInt16(((System.ValueType) v)) : (short) ((System.Char) v)); dst += 2; break; case ClassDescriptor.tpInt: buf.Extend(dst + 4); Bytes.Pack4(buf.arr, dst, Convert.ToInt32(((System.ValueType) v))); dst += 4; break; case ClassDescriptor.tpObject: buf.Extend(dst + 4); Bytes.Pack4(buf.arr, dst, v == null ? 0 : ((IPersistent) v).Oid); dst += 4; break; case ClassDescriptor.tpLong: buf.Extend(dst + 8); Bytes.Pack8(buf.arr, dst, Convert.ToInt64(((System.ValueType) v))); dst += 8; break; case ClassDescriptor.tpDate: buf.Extend(dst + 8); //UPGRADE_TODO: Method 'java.util.Date.getTime' was converted to 'DateTime.Ticks' which has a different behavior. Bytes.Pack8(buf.arr, dst, v == null ? -1 : ((DateTime) v).Ticks); dst += 8; break; case ClassDescriptor.tpFloat: buf.Extend(dst + 4); float f = (float)v; Bytes.PackF4(buf.arr, dst, f); dst += 4; break; case ClassDescriptor.tpDouble: buf.Extend(dst + 8); double d = (double)v; // TODOPORT: Convert.ToDouble(((System.ValueType) v) ? Bytes.PackF8(buf.arr, dst, d); dst += 8; break; case ClassDescriptor.tpString: { buf.Extend(dst + 4); if (v != null) { string str = (string) v; int len = str.Length; Bytes.Pack4(buf.arr, dst, len); dst += 4; buf.Extend(dst + len * 2); for (int j = 0; j < len; j++) { Bytes.Pack2(buf.arr, dst, (short) str[j]); dst += 2; } } else { Bytes.Pack4(buf.arr, dst, 0); dst += 4; } break; } case ClassDescriptor.tpArrayOfByte: { buf.Extend(dst + 4); if (v != null) { byte[] arr = (byte[]) v; 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: Assert.Failed("Invalid type"); break; } } return new Key(buf.ToArray(), key.inclusion != 0); }
public virtual long Remove(DateTime from, DateTime till) { long low = from.Ticks; long high = till.Ticks; long nRemoved = 0; Key fromKey = new Key(low - maxBlockTimeInterval); Key tillKey = new Key(high); IEnumerator blockIterator = index.GetEnumerator(fromKey, tillKey, IndexSortOrder.Ascent); //UPGRADE_TODO: Method 'java.util.Iterator.hasNext' was converted to 'IEnumerator.MoveNext' which has a different behavior. while (blockIterator.MoveNext()) { //UPGRADE_TODO: Method 'java.util.Iterator.next' was converted to 'IEnumerator.Current' which has a different behavior. TimeSeriesBlock block = (TimeSeriesBlock) blockIterator.Current; int n = block.used; TimeSeriesTick[] e = block.Ticks; int l = 0, r = n; while (l < r) { int i = (l + r) >> 1; if (low > e[i].Time) { l = i + 1; } else { r = i; } } Assert.That(l == r && (l == n || e[l].Time >= low)); while (r < n && e[r].Time <= high) { r += 1; nRemoved += 1; } if (l == 0 && r == n) { index.Remove(new Key(block.timestamp), block); blockIterator = index.GetEnumerator(fromKey, tillKey, IndexSortOrder.Ascent); block.Deallocate(); } else if (l < n && l != r) { if (l == 0) { index.Remove(new Key(block.timestamp), block); block.timestamp = e[r].Time; index.Put(new Key(block.timestamp), block); blockIterator = index.GetEnumerator(fromKey, tillKey, IndexSortOrder.Ascent); } while (r < n) { e[l++] = e[r++]; } block.used = l; block.Modify(); } } return nRemoved; }
public override IPersistent[] Get(Key from, Key till) { ArrayList list = new ArrayList(); if (root != null) { root.Find(ConvertKey(from), ConvertKey(till), height, list); } return (IPersistent[]) SupportClass.ICollectionSupport.ToArray(list, (object[]) System.Array.CreateInstance(cls, list.Count)); }
public override IEnumerator GetEnumerator(Key from, Key till, IndexSortOrder order) { return base.GetEnumerator(ConvertKey(from), ConvertKey(till), order); }
public virtual IPersistent Set(Key key, IPersistent obj) { return Insert(key, obj, true); }
internal virtual Key CheckKey(Key key) { if (key != null) { if (key.type != type) { throw new StorageError(StorageError.INCOMPATIBLE_KEY_TYPE); } if (type == ClassDescriptor.tpObject && key.ival == 0 && key.oval != null) { throw new StorageError(StorageError.INVALID_OID); } if (key.oval is char[]) { key = new Key(new string((char[]) key.oval), key.inclusion != 0); } } return key; }
public virtual void Remove(Key key, IPersistent obj) { Remove(new BtreeKey(CheckKey(key), obj)); }
public virtual IPersistent Remove(Key key) { if (!unique) { throw new StorageError(StorageError.KEY_NOT_UNIQUE); } BtreeKey rk = new BtreeKey(CheckKey(key), null); Remove(rk); return rk.oldNode; }
public virtual bool Put(Key key, IPersistent obj) { return Insert(key, obj, false) == null; }
internal BtreeSelectionIterator(AltBtree enclosingInstance, Key from, Key till, IndexSortOrder order) { InitBlock(enclosingInstance); int i, l, r; sp = 0; counter = Enclosing_Instance.updateCounter; if (Enclosing_Instance.height == 0) { return; } BtreePage page = Enclosing_Instance.root; int h = Enclosing_Instance.height; this.from = from; this.till = till; this.order = order; pageStack = new BtreePage[h]; posStack = new int[h]; if (order == TenderBase.IndexSortOrder.Ascent) { if (from == null) { while (--h > 0) { posStack[sp] = 0; pageStack[sp] = page; page = (BtreePage) page.items.Get(0); sp += 1; } posStack[sp] = 0; pageStack[sp] = page; end = page.nItems; sp += 1; } else { while (--h > 0) { pageStack[sp] = page; l = 0; r = page.nItems; while (l < r) { i = (l + r) >> 1; if (page.Compare(from, i) >= from.inclusion) { l = i + 1; } else { r = i; } } Assert.That(r == l); posStack[sp] = r; page = (BtreePage) page.items.Get(r); sp += 1; } pageStack[sp] = page; l = 0; r = end = page.nItems; while (l < r) { i = (l + r) >> 1; if (page.Compare(from, i) >= from.inclusion) { l = i + 1; } else { r = i; } } Assert.That(r == l); if (r == end) { sp += 1; GotoNextItem(page, r - 1); } else { posStack[sp++] = r; } } if (sp != 0 && till != null) { page = pageStack[sp - 1]; if (-page.Compare(till, posStack[sp - 1]) >= till.inclusion) { sp = 0; } } } else { // descent order if (till == null) { while (--h > 0) { pageStack[sp] = page; posStack[sp] = page.nItems; page = (BtreePage) page.items.Get(page.nItems); sp += 1; } pageStack[sp] = page; posStack[sp++] = page.nItems - 1; } else { while (--h > 0) { pageStack[sp] = page; l = 0; r = page.nItems; while (l < r) { i = (l + r) >> 1; if (page.Compare(till, i) >= 1 - till.inclusion) { l = i + 1; } else { r = i; } } Assert.That(r == l); posStack[sp] = r; page = (BtreePage) page.items.Get(r); sp += 1; } pageStack[sp] = page; l = 0; r = page.nItems; while (l < r) { i = (l + r) >> 1; if (page.Compare(till, i) >= 1 - till.inclusion) { l = i + 1; } else { r = i; } } Assert.That(r == l); if (r == 0) { sp += 1; GotoNextItem(page, r); } else { posStack[sp++] = r - 1; } } if (sp != 0 && from != null) { page = pageStack[sp - 1]; if (page.Compare(from, posStack[sp - 1]) >= from.inclusion) { sp = 0; } } } }
internal BtreeSelectionEntryIterator(AltBtree enclosingInstance, Key from, Key till, IndexSortOrder order) : base(enclosingInstance, from, till, order) { InitBlock(enclosingInstance); }
internal static bool Find(StorageImpl db, int pageId, Key firstKey, Key lastKey, Btree tree, int height, ArrayList result) { Page pg = db.GetPage(pageId); int l = 0, n = GetItemsCount(pg), r = n; int oid; height -= 1; try { if (tree.type == ClassDescriptor.tpString) { if (firstKey != null) { while (l < r) { int i = (l + r) >> 1; if (CompareStr(firstKey, pg, i) >= firstKey.inclusion) { l = i + 1; } else { r = i; } } Assert.That(r == l); } if (lastKey != null) { if (height == 0) { while (l < n) { if (-CompareStr(lastKey, pg, l) >= lastKey.inclusion) { return false; } oid = GetKeyStrOid(pg, l); result.Add(db.LookupObject(oid, null)); l += 1; } } else { do { if (!Find(db, GetKeyStrOid(pg, l), firstKey, lastKey, tree, height, result)) { return false; } if (l == n) { return true; } } while (CompareStr(lastKey, pg, l++) >= 0); return false; } } else { if (height == 0) { while (l < n) { oid = GetKeyStrOid(pg, l); result.Add(db.LookupObject(oid, null)); l += 1; } } else { do { if (!Find(db, GetKeyStrOid(pg, l), firstKey, lastKey, tree, height, result)) { return false; } } while (++l <= n); } } } else if (tree.type == ClassDescriptor.tpArrayOfByte) { if (firstKey != null) { while (l < r) { int i = (l + r) >> 1; if (tree.CompareByteArrays(firstKey, pg, i) >= firstKey.inclusion) { l = i + 1; } else { r = i; } } Assert.That(r == l); } if (lastKey != null) { if (height == 0) { while (l < n) { if (-tree.CompareByteArrays(lastKey, pg, l) >= lastKey.inclusion) { return false; } oid = GetKeyStrOid(pg, l); result.Add(db.LookupObject(oid, null)); l += 1; } } else { do { if (!Find(db, GetKeyStrOid(pg, l), firstKey, lastKey, tree, height, result)) { return false; } if (l == n) { return true; } } while (tree.CompareByteArrays(lastKey, pg, l++) >= 0); return false; } } else { if (height == 0) { while (l < n) { oid = GetKeyStrOid(pg, l); result.Add(db.LookupObject(oid, null)); l += 1; } } else { do { if (!Find(db, GetKeyStrOid(pg, l), firstKey, lastKey, tree, height, result)) { return false; } } while (++l <= n); } } } else { if (firstKey != null) { while (l < r) { int i = (l + r) >> 1; if (Compare(firstKey, pg, i) >= firstKey.inclusion) { l = i + 1; } else { r = i; } } Assert.That(r == l); } if (lastKey != null) { if (height == 0) { while (l < n) { if (-Compare(lastKey, pg, l) >= lastKey.inclusion) { return false; } oid = GetReference(pg, maxItems - 1 - l); result.Add(db.LookupObject(oid, null)); l += 1; } return true; } else { do { if (!Find(db, GetReference(pg, maxItems - 1 - l), firstKey, lastKey, tree, height, result)) { return false; } if (l == n) { return true; } } while (Compare(lastKey, pg, l++) >= 0); return false; } } if (height == 0) { while (l < n) { oid = GetReference(pg, maxItems - 1 - l); result.Add(db.LookupObject(oid, null)); l += 1; } } else { do { if (!Find(db, GetReference(pg, maxItems - 1 - l), firstKey, lastKey, tree, height, result)) { return false; } } while (++l <= n); } } } finally { db.pool.Unfix(pg); } return true; }
internal IPersistent Insert(Key key, IPersistent obj, bool overwrite) { BtreeKey ins = new BtreeKey(CheckKey(key), obj); if (root == null) { AllocateRootPage(ins); height = 1; } else { int result = root.Insert(ins, height, unique, overwrite); if (result == op_overflow) { AllocateRootPage(ins); height += 1; } else if (result == op_duplicate || result == op_overwrite) { return ins.oldNode; } } updateCounter += 1; nElems += 1; Modify(); return null; }
private Key ConvertKey(Key key) { if (key == null) { return null; } if (key.type != ClassDescriptor.tpArrayOfObject) { throw new StorageError(StorageError.INCOMPATIBLE_KEY_TYPE); } return new Key(new CompoundKey((object[]) key.oval), key.inclusion != 0); }
internal abstract int Compare(Key key, int i);
public override IPersistent Get(Key key) { return base.Get(ConvertKey(key)); }
internal override int Compare(Key key, int i) { return (short) key.ival - data[i]; }
public override IPersistent Remove(Key key) { return base.Remove(ConvertKey(key)); }
private Key ExtractKey(IPersistent obj) { try { FieldInfo f = fld; Key key = null; switch (type) { case ClassDescriptor.tpBoolean: key = new Key((bool) f.GetValue(obj)); break; case ClassDescriptor.tpByte: key = new Key((byte) f.GetValue(obj)); break; case ClassDescriptor.tpShort: key = new Key((short) f.GetValue(obj)); break; case ClassDescriptor.tpChar: key = new Key((char) f.GetValue(obj)); break; case ClassDescriptor.tpInt: key = new Key((int) f.GetValue(obj)); break; case ClassDescriptor.tpObject: key = new Key((IPersistent) f.GetValue(obj)); break; case ClassDescriptor.tpLong: key = new Key((long) f.GetValue(obj)); break; case ClassDescriptor.tpDate: //UPGRADE_NOTE: ref keyword was added to struct-type parameters. key = new Key(ref new DateTime[] { (DateTime) f.GetValue(obj) } [0]); break; case ClassDescriptor.tpFloat: key = new Key((float) f.GetValue(obj)); break; case ClassDescriptor.tpDouble: key = new Key((double) f.GetValue(obj)); break; case ClassDescriptor.tpString: key = new Key(((string) f.GetValue(obj)).ToCharArray()); break; default: Assert.Failed("Invalid type"); break; } return key; } catch (System.Exception x) { throw new StorageError(StorageError.ACCESS_VIOLATION, x); } }
public override IPersistent[] Get(Key from, Key till) { ArrayList list = new ArrayList(); if (root != 0) { BtreePage.Find((StorageImpl) Storage, root, ConvertKey(from), ConvertKey(till), this, height, list); } return (IPersistent[]) SupportClass.ICollectionSupport.ToArray(list, (object[]) System.Array.CreateInstance(cls, list.Count)); }
internal override int Compare(Key key, int i) { return ((System.IComparable) key.oval).CompareTo(((object[]) data)[i]); }
internal virtual bool Find(Key firstKey, Key lastKey, int height, ArrayList result) { int l = 0, n = nItems, r = n; height -= 1; if (firstKey != null) { while (l < r) { int i = (l + r) >> 1; if (Compare(firstKey, i) >= firstKey.inclusion) { l = i + 1; } else { r = i; } } Assert.That(r == l); } if (lastKey != null) { if (height == 0) { while (l < n) { if (-Compare(lastKey, l) >= lastKey.inclusion) { return false; } result.Add(items.Get(l)); l += 1; } return true; } else { do { if (!((BtreePage) items.Get(l)).Find(firstKey, lastKey, height, result)) { return false; } if (l == n) { return true; } } while (Compare(lastKey, l++) >= 0); return false; } } if (height == 0) { while (l < n) { result.Add(items.Get(l)); l += 1; } } else { do { if (!((BtreePage) items.Get(l)).Find(firstKey, lastKey, height, result)) { return false; } } while (++l <= n); } return true; }
internal override int Compare(Key key, int i) { return String.CompareOrdinal(((string) key.oval), data[i]); }