FilesystemStore implements PersistentStore { @Override public synchronized void put(@Nonnull Key key, @Nonnull StoreInfoAndData data) throws IOException, SQLException { init(); resizeIfRequired(key, data); final Path privatePath = getPrivatePath(key); if (Files.exists(privatePath)) { currentSize -= Files.size(privatePath); } Files.createDirectories(privatePath.getParent()); Files.write(privatePath, data.data); currentSize += data.data.length; updateDbCurrentSize(); Path publicPath = getPublicPath(key); Files.deleteIfExists(publicPath); if (data.isPublished() && key.hideWithheld) { Files.createDirectories(publicPath.getParent()); try { Files.createLink(publicPath, privatePath); } catch (UnsupportedOperationException | SecurityException e) { Files.copy(privatePath, publicPath); } } try (PreparedStatement statement = this.metadataDb.prepareStatement(QUERY_PUT)) { statement.setInt(1, key.hashCode()); statement.setLong(2, data.getChangeDate()); statement.setBoolean(3, data.isPublished()); statement.setString(4, privatePath.toUri().toString()); statement.execute(); } }  @Override synchronized StoreInfoAndData get(@Nonnull Key key); @Override synchronized StoreInfo getInfo(@Nonnull Key key); @Override synchronized void put(@Nonnull Key key, @Nonnull StoreInfoAndData data); @Nullable @Override byte[] getPublished(@Nonnull Key key); @Override synchronized void remove(@Nonnull Key key); @Override void setPublished(int metadataId, final boolean published); @Override void clear(); void setGeonetworkDataDir(GeonetworkDataDirectory geonetworkDataDir); Path getPrivatePath(Key key); Path getPublicPath(Key key); void setMaxSizeKb(long maxSize); void setMaxSizeMb(int maxSize); void setMaxSizeGb(int maxSize); void setTesting(boolean testing); static final String WITHHELD_MD_DIRNAME; static final String FULL_MD_NAME; static final String QUERY_SETCURRENT_SIZE; static final String QUERY_GETCURRENT_SIZE; }
@Test public void testDiskSizeRestrictionReduceSizeOnOverflow() throws Exception { Key[] keys = prepareDiskSizeRestrictionTests(); store.put(keys[5], new StoreInfoAndData(new byte[200], 6, false)); assertStoreContains(keys, keys[3], keys[4], keys[5]); }
@Test public void testDiskSizeRestrictionReplace() throws Exception { Key[] keys = prepareDiskSizeRestrictionTests(); store.put(keys[4], new StoreInfoAndData(new byte[200], 4, false)); assertStoreContains(keys, keys[0], keys[1], keys[2], keys[3], keys[4]); store.put(keys[0], new StoreInfoAndData(new byte[200], 0, false)); assertStoreContains(keys, keys[0], keys[1], keys[2], keys[3], keys[4]); store.put(keys[1], new StoreInfoAndData(new byte[200], 1, false)); assertStoreContains(keys, keys[0], keys[1], keys[2], keys[3], keys[4]); store.put(keys[3], new StoreInfoAndData(new byte[200], 3, false)); assertStoreContains(keys, keys[0], keys[1], keys[2], keys[3], keys[4]); store.put(keys[2], new StoreInfoAndData(new byte[200], 2, false)); assertStoreContains(keys, keys[0], keys[1], keys[2], keys[3], keys[4]); try ( Statement statement = store.metadataDb.createStatement(); ResultSet rs = statement.executeQuery(FilesystemStore.QUERY_GETCURRENT_SIZE)) { assertTrue(rs.next()); assertEquals(1000L, Long.parseLong(rs.getString(1))); } store.put(keys[4], new StoreInfoAndData(new byte[100], 4, false)); assertStoreContains(keys, keys[0], keys[1], keys[2], keys[3], keys[4]); store.put(keys[0], new StoreInfoAndData(new byte[300], 0, false)); assertStoreContains(keys, keys[0], keys[1], keys[2], keys[3], keys[4]); store.put(keys[1], new StoreInfoAndData(new byte[200], 1, false)); assertStoreContains(keys, keys[0], keys[1], keys[2], keys[3], keys[4]); store.put(keys[3], new StoreInfoAndData(new byte[100], 3, false)); assertStoreContains(keys, keys[0], keys[1], keys[2], keys[3], keys[4]); store.put(keys[2], new StoreInfoAndData(new byte[300], 2, false)); assertStoreContains(keys, keys[0], keys[1], keys[2], keys[3], keys[4]); store.put(keys[2], new StoreInfoAndData(new byte[400], 2, false)); assertStoreContains(keys, keys[2], keys[3], keys[4]); }