func Open(name string, disk disk.Disk) (*Manager, error) { f, err := shm.Open(name, os.O_RDWR) if err != nil { return nil, err } buckets, mmap, err := asBuckets(f) if err != nil { return nil, err } mgr := &Manager{ f: f, disk: disk, buckets: buckets, mmap: mmap, open: make(map[int]*openBucket), empty: make([]int, 0), } for _, bkt := range buckets { if bkt.state == EMPTY { mgr.empty = append(mgr.empty, bkt.idx) } } for len(mgr.open) < MaxOpen { mgr.open_new_bucket() } return mgr, nil }
func Create(name string, diskSize uint64) error { bucketCount := diskSize / BucketSize f, err := shm.Open(name, os.O_RDWR|os.O_CREATE|os.O_EXCL) if err != nil { return err } defer f.Close() err = f.Truncate(int64(bucketCount * uint64(unsafe.Sizeof(bucket{})))) if err != nil { return err } buckets, mmap, err := asBuckets(f) if err != nil { return err } for idx := range buckets { bkt := &buckets[idx] bkt.idx = idx bkt.state = EMPTY } return mmap.Sync(gommap.MS_SYNC) }
func Create(name string, n uint64) error { f, err := shm.Open(name, os.O_RDWR|os.O_CREATE|os.O_EXCL) if err != nil { return err } defer f.Close() err = f.Truncate(int64(n) * int64(unsafe.Sizeof(entry{}))) if err != nil { return err } return nil }
func Open(name string) (Table, error) { f, err := shm.Open(name, os.O_RDWR) if err != nil { return nil, err } arr, mmap, err := shm.MMapTyped(f, entry{}) if err != nil { return nil, err } entries, ok := arr.([]entry) if !ok { panic("failed to cast []byte to []entry") } return &table{ f: f, entries: entries, mmap: mmap, }, nil }