FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ServerlessLibrary/LibraryStoreMigration/Program.cs at dev-react · Azure/ServerlessLibrary · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
This repository was archived by the owner on Nov 5, 2025. It is now read-only.
Azure
/
ServerlessLibrary
Public archive
Notifications
You must be signed in to change notification settings
Fork
27
Star
128
Code
Issues
17
Pull requests
9
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
ServerlessLibrary
/
LibraryStoreMigration
/
Program.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
135 lines (122 loc) · 5.31 KB
Breadcrumbs
ServerlessLibrary
/
LibraryStoreMigration
/
Program.cs
Copy path
File metadata and controls
135 lines (122 loc) · 5.31 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using
System
;
using
System
.
Reflection
;
using
System
.
IO
;
using
ServerlessLibrary
.
Models
;
using
System
.
Collections
.
Generic
;
using
Newtonsoft
.
Json
;
using
ServerlessLibrary
;
using
Microsoft
.
WindowsAzure
.
Storage
.
Table
;
using
Microsoft
.
WindowsAzure
.
Storage
;
using
Microsoft
.
WindowsAzure
.
Storage
.
RetryPolicies
;
using
System
.
Threading
.
Tasks
;
using
System
.
Linq
;
namespace
LibraryStoreMigration
{
class
Program
{
const
string
tableName
=
"slitemstats"
;
static
void
Main
(
string
[
]
args
)
{
if
(
args
.
Length
<
1
)
{
Console
.
WriteLine
(
"Please enter specify operation to be performed (cosmosdb|stats)"
)
;
Console
.
WriteLine
(
"Please note that connection informations need to be provided as environment variables."
)
;
return
;
}
if
(
args
[
0
]
.
Equals
(
"cosmosdb"
,
StringComparison
.
OrdinalIgnoreCase
)
)
{
MigrateToCosmosDB
(
)
;
}
if
(
args
[
0
]
.
Equals
(
"stats"
,
StringComparison
.
OrdinalIgnoreCase
)
)
{
AddNewStatsColumns
(
)
;
}
}
public
static
void
AddNewStatsColumns
(
)
{
TableRequestOptions
tableRequestRetry
=
new
TableRequestOptions
{
RetryPolicy
=
new
LinearRetry
(
TimeSpan
.
FromSeconds
(
2
)
,
3
)
}
;
TableQuery
<
NewSLItemStats
>
query
=
new
TableQuery
<
NewSLItemStats
>
(
)
;
TableContinuationToken
continuationToken
=
null
;
List
<
NewSLItemStats
>
entities
=
new
List
<
NewSLItemStats
>
(
)
;
CloudStorageAccount
storageAccount
=
CloudStorageAccount
.
Parse
(
ServerlessLibrarySettings
.
SLStorageString
)
;
CloudTableClient
tableClient
=
storageAccount
.
CreateCloudTableClient
(
)
;
CloudTable
table
=
tableClient
.
GetTableReference
(
tableName
)
;
var
opContext
=
new
OperationContext
(
)
;
do
{
TableQuerySegment
<
NewSLItemStats
>
queryResults
=
(
table
)
.
ExecuteQuerySegmentedAsync
(
query
,
continuationToken
,
tableRequestRetry
,
opContext
)
.
Result
;
continuationToken
=
queryResults
.
ContinuationToken
;
entities
.
AddRange
(
queryResults
.
Results
)
;
}
while
(
continuationToken
!=
null
)
;
Console
.
WriteLine
(
entities
.
Count
)
;
List
<
LibraryItem
>
libraryItems
=
GetAllLibraryItemsFromFile
(
)
;
foreach
(
var
entity
in
entities
)
{
entity
.
id
=
libraryItems
.
FirstOrDefault
(
l
=>
l
.
Template
==
entity
.
template
)
.
Id
;
entity
.
likes
=
0
;
entity
.
dislikes
=
0
;
TableOperation
operation
=
TableOperation
.
InsertOrMerge
(
entity
)
;
Task
<
TableResult
>
r
=
table
.
ExecuteAsync
(
operation
)
;
TableResult
a
=
r
.
Result
;
}
}
public
static
void
MigrateToCosmosDB
(
)
{
var
libraryItems
=
GetAllLibraryItemsFromFile
(
)
;
Console
.
WriteLine
(
"Number of samples to be migrated from file to cosmos db: {0}"
,
libraryItems
.
Count
)
;
CosmosLibraryStore
libraryStore
=
new
CosmosLibraryStore
(
)
;
IList
<
LibraryItem
>
libraryItemsInCosmos
=
libraryStore
.
GetAllItems
(
)
.
Result
;
Console
.
WriteLine
(
"Number of samples already present in cosmos db: {0}"
,
libraryItemsInCosmos
.
Count
)
;
if
(
libraryItemsInCosmos
.
Count
!=
libraryItems
.
Count
)
{
foreach
(
LibraryItem
libraryItem
in
libraryItems
)
{
if
(
!
libraryItemsInCosmos
.
Any
(
c
=>
c
.
Id
==
libraryItem
.
Id
)
)
{
Console
.
WriteLine
(
"Item {0} not present in cosmos db. will be migrated"
+
libraryItem
.
Id
)
;
try
{
libraryStore
.
Add
(
libraryItem
)
.
Wait
(
)
;
Console
.
WriteLine
(
"Migrated sample with id {0}"
+
libraryItem
.
Id
)
;
}
catch
(
Exception
ex
)
{
Console
.
WriteLine
(
"Got exception {0}"
,
ex
)
;
throw
;
}
}
}
Console
.
WriteLine
(
"Samples are successfully migrated to cosmos db"
)
;
}
else
{
Console
.
WriteLine
(
"Samples are already migrated to cosmos db"
)
;
}
}
public
static
List
<
LibraryItem
>
GetAllLibraryItemsFromFile
(
)
{
var
assembly
=
Assembly
.
GetExecutingAssembly
(
)
;
using
(
Stream
stream
=
assembly
.
GetManifestResourceStream
(
"LibraryStoreMigration.items.json"
)
)
using
(
StreamReader
reader
=
new
StreamReader
(
stream
)
)
{
string
result
=
reader
.
ReadToEnd
(
)
;
return
JsonConvert
.
DeserializeObject
<
List
<
LibraryItem
>
>
(
result
)
;
}
}
}
public
class
OldSLItemStats
:
TableEntity
{
public
string
template
{
get
;
set
;
}
public
int
totalDownloads
{
get
;
set
;
}
public
int
downloadsToday
{
get
;
set
;
}
public
int
downloadsThisWeek
{
get
;
set
;
}
public
int
downloadsThisMonth
{
get
;
set
;
}
public
DateTime
lastUpdated
{
get
;
set
;
}
}
public
class
NewSLItemStats
:
OldSLItemStats
{
public
string
id
{
get
;
set
;
}
public
int
likes
{
get
;
set
;
}
public
int
dislikes
{
get
;
set
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL