This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static bool find_smallest_nearby_buffer(struct range_finder *restrict range) { | |
struct selected_range current = {0, SIZE_MAX}; | |
size_t pickiness_boundary = | |
(range->index + 1) * 64 + | |
// the bigger the request range, the less we care about locality | |
+range->size_required; | |
while (find_next(range)) { | |
if (range->size_required == range->selection.size_available) | |
return true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct selected_range { | |
size_t position; | |
size_t size_available; | |
}; | |
struct range_finder { | |
// input | |
uint64_t *bitmap; | |
size_t bitmap_size; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void init_range(uint64_t *bitmap, size_t bitmap_size, | |
size_t size_required, | |
struct range_finder *restrict range) { | |
range->bitmap = bitmap; | |
range->bitmap_size = bitmap_size; | |
range->size_required = size_required; | |
range->current = bitmap[0]; | |
range->previous_set_bit = ULONG_MAX; | |
range->index = 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static bool handle_zero_word(struct range_finder *restrict range) { | |
range->current_set_bit = (range->index + 1) * 64; | |
if (range->current_set_bit > range->previous_set_bit + range->size_required) { | |
range->selected_position = | |
range->previous_set_bit + 1; // intentionally overflowing here | |
range->size_available = (range->current_set_bit - range->selected_position); | |
return true; | |
} | |
return false; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct range_finder { | |
// input | |
uint64_t *bitmap; | |
size_t bitmap_size; | |
size_t size_required; | |
size_t index; | |
// output | |
size_t size_available; | |
size_t selected_position; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static bool find_clear_range(uint64_t* bitmap, size_t bitmapsize, | |
size_t start_search, size_t size_requested, | |
size_t* bit_pos, size_t* available_size){ | |
uint64_t bitset; | |
uint64_t previous_set_bit = ULONG_MAX; | |
uint64_t current; | |
size_t k = start_search/64; | |
if(k >= bitmapsize) | |
return false; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Net.Sockets; | |
using System.Threading.Tasks; | |
using Nito.AsyncEx; | |
using Raven.Client.Documents; | |
using System.Reactive.Linq; | |
using System.Threading; | |
using Raven.Client.Documents.Indexes; | |
using Raven.Client.Documents.Linq.Indexing; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using (var session = store.OpenAsyncSession(new SessionOptions | |
{ | |
TransactionMode = TransactionMode.ClusterWide | |
})) | |
{ | |
var src = await session.LoadAsync<Account>("accounts/123-A", | |
x => x.IncludeCompareExchangeValue(a=>a.PublicKey)); | |
var dst = await session.LoadAsync<Account>("accounts/456-B", | |
x => x.IncludeCompareExchangeValue(a=>a.PublicKey)); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using (var session = store.OpenAsyncSession()) | |
{ | |
var src = await session.LoadAsync<Account>("accounts/123-A"); | |
var dst = await session.LoadAsync<Account>("accounts/456-B"); | |
if(src.Blocked || dst.Blocked) | |
throw new InvalidOperationException("Unable to transfer money from/to blocked accounts"); | |
if(src.Funds < 5) | |
throw new InvalidOperationException("Insufficient funds"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class SessionExtensions | |
{ | |
public static async Task<(T Entity, string ChangeVector)?> ConditionalLoadAsync<T>(this IAsyncDocumentSession session, | |
string id, string changeVector, CancellationToken token = default) | |
{ | |
if (session.Advanced.IsLoaded(id)) | |
{ | |
var e = await session.LoadAsync<T>(id, token); | |
var cv = session.Advanced.GetChangeVectorFor(e); | |
return (e, cv); |