// Path: src/main/java/net/kimleo/rec/common/Pair.java
// public class Pair<K, V> {
//     public final K first;
//     public final V second;
// 
// 
//     public Pair(K first, V second) {
//         this.first = first;
//         this.second = second;
//     }
// 
//     public K getFirst() {
//         return first;
//     }
// 
//     public V getSecond() {
//         return second;
//     }
// 
// }
// 
// Path: src/main/java/net/kimleo/rec/common/concept/Mapped.java
// public interface Mapped<T> {
//     T get(String field);
// 
//     List<String> keys();
// }
// 
// Path: src/main/java/net/kimleo/rec/common/exception/ResourceAccessException.java
// public class ResourceAccessException extends RuntimeException {
//     public ResourceAccessException(String message, Throwable ex) {
//         super(message, ex);
//     }
// }


import net.kimleo.rec.common.Pair;
import net.kimleo.rec.common.concept.Mapped;
import net.kimleo.rec.common.exception.ResourceAccessException;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
package net.kimleo.rec.v2.utils;



public class Records {
    public static ByteBuffer encode(Mapped<String> record, List<String> keys) {
        int size = keys.size();
        ByteBuffer byteBuffer = ByteBuffer.allocate(sizeof(record, keys));

        byteBuffer.putInt(size);
        for (String key : keys) {
            String field = record.get(key);
            byteBuffer.putInt(field.getBytes().length);
            byteBuffer.put(field.getBytes());
        }
        return byteBuffer;
    }

    private static int sizeof(Mapped<String> record, List<String> keys) {
        int size = 4;

        for (String key : keys) {
            size += 4;
            size += record.get(key).getBytes().length;
        }
        return size;
    }

// Path: src/main/java/net/kimleo/rec/common/Pair.java
// public class Pair<K, V> {
//     public final K first;
//     public final V second;
// 
// 
//     public Pair(K first, V second) {
//         this.first = first;
//         this.second = second;
//     }
// 
//     public K getFirst() {
//         return first;
//     }
// 
//     public V getSecond() {
//         return second;
//     }
// 
// }
// 
// Path: src/main/java/net/kimleo/rec/common/concept/Mapped.java
// public interface Mapped<T> {
//     T get(String field);
// 
//     List<String> keys();
// }
// 
// Path: src/main/java/net/kimleo/rec/common/exception/ResourceAccessException.java
// public class ResourceAccessException extends RuntimeException {
//     public ResourceAccessException(String message, Throwable ex) {
//         super(message, ex);
//     }
// }


// Path: src/main/java/net/kimleo/rec/v2/utils/Records.java
import net.kimleo.rec.common.Pair;
import net.kimleo.rec.common.concept.Mapped;
import net.kimleo.rec.common.exception.ResourceAccessException;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

package net.kimleo.rec.v2.utils;



public class Records {
    public static ByteBuffer encode(Mapped<String> record, List<String> keys) {
        int size = keys.size();
        ByteBuffer byteBuffer = ByteBuffer.allocate(sizeof(record, keys));

        byteBuffer.putInt(size);
        for (String key : keys) {
            String field = record.get(key);
            byteBuffer.putInt(field.getBytes().length);
            byteBuffer.put(field.getBytes());
        }
        return byteBuffer;
    }

    private static int sizeof(Mapped<String> record, List<String> keys) {
        int size = 4;

        for (String key : keys) {
            size += 4;
            size += record.get(key).getBytes().length;
        }
        return size;
    }

    public static Pair<List<String>, Integer> decode(ByteBuffer buffer, int readPos, int expectedSize) {