Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
liber
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ale
liber
Commits
9e122e3d
Commit
9e122e3d
authored
7 years ago
by
ale
Browse files
Options
Downloads
Patches
Plain Diff
Preliminary support for db migrations
parent
9d2cd925
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
database.go
+53
-16
53 additions, 16 deletions
database.go
with
53 additions
and
16 deletions
database.go
+
53
−
16
View file @
9e122e3d
...
...
@@ -50,9 +50,22 @@ func (id BookId) String() string {
}
func
(
id
BookId
)
Key
()
[]
byte
{
var
buf
bytes
.
Buffer
binary
.
Write
(
&
buf
,
binary
.
LittleEndian
,
id
)
return
buf
.
Bytes
()
var
b
[
8
]
byte
binary
.
LittleEndian
.
PutUint64
(
b
[
:
],
uint64
(
id
))
return
b
[
:
]
}
func
NewID
()
BookId
{
return
BookId
(
rand
.
Int63
())
}
func
ParseID
(
s
string
)
BookId
{
id
,
_
:=
strconv
.
ParseUint
(
s
,
10
,
64
)
return
BookId
(
id
)
}
func
ParseBinaryID
(
b
[]
byte
)
BookId
{
return
BookId
(
binary
.
LittleEndian
.
Uint64
(
b
))
}
type
Book
struct
{
...
...
@@ -85,15 +98,6 @@ func init() {
rand
.
Seed
(
seed
)
}
func
NewID
()
BookId
{
return
BookId
(
rand
.
Int63
())
}
func
ParseID
(
s
string
)
BookId
{
id
,
_
:=
strconv
.
ParseUint
(
s
,
10
,
64
)
return
BookId
(
id
)
}
// The structure that gets actually indexed.
type
flatBook
struct
{
Title
string
`json:"title"`
...
...
@@ -251,6 +255,42 @@ func (db *Database) setupIndex(path string) error {
return
nil
}
var
schemaVersionKey
=
[]
byte
(
"_liber_schema_version"
)
func
(
db
*
Database
)
getSchemaVersion
()
uint64
{
data
,
err
:=
db
.
ldb
.
Get
(
schemaVersionKey
,
nil
)
if
err
!=
nil
{
return
0
}
return
binary
.
LittleEndian
.
Uint64
(
data
)
}
func
(
db
*
Database
)
setSchemaVersion
(
v
uint64
)
{
var
b
[
8
]
byte
binary
.
LittleEndian
.
PutUint64
(
b
[
:
],
v
)
db
.
ldb
.
Put
(
schemaVersionKey
,
b
[
:
],
nil
)
}
type
databaseMigration
struct
{
version
uint64
run
func
(
db
*
Database
)
error
}
func
(
db
*
Database
)
runMigrations
(
migrations
[]
databaseMigration
)
error
{
version
:=
db
.
getSchemaVersion
()
for
_
,
m
:=
range
migrations
{
if
m
.
version
<
version
{
continue
}
if
err
:=
m
.
run
(
db
);
err
!=
nil
{
return
err
}
version
=
m
.
version
db
.
setSchemaVersion
(
version
)
}
return
nil
}
func
(
db
*
Database
)
Close
()
{
db
.
index
.
Close
()
db
.
ldb
.
Close
()
...
...
@@ -570,10 +610,7 @@ func keyToId(key []byte) BookId {
if
n
<
0
{
return
0
}
var
id
uint64
binary
.
Read
(
bytes
.
NewReader
(
key
[
n
+
1
:
]),
binary
.
LittleEndian
,
&
id
)
return
BookId
(
id
)
return
ParseBinaryID
(
key
[
n
+
1
:
])
}
func
keyRange
(
prefix
[]
byte
)
([]
byte
,
[]
byte
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment