private void testRangeScan(long rangeSize) throws Exception { addAscending(0L, 1L, VALUE_COUNT); btree.sync(); byte[] minData = new byte[8]; byte[] maxData = new byte[8]; Thread.sleep(500L); long startTime = System.currentTimeMillis(); for (long minValue = 0L; minValue < VALUE_COUNT; minValue += rangeSize) { ByteArrayUtil.putLong(minValue, minData, 0); ByteArrayUtil.putLong(minValue + rangeSize, maxData, 0); RecordIterator iter = btree.iterateRange(minData, maxData); try { while (iter.next() != null) {} } finally { iter.close(); } } long endTime = System.currentTimeMillis(); printTime(startTime, endTime, "testRangeScan" + rangeSize); }
final long fetchRecord(Version shared) { if (Debug.ENABLED) Debug.assertion(!_updatedVersions.contains(shared)); long record = Record.NOT_STORED; Object union = shared.getUnion(); if (union instanceof Descriptor) { Descriptor descriptor = (Descriptor) union; Session session = descriptor.getSession(); long records = session.getRecords(); if (records == Record.NOT_STORED) { records = _sessions.fetch(session.getSharedVersion_objectfabric().getUID()); session.setRecords(records); } record = fetchRecord(records, descriptor.getId() & 0xff); } else { byte[] uid = shared.getUID(); if (uid != null) { long records = _sessions.fetch(uid); record = fetchRecord(records, Session.UID_OBJECT_ID & 0xff); } } if (Debug.ENABLED) Debug.assertion(record != Record.UNKNOWN); return record; }
protected IndexSegmentCheckpoint doBuildAndDiscardCache(final BTree btree, final int m) throws IOException, Exception { final long commitTime = System.currentTimeMillis(); final IndexSegmentCheckpoint checkpoint = IndexSegmentBuilder.newInstance( outFile, tmpDir, btree.getEntryCount(), btree.rangeIterator(), m, btree.getIndexMetadata(), commitTime, true /* compactingMerge */, bufferNodes) .call(); // @see BLZG-1501 (remove LRUNexus) // if (LRUNexus.INSTANCE != null) { // // /* // * Clear the records for the index segment from the cache so we will // * read directly from the file. This is necessary to ensure that the // * data on the file is good rather than just the data in the cache. // */ // // LRUNexus.INSTANCE.deleteCache(checkpoint.segmentUUID); // // } return checkpoint; }
/** * Split function. Find the middle element 'mid'. And split the NodeArray using 'mid'. Copy the * split array into a new Node 'newNode' of type 'Btree'. Insert corresponding child elements with * it Update keycount and childcount * * @param i */ private void split(int i) { if ((childArray[i].keycount) % 2 != 0) { int x1 = childArray[i].keycount / 2; int x2 = childArray[i].childcount / 2; int mid = childArray[i].keyArray[childArray[i].keycount / 2]; // Split the node at mid int midv = childArray[i].valueArray[childArray[i].keycount / 2]; BTree splitNode = new BTree(max); // New node created to store split data for (int j = x1 + 1; j <= 2 * x1; j++) splitNode.keyArray[j - x1 - 1] = childArray[i].keyArray[j]; for (int j = x1 + 1; j <= 2 * x1; j++) splitNode.valueArray[j - x1 - 1] = childArray[i].valueArray[j]; for (int j = x2; j < 2 * x2; j++) splitNode.childArray[j - x2] = childArray[i].childArray[j]; // Copy it's respective childArray childArray[i].keycount = x1; // Update Values' count of original child childArray[i].childcount = x2; // Update it's child count splitNode.keycount = x1; // Set new node's value count splitNode.childcount = x2; // Set new node's child count int temp = scanKeyArray(mid); // Insert the 'mid' node into it's parent insertKey(temp, mid, midv); insertChild(i + 1, splitNode); } }
@SuppressWarnings("unchecked") @Override protected void getAsync(UserTObject object, Object key, FutureWithCallback<Object> future) { if (object.getSharedVersion_objectfabric().getUnion() instanceof Record) { long record = getOrFetchRecord(object.getSharedVersion_objectfabric()); if (Record.isStored(record)) { BTree tree = BTree.load(getRecordManager(), record, false); _writer.write(key); byte[] data = new byte[_writer.getOffset()]; PlatformAdapter.arraycopy(_writer.getBuffer(), 0, data, 0, data.length); long id = tree.fetch(data); if (id != 0) { data = getRecordManager().fetch(id); Object value = _reader.read(data); _reader.readVersions(); Version version = _reader.getOrCreateVersion(object); TKeyedEntry entry = new TKeyedEntry(key, TKeyed.hash(key), value, false); if (Debug.ENABLED) Debug.assertion(value != TKeyedEntry.REMOVAL); ((LazyMapVersion) version).putEntry(key, entry, true, true); _reader.importVersions(); future.set(value); return; } } } future.set(null); }
private BTreeRange fieldIndexKeySearch(Transaction trans, BTree btree, Object key) { // SearchTarget should not make a difference, HIGHEST is faster BTreeNodeSearchResult start = btree.searchLeaf(trans, fieldIndexKey(0, key), SearchTarget.LOWEST); BTreeNodeSearchResult end = btree.searchLeaf(trans, fieldIndexKey(Integer.MAX_VALUE, key), SearchTarget.LOWEST); return start.createIncludingRange(end); }
@Test public void removeFromOneElementTree() { tree = new BTree<Integer>(); tree.add(1); tree.remove(1); assertThat(tree.size(), is(0)); }
@Test public void removeElement2() { tree.remove(2); assertThat(tree.size(), is(5)); List<Integer> list = tree.preorder(); List<Integer> expected = Arrays.asList(3, 1, 5, 4, 6); assertThat(list, is(equalTo(expected))); }
public void mapId(int id, Slot slot) { _slotTree.add(trans(), new IdSlotMapping(id, slot.address(), slot.length())); if (_commitFrequency > 0) { _slotInsertCount++; if (_commitFrequency == _slotInsertCount) { _slotTree.commit(trans()); _slotInsertCount = 0; } } }
@Before public void setup() { tree = new BTree<Integer>(); tree.add(2); tree.add(4); tree.add(3); tree.add(1); tree.add(5); tree.add(6); }
@Test public void removeANotFoundElement() { tree.remove(0); assertThat(tree.size(), is(6)); List<Integer> list = tree.preorder(); List<Integer> expected = Arrays.asList(2, 1, 4, 3, 5, 6); assertThat(list, is(equalTo(expected))); }
protected void mapNonClassIDs(int origID, int mappedID) { _cache = new MappedIDPair(origID, mappedID); _idTree.add(trans(), _cache); if (_commitFrequency > 0) { _idInsertCount++; if (_commitFrequency == _idInsertCount) { _idTree.commit(trans()); _idInsertCount = 0; } } }
/** * Create, populate, and return a btree with a branching factor of (3) and ten sequential keys * [1:10]. The values are {@link SimpleEntry} objects whose state is the same as the corresponding * key. * * @return The btree. * @see src/architecture/btree.xls, which details this input tree and a series of output trees * with various branching factors. */ public BTree getProblem1() { final BTree btree = getBTree(3); for (int i = 1; i <= 10; i++) { btree.insert(TestKeyBuilder.asSortKey(i), new SimpleEntry(i)); } return btree; }
@Test public void checkAddElements() { assertThat(tree.size(), is(6)); assertThat(tree.steps(2), is(1)); assertThat(tree.steps(4), is(2)); assertThat(tree.steps(3), is(3)); assertThat(tree.steps(1), is(2)); assertThat(tree.steps(5), is(3)); assertThat(tree.steps(6), is(4)); assertThat(tree.steps(0), is(-1)); }
public static void main(String[] args) { BTree btree = new BTree(2); btree.left = new BTree(1); btree.right = new BTree(3); System.out.println(isObeysBST(btree) == true); btree.left.left = new BTree(4); btree.left.right = new BTree(5); System.out.println(isObeysBST(btree) == false); }
@Test public void sucessorElement_() { BTree<Integer> t = new BTree<Integer>(); t.add(5); t.add(3); t.add(7); t.add(2); t.add(4); t.add(6); Integer suc = t.sucessor(4); assertThat(suc, is(5)); }
public synchronized <K> NavigableSet<K> createTreeSet( String name, Comparator<K> keyComparator, Serializer<K> keySerializer) { try { assertNameNotExist(name); BTree<K, Object> tree = BTree.createInstance(this, keyComparator, keySerializer, null, false); setNamedObject(name, tree.getRecid()); BTreeSet<K> ret = new BTreeSet<K>(new BTreeMap(tree, false)); collections.put(name, new WeakReference<Object>(ret)); return ret; } catch (IOException e) { throw new IOError(e); } }
@Test public void testRemove() throws Exception { addAscending(0L, 1L, VALUE_COUNT); btree.sync(); Thread.sleep(500L); long startTime = System.currentTimeMillis(); remove(0L, 4L, VALUE_COUNT / 4); btree.sync(); long endTime = System.currentTimeMillis(); printTime(startTime, endTime, "testRemove"); }
public synchronized void deleteCollection(String name) { try { long nameDirectory_recid = getRoot(NAME_DIRECTORY_ROOT); if (nameDirectory_recid == 0) throw new IOException("Collection not found"); HTree<String, Long> dir = fetch(nameDirectory_recid); Long recid = dir.get(name); if (recid == null) throw new IOException("Collection not found"); Object o = fetch(recid); // we can not use O instance since it is not correctly initialized if (o instanceof LinkedList2) { LinkedList2 l = (LinkedList2) o; l.clear(); delete(l.rootRecid); } else if (o instanceof BTree) { ((BTree) o).clear(); } else if (o instanceof HTree) { HTree t = (HTree) o; t.clear(); HTreeDirectory n = (HTreeDirectory) fetch(t.rootRecid, t.SERIALIZER); n.deleteAllChildren(); delete(t.rootRecid); } else { throw new InternalError("unknown collection type: " + (o == null ? null : o.getClass())); } delete(recid); collections.remove(name); dir.remove(name); } catch (IOException e) { throw new IOError(e); } }
final void updateRecord(Version shared, Session session, byte[] uid, long currentRecord, long newRecord, int id) { if (Debug.ENABLED) Debug.assertion(newRecord != currentRecord); int index = _updatedVersions.add(shared); if (index == _updatedVersionsRecords.length) _updatedVersionsRecords = Utils.extend(_updatedVersionsRecords); _updatedVersionsRecords[index] = newRecord; // Sessions int sessionIndex = _updatedSessions.indexOf(session); long records = sessionIndex >= 0 ? _updatedSessionsRecords[sessionIndex] : session.getRecords(); final long initialRecords = records; if (records == Record.NOT_STORED) records = _sessions.fetch(uid); if (records != Record.NOT_STORED) { byte[] data = getRecordManager().fetch(records); if (Debug.ENABLED) { Debug.assertion(data.length == Session.TOTAL_LENGTH * 8); Debug.assertion(Utils.readLong(data, id * 8) == currentRecord); } Utils.writeLong(data, id * 8, newRecord); getRecordManager().update(records, data, 0, data.length); } else { byte[] data = new byte[Session.TOTAL_LENGTH * 8]; Utils.writeLong(data, id * 8, newRecord); records = getRecordManager().insert(data, 0, data.length); _sessions.put(uid, records); } if (records != initialRecords) { index = _updatedSessions.add(session); if (index == _updatedSessionsRecords.length) _updatedSessionsRecords = Utils.extend(_updatedSessionsRecords); _updatedSessionsRecords[index] = records; } }
public synchronized <K> NavigableSet<K> getTreeSet(String name) { Object o = getCollectionInstance(name); if (o != null) return (NavigableSet<K>) o; try { long recid = getNamedObject(name); if (recid == 0) return null; BTree t = BTree.<K, Object>load(this, recid); if (t.hasValues()) throw new ClassCastException("TreeMap is not TreeSet"); BTreeSet<K> ret = new BTreeSet<K>(new BTreeMap(t, false)); collections.put(name, new WeakReference<Object>(ret)); return ret; } catch (IOException e) { throw new IOError(e); } }
public synchronized <K, V> ConcurrentNavigableMap<K, V> createTreeMap( String name, Comparator<K> keyComparator, Serializer<K> keySerializer, Serializer<V> valueSerializer) { try { assertNameNotExist(name); BTree<K, V> tree = BTree.createInstance(this, keyComparator, keySerializer, valueSerializer, true); setNamedObject(name, tree.getRecid()); ConcurrentNavigableMap<K, V> ret = new BTreeMap<K, V>(tree, false); // TODO put readonly flag here collections.put(name, new WeakReference<Object>(ret)); return ret; } catch (IOException e) { throw new IOError(e); } }
public synchronized <K, V> ConcurrentNavigableMap<K, V> getTreeMap(String name) { Object o = getCollectionInstance(name); if (o != null) return (ConcurrentNavigableMap<K, V>) o; try { long recid = getNamedObject(name); if (recid == 0) return null; BTree t = BTree.<K, V>load(this, recid); if (!t.hasValues()) throw new ClassCastException("TreeSet is not TreeMap"); ConcurrentNavigableMap<K, V> ret = new BTreeMap<K, V>(t, false); // TODO put readonly flag here collections.put(name, new WeakReference<Object>(ret)); return ret; } catch (IOException e) { throw new IOError(e); } }
private void addRandom(int valueCount) throws IOException { Random random = new Random(0L); byte[] data = new byte[8]; for (int i = 0; i < valueCount; i++) { ByteArrayUtil.putLong(random.nextLong(), data, 0); btree.insert(data); } }
@Test public void testFullScan() throws Exception { addAscending(0L, 1L, VALUE_COUNT); btree.sync(); Thread.sleep(500L); long startTime = System.currentTimeMillis(); RecordIterator iter = btree.iterateAll(); try { while (iter.next() != null) {} } finally { iter.close(); } long endTime = System.currentTimeMillis(); printTime(startTime, endTime, "testFullScan"); }
public int addressForId(int id) { BTreeRange range = _slotTree.searchRange(trans(), new IdSlotMapping(id, 0, 0)); Iterator4 pointers = range.pointers(); if (pointers.moveNext()) { BTreePointer pointer = (BTreePointer) pointers.current(); return ((IdSlotMapping) pointer.key())._address; } return 0; }
public void write(DataOutput out) throws IOException { out.writeInt(valueSize); out.writeInt(valueCount); Value<V> value = firstValue; while (value != null) { btree.writeValue(out, value.getValue()); value = value.getNext(); } }
/** * The Basic Insert function. This funcyion insert the value in the tree. If the NodeArray is * full, then it splits the NodeArray by calling the split function. * * @param value */ public void BTreeInsert(int key, int value) { preInsert(key, value); // Simple insert if (keycount > max) // Check if it has max values { BTree newNode = new BTree(max); newNode.keycount = keycount; newNode.childcount = childcount; System.arraycopy(this.valueArray, 0, newNode.valueArray, 0, keycount); System.arraycopy(this.keyArray, 0, newNode.keyArray, 0, keycount); System.arraycopy(this.childArray, 0, newNode.childArray, 0, childcount); this.keycount = 0; this.childcount = 1; childArray[0] = newNode; split(0); } }
private void remove(long startValue, long increment, int valueCount) throws IOException { long value = startValue; byte[] data = new byte[8]; for (int i = 0; i < valueCount; i++) { ByteArrayUtil.putLong(value, data, 0); btree.remove(data); value += increment; } }
// ------------------------------------------------------------ public BTree<Integer> rndTree(int size) { itrWithRights = new Vector<BTreeItr<Integer>>(); int i = 0; BTree<Integer> t = new BTree<Integer>(); BTreeItr<Integer> ti; int[] tab = new int[size]; for (i = 0; i < size; i++) tab[i] = i; shuffle(tab, rnd); for (i = 0; i < size; i++) { ti = t.root(); while (!ti.isBottom()) { if (rnd.nextInt(2) == 0) ti = ti.left(); else ti = ti.right(); } ti.insert(new Integer(tab[i])); if (!ti.isRoot() && !ti.isLeftArc()) itrWithRights.add(ti.up()); } return t; }