forked from ravendb/ravendb
/
MappedResultsStorageAction.cs
118 lines (109 loc) · 4.21 KB
/
MappedResultsStorageAction.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//-----------------------------------------------------------------------
// <copyright file="MappedResultsStorageAction.cs" company="Hibernating Rhinos LTD">
// Copyright (c) Hibernating Rhinos LTD. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.IO;
using Raven.Abstractions;
using Raven.Abstractions.Extensions;
using Raven.Database.Impl;
using Raven.Database.Storage;
using Raven.Json.Linq;
using Raven.Storage.Managed.Impl;
using System.Linq;
using Raven.Database.Json;
namespace Raven.Storage.Managed
{
public class MappedResultsStorageAction : IMappedResultsStorageAction
{
private readonly TableStorage storage;
private readonly IUuidGenerator generator;
public MappedResultsStorageAction(TableStorage storage, IUuidGenerator generator)
{
this.storage = storage;
this.generator = generator;
}
public void PutMappedResult(string view, string docId, string reduceKey, RavenJObject data, byte[] viewAndReduceKeyHashed)
{
var ms = new MemoryStream();
data.WriteTo(ms);
var byteArray = generator.CreateSequentialUuid().ToByteArray();
var key = new RavenJObject
{
{"view", view},
{"reduceKey", reduceKey},
{"docId", docId},
{"etag", byteArray},
{"timestamp", SystemTime.Now}
};
storage.MappedResults.Put(key, ms.ToArray());
}
public IEnumerable<RavenJObject> GetMappedResults(params GetMappedResultsParams[] getMappedResultsParams)
{
return getMappedResultsParams.SelectMany(GetMappedResults);
}
public IEnumerable<RavenJObject> GetMappedResults(GetMappedResultsParams getMappedResultsParams)
{
return storage.MappedResults["ByViewAndReduceKey"].SkipTo(new RavenJObject
{
{"view", getMappedResultsParams.View},
{"reduceKey", getMappedResultsParams.ReduceKey}
}).TakeWhile(x => StringComparer.InvariantCultureIgnoreCase.Equals(x.Value<string>("view"), getMappedResultsParams.View) &&
StringComparer.InvariantCultureIgnoreCase.Equals(x.Value<string>("reduceKey"), getMappedResultsParams.ReduceKey))
.Select(x =>
{
var readResult = storage.MappedResults.Read(x);
if (readResult == null)
return null;
return readResult.Data().ToJObject();
}).Where(x => x != null);
}
public IEnumerable<string> DeleteMappedResultsForDocumentId(string documentId, string view)
{
foreach (var key in storage.MappedResults["ByViewAndDocumentId"].SkipTo(new RavenJObject
{
{"view", view},
{"docId", documentId}
}).TakeWhile(x => StringComparer.InvariantCultureIgnoreCase.Equals(x.Value<string>("view"), view) &&
StringComparer.InvariantCultureIgnoreCase.Equals(x.Value<string>("docId"), documentId)))
{
storage.MappedResults.Remove(key);
yield return key.Value<string>("reduceKey");
}
}
public void DeleteMappedResultsForView(string view)
{
foreach (var key in storage.MappedResults["ByViewAndReduceKey"].SkipTo(new RavenJObject{{"view", view}})
.TakeWhile(x => StringComparer.InvariantCultureIgnoreCase.Equals(x.Value<string>("view"), view)))
{
storage.MappedResults.Remove(key);
}
}
public IEnumerable<MappedResultInfo> GetMappedResultsReduceKeysAfter(string indexName, Guid lastReducedEtag, bool loadData)
{
return storage.MappedResults["ByViewAndEtagDesc"]
// the index is sorted view ascending and then etag descending
// we index before this index, then backward toward the last one.
.SkipBefore(new RavenJObject { { "view", indexName }, {"etag", lastReducedEtag.ToByteArray()}})
.TakeWhile(x => StringComparer.InvariantCultureIgnoreCase.Equals(x.Value<string>("view"), indexName))
.Select(key =>
{
var mappedResultInfo = new MappedResultInfo
{
ReduceKey = key.Value<string>("reduceKey"),
Etag = new Guid(key.Value<byte[]>("etag")),
Timestamp = key.Value<DateTime>("timestamp"),
};
if(loadData)
{
var readResult = storage.MappedResults.Read(key);
if (readResult != null)
mappedResultInfo.Data = readResult.Data().ToJObject();
}
return mappedResultInfo;
});
}
}
}