Commit c57fac16 authored by 黄大凯's avatar 黄大凯
Browse files

Initial filelist caching system

parent f90238ee
Loading
Loading
Loading
Loading

filelist/cache.go

0 → 100644
+56 −0
Original line number Diff line number Diff line
package filelist

import (
	"google.golang.org/protobuf/proto"
	"rsync2os/rsync"
	bolt "go.etcd.io/bbolt"
)

type Cache struct {
	db *bolt.DB
	module *bolt.Bucket
	prepath string
}

func Open(module string, prepath string) {
	db, err := bolt.Open("r.db", 0666, nil)
	if err != nil {
		//return err
	}
	tx, err := db.Begin(true)
	defer tx.Rollback()

	_, err = tx.CreateBucketIfNotExists([]byte(module))
	if err := tx.Commit(); err != nil {

	}
}

func (cache *Cache) Put(info *rsync.FileInfo) error {
	key := []byte(cache.prepath+info.Path)
	value, err := proto.Marshal(&FInfo{
		Size: info.Size,
		Mtime: info.Mtime,
		Mode: info.Mode,
	})
	if err != nil {
		return err
	}
	return cache.module.Put(key, value)
}

func (cache *Cache) Get(key []byte) *FInfo {
	value := cache.module.Get(key)
	if value != nil {
		info := &FInfo{}
		err := proto.Unmarshal(value, info)
		if err == nil {
			return info
		}
	}
	return nil
}

func Save(list *rsync.FileList) {

}

filelist/diff.go

0 → 100644
+8 −0
Original line number Diff line number Diff line
package filelist

import "rsync2os/rsync"

// Return two lists: new files, deleted files
func (cache *Cache) Diff(list *rsync.FileList) (*rsync.FileList, *rsync.FileList) {
	return nil, nil
}
 No newline at end of file

filelist/finfo.pb.go

0 → 100644
+166 −0
Original line number Diff line number Diff line
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// 	protoc-gen-go v1.24.0
// 	protoc        v3.12.0
// source: finfo.proto

package filelist

import (
	proto "github.com/golang/protobuf/proto"
	protoreflect "google.golang.org/protobuf/reflect/protoreflect"
	protoimpl "google.golang.org/protobuf/runtime/protoimpl"
	reflect "reflect"
	sync "sync"
)

const (
	// Verify that this generated code is sufficiently up-to-date.
	_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
	// Verify that runtime/protoimpl is sufficiently up-to-date.
	_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)

// This is a compile-time assertion that a sufficiently up-to-date version
// of the legacy proto package is being used.
const _ = proto.ProtoPackageIsVersion4

type FInfo struct {
	state         protoimpl.MessageState
	sizeCache     protoimpl.SizeCache
	unknownFields protoimpl.UnknownFields

	Size  int64 `protobuf:"varint,1,opt,name=Size,proto3" json:"Size,omitempty"`
	Mtime int32 `protobuf:"varint,2,opt,name=Mtime,proto3" json:"Mtime,omitempty"`
	Mode  int32 `protobuf:"varint,3,opt,name=Mode,proto3" json:"Mode,omitempty"` // hash 16bytes
}

func (x *FInfo) Reset() {
	*x = FInfo{}
	if protoimpl.UnsafeEnabled {
		mi := &file_finfo_proto_msgTypes[0]
		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
		ms.StoreMessageInfo(mi)
	}
}

func (x *FInfo) String() string {
	return protoimpl.X.MessageStringOf(x)
}

func (*FInfo) ProtoMessage() {}

func (x *FInfo) ProtoReflect() protoreflect.Message {
	mi := &file_finfo_proto_msgTypes[0]
	if protoimpl.UnsafeEnabled && x != nil {
		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
		if ms.LoadMessageInfo() == nil {
			ms.StoreMessageInfo(mi)
		}
		return ms
	}
	return mi.MessageOf(x)
}

// Deprecated: Use FInfo.ProtoReflect.Descriptor instead.
func (*FInfo) Descriptor() ([]byte, []int) {
	return file_finfo_proto_rawDescGZIP(), []int{0}
}

func (x *FInfo) GetSize() int64 {
	if x != nil {
		return x.Size
	}
	return 0
}

func (x *FInfo) GetMtime() int32 {
	if x != nil {
		return x.Mtime
	}
	return 0
}

func (x *FInfo) GetMode() int32 {
	if x != nil {
		return x.Mode
	}
	return 0
}

var File_finfo_proto protoreflect.FileDescriptor

var file_finfo_proto_rawDesc = []byte{
	0x0a, 0x0b, 0x66, 0x69, 0x6e, 0x66, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x05, 0x70,
	0x72, 0x6f, 0x74, 0x6f, 0x22, 0x45, 0x0a, 0x05, 0x46, 0x49, 0x6e, 0x66, 0x6f, 0x12, 0x12, 0x0a,
	0x04, 0x53, 0x69, 0x7a, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x04, 0x53, 0x69, 0x7a,
	0x65, 0x12, 0x14, 0x0a, 0x05, 0x4d, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05,
	0x52, 0x05, 0x4d, 0x74, 0x69, 0x6d, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x4d, 0x6f, 0x64, 0x65, 0x18,
	0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x4d, 0x6f, 0x64, 0x65, 0x42, 0x25, 0x5a, 0x23, 0x67,
	0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6b, 0x61, 0x69, 0x61, 0x6b, 0x7a,
	0x2f, 0x72, 0x73, 0x79, 0x6e, 0x63, 0x32, 0x6f, 0x73, 0x2f, 0x66, 0x69, 0x6c, 0x65, 0x6c, 0x69,
	0x73, 0x74, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}

var (
	file_finfo_proto_rawDescOnce sync.Once
	file_finfo_proto_rawDescData = file_finfo_proto_rawDesc
)

func file_finfo_proto_rawDescGZIP() []byte {
	file_finfo_proto_rawDescOnce.Do(func() {
		file_finfo_proto_rawDescData = protoimpl.X.CompressGZIP(file_finfo_proto_rawDescData)
	})
	return file_finfo_proto_rawDescData
}

var file_finfo_proto_msgTypes = make([]protoimpl.MessageInfo, 1)
var file_finfo_proto_goTypes = []interface{}{
	(*FInfo)(nil), // 0: proto.FInfo
}
var file_finfo_proto_depIdxs = []int32{
	0, // [0:0] is the sub-list for method output_type
	0, // [0:0] is the sub-list for method input_type
	0, // [0:0] is the sub-list for extension type_name
	0, // [0:0] is the sub-list for extension extendee
	0, // [0:0] is the sub-list for field type_name
}

func init() { file_finfo_proto_init() }
func file_finfo_proto_init() {
	if File_finfo_proto != nil {
		return
	}
	if !protoimpl.UnsafeEnabled {
		file_finfo_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
			switch v := v.(*FInfo); i {
			case 0:
				return &v.state
			case 1:
				return &v.sizeCache
			case 2:
				return &v.unknownFields
			default:
				return nil
			}
		}
	}
	type x struct{}
	out := protoimpl.TypeBuilder{
		File: protoimpl.DescBuilder{
			GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
			RawDescriptor: file_finfo_proto_rawDesc,
			NumEnums:      0,
			NumMessages:   1,
			NumExtensions: 0,
			NumServices:   0,
		},
		GoTypes:           file_finfo_proto_goTypes,
		DependencyIndexes: file_finfo_proto_depIdxs,
		MessageInfos:      file_finfo_proto_msgTypes,
	}.Build()
	File_finfo_proto = out.File
	file_finfo_proto_rawDesc = nil
	file_finfo_proto_goTypes = nil
	file_finfo_proto_depIdxs = nil
}

filelist/finfo.proto

0 → 100644
+11 −0
Original line number Diff line number Diff line
syntax = "proto3";
package proto;

option go_package = "github.com/kaiakz/rsync2os/filelist";

message FInfo {
    int64 Size = 1;
    int32 Mtime = 2;
    int32 Mode = 3;
    // hash 16bytes
}
 No newline at end of file

filelist/pbgen.sh

0 → 100644
+2 −0
Original line number Diff line number Diff line
protoc -I=.. --go_out=. --go_opt=paths=source_relative finfo.proto
# protoc -I=filelist/ --go_out=filelist/ --go_opt=paths=source_relative filelist/finfo.proto
 No newline at end of file
Loading