Skip to content

Instantly share code, notes, and snippets.

@kid-cavaquinho
kid-cavaquinho / normalizervsanalyzer.json
Created March 20, 2023 08:54
elasticsearch normalizer and analyzers
PUT index-test
{
"settings": {
"analysis": {
"normalizer": {
"my_normalizer": {
"type": "custom",
"char_filter": [],
"filter": ["lowercase", "asciifolding"]
}
@kid-cavaquinho
kid-cavaquinho / docker-compose.yml
Created March 8, 2023 19:08
opensearch cluster docker
# Run `docker compose up`
# If in Windows while running WSL open the powershell and run `wsl -d docker-desktop` then `sysctl -w vm.max_map_count=262144` to increase memory limits
# Create an index: `curl -X PUT "https://localhost:9200/test00001?pretty"`
# Add test data to the previously created index: `curl -X POST "https://localhost:9200/test00001/_bulk?pretty&refresh" -H "Content-Type: application/x-ndjson" --data-binary @"test-data.json" -u "admin:admin" --insecure`
version: '3'
services:
opensearch-node1:
image: opensearchproject/opensearch:latest
container_name: opensearch-node1
@kid-cavaquinho
kid-cavaquinho / Gzip.cs
Created December 7, 2022 08:29
Gzip compression/decompression
using System.IO.Compression;
public static class Gzip
{
private static async Task<byte[]> DecompressBytesAsync(byte[] bytes, CancellationToken cancel = default)
{
using var inputStream = new MemoryStream(bytes);
using var outputStream = new MemoryStream();
await using (var compressionStream = new GZipStream(inputStream, CompressionMode.Decompress))
{
// The Manhattan distance between two points is the sum of absolute difference of the coordinates.
static int Resolve(int M, int N, int X1, int Y1, int X2, int Y2)
{
return Math.Abs(X2 - X1) + Math.Abs(Y2 - Y1);
}
public static void Main()
{
// Define size of 2-D array
services.Configure<ApiBehaviorOptions>(options =>
{
options.InvalidModelStateResponseFactory = actionContext =>
{
var problemDetails = new ValidationProblemDetails
{
Detail = "For big problem!",
Title = "Get a big localized solution!",
};
return new BadRequestObjectResult(problemDetails);
public class ModelBindingFailureFilterSimple : IActionFilter
{
private readonly ILogger _logger;
public ModelBindingFailureFilterSimple(ILogger logger)
{
_logger = logger;
}
public void OnActionExecuted(ActionExecutedContext context)
{
public class ModelBindingFailureFilter : IActionFilter
{
public void OnActionExecuted(ActionExecutedContext context)
{
}
public void OnActionExecuting(ActionExecutingContext context)
{
if (!context.ModelState.IsValid)
{
@kid-cavaquinho
kid-cavaquinho / StringCalculator.cs
Last active February 26, 2020 22:47
String Calculator Kata (http://osherove.com/tdd-kata-1) This was my first iteration for 15 minutes.
public class StringCalculator
{
private const string DefaultDelimiters = ",\n";
public int Add(string numbers)
{
if (string.IsNullOrWhiteSpace(numbers))
{
return 0;
}
@kid-cavaquinho
kid-cavaquinho / WKTGeometryValidation.sql
Last active October 25, 2018 08:10
Validate WKT geometries in SQL
DECLARE @valid BIT
DECLARE @geometryWKT NVARCHAR(MAX)
DECLARE @geometry GEOMETRY
SET @geometryWKT = 'LINESTRING(264424.9652 569222.1755, 264391.5552 569306.8028, 264347.5728 569370.1507, 264279.3844 569496.5162, 264151.5842 569779.2783, 264102.1571 569876.3473, 264064.9039 569909.1248, 264082.4099 569934.902, 264148.7657 569980.9312, 264197.7566 570045.2707, 264264.2537 570126.3715, 264351.5345 570242.1204, 264461.6298 570397.3514, 264508.7931 570471.0041, 264540.9409 570500.789, 264585.7437 570474.7547, 264663.5564 570446.4651, 264710.7298 570389.8719, 264861.704 570149.3973, 264802.3649 570104.5195, 264579.9936 570005.1748, 264774.1915 569593.5737, 264627.7661 569530.0202, 264604.2309 569589.8227, 264583.3815 569693.6588, 264564.4047 569744.5479, 264545.5456 569781.413, 264516.4291 569824.5021, 264497.7144 569849.1252, 264385.1763 569780.4476, 264471.0827 569602.4976, 264410.3238 569572.5101, 264548.9528 569291.7813, 264424.9652 569222.1755)'-- Test faulty value
SET @valid = 1
BEGIN TRY
public interface IValidatable
{
(IEnumerable<string> results, bool valid) Validate();
}