public static int findRootRecord(String rootUrl) throws IOException { try { w.lock(); DbConnection.markDirty(); final int root = getNames().enumerate(rootUrl); final DataInputStream input = readAttribute(1, CHILDREN_ATT); int[] names = ArrayUtil.EMPTY_INT_ARRAY; int[] ids = ArrayUtil.EMPTY_INT_ARRAY; if (input != null) { try { final int count = DataInputOutputUtil.readINT(input); names = ArrayUtil.newIntArray(count); ids = ArrayUtil.newIntArray(count); for (int i = 0; i < count; i++) { final int name = DataInputOutputUtil.readINT(input); final int id = DataInputOutputUtil.readINT(input); if (name == root) { return id; } names[i] = name; ids[i] = id; } } finally { input.close(); } } final DataOutputStream output = writeAttribute(1, CHILDREN_ATT, false); int id; try { id = createRecord(); DataInputOutputUtil.writeINT(output, names.length + 1); for (int i = 0; i < names.length; i++) { DataInputOutputUtil.writeINT(output, names[i]); DataInputOutputUtil.writeINT(output, ids[i]); } DataInputOutputUtil.writeINT(output, root); DataInputOutputUtil.writeINT(output, id); } finally { output.close(); } return id; } finally { w.unlock(); } }
private static void deleteRecord(final int id) { try { w.lock(); DbConnection.markDirty(); deleteContentAndAttributes(id); DbConnection.cleanRecord(id); addToFreeRecordsList(id); } catch (Throwable e) { throw DbConnection.handleError(e); } finally { w.unlock(); } }
private static void incModCount(int id) { DbConnection.markDirty(); ourLocalModificationCount++; final int count = getModCount() + 1; getRecords().putInt(HEADER_GLOBAL_MOD_COUNT_OFFSET, count); int parent = id; int depth = 10000; while (parent != 0) { setModCount(parent, count); parent = getParent(parent); if (depth-- == 0) { LOG.error("Cyclic parent child relation? file: " + getName(id)); return; } } }
public static void deleteRootRecord(int id) throws IOException { try { w.lock(); DbConnection.markDirty(); final DataInputStream input = readAttribute(1, CHILDREN_ATT); assert input != null; int count; int[] names; int[] ids; try { count = DataInputOutputUtil.readINT(input); names = ArrayUtil.newIntArray(count); ids = ArrayUtil.newIntArray(count); for (int i = 0; i < count; i++) { names[i] = DataInputOutputUtil.readINT(input); ids[i] = DataInputOutputUtil.readINT(input); } } finally { input.close(); } final int index = ArrayUtil.find(ids, id); assert index >= 0; names = ArrayUtil.remove(names, index); ids = ArrayUtil.remove(ids, index); final DataOutputStream output = writeAttribute(1, CHILDREN_ATT, false); try { DataInputOutputUtil.writeINT(output, count - 1); for (int i = 0; i < names.length; i++) { DataInputOutputUtil.writeINT(output, names[i]); DataInputOutputUtil.writeINT(output, ids[i]); } } finally { output.close(); } } finally { w.unlock(); } }
public static void updateList(int id, @NotNull int[] children) { try { w.lock(); DbConnection.markDirty(); final DataOutputStream record = writeAttribute(id, CHILDREN_ATT, false); DataInputOutputUtil.writeINT(record, children.length); for (int child : children) { if (child == id) { LOG.error("Cyclic parent child relations"); } else { child = child > id ? child - id : -child; DataInputOutputUtil.writeINT(record, child); } } record.close(); } catch (Throwable e) { throw DbConnection.handleError(e); } finally { w.unlock(); } }
public static int createRecord() { try { w.lock(); DbConnection.markDirty(); final int free = DbConnection.getFreeRecord(); if (free == 0) { final int filelength = (int) getRecords().length(); LOG.assertTrue(filelength % RECORD_SIZE == 0); int newrecord = filelength / RECORD_SIZE; DbConnection.cleanRecord(newrecord); assert filelength + RECORD_SIZE == getRecords().length(); return newrecord; } else { DbConnection.cleanRecord(free); return free; } } catch (Throwable e) { throw DbConnection.handleError(e); } finally { w.unlock(); } }