Skip to content

Instantly share code, notes, and snippets.

View TheDIM47's full-sized avatar

Dmitry Koudryavtsev TheDIM47

View GitHub Profile
@TheDIM47
TheDIM47 / little_endian.rs
Created February 14, 2025 16:38
A utility class for handling little-endian numbers
use std::io::Read;
use byteorder::{ByteOrder, ReadBytesExt};
/// a utility class for handling little-endian numbers, which the 80x86 world is
/// replete with. The methods are all static, and input/output is from/to byte
/// arrays, or from InputStreams.
/// Source: https://github.com/apache/poi/blob/trunk/poi/src/main/java/org/apache/poi/util/LittleEndian.java
pub struct LittleEndian;
@TheDIM47
TheDIM47 / file_magic.rs
Created February 14, 2025 16:26
Rust: The file magic number, i.e. the file identification based on the first bytes of the file
use std::collections::HashMap;
use super::{header_block_constants::HeaderBlockConstants, little_endian::LittleEndian};
/// The file magic number, i.e. the file identification based on the first bytes of the file
/// Source: https://github.com/apache/poi/blob/trunk/poi/src/main/java/org/apache/poi/poifs/filesystem/FileMagic.java
#[derive(Debug, PartialEq, Eq, Hash)]
struct Magic(Vec<Vec<u8>>);
#[derive(Debug, PartialEq)]
@TheDIM47
TheDIM47 / cqrs.rs
Created January 21, 2025 11:22
CQRS
// https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=65bf7ea271639b39204804a5351a96a3
//! ## Task Description
//!
//! The goal is to develop a backend service for shortening URLs using CQRS
//! (Command Query Responsibility Segregation) and ES (Event Sourcing)
//! approaches. The service should support the following features:
//!
//! ## Functional Requirements
//!
import scala.math.Ordering.Implicits.infixOrderingOps
/** simple interval [min, max) (or >= min if max is empty) */
final class Interval[A](val min: A, val max: Option[A])(implicit num: Numeric[A]) extends Serializable {
/** if max nonEmpty, then max > min */
require(testRightBound(max, min))
def in(value: A): Boolean =
value >= min && testRightBound(max, value)
@TheDIM47
TheDIM47 / Slf4jLogger.scala
Created March 25, 2022 17:46
scala: slf4j logger for doobie
package util
import doobie.LogHandler
import doobie.util.log.{ExecFailure, ProcessingFailure, Success}
import org.slf4j.LoggerFactory
object Slf4jLogger {
val slf4jLogHandler: LogHandler = {
val logger = LoggerFactory.getLogger(getClass.getName)
// RHO, CompileRoutesSuite
sealed trait Foo extends Product with Serializable
case class Bar(i: Int) extends Foo
case class Baz(v: UUID) extends Foo
test("A CompileService should make routes from a collection of RhoRoutes 2") {
import org.http4s.rho.bits.StringParser.booleanParser
import org.http4s.rho.bits.StringParser.uuidParser
import scala.reflect.runtime.universe.TypeTag
@TheDIM47
TheDIM47 / MySqlCatalog.java
Created October 19, 2021 09:47
MySqlCatalog - Flink MySQL catalog implementation
import org.apache.flink.connector.jdbc.catalog.AbstractJdbcCatalog;
import org.apache.flink.connector.jdbc.table.JdbcConnectorOptions;
import org.apache.flink.connector.jdbc.table.JdbcDynamicTableFactory;
import org.apache.flink.table.api.DataTypes;
import org.apache.flink.table.api.Schema;
import org.apache.flink.table.api.ValidationException;
import org.apache.flink.table.api.constraints.UniqueConstraint;
import org.apache.flink.table.catalog.CatalogBaseTable;
import org.apache.flink.table.catalog.CatalogDatabase;
import org.apache.flink.table.catalog.CatalogDatabaseImpl;
@TheDIM47
TheDIM47 / MySqlTablePath.java
Created October 19, 2021 09:47
MySqlTablePath - Flink MySQL catalog implementation
import org.apache.flink.util.StringUtils;
import java.util.Objects;
import static org.apache.flink.util.Preconditions.checkArgument;
public class MySqlTablePath {
private static final String DEFAULT_SCHEMA_NAME = "public";
private final String schemaName;
private final String tableName;
@TheDIM47
TheDIM47 / flatten.scala
Created August 28, 2021 17:55
flatten benchmarks
package benchmarks
import org.openjdk.jmh.annotations._
import scala.util.Random
import java.util.concurrent.TimeUnit
import scala.annotation.tailrec
/*
[info] Benchmark (size) Mode Cnt Score Error Units
/**
* Flood Fill
* Given a matrix a containing numbers that represents a
* labyrinth with empty space and walls, as follows:
* 0 is empty space, 1 is a wall, and 2 is water.
*
* Write a program that fills the labyrinth with water starting
* from a given position given by coordinates x and y,
* The top-left coordinate of the matrix is 0,0.
* ```