package apoc.monitor;

import apoc.result.StoreInfoResult;
import org.apache.commons.io.FileUtils;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.io.layout.DatabaseLayout;
import org.neo4j.kernel.database.Database;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
import org.neo4j.procedure.Context;
import org.neo4j.procedure.Description;
import org.neo4j.procedure.Procedure;

import java.io.File;
import java.util.stream.Stream;

public class Store {

    @Context
    public GraphDatabaseService db;

    @Procedure
    @Description("apoc.monitor.store() returns informations about the sizes of the different parts of the neo4j graph store")
    public Stream<StoreInfoResult> store() {

        Database database = ((GraphDatabaseAPI) db).getDependencyResolver().resolveDependency(Database.class);
        DatabaseLayout databaseLayout = database.getDatabaseLayout();
        return Stream.of(new StoreInfoResult(
                getDirectorySize(databaseLayout.getTransactionLogsDirectory().toFile()),
                databaseLayout.propertyStringStore().toFile().length(),
                databaseLayout.propertyArrayStore().toFile().length(),
                databaseLayout.relationshipStore().toFile().length(),
                databaseLayout.propertyStore().toFile().length(),
                getDirectorySize(databaseLayout.databaseDirectory().toFile()), //databaseLayout.storeFiles().stream().mapToLong(File::length).sum(),
                databaseLayout.nodeStore().toFile().length()
        ));
    }

    private long getDirectorySize(File folder) {
        return folder.exists() ? FileUtils.sizeOfDirectory(folder) : 0;
    }


}

@@ -92,16 +92,71 @@
 eUtils;%0A
+%0Aimport java.io.File;%0Aimport java.util.stream.Stream;%0A%0A
 import o
@@ -221,16 +221,36 @@
 .layout.
+recordstorage.Record
 Database
@@ -470,62 +470,8 @@
 e;%0A%0A
-import java.io.File;%0Aimport java.util.stream.Stream;%0A%0A
 publ
@@ -836,32 +836,154 @@
 class);%0A        
+//This will work only on Record format databases. Has to be updated when any additional format is available%0A        Record
 DatabaseLayout d
@@ -997,16 +997,43 @@
 Layout =
+ RecordDatabaseLayout.cast(
  databas
@@ -1053,16 +1053,18 @@
 Layout()
+ )
 ;%0A      

Fix compilation error in apoc.monitor.store()