Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private int getOffset(int capacity, int items) {
- return ((getMaxCapacity() + 1) * items + capacity) * BLOCK_SIZE;
- }
- @Override
- public int getSolution(int capacity, int items) {
- if (items == syncsPassed ) {
- return previousItemSolutions[capacity];
- }
- ByteBuffer byteBuffer = ByteBuffer.allocate(BLOCK_SIZE);
- int offset = getOffset(capacity, items);
- try {
- FileChannel channel = cacheFile.getChannel();
- int readBytes = channel.read(byteBuffer, offset);
- System.out.println(readBytes);
- return byteBuffer.getInt();
- } catch (IOException e) {
- e.printStackTrace();
- close();
- throw new IllegalStateException(e);
- }
- }
- public void sync(int items) {
- checkTrue(items == syncsPassed,
- "Illegal sync: only " + syncsPassed + " rows were synchronized, but sync is called for row " + items);
- try {
- int offset = getOffset(0, items);
- FileChannel channel = cacheFile.getChannel().position(offset);
- IntBuffer buffer = IntBuffer.wrap(currentItemSolutions);
- ByteBuffer byteBuffer = ByteBuffer.allocate(currentItemSolutions.length * BLOCK_SIZE);
- byteBuffer.asIntBuffer().put(buffer);
- channel.write(byteBuffer);
- channel.force(false);
- } catch (IOException e) {
- close();
- throw new RuntimeException(e);
- }
- this.previousItemSolutions = this.currentItemSolutions.clone();
- this.currentItemSolutions = new int[getMaxCapacity() + 1];
- syncsPassed++;
- }
Add Comment
Please, Sign In to add comment