public void AddSignature(string name, int level, Action <Stream> action) { var signatureData = storage.Signatures.GetIndex(Tables.Signatures.Indices.Data); var signaturesByName = storage.Signatures.GetIndex(Tables.Signatures.Indices.ByName); var id = IdGenerator.GetNextIdForTable(storage.Signatures); var key = CreateKey(id); var signature = new RavenJObject { { "id", id }, { "name", name }, { "level", level }, { "created_at", DateTime.UtcNow } }; var stream = CreateStream(); action(stream); stream.Position = 0; storage.Signatures.Add(writeBatch.Value, key, signature, 0); signatureData.Add(writeBatch.Value, key, stream, 0); signaturesByName.MultiAdd(writeBatch.Value, CreateKey(name), key); }
public int InsertPage(byte[] buffer, int size) { var hashKey = new HashKey(buffer, size); var key = (Slice)ConvertToKey(hashKey); var pageByKey = storage.Pages.GetIndex(Tables.Pages.Indices.ByKey); var pageData = storage.Pages.GetIndex(Tables.Pages.Indices.Data); var result = pageByKey.Read(Snapshot, key, writeBatch.Value); if (result != null) { var id = (Slice)result.Reader.ToStringValue(); ushort version; var page = LoadJson(storage.Pages, id, writeBatch.Value, out version); if (page == null) { throw new InvalidOperationException(string.Format("Could not find page '{0}'. Probably data is corrupted.", id)); } var usageCount = page.Value <int>("usage_count"); page["usage_count"] = usageCount + 1; storage.Pages.Add(writeBatch.Value, id, page, version); return(page.Value <int>("id")); } var newPageId = IdGenerator.GetNextIdForTable(storage.Pages); var newPageKeyString = CreateKey(newPageId); var newPageKey = (Slice)newPageKeyString; var newPage = new RavenJObject { { "id", newPageId }, { "page_strong_hash", hashKey.Strong }, { "page_weak_hash", hashKey.Weak }, { "usage_count", 1 } }; storage.Pages.Add(writeBatch.Value, newPageKey, newPage, 0); var dataStream = CreateStream(); using (var finalDataStream = fileCodecs.Aggregate((Stream) new UndisposableStream(dataStream), (current, codec) => codec.EncodePage(current))) { finalDataStream.Write(buffer, 0, size); finalDataStream.Flush(); } dataStream.Position = 0; pageData.Add(writeBatch.Value, newPageKey, dataStream, 0); pageByKey.Add(writeBatch.Value, key, newPageKeyString); return(newPageId); }
private void CopyUsage(string sourceFilename, string targetFilename, bool commitPeriodically) { var oldKey = CreateKey(sourceFilename); var newKey = CreateKey(targetFilename); var usageByFileName = storage.Usage.GetIndex(Tables.Usage.Indices.ByFileName); var usageByFileNameAndPosition = storage.Usage.GetIndex(Tables.Usage.Indices.ByFileNameAndPosition); using (var iterator = usageByFileName.MultiRead(Snapshot, oldKey)) { if (!iterator.Seek(Slice.BeforeAllKeys)) { return; } var count = 0; do { var usageId = iterator.CurrentKey.ToString(); ushort version; var usage = LoadJson(storage.Usage, usageId, writeBatch.Value, out version); var newId = IdGenerator.GetNextIdForTable(storage.Usage); var position = usage.Value <int>("file_pos"); var pageId = usage.Value <int>("page_id"); var newUsage = new RavenJObject { { "id", newId }, { "name", targetFilename }, { "file_pos", position }, { "page_id", pageId }, { "page_size", usage.Value <int>("page_size") } }; var newUsageId = CreateKey(newId); storage.Usage.Add(writeBatch.Value, newUsageId, newUsage); usageByFileName.MultiAdd(writeBatch.Value, newKey, newUsageId); usageByFileNameAndPosition.Add(writeBatch.Value, CreateKey(targetFilename, position), newUsageId); IncrementUsageCount(pageId); if (commitPeriodically && count++ > 1000) { PulseTransaction(); count = 0; } }while (iterator.MoveNext()); } }
public void AssociatePage(string filename, int pageId, int pagePositionInFile, int pageSize, bool incrementUsageCount = false) { var usageByFileName = storage.Usage.GetIndex(Tables.Usage.Indices.ByFileName); var usageByFileNameAndPosition = storage.Usage.GetIndex(Tables.Usage.Indices.ByFileNameAndPosition); var key = CreateKey(filename); var keySlice = (Slice)key; ushort version; var file = LoadFileByKey(key, out version); var totalSize = file.Value <long?>("total_size"); var uploadedSize = file.Value <int>("uploaded_size"); if (totalSize != null && totalSize >= 0 && uploadedSize + pageSize > totalSize) { throw new InvalidDataException("Try to upload more data than the file was allocated for (" + totalSize + ") and new size would be: " + (uploadedSize + pageSize)); } file["uploaded_size"] = uploadedSize + pageSize; // using chunked encoding, we don't know what the size is // we use negative values here for keeping track of the unknown size if (totalSize == null || totalSize < 0) { var actualSize = totalSize ?? 0; file["total_size"] = actualSize - pageSize; } storage.Files.Add(writeBatch.Value, keySlice, file, version); var id = IdGenerator.GetNextIdForTable(storage.Usage); var usageKey = CreateKey(id); var usageKeySlice = (Slice)usageKey; var usage = new RavenJObject { { "id", id }, { "name", filename }, { "file_pos", pagePositionInFile }, { "page_id", pageId }, { "page_size", pageSize } }; storage.Usage.Add(writeBatch.Value, usageKeySlice, usage, 0); usageByFileName.MultiAdd(writeBatch.Value, (Slice)CreateKey(filename), usageKeySlice); usageByFileNameAndPosition.Add(writeBatch.Value, (Slice)CreateKey(filename, pagePositionInFile), usageKey, 0); if (incrementUsageCount) { IncrementUsageCount(pageId); } }
public int InsertPage(byte[] buffer, int size) { var hashKey = new HashKey(buffer, size); var key = ConvertToKey(hashKey); var pageByKey = storage.Pages.GetIndex(Tables.Pages.Indices.ByKey); var pageData = storage.Pages.GetIndex(Tables.Pages.Indices.Data); var result = pageByKey.Read(Snapshot, key, writeBatch.Value); if (result != null) { var id = result.Reader.ToStringValue(); ushort version; var page = LoadJson(storage.Pages, id, writeBatch.Value, out version); if (page == null) { throw new InvalidOperationException(string.Format("Could not find page '{0}'. Probably data is corrupted.", id)); } var usageCount = page.Value <int>("usage_count"); page["usage_count"] = usageCount + 1; storage.Pages.Add(writeBatch.Value, id, page, version); return(page.Value <int>("id")); } var newId = IdGenerator.GetNextIdForTable(storage.Pages); var newKey = CreateKey(newId); var newPage = new RavenJObject { { "id", newId }, { "page_strong_hash", hashKey.Strong }, { "page_weak_hash", hashKey.Weak }, { "usage_count", 1 } }; storage.Pages.Add(writeBatch.Value, newKey, newPage, 0); pageData.Add(writeBatch.Value, newKey, buffer, 0); pageByKey.Add(writeBatch.Value, key, newKey); return(newId); }