本文整理匯總了Golang中github.com/google/cayley.Quad函數的典型用法代碼示例。如果您正苦於以下問題:Golang Quad函數的具體用法?Golang Quad怎麽用?Golang Quad使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Quad函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: pruneLocks
// pruneLocks removes every expired locks from the database
func pruneLocks() {
now := time.Now()
// Delete every expired locks
it, _ := cayley.StartPath(store, "locked").In("locked").Save("locked_until", "locked_until").Save("locked_by", "locked_by").BuildIterator().Optimize()
defer it.Close()
for cayley.RawNext(it) {
tags := make(map[string]graph.Value)
it.TagResults(tags)
n := store.NameOf(it.Result())
t := store.NameOf(tags["locked_until"])
o := store.NameOf(tags["locked_by"])
tt, _ := strconv.ParseInt(t, 10, 64)
if now.Unix() > tt {
log.Debugf("lock %s owned by %s has expired.", n, o)
tr := cayley.NewTransaction()
tr.RemoveQuad(cayley.Quad(n, "locked", "locked", ""))
tr.RemoveQuad(cayley.Quad(n, "locked_until", t, ""))
tr.RemoveQuad(cayley.Quad(n, "locked_by", o, ""))
err := store.ApplyTransaction(tr)
if err != nil {
log.Errorf("failed transaction (pruneLocks): %s", err)
continue
}
log.Debugf("lock %s has been successfully pruned.", n)
}
}
if it.Err() != nil {
log.Errorf("failed query in Unlock: %s", it.Err())
}
}
示例2: WriteData
func (nd *Node) WriteData(data []byte, offset int64) error {
if offset%BLOCK_SIZE != 0 {
return errors.New(fmt.Sprintf("%d is not a valid offset for block size %d", offset, BLOCK_SIZE))
}
hash := Hash(data)
transaction := graph.NewTransaction()
// Determine if we already have a block for this offset
linkName := fmt.Sprint("offset-", offset)
if existingBlockHash := nd.BlockWithOffset(offset); existingBlockHash != "" {
transaction.RemoveQuad(cayley.Quad(nd.Id, linkName, string(existingBlockHash), ""))
}
transaction.AddQuad(cayley.Quad(nd.Id, linkName, hash, ""))
if err := nd.graph.ApplyTransaction(transaction); err != nil {
return err
}
if _, err := Write(hash, data); err != nil {
return err
}
return nil
}
示例3: UpdateFlag
// UpdateFlag creates a flag or update an existing flag's value
func UpdateFlag(name, value string) error {
if name == "" || value == "" {
log.Warning("could not insert a flag which has an empty name or value")
return cerrors.NewBadRequestError("could not insert a flag which has an empty name or value")
}
// Initialize transaction
t := cayley.NewTransaction()
// Get current flag value
currentValue, err := GetFlagValue(name)
if err != nil {
return err
}
// Build transaction
name = "flag:" + name
if currentValue != "" {
t.RemoveQuad(cayley.Quad(name, "value", currentValue, ""))
}
t.AddQuad(cayley.Quad(name, "value", value, ""))
// Apply transaction
if err = store.ApplyTransaction(t); err != nil {
log.Errorf("failed transaction (UpdateFlag): %s", err)
return ErrTransaction
}
// Return
return nil
}
示例4: pruneLocks
// pruneLocks removes every expired locks from the database
func pruneLocks() {
now := time.Now()
// Delete every expired locks
tr := cayley.NewTransaction()
it, _ := cayley.StartPath(store, "locked").In("locked").Save("locked_until", "locked_until").Save("locked_by", "locked_by").BuildIterator().Optimize()
defer it.Close()
for cayley.RawNext(it) {
tags := make(map[string]graph.Value)
it.TagResults(tags)
n := store.NameOf(it.Result())
t := store.NameOf(tags["locked_until"])
o := store.NameOf(tags["locked_by"])
tt, _ := strconv.ParseInt(t, 10, 64)
if now.Unix() > tt {
log.Debugf("Lock %s owned by %s has expired.", n, o)
tr.RemoveQuad(cayley.Quad(n, "locked", "locked", ""))
tr.RemoveQuad(cayley.Quad(n, "locked_until", t, ""))
tr.RemoveQuad(cayley.Quad(n, "locked_by", o, ""))
}
}
store.ApplyTransaction(tr)
}
示例5: Lock
// Lock tries to set a temporary lock in the database.
// If a lock already exists with the given name/owner, then the lock is renewed
//
// Lock does not block, instead, it returns true and its expiration time
// is the lock has been successfully acquired or false otherwise
func Lock(name string, duration time.Duration, owner string) (bool, time.Time) {
pruneLocks()
until := time.Now().Add(duration)
untilString := strconv.FormatInt(until.Unix(), 10)
// Try to get the expiration time of a lock with the same name/owner
currentExpiration, err := toValue(cayley.StartPath(store, name).Has("locked_by", owner).Out("locked_until"))
if err == nil && currentExpiration != "" {
// Renew our lock
if currentExpiration == untilString {
return true, until
}
t := cayley.NewTransaction()
t.RemoveQuad(cayley.Quad(name, "locked_until", currentExpiration, ""))
t.AddQuad(cayley.Quad(name, "locked_until", untilString, ""))
// It is not necessary to verify if the lock is ours again in the transaction
// because if someone took it, the lock's current expiration probably changed and the transaction will fail
return store.ApplyTransaction(t) == nil, until
}
t := cayley.NewTransaction()
t.AddQuad(cayley.Quad(name, "locked", "locked", "")) // Necessary to make the transaction fails if the lock already exists (and has not been pruned)
t.AddQuad(cayley.Quad(name, "locked_until", untilString, ""))
t.AddQuad(cayley.Quad(name, "locked_by", owner, ""))
glog.SetStderrThreshold("FATAL")
success := store.ApplyTransaction(t) == nil
glog.SetStderrThreshold("ERROR")
return success, until
}
示例6: removeNode
func (ng *NodeGraph) removeNode(nd *Node) (err error) {
if len(nd.Children()) > 0 {
return errors.New("Can't delete node with children, must delete children first")
}
transaction := cayley.NewTransaction()
if nd.Mode() > 0 {
transaction.RemoveQuad(cayley.Quad(nd.Id, modeLink, fmt.Sprint(int(nd.Mode())), ""))
}
if !nd.MTime().IsZero() {
transaction.RemoveQuad(cayley.Quad(nd.Id, mTimeLink, fmt.Sprint(nd.MTime().Format(timeFormat)), ""))
}
if nd.Name() != "" {
transaction.RemoveQuad(cayley.Quad(nd.Id, nameLink, nd.Name(), ""))
}
if nd.Parent() != nil {
transaction.RemoveQuad(cayley.Quad(nd.Id, parentLink, nd.Parent().Id, ""))
}
if nd.Type() != "" {
transaction.RemoveQuad(cayley.Quad(nd.Id, typeLink, nd.Type(), ""))
}
err = ng.ApplyTransaction(transaction)
if err == nil {
nd.mode = os.FileMode(0)
nd.mTime = time.Time{}
nd.name = ""
nd.parentId = ""
nd.Id = ""
}
return
}
示例7: Unlock
// Unlock unlocks a lock specified by its name if I own it
func Unlock(name, owner string) {
unlocked := 0
it, _ := cayley.StartPath(store, name).Has("locked", "locked").Has("locked_by", owner).Save("locked_until", "locked_until").BuildIterator().Optimize()
defer it.Close()
for cayley.RawNext(it) {
tags := make(map[string]graph.Value)
it.TagResults(tags)
t := cayley.NewTransaction()
t.RemoveQuad(cayley.Quad(name, "locked", "locked", ""))
t.RemoveQuad(cayley.Quad(name, "locked_until", store.NameOf(tags["locked_until"]), ""))
t.RemoveQuad(cayley.Quad(name, "locked_by", owner, ""))
err := store.ApplyTransaction(t)
if err != nil {
log.Errorf("failed transaction (Unlock): %s", err)
}
unlocked++
}
if it.Err() != nil {
log.Errorf("failed query in Unlock: %s", it.Err())
}
if unlocked > 1 {
// We should never see this, it would mean that our database doesn't ensure quad uniqueness
// and that the entire lock system is jeopardized.
log.Errorf("found inconsistency in Unlock: matched %d times a locked named: %s", unlocked, name)
}
}
示例8: save
func (nd *Node) save() (err error) {
if nd.name == "" && nd.Name() == "" {
return errors.New("Cannot add nameless file")
} else if nd.Parent() == nil && nd.parentId == "" && nd.Id != RootNodeId {
return errors.New("Cannot add file without parent")
}
staleQuads := graph.NewTransaction()
newQuads := graph.NewTransaction()
if name := nd.Name(); nd.name != name && name != "" && nd.name != "" {
staleQuads.RemoveQuad(cayley.Quad(nd.Id, nameLink, name, ""))
}
if nd.name != "" && nd.name != nd.Name() {
newQuads.AddQuad(cayley.Quad(nd.Id, nameLink, nd.name, ""))
nd.name = ""
}
if mimeType := nd.Type(); nd.mimeType != mimeType && mimeType != "" && nd.mimeType != "" {
staleQuads.RemoveQuad(cayley.Quad(nd.Id, typeLink, mimeType, ""))
}
if nd.mimeType != "" && nd.mimeType != nd.Type() {
newQuads.AddQuad(cayley.Quad(nd.Id, typeLink, nd.mimeType, ""))
nd.mimeType = ""
}
if mode := int(nd.Mode()); int(nd.mode) != mode && mode != 0 && int(nd.mode) != 0 {
staleQuads.RemoveQuad(cayley.Quad(nd.Id, modeLink, fmt.Sprint(mode), ""))
}
if int(nd.mode) > 0 && nd.mode != nd.Mode() {
newQuads.AddQuad(cayley.Quad(nd.Id, modeLink, fmt.Sprint(int(nd.mode)), ""))
nd.mode = os.FileMode(0)
}
if mTime := nd.MTime(); nd.mTime != mTime && !mTime.IsZero() && !nd.mTime.IsZero() {
staleQuads.RemoveQuad(cayley.Quad(nd.Id, mTimeLink, nd.MTime().Format(timeFormat), ""))
}
if !nd.mTime.IsZero() && nd.mTime != nd.MTime() {
newQuads.AddQuad(cayley.Quad(nd.Id, mTimeLink, nd.mTime.Format(timeFormat), ""))
nd.mTime = time.Time{}
}
if parent := nd.Parent(); parent != nil && parent.Id != nd.parentId && nd.parentId != "" {
staleQuads.RemoveQuad(cayley.Quad(nd.Id, parentLink, nd.Parent().Id, ""))
} else if parent != nil && parent.Id == nd.parentId {
nd.parentId = ""
}
if nd.parentId != "" {
newQuads.AddQuad(cayley.Quad(nd.Id, parentLink, nd.parentId, ""))
nd.parentId = ""
}
if err = nd.graph.ApplyTransaction(staleQuads); err != nil {
return
} else if err = nd.graph.ApplyTransaction(newQuads); err != nil {
return
}
return nil
}
示例9: MarkNotificationAsSent
// MarkNotificationAsSent marks a notification as sent.
func MarkNotificationAsSent(node string) {
// Initialize transaction
t := cayley.NewTransaction()
t.RemoveQuad(cayley.Quad(node, "isSent", strconv.FormatBool(false), ""))
t.AddQuad(cayley.Quad(node, "isSent", strconv.FormatBool(true), ""))
// Apply transaction
store.ApplyTransaction(t)
}
示例10: submitLink
/*
Calls the extractArticleData package to obtain Article Data and then adds the information to the database
*/
func submitLink(w http.ResponseWriter, r *http.Request, store *cayley.Handle) {
data := extractArticleData.GetDataFromArticle(r.FormValue("UploadLink"))
store.AddQuad(cayley.Quad(data.Link, "has_parent", data.Parent, ""))
store.AddQuad(cayley.Quad(data.Link, "has_date", data.Date, ""))
store.AddQuad(cayley.Quad(data.Link, "has_content", data.Content, ""))
store.AddQuad(cayley.Quad(data.Link, "has_title", data.Title, ""))
store.AddQuad(cayley.Quad(data.Link, "has_description", data.Description, ""))
store.AddQuad(cayley.Quad(data.Link, "has_image", data.Image, ""))
for _, entity := range data.Entities {
store.AddQuad(cayley.Quad(data.Link, "has_entity", entity.Name, ""))
for _, class := range entity.Classes {
store.AddQuad(cayley.Quad(entity.Name, "has_class", class, ""))
}
for _, category := range entity.Categories {
store.AddQuad(cayley.Quad(entity.Name, "has_category", category, ""))
}
}
}
示例11: main
func main() {
//graph.InitQuadStore("bolt", dbPath, nil)
store, err := cayley.NewGraph("bolt", dbPath, nil)
if err != nil {
fmt.Println("error in creating database", err)
}
err = store.AddQuad(cayley.Quad("food", "is", "good", ""))
// if err != nil {
// fmt.Println("write error", err)
// }
err = store.AddQuad(cayley.Quad("nothing", "is", "good", ""))
}
示例12: SaveUser
func (s *Storage) SaveUser(u *User) {
s.AddQuad(cayley.Quad(u.Name, "is", "user", ""))
p := cayley.StartPath(u.getStorage(), u.Name).Out("free at")
iterationTime := u.IterationTime()
it := p.BuildIterator()
for cayley.RawNext(it) {
s.RemoveQuad(cayley.Quad(u.Name, "free at", s.NameOf(it.Result()), ""))
}
s.AddQuad(cayley.Quad(u.Name, "free at", strconv.FormatInt(iterationTime, 10), ""))
}
示例13: AddUser
func (db Db) AddUser(r io.Reader) (string, error) {
uuid := uuid.NewUUID()
if uuid == nil {
log.Fatal("uuid is nil")
}
fmt.Println(uuid)
decoder := json.NewDecoder(r)
var u User
err := decoder.Decode(&u)
if err != nil {
return "", fmt.Errorf("invalid json")
}
fmt.Println(u)
ok := validEmail(u.Email)
if !ok {
return "", fmt.Errorf("invalid email")
}
db.Store.AddQuad(cayley.Quad("person:"+uuid.String(), "type", "Person", ""))
// store.AddQuad(cayley.Quad("person:UUID", "email", u.email, ""))
// store.AddQuad(cayley.Quad("person:UUID", "password", u.password, ""))
return db.location, nil
}
示例14: Unlock
// Unlock unlocks a lock specified by its name if I own it
func Unlock(name, owner string) {
pruneLocks()
t := cayley.NewTransaction()
it, _ := cayley.StartPath(store, name).Has("locked", "locked").Has("locked_by", owner).Save("locked_until", "locked_until").BuildIterator().Optimize()
defer it.Close()
for cayley.RawNext(it) {
tags := make(map[string]graph.Value)
it.TagResults(tags)
t.RemoveQuad(cayley.Quad(name, "locked", "locked", ""))
t.RemoveQuad(cayley.Quad(name, "locked_until", store.NameOf(tags["locked_until"]), ""))
t.RemoveQuad(cayley.Quad(name, "locked_by", owner, ""))
}
store.ApplyTransaction(t)
}
示例15: LinkServiceToAPI
func (s *localSignallingLayer) LinkServiceToAPI(serviceIdentifier string, version string, apiVersion string) error {
var err = s.base.LinkServiceToAPI(serviceIdentifier, version, apiVersion)
if err == nil {
var apiNodeID = utils.CreateAPIVersionKey(apiVersion)
var serviceVersionNodeID = utils.CreateServiceVersionKey(serviceIdentifier, version)
err = s.graph.AddQuad(cayley.Quad(apiNodeID, utils.ContainsVersionRel, serviceVersionNodeID, ""))
if err != nil {
return err
}
err = s.graph.AddQuad(cayley.Quad(serviceVersionNodeID, utils.InApiRel, apiNodeID, ""))
if err != nil {
return err
}
}
return err
}