public static void dispose() { try { w.lock(); DbConnection.force(); DbConnection.closeFiles(); } catch (Throwable e) { throw DbConnection.handleError(e); } finally { ourIsDisposed = true; 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(); } }
public static void releaseContent(int contentId) { try { getContentStorage().releaseRecord(contentId); } catch (Throwable e) { throw DbConnection.handleError(e); } }
public static Pair<String[], int[]> listAll(int parentId) { try { r.lock(); try { final DataInputStream input = readAttribute(parentId, CHILDREN_ATT); if (input == null) return Pair.create(ArrayUtil.EMPTY_STRING_ARRAY, ArrayUtil.EMPTY_INT_ARRAY); final int count = DataInputOutputUtil.readINT(input); final int[] ids = ArrayUtil.newIntArray(count); final String[] names = ArrayUtil.newStringArray(count); for (int i = 0; i < count; i++) { int id = DataInputOutputUtil.readINT(input); id = id >= 0 ? id + parentId : -id; ids[i] = id; names[i] = getName(id); } input.close(); return Pair.create(names, ids); } finally { r.unlock(); } } catch (Throwable e) { throw DbConnection.handleError(e); } }
public static long getCreationTimestamp() { try { r.lock(); return DbConnection.getTimestamp(); } finally { r.unlock(); } }
@Nullable public static DataInputStream readContentById(int contentId) { try { return getContentStorage().readStream(contentId); } catch (Throwable e) { throw DbConnection.handleError(e); } }
@Override public void close() throws IOException { super.close(); try { doFlush(); } catch (Throwable e) { throw DbConnection.handleError(e); } }
public static void deleteRecordRecursively(int id) { try { w.lock(); incModCount(id); doDeleteRecursively(id); } catch (Throwable e) { throw DbConnection.handleError(e); } finally { w.unlock(); } }
public static void setName(int id, String name) { try { w.lock(); incModCount(id); putRecordInt(id, NAME_OFFSET, getNames().enumerate(name)); } catch (Throwable e) { throw DbConnection.handleError(e); } finally { w.unlock(); } }
public static int storeUnlinkedContent(byte[] bytes) { try { int recordId = getContentStorage().acquireNewRecord(); AbstractStorage.StorageDataOutput output = getContentStorage().writeStream(recordId, true); output.write(bytes); output.close(); return recordId; } catch (IOException e) { throw DbConnection.handleError(e); } }
public static void setLength(int id, long len) { try { w.lock(); incModCount(id); getRecords().putLong(getOffset(id, LENGTH_OFFSET), len); } catch (Throwable e) { throw DbConnection.handleError(e); } finally { w.unlock(); } }
public static void setTimestamp(int id, long value) { try { w.lock(); incModCount(id); getRecords().putLong(getOffset(id, TIMESTAMP_OFFSET), value); } catch (Throwable e) { throw DbConnection.handleError(e); } 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 getContentId(int fileId) { try { r.lock(); try { return getContentRecordId(fileId); } finally { r.unlock(); } } catch (Throwable e) { throw DbConnection.handleError(e); } }
public static int acquireFileContent(int fileId) { try { w.lock(); int record = getContentRecordId(fileId); if (record > 0) getContentStorage().acquireRecord(record); return record; } catch (Throwable e) { throw DbConnection.handleError(e); } finally { w.unlock(); } }
public static boolean wereChildrenAccessed(int id) { try { r.lock(); try { return findAttributePage(id, CHILDREN_ATT, false) != 0; } finally { r.unlock(); } } catch (Throwable e) { throw DbConnection.handleError(e); } }
private static void checkAttributesStorageSanity( int id, IntArrayList usedAttributeRecordIds, IntArrayList validAttributeIds) { int attributeRecordId = getAttributeRecordId(id); assert attributeRecordId >= 0; if (attributeRecordId > 0) { try { checkAttributesSanity(attributeRecordId, usedAttributeRecordIds, validAttributeIds); } catch (IOException ex) { throw DbConnection.handleError(ex); } } }
public static void setFlags(int id, int flags, final boolean markAsChange) { try { w.lock(); if (markAsChange) { incModCount(id); } putRecordInt(id, FLAGS_OFFSET, flags); } catch (Throwable e) { throw DbConnection.handleError(e); } finally { w.unlock(); } }
public static String getName(int id) { try { r.lock(); try { final int nameId = getRecordInt(id, NAME_OFFSET); return nameId != 0 ? getNames().valueOf(nameId) : ""; } finally { r.unlock(); } } catch (Throwable e) { throw DbConnection.handleError(e); } }
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(); } }
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(); } }
@Nullable public static DataInputStream readContent(int fileId) { try { int page; try { r.lock(); page = findContentPage(fileId, false); if (page == 0) return null; } finally { r.unlock(); } return getContentStorage().readStream(page); } catch (Throwable e) { throw DbConnection.handleError(e); } }
public static void setParent(int id, int parent) { if (id == parent) { LOG.error("Cyclic parent/child relations"); return; } try { w.lock(); incModCount(id); putRecordInt(id, PARENT_OFFSET, parent); } 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; } } }
@Nullable public static DataInputStream readAttribute(int fileId, String attId) { try { synchronized (attId) { int page; try { r.lock(); page = findAttributePage(fileId, attId, false); if (page == 0) return null; } finally { r.unlock(); } return getAttributesStorage().readStream(page); } } catch (Throwable e) { throw DbConnection.handleError(e); } }
public static int getParent(int id) { try { r.lock(); try { final int parentId = getRecordInt(id, PARENT_OFFSET); if (parentId == id) { LOG.error("Cyclic parent child relations in the database. id = " + id); return 0; } return parentId; } finally { r.unlock(); } } catch (Throwable e) { throw DbConnection.handleError(e); } }
private static int findAttributePage(int fileId, String attrId, boolean toWrite) throws IOException { checkFileIsValid(fileId); Storage storage = getAttributesStorage(); int encodedAttrId = DbConnection.getAttributeId(attrId); int recordId = getAttributeRecordId(fileId); if (recordId == 0) { if (!toWrite) return 0; recordId = storage.createNewRecord(); setAttributeRecordId(fileId, recordId); } else { DataInputStream attrRefs = storage.readStream(recordId); try { while (attrRefs.available() > 0) { final int attIdOnPage = DataInputOutputUtil.readINT(attrRefs); final int attrAddress = DataInputOutputUtil.readINT(attrRefs); if (attIdOnPage == encodedAttrId) return attrAddress; } } finally { attrRefs.close(); } } if (toWrite) { Storage.AppenderStream appender = storage.appendStream(recordId); DataInputOutputUtil.writeINT(appender, encodedAttrId); int attrAddress = storage.createNewRecord(); DataInputOutputUtil.writeINT(appender, attrAddress); DbConnection.REASONABLY_SMALL.myAttrPageRequested = true; try { appender.close(); } finally { DbConnection.REASONABLY_SMALL.myAttrPageRequested = false; } return attrAddress; } return 0; }
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 int[] list(int id) { try { r.lock(); try { final DataInputStream input = readAttribute(id, CHILDREN_ATT); if (input == null) return ArrayUtil.EMPTY_INT_ARRAY; final int count = DataInputOutputUtil.readINT(input); final int[] result = ArrayUtil.newIntArray(count); for (int i = 0; i < count; i++) { int childId = DataInputOutputUtil.readINT(input); childId = childId >= 0 ? childId + id : -childId; result[i] = childId; } input.close(); return result; } finally { r.unlock(); } } catch (Throwable e) { throw DbConnection.handleError(e); } }
@Override public boolean isDirty() { return DbConnection.isDirty(); }