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);
    }
  }
 @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 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 deleteRecordRecursively(int id) {
   try {
     w.lock();
     incModCount(id);
     doDeleteRecursively(id);
   } 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 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 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 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);
   }
 }
 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 void dispose() {
   try {
     w.lock();
     DbConnection.force();
     DbConnection.closeFiles();
   } catch (Throwable e) {
     throw DbConnection.handleError(e);
   } finally {
     ourIsDisposed = true;
     w.unlock();
   }
 }
 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();
   }
 }
  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);
   }
 }
  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();
    }
  }
 @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();
    }
  }
  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);
    }
  }
 @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 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[] 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);
    }
  }
  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 RuntimeException handleError(Throwable e) {
   return DbConnection.handleError(e);
 }