Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
incubos authored Oct 5, 2023
2 parents 17d2ad9 + 1d73716 commit d5b1d09
Show file tree
Hide file tree
Showing 115 changed files with 4,366 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
version: "2"
plugins:
duplication:
enabled: false
config:
languages:
java:
checkstyle:
enabled: true
config:
Expand Down
18 changes: 18 additions & 0 deletions .github/workflows/autoupdate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: autoupdate
on:
push:
branches:
- main
jobs:
autoupdate:
name: autoupdate
runs-on: ubuntu-22.04
steps:
- uses: docker://chinthakagodawita/autoupdate-action:v1
env:
GITHUB_TOKEN: '${{ secrets.GITHUB_TOKEN }}'
- run: echo 'Merge conflicts found!'
if: ${{ steps.autoupdate.outputs.conflicted }}
- run: echo 'No merge conflicts'
if: ${{ !steps.autoupdate.outputs.conflicted }}

5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ java {
repositories {
mavenCentral()
}

dependencies {
// Checks
errorprone 'com.google.errorprone:error_prone_core:2.11.0'
Expand All @@ -37,9 +38,11 @@ checkstyle {

compileJava {
options.compilerArgs += ["--enable-preview"]

// Enforce errors
options.compilerArgs += ["-Werror"]
}

compileTestJava {
options.compilerArgs += ["--enable-preview"]
}

1 change: 1 addition & 0 deletions src/main/java/ru/vk/itmo/Dao.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ default E get(D key) {
if (!iterator.hasNext()) {
return null;
}

E next = iterator.next();
if (next.key().equals(key)) {
return next;
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/ru/vk/itmo/abramovilya/InMemoryDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package ru.vk.itmo.abramovilya;

import ru.vk.itmo.Dao;
import ru.vk.itmo.Entry;

import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
import java.util.Iterator;
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;

public class InMemoryDao implements Dao<MemorySegment, Entry<MemorySegment>> {
private final ConcurrentNavigableMap<MemorySegment, Entry<MemorySegment>> map =
new ConcurrentSkipListMap<>(InMemoryDao::compareMemorySegments);

@Override
public Iterator<Entry<MemorySegment>> get(MemorySegment from, MemorySegment to) {
ConcurrentNavigableMap<MemorySegment, Entry<MemorySegment>> subMap;
if (from == null && to == null) {
subMap = map;
} else if (from == null) {
subMap = map.headMap(to);
} else if (to == null) {
subMap = map.tailMap(from);
} else {
subMap = map.subMap(from, to);
}
return subMap.values().iterator();
}

@Override
public void upsert(Entry<MemorySegment> entry) {
map.put(entry.key(), entry);
}

@Override
public Entry<MemorySegment> get(MemorySegment key) {
return map.get(key);
}

private static int compareMemorySegments(MemorySegment segment1, MemorySegment segment2) {
long offset = segment1.mismatch(segment2);
if (offset == -1) {
return 0;
} else if (offset == segment1.byteSize()) {
return -1;
} else if (offset == segment2.byteSize()) {
return 1;
}
return Byte.compare(
segment1.get(ValueLayout.JAVA_BYTE, offset),
segment2.get(ValueLayout.JAVA_BYTE, offset)
);
}
}
56 changes: 56 additions & 0 deletions src/main/java/ru/vk/itmo/bazhenovkirill/InMemoryDaoImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package ru.vk.itmo.bazhenovkirill;

import ru.vk.itmo.Dao;
import ru.vk.itmo.Entry;

import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
import java.util.Iterator;
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;

public class InMemoryDaoImpl implements Dao<MemorySegment, Entry<MemorySegment>> {

private final ConcurrentNavigableMap<MemorySegment, Entry<MemorySegment>> storage = new ConcurrentSkipListMap<>(
(ms1, ms2) -> {
long mismatch = ms1.mismatch(ms2);
if (mismatch == -1) {
return 0;
}
if (ms2.byteSize() == mismatch) {
return 1;
}
if (ms1.byteSize() == mismatch) {
return -1;
}
return Byte.compare(
ms1.get(ValueLayout.JAVA_BYTE, mismatch),
ms2.get(ValueLayout.JAVA_BYTE, mismatch));
}
);

@Override
public Iterator<Entry<MemorySegment>> get(MemorySegment from, MemorySegment to) {
if (from == null) {
if (to != null) {
return storage.headMap(to).values().iterator();
}
return storage.values().iterator();
}
if (to == null) {
return storage.tailMap(from).values().iterator();
}
return storage.subMap(from, true, to, false).values().iterator();
}

@Override
public Entry<MemorySegment> get(MemorySegment key) {
return storage.get(key);
}

@Override
public void upsert(Entry<MemorySegment> entry) {
storage.put(entry.key(), entry);
}

}
60 changes: 60 additions & 0 deletions src/main/java/ru/vk/itmo/belonogovnikolay/InMemoryTreeDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package ru.vk.itmo.belonogovnikolay;

import ru.vk.itmo.Dao;
import ru.vk.itmo.Entry;

import java.lang.foreign.MemorySegment;
import java.util.Iterator;
import java.util.NavigableMap;
import java.util.Objects;
import java.util.concurrent.ConcurrentSkipListMap;

public final class InMemoryTreeDao implements Dao<MemorySegment, Entry<MemorySegment>> {

private final NavigableMap<MemorySegment, Entry<MemorySegment>> arena;

private InMemoryTreeDao() {
this.arena = new ConcurrentSkipListMap<>(new MemorySegmentComparator());
}

public static Dao<MemorySegment, Entry<MemorySegment>> newInstance() {
return new InMemoryTreeDao();
}

@Override
public Iterator<Entry<MemorySegment>> allFrom(MemorySegment from) {
return this.arena.tailMap(from).values().iterator();
}

@Override
public Iterator<Entry<MemorySegment>> allTo(MemorySegment to) {
return this.arena.headMap(to).values().iterator();
}

@Override
public Iterator<Entry<MemorySegment>> get(MemorySegment from, MemorySegment to) {
if (Objects.isNull(from) && Objects.isNull(to)) {
return this.arena.values().iterator();
} else if (Objects.isNull(from)) {
return allTo(to);
} else if (Objects.isNull(to)) {
return allFrom(from);
}

return this.arena.subMap(from, to).values().iterator();
}

@Override
public Entry<MemorySegment> get(MemorySegment key) {
return this.arena.get(key);
}

@Override
public void upsert(Entry<MemorySegment> entry) {
if (Objects.isNull(entry)) {
return;
}

this.arena.put(entry.key(), entry);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ru.vk.itmo.belonogovnikolay;

import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
import java.util.Comparator;

public class MemorySegmentComparator implements Comparator<MemorySegment> {

@Override
public int compare(MemorySegment prevSegment, MemorySegment nextSegment) {

long offset = prevSegment.mismatch(nextSegment);

if (offset == nextSegment.byteSize()) {
return 1;
} else if (offset == prevSegment.byteSize()) {
return -1;
} else if (offset == -1) {
return 0;
}

return Byte.compare(prevSegment.get(ValueLayout.JAVA_BYTE, offset),
nextSegment.get(ValueLayout.JAVA_BYTE, offset));
}
}
49 changes: 49 additions & 0 deletions src/main/java/ru/vk/itmo/boturkhonovkamron/InMemoryDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package ru.vk.itmo.boturkhonovkamron;

import ru.vk.itmo.Dao;
import ru.vk.itmo.Entry;

import java.lang.foreign.MemorySegment;
import java.util.Iterator;
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;

/**
* Реализация интерфейса Dao для работы с объектами типа MemorySegment в памяти.
*
* @author Kamron Boturkhonov
* @since 2023.09.26
*/
public class InMemoryDao implements Dao<MemorySegment, Entry<MemorySegment>> {

final ConcurrentNavigableMap<MemorySegment, Entry<MemorySegment>> data;

public InMemoryDao() {
data = new ConcurrentSkipListMap<>(new MemorySegmentComparator());
}

@Override
public Entry<MemorySegment> get(final MemorySegment key) {
return data.get(key);
}

@Override
public Iterator<Entry<MemorySegment>> get(final MemorySegment from, final MemorySegment to) {
final ConcurrentNavigableMap<MemorySegment, Entry<MemorySegment>> subMap;
if (from == null && to == null) {
subMap = data;
} else if (from == null) {
subMap = data.headMap(to, false);
} else if (to == null) {
subMap = data.tailMap(from, true);
} else {
subMap = data.subMap(from, to);
}
return subMap.values().iterator();
}

@Override
public void upsert(final Entry<MemorySegment> entry) {
data.merge(entry.key(), entry, (oldValue, newValue) -> newValue);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ru.vk.itmo.boturkhonovkamron;

import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
import java.util.Comparator;

/**
* Реализация компаратора для сравнения объектов типа MemorySegment.
*
* @author Kamron Boturkhonov
* @since 2023.09.27
*/
public class MemorySegmentComparator implements Comparator<MemorySegment> {

@Override
public int compare(final MemorySegment left, final MemorySegment right) {
if (left == null || right == null) {
return left == null ? -1 : 1;
}
if (left.equals(right)) {
return 0;
}
final long leftSize = left.byteSize();
final long rightSize = right.byteSize();
final long mismatch = left.mismatch(right);
if (mismatch >= 0 && leftSize != mismatch && rightSize != mismatch) {
return Byte.compare(left.get(ValueLayout.JAVA_BYTE, mismatch),
right.get(ValueLayout.JAVA_BYTE, mismatch));
}
return Long.compare(leftSize, rightSize);
}
}
Loading

0 comments on commit d5b1d09

Please sign in to comment.