#include "batch.h" #include typedef struct { size_t length; unsigned char bytes[STORE_CAPACITY]; } saved_slot; static void take_snapshot(saved_slot *saved) { int slot; for (slot = 0; slot < STORE_SLOTS; ++slot) { unsigned char *span = store_span(slot); saved[slot].length = store_length(slot); if (saved[slot].length) { memcpy(saved[slot].bytes, span, saved[slot].length); } } } static void restore_snapshot(const saved_slot *saved) { int slot; for (slot = 0; slot < STORE_SLOTS; ++slot) { store_write(slot, saved[slot].bytes, saved[slot].length); } } static int apply_one(const batch_operation *operation) { switch (operation->kind) { case BATCH_WRITE: if (operation->length > STORE_CAPACITY) { return STORE_TOO_LONG; } return store_write(operation->slot, operation->data, operation->length); case BATCH_APPEND: if (operation->length > STORE_CAPACITY) { return STORE_TOO_LONG; } return store_append(operation->slot, operation->data, operation->length); case BATCH_PATCH: if (operation->length > STORE_CAPACITY) { return STORE_BAD_RANGE; } return store_patch(operation->slot, operation->offset, operation->data, operation->length); case BATCH_ERASE: return store_erase(operation->slot, operation->offset, operation->length); case BATCH_COPY: return store_copy(operation->slot, operation->peer); case BATCH_REVERSE: return store_reverse(operation->slot); case BATCH_ROTATE: return store_rotate(operation->slot, operation->offset); case BATCH_XOR: return store_xor(operation->slot, operation->value); case BATCH_FILL: return store_fill(operation->slot, operation->value, operation->length); case BATCH_SWAP: { unsigned char temp[STORE_CAPACITY]; size_t first, second; unsigned char *a = store_span(operation->slot); unsigned char *b = store_span(operation->peer); if (a == NULL || b == NULL) { return STORE_BAD_SLOT; } if (operation->slot == operation->peer) { return STORE_OK; } first = store_length(operation->slot); second = store_length(operation->peer); if (first) { memcpy(temp, a, first); } store_write(operation->slot, b, second); store_write(operation->peer, temp, first); return STORE_OK; } default: return STORE_BAD_RANGE; } } int batch_apply(const batch_operation *operations, size_t count, size_t *failed_at) { saved_slot saved[STORE_SLOTS]; size_t index; int result; if (failed_at == NULL) { return STORE_BAD_RANGE; } *failed_at = count; if (count > BATCH_LIMIT || (count && operations == NULL)) { *failed_at = 0; return STORE_BAD_RANGE; } take_snapshot(saved); for (index = 0; index < count; ++index) { result = apply_one(&operations[index]); if (result != STORE_OK) { restore_snapshot(saved); *failed_at = index; return result; } } return STORE_OK; } uint32_t batch_fingerprint(void) { uint32_t hash = 2166136261u; int slot; for (slot = 0; slot < STORE_SLOTS; ++slot) { size_t index, length = store_length(slot); unsigned char *data = store_span(slot); hash ^= (unsigned char)slot; hash *= 16777619u; hash ^= (unsigned char)length; hash *= 16777619u; for (index = 0; index < length; ++index) { hash ^= data[index]; hash *= 16777619u; } } return hash; } int batch_export(int slot, unsigned char *output, size_t capacity, size_t *written) { size_t length; unsigned char *data; if (written == NULL || output == NULL) { return STORE_BAD_RANGE; } *written = 0; data = store_span(slot); if (data == NULL) { return STORE_BAD_SLOT; } length = store_length(slot); if (capacity < length + 1) { return STORE_TOO_LONG; } output[0] = (unsigned char)length; if (length) { memcpy(output + 1, data, length); } *written = length + 1; return STORE_OK; } int batch_import(int slot, const unsigned char *input, size_t length) { size_t claimed; if (input == NULL || length == 0) { return STORE_BAD_RANGE; } claimed = input[0]; if (claimed > STORE_CAPACITY || length != claimed + 1) { return STORE_BAD_RANGE; } return store_write(slot, input + 1, claimed); } int batch_slice(int slot, size_t offset, size_t length, unsigned char *output, size_t capacity, size_t *written) { unsigned char *data; size_t available; if (output == NULL || written == NULL) { return STORE_BAD_RANGE; } *written = 0; data = store_span(slot); if (data == NULL) { return STORE_BAD_SLOT; } available = store_length(slot); if (offset > available || length > available - offset) { return STORE_BAD_RANGE; } if (capacity < length) { return STORE_TOO_LONG; } if (length) { memmove(output, data + offset, length); } *written = length; return STORE_OK; }