Skip to content

Instantly share code, notes, and snippets.

@matteobertozzi
matteobertozzi / DemoVirtualThreads.java
Last active June 24, 2024 22:08
Demo Virtual Threads - Synchronous but concurrent code, like async/await
// Run with JDK 21
// $ java -Djdk.virtualThreadScheduler.parallelism=1 -Djava.util.concurrent.ForkJoinPool.common.parallelism=1 DemoVirtualThreads.java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.time.Duration;
import java.time.LocalDateTime;
@matteobertozzi
matteobertozzi / long-tasks.py
Created May 11, 2024 20:11
HTTP Long Tasks
#!/usr/bin/env bash
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
@matteobertozzi
matteobertozzi / sm.py
Created February 10, 2024 06:45
Amazon States Language Demo Execution
#!/usr/bin/env python3
# Amazon States Language - https://states-language.net/
import jsonpath_ng
from collections import deque, namedtuple
import asyncio
import time
import json
@matteobertozzi
matteobertozzi / nested-to-columnar.py
Created December 8, 2023 20:16
Nested Json to Columnar encoding/decoding (Demo)
# Data is transformed in a tree (parent_id, index)
# There are 3 types of objects: STRUCT, ARRAY, VALUES
# the encode() method returns a list of columns containing:
# (parent_id, index, name, nulls[], values[], type)
# for ARRAYs values[] contains the number of items per row
from dataclasses import is_dataclass, asdict as dataclass_as_dict, dataclass
import types
CALLABLES_TYPES = (types.FunctionType, types.MethodType)
@matteobertozzi
matteobertozzi / MostlyZeroEncoder.java
Created November 19, 2023 14:14
Encode an Array of numbers using a bitmap + int-pack4 (good for arrays with mostly zeros)
// Encode an Array of numbers using a bitmap + int-pack4
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import io.github.matteobertozzi.rednaco.bytes.encoding.IntDecoder;
import io.github.matteobertozzi.rednaco.bytes.encoding.IntEncoder;
import io.github.matteobertozzi.rednaco.bytes.encoding.IntUtil;
public final class MostlyZeroEncoder {
@matteobertozzi
matteobertozzi / DemoParquetWriter.java
Created November 11, 2023 14:41
Java Demo Parquet Writer (Simple)
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.List;
import org.apache.avro.Schema;
import org.apache.avro.Schema.Type;
import org.apache.avro.generic.GenericData;
@matteobertozzi
matteobertozzi / DemoVirtualThreads.java
Last active May 28, 2024 18:08
Java 21: Virtual Threads Behaviour Examples
// Run with JDK 21
// $ java -Djdk.virtualThreadScheduler.parallelism=2 -Djava.util.concurrent.ForkJoinPool.common.parallelism=2 DemoVirtualThreads.java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.time.Duration;
import java.util.concurrent.ExecutorService;
@matteobertozzi
matteobertozzi / dataclass_from_dict.py
Last active June 2, 2024 12:27
Python dataclass from dict
from dataclasses import dataclass, is_dataclass, asdict as dataclass_as_dict, fields as dataclass_fields
def dataclass_from_dict(cls, src):
kwargs = {}
fields_lookup = {field.name: field for field in dataclass_fields(cls)}
for field_name, value in src.items():
field = fields_lookup.get(field_name)
if not field:
#logger.warning('config field %s not found for class %s', field_name, cls.__name__)
continue
@matteobertozzi
matteobertozzi / test-gps.py
Created June 9, 2023 15:00
Read U-blox M9N GNSS from /dev/ttyS3
import serial # (ubuntu python3-serial package) - https://pyserial.readthedocs.io/en/latest/
def gps_read():
# nROK 1031 Built-in U-blox M9N GNSS is on /dev/ttyS3 (Ubuntu 22.04 LTS)
with serial.Serial('/dev/ttyS3', baudrate=38400) as sd:
sd.flushInput()
sd.flushOutput()
while True:
yield sd.readline()
@matteobertozzi
matteobertozzi / 1_otp.ts
Last active June 5, 2024 19:04
Generate Time Based OTP in Javascript/Typescript, Python, Java
async function generateOneTimePassword(rawKey: Uint8Array, counter: number): Promise<number> {
const data = new DataView(new ArrayBuffer(8));
data.setBigUint64(0, BigInt(Math.floor(counter)), false);
const algo = { name: 'HMAC', hash: 'SHA-1' };
const key = await crypto.subtle.importKey('raw', rawKey, algo, false, ['sign']);
const hmacHash = new Uint8Array(await crypto.subtle.sign(algo, key, data.buffer));
const offset = hmacHash[hmacHash.byteLength - 1] & 0x0f;
const hotp = (hmacHash[offset] & 0x7f) << 24