Skip to content

Instantly share code, notes, and snippets.

View SHSE's full-sized avatar

Sergey Shumov SHSE

  • Chime
  • Vancouver, Canada
View GitHub Profile
var semaphore = new SemaphoreSlim(5);
var pending = 0;
Action onCompleted = () => Interlocked.Decrement(ref pending);
for (var i = 0; i < 20; i++) {
await semaphore.WaitAsync();
Interlocked.Increment(ref pending);
int n = 1000000;
int intMax = 2147483647;
int chunkSize = intMax/n;
int resultIndex = 0;
int[] result = new int[n];
int lower = 0;
int currentChunkSize = chunkSize;
@SHSE
SHSE / gist:6791622
Last active December 24, 2015 11:29
int n = 1000000;
int intMax = 2147483647;
int k = intMax/n;
int resultIndex = 0;
int[n] result;
for (int i = 0; i < n; i++) {
double normalized = rand()/(double) RAND_MAX;
@SHSE
SHSE / AutoResetEventAsync.cs
Last active September 2, 2019 12:44
Asynchronous version of AutoResetEvent.
/// <summary>
/// Asynchronous version of <see cref="AutoResetEvent" />
/// </summary>
public sealed class AutoResetEventAsync {
private static readonly Task<bool> Completed = Task.FromResult(true);
private readonly ConcurrentQueue<TaskCompletionSource<bool>> handlers =
new ConcurrentQueue<TaskCompletionSource<bool>>();
private int isSet;
internal class Program {
private static void Main(string[] args) {
using (var documntStore = new DocumentStore {Url = "http://localhost:8080", DefaultDatabase = "test", Credentials = new NetworkCredential("<user>","<password>")}.Initialize())
using (var bulk = documntStore.BulkInsert()) {
bulk.Report += Console.WriteLine;
for (var i = 0; i < 80000000; i++) {
bulk.Store(new MyItem
{
Hash = Guid.NewGuid().ToString("N")
@SHSE
SHSE / gist:4620069
Last active December 11, 2015 15:18
using System;
using System.Collections.Generic;
using System.Linq;
using FluentNHibernate;
using FluentNHibernate.Automapping;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate.Linq;
using Environment = NHibernate.Cfg.Environment;
private static long FindPosition(Stream stream, byte[] bytes) {
var buffer = new byte[Math.Max(1024, bytes.Length)];
if (buffer.Length == 0)
return -1;
while (true) {
var count = stream.Read(buffer, 0, buffer.Length);
if (count < bytes.Length)
@SHSE
SHSE / gist:4351455
Last active December 10, 2015 00:29
namespace ConsoleApplication1
{
internal class Program
{
private static void Main(string[] args)
{
var idents = new[] {1, 2, 3, 4, 5, 6, 7};
var index = 0;
using (var documentStore = new EmbeddableDocumentStore {UseEmbeddedHttpServer = true}.Initialize())
@SHSE
SHSE / gist:4135346
Created November 23, 2012 12:11
RavenDB Build #2159-Unstable Crash StackTrace
[EsentTempPathInUseException: Temp path already used by another database instance]
Microsoft.Isam.Esent.Interop.Api.Check(Int32 err) in C:\Work\ravendb\SharedLibs\Sources\managedesent-61618\EsentInterop\Api.cs:2739
Microsoft.Isam.Esent.Interop.Api.JetInit(JET_INSTANCE& instance) in C:\Work\ravendb\SharedLibs\Sources\managedesent-61618\EsentInterop\Api.cs:131
Raven.Storage.Esent.TransactionalStorage.Initialize(IUuidGenerator uuidGenerator, OrderedPartCollection`1 documentCodecs) in c:\Builds\RavenDB-Unstable-v2.0\Raven.Database\Storage\Esent\TransactionalStorage.cs:302
[InvalidOperationException: Could not open transactional storage: C:\RavenDB\Web\RavenData\Data]
Raven.Storage.Esent.TransactionalStorage.Initialize(IUuidGenerator uuidGenerator, OrderedPartCollection`1 documentCodecs) in c:\Builds\RavenDB-Unstable-v2.0\Raven.Database\Storage\Esent\TransactionalStorage.cs:317
Raven.Database.DocumentDatabase..ctor(InMemoryRavenConfiguration configuration) in c:\Builds\RavenDB-Unstable-v2.0\Raven.D
public Task Save(IEnumerable<MyEntity> entities, CancellationToken cancellationToken) {
return Task.Factory.StartNew(() => {
foreach (var batch in Batch(entities, 20)) {
cancellationToken.ThrowIfCancellationRequested();
this.documentStore.DatabaseCommands.Batch(
from entity in batch
select new PutCommandData {
Document = RavenJObject.FromObject(entity),
Key = entity.Id,