Пример #1
0
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
}
Пример #2
0
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
0
func TestTransaction(t *testing.T) {
	qs, w, _ := makeTestStore(simpleGraph)
	size := qs.Size()

	tx := graph.NewTransaction()
	tx.AddQuad(quad.Make(
		"E",
		"follows",
		"G",
		""))
	tx.RemoveQuad(quad.Make(
		"Non",
		"existent",
		"quad",
		""))

	err := w.ApplyTransaction(tx)
	if err == nil {
		t.Error("Able to remove a non-existent quad")
	}
	if size != qs.Size() {
		t.Error("Appended a new quad in a failed transaction")
	}
}
Пример #4
0
func TestTransaction(t *testing.T) {
	qs, w, _ := makeTestStore(simpleGraph)
	size := qs.Size()

	tx := graph.NewTransaction()
	tx.AddQuad(quad.Quad{
		Subject:   "E",
		Predicate: "follows",
		Object:    "G",
		Label:     ""})
	tx.RemoveQuad(quad.Quad{
		Subject:   "Non",
		Predicate: "existent",
		Object:    "quad",
		Label:     ""})

	err := w.ApplyTransaction(tx)
	if err == nil {
		t.Error("Able to remove a non-existent quad")
	}
	if size != qs.Size() {
		t.Error("Appended a new quad in a failed transaction")
	}
}