Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
dgraph
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Mirror
dgraph
Commits
ef947c7b
Commit
ef947c7b
authored
9 years ago
by
Manish R Jain
Browse files
Options
Downloads
Patches
Plain Diff
Start working on an append only commit log.
parent
d671f4a9
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
commit/log.go
+97
-0
97 additions, 0 deletions
commit/log.go
commit/log_test.go
+47
-0
47 additions, 0 deletions
commit/log_test.go
with
144 additions
and
0 deletions
commit/log.go
0 → 100644
+
97
−
0
View file @
ef947c7b
/*
* Copyright 2015 Manish R Jain <manishrjain@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// commit package provides commit logs for storing mutations, as they arrive
// at the server. Mutations also get stored in memory within posting.List.
// So, commit logs are useful to handle machine crashes, and re-init of a
// posting list.
// This package provides functionality to write to a rotating log, and a way
// to quickly filter relevant entries corresponding to an attribute.
package
commit
import
(
"container/list"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"github.com/dgraph-io/dgraph/x"
)
var
glog
=
x
.
Log
(
"commitlog"
)
type
logFile
struct
{
sync
.
RWMutex
startTs
uint64
f
*
os
.
File
size
uint64
}
type
Logger
struct
{
// Directory to store logs into.
dir
string
// Prefix all filenames with this.
filePrefix
string
// MaxSize is the maximum size of commit log file in bytes,
// before it gets rotated.
maxSize
uint64
flistm
sync
.
RWMutex
flist
*
list
.
List
}
func
NewLogger
(
dir
string
,
fileprefix
string
,
maxSize
uint64
)
*
Logger
{
l
:=
new
(
Logger
)
l
.
dir
=
dir
l
.
filePrefix
=
fileprefix
l
.
maxSize
=
maxSize
l
.
flist
=
list
.
New
()
return
l
}
func
(
l
*
Logger
)
handleFile
(
path
string
,
info
os
.
FileInfo
,
err
error
)
error
{
if
info
.
IsDir
()
{
return
nil
}
if
!
strings
.
HasPrefix
(
info
.
Name
(),
l
.
filePrefix
+
"-"
)
{
return
nil
}
if
!
strings
.
HasSuffix
(
info
.
Name
(),
".log"
)
{
return
nil
}
lidx
:=
strings
.
LastIndex
(
info
.
Name
(),
".log"
)
tstring
:=
info
.
Name
()[
len
(
l
.
filePrefix
)
+
1
:
lidx
]
glog
.
WithField
(
"log_ts"
,
tstring
)
.
Debug
(
"Found log."
)
return
nil
}
func
(
l
*
Logger
)
Init
()
{
if
err
:=
filepath
.
Walk
(
l
.
dir
,
l
.
handleFile
);
err
!=
nil
{
glog
.
WithError
(
err
)
.
Fatal
(
"While walking over directory"
)
}
}
func
(
l
*
Logger
)
filepath
(
ts
int64
)
string
{
return
fmt
.
Sprintf
(
"%s-%s.log"
,
l
.
filePrefix
,
strconv
.
FormatInt
(
ts
,
16
))
}
func
(
l
*
Logger
)
AddLog
(
ts
uint64
,
hash
uint32
,
value
[]
byte
)
{
}
This diff is collapsed.
Click to expand it.
commit/log_test.go
0 → 100644
+
47
−
0
View file @
ef947c7b
/*
* Copyright 2015 Manish R Jain <manishrjain@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
commit
import
(
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
)
func
TestHandleFile
(
t
*
testing
.
T
)
{
dir
,
err
:=
ioutil
.
TempDir
(
""
,
"dgraph-log"
)
if
err
!=
nil
{
t
.
Error
(
err
)
return
}
defer
os
.
RemoveAll
(
dir
)
l
:=
NewLogger
(
dir
,
"dgraph"
,
50
<<
20
)
ts
:=
time
.
Now
()
.
UnixNano
()
for
i
:=
0
;
i
<
10
;
i
++
{
ts
+=
1
fp
:=
filepath
.
Join
(
dir
,
l
.
filepath
(
ts
))
if
err
:=
ioutil
.
WriteFile
(
fp
,
[]
byte
(
"test calling"
),
os
.
ModeAppend
);
err
!=
nil
{
t
.
Error
(
err
)
return
}
}
l
.
Init
()
}
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